Code Duplication    Length = 104-108 lines in 2 locations

lib/api_client.js 2 locations

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