Code Duplication    Length = 72-76 lines in 2 locations

lib/api_client.js 2 locations

@@ 1543-1618 (lines=76) @@
1540
        .then(function(options) {
1541
            return doRemainingWalletDataV2_3(options, self.network, deferred.notify.bind(deferred));
1542
        })
1543
        .then(function(options) {
1544
            // create a checksum of our private key which we'll later use to verify we used the right password
1545
            var pubKeyHash = bitcoin.crypto.hash160(options.primaryPrivateKey.getPublicKeyBuffer());
1546
            var checksum = bitcoin.address.toBase58Check(pubKeyHash, self.network.pubKeyHash);
1547
            var keyIndex = options.keyIndex;
1548
1549
            // send the public keys and encrypted data to server
1550
            return self.storeNewWalletV2(
1551
                options.identifier,
1552
                [options.primaryPublicKey.toBase58(), "M/" + keyIndex + "'"],
1553
                [options.backupPublicKey.toBase58(), "M"],
1554
                options.storeDataOnServer ? options.encryptedPrimarySeed : false,
1555
                options.storeDataOnServer ? options.encryptedSecret : false,
1556
                options.storeDataOnServer ? options.recoverySecret : false,
1557
                checksum,
1558
                keyIndex,
1559
                options.support_secret || null,
1560
                options.segwit || null
1561
            )
1562
                .then(
1563
                function(result) {
1564
                    deferred.notify(APIClient.CREATE_WALLET_PROGRESS_INIT);
1565
1566
                    var blocktrailPublicKeys = _.mapValues(result.blocktrail_public_keys, function(blocktrailPublicKey) {
1567
                        return bitcoin.HDNode.fromBase58(blocktrailPublicKey[0], self.network);
1568
                    });
1569
1570
                    var wallet = new Wallet(
1571
                        self,
1572
                        options.identifier,
1573
                        Wallet.WALLET_VERSION_V2,
1574
                        null,
1575
                        options.storeDataOnServer ? options.encryptedPrimarySeed : null,
1576
                        options.storeDataOnServer ? options.encryptedSecret : null,
1577
                        {keyIndex: options.primaryPublicKey},
1578
                        options.backupPublicKey,
1579
                        blocktrailPublicKeys,
1580
                        keyIndex,
1581
                        result.segwit || 0,
1582
                        self.testnet,
1583
                        self.regtest,
1584
                        checksum,
1585
                        result.upgrade_key_index,
1586
                        options.useCashAddress,
1587
                        options.bypassNewAddressCheck
1588
                    );
1589
1590
                    // pass along decrypted data to avoid extra work
1591
                    return wallet.unlock({
1592
                        walletVersion: Wallet.WALLET_VERSION_V2,
1593
                        passphrase: options.passphrase,
1594
                        primarySeed: options.primarySeed,
1595
                        secret: options.secret
1596
                    }).then(function() {
1597
                        deferred.notify(APIClient.CREATE_WALLET_PROGRESS_DONE);
1598
                        return [
1599
                            wallet,
1600
                            {
1601
                                walletVersion: wallet.walletVersion,
1602
                                encryptedPrimarySeed: options.encryptedPrimarySeed ?
1603
                                    bip39.entropyToMnemonic(blocktrail.convert(options.encryptedPrimarySeed, 'base64', 'hex')) :
1604
                                    null,
1605
                                backupSeed: options.backupSeed ? bip39.entropyToMnemonic(options.backupSeed.toString('hex')) : null,
1606
                                recoveryEncryptedSecret: options.recoveryEncryptedSecret ?
1607
                                    bip39.entropyToMnemonic(blocktrail.convert(options.recoveryEncryptedSecret, 'base64', 'hex')) :
1608
                                    null,
1609
                                encryptedSecret: options.encryptedSecret ?
1610
                                    bip39.entropyToMnemonic(blocktrail.convert(options.encryptedSecret, 'base64', 'hex')) :
1611
                                    null,
1612
                                blocktrailPublicKeys: blocktrailPublicKeys
1613
                            }
1614
                        ];
1615
                    });
1616
                }
1617
            );
1618
        })
1619
       .then(function(r) { deferred.resolve(r); }, function(e) { deferred.reject(e); });
1620
1621
    return deferred.promise;
@@ 1653-1724 (lines=72) @@
1650
        .then(function(options) {
1651
            return doRemainingWalletDataV2_3(options, self.network, deferred.notify.bind(deferred));
1652
        })
1653
        .then(function(options) {
1654
            // create a checksum of our private key which we'll later use to verify we used the right password
1655
            var pubKeyHash = bitcoin.crypto.hash160(options.primaryPrivateKey.getPublicKeyBuffer());
1656
            var checksum = bitcoin.address.toBase58Check(pubKeyHash, self.network.pubKeyHash);
1657
            var keyIndex = options.keyIndex;
1658
1659
            // send the public keys and encrypted data to server
1660
            return self.storeNewWalletV3(
1661
                options.identifier,
1662
                [options.primaryPublicKey.toBase58(), "M/" + keyIndex + "'"],
1663
                [options.backupPublicKey.toBase58(), "M"],
1664
                options.storeDataOnServer ? options.encryptedPrimarySeed : false,
1665
                options.storeDataOnServer ? options.encryptedSecret : false,
1666
                options.storeDataOnServer ? options.recoverySecret : false,
1667
                checksum,
1668
                keyIndex,
1669
                options.support_secret || null,
1670
                options.segwit || null
1671
            )
1672
                .then(
1673
                    // result, deferred, self(apiclient)
1674
                    function(result) {
1675
                        deferred.notify(APIClient.CREATE_WALLET_PROGRESS_INIT);
1676
1677
                        var blocktrailPublicKeys = _.mapValues(result.blocktrail_public_keys, function(blocktrailPublicKey) {
1678
                            return bitcoin.HDNode.fromBase58(blocktrailPublicKey[0], self.network);
1679
                        });
1680
1681
                        var wallet = new Wallet(
1682
                            self,
1683
                            options.identifier,
1684
                            Wallet.WALLET_VERSION_V3,
1685
                            null,
1686
                            options.storeDataOnServer ? options.encryptedPrimarySeed : null,
1687
                            options.storeDataOnServer ? options.encryptedSecret : null,
1688
                            {keyIndex: options.primaryPublicKey},
1689
                            options.backupPublicKey,
1690
                            blocktrailPublicKeys,
1691
                            keyIndex,
1692
                            result.segwit || 0,
1693
                            self.testnet,
1694
                            self.regtest,
1695
                            checksum,
1696
                            result.upgrade_key_index,
1697
                            options.useCashAddress,
1698
                            options.bypassNewAddressCheck
1699
                        );
1700
1701
                        // pass along decrypted data to avoid extra work
1702
                        return wallet.unlock({
1703
                            walletVersion: Wallet.WALLET_VERSION_V3,
1704
                            passphrase: options.passphrase,
1705
                            primarySeed: options.primarySeed,
1706
                            secret: options.secret
1707
                        }).then(function() {
1708
                            deferred.notify(APIClient.CREATE_WALLET_PROGRESS_DONE);
1709
                            return [
1710
                                wallet,
1711
                                {
1712
                                    walletVersion: wallet.walletVersion,
1713
                                    encryptedPrimarySeed: options.encryptedPrimarySeed ? EncryptionMnemonic.encode(options.encryptedPrimarySeed) : null,
1714
                                    backupSeed: options.backupSeed ? bip39.entropyToMnemonic(options.backupSeed) : null,
1715
                                    recoveryEncryptedSecret: options.recoveryEncryptedSecret ?
1716
                                        EncryptionMnemonic.encode(options.recoveryEncryptedSecret) : null,
1717
                                    encryptedSecret: options.encryptedSecret ? EncryptionMnemonic.encode(options.encryptedSecret) : null,
1718
                                    blocktrailPublicKeys: blocktrailPublicKeys
1719
                                }
1720
                            ];
1721
                        });
1722
                    }
1723
                );
1724
        })
1725
        .then(function(r) { deferred.resolve(r); }, function(e) { deferred.reject(e); });
1726
1727
    return deferred.promise;