Code Duplication    Length = 104-108 lines in 2 locations

lib/api_client.js 2 locations

@@ 1155-1262 (lines=108) @@
1152
1153
    var deferred = q.defer();
1154
1155
    q.nextTick(function() {
1156
1157
        if (!options.primaryMnemonic && !options.primarySeed) {
1158
            if (!options.passphrase && !options.password) {
1159
                deferred.reject(new blocktrail.WalletCreateError("Can't generate Primary Mnemonic without a passphrase"));
1160
                return deferred.promise;
1161
            } else {
1162
                options.primaryMnemonic = bip39.generateMnemonic(Wallet.WALLET_ENTROPY_BITS);
1163
                if (options.storePrimaryMnemonic !== false) {
1164
                    options.storePrimaryMnemonic = true;
1165
                }
1166
            }
1167
        }
1168
1169
        if (!options.backupMnemonic && !options.backupPublicKey) {
1170
            options.backupMnemonic = bip39.generateMnemonic(Wallet.WALLET_ENTROPY_BITS);
1171
        }
1172
1173
        deferred.notify(APIClient.CREATE_WALLET_PROGRESS_PRIMARY);
1174
1175
        self.resolvePrimaryPrivateKeyFromOptions(options)
1176
            .then(function(options) {
1177
                deferred.notify(APIClient.CREATE_WALLET_PROGRESS_BACKUP);
1178
1179
                return self.resolveBackupPublicKeyFromOptions(options)
1180
                    .then(function(options) {
1181
                        deferred.notify(APIClient.CREATE_WALLET_PROGRESS_SUBMIT);
1182
1183
                        // create a checksum of our private key which we'll later use to verify we used the right password
1184
                        var pubKeyHash = bitcoin.crypto.hash160(options.primaryPrivateKey.getPublicKeyBuffer());
1185
                        var checksum = bitcoin.address.toBase58Check(pubKeyHash, self.network.pubKeyHash);
1186
                        var keyIndex = options.keyIndex;
1187
1188
                        var primaryPublicKey = options.primaryPrivateKey.deriveHardened(keyIndex).neutered();
1189
1190
                        // send the public keys to the server to store them
1191
                        //  and the mnemonic, which is safe because it's useless without the password
1192
                        return self.storeNewWalletV1(
1193
                            options.identifier,
1194
                            [primaryPublicKey.toBase58(), "M/" + keyIndex + "'"],
1195
                            [options.backupPublicKey.toBase58(), "M"],
1196
                            options.storePrimaryMnemonic ? options.primaryMnemonic : false,
1197
                            checksum,
1198
                            keyIndex,
1199
                            options.segwit || null
1200
                        )
1201
                            .then(function(result) {
1202
                                deferred.notify(APIClient.CREATE_WALLET_PROGRESS_INIT);
1203
1204
                                var blocktrailPublicKeys = _.mapValues(result.blocktrail_public_keys, function(blocktrailPublicKey) {
1205
                                    return bitcoin.HDNode.fromBase58(blocktrailPublicKey[0], self.network);
1206
                                });
1207
1208
                                var wallet = new Wallet(
1209
                                    self,
1210
                                    options.identifier,
1211
                                    Wallet.WALLET_VERSION_V1,
1212
                                    options.primaryMnemonic,
1213
                                    null,
1214
                                    null,
1215
                                    {keyIndex: primaryPublicKey},
1216
                                    options.backupPublicKey,
1217
                                    blocktrailPublicKeys,
1218
                                    keyIndex,
1219
                                    result.segwit || 0,
1220
                                    self.testnet,
1221
                                    checksum,
1222
                                    result.upgrade_key_index,
1223
                                    options.useCashAddress,
1224
                                    options.bypassNewAddressCheck
1225
                                );
1226
1227
                                return wallet.unlock({
1228
                                    walletVersion: Wallet.WALLET_VERSION_V1,
1229
                                    passphrase: options.passphrase,
1230
                                    primarySeed: options.primarySeed,
1231
                                    primaryMnemonic: null // explicit null
1232
                                }).then(function() {
1233
                                    deferred.notify(APIClient.CREATE_WALLET_PROGRESS_DONE);
1234
                                    return [
1235
                                        wallet,
1236
                                        {
1237
                                            walletVersion: wallet.walletVersion,
1238
                                            primaryMnemonic: options.primaryMnemonic,
1239
                                            backupMnemonic: options.backupMnemonic,
1240
                                            blocktrailPublicKeys: blocktrailPublicKeys
1241
                                        }
1242
                                    ];
1243
                                });
1244
                            });
1245
                    }
1246
                );
1247
            })
1248
            .then(
1249
            function(r) {
1250
                deferred.resolve(r);
1251
            },
1252
            function(e) {
1253
                deferred.reject(e);
1254
            }
1255
        )
1256
        ;
1257
    });
1258
1259
    return deferred.promise;
1260
};
1261
1262
APIClient.prototype._createNewWalletV2 = function(options) {
1263
    var self = this;
1264
1265
    var deferred = q.defer();
@@ 1264-1367 (lines=104) @@
1261
1262
APIClient.prototype._createNewWalletV2 = function(options) {
1263
    var self = this;
1264
1265
    var deferred = q.defer();
1266
1267
    // avoid modifying passed options
1268
    options = _.merge({}, options);
1269
1270
    determineDataStorageV2_3(options)
1271
        .then(function(options) {
1272
            options.passphrase = options.passphrase || options.password;
1273
            delete options.password;
1274
1275
            // avoid deprecated options
1276
            if (options.primaryPrivateKey) {
1277
                throw new blocktrail.WalletInitError("Can't specify; Primary PrivateKey");
1278
            }
1279
1280
            // seed should be provided or generated
1281
            options.primarySeed = options.primarySeed || randomBytes(Wallet.WALLET_ENTROPY_BITS / 8);
1282
1283
            return options;
1284
        })
1285
        .then(function(options) {
1286
            return produceEncryptedDataV2(options, deferred.notify.bind(deferred));
1287
        })
1288
        .then(function(options) {
1289
            return doRemainingWalletDataV2_3(options, self.network, deferred.notify.bind(deferred));
1290
        })
1291
        .then(function(options) {
1292
            // create a checksum of our private key which we'll later use to verify we used the right password
1293
            var pubKeyHash = bitcoin.crypto.hash160(options.primaryPrivateKey.getPublicKeyBuffer());
1294
            var checksum = bitcoin.address.toBase58Check(pubKeyHash, self.network.pubKeyHash);
1295
            var keyIndex = options.keyIndex;
1296
1297
            // send the public keys and encrypted data to server
1298
            return self.storeNewWalletV2(
1299
                options.identifier,
1300
                [options.primaryPublicKey.toBase58(), "M/" + keyIndex + "'"],
1301
                [options.backupPublicKey.toBase58(), "M"],
1302
                options.storeDataOnServer ? options.encryptedPrimarySeed : false,
1303
                options.storeDataOnServer ? options.encryptedSecret : false,
1304
                options.storeDataOnServer ? options.recoverySecret : false,
1305
                checksum,
1306
                keyIndex,
1307
                options.support_secret || null,
1308
                options.segwit || null
1309
            )
1310
                .then(
1311
                function(result) {
1312
                    deferred.notify(APIClient.CREATE_WALLET_PROGRESS_INIT);
1313
1314
                    var blocktrailPublicKeys = _.mapValues(result.blocktrail_public_keys, function(blocktrailPublicKey) {
1315
                        return bitcoin.HDNode.fromBase58(blocktrailPublicKey[0], self.network);
1316
                    });
1317
1318
                    var wallet = new Wallet(
1319
                        self,
1320
                        options.identifier,
1321
                        Wallet.WALLET_VERSION_V2,
1322
                        null,
1323
                        options.storeDataOnServer ? options.encryptedPrimarySeed : null,
1324
                        options.storeDataOnServer ? options.encryptedSecret : null,
1325
                        {keyIndex: options.primaryPublicKey},
1326
                        options.backupPublicKey,
1327
                        blocktrailPublicKeys,
1328
                        keyIndex,
1329
                        result.segwit || 0,
1330
                        self.testnet,
1331
                        checksum,
1332
                        result.upgrade_key_index,
1333
                        options.useCashAddress,
1334
                        options.bypassNewAddressCheck
1335
                    );
1336
1337
                    // pass along decrypted data to avoid extra work
1338
                    return wallet.unlock({
1339
                        walletVersion: Wallet.WALLET_VERSION_V2,
1340
                        passphrase: options.passphrase,
1341
                        primarySeed: options.primarySeed,
1342
                        secret: options.secret
1343
                    }).then(function() {
1344
                        deferred.notify(APIClient.CREATE_WALLET_PROGRESS_DONE);
1345
                        return [
1346
                            wallet,
1347
                            {
1348
                                walletVersion: wallet.walletVersion,
1349
                                encryptedPrimarySeed: options.encryptedPrimarySeed ?
1350
                                    bip39.entropyToMnemonic(blocktrail.convert(options.encryptedPrimarySeed, 'base64', 'hex')) :
1351
                                    null,
1352
                                backupSeed: options.backupSeed ? bip39.entropyToMnemonic(options.backupSeed.toString('hex')) : null,
1353
                                recoveryEncryptedSecret: options.recoveryEncryptedSecret ?
1354
                                    bip39.entropyToMnemonic(blocktrail.convert(options.recoveryEncryptedSecret, 'base64', 'hex')) :
1355
                                    null,
1356
                                encryptedSecret: options.encryptedSecret ?
1357
                                    bip39.entropyToMnemonic(blocktrail.convert(options.encryptedSecret, 'base64', 'hex')) :
1358
                                    null,
1359
                                blocktrailPublicKeys: blocktrailPublicKeys
1360
                            }
1361
                        ];
1362
                    });
1363
                }
1364
            );
1365
        })
1366
       .then(function(r) { deferred.resolve(r); }, function(e) { deferred.reject(e); });
1367
1368
    return deferred.promise;
1369
};
1370