wallet.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Wallet = void 0;
  4. const index_js_1 = require("../crypto/index.js");
  5. const index_js_2 = require("../utils/index.js");
  6. const base_wallet_js_1 = require("./base-wallet.js");
  7. const hdwallet_js_1 = require("./hdwallet.js");
  8. const json_crowdsale_js_1 = require("./json-crowdsale.js");
  9. const json_keystore_js_1 = require("./json-keystore.js");
  10. const mnemonic_js_1 = require("./mnemonic.js");
  11. function stall(duration) {
  12. return new Promise((resolve) => { setTimeout(() => { resolve(); }, duration); });
  13. }
  14. /**
  15. * A **Wallet** manages a single private key which is used to sign
  16. * transactions, messages and other common payloads.
  17. *
  18. * This class is generally the main entry point for developers
  19. * that wish to use a private key directly, as it can create
  20. * instances from a large variety of common sources, including
  21. * raw private key, [[link-bip-39]] mnemonics and encrypte JSON
  22. * wallets.
  23. */
  24. class Wallet extends base_wallet_js_1.BaseWallet {
  25. /**
  26. * Create a new wallet for the private %%key%%, optionally connected
  27. * to %%provider%%.
  28. */
  29. constructor(key, provider) {
  30. if (typeof (key) === "string" && !key.startsWith("0x")) {
  31. key = "0x" + key;
  32. }
  33. let signingKey = (typeof (key) === "string") ? new index_js_1.SigningKey(key) : key;
  34. super(signingKey, provider);
  35. }
  36. connect(provider) {
  37. return new Wallet(this.signingKey, provider);
  38. }
  39. /**
  40. * Resolves to a [JSON Keystore Wallet](json-wallets) encrypted with
  41. * %%password%%.
  42. *
  43. * If %%progressCallback%% is specified, it will receive periodic
  44. * updates as the encryption process progreses.
  45. */
  46. async encrypt(password, progressCallback) {
  47. const account = { address: this.address, privateKey: this.privateKey };
  48. return await (0, json_keystore_js_1.encryptKeystoreJson)(account, password, { progressCallback });
  49. }
  50. /**
  51. * Returns a [JSON Keystore Wallet](json-wallets) encryped with
  52. * %%password%%.
  53. *
  54. * It is preferred to use the [async version](encrypt) instead,
  55. * which allows a [[ProgressCallback]] to keep the user informed.
  56. *
  57. * This method will block the event loop (freezing all UI) until
  58. * it is complete, which may be a non-trivial duration.
  59. */
  60. encryptSync(password) {
  61. const account = { address: this.address, privateKey: this.privateKey };
  62. return (0, json_keystore_js_1.encryptKeystoreJsonSync)(account, password);
  63. }
  64. static #fromAccount(account) {
  65. (0, index_js_2.assertArgument)(account, "invalid JSON wallet", "json", "[ REDACTED ]");
  66. if ("mnemonic" in account && account.mnemonic && account.mnemonic.locale === "en") {
  67. const mnemonic = mnemonic_js_1.Mnemonic.fromEntropy(account.mnemonic.entropy);
  68. const wallet = hdwallet_js_1.HDNodeWallet.fromMnemonic(mnemonic, account.mnemonic.path);
  69. if (wallet.address === account.address && wallet.privateKey === account.privateKey) {
  70. return wallet;
  71. }
  72. console.log("WARNING: JSON mismatch address/privateKey != mnemonic; fallback onto private key");
  73. }
  74. const wallet = new Wallet(account.privateKey);
  75. (0, index_js_2.assertArgument)(wallet.address === account.address, "address/privateKey mismatch", "json", "[ REDACTED ]");
  76. return wallet;
  77. }
  78. /**
  79. * Creates (asynchronously) a **Wallet** by decrypting the %%json%%
  80. * with %%password%%.
  81. *
  82. * If %%progress%% is provided, it is called periodically during
  83. * decryption so that any UI can be updated.
  84. */
  85. static async fromEncryptedJson(json, password, progress) {
  86. let account = null;
  87. if ((0, json_keystore_js_1.isKeystoreJson)(json)) {
  88. account = await (0, json_keystore_js_1.decryptKeystoreJson)(json, password, progress);
  89. }
  90. else if ((0, json_crowdsale_js_1.isCrowdsaleJson)(json)) {
  91. if (progress) {
  92. progress(0);
  93. await stall(0);
  94. }
  95. account = (0, json_crowdsale_js_1.decryptCrowdsaleJson)(json, password);
  96. if (progress) {
  97. progress(1);
  98. await stall(0);
  99. }
  100. }
  101. return Wallet.#fromAccount(account);
  102. }
  103. /**
  104. * Creates a **Wallet** by decrypting the %%json%% with %%password%%.
  105. *
  106. * The [[fromEncryptedJson]] method is preferred, as this method
  107. * will lock up and freeze the UI during decryption, which may take
  108. * some time.
  109. */
  110. static fromEncryptedJsonSync(json, password) {
  111. let account = null;
  112. if ((0, json_keystore_js_1.isKeystoreJson)(json)) {
  113. account = (0, json_keystore_js_1.decryptKeystoreJsonSync)(json, password);
  114. }
  115. else if ((0, json_crowdsale_js_1.isCrowdsaleJson)(json)) {
  116. account = (0, json_crowdsale_js_1.decryptCrowdsaleJson)(json, password);
  117. }
  118. else {
  119. (0, index_js_2.assertArgument)(false, "invalid JSON wallet", "json", "[ REDACTED ]");
  120. }
  121. return Wallet.#fromAccount(account);
  122. }
  123. /**
  124. * Creates a new random [[HDNodeWallet]] using the available
  125. * [cryptographic random source](randomBytes).
  126. *
  127. * If there is no crytographic random source, this will throw.
  128. */
  129. static createRandom(provider) {
  130. const wallet = hdwallet_js_1.HDNodeWallet.createRandom();
  131. if (provider) {
  132. return wallet.connect(provider);
  133. }
  134. return wallet;
  135. }
  136. /**
  137. * Creates a [[HDNodeWallet]] for %%phrase%%.
  138. */
  139. static fromPhrase(phrase, provider) {
  140. const wallet = hdwallet_js_1.HDNodeWallet.fromPhrase(phrase);
  141. if (provider) {
  142. return wallet.connect(provider);
  143. }
  144. return wallet;
  145. }
  146. }
  147. exports.Wallet = Wallet;
  148. //# sourceMappingURL=wallet.js.map