Code Duplication    Length = 71-75 lines in 2 locations

lib/api_client.js 2 locations

@@ 1419-1493 (lines=75) @@
1416
                checksum,
1417
                keyIndex,
1418
                options.support_secret || null,
1419
                options.segwit || null
1420
            )
1421
                .then(
1422
                    // result, deferred, self(apiclient)
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_V3,
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_V3,
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 ? EncryptionMnemonic.encode(options.encryptedPrimarySeed) : null,
1462
                                    backupSeed: options.backupSeed ? bip39.entropyToMnemonic(options.backupSeed) : null,
1463
                                    recoveryEncryptedSecret: options.recoveryEncryptedSecret ?
1464
                                        EncryptionMnemonic.encode(options.recoveryEncryptedSecret) : null,
1465
                                    encryptedSecret: options.encryptedSecret ? EncryptionMnemonic.encode(options.encryptedSecret) : null,
1466
                                    blocktrailPublicKeys: blocktrailPublicKeys
1467
                                }
1468
                            ];
1469
                        });
1470
                    }
1471
                );
1472
        })
1473
        .then(function(r) { deferred.resolve(r); }, function(e) { deferred.reject(e); });
1474
1475
    return deferred.promise;
1476
};
1477
1478
function verifyPublicBip32Key(bip32Key, network) {
1479
    var hk = bitcoin.HDNode.fromBase58(bip32Key[0], network);
1480
    if (typeof hk.keyPair.d !== "undefined") {
1481
        throw new Error('BIP32Key contained private key material - abort');
1482
    }
1483
1484
    if (bip32Key[1].slice(0, 1) !== "M") {
1485
        throw new Error("BIP32Key contained non-public path - abort");
1486
    }
1487
}
1488
1489
function verifyPublicOnly(walletData, network) {
1490
    verifyPublicBip32Key(walletData.primary_public_key, network);
1491
    verifyPublicBip32Key(walletData.backup_public_key, network);
1492
}
1493
1494
/**
1495
 * create wallet using the API
1496
 *
@@ 1528-1598 (lines=71) @@
1525
1526
/**
1527
 * create wallet using the API
1528
 *
1529
 * @param identifier            string      the wallet identifier to create
1530
 * @param primaryPublicKey      array       the primary public key - [key, path] should be M/<keyIndex>'
1531
 * @param backupPublicKey       array       the backup public key - [key, path] should be M/<keyIndex>'
1532
 * @param encryptedPrimarySeed  string      openssl format
1533
 * @param encryptedSecret       string      openssl format
1534
 * @param recoverySecret        string      openssl format
1535
 * @param checksum              string      checksum to store
1536
 * @param keyIndex              int         keyIndex that was used to create wallet
1537
 * @param supportSecret         string
1538
 * @param segwit                bool
1539
 * @returns {q.Promise}
1540
 */
1541
APIClient.prototype.storeNewWalletV2 = function(identifier, primaryPublicKey, backupPublicKey, encryptedPrimarySeed, encryptedSecret,
1542
                                                recoverySecret, checksum, keyIndex, supportSecret, segwit) {
1543
    var self = this;
1544
1545
    var postData = {
1546
        identifier: identifier,
1547
        wallet_version: Wallet.WALLET_VERSION_V2,
1548
        primary_public_key: primaryPublicKey,
1549
        backup_public_key: backupPublicKey,
1550
        encrypted_primary_seed: encryptedPrimarySeed,
1551
        encrypted_secret: encryptedSecret,
1552
        recovery_secret: recoverySecret,
1553
        checksum: checksum,
1554
        key_index: keyIndex,
1555
        support_secret: supportSecret || null,
1556
        segwit: segwit
1557
    };
1558
1559
    verifyPublicOnly(postData, self.network);
1560
1561
    return self.client.post("/wallet", null, postData);
1562
};
1563
1564
/**
1565
 * create wallet using the API
1566
 *
1567
 * @param identifier            string      the wallet identifier to create
1568
 * @param primaryPublicKey      array       the primary public key - [key, path] should be M/<keyIndex>'
1569
 * @param backupPublicKey       array       the backup public key - [key, path] should be M/<keyIndex>'
1570
 * @param encryptedPrimarySeed  Buffer      buffer of ciphertext
1571
 * @param encryptedSecret       Buffer      buffer of ciphertext
1572
 * @param recoverySecret        Buffer      buffer of recovery secret
1573
 * @param checksum              string      checksum to store
1574
 * @param keyIndex              int         keyIndex that was used to create wallet
1575
 * @param supportSecret         string
1576
 * @param segwit                bool
1577
 * @returns {q.Promise}
1578
 */
1579
APIClient.prototype.storeNewWalletV3 = function(identifier, primaryPublicKey, backupPublicKey, encryptedPrimarySeed, encryptedSecret,
1580
                                                recoverySecret, checksum, keyIndex, supportSecret, segwit) {
1581
    var self = this;
1582
1583
    var postData = {
1584
        identifier: identifier,
1585
        wallet_version: Wallet.WALLET_VERSION_V3,
1586
        primary_public_key: primaryPublicKey,
1587
        backup_public_key: backupPublicKey,
1588
        encrypted_primary_seed: encryptedPrimarySeed.toString('base64'),
1589
        encrypted_secret: encryptedSecret.toString('base64'),
1590
        recovery_secret: recoverySecret.toString('hex'),
1591
        checksum: checksum,
1592
        key_index: keyIndex,
1593
        support_secret: supportSecret || null,
1594
        segwit: segwit
1595
    };
1596
1597
    verifyPublicOnly(postData, self.network);
1598
1599
    return self.client.post("/wallet", null, postData);
1600
};
1601