Code Duplication    Length = 104-108 lines in 2 locations

lib/api_client.js 2 locations

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