Code Duplication    Length = 104-108 lines in 2 locations

lib/api_client.js 2 locations

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