Code Duplication    Length = 104-108 lines in 2 locations

lib/api_client.js 2 locations

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