Code Duplication    Length = 105-109 lines in 2 locations

lib/api_client.js 2 locations

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