Code Duplication    Length = 105-109 lines in 2 locations

lib/api_client.js 2 locations

@@ 1518-1626 (lines=109) @@
1515
    return deferred.promise;
1516
};
1517
1518
APIClient.prototype._createNewWalletV2 = function(options) {
1519
    var self = this;
1520
1521
    var deferred = q.defer();
1522
1523
    // avoid modifying passed options
1524
    options = _.merge({}, options);
1525
1526
    determineDataStorageV2_3(options)
1527
        .then(function(options) {
1528
            options.passphrase = options.passphrase || options.password;
1529
            delete options.password;
1530
1531
            // avoid deprecated options
1532
            if (options.primaryPrivateKey) {
1533
                throw new blocktrail.WalletInitError("Can't specify; Primary PrivateKey");
1534
            }
1535
1536
            // seed should be provided or generated
1537
            options.primarySeed = options.primarySeed || randomBytes(Wallet.WALLET_ENTROPY_BITS / 8);
1538
1539
            return options;
1540
        })
1541
        .then(function(options) {
1542
            return produceEncryptedDataV2(options, deferred.notify.bind(deferred));
1543
        })
1544
        .then(function(options) {
1545
            return doRemainingWalletDataV2_3(options, self.network, deferred.notify.bind(deferred));
1546
        })
1547
        .then(function(options) {
1548
            // create a checksum of our private key which we'll later use to verify we used the right password
1549
            var pubKeyHash = bitcoin.crypto.hash160(options.primaryPrivateKey.getPublicKeyBuffer());
1550
            var checksum = bitcoin.address.toBase58Check(pubKeyHash, self.network.pubKeyHash);
1551
            var keyIndex = options.keyIndex;
1552
1553
            // send the public keys and encrypted data to server
1554
            return self.storeNewWalletV2(
1555
                options.identifier,
1556
                [options.primaryPublicKey.toBase58(), "M/" + keyIndex + "'"],
1557
                [options.backupPublicKey.toBase58(), "M"],
1558
                options.storeDataOnServer ? options.encryptedPrimarySeed : false,
1559
                options.storeDataOnServer ? options.encryptedSecret : false,
1560
                options.storeDataOnServer ? options.recoverySecret : false,
1561
                checksum,
1562
                keyIndex,
1563
                options.support_secret || null,
1564
                options.segwit || null
1565
            )
1566
                .then(
1567
                function(result) {
1568
                    deferred.notify(APIClient.CREATE_WALLET_PROGRESS_INIT);
1569
1570
                    var blocktrailPublicKeys = _.mapValues(result.blocktrail_public_keys, function(blocktrailPublicKey) {
1571
                        return bitcoin.HDNode.fromBase58(blocktrailPublicKey[0], self.network);
1572
                    });
1573
1574
                    var wallet = new Wallet(
1575
                        self,
1576
                        options.identifier,
1577
                        Wallet.WALLET_VERSION_V2,
1578
                        null,
1579
                        options.storeDataOnServer ? options.encryptedPrimarySeed : null,
1580
                        options.storeDataOnServer ? options.encryptedSecret : null,
1581
                        {keyIndex: options.primaryPublicKey},
1582
                        options.backupPublicKey,
1583
                        blocktrailPublicKeys,
1584
                        keyIndex,
1585
                        result.segwit || 0,
1586
                        self.testnet,
1587
                        self.regtest,
1588
                        checksum,
1589
                        result.upgrade_key_index,
1590
                        options.useCashAddress,
1591
                        options.bypassNewAddressCheck
1592
                    );
1593
1594
                    // pass along decrypted data to avoid extra work
1595
                    return wallet.unlock({
1596
                        walletVersion: Wallet.WALLET_VERSION_V2,
1597
                        passphrase: options.passphrase,
1598
                        primarySeed: options.primarySeed,
1599
                        secret: options.secret
1600
                    }).then(function() {
1601
                        deferred.notify(APIClient.CREATE_WALLET_PROGRESS_DONE);
1602
                        return [
1603
                            wallet,
1604
                            {
1605
                                walletVersion: wallet.walletVersion,
1606
                                encryptedPrimarySeed: options.encryptedPrimarySeed ?
1607
                                    bip39.entropyToMnemonic(blocktrail.convert(options.encryptedPrimarySeed, 'base64', 'hex')) :
1608
                                    null,
1609
                                backupSeed: options.backupSeed ? bip39.entropyToMnemonic(options.backupSeed.toString('hex')) : null,
1610
                                recoveryEncryptedSecret: options.recoveryEncryptedSecret ?
1611
                                    bip39.entropyToMnemonic(blocktrail.convert(options.recoveryEncryptedSecret, 'base64', 'hex')) :
1612
                                    null,
1613
                                encryptedSecret: options.encryptedSecret ?
1614
                                    bip39.entropyToMnemonic(blocktrail.convert(options.encryptedSecret, 'base64', 'hex')) :
1615
                                    null,
1616
                                blocktrailPublicKeys: blocktrailPublicKeys
1617
                            }
1618
                        ];
1619
                    });
1620
                }
1621
            );
1622
        })
1623
       .then(function(r) { deferred.resolve(r); }, function(e) { deferred.reject(e); });
1624
1625
    return deferred.promise;
1626
};
1627
1628
APIClient.prototype._createNewWalletV3 = function(options) {
1629
    var self = this;
@@ 1628-1732 (lines=105) @@
1625
    return deferred.promise;
1626
};
1627
1628
APIClient.prototype._createNewWalletV3 = function(options) {
1629
    var self = this;
1630
1631
    var deferred = q.defer();
1632
1633
    // avoid modifying passed options
1634
    options = _.merge({}, options);
1635
1636
    determineDataStorageV2_3(options)
1637
        .then(function(options) {
1638
            options.passphrase = options.passphrase || options.password;
1639
            delete options.password;
1640
1641
            // avoid deprecated options
1642
            if (options.primaryPrivateKey) {
1643
                throw new blocktrail.WalletInitError("Can't specify; Primary PrivateKey");
1644
            }
1645
1646
            // seed should be provided or generated
1647
            options.primarySeed = options.primarySeed || randomBytes(Wallet.WALLET_ENTROPY_BITS / 8);
1648
1649
            return options;
1650
        })
1651
        .then(function(options) {
1652
            return self.produceEncryptedDataV3(options, deferred.notify.bind(deferred));
1653
        })
1654
        .then(function(options) {
1655
            return doRemainingWalletDataV2_3(options, self.network, deferred.notify.bind(deferred));
1656
        })
1657
        .then(function(options) {
1658
            // create a checksum of our private key which we'll later use to verify we used the right password
1659
            var pubKeyHash = bitcoin.crypto.hash160(options.primaryPrivateKey.getPublicKeyBuffer());
1660
            var checksum = bitcoin.address.toBase58Check(pubKeyHash, self.network.pubKeyHash);
1661
            var keyIndex = options.keyIndex;
1662
1663
            // send the public keys and encrypted data to server
1664
            return self.storeNewWalletV3(
1665
                options.identifier,
1666
                [options.primaryPublicKey.toBase58(), "M/" + keyIndex + "'"],
1667
                [options.backupPublicKey.toBase58(), "M"],
1668
                options.storeDataOnServer ? options.encryptedPrimarySeed : false,
1669
                options.storeDataOnServer ? options.encryptedSecret : false,
1670
                options.storeDataOnServer ? options.recoverySecret : false,
1671
                checksum,
1672
                keyIndex,
1673
                options.support_secret || null,
1674
                options.segwit || null
1675
            )
1676
                .then(
1677
                    // result, deferred, self(apiclient)
1678
                    function(result) {
1679
                        deferred.notify(APIClient.CREATE_WALLET_PROGRESS_INIT);
1680
1681
                        var blocktrailPublicKeys = _.mapValues(result.blocktrail_public_keys, function(blocktrailPublicKey) {
1682
                            return bitcoin.HDNode.fromBase58(blocktrailPublicKey[0], self.network);
1683
                        });
1684
1685
                        var wallet = new Wallet(
1686
                            self,
1687
                            options.identifier,
1688
                            Wallet.WALLET_VERSION_V3,
1689
                            null,
1690
                            options.storeDataOnServer ? options.encryptedPrimarySeed : null,
1691
                            options.storeDataOnServer ? options.encryptedSecret : null,
1692
                            {keyIndex: options.primaryPublicKey},
1693
                            options.backupPublicKey,
1694
                            blocktrailPublicKeys,
1695
                            keyIndex,
1696
                            result.segwit || 0,
1697
                            self.testnet,
1698
                            self.regtest,
1699
                            checksum,
1700
                            result.upgrade_key_index,
1701
                            options.useCashAddress,
1702
                            options.bypassNewAddressCheck
1703
                        );
1704
1705
                        // pass along decrypted data to avoid extra work
1706
                        return wallet.unlock({
1707
                            walletVersion: Wallet.WALLET_VERSION_V3,
1708
                            passphrase: options.passphrase,
1709
                            primarySeed: options.primarySeed,
1710
                            secret: options.secret
1711
                        }).then(function() {
1712
                            deferred.notify(APIClient.CREATE_WALLET_PROGRESS_DONE);
1713
                            return [
1714
                                wallet,
1715
                                {
1716
                                    walletVersion: wallet.walletVersion,
1717
                                    encryptedPrimarySeed: options.encryptedPrimarySeed ? EncryptionMnemonic.encode(options.encryptedPrimarySeed) : null,
1718
                                    backupSeed: options.backupSeed ? bip39.entropyToMnemonic(options.backupSeed) : null,
1719
                                    recoveryEncryptedSecret: options.recoveryEncryptedSecret ?
1720
                                        EncryptionMnemonic.encode(options.recoveryEncryptedSecret) : null,
1721
                                    encryptedSecret: options.encryptedSecret ? EncryptionMnemonic.encode(options.encryptedSecret) : null,
1722
                                    blocktrailPublicKeys: blocktrailPublicKeys
1723
                                }
1724
                            ];
1725
                        });
1726
                    }
1727
                );
1728
        })
1729
        .then(function(r) { deferred.resolve(r); }, function(e) { deferred.reject(e); });
1730
1731
    return deferred.promise;
1732
};
1733
1734
function verifyPublicBip32Key(bip32Key, network) {
1735
    var hk = bitcoin.HDNode.fromBase58(bip32Key[0], network);