Code Duplication    Length = 105-109 lines in 2 locations

lib/api_client.js 2 locations

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