Code Duplication    Length = 105-108 lines in 2 locations

lib/api_client.js 2 locations

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