Code Duplication    Length = 105-109 lines in 2 locations

lib/api_client.js 2 locations

@@ 1283-1391 (lines=109) @@
1280
    return deferred.promise;
1281
};
1282
1283
APIClient.prototype._createNewWalletV2 = function(options) {
1284
    var self = this;
1285
1286
    var deferred = q.defer();
1287
1288
    // avoid modifying passed options
1289
    options = _.merge({}, options);
1290
1291
    determineDataStorageV2_3(options)
1292
        .then(function(options) {
1293
            options.passphrase = options.passphrase || options.password;
1294
            delete options.password;
1295
1296
            // avoid deprecated options
1297
            if (options.primaryPrivateKey) {
1298
                throw new blocktrail.WalletInitError("Can't specify; Primary PrivateKey");
1299
            }
1300
1301
            // seed should be provided or generated
1302
            options.primarySeed = options.primarySeed || randomBytes(Wallet.WALLET_ENTROPY_BITS / 8);
1303
1304
            return options;
1305
        })
1306
        .then(function(options) {
1307
            return produceEncryptedDataV2(options, deferred.notify.bind(deferred));
1308
        })
1309
        .then(function(options) {
1310
            return doRemainingWalletDataV2_3(options, self.network, deferred.notify.bind(deferred));
1311
        })
1312
        .then(function(options) {
1313
            // create a checksum of our private key which we'll later use to verify we used the right password
1314
            var pubKeyHash = bitcoin.crypto.hash160(options.primaryPrivateKey.getPublicKeyBuffer());
1315
            var checksum = bitcoin.address.toBase58Check(pubKeyHash, self.network.pubKeyHash);
1316
            var keyIndex = options.keyIndex;
1317
1318
            // send the public keys and encrypted data to server
1319
            return self.storeNewWalletV2(
1320
                options.identifier,
1321
                [options.primaryPublicKey.toBase58(), "M/" + keyIndex + "'"],
1322
                [options.backupPublicKey.toBase58(), "M"],
1323
                options.storeDataOnServer ? options.encryptedPrimarySeed : false,
1324
                options.storeDataOnServer ? options.encryptedSecret : false,
1325
                options.storeDataOnServer ? options.recoverySecret : false,
1326
                checksum,
1327
                keyIndex,
1328
                options.support_secret || null,
1329
                options.segwit || null
1330
            )
1331
                .then(
1332
                function(result) {
1333
                    deferred.notify(APIClient.CREATE_WALLET_PROGRESS_INIT);
1334
1335
                    var blocktrailPublicKeys = _.mapValues(result.blocktrail_public_keys, function(blocktrailPublicKey) {
1336
                        return bitcoin.HDNode.fromBase58(blocktrailPublicKey[0], self.network);
1337
                    });
1338
1339
                    var wallet = new Wallet(
1340
                        self,
1341
                        options.identifier,
1342
                        Wallet.WALLET_VERSION_V2,
1343
                        null,
1344
                        options.storeDataOnServer ? options.encryptedPrimarySeed : null,
1345
                        options.storeDataOnServer ? options.encryptedSecret : null,
1346
                        {keyIndex: options.primaryPublicKey},
1347
                        options.backupPublicKey,
1348
                        blocktrailPublicKeys,
1349
                        keyIndex,
1350
                        result.segwit || 0,
1351
                        self.testnet,
1352
                        self.regtest,
1353
                        checksum,
1354
                        result.upgrade_key_index,
1355
                        options.useCashAddress,
1356
                        options.bypassNewAddressCheck
1357
                    );
1358
1359
                    // pass along decrypted data to avoid extra work
1360
                    return wallet.unlock({
1361
                        walletVersion: Wallet.WALLET_VERSION_V2,
1362
                        passphrase: options.passphrase,
1363
                        primarySeed: options.primarySeed,
1364
                        secret: options.secret
1365
                    }).then(function() {
1366
                        deferred.notify(APIClient.CREATE_WALLET_PROGRESS_DONE);
1367
                        return [
1368
                            wallet,
1369
                            {
1370
                                walletVersion: wallet.walletVersion,
1371
                                encryptedPrimarySeed: options.encryptedPrimarySeed ?
1372
                                    bip39.entropyToMnemonic(blocktrail.convert(options.encryptedPrimarySeed, 'base64', 'hex')) :
1373
                                    null,
1374
                                backupSeed: options.backupSeed ? bip39.entropyToMnemonic(options.backupSeed.toString('hex')) : null,
1375
                                recoveryEncryptedSecret: options.recoveryEncryptedSecret ?
1376
                                    bip39.entropyToMnemonic(blocktrail.convert(options.recoveryEncryptedSecret, 'base64', 'hex')) :
1377
                                    null,
1378
                                encryptedSecret: options.encryptedSecret ?
1379
                                    bip39.entropyToMnemonic(blocktrail.convert(options.encryptedSecret, 'base64', 'hex')) :
1380
                                    null,
1381
                                blocktrailPublicKeys: blocktrailPublicKeys
1382
                            }
1383
                        ];
1384
                    });
1385
                }
1386
            );
1387
        })
1388
       .then(function(r) { deferred.resolve(r); }, function(e) { deferred.reject(e); });
1389
1390
    return deferred.promise;
1391
};
1392
1393
APIClient.prototype._createNewWalletV3 = function(options) {
1394
    var self = this;
@@ 1393-1497 (lines=105) @@
1390
    return deferred.promise;
1391
};
1392
1393
APIClient.prototype._createNewWalletV3 = function(options) {
1394
    var self = this;
1395
1396
    var deferred = q.defer();
1397
1398
    // avoid modifying passed options
1399
    options = _.merge({}, options);
1400
1401
    determineDataStorageV2_3(options)
1402
        .then(function(options) {
1403
            options.passphrase = options.passphrase || options.password;
1404
            delete options.password;
1405
1406
            // avoid deprecated options
1407
            if (options.primaryPrivateKey) {
1408
                throw new blocktrail.WalletInitError("Can't specify; Primary PrivateKey");
1409
            }
1410
1411
            // seed should be provided or generated
1412
            options.primarySeed = options.primarySeed || randomBytes(Wallet.WALLET_ENTROPY_BITS / 8);
1413
1414
            return options;
1415
        })
1416
        .then(function(options) {
1417
            return self.produceEncryptedDataV3(options, deferred.notify.bind(deferred));
1418
        })
1419
        .then(function(options) {
1420
            return doRemainingWalletDataV2_3(options, self.network, deferred.notify.bind(deferred));
1421
        })
1422
        .then(function(options) {
1423
            // create a checksum of our private key which we'll later use to verify we used the right password
1424
            var pubKeyHash = bitcoin.crypto.hash160(options.primaryPrivateKey.getPublicKeyBuffer());
1425
            var checksum = bitcoin.address.toBase58Check(pubKeyHash, self.network.pubKeyHash);
1426
            var keyIndex = options.keyIndex;
1427
1428
            // send the public keys and encrypted data to server
1429
            return self.storeNewWalletV3(
1430
                options.identifier,
1431
                [options.primaryPublicKey.toBase58(), "M/" + keyIndex + "'"],
1432
                [options.backupPublicKey.toBase58(), "M"],
1433
                options.storeDataOnServer ? options.encryptedPrimarySeed : false,
1434
                options.storeDataOnServer ? options.encryptedSecret : false,
1435
                options.storeDataOnServer ? options.recoverySecret : false,
1436
                checksum,
1437
                keyIndex,
1438
                options.support_secret || null,
1439
                options.segwit || null
1440
            )
1441
                .then(
1442
                    // result, deferred, self(apiclient)
1443
                    function(result) {
1444
                        deferred.notify(APIClient.CREATE_WALLET_PROGRESS_INIT);
1445
1446
                        var blocktrailPublicKeys = _.mapValues(result.blocktrail_public_keys, function(blocktrailPublicKey) {
1447
                            return bitcoin.HDNode.fromBase58(blocktrailPublicKey[0], self.network);
1448
                        });
1449
1450
                        var wallet = new Wallet(
1451
                            self,
1452
                            options.identifier,
1453
                            Wallet.WALLET_VERSION_V3,
1454
                            null,
1455
                            options.storeDataOnServer ? options.encryptedPrimarySeed : null,
1456
                            options.storeDataOnServer ? options.encryptedSecret : null,
1457
                            {keyIndex: options.primaryPublicKey},
1458
                            options.backupPublicKey,
1459
                            blocktrailPublicKeys,
1460
                            keyIndex,
1461
                            result.segwit || 0,
1462
                            self.testnet,
1463
                            self.regtest,
1464
                            checksum,
1465
                            result.upgrade_key_index,
1466
                            options.useCashAddress,
1467
                            options.bypassNewAddressCheck
1468
                        );
1469
1470
                        // pass along decrypted data to avoid extra work
1471
                        return wallet.unlock({
1472
                            walletVersion: Wallet.WALLET_VERSION_V3,
1473
                            passphrase: options.passphrase,
1474
                            primarySeed: options.primarySeed,
1475
                            secret: options.secret
1476
                        }).then(function() {
1477
                            deferred.notify(APIClient.CREATE_WALLET_PROGRESS_DONE);
1478
                            return [
1479
                                wallet,
1480
                                {
1481
                                    walletVersion: wallet.walletVersion,
1482
                                    encryptedPrimarySeed: options.encryptedPrimarySeed ? EncryptionMnemonic.encode(options.encryptedPrimarySeed) : null,
1483
                                    backupSeed: options.backupSeed ? bip39.entropyToMnemonic(options.backupSeed) : null,
1484
                                    recoveryEncryptedSecret: options.recoveryEncryptedSecret ?
1485
                                        EncryptionMnemonic.encode(options.recoveryEncryptedSecret) : null,
1486
                                    encryptedSecret: options.encryptedSecret ? EncryptionMnemonic.encode(options.encryptedSecret) : null,
1487
                                    blocktrailPublicKeys: blocktrailPublicKeys
1488
                                }
1489
                            ];
1490
                        });
1491
                    }
1492
                );
1493
        })
1494
        .then(function(r) { deferred.resolve(r); }, function(e) { deferred.reject(e); });
1495
1496
    return deferred.promise;
1497
};
1498
1499
function verifyPublicBip32Key(bip32Key, network) {
1500
    var hk = bitcoin.HDNode.fromBase58(bip32Key[0], network);