Code Duplication    Length = 72-76 lines in 2 locations

lib/api_client.js 2 locations

@@ 1505-1580 (lines=76) @@
1502
        .then(function(options) {
1503
            return doRemainingWalletDataV2_3(options, self.network, deferred.notify.bind(deferred));
1504
        })
1505
        .then(function(options) {
1506
            // create a checksum of our private key which we'll later use to verify we used the right password
1507
            var pubKeyHash = bitcoin.crypto.hash160(options.primaryPrivateKey.getPublicKeyBuffer());
1508
            var checksum = bitcoin.address.toBase58Check(pubKeyHash, self.network.pubKeyHash);
1509
            var keyIndex = options.keyIndex;
1510
1511
            // send the public keys and encrypted data to server
1512
            return self.storeNewWalletV2(
1513
                options.identifier,
1514
                [options.primaryPublicKey.toBase58(), "M/" + keyIndex + "'"],
1515
                [options.backupPublicKey.toBase58(), "M"],
1516
                options.storeDataOnServer ? options.encryptedPrimarySeed : false,
1517
                options.storeDataOnServer ? options.encryptedSecret : false,
1518
                options.storeDataOnServer ? options.recoverySecret : false,
1519
                checksum,
1520
                keyIndex,
1521
                options.support_secret || null,
1522
                options.segwit || null
1523
            )
1524
                .then(
1525
                function(result) {
1526
                    deferred.notify(APIClient.CREATE_WALLET_PROGRESS_INIT);
1527
1528
                    var blocktrailPublicKeys = _.mapValues(result.blocktrail_public_keys, function(blocktrailPublicKey) {
1529
                        return bitcoin.HDNode.fromBase58(blocktrailPublicKey[0], self.network);
1530
                    });
1531
1532
                    var wallet = new Wallet(
1533
                        self,
1534
                        options.identifier,
1535
                        Wallet.WALLET_VERSION_V2,
1536
                        null,
1537
                        options.storeDataOnServer ? options.encryptedPrimarySeed : null,
1538
                        options.storeDataOnServer ? options.encryptedSecret : null,
1539
                        {keyIndex: options.primaryPublicKey},
1540
                        options.backupPublicKey,
1541
                        blocktrailPublicKeys,
1542
                        keyIndex,
1543
                        result.segwit || 0,
1544
                        self.testnet,
1545
                        self.regtest,
1546
                        checksum,
1547
                        result.upgrade_key_index,
1548
                        options.useCashAddress,
1549
                        options.bypassNewAddressCheck
1550
                    );
1551
1552
                    // pass along decrypted data to avoid extra work
1553
                    return wallet.unlock({
1554
                        walletVersion: Wallet.WALLET_VERSION_V2,
1555
                        passphrase: options.passphrase,
1556
                        primarySeed: options.primarySeed,
1557
                        secret: options.secret
1558
                    }).then(function() {
1559
                        deferred.notify(APIClient.CREATE_WALLET_PROGRESS_DONE);
1560
                        return [
1561
                            wallet,
1562
                            {
1563
                                walletVersion: wallet.walletVersion,
1564
                                encryptedPrimarySeed: options.encryptedPrimarySeed ?
1565
                                    bip39.entropyToMnemonic(blocktrail.convert(options.encryptedPrimarySeed, 'base64', 'hex')) :
1566
                                    null,
1567
                                backupSeed: options.backupSeed ? bip39.entropyToMnemonic(options.backupSeed.toString('hex')) : null,
1568
                                recoveryEncryptedSecret: options.recoveryEncryptedSecret ?
1569
                                    bip39.entropyToMnemonic(blocktrail.convert(options.recoveryEncryptedSecret, 'base64', 'hex')) :
1570
                                    null,
1571
                                encryptedSecret: options.encryptedSecret ?
1572
                                    bip39.entropyToMnemonic(blocktrail.convert(options.encryptedSecret, 'base64', 'hex')) :
1573
                                    null,
1574
                                blocktrailPublicKeys: blocktrailPublicKeys
1575
                            }
1576
                        ];
1577
                    });
1578
                }
1579
            );
1580
        })
1581
       .then(function(r) { deferred.resolve(r); }, function(e) { deferred.reject(e); });
1582
1583
    return deferred.promise;
@@ 1615-1686 (lines=72) @@
1612
        .then(function(options) {
1613
            return doRemainingWalletDataV2_3(options, self.network, deferred.notify.bind(deferred));
1614
        })
1615
        .then(function(options) {
1616
            // create a checksum of our private key which we'll later use to verify we used the right password
1617
            var pubKeyHash = bitcoin.crypto.hash160(options.primaryPrivateKey.getPublicKeyBuffer());
1618
            var checksum = bitcoin.address.toBase58Check(pubKeyHash, self.network.pubKeyHash);
1619
            var keyIndex = options.keyIndex;
1620
1621
            // send the public keys and encrypted data to server
1622
            return self.storeNewWalletV3(
1623
                options.identifier,
1624
                [options.primaryPublicKey.toBase58(), "M/" + keyIndex + "'"],
1625
                [options.backupPublicKey.toBase58(), "M"],
1626
                options.storeDataOnServer ? options.encryptedPrimarySeed : false,
1627
                options.storeDataOnServer ? options.encryptedSecret : false,
1628
                options.storeDataOnServer ? options.recoverySecret : false,
1629
                checksum,
1630
                keyIndex,
1631
                options.support_secret || null,
1632
                options.segwit || null
1633
            )
1634
                .then(
1635
                    // result, deferred, self(apiclient)
1636
                    function(result) {
1637
                        deferred.notify(APIClient.CREATE_WALLET_PROGRESS_INIT);
1638
1639
                        var blocktrailPublicKeys = _.mapValues(result.blocktrail_public_keys, function(blocktrailPublicKey) {
1640
                            return bitcoin.HDNode.fromBase58(blocktrailPublicKey[0], self.network);
1641
                        });
1642
1643
                        var wallet = new Wallet(
1644
                            self,
1645
                            options.identifier,
1646
                            Wallet.WALLET_VERSION_V3,
1647
                            null,
1648
                            options.storeDataOnServer ? options.encryptedPrimarySeed : null,
1649
                            options.storeDataOnServer ? options.encryptedSecret : null,
1650
                            {keyIndex: options.primaryPublicKey},
1651
                            options.backupPublicKey,
1652
                            blocktrailPublicKeys,
1653
                            keyIndex,
1654
                            result.segwit || 0,
1655
                            self.testnet,
1656
                            self.regtest,
1657
                            checksum,
1658
                            result.upgrade_key_index,
1659
                            options.useCashAddress,
1660
                            options.bypassNewAddressCheck
1661
                        );
1662
1663
                        // pass along decrypted data to avoid extra work
1664
                        return wallet.unlock({
1665
                            walletVersion: Wallet.WALLET_VERSION_V3,
1666
                            passphrase: options.passphrase,
1667
                            primarySeed: options.primarySeed,
1668
                            secret: options.secret
1669
                        }).then(function() {
1670
                            deferred.notify(APIClient.CREATE_WALLET_PROGRESS_DONE);
1671
                            return [
1672
                                wallet,
1673
                                {
1674
                                    walletVersion: wallet.walletVersion,
1675
                                    encryptedPrimarySeed: options.encryptedPrimarySeed ? EncryptionMnemonic.encode(options.encryptedPrimarySeed) : null,
1676
                                    backupSeed: options.backupSeed ? bip39.entropyToMnemonic(options.backupSeed) : null,
1677
                                    recoveryEncryptedSecret: options.recoveryEncryptedSecret ?
1678
                                        EncryptionMnemonic.encode(options.recoveryEncryptedSecret) : null,
1679
                                    encryptedSecret: options.encryptedSecret ? EncryptionMnemonic.encode(options.encryptedSecret) : null,
1680
                                    blocktrailPublicKeys: blocktrailPublicKeys
1681
                                }
1682
                            ];
1683
                        });
1684
                    }
1685
                );
1686
        })
1687
        .then(function(r) { deferred.resolve(r); }, function(e) { deferred.reject(e); });
1688
1689
    return deferred.promise;