base-wallet.ts 4.5 KB

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