Code Duplication    Length = 104-108 lines in 2 locations

lib/api_client.js 2 locations

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