Code Duplication    Length = 104-108 lines in 2 locations

lib/api_client.js 2 locations

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