Code Duplication    Length = 105-109 lines in 2 locations

lib/api_client.js 2 locations

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