Code Duplication    Length = 105-109 lines in 2 locations

lib/api_client.js 2 locations

@@ 1283-1391 (lines=109) @@
1280
                deferred.resolve(r);
1281
            },
1282
            function(e) {
1283
                deferred.reject(e);
1284
            }
1285
        )
1286
        ;
1287
    });
1288
1289
    return deferred.promise;
1290
};
1291
1292
APIClient.prototype._createNewWalletV2 = function(options) {
1293
    var self = this;
1294
1295
    var deferred = q.defer();
1296
1297
    // avoid modifying passed options
1298
    options = _.merge({}, options);
1299
1300
    determineDataStorageV2_3(options)
1301
        .then(function(options) {
1302
            options.passphrase = options.passphrase || options.password;
1303
            delete options.password;
1304
1305
            // avoid deprecated options
1306
            if (options.primaryPrivateKey) {
1307
                throw new blocktrail.WalletInitError("Can't specify; Primary PrivateKey");
1308
            }
1309
1310
            // seed should be provided or generated
1311
            options.primarySeed = options.primarySeed || randomBytes(Wallet.WALLET_ENTROPY_BITS / 8);
1312
1313
            return options;
1314
        })
1315
        .then(function(options) {
1316
            return produceEncryptedDataV2(options, deferred.notify.bind(deferred));
1317
        })
1318
        .then(function(options) {
1319
            return doRemainingWalletDataV2_3(options, self.network, deferred.notify.bind(deferred));
1320
        })
1321
        .then(function(options) {
1322
            // create a checksum of our private key which we'll later use to verify we used the right password
1323
            var pubKeyHash = bitcoin.crypto.hash160(options.primaryPrivateKey.getPublicKeyBuffer());
1324
            var checksum = bitcoin.address.toBase58Check(pubKeyHash, self.network.pubKeyHash);
1325
            var keyIndex = options.keyIndex;
1326
1327
            // send the public keys and encrypted data to server
1328
            return self.storeNewWalletV2(
1329
                options.identifier,
1330
                [options.primaryPublicKey.toBase58(), "M/" + keyIndex + "'"],
1331
                [options.backupPublicKey.toBase58(), "M"],
1332
                options.storeDataOnServer ? options.encryptedPrimarySeed : false,
1333
                options.storeDataOnServer ? options.encryptedSecret : false,
1334
                options.storeDataOnServer ? options.recoverySecret : false,
1335
                checksum,
1336
                keyIndex,
1337
                options.support_secret || null,
1338
                options.segwit || null
1339
            )
1340
                .then(
1341
                function(result) {
1342
                    deferred.notify(APIClient.CREATE_WALLET_PROGRESS_INIT);
1343
1344
                    var blocktrailPublicKeys = _.mapValues(result.blocktrail_public_keys, function(blocktrailPublicKey) {
1345
                        return bitcoin.HDNode.fromBase58(blocktrailPublicKey[0], self.network);
1346
                    });
1347
1348
                    var wallet = new Wallet(
1349
                        self,
1350
                        options.identifier,
1351
                        Wallet.WALLET_VERSION_V2,
1352
                        null,
1353
                        options.storeDataOnServer ? options.encryptedPrimarySeed : null,
1354
                        options.storeDataOnServer ? options.encryptedSecret : null,
1355
                        {keyIndex: options.primaryPublicKey},
1356
                        options.backupPublicKey,
1357
                        blocktrailPublicKeys,
1358
                        keyIndex,
1359
                        result.segwit || 0,
1360
                        self.testnet,
1361
                        self.regtest,
1362
                        checksum,
1363
                        result.upgrade_key_index,
1364
                        options.useCashAddress,
1365
                        options.bypassNewAddressCheck
1366
                    );
1367
1368
                    // pass along decrypted data to avoid extra work
1369
                    return wallet.unlock({
1370
                        walletVersion: Wallet.WALLET_VERSION_V2,
1371
                        passphrase: options.passphrase,
1372
                        primarySeed: options.primarySeed,
1373
                        secret: options.secret
1374
                    }).then(function() {
1375
                        deferred.notify(APIClient.CREATE_WALLET_PROGRESS_DONE);
1376
                        return [
1377
                            wallet,
1378
                            {
1379
                                walletVersion: wallet.walletVersion,
1380
                                encryptedPrimarySeed: options.encryptedPrimarySeed ?
1381
                                    bip39.entropyToMnemonic(blocktrail.convert(options.encryptedPrimarySeed, 'base64', 'hex')) :
1382
                                    null,
1383
                                backupSeed: options.backupSeed ? bip39.entropyToMnemonic(options.backupSeed.toString('hex')) : null,
1384
                                recoveryEncryptedSecret: options.recoveryEncryptedSecret ?
1385
                                    bip39.entropyToMnemonic(blocktrail.convert(options.recoveryEncryptedSecret, 'base64', 'hex')) :
1386
                                    null,
1387
                                encryptedSecret: options.encryptedSecret ?
1388
                                    bip39.entropyToMnemonic(blocktrail.convert(options.encryptedSecret, 'base64', 'hex')) :
1389
                                    null,
1390
                                blocktrailPublicKeys: blocktrailPublicKeys
1391
                            }
1392
                        ];
1393
                    });
1394
                }
@@ 1393-1497 (lines=105) @@
1390
                                blocktrailPublicKeys: blocktrailPublicKeys
1391
                            }
1392
                        ];
1393
                    });
1394
                }
1395
            );
1396
        })
1397
       .then(function(r) { deferred.resolve(r); }, function(e) { deferred.reject(e); });
1398
1399
    return deferred.promise;
1400
};
1401
1402
APIClient.prototype._createNewWalletV3 = function(options) {
1403
    var self = this;
1404
1405
    var deferred = q.defer();
1406
1407
    // avoid modifying passed options
1408
    options = _.merge({}, options);
1409
1410
    determineDataStorageV2_3(options)
1411
        .then(function(options) {
1412
            options.passphrase = options.passphrase || options.password;
1413
            delete options.password;
1414
1415
            // avoid deprecated options
1416
            if (options.primaryPrivateKey) {
1417
                throw new blocktrail.WalletInitError("Can't specify; Primary PrivateKey");
1418
            }
1419
1420
            // seed should be provided or generated
1421
            options.primarySeed = options.primarySeed || randomBytes(Wallet.WALLET_ENTROPY_BITS / 8);
1422
1423
            return options;
1424
        })
1425
        .then(function(options) {
1426
            return self.produceEncryptedDataV3(options, deferred.notify.bind(deferred));
1427
        })
1428
        .then(function(options) {
1429
            return doRemainingWalletDataV2_3(options, self.network, deferred.notify.bind(deferred));
1430
        })
1431
        .then(function(options) {
1432
            // create a checksum of our private key which we'll later use to verify we used the right password
1433
            var pubKeyHash = bitcoin.crypto.hash160(options.primaryPrivateKey.getPublicKeyBuffer());
1434
            var checksum = bitcoin.address.toBase58Check(pubKeyHash, self.network.pubKeyHash);
1435
            var keyIndex = options.keyIndex;
1436
1437
            // send the public keys and encrypted data to server
1438
            return self.storeNewWalletV3(
1439
                options.identifier,
1440
                [options.primaryPublicKey.toBase58(), "M/" + keyIndex + "'"],
1441
                [options.backupPublicKey.toBase58(), "M"],
1442
                options.storeDataOnServer ? options.encryptedPrimarySeed : false,
1443
                options.storeDataOnServer ? options.encryptedSecret : false,
1444
                options.storeDataOnServer ? options.recoverySecret : false,
1445
                checksum,
1446
                keyIndex,
1447
                options.support_secret || null,
1448
                options.segwit || null
1449
            )
1450
                .then(
1451
                    // result, deferred, self(apiclient)
1452
                    function(result) {
1453
                        deferred.notify(APIClient.CREATE_WALLET_PROGRESS_INIT);
1454
1455
                        var blocktrailPublicKeys = _.mapValues(result.blocktrail_public_keys, function(blocktrailPublicKey) {
1456
                            return bitcoin.HDNode.fromBase58(blocktrailPublicKey[0], self.network);
1457
                        });
1458
1459
                        var wallet = new Wallet(
1460
                            self,
1461
                            options.identifier,
1462
                            Wallet.WALLET_VERSION_V3,
1463
                            null,
1464
                            options.storeDataOnServer ? options.encryptedPrimarySeed : null,
1465
                            options.storeDataOnServer ? options.encryptedSecret : null,
1466
                            {keyIndex: options.primaryPublicKey},
1467
                            options.backupPublicKey,
1468
                            blocktrailPublicKeys,
1469
                            keyIndex,
1470
                            result.segwit || 0,
1471
                            self.testnet,
1472
                            self.regtest,
1473
                            checksum,
1474
                            result.upgrade_key_index,
1475
                            options.useCashAddress,
1476
                            options.bypassNewAddressCheck
1477
                        );
1478
1479
                        // pass along decrypted data to avoid extra work
1480
                        return wallet.unlock({
1481
                            walletVersion: Wallet.WALLET_VERSION_V3,
1482
                            passphrase: options.passphrase,
1483
                            primarySeed: options.primarySeed,
1484
                            secret: options.secret
1485
                        }).then(function() {
1486
                            deferred.notify(APIClient.CREATE_WALLET_PROGRESS_DONE);
1487
                            return [
1488
                                wallet,
1489
                                {
1490
                                    walletVersion: wallet.walletVersion,
1491
                                    encryptedPrimarySeed: options.encryptedPrimarySeed ? EncryptionMnemonic.encode(options.encryptedPrimarySeed) : null,
1492
                                    backupSeed: options.backupSeed ? bip39.entropyToMnemonic(options.backupSeed) : null,
1493
                                    recoveryEncryptedSecret: options.recoveryEncryptedSecret ?
1494
                                        EncryptionMnemonic.encode(options.recoveryEncryptedSecret) : null,
1495
                                    encryptedSecret: options.encryptedSecret ? EncryptionMnemonic.encode(options.encryptedSecret) : null,
1496
                                    blocktrailPublicKeys: blocktrailPublicKeys
1497
                                }
1498
                            ];
1499
                        });
1500
                    }