|
@@ 1293-1401 (lines=109) @@
|
| 1290 |
|
return deferred.promise; |
| 1291 |
|
}; |
| 1292 |
|
|
| 1293 |
|
APIClient.prototype._createNewWalletV2 = function(options) { |
| 1294 |
|
var self = this; |
| 1295 |
|
|
| 1296 |
|
var deferred = q.defer(); |
| 1297 |
|
|
| 1298 |
|
// avoid modifying passed options |
| 1299 |
|
options = _.merge({}, options); |
| 1300 |
|
|
| 1301 |
|
determineDataStorageV2_3(options) |
| 1302 |
|
.then(function(options) { |
| 1303 |
|
options.passphrase = options.passphrase || options.password; |
| 1304 |
|
delete options.password; |
| 1305 |
|
|
| 1306 |
|
// avoid deprecated options |
| 1307 |
|
if (options.primaryPrivateKey) { |
| 1308 |
|
throw new blocktrail.WalletInitError("Can't specify; Primary PrivateKey"); |
| 1309 |
|
} |
| 1310 |
|
|
| 1311 |
|
// seed should be provided or generated |
| 1312 |
|
options.primarySeed = options.primarySeed || randomBytes(Wallet.WALLET_ENTROPY_BITS / 8); |
| 1313 |
|
|
| 1314 |
|
return options; |
| 1315 |
|
}) |
| 1316 |
|
.then(function(options) { |
| 1317 |
|
return produceEncryptedDataV2(options, deferred.notify.bind(deferred)); |
| 1318 |
|
}) |
| 1319 |
|
.then(function(options) { |
| 1320 |
|
return doRemainingWalletDataV2_3(options, self.network, deferred.notify.bind(deferred)); |
| 1321 |
|
}) |
| 1322 |
|
.then(function(options) { |
| 1323 |
|
// create a checksum of our private key which we'll later use to verify we used the right password |
| 1324 |
|
var pubKeyHash = bitcoin.crypto.hash160(options.primaryPrivateKey.getPublicKeyBuffer()); |
| 1325 |
|
var checksum = bitcoin.address.toBase58Check(pubKeyHash, self.network.pubKeyHash); |
| 1326 |
|
var keyIndex = options.keyIndex; |
| 1327 |
|
|
| 1328 |
|
// send the public keys and encrypted data to server |
| 1329 |
|
return self.storeNewWalletV2( |
| 1330 |
|
options.identifier, |
| 1331 |
|
[options.primaryPublicKey.toBase58(), "M/" + keyIndex + "'"], |
| 1332 |
|
[options.backupPublicKey.toBase58(), "M"], |
| 1333 |
|
options.storeDataOnServer ? options.encryptedPrimarySeed : false, |
| 1334 |
|
options.storeDataOnServer ? options.encryptedSecret : false, |
| 1335 |
|
options.storeDataOnServer ? options.recoverySecret : false, |
| 1336 |
|
checksum, |
| 1337 |
|
keyIndex, |
| 1338 |
|
options.support_secret || null, |
| 1339 |
|
options.segwit || null |
| 1340 |
|
) |
| 1341 |
|
.then( |
| 1342 |
|
function(result) { |
| 1343 |
|
deferred.notify(APIClient.CREATE_WALLET_PROGRESS_INIT); |
| 1344 |
|
|
| 1345 |
|
var blocktrailPublicKeys = _.mapValues(result.blocktrail_public_keys, function(blocktrailPublicKey) { |
| 1346 |
|
return bitcoin.HDNode.fromBase58(blocktrailPublicKey[0], self.network); |
| 1347 |
|
}); |
| 1348 |
|
|
| 1349 |
|
var wallet = new Wallet( |
| 1350 |
|
self, |
| 1351 |
|
options.identifier, |
| 1352 |
|
Wallet.WALLET_VERSION_V2, |
| 1353 |
|
null, |
| 1354 |
|
options.storeDataOnServer ? options.encryptedPrimarySeed : null, |
| 1355 |
|
options.storeDataOnServer ? options.encryptedSecret : null, |
| 1356 |
|
{keyIndex: options.primaryPublicKey}, |
| 1357 |
|
options.backupPublicKey, |
| 1358 |
|
blocktrailPublicKeys, |
| 1359 |
|
keyIndex, |
| 1360 |
|
result.segwit || 0, |
| 1361 |
|
self.testnet, |
| 1362 |
|
self.regtest, |
| 1363 |
|
checksum, |
| 1364 |
|
result.upgrade_key_index, |
| 1365 |
|
options.useCashAddress, |
| 1366 |
|
options.bypassNewAddressCheck |
| 1367 |
|
); |
| 1368 |
|
|
| 1369 |
|
// pass along decrypted data to avoid extra work |
| 1370 |
|
return wallet.unlock({ |
| 1371 |
|
walletVersion: Wallet.WALLET_VERSION_V2, |
| 1372 |
|
passphrase: options.passphrase, |
| 1373 |
|
primarySeed: options.primarySeed, |
| 1374 |
|
secret: options.secret |
| 1375 |
|
}).then(function() { |
| 1376 |
|
deferred.notify(APIClient.CREATE_WALLET_PROGRESS_DONE); |
| 1377 |
|
return [ |
| 1378 |
|
wallet, |
| 1379 |
|
{ |
| 1380 |
|
walletVersion: wallet.walletVersion, |
| 1381 |
|
encryptedPrimarySeed: options.encryptedPrimarySeed ? |
| 1382 |
|
bip39.entropyToMnemonic(blocktrail.convert(options.encryptedPrimarySeed, 'base64', 'hex')) : |
| 1383 |
|
null, |
| 1384 |
|
backupSeed: options.backupSeed ? bip39.entropyToMnemonic(options.backupSeed.toString('hex')) : null, |
| 1385 |
|
recoveryEncryptedSecret: options.recoveryEncryptedSecret ? |
| 1386 |
|
bip39.entropyToMnemonic(blocktrail.convert(options.recoveryEncryptedSecret, 'base64', 'hex')) : |
| 1387 |
|
null, |
| 1388 |
|
encryptedSecret: options.encryptedSecret ? |
| 1389 |
|
bip39.entropyToMnemonic(blocktrail.convert(options.encryptedSecret, 'base64', 'hex')) : |
| 1390 |
|
null, |
| 1391 |
|
blocktrailPublicKeys: blocktrailPublicKeys |
| 1392 |
|
} |
| 1393 |
|
]; |
| 1394 |
|
}); |
| 1395 |
|
} |
| 1396 |
|
); |
| 1397 |
|
}) |
| 1398 |
|
.then(function(r) { deferred.resolve(r); }, function(e) { deferred.reject(e); }); |
| 1399 |
|
|
| 1400 |
|
return deferred.promise; |
| 1401 |
|
}; |
| 1402 |
|
|
| 1403 |
|
APIClient.prototype._createNewWalletV3 = function(options) { |
| 1404 |
|
var self = this; |
|
@@ 1403-1507 (lines=105) @@
|
| 1400 |
|
return deferred.promise; |
| 1401 |
|
}; |
| 1402 |
|
|
| 1403 |
|
APIClient.prototype._createNewWalletV3 = function(options) { |
| 1404 |
|
var self = this; |
| 1405 |
|
|
| 1406 |
|
var deferred = q.defer(); |
| 1407 |
|
|
| 1408 |
|
// avoid modifying passed options |
| 1409 |
|
options = _.merge({}, options); |
| 1410 |
|
|
| 1411 |
|
determineDataStorageV2_3(options) |
| 1412 |
|
.then(function(options) { |
| 1413 |
|
options.passphrase = options.passphrase || options.password; |
| 1414 |
|
delete options.password; |
| 1415 |
|
|
| 1416 |
|
// avoid deprecated options |
| 1417 |
|
if (options.primaryPrivateKey) { |
| 1418 |
|
throw new blocktrail.WalletInitError("Can't specify; Primary PrivateKey"); |
| 1419 |
|
} |
| 1420 |
|
|
| 1421 |
|
// seed should be provided or generated |
| 1422 |
|
options.primarySeed = options.primarySeed || randomBytes(Wallet.WALLET_ENTROPY_BITS / 8); |
| 1423 |
|
|
| 1424 |
|
return options; |
| 1425 |
|
}) |
| 1426 |
|
.then(function(options) { |
| 1427 |
|
return self.produceEncryptedDataV3(options, deferred.notify.bind(deferred)); |
| 1428 |
|
}) |
| 1429 |
|
.then(function(options) { |
| 1430 |
|
return doRemainingWalletDataV2_3(options, self.network, deferred.notify.bind(deferred)); |
| 1431 |
|
}) |
| 1432 |
|
.then(function(options) { |
| 1433 |
|
// create a checksum of our private key which we'll later use to verify we used the right password |
| 1434 |
|
var pubKeyHash = bitcoin.crypto.hash160(options.primaryPrivateKey.getPublicKeyBuffer()); |
| 1435 |
|
var checksum = bitcoin.address.toBase58Check(pubKeyHash, self.network.pubKeyHash); |
| 1436 |
|
var keyIndex = options.keyIndex; |
| 1437 |
|
|
| 1438 |
|
// send the public keys and encrypted data to server |
| 1439 |
|
return self.storeNewWalletV3( |
| 1440 |
|
options.identifier, |
| 1441 |
|
[options.primaryPublicKey.toBase58(), "M/" + keyIndex + "'"], |
| 1442 |
|
[options.backupPublicKey.toBase58(), "M"], |
| 1443 |
|
options.storeDataOnServer ? options.encryptedPrimarySeed : false, |
| 1444 |
|
options.storeDataOnServer ? options.encryptedSecret : false, |
| 1445 |
|
options.storeDataOnServer ? options.recoverySecret : false, |
| 1446 |
|
checksum, |
| 1447 |
|
keyIndex, |
| 1448 |
|
options.support_secret || null, |
| 1449 |
|
options.segwit || null |
| 1450 |
|
) |
| 1451 |
|
.then( |
| 1452 |
|
// result, deferred, self(apiclient) |
| 1453 |
|
function(result) { |
| 1454 |
|
deferred.notify(APIClient.CREATE_WALLET_PROGRESS_INIT); |
| 1455 |
|
|
| 1456 |
|
var blocktrailPublicKeys = _.mapValues(result.blocktrail_public_keys, function(blocktrailPublicKey) { |
| 1457 |
|
return bitcoin.HDNode.fromBase58(blocktrailPublicKey[0], self.network); |
| 1458 |
|
}); |
| 1459 |
|
|
| 1460 |
|
var wallet = new Wallet( |
| 1461 |
|
self, |
| 1462 |
|
options.identifier, |
| 1463 |
|
Wallet.WALLET_VERSION_V3, |
| 1464 |
|
null, |
| 1465 |
|
options.storeDataOnServer ? options.encryptedPrimarySeed : null, |
| 1466 |
|
options.storeDataOnServer ? options.encryptedSecret : null, |
| 1467 |
|
{keyIndex: options.primaryPublicKey}, |
| 1468 |
|
options.backupPublicKey, |
| 1469 |
|
blocktrailPublicKeys, |
| 1470 |
|
keyIndex, |
| 1471 |
|
result.segwit || 0, |
| 1472 |
|
self.testnet, |
| 1473 |
|
self.regtest, |
| 1474 |
|
checksum, |
| 1475 |
|
result.upgrade_key_index, |
| 1476 |
|
options.useCashAddress, |
| 1477 |
|
options.bypassNewAddressCheck |
| 1478 |
|
); |
| 1479 |
|
|
| 1480 |
|
// pass along decrypted data to avoid extra work |
| 1481 |
|
return wallet.unlock({ |
| 1482 |
|
walletVersion: Wallet.WALLET_VERSION_V3, |
| 1483 |
|
passphrase: options.passphrase, |
| 1484 |
|
primarySeed: options.primarySeed, |
| 1485 |
|
secret: options.secret |
| 1486 |
|
}).then(function() { |
| 1487 |
|
deferred.notify(APIClient.CREATE_WALLET_PROGRESS_DONE); |
| 1488 |
|
return [ |
| 1489 |
|
wallet, |
| 1490 |
|
{ |
| 1491 |
|
walletVersion: wallet.walletVersion, |
| 1492 |
|
encryptedPrimarySeed: options.encryptedPrimarySeed ? EncryptionMnemonic.encode(options.encryptedPrimarySeed) : null, |
| 1493 |
|
backupSeed: options.backupSeed ? bip39.entropyToMnemonic(options.backupSeed) : null, |
| 1494 |
|
recoveryEncryptedSecret: options.recoveryEncryptedSecret ? |
| 1495 |
|
EncryptionMnemonic.encode(options.recoveryEncryptedSecret) : null, |
| 1496 |
|
encryptedSecret: options.encryptedSecret ? EncryptionMnemonic.encode(options.encryptedSecret) : null, |
| 1497 |
|
blocktrailPublicKeys: blocktrailPublicKeys |
| 1498 |
|
} |
| 1499 |
|
]; |
| 1500 |
|
}); |
| 1501 |
|
} |
| 1502 |
|
); |
| 1503 |
|
}) |
| 1504 |
|
.then(function(r) { deferred.resolve(r); }, function(e) { deferred.reject(e); }); |
| 1505 |
|
|
| 1506 |
|
return deferred.promise; |
| 1507 |
|
}; |
| 1508 |
|
|
| 1509 |
|
function verifyPublicBip32Key(bip32Key, network) { |
| 1510 |
|
var hk = bitcoin.HDNode.fromBase58(bip32Key[0], network); |