Code Duplication    Length = 104-108 lines in 2 locations

lib/api_client.js 2 locations

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