Code Duplication    Length = 71-75 lines in 2 locations

lib/api_client.js 2 locations

@@ 1419-1493 (lines=75) @@
1416
        .then(function(options) {
1417
            return doRemainingWalletDataV2_3(options, self.network, deferred.notify.bind(deferred));
1418
        })
1419
        .then(function(options) {
1420
            // create a checksum of our private key which we'll later use to verify we used the right password
1421
            var pubKeyHash = bitcoin.crypto.hash160(options.primaryPrivateKey.getPublicKeyBuffer());
1422
            var checksum = bitcoin.address.toBase58Check(pubKeyHash, self.network.pubKeyHash);
1423
            var keyIndex = options.keyIndex;
1424
1425
            // send the public keys and encrypted data to server
1426
            return self.storeNewWalletV2(
1427
                options.identifier,
1428
                [options.primaryPublicKey.toBase58(), "M/" + keyIndex + "'"],
1429
                [options.backupPublicKey.toBase58(), "M"],
1430
                options.storeDataOnServer ? options.encryptedPrimarySeed : false,
1431
                options.storeDataOnServer ? options.encryptedSecret : false,
1432
                options.storeDataOnServer ? options.recoverySecret : false,
1433
                checksum,
1434
                keyIndex,
1435
                options.support_secret || null,
1436
                options.segwit || null
1437
            )
1438
                .then(
1439
                function(result) {
1440
                    deferred.notify(APIClient.CREATE_WALLET_PROGRESS_INIT);
1441
1442
                    var blocktrailPublicKeys = _.mapValues(result.blocktrail_public_keys, function(blocktrailPublicKey) {
1443
                        return bitcoin.HDNode.fromBase58(blocktrailPublicKey[0], self.network);
1444
                    });
1445
1446
                    var wallet = new Wallet(
1447
                        self,
1448
                        options.identifier,
1449
                        Wallet.WALLET_VERSION_V2,
1450
                        null,
1451
                        options.storeDataOnServer ? options.encryptedPrimarySeed : null,
1452
                        options.storeDataOnServer ? options.encryptedSecret : null,
1453
                        {keyIndex: options.primaryPublicKey},
1454
                        options.backupPublicKey,
1455
                        blocktrailPublicKeys,
1456
                        keyIndex,
1457
                        result.segwit || 0,
1458
                        self.testnet,
1459
                        checksum,
1460
                        result.upgrade_key_index,
1461
                        options.useCashAddress,
1462
                        options.bypassNewAddressCheck
1463
                    );
1464
1465
                    // pass along decrypted data to avoid extra work
1466
                    return wallet.unlock({
1467
                        walletVersion: Wallet.WALLET_VERSION_V2,
1468
                        passphrase: options.passphrase,
1469
                        primarySeed: options.primarySeed,
1470
                        secret: options.secret
1471
                    }).then(function() {
1472
                        deferred.notify(APIClient.CREATE_WALLET_PROGRESS_DONE);
1473
                        return [
1474
                            wallet,
1475
                            {
1476
                                walletVersion: wallet.walletVersion,
1477
                                encryptedPrimarySeed: options.encryptedPrimarySeed ?
1478
                                    bip39.entropyToMnemonic(blocktrail.convert(options.encryptedPrimarySeed, 'base64', 'hex')) :
1479
                                    null,
1480
                                backupSeed: options.backupSeed ? bip39.entropyToMnemonic(options.backupSeed.toString('hex')) : null,
1481
                                recoveryEncryptedSecret: options.recoveryEncryptedSecret ?
1482
                                    bip39.entropyToMnemonic(blocktrail.convert(options.recoveryEncryptedSecret, 'base64', 'hex')) :
1483
                                    null,
1484
                                encryptedSecret: options.encryptedSecret ?
1485
                                    bip39.entropyToMnemonic(blocktrail.convert(options.encryptedSecret, 'base64', 'hex')) :
1486
                                    null,
1487
                                blocktrailPublicKeys: blocktrailPublicKeys
1488
                            }
1489
                        ];
1490
                    });
1491
                }
1492
            );
1493
        })
1494
       .then(function(r) { deferred.resolve(r); }, function(e) { deferred.reject(e); });
1495
1496
    return deferred.promise;
@@ 1528-1598 (lines=71) @@
1525
        .then(function(options) {
1526
            return doRemainingWalletDataV2_3(options, self.network, deferred.notify.bind(deferred));
1527
        })
1528
        .then(function(options) {
1529
            // create a checksum of our private key which we'll later use to verify we used the right password
1530
            var pubKeyHash = bitcoin.crypto.hash160(options.primaryPrivateKey.getPublicKeyBuffer());
1531
            var checksum = bitcoin.address.toBase58Check(pubKeyHash, self.network.pubKeyHash);
1532
            var keyIndex = options.keyIndex;
1533
1534
            // send the public keys and encrypted data to server
1535
            return self.storeNewWalletV3(
1536
                options.identifier,
1537
                [options.primaryPublicKey.toBase58(), "M/" + keyIndex + "'"],
1538
                [options.backupPublicKey.toBase58(), "M"],
1539
                options.storeDataOnServer ? options.encryptedPrimarySeed : false,
1540
                options.storeDataOnServer ? options.encryptedSecret : false,
1541
                options.storeDataOnServer ? options.recoverySecret : false,
1542
                checksum,
1543
                keyIndex,
1544
                options.support_secret || null,
1545
                options.segwit || null
1546
            )
1547
                .then(
1548
                    // result, deferred, self(apiclient)
1549
                    function(result) {
1550
                        deferred.notify(APIClient.CREATE_WALLET_PROGRESS_INIT);
1551
1552
                        var blocktrailPublicKeys = _.mapValues(result.blocktrail_public_keys, function(blocktrailPublicKey) {
1553
                            return bitcoin.HDNode.fromBase58(blocktrailPublicKey[0], self.network);
1554
                        });
1555
1556
                        var wallet = new Wallet(
1557
                            self,
1558
                            options.identifier,
1559
                            Wallet.WALLET_VERSION_V3,
1560
                            null,
1561
                            options.storeDataOnServer ? options.encryptedPrimarySeed : null,
1562
                            options.storeDataOnServer ? options.encryptedSecret : null,
1563
                            {keyIndex: options.primaryPublicKey},
1564
                            options.backupPublicKey,
1565
                            blocktrailPublicKeys,
1566
                            keyIndex,
1567
                            result.segwit || 0,
1568
                            self.testnet,
1569
                            checksum,
1570
                            result.upgrade_key_index,
1571
                            options.useCashAddress,
1572
                            options.bypassNewAddressCheck
1573
                        );
1574
1575
                        // pass along decrypted data to avoid extra work
1576
                        return wallet.unlock({
1577
                            walletVersion: Wallet.WALLET_VERSION_V3,
1578
                            passphrase: options.passphrase,
1579
                            primarySeed: options.primarySeed,
1580
                            secret: options.secret
1581
                        }).then(function() {
1582
                            deferred.notify(APIClient.CREATE_WALLET_PROGRESS_DONE);
1583
                            return [
1584
                                wallet,
1585
                                {
1586
                                    walletVersion: wallet.walletVersion,
1587
                                    encryptedPrimarySeed: options.encryptedPrimarySeed ? EncryptionMnemonic.encode(options.encryptedPrimarySeed) : null,
1588
                                    backupSeed: options.backupSeed ? bip39.entropyToMnemonic(options.backupSeed) : null,
1589
                                    recoveryEncryptedSecret: options.recoveryEncryptedSecret ?
1590
                                        EncryptionMnemonic.encode(options.recoveryEncryptedSecret) : null,
1591
                                    encryptedSecret: options.encryptedSecret ? EncryptionMnemonic.encode(options.encryptedSecret) : null,
1592
                                    blocktrailPublicKeys: blocktrailPublicKeys
1593
                                }
1594
                            ];
1595
                        });
1596
                    }
1597
                );
1598
        })
1599
        .then(function(r) { deferred.resolve(r); }, function(e) { deferred.reject(e); });
1600
1601
    return deferred.promise;