Code Duplication    Length = 105-109 lines in 2 locations

lib/api_client.js 2 locations

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