base-wallet.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { getAddress, resolveAddress } from "../address/index.js";
  2. import { hashMessage, TypedDataEncoder } from "../hash/index.js";
  3. import { AbstractSigner, copyRequest } from "../providers/index.js";
  4. import { computeAddress, Transaction } from "../transaction/index.js";
  5. import { defineProperties, resolveProperties, assert, assertArgument } from "../utils/index.js";
  6. /**
  7. * The **BaseWallet** is a stream-lined implementation of a
  8. * [[Signer]] that operates with a private key.
  9. *
  10. * It is preferred to use the [[Wallet]] class, as it offers
  11. * additional functionality and simplifies loading a variety
  12. * of JSON formats, Mnemonic Phrases, etc.
  13. *
  14. * This class may be of use for those attempting to implement
  15. * a minimal Signer.
  16. */
  17. export class BaseWallet extends AbstractSigner {
  18. /**
  19. * The wallet address.
  20. */
  21. address;
  22. #signingKey;
  23. /**
  24. * Creates a new BaseWallet for %%privateKey%%, optionally
  25. * connected to %%provider%%.
  26. *
  27. * If %%provider%% is not specified, only offline methods can
  28. * be used.
  29. */
  30. constructor(privateKey, provider) {
  31. super(provider);
  32. assertArgument(privateKey && typeof (privateKey.sign) === "function", "invalid private key", "privateKey", "[ REDACTED ]");
  33. this.#signingKey = privateKey;
  34. const address = computeAddress(this.signingKey.publicKey);
  35. defineProperties(this, { address });
  36. }
  37. // Store private values behind getters to reduce visibility
  38. // in console.log
  39. /**
  40. * The [[SigningKey]] used for signing payloads.
  41. */
  42. get signingKey() { return this.#signingKey; }
  43. /**
  44. * The private key for this wallet.
  45. */
  46. get privateKey() { return this.signingKey.privateKey; }
  47. async getAddress() { return this.address; }
  48. connect(provider) {
  49. return new BaseWallet(this.#signingKey, provider);
  50. }
  51. async signTransaction(tx) {
  52. tx = copyRequest(tx);
  53. // Replace any Addressable or ENS name with an address
  54. const { to, from } = await resolveProperties({
  55. to: (tx.to ? resolveAddress(tx.to, this.provider) : undefined),
  56. from: (tx.from ? resolveAddress(tx.from, this.provider) : undefined)
  57. });
  58. if (to != null) {
  59. tx.to = to;
  60. }
  61. if (from != null) {
  62. tx.from = from;
  63. }
  64. if (tx.from != null) {
  65. assertArgument(getAddress((tx.from)) === this.address, "transaction from address mismatch", "tx.from", tx.from);
  66. delete tx.from;
  67. }
  68. // Build the transaction
  69. const btx = Transaction.from(tx);
  70. btx.signature = this.signingKey.sign(btx.unsignedHash);
  71. return btx.serialized;
  72. }
  73. async signMessage(message) {
  74. return this.signMessageSync(message);
  75. }
  76. // @TODO: Add a secialized signTx and signTyped sync that enforces
  77. // all parameters are known?
  78. /**
  79. * Returns the signature for %%message%% signed with this wallet.
  80. */
  81. signMessageSync(message) {
  82. return this.signingKey.sign(hashMessage(message)).serialized;
  83. }
  84. async signTypedData(domain, types, value) {
  85. // Populate any ENS names
  86. const populated = await TypedDataEncoder.resolveNames(domain, types, value, async (name) => {
  87. // @TODO: this should use resolveName; addresses don't
  88. // need a provider
  89. assert(this.provider != null, "cannot resolve ENS names without a provider", "UNSUPPORTED_OPERATION", {
  90. operation: "resolveName",
  91. info: { name }
  92. });
  93. const address = await this.provider.resolveName(name);
  94. assert(address != null, "unconfigured ENS name", "UNCONFIGURED_NAME", {
  95. value: name
  96. });
  97. return address;
  98. });
  99. return this.signingKey.sign(TypedDataEncoder.hash(populated.domain, types, populated.value)).serialized;
  100. }
  101. }
  102. //# sourceMappingURL=base-wallet.js.map