signer.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import type { Addressable, NameResolver } from "../address/index.js";
  2. import type { TypedDataDomain, TypedDataField } from "../hash/index.js";
  3. import type { TransactionLike } from "../transaction/index.js";
  4. import type { ContractRunner } from "./contracts.js";
  5. import type { BlockTag, Provider, TransactionRequest, TransactionResponse } from "./provider.js";
  6. /**
  7. * A Signer represents an account on the Ethereum Blockchain, and is most often
  8. * backed by a private key represented by a mnemonic or residing on a Hardware Wallet.
  9. *
  10. * The API remains abstract though, so that it can deal with more advanced exotic
  11. * Signing entities, such as Smart Contract Wallets or Virtual Wallets (where the
  12. * private key may not be known).
  13. */
  14. export interface Signer extends Addressable, ContractRunner, NameResolver {
  15. /**
  16. * The [[Provider]] attached to this Signer (if any).
  17. */
  18. provider: null | Provider;
  19. /**
  20. * Returns a new instance of this Signer connected to //provider// or detached
  21. * from any Provider if null.
  22. */
  23. connect(provider: null | Provider): Signer;
  24. ////////////////////
  25. // State
  26. /**
  27. * Get the address of the Signer.
  28. */
  29. getAddress(): Promise<string>;
  30. /**
  31. * Gets the next nonce required for this Signer to send a transaction.
  32. *
  33. * @param blockTag - The blocktag to base the transaction count on, keep in mind
  34. * many nodes do not honour this value and silently ignore it [default: ``"latest"``]
  35. */
  36. getNonce(blockTag?: BlockTag): Promise<number>;
  37. ////////////////////
  38. // Preparation
  39. /**
  40. * Prepares a {@link TransactionRequest} for calling:
  41. * - resolves ``to`` and ``from`` addresses
  42. * - if ``from`` is specified , check that it matches this Signer
  43. *
  44. * @param tx - The call to prepare
  45. */
  46. populateCall(tx: TransactionRequest): Promise<TransactionLike<string>>;
  47. /**
  48. * Prepares a {@link TransactionRequest} for sending to the network by
  49. * populating any missing properties:
  50. * - resolves ``to`` and ``from`` addresses
  51. * - if ``from`` is specified , check that it matches this Signer
  52. * - populates ``nonce`` via ``signer.getNonce("pending")``
  53. * - populates ``gasLimit`` via ``signer.estimateGas(tx)``
  54. * - populates ``chainId`` via ``signer.provider.getNetwork()``
  55. * - populates ``type`` and relevant fee data for that type (``gasPrice``
  56. * for legacy transactions, ``maxFeePerGas`` for EIP-1559, etc)
  57. *
  58. * @note Some Signer implementations may skip populating properties that
  59. * are populated downstream; for example JsonRpcSigner defers to the
  60. * node to populate the nonce and fee data.
  61. *
  62. * @param tx - The call to prepare
  63. */
  64. populateTransaction(tx: TransactionRequest): Promise<TransactionLike<string>>;
  65. ////////////////////
  66. // Execution
  67. /**
  68. * Estimates the required gas required to execute //tx// on the Blockchain. This
  69. * will be the expected amount a transaction will require as its ``gasLimit``
  70. * to successfully run all the necessary computations and store the needed state
  71. * that the transaction intends.
  72. *
  73. * Keep in mind that this is **best efforts**, since the state of the Blockchain
  74. * is in flux, which could affect transaction gas requirements.
  75. *
  76. * @throws UNPREDICTABLE_GAS_LIMIT A transaction that is believed by the node to likely
  77. * fail will throw an error during gas estimation. This could indicate that it
  78. * will actually fail or that the circumstances are simply too complex for the
  79. * node to take into account. In these cases, a manually determined ``gasLimit``
  80. * will need to be made.
  81. */
  82. estimateGas(tx: TransactionRequest): Promise<bigint>;
  83. /**
  84. * Evaluates the //tx// by running it against the current Blockchain state. This
  85. * cannot change state and has no cost in ether, as it is effectively simulating
  86. * execution.
  87. *
  88. * This can be used to have the Blockchain perform computations based on its state
  89. * (e.g. running a Contract's getters) or to simulate the effect of a transaction
  90. * before actually performing an operation.
  91. */
  92. call(tx: TransactionRequest): Promise<string>;
  93. /**
  94. * Resolves an ENS Name to an address.
  95. */
  96. resolveName(name: string): Promise<null | string>;
  97. ////////////////////
  98. // Signing
  99. /**
  100. * Signs %%tx%%, returning the fully signed transaction. This does not
  101. * populate any additional properties within the transaction.
  102. */
  103. signTransaction(tx: TransactionRequest): Promise<string>;
  104. /**
  105. * Sends %%tx%% to the Network. The ``signer.populateTransaction(tx)``
  106. * is called first to ensure all necessary properties for the
  107. * transaction to be valid have been popualted first.
  108. */
  109. sendTransaction(tx: TransactionRequest): Promise<TransactionResponse>;
  110. /**
  111. * Signs an [[link-eip-191]] prefixed personal message.
  112. *
  113. * If the %%message%% is a string, it is signed as UTF-8 encoded bytes. It is **not**
  114. * interpretted as a [[BytesLike]]; so the string ``"0x1234"`` is signed as six
  115. * characters, **not** two bytes.
  116. *
  117. * To sign that example as two bytes, the Uint8Array should be used
  118. * (i.e. ``new Uint8Array([ 0x12, 0x34 ])``).
  119. */
  120. signMessage(message: string | Uint8Array): Promise<string>;
  121. /**
  122. * Signs the [[link-eip-712]] typed data.
  123. */
  124. signTypedData(domain: TypedDataDomain, types: Record<string, Array<TypedDataField>>, value: Record<string, any>): Promise<string>;
  125. }