json-crowdsale.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. "use strict";
  2. /**
  3. * @_subsection: api/wallet:JSON Wallets [json-wallets]
  4. */
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.decryptCrowdsaleJson = exports.isCrowdsaleJson = void 0;
  7. const aes_js_1 = require("aes-js");
  8. const index_js_1 = require("../address/index.js");
  9. const index_js_2 = require("../crypto/index.js");
  10. const index_js_3 = require("../hash/index.js");
  11. const index_js_4 = require("../utils/index.js");
  12. const utils_js_1 = require("./utils.js");
  13. /**
  14. * Returns true if %%json%% is a valid JSON Crowdsale wallet.
  15. */
  16. function isCrowdsaleJson(json) {
  17. try {
  18. const data = JSON.parse(json);
  19. if (data.encseed) {
  20. return true;
  21. }
  22. }
  23. catch (error) { }
  24. return false;
  25. }
  26. exports.isCrowdsaleJson = isCrowdsaleJson;
  27. // See: https://github.com/ethereum/pyethsaletool
  28. /**
  29. * Before Ethereum launched, it was necessary to create a wallet
  30. * format for backers to use, which would be used to receive ether
  31. * as a reward for contributing to the project.
  32. *
  33. * The [[link-crowdsale]] format is now obsolete, but it is still
  34. * useful to support and the additional code is fairly trivial as
  35. * all the primitives required are used through core portions of
  36. * the library.
  37. */
  38. function decryptCrowdsaleJson(json, _password) {
  39. const data = JSON.parse(json);
  40. const password = (0, utils_js_1.getPassword)(_password);
  41. // Ethereum Address
  42. const address = (0, index_js_1.getAddress)((0, utils_js_1.spelunk)(data, "ethaddr:string!"));
  43. // Encrypted Seed
  44. const encseed = (0, utils_js_1.looseArrayify)((0, utils_js_1.spelunk)(data, "encseed:string!"));
  45. (0, index_js_4.assertArgument)(encseed && (encseed.length % 16) === 0, "invalid encseed", "json", json);
  46. const key = (0, index_js_4.getBytes)((0, index_js_2.pbkdf2)(password, password, 2000, 32, "sha256")).slice(0, 16);
  47. const iv = encseed.slice(0, 16);
  48. const encryptedSeed = encseed.slice(16);
  49. // Decrypt the seed
  50. const aesCbc = new aes_js_1.CBC(key, iv);
  51. const seed = (0, aes_js_1.pkcs7Strip)((0, index_js_4.getBytes)(aesCbc.decrypt(encryptedSeed)));
  52. // This wallet format is weird... Convert the binary encoded hex to a string.
  53. let seedHex = "";
  54. for (let i = 0; i < seed.length; i++) {
  55. seedHex += String.fromCharCode(seed[i]);
  56. }
  57. return { address, privateKey: (0, index_js_3.id)(seedHex) };
  58. }
  59. exports.decryptCrowdsaleJson = decryptCrowdsaleJson;
  60. //# sourceMappingURL=json-crowdsale.js.map