Code Duplication    Length = 102-106 lines in 2 locations

lib/api_client.js 2 locations

@@ 1151-1256 (lines=106) @@
1148
    return deferred.promise;
1149
};
1150
1151
APIClient.prototype._createNewWalletV2 = function(options) {
1152
    var self = this;
1153
1154
    var deferred = q.defer();
1155
1156
    // avoid modifying passed options
1157
    options = _.merge({}, options);
1158
1159
    determineDataStorageV2_3(options)
1160
        .then(function(options) {
1161
            options.passphrase = options.passphrase || options.password;
1162
            delete options.password;
1163
1164
            // avoid deprecated options
1165
            if (options.primaryPrivateKey) {
1166
                throw new blocktrail.WalletInitError("Can't specify; Primary PrivateKey");
1167
            }
1168
1169
            // seed should be provided or generated
1170
            options.primarySeed = options.primarySeed || randomBytes(Wallet.WALLET_ENTROPY_BITS / 8);
1171
1172
            return options;
1173
        })
1174
        .then(function(options) {
1175
            return produceEncryptedDataV2(options, deferred.notify.bind(deferred));
1176
        })
1177
        .then(function(options) {
1178
            return doRemainingWalletDataV2_3(options, self.network, deferred.notify.bind(deferred));
1179
        })
1180
        .then(function(options) {
1181
            // create a checksum of our private key which we'll later use to verify we used the right password
1182
            var checksum = options.primaryPrivateKey.getAddress();
1183
            var keyIndex = options.keyIndex;
1184
1185
            // send the public keys and encrypted data to server
1186
            return self.storeNewWalletV2(
1187
                options.identifier,
1188
                [options.primaryPublicKey.toBase58(), "M/" + keyIndex + "'"],
1189
                [options.backupPublicKey.toBase58(), "M"],
1190
                options.storeDataOnServer ? options.encryptedPrimarySeed : false,
1191
                options.storeDataOnServer ? options.encryptedSecret : false,
1192
                options.storeDataOnServer ? options.recoverySecret : false,
1193
                checksum,
1194
                keyIndex,
1195
                options.support_secret || null,
1196
                options.segwit || null
1197
            )
1198
                .then(
1199
                function(result) {
1200
                    deferred.notify(APIClient.CREATE_WALLET_PROGRESS_INIT);
1201
1202
                    var blocktrailPublicKeys = _.mapValues(result.blocktrail_public_keys, function(blocktrailPublicKey) {
1203
                        return bitcoin.HDNode.fromBase58(blocktrailPublicKey[0], self.network);
1204
                    });
1205
1206
                    var wallet = new Wallet(
1207
                        self,
1208
                        options.identifier,
1209
                        Wallet.WALLET_VERSION_V2,
1210
                        null,
1211
                        options.storeDataOnServer ? options.encryptedPrimarySeed : null,
1212
                        options.storeDataOnServer ? options.encryptedSecret : null,
1213
                        {keyIndex: options.primaryPublicKey},
1214
                        options.backupPublicKey,
1215
                        blocktrailPublicKeys,
1216
                        keyIndex,
1217
                        result.segwit || 0,
1218
                        self.testnet,
1219
                        checksum,
1220
                        result.upgrade_key_index,
1221
                        options.bypassNewAddressCheck
1222
                    );
1223
1224
                    // pass along decrypted data to avoid extra work
1225
                    return wallet.unlock({
1226
                        walletVersion: Wallet.WALLET_VERSION_V2,
1227
                        passphrase: options.passphrase,
1228
                        primarySeed: options.primarySeed,
1229
                        secret: options.secret
1230
                    }).then(function() {
1231
                        deferred.notify(APIClient.CREATE_WALLET_PROGRESS_DONE);
1232
                        return [
1233
                            wallet,
1234
                            {
1235
                                walletVersion: wallet.walletVersion,
1236
                                encryptedPrimarySeed: options.encryptedPrimarySeed ?
1237
                                    bip39.entropyToMnemonic(blocktrail.convert(options.encryptedPrimarySeed, 'base64', 'hex')) :
1238
                                    null,
1239
                                backupSeed: options.backupSeed ? bip39.entropyToMnemonic(options.backupSeed.toString('hex')) : null,
1240
                                recoveryEncryptedSecret: options.recoveryEncryptedSecret ?
1241
                                    bip39.entropyToMnemonic(blocktrail.convert(options.recoveryEncryptedSecret, 'base64', 'hex')) :
1242
                                    null,
1243
                                encryptedSecret: options.encryptedSecret ?
1244
                                    bip39.entropyToMnemonic(blocktrail.convert(options.encryptedSecret, 'base64', 'hex')) :
1245
                                    null,
1246
                                blocktrailPublicKeys: blocktrailPublicKeys
1247
                            }
1248
                        ];
1249
                    });
1250
                }
1251
            );
1252
        })
1253
       .then(function(r) { deferred.resolve(r); }, function(e) { deferred.reject(e); });
1254
1255
    return deferred.promise;
1256
};
1257
1258
APIClient.prototype._createNewWalletV3 = function(options) {
1259
    var self = this;
@@ 1258-1359 (lines=102) @@
1255
    return deferred.promise;
1256
};
1257
1258
APIClient.prototype._createNewWalletV3 = function(options) {
1259
    var self = this;
1260
1261
    var deferred = q.defer();
1262
1263
    // avoid modifying passed options
1264
    options = _.merge({}, options);
1265
1266
    determineDataStorageV2_3(options)
1267
        .then(function(options) {
1268
            options.passphrase = options.passphrase || options.password;
1269
            delete options.password;
1270
1271
            // avoid deprecated options
1272
            if (options.primaryPrivateKey) {
1273
                throw new blocktrail.WalletInitError("Can't specify; Primary PrivateKey");
1274
            }
1275
1276
            // seed should be provided or generated
1277
            options.primarySeed = options.primarySeed || randomBytes(Wallet.WALLET_ENTROPY_BITS / 8);
1278
1279
            return options;
1280
        })
1281
        .then(function(options) {
1282
            return self.produceEncryptedDataV3(options, deferred.notify.bind(deferred));
1283
        })
1284
        .then(function(options) {
1285
            return doRemainingWalletDataV2_3(options, self.network, deferred.notify.bind(deferred));
1286
        })
1287
        .then(function(options) {
1288
            // create a checksum of our private key which we'll later use to verify we used the right password
1289
            var checksum = options.primaryPrivateKey.getAddress();
1290
            var keyIndex = options.keyIndex;
1291
1292
            // send the public keys and encrypted data to server
1293
            return self.storeNewWalletV3(
1294
                options.identifier,
1295
                [options.primaryPublicKey.toBase58(), "M/" + keyIndex + "'"],
1296
                [options.backupPublicKey.toBase58(), "M"],
1297
                options.storeDataOnServer ? options.encryptedPrimarySeed : false,
1298
                options.storeDataOnServer ? options.encryptedSecret : false,
1299
                options.storeDataOnServer ? options.recoverySecret : false,
1300
                checksum,
1301
                keyIndex,
1302
                options.support_secret || null,
1303
                options.segwit || null
1304
            )
1305
                .then(
1306
                    // result, deferred, self(apiclient)
1307
                    function(result) {
1308
                        deferred.notify(APIClient.CREATE_WALLET_PROGRESS_INIT);
1309
1310
                        var blocktrailPublicKeys = _.mapValues(result.blocktrail_public_keys, function(blocktrailPublicKey) {
1311
                            return bitcoin.HDNode.fromBase58(blocktrailPublicKey[0], self.network);
1312
                        });
1313
1314
                        var wallet = new Wallet(
1315
                            self,
1316
                            options.identifier,
1317
                            Wallet.WALLET_VERSION_V3,
1318
                            null,
1319
                            options.storeDataOnServer ? options.encryptedPrimarySeed : null,
1320
                            options.storeDataOnServer ? options.encryptedSecret : null,
1321
                            {keyIndex: options.primaryPublicKey},
1322
                            options.backupPublicKey,
1323
                            blocktrailPublicKeys,
1324
                            keyIndex,
1325
                            result.segwit || 0,
1326
                            self.testnet,
1327
                            checksum,
1328
                            result.upgrade_key_index,
1329
                            options.bypassNewAddressCheck
1330
                        );
1331
1332
                        // pass along decrypted data to avoid extra work
1333
                        return wallet.unlock({
1334
                            walletVersion: Wallet.WALLET_VERSION_V3,
1335
                            passphrase: options.passphrase,
1336
                            primarySeed: options.primarySeed,
1337
                            secret: options.secret
1338
                        }).then(function() {
1339
                            deferred.notify(APIClient.CREATE_WALLET_PROGRESS_DONE);
1340
                            return [
1341
                                wallet,
1342
                                {
1343
                                    walletVersion: wallet.walletVersion,
1344
                                    encryptedPrimarySeed: options.encryptedPrimarySeed ? EncryptionMnemonic.encode(options.encryptedPrimarySeed) : null,
1345
                                    backupSeed: options.backupSeed ? bip39.entropyToMnemonic(options.backupSeed) : null,
1346
                                    recoveryEncryptedSecret: options.recoveryEncryptedSecret ?
1347
                                        EncryptionMnemonic.encode(options.recoveryEncryptedSecret) : null,
1348
                                    encryptedSecret: options.encryptedSecret ? EncryptionMnemonic.encode(options.encryptedSecret) : null,
1349
                                    blocktrailPublicKeys: blocktrailPublicKeys
1350
                                }
1351
                            ];
1352
                        });
1353
                    }
1354
                );
1355
        })
1356
        .then(function(r) { deferred.resolve(r); }, function(e) { deferred.reject(e); });
1357
1358
    return deferred.promise;
1359
};
1360
1361
function verifyPublicBip32Key(bip32Key, network) {
1362
    var hk = bitcoin.HDNode.fromBase58(bip32Key[0], network);