Code Duplication    Length = 105-109 lines in 2 locations

lib/api_client.js 2 locations

@@ 1460-1568 (lines=109) @@
1457
    return deferred.promise;
1458
};
1459
1460
APIClient.prototype._createNewWalletV2 = function(options) {
1461
    var self = this;
1462
1463
    var deferred = q.defer();
1464
1465
    // avoid modifying passed options
1466
    options = _.merge({}, options);
1467
1468
    determineDataStorageV2_3(options)
1469
        .then(function(options) {
1470
            options.passphrase = options.passphrase || options.password;
1471
            delete options.password;
1472
1473
            // avoid deprecated options
1474
            if (options.primaryPrivateKey) {
1475
                throw new blocktrail.WalletInitError("Can't specify; Primary PrivateKey");
1476
            }
1477
1478
            // seed should be provided or generated
1479
            options.primarySeed = options.primarySeed || randomBytes(Wallet.WALLET_ENTROPY_BITS / 8);
1480
1481
            return options;
1482
        })
1483
        .then(function(options) {
1484
            return produceEncryptedDataV2(options, deferred.notify.bind(deferred));
1485
        })
1486
        .then(function(options) {
1487
            return doRemainingWalletDataV2_3(options, self.network, deferred.notify.bind(deferred));
1488
        })
1489
        .then(function(options) {
1490
            // create a checksum of our private key which we'll later use to verify we used the right password
1491
            var pubKeyHash = bitcoin.crypto.hash160(options.primaryPrivateKey.getPublicKeyBuffer());
1492
            var checksum = bitcoin.address.toBase58Check(pubKeyHash, self.network.pubKeyHash);
1493
            var keyIndex = options.keyIndex;
1494
1495
            // send the public keys and encrypted data to server
1496
            return self.storeNewWalletV2(
1497
                options.identifier,
1498
                [options.primaryPublicKey.toBase58(), "M/" + keyIndex + "'"],
1499
                [options.backupPublicKey.toBase58(), "M"],
1500
                options.storeDataOnServer ? options.encryptedPrimarySeed : false,
1501
                options.storeDataOnServer ? options.encryptedSecret : false,
1502
                options.storeDataOnServer ? options.recoverySecret : false,
1503
                checksum,
1504
                keyIndex,
1505
                options.support_secret || null,
1506
                options.segwit || null
1507
            )
1508
                .then(
1509
                function(result) {
1510
                    deferred.notify(APIClient.CREATE_WALLET_PROGRESS_INIT);
1511
1512
                    var blocktrailPublicKeys = _.mapValues(result.blocktrail_public_keys, function(blocktrailPublicKey) {
1513
                        return bitcoin.HDNode.fromBase58(blocktrailPublicKey[0], self.network);
1514
                    });
1515
1516
                    var wallet = new Wallet(
1517
                        self,
1518
                        options.identifier,
1519
                        Wallet.WALLET_VERSION_V2,
1520
                        null,
1521
                        options.storeDataOnServer ? options.encryptedPrimarySeed : null,
1522
                        options.storeDataOnServer ? options.encryptedSecret : null,
1523
                        {keyIndex: options.primaryPublicKey},
1524
                        options.backupPublicKey,
1525
                        blocktrailPublicKeys,
1526
                        keyIndex,
1527
                        result.segwit || 0,
1528
                        self.testnet,
1529
                        self.regtest,
1530
                        checksum,
1531
                        result.upgrade_key_index,
1532
                        options.useCashAddress,
1533
                        options.bypassNewAddressCheck
1534
                    );
1535
1536
                    // pass along decrypted data to avoid extra work
1537
                    return wallet.unlock({
1538
                        walletVersion: Wallet.WALLET_VERSION_V2,
1539
                        passphrase: options.passphrase,
1540
                        primarySeed: options.primarySeed,
1541
                        secret: options.secret
1542
                    }).then(function() {
1543
                        deferred.notify(APIClient.CREATE_WALLET_PROGRESS_DONE);
1544
                        return [
1545
                            wallet,
1546
                            {
1547
                                walletVersion: wallet.walletVersion,
1548
                                encryptedPrimarySeed: options.encryptedPrimarySeed ?
1549
                                    bip39.entropyToMnemonic(blocktrail.convert(options.encryptedPrimarySeed, 'base64', 'hex')) :
1550
                                    null,
1551
                                backupSeed: options.backupSeed ? bip39.entropyToMnemonic(options.backupSeed.toString('hex')) : null,
1552
                                recoveryEncryptedSecret: options.recoveryEncryptedSecret ?
1553
                                    bip39.entropyToMnemonic(blocktrail.convert(options.recoveryEncryptedSecret, 'base64', 'hex')) :
1554
                                    null,
1555
                                encryptedSecret: options.encryptedSecret ?
1556
                                    bip39.entropyToMnemonic(blocktrail.convert(options.encryptedSecret, 'base64', 'hex')) :
1557
                                    null,
1558
                                blocktrailPublicKeys: blocktrailPublicKeys
1559
                            }
1560
                        ];
1561
                    });
1562
                }
1563
            );
1564
        })
1565
       .then(function(r) { deferred.resolve(r); }, function(e) { deferred.reject(e); });
1566
1567
    return deferred.promise;
1568
};
1569
1570
APIClient.prototype._createNewWalletV3 = function(options) {
1571
    var self = this;
@@ 1570-1674 (lines=105) @@
1567
    return deferred.promise;
1568
};
1569
1570
APIClient.prototype._createNewWalletV3 = function(options) {
1571
    var self = this;
1572
1573
    var deferred = q.defer();
1574
1575
    // avoid modifying passed options
1576
    options = _.merge({}, options);
1577
1578
    determineDataStorageV2_3(options)
1579
        .then(function(options) {
1580
            options.passphrase = options.passphrase || options.password;
1581
            delete options.password;
1582
1583
            // avoid deprecated options
1584
            if (options.primaryPrivateKey) {
1585
                throw new blocktrail.WalletInitError("Can't specify; Primary PrivateKey");
1586
            }
1587
1588
            // seed should be provided or generated
1589
            options.primarySeed = options.primarySeed || randomBytes(Wallet.WALLET_ENTROPY_BITS / 8);
1590
1591
            return options;
1592
        })
1593
        .then(function(options) {
1594
            return self.produceEncryptedDataV3(options, deferred.notify.bind(deferred));
1595
        })
1596
        .then(function(options) {
1597
            return doRemainingWalletDataV2_3(options, self.network, deferred.notify.bind(deferred));
1598
        })
1599
        .then(function(options) {
1600
            // create a checksum of our private key which we'll later use to verify we used the right password
1601
            var pubKeyHash = bitcoin.crypto.hash160(options.primaryPrivateKey.getPublicKeyBuffer());
1602
            var checksum = bitcoin.address.toBase58Check(pubKeyHash, self.network.pubKeyHash);
1603
            var keyIndex = options.keyIndex;
1604
1605
            // send the public keys and encrypted data to server
1606
            return self.storeNewWalletV3(
1607
                options.identifier,
1608
                [options.primaryPublicKey.toBase58(), "M/" + keyIndex + "'"],
1609
                [options.backupPublicKey.toBase58(), "M"],
1610
                options.storeDataOnServer ? options.encryptedPrimarySeed : false,
1611
                options.storeDataOnServer ? options.encryptedSecret : false,
1612
                options.storeDataOnServer ? options.recoverySecret : false,
1613
                checksum,
1614
                keyIndex,
1615
                options.support_secret || null,
1616
                options.segwit || null
1617
            )
1618
                .then(
1619
                    // result, deferred, self(apiclient)
1620
                    function(result) {
1621
                        deferred.notify(APIClient.CREATE_WALLET_PROGRESS_INIT);
1622
1623
                        var blocktrailPublicKeys = _.mapValues(result.blocktrail_public_keys, function(blocktrailPublicKey) {
1624
                            return bitcoin.HDNode.fromBase58(blocktrailPublicKey[0], self.network);
1625
                        });
1626
1627
                        var wallet = new Wallet(
1628
                            self,
1629
                            options.identifier,
1630
                            Wallet.WALLET_VERSION_V3,
1631
                            null,
1632
                            options.storeDataOnServer ? options.encryptedPrimarySeed : null,
1633
                            options.storeDataOnServer ? options.encryptedSecret : null,
1634
                            {keyIndex: options.primaryPublicKey},
1635
                            options.backupPublicKey,
1636
                            blocktrailPublicKeys,
1637
                            keyIndex,
1638
                            result.segwit || 0,
1639
                            self.testnet,
1640
                            self.regtest,
1641
                            checksum,
1642
                            result.upgrade_key_index,
1643
                            options.useCashAddress,
1644
                            options.bypassNewAddressCheck
1645
                        );
1646
1647
                        // pass along decrypted data to avoid extra work
1648
                        return wallet.unlock({
1649
                            walletVersion: Wallet.WALLET_VERSION_V3,
1650
                            passphrase: options.passphrase,
1651
                            primarySeed: options.primarySeed,
1652
                            secret: options.secret
1653
                        }).then(function() {
1654
                            deferred.notify(APIClient.CREATE_WALLET_PROGRESS_DONE);
1655
                            return [
1656
                                wallet,
1657
                                {
1658
                                    walletVersion: wallet.walletVersion,
1659
                                    encryptedPrimarySeed: options.encryptedPrimarySeed ? EncryptionMnemonic.encode(options.encryptedPrimarySeed) : null,
1660
                                    backupSeed: options.backupSeed ? bip39.entropyToMnemonic(options.backupSeed) : null,
1661
                                    recoveryEncryptedSecret: options.recoveryEncryptedSecret ?
1662
                                        EncryptionMnemonic.encode(options.recoveryEncryptedSecret) : null,
1663
                                    encryptedSecret: options.encryptedSecret ? EncryptionMnemonic.encode(options.encryptedSecret) : null,
1664
                                    blocktrailPublicKeys: blocktrailPublicKeys
1665
                                }
1666
                            ];
1667
                        });
1668
                    }
1669
                );
1670
        })
1671
        .then(function(r) { deferred.resolve(r); }, function(e) { deferred.reject(e); });
1672
1673
    return deferred.promise;
1674
};
1675
1676
function verifyPublicBip32Key(bip32Key, network) {
1677
    var hk = bitcoin.HDNode.fromBase58(bip32Key[0], network);