abstract-signer.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * Generally the [[Wallet]] and [[JsonRpcSigner]] and their sub-classes
  3. * are sufficent for most developers, but this is provided to
  4. * fascilitate more complex Signers.
  5. *
  6. * @_section: api/providers/abstract-signer: Subclassing Signer [abstract-signer]
  7. */
  8. import { resolveAddress } from "../address/index.js";
  9. import { Transaction } from "../transaction/index.js";
  10. import { defineProperties, getBigInt, resolveProperties, assert, assertArgument } from "../utils/index.js";
  11. import { copyRequest } from "./provider.js";
  12. function checkProvider(signer, operation) {
  13. if (signer.provider) {
  14. return signer.provider;
  15. }
  16. assert(false, "missing provider", "UNSUPPORTED_OPERATION", { operation });
  17. }
  18. async function populate(signer, tx) {
  19. let pop = copyRequest(tx);
  20. if (pop.to != null) {
  21. pop.to = resolveAddress(pop.to, signer);
  22. }
  23. if (pop.from != null) {
  24. const from = pop.from;
  25. pop.from = Promise.all([
  26. signer.getAddress(),
  27. resolveAddress(from, signer)
  28. ]).then(([address, from]) => {
  29. assertArgument(address.toLowerCase() === from.toLowerCase(), "transaction from mismatch", "tx.from", from);
  30. return address;
  31. });
  32. }
  33. else {
  34. pop.from = signer.getAddress();
  35. }
  36. return await resolveProperties(pop);
  37. }
  38. /**
  39. * An **AbstractSigner** includes most of teh functionality required
  40. * to get a [[Signer]] working as expected, but requires a few
  41. * Signer-specific methods be overridden.
  42. *
  43. */
  44. export class AbstractSigner {
  45. /**
  46. * The provider this signer is connected to.
  47. */
  48. provider;
  49. /**
  50. * Creates a new Signer connected to %%provider%%.
  51. */
  52. constructor(provider) {
  53. defineProperties(this, { provider: (provider || null) });
  54. }
  55. async getNonce(blockTag) {
  56. return checkProvider(this, "getTransactionCount").getTransactionCount(await this.getAddress(), blockTag);
  57. }
  58. async populateCall(tx) {
  59. const pop = await populate(this, tx);
  60. return pop;
  61. }
  62. async populateTransaction(tx) {
  63. const provider = checkProvider(this, "populateTransaction");
  64. const pop = await populate(this, tx);
  65. if (pop.nonce == null) {
  66. pop.nonce = await this.getNonce("pending");
  67. }
  68. if (pop.gasLimit == null) {
  69. pop.gasLimit = await this.estimateGas(pop);
  70. }
  71. // Populate the chain ID
  72. const network = await (this.provider).getNetwork();
  73. if (pop.chainId != null) {
  74. const chainId = getBigInt(pop.chainId);
  75. assertArgument(chainId === network.chainId, "transaction chainId mismatch", "tx.chainId", tx.chainId);
  76. }
  77. else {
  78. pop.chainId = network.chainId;
  79. }
  80. // Do not allow mixing pre-eip-1559 and eip-1559 properties
  81. const hasEip1559 = (pop.maxFeePerGas != null || pop.maxPriorityFeePerGas != null);
  82. if (pop.gasPrice != null && (pop.type === 2 || hasEip1559)) {
  83. assertArgument(false, "eip-1559 transaction do not support gasPrice", "tx", tx);
  84. }
  85. else if ((pop.type === 0 || pop.type === 1) && hasEip1559) {
  86. assertArgument(false, "pre-eip-1559 transaction do not support maxFeePerGas/maxPriorityFeePerGas", "tx", tx);
  87. }
  88. if ((pop.type === 2 || pop.type == null) && (pop.maxFeePerGas != null && pop.maxPriorityFeePerGas != null)) {
  89. // Fully-formed EIP-1559 transaction (skip getFeeData)
  90. pop.type = 2;
  91. }
  92. else if (pop.type === 0 || pop.type === 1) {
  93. // Explicit Legacy or EIP-2930 transaction
  94. // We need to get fee data to determine things
  95. const feeData = await provider.getFeeData();
  96. assert(feeData.gasPrice != null, "network does not support gasPrice", "UNSUPPORTED_OPERATION", {
  97. operation: "getGasPrice"
  98. });
  99. // Populate missing gasPrice
  100. if (pop.gasPrice == null) {
  101. pop.gasPrice = feeData.gasPrice;
  102. }
  103. }
  104. else {
  105. // We need to get fee data to determine things
  106. const feeData = await provider.getFeeData();
  107. if (pop.type == null) {
  108. // We need to auto-detect the intended type of this transaction...
  109. if (feeData.maxFeePerGas != null && feeData.maxPriorityFeePerGas != null) {
  110. // The network supports EIP-1559!
  111. // Upgrade transaction from null to eip-1559
  112. pop.type = 2;
  113. if (pop.gasPrice != null) {
  114. // Using legacy gasPrice property on an eip-1559 network,
  115. // so use gasPrice as both fee properties
  116. const gasPrice = pop.gasPrice;
  117. delete pop.gasPrice;
  118. pop.maxFeePerGas = gasPrice;
  119. pop.maxPriorityFeePerGas = gasPrice;
  120. }
  121. else {
  122. // Populate missing fee data
  123. if (pop.maxFeePerGas == null) {
  124. pop.maxFeePerGas = feeData.maxFeePerGas;
  125. }
  126. if (pop.maxPriorityFeePerGas == null) {
  127. pop.maxPriorityFeePerGas = feeData.maxPriorityFeePerGas;
  128. }
  129. }
  130. }
  131. else if (feeData.gasPrice != null) {
  132. // Network doesn't support EIP-1559...
  133. // ...but they are trying to use EIP-1559 properties
  134. assert(!hasEip1559, "network does not support EIP-1559", "UNSUPPORTED_OPERATION", {
  135. operation: "populateTransaction"
  136. });
  137. // Populate missing fee data
  138. if (pop.gasPrice == null) {
  139. pop.gasPrice = feeData.gasPrice;
  140. }
  141. // Explicitly set untyped transaction to legacy
  142. // @TODO: Maybe this shold allow type 1?
  143. pop.type = 0;
  144. }
  145. else {
  146. // getFeeData has failed us.
  147. assert(false, "failed to get consistent fee data", "UNSUPPORTED_OPERATION", {
  148. operation: "signer.getFeeData"
  149. });
  150. }
  151. }
  152. else if (pop.type === 2 || pop.type === 3) {
  153. // Explicitly using EIP-1559 or EIP-4844
  154. // Populate missing fee data
  155. if (pop.maxFeePerGas == null) {
  156. pop.maxFeePerGas = feeData.maxFeePerGas;
  157. }
  158. if (pop.maxPriorityFeePerGas == null) {
  159. pop.maxPriorityFeePerGas = feeData.maxPriorityFeePerGas;
  160. }
  161. }
  162. }
  163. //@TOOD: Don't await all over the place; save them up for
  164. // the end for better batching
  165. return await resolveProperties(pop);
  166. }
  167. async estimateGas(tx) {
  168. return checkProvider(this, "estimateGas").estimateGas(await this.populateCall(tx));
  169. }
  170. async call(tx) {
  171. return checkProvider(this, "call").call(await this.populateCall(tx));
  172. }
  173. async resolveName(name) {
  174. const provider = checkProvider(this, "resolveName");
  175. return await provider.resolveName(name);
  176. }
  177. async sendTransaction(tx) {
  178. const provider = checkProvider(this, "sendTransaction");
  179. const pop = await this.populateTransaction(tx);
  180. delete pop.from;
  181. const txObj = Transaction.from(pop);
  182. return await provider.broadcastTransaction(await this.signTransaction(txObj));
  183. }
  184. }
  185. /**
  186. * A **VoidSigner** is a class deisgned to allow an address to be used
  187. * in any API which accepts a Signer, but for which there are no
  188. * credentials available to perform any actual signing.
  189. *
  190. * This for example allow impersonating an account for the purpose of
  191. * static calls or estimating gas, but does not allow sending transactions.
  192. */
  193. export class VoidSigner extends AbstractSigner {
  194. /**
  195. * The signer address.
  196. */
  197. address;
  198. /**
  199. * Creates a new **VoidSigner** with %%address%% attached to
  200. * %%provider%%.
  201. */
  202. constructor(address, provider) {
  203. super(provider);
  204. defineProperties(this, { address });
  205. }
  206. async getAddress() { return this.address; }
  207. connect(provider) {
  208. return new VoidSigner(this.address, provider);
  209. }
  210. #throwUnsupported(suffix, operation) {
  211. assert(false, `VoidSigner cannot sign ${suffix}`, "UNSUPPORTED_OPERATION", { operation });
  212. }
  213. async signTransaction(tx) {
  214. this.#throwUnsupported("transactions", "signTransaction");
  215. }
  216. async signMessage(message) {
  217. this.#throwUnsupported("messages", "signMessage");
  218. }
  219. async signTypedData(domain, types, value) {
  220. this.#throwUnsupported("typed-data", "signTypedData");
  221. }
  222. }
  223. //# sourceMappingURL=abstract-signer.js.map