Code Duplication    Length = 104-108 lines in 2 locations

lib/api_client.js 2 locations

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