Code Duplication    Length = 104-108 lines in 2 locations

lib/api_client.js 2 locations

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