wallet.js 5.5 KB

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