Code Duplication    Length = 105-109 lines in 2 locations

lib/api_client.js 2 locations

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