Code Duplication    Length = 104-108 lines in 2 locations

lib/api_client.js 2 locations

@@ 1378-1485 (lines=108) @@
1375
    return deferred.promise;
1376
};
1377
1378
APIClient.prototype._createNewWalletV2 = function(options) {
1379
    var self = this;
1380
1381
    var deferred = q.defer();
1382
1383
    // avoid modifying passed options
1384
    options = _.merge({}, options);
1385
1386
    determineDataStorageV2_3(options)
1387
        .then(function(options) {
1388
            options.passphrase = options.passphrase || options.password;
1389
            delete options.password;
1390
1391
            // avoid deprecated options
1392
            if (options.primaryPrivateKey) {
1393
                throw new blocktrail.WalletInitError("Can't specify; Primary PrivateKey");
1394
            }
1395
1396
            // seed should be provided or generated
1397
            options.primarySeed = options.primarySeed || randomBytes(Wallet.WALLET_ENTROPY_BITS / 8);
1398
1399
            return options;
1400
        })
1401
        .then(function(options) {
1402
            return produceEncryptedDataV2(options, deferred.notify.bind(deferred));
1403
        })
1404
        .then(function(options) {
1405
            return doRemainingWalletDataV2_3(options, self.network, deferred.notify.bind(deferred));
1406
        })
1407
        .then(function(options) {
1408
            // create a checksum of our private key which we'll later use to verify we used the right password
1409
            var pubKeyHash = bitcoin.crypto.hash160(options.primaryPrivateKey.getPublicKeyBuffer());
1410
            var checksum = bitcoin.address.toBase58Check(pubKeyHash, self.network.pubKeyHash);
1411
            var keyIndex = options.keyIndex;
1412
1413
            // send the public keys and encrypted data to server
1414
            return self.storeNewWalletV2(
1415
                options.identifier,
1416
                [options.primaryPublicKey.toBase58(), "M/" + keyIndex + "'"],
1417
                [options.backupPublicKey.toBase58(), "M"],
1418
                options.storeDataOnServer ? options.encryptedPrimarySeed : false,
1419
                options.storeDataOnServer ? options.encryptedSecret : false,
1420
                options.storeDataOnServer ? options.recoverySecret : false,
1421
                checksum,
1422
                keyIndex,
1423
                options.support_secret || null,
1424
                options.segwit || null
1425
            )
1426
                .then(
1427
                function(result) {
1428
                    deferred.notify(APIClient.CREATE_WALLET_PROGRESS_INIT);
1429
1430
                    var blocktrailPublicKeys = _.mapValues(result.blocktrail_public_keys, function(blocktrailPublicKey) {
1431
                        return bitcoin.HDNode.fromBase58(blocktrailPublicKey[0], self.network);
1432
                    });
1433
1434
                    var wallet = new Wallet(
1435
                        self,
1436
                        options.identifier,
1437
                        Wallet.WALLET_VERSION_V2,
1438
                        null,
1439
                        options.storeDataOnServer ? options.encryptedPrimarySeed : null,
1440
                        options.storeDataOnServer ? options.encryptedSecret : null,
1441
                        {keyIndex: options.primaryPublicKey},
1442
                        options.backupPublicKey,
1443
                        blocktrailPublicKeys,
1444
                        keyIndex,
1445
                        result.segwit || 0,
1446
                        self.testnet,
1447
                        checksum,
1448
                        result.upgrade_key_index,
1449
                        options.useCashAddress,
1450
                        options.bypassNewAddressCheck
1451
                    );
1452
1453
                    // pass along decrypted data to avoid extra work
1454
                    return wallet.unlock({
1455
                        walletVersion: Wallet.WALLET_VERSION_V2,
1456
                        passphrase: options.passphrase,
1457
                        primarySeed: options.primarySeed,
1458
                        secret: options.secret
1459
                    }).then(function() {
1460
                        deferred.notify(APIClient.CREATE_WALLET_PROGRESS_DONE);
1461
                        return [
1462
                            wallet,
1463
                            {
1464
                                walletVersion: wallet.walletVersion,
1465
                                encryptedPrimarySeed: options.encryptedPrimarySeed ?
1466
                                    bip39.entropyToMnemonic(blocktrail.convert(options.encryptedPrimarySeed, 'base64', 'hex')) :
1467
                                    null,
1468
                                backupSeed: options.backupSeed ? bip39.entropyToMnemonic(options.backupSeed.toString('hex')) : null,
1469
                                recoveryEncryptedSecret: options.recoveryEncryptedSecret ?
1470
                                    bip39.entropyToMnemonic(blocktrail.convert(options.recoveryEncryptedSecret, 'base64', 'hex')) :
1471
                                    null,
1472
                                encryptedSecret: options.encryptedSecret ?
1473
                                    bip39.entropyToMnemonic(blocktrail.convert(options.encryptedSecret, 'base64', 'hex')) :
1474
                                    null,
1475
                                blocktrailPublicKeys: blocktrailPublicKeys
1476
                            }
1477
                        ];
1478
                    });
1479
                }
1480
            );
1481
        })
1482
       .then(function(r) { deferred.resolve(r); }, function(e) { deferred.reject(e); });
1483
1484
    return deferred.promise;
1485
};
1486
1487
APIClient.prototype._createNewWalletV3 = function(options) {
1488
    var self = this;
@@ 1487-1590 (lines=104) @@
1484
    return deferred.promise;
1485
};
1486
1487
APIClient.prototype._createNewWalletV3 = function(options) {
1488
    var self = this;
1489
1490
    var deferred = q.defer();
1491
1492
    // avoid modifying passed options
1493
    options = _.merge({}, options);
1494
1495
    determineDataStorageV2_3(options)
1496
        .then(function(options) {
1497
            options.passphrase = options.passphrase || options.password;
1498
            delete options.password;
1499
1500
            // avoid deprecated options
1501
            if (options.primaryPrivateKey) {
1502
                throw new blocktrail.WalletInitError("Can't specify; Primary PrivateKey");
1503
            }
1504
1505
            // seed should be provided or generated
1506
            options.primarySeed = options.primarySeed || randomBytes(Wallet.WALLET_ENTROPY_BITS / 8);
1507
1508
            return options;
1509
        })
1510
        .then(function(options) {
1511
            return self.produceEncryptedDataV3(options, deferred.notify.bind(deferred));
1512
        })
1513
        .then(function(options) {
1514
            return doRemainingWalletDataV2_3(options, self.network, deferred.notify.bind(deferred));
1515
        })
1516
        .then(function(options) {
1517
            // create a checksum of our private key which we'll later use to verify we used the right password
1518
            var pubKeyHash = bitcoin.crypto.hash160(options.primaryPrivateKey.getPublicKeyBuffer());
1519
            var checksum = bitcoin.address.toBase58Check(pubKeyHash, self.network.pubKeyHash);
1520
            var keyIndex = options.keyIndex;
1521
1522
            // send the public keys and encrypted data to server
1523
            return self.storeNewWalletV3(
1524
                options.identifier,
1525
                [options.primaryPublicKey.toBase58(), "M/" + keyIndex + "'"],
1526
                [options.backupPublicKey.toBase58(), "M"],
1527
                options.storeDataOnServer ? options.encryptedPrimarySeed : false,
1528
                options.storeDataOnServer ? options.encryptedSecret : false,
1529
                options.storeDataOnServer ? options.recoverySecret : false,
1530
                checksum,
1531
                keyIndex,
1532
                options.support_secret || null,
1533
                options.segwit || null
1534
            )
1535
                .then(
1536
                    // result, deferred, self(apiclient)
1537
                    function(result) {
1538
                        deferred.notify(APIClient.CREATE_WALLET_PROGRESS_INIT);
1539
1540
                        var blocktrailPublicKeys = _.mapValues(result.blocktrail_public_keys, function(blocktrailPublicKey) {
1541
                            return bitcoin.HDNode.fromBase58(blocktrailPublicKey[0], self.network);
1542
                        });
1543
1544
                        var wallet = new Wallet(
1545
                            self,
1546
                            options.identifier,
1547
                            Wallet.WALLET_VERSION_V3,
1548
                            null,
1549
                            options.storeDataOnServer ? options.encryptedPrimarySeed : null,
1550
                            options.storeDataOnServer ? options.encryptedSecret : null,
1551
                            {keyIndex: options.primaryPublicKey},
1552
                            options.backupPublicKey,
1553
                            blocktrailPublicKeys,
1554
                            keyIndex,
1555
                            result.segwit || 0,
1556
                            self.testnet,
1557
                            checksum,
1558
                            result.upgrade_key_index,
1559
                            options.useCashAddress,
1560
                            options.bypassNewAddressCheck
1561
                        );
1562
1563
                        // pass along decrypted data to avoid extra work
1564
                        return wallet.unlock({
1565
                            walletVersion: Wallet.WALLET_VERSION_V3,
1566
                            passphrase: options.passphrase,
1567
                            primarySeed: options.primarySeed,
1568
                            secret: options.secret
1569
                        }).then(function() {
1570
                            deferred.notify(APIClient.CREATE_WALLET_PROGRESS_DONE);
1571
                            return [
1572
                                wallet,
1573
                                {
1574
                                    walletVersion: wallet.walletVersion,
1575
                                    encryptedPrimarySeed: options.encryptedPrimarySeed ? EncryptionMnemonic.encode(options.encryptedPrimarySeed) : null,
1576
                                    backupSeed: options.backupSeed ? bip39.entropyToMnemonic(options.backupSeed) : null,
1577
                                    recoveryEncryptedSecret: options.recoveryEncryptedSecret ?
1578
                                        EncryptionMnemonic.encode(options.recoveryEncryptedSecret) : null,
1579
                                    encryptedSecret: options.encryptedSecret ? EncryptionMnemonic.encode(options.encryptedSecret) : null,
1580
                                    blocktrailPublicKeys: blocktrailPublicKeys
1581
                                }
1582
                            ];
1583
                        });
1584
                    }
1585
                );
1586
        })
1587
        .then(function(r) { deferred.resolve(r); }, function(e) { deferred.reject(e); });
1588
1589
    return deferred.promise;
1590
};
1591
1592
function verifyPublicBip32Key(bip32Key, network) {
1593
    var hk = bitcoin.HDNode.fromBase58(bip32Key[0], network);