Code Duplication    Length = 72-76 lines in 2 locations

lib/api_client.js 2 locations

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