Code Duplication    Length = 72-76 lines in 2 locations

lib/api_client.js 2 locations

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