json-crowdsale.js 2.1 KB

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