quantumcoin 6.14.2 → 6.14.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (317) hide show
  1. package/CHANGELOG.md +442 -442
  2. package/FUNDING.json +10 -10
  3. package/LICENSE.md +21 -21
  4. package/README.md +132 -142
  5. package/SECURITY.md +34 -34
  6. package/dist/README.md +22 -22
  7. package/dist/quantumcoin.js +1127 -1324
  8. package/dist/quantumcoin.js.map +1 -1
  9. package/dist/quantumcoin.min.js +1 -1
  10. package/dist/quantumcoin.umd.js +1128 -1327
  11. package/dist/quantumcoin.umd.js.map +1 -1
  12. package/dist/quantumcoin.umd.min.js +1 -1
  13. package/dist/wordlists-extra.js +1 -1
  14. package/dist/wordlists-extra.js.map +1 -1
  15. package/dist/wordlists-extra.min.js +1 -1
  16. package/lib.commonjs/README.md +16 -16
  17. package/lib.commonjs/_version.js +1 -1
  18. package/lib.commonjs/crypto/signature.d.ts +3 -76
  19. package/lib.commonjs/crypto/signature.d.ts.map +1 -1
  20. package/lib.commonjs/crypto/signature.js +15 -199
  21. package/lib.commonjs/crypto/signature.js.map +1 -1
  22. package/lib.commonjs/crypto/signing-key.d.ts +1 -1
  23. package/lib.commonjs/crypto/signing-key.d.ts.map +1 -1
  24. package/lib.commonjs/crypto/signing-key.js +19 -10
  25. package/lib.commonjs/crypto/signing-key.js.map +1 -1
  26. package/lib.commonjs/package.json +12 -12
  27. package/lib.commonjs/providers/provider-jsonrpc.d.ts +0 -1
  28. package/lib.commonjs/providers/provider-jsonrpc.d.ts.map +1 -1
  29. package/lib.commonjs/providers/provider-jsonrpc.js +0 -1
  30. package/lib.commonjs/providers/provider-jsonrpc.js.map +1 -1
  31. package/lib.commonjs/quantumcoin.d.ts +2 -0
  32. package/lib.commonjs/quantumcoin.d.ts.map +1 -1
  33. package/lib.commonjs/quantumcoin.js +11 -5
  34. package/lib.commonjs/quantumcoin.js.map +1 -1
  35. package/lib.commonjs/transaction/address.d.ts.map +1 -1
  36. package/lib.commonjs/transaction/address.js +8 -3
  37. package/lib.commonjs/transaction/address.js.map +1 -1
  38. package/lib.commonjs/transaction/transaction.d.ts.map +1 -1
  39. package/lib.commonjs/transaction/transaction.js +7 -40
  40. package/lib.commonjs/transaction/transaction.js.map +1 -1
  41. package/lib.commonjs/wallet/json-keystore.d.ts.map +1 -1
  42. package/lib.commonjs/wallet/json-keystore.js +7 -7
  43. package/lib.commonjs/wallet/json-keystore.js.map +1 -1
  44. package/lib.commonjs/wallet/wallet.d.ts.map +1 -1
  45. package/lib.commonjs/wallet/wallet.js +2 -2
  46. package/lib.commonjs/wallet/wallet.js.map +1 -1
  47. package/lib.esm/README.md +16 -16
  48. package/lib.esm/_version.js +1 -1
  49. package/lib.esm/crypto/signature.d.ts +3 -76
  50. package/lib.esm/crypto/signature.d.ts.map +1 -1
  51. package/lib.esm/crypto/signature.js +16 -202
  52. package/lib.esm/crypto/signature.js.map +1 -1
  53. package/lib.esm/crypto/signing-key.d.ts +1 -1
  54. package/lib.esm/crypto/signing-key.d.ts.map +1 -1
  55. package/lib.esm/crypto/signing-key.js +20 -9
  56. package/lib.esm/crypto/signing-key.js.map +1 -1
  57. package/lib.esm/package.json +12 -12
  58. package/lib.esm/providers/provider-jsonrpc.d.ts +0 -1
  59. package/lib.esm/providers/provider-jsonrpc.d.ts.map +1 -1
  60. package/lib.esm/providers/provider-jsonrpc.js +0 -1
  61. package/lib.esm/providers/provider-jsonrpc.js.map +1 -1
  62. package/lib.esm/quantumcoin.d.ts +2 -0
  63. package/lib.esm/quantumcoin.d.ts.map +1 -1
  64. package/lib.esm/quantumcoin.js +6 -0
  65. package/lib.esm/quantumcoin.js.map +1 -1
  66. package/lib.esm/transaction/address.d.ts.map +1 -1
  67. package/lib.esm/transaction/address.js +8 -2
  68. package/lib.esm/transaction/address.js.map +1 -1
  69. package/lib.esm/transaction/transaction.d.ts.map +1 -1
  70. package/lib.esm/transaction/transaction.js +7 -40
  71. package/lib.esm/transaction/transaction.js.map +1 -1
  72. package/lib.esm/wallet/json-keystore.d.ts.map +1 -1
  73. package/lib.esm/wallet/json-keystore.js +11 -5
  74. package/lib.esm/wallet/json-keystore.js.map +1 -1
  75. package/lib.esm/wallet/wallet.d.ts.map +1 -1
  76. package/lib.esm/wallet/wallet.js +3 -1
  77. package/lib.esm/wallet/wallet.js.map +1 -1
  78. package/package.json +6 -5
  79. package/rollup.config.mjs +50 -50
  80. package/src.ts/_version.ts +1 -1
  81. package/src.ts/abi/abi-coder.ts +237 -237
  82. package/src.ts/abi/bytes32.ts +45 -45
  83. package/src.ts/abi/coders/abstract-coder.ts +541 -541
  84. package/src.ts/abi/coders/address.ts +36 -36
  85. package/src.ts/abi/coders/anonymous.ts +29 -29
  86. package/src.ts/abi/coders/array.ts +199 -199
  87. package/src.ts/abi/coders/boolean.ts +27 -27
  88. package/src.ts/abi/coders/bytes.ts +43 -43
  89. package/src.ts/abi/coders/fixed-bytes.ts +37 -37
  90. package/src.ts/abi/coders/null.ts +28 -28
  91. package/src.ts/abi/coders/number.ts +63 -63
  92. package/src.ts/abi/coders/string.ts +29 -29
  93. package/src.ts/abi/coders/tuple.ts +69 -69
  94. package/src.ts/abi/fragments.ts +1617 -1617
  95. package/src.ts/abi/index.ts +41 -41
  96. package/src.ts/abi/interface.ts +1271 -1271
  97. package/src.ts/abi/typed.ts +796 -796
  98. package/src.ts/address/address.ts +148 -148
  99. package/src.ts/address/checks.ts +123 -123
  100. package/src.ts/address/contract-address.ts +80 -80
  101. package/src.ts/address/index.ts +57 -57
  102. package/src.ts/constants/addresses.ts +8 -8
  103. package/src.ts/constants/hashes.ts +7 -7
  104. package/src.ts/constants/index.ts +16 -16
  105. package/src.ts/constants/numbers.ts +35 -35
  106. package/src.ts/constants/strings.ts +16 -16
  107. package/src.ts/contract/contract.ts +1120 -1120
  108. package/src.ts/contract/factory.ts +143 -143
  109. package/src.ts/contract/index.ts +31 -31
  110. package/src.ts/contract/types.ts +236 -236
  111. package/src.ts/contract/wrappers.ts +225 -225
  112. package/src.ts/crypto/crypto-browser.ts +64 -64
  113. package/src.ts/crypto/crypto.ts +4 -4
  114. package/src.ts/crypto/hmac.ts +51 -51
  115. package/src.ts/crypto/index.ts +59 -59
  116. package/src.ts/crypto/keccak.ts +54 -54
  117. package/src.ts/crypto/pbkdf2.ts +55 -55
  118. package/src.ts/crypto/random.ts +36 -36
  119. package/src.ts/crypto/ripemd160.ts +43 -43
  120. package/src.ts/crypto/scrypt.ts +114 -114
  121. package/src.ts/crypto/sha2.ts +78 -78
  122. package/src.ts/crypto/signature.ts +145 -349
  123. package/src.ts/crypto/signing-key.ts +126 -118
  124. package/src.ts/hash/authorization.ts +38 -38
  125. package/src.ts/hash/id.ts +17 -17
  126. package/src.ts/hash/index.ts +18 -18
  127. package/src.ts/hash/message.ts +51 -51
  128. package/src.ts/hash/namehash.ts +101 -101
  129. package/src.ts/hash/solidity.ts +117 -117
  130. package/src.ts/hash/typed-data.ts +658 -658
  131. package/src.ts/index.ts +12 -12
  132. package/src.ts/providers/abstract-provider.ts +1761 -1761
  133. package/src.ts/providers/abstract-signer.ts +314 -314
  134. package/src.ts/providers/community.ts +49 -49
  135. package/src.ts/providers/contracts.ts +42 -42
  136. package/src.ts/providers/default-provider.ts +96 -96
  137. package/src.ts/providers/ens-resolver.ts +606 -606
  138. package/src.ts/providers/format.ts +320 -320
  139. package/src.ts/providers/formatting.ts +418 -418
  140. package/src.ts/providers/index.ts +125 -125
  141. package/src.ts/providers/network.ts +327 -327
  142. package/src.ts/providers/pagination.ts +8 -8
  143. package/src.ts/providers/plugin-fallback.ts +35 -35
  144. package/src.ts/providers/plugins-network.ts +281 -281
  145. package/src.ts/providers/provider-browser.ts +334 -334
  146. package/src.ts/providers/provider-fallback.ts +801 -801
  147. package/src.ts/providers/provider-ipcsocket-browser.ts +3 -3
  148. package/src.ts/providers/provider-ipcsocket.ts +81 -81
  149. package/src.ts/providers/provider-jsonrpc.ts +1334 -1335
  150. package/src.ts/providers/provider-socket.ts +352 -352
  151. package/src.ts/providers/provider-websocket.ts +103 -103
  152. package/src.ts/providers/provider.ts +2136 -2136
  153. package/src.ts/providers/signer-noncemanager.ts +98 -98
  154. package/src.ts/providers/signer.ts +166 -166
  155. package/src.ts/providers/subscriber-connection.ts +74 -74
  156. package/src.ts/providers/subscriber-filterid.ts +199 -199
  157. package/src.ts/providers/subscriber-polling.ts +321 -321
  158. package/src.ts/providers/ws-browser.ts +11 -11
  159. package/src.ts/providers/ws.ts +3 -3
  160. package/src.ts/quantumcoin.ts +219 -211
  161. package/src.ts/thirdparty.d.ts +16 -16
  162. package/src.ts/transaction/accesslist.ts +43 -43
  163. package/src.ts/transaction/address.ts +35 -31
  164. package/src.ts/transaction/authorization.ts +14 -14
  165. package/src.ts/transaction/index.ts +51 -51
  166. package/src.ts/transaction/transaction.ts +1349 -1379
  167. package/src.ts/utils/base58.ts +73 -73
  168. package/src.ts/utils/base64-browser.ts +25 -25
  169. package/src.ts/utils/base64.ts +56 -56
  170. package/src.ts/utils/data.ts +199 -199
  171. package/src.ts/utils/errors.ts +793 -793
  172. package/src.ts/utils/events.ts +105 -105
  173. package/src.ts/utils/fetch.ts +970 -970
  174. package/src.ts/utils/fixednumber.ts +643 -643
  175. package/src.ts/utils/geturl-browser.ts +81 -81
  176. package/src.ts/utils/geturl.ts +134 -134
  177. package/src.ts/utils/index.ts +95 -95
  178. package/src.ts/utils/maths.ts +240 -240
  179. package/src.ts/utils/properties.ts +60 -60
  180. package/src.ts/utils/rlp-decode.ts +104 -104
  181. package/src.ts/utils/rlp-encode.ts +64 -64
  182. package/src.ts/utils/rlp.ts +20 -20
  183. package/src.ts/utils/units.ts +91 -91
  184. package/src.ts/utils/utf8.ts +325 -325
  185. package/src.ts/utils/uuid.ts +36 -36
  186. package/src.ts/wallet/base-wallet.ts +160 -160
  187. package/src.ts/wallet/index.ts +32 -32
  188. package/src.ts/wallet/json-keystore.ts +108 -106
  189. package/src.ts/wallet/utils.ts +147 -147
  190. package/src.ts/wallet/wallet.ts +138 -139
  191. package/src.ts/wordlists/bit-reader.ts +35 -35
  192. package/src.ts/wordlists/decode-owl.ts +58 -58
  193. package/src.ts/wordlists/decode-owla.ts +33 -33
  194. package/src.ts/wordlists/generation/encode-latin.ts +370 -370
  195. package/src.ts/wordlists/index.ts +26 -26
  196. package/src.ts/wordlists/lang-cz.ts +33 -33
  197. package/src.ts/wordlists/lang-en.ts +33 -33
  198. package/src.ts/wordlists/lang-es.ts +35 -35
  199. package/src.ts/wordlists/lang-fr.ts +34 -34
  200. package/src.ts/wordlists/lang-it.ts +33 -33
  201. package/src.ts/wordlists/lang-ja.ts +181 -181
  202. package/src.ts/wordlists/lang-ko.ts +104 -104
  203. package/src.ts/wordlists/lang-pt.ts +34 -34
  204. package/src.ts/wordlists/lang-zh.ts +112 -112
  205. package/src.ts/wordlists/wordlist-owl.ts +77 -77
  206. package/src.ts/wordlists/wordlist-owla.ts +41 -41
  207. package/src.ts/wordlists/wordlist.ts +59 -59
  208. package/src.ts/wordlists/wordlists-browser.ts +8 -8
  209. package/src.ts/wordlists/wordlists-extra.ts +9 -9
  210. package/src.ts/wordlists/wordlists.ts +38 -38
  211. package/dist/quantumcoin.min.js'.gz' +0 -0
  212. package/dist/quantumcoin.umd.min.js'.gz' +0 -0
  213. package/dist/wordlists-extra.min.js'.gz' +0 -0
  214. package/lib.commonjs/providers/provider-alchemy.d.ts +0 -50
  215. package/lib.commonjs/providers/provider-alchemy.d.ts.map +0 -1
  216. package/lib.commonjs/providers/provider-alchemy.js +0 -151
  217. package/lib.commonjs/providers/provider-alchemy.js.map +0 -1
  218. package/lib.commonjs/providers/provider-ankr.d.ts +0 -61
  219. package/lib.commonjs/providers/provider-ankr.d.ts.map +0 -1
  220. package/lib.commonjs/providers/provider-ankr.js +0 -137
  221. package/lib.commonjs/providers/provider-ankr.js.map +0 -1
  222. package/lib.commonjs/providers/provider-blockscout.d.ts +0 -59
  223. package/lib.commonjs/providers/provider-blockscout.d.ts.map +0 -1
  224. package/lib.commonjs/providers/provider-blockscout.js +0 -145
  225. package/lib.commonjs/providers/provider-blockscout.js.map +0 -1
  226. package/lib.commonjs/providers/provider-chainstack.d.ts +0 -46
  227. package/lib.commonjs/providers/provider-chainstack.d.ts.map +0 -1
  228. package/lib.commonjs/providers/provider-chainstack.js +0 -102
  229. package/lib.commonjs/providers/provider-chainstack.js.map +0 -1
  230. package/lib.commonjs/providers/provider-cloudflare.d.ts +0 -14
  231. package/lib.commonjs/providers/provider-cloudflare.d.ts.map +0 -1
  232. package/lib.commonjs/providers/provider-cloudflare.js +0 -26
  233. package/lib.commonjs/providers/provider-cloudflare.js.map +0 -1
  234. package/lib.commonjs/providers/provider-etherscan.d.ts +0 -147
  235. package/lib.commonjs/providers/provider-etherscan.d.ts.map +0 -1
  236. package/lib.commonjs/providers/provider-etherscan.js +0 -587
  237. package/lib.commonjs/providers/provider-etherscan.js.map +0 -1
  238. package/lib.commonjs/providers/provider-infura.d.ts +0 -101
  239. package/lib.commonjs/providers/provider-infura.d.ts.map +0 -1
  240. package/lib.commonjs/providers/provider-infura.js +0 -206
  241. package/lib.commonjs/providers/provider-infura.js.map +0 -1
  242. package/lib.commonjs/providers/provider-pocket.d.ts +0 -54
  243. package/lib.commonjs/providers/provider-pocket.d.ts.map +0 -1
  244. package/lib.commonjs/providers/provider-pocket.js +0 -109
  245. package/lib.commonjs/providers/provider-pocket.js.map +0 -1
  246. package/lib.commonjs/providers/provider-quicknode.d.ts +0 -59
  247. package/lib.commonjs/providers/provider-quicknode.d.ts.map +0 -1
  248. package/lib.commonjs/providers/provider-quicknode.js +0 -163
  249. package/lib.commonjs/providers/provider-quicknode.js.map +0 -1
  250. package/lib.commonjs/wallet/hdwallet.d.ts +0 -248
  251. package/lib.commonjs/wallet/hdwallet.d.ts.map +0 -1
  252. package/lib.commonjs/wallet/hdwallet.js +0 -505
  253. package/lib.commonjs/wallet/hdwallet.js.map +0 -1
  254. package/lib.commonjs/wallet/json-crowdsale.d.ts +0 -27
  255. package/lib.commonjs/wallet/json-crowdsale.d.ts.map +0 -1
  256. package/lib.commonjs/wallet/json-crowdsale.js +0 -60
  257. package/lib.commonjs/wallet/json-crowdsale.js.map +0 -1
  258. package/lib.commonjs/wallet/mnemonic.d.ts +0 -65
  259. package/lib.commonjs/wallet/mnemonic.d.ts.map +0 -1
  260. package/lib.commonjs/wallet/mnemonic.js +0 -169
  261. package/lib.commonjs/wallet/mnemonic.js.map +0 -1
  262. package/lib.commonjs/wallet/seedwallet.d.ts +0 -4
  263. package/lib.commonjs/wallet/seedwallet.d.ts.map +0 -1
  264. package/lib.commonjs/wallet/seedwallet.js +0 -8
  265. package/lib.commonjs/wallet/seedwallet.js.map +0 -1
  266. package/lib.esm/providers/provider-alchemy.d.ts +0 -50
  267. package/lib.esm/providers/provider-alchemy.d.ts.map +0 -1
  268. package/lib.esm/providers/provider-alchemy.js +0 -147
  269. package/lib.esm/providers/provider-alchemy.js.map +0 -1
  270. package/lib.esm/providers/provider-ankr.d.ts +0 -61
  271. package/lib.esm/providers/provider-ankr.d.ts.map +0 -1
  272. package/lib.esm/providers/provider-ankr.js +0 -133
  273. package/lib.esm/providers/provider-ankr.js.map +0 -1
  274. package/lib.esm/providers/provider-blockscout.d.ts +0 -59
  275. package/lib.esm/providers/provider-blockscout.d.ts.map +0 -1
  276. package/lib.esm/providers/provider-blockscout.js +0 -141
  277. package/lib.esm/providers/provider-blockscout.js.map +0 -1
  278. package/lib.esm/providers/provider-chainstack.d.ts +0 -46
  279. package/lib.esm/providers/provider-chainstack.d.ts.map +0 -1
  280. package/lib.esm/providers/provider-chainstack.js +0 -98
  281. package/lib.esm/providers/provider-chainstack.js.map +0 -1
  282. package/lib.esm/providers/provider-cloudflare.d.ts +0 -14
  283. package/lib.esm/providers/provider-cloudflare.d.ts.map +0 -1
  284. package/lib.esm/providers/provider-cloudflare.js +0 -22
  285. package/lib.esm/providers/provider-cloudflare.js.map +0 -1
  286. package/lib.esm/providers/provider-etherscan.d.ts +0 -147
  287. package/lib.esm/providers/provider-etherscan.d.ts.map +0 -1
  288. package/lib.esm/providers/provider-etherscan.js +0 -584
  289. package/lib.esm/providers/provider-etherscan.js.map +0 -1
  290. package/lib.esm/providers/provider-infura.d.ts +0 -101
  291. package/lib.esm/providers/provider-infura.d.ts.map +0 -1
  292. package/lib.esm/providers/provider-infura.js +0 -201
  293. package/lib.esm/providers/provider-infura.js.map +0 -1
  294. package/lib.esm/providers/provider-pocket.d.ts +0 -54
  295. package/lib.esm/providers/provider-pocket.d.ts.map +0 -1
  296. package/lib.esm/providers/provider-pocket.js +0 -105
  297. package/lib.esm/providers/provider-pocket.js.map +0 -1
  298. package/lib.esm/providers/provider-quicknode.d.ts +0 -59
  299. package/lib.esm/providers/provider-quicknode.d.ts.map +0 -1
  300. package/lib.esm/providers/provider-quicknode.js +0 -159
  301. package/lib.esm/providers/provider-quicknode.js.map +0 -1
  302. package/lib.esm/wallet/hdwallet.d.ts +0 -248
  303. package/lib.esm/wallet/hdwallet.d.ts.map +0 -1
  304. package/lib.esm/wallet/hdwallet.js +0 -498
  305. package/lib.esm/wallet/hdwallet.js.map +0 -1
  306. package/lib.esm/wallet/json-crowdsale.d.ts +0 -27
  307. package/lib.esm/wallet/json-crowdsale.d.ts.map +0 -1
  308. package/lib.esm/wallet/json-crowdsale.js +0 -55
  309. package/lib.esm/wallet/json-crowdsale.js.map +0 -1
  310. package/lib.esm/wallet/mnemonic.d.ts +0 -65
  311. package/lib.esm/wallet/mnemonic.d.ts.map +0 -1
  312. package/lib.esm/wallet/mnemonic.js +0 -165
  313. package/lib.esm/wallet/mnemonic.js.map +0 -1
  314. package/lib.esm/wallet/seedwallet.d.ts +0 -4
  315. package/lib.esm/wallet/seedwallet.d.ts.map +0 -1
  316. package/lib.esm/wallet/seedwallet.js +0 -4
  317. package/lib.esm/wallet/seedwallet.js.map +0 -1
@@ -1,1379 +1,1349 @@
1
-
2
- import { getAddress } from "../address/index.js";
3
- import { ZeroAddress } from "../constants/addresses.js";
4
- import {
5
- keccak256, sha256, Signature, SigningKey
6
- } from "../crypto/index.js";
7
- import {
8
- concat, decodeRlp, encodeRlp, getBytes, getBigInt, getNumber, hexlify,
9
- assert, assertArgument, isBytesLike, isHexString, toBeArray, zeroPadValue
10
- } from "../utils/index.js";
11
-
12
- import { accessListify } from "./accesslist.js";
13
- import { authorizationify } from "./authorization.js";
14
- import { recoverAddress } from "./address.js";
15
-
16
- import type { BigNumberish, BytesLike } from "../utils/index.js";
17
- import type { SignatureLike } from "../crypto/index.js";
18
-
19
- import type {
20
- AccessList, AccessListish, Authorization, AuthorizationLike
21
- } from "./index.js";
22
-
23
-
24
- const BN_0 = BigInt(0);
25
- const BN_2 = BigInt(2);
26
- const BN_27 = BigInt(27)
27
- const BN_28 = BigInt(28)
28
- const BN_35 = BigInt(35);
29
- const BN_MAX_UINT = BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
30
-
31
- const BLOB_SIZE = 4096 * 32;
32
-
33
- // The BLS Modulo; each field within a BLOb must be less than this
34
- //const BLOB_BLS_MODULO = BigInt("0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001");
35
-
36
- /**
37
- * A **TransactionLike** is an object which is appropriate as a loose
38
- * input for many operations which will populate missing properties of
39
- * a transaction.
40
- */
41
- export interface TransactionLike<A = string> {
42
- /**
43
- * The type.
44
- */
45
- type?: null | number;
46
-
47
- /**
48
- * The recipient address or ``null`` for an ``init`` transaction.
49
- */
50
- to?: null | A;
51
-
52
- /**
53
- * The sender.
54
- */
55
- from?: null | A;
56
-
57
- /**
58
- * The nonce.
59
- */
60
- nonce?: null | number;
61
-
62
- /**
63
- * The maximum amount of gas that can be used.
64
- */
65
- gasLimit?: null | BigNumberish;
66
-
67
- /**
68
- * The gas price for legacy and berlin transactions.
69
- */
70
- gasPrice?: null | BigNumberish;
71
-
72
- /**
73
- * The maximum priority fee per gas for london transactions.
74
- */
75
- maxPriorityFeePerGas?: null | BigNumberish;
76
-
77
- /**
78
- * The maximum total fee per gas for london transactions.
79
- */
80
- maxFeePerGas?: null | BigNumberish;
81
-
82
- /**
83
- * The data.
84
- */
85
- data?: null | string;
86
-
87
- /**
88
- * The value (in wei) to send.
89
- */
90
- value?: null | BigNumberish;
91
-
92
- /**
93
- * The chain ID the transaction is valid on.
94
- */
95
- chainId?: null | BigNumberish;
96
-
97
- /**
98
- * The transaction hash.
99
- */
100
- hash?: null | string;
101
-
102
- /**
103
- * The signature provided by the sender.
104
- */
105
- signature?: null | SignatureLike;
106
-
107
- /**
108
- * The access list for berlin and london transactions.
109
- */
110
- accessList?: null | AccessListish;
111
-
112
- /**
113
- * The maximum fee per blob gas (see [[link-eip-4844]]).
114
- */
115
- maxFeePerBlobGas?: null | BigNumberish;
116
-
117
- /**
118
- * The versioned hashes (see [[link-eip-4844]]).
119
- */
120
- blobVersionedHashes?: null | Array<string>;
121
-
122
- /**
123
- * The blobs (if any) attached to this transaction (see [[link-eip-4844]]).
124
- */
125
- blobs?: null | Array<BlobLike>
126
-
127
- /**
128
- * An external library for computing the KZG commitments and
129
- * proofs necessary for EIP-4844 transactions (see [[link-eip-4844]]).
130
- *
131
- * This is generally ``null``, unless you are creating BLOb
132
- * transactions.
133
- */
134
- kzg?: null | KzgLibraryLike;
135
-
136
- /**
137
- * The [[link-eip-7702]] authorizations (if any).
138
- */
139
- authorizationList?: null | Array<Authorization>;
140
- }
141
-
142
- /**
143
- * A full-valid BLOb object for [[link-eip-4844]] transactions.
144
- *
145
- * The commitment and proof should have been computed using a
146
- * KZG library.
147
- */
148
- export interface Blob {
149
- data: string;
150
- proof: string;
151
- commitment: string;
152
- }
153
-
154
- /**
155
- * A BLOb object that can be passed for [[link-eip-4844]]
156
- * transactions.
157
- *
158
- * It may have had its commitment and proof already provided
159
- * or rely on an attached [[KzgLibrary]] to compute them.
160
- */
161
- export type BlobLike = BytesLike | {
162
- data: BytesLike;
163
- proof: BytesLike;
164
- commitment: BytesLike;
165
- };
166
-
167
- /**
168
- * A KZG Library with the necessary functions to compute
169
- * BLOb commitments and proofs.
170
- */
171
- export interface KzgLibrary {
172
- blobToKzgCommitment: (blob: Uint8Array) => Uint8Array;
173
- computeBlobKzgProof: (blob: Uint8Array, commitment: Uint8Array) => Uint8Array;
174
- }
175
-
176
- /**
177
- * A KZG Library with any of the various API configurations.
178
- * As the library is still experimental and the API is not
179
- * stable, depending on the version used the method names and
180
- * signatures are still in flux.
181
- *
182
- * This allows any of the versions to be passed into Transaction
183
- * while providing a stable external API.
184
- */
185
- export type KzgLibraryLike = KzgLibrary | {
186
- // kzg-wasm >= 0.5.0
187
- blobToKZGCommitment: (blob: string) => string;
188
- computeBlobKZGProof: (blob: string, commitment: string) => string;
189
- } | {
190
- // micro-ecc-signer
191
- blobToKzgCommitment: (blob: string) => string | Uint8Array;
192
- computeBlobProof: (blob: string, commitment: string) => string | Uint8Array;
193
- };
194
-
195
- function getKzgLibrary(kzg: KzgLibraryLike): KzgLibrary {
196
-
197
- const blobToKzgCommitment = (blob: Uint8Array) => {
198
-
199
- if ("computeBlobProof" in kzg) {
200
- // micro-ecc-signer; check for computeBlobProof since this API
201
- // expects a string while the kzg-wasm below expects a Unit8Array
202
-
203
- if ("blobToKzgCommitment" in kzg && typeof(kzg.blobToKzgCommitment) === "function") {
204
- return getBytes(kzg.blobToKzgCommitment(hexlify(blob)))
205
- }
206
-
207
- } else if ("blobToKzgCommitment" in kzg && typeof(kzg.blobToKzgCommitment) === "function") {
208
- // kzg-wasm <0.5.0; blobToKzgCommitment(Uint8Array) => Uint8Array
209
-
210
- return getBytes(kzg.blobToKzgCommitment(blob));
211
- }
212
-
213
- // kzg-wasm >= 0.5.0; blobToKZGCommitment(string) => string
214
- if ("blobToKZGCommitment" in kzg && typeof(kzg.blobToKZGCommitment) === "function") {
215
- return getBytes(kzg.blobToKZGCommitment(hexlify(blob)));
216
- }
217
-
218
- assertArgument(false, "unsupported KZG library", "kzg", kzg);
219
- };
220
-
221
- const computeBlobKzgProof = (blob: Uint8Array, commitment: Uint8Array) => {
222
-
223
- // micro-ecc-signer
224
- if ("computeBlobProof" in kzg && typeof(kzg.computeBlobProof) === "function") {
225
- return getBytes(kzg.computeBlobProof(hexlify(blob), hexlify(commitment)))
226
- }
227
-
228
- // kzg-wasm <0.5.0; computeBlobKzgProof(Uint8Array, Uint8Array) => Uint8Array
229
- if ("computeBlobKzgProof" in kzg && typeof(kzg.computeBlobKzgProof) === "function") {
230
- return kzg.computeBlobKzgProof(blob, commitment);
231
- }
232
-
233
- // kzg-wasm >= 0.5.0; computeBlobKZGProof(string, string) => string
234
- if ("computeBlobKZGProof" in kzg && typeof(kzg.computeBlobKZGProof) === "function") {
235
- return getBytes(kzg.computeBlobKZGProof(hexlify(blob), hexlify(commitment)));
236
- }
237
-
238
- assertArgument(false, "unsupported KZG library", "kzg", kzg);
239
- };
240
-
241
- return { blobToKzgCommitment, computeBlobKzgProof };
242
- }
243
-
244
- function getVersionedHash(version: number, hash: BytesLike): string {
245
- let versioned = version.toString(16);
246
- while (versioned.length < 2) { versioned = "0" + versioned; }
247
- versioned += sha256(hash).substring(4);
248
- return "0x" + versioned;
249
- }
250
-
251
- function handleAddress(value: string): null | string {
252
- if (value === "0x") { return null; }
253
- return getAddress(value);
254
- }
255
-
256
- function handleAccessList(value: any, param: string): AccessList {
257
- try {
258
- return accessListify(value);
259
- } catch (error: any) {
260
- assertArgument(false, error.message, param, value);
261
- }
262
- }
263
-
264
- function handleAuthorizationList(value: any, param: string): Array<Authorization> {
265
- try {
266
- if (!Array.isArray(value)) { throw new Error("authorizationList: invalid array"); }
267
- const result: Array<Authorization> = [ ];
268
- for (let i = 0; i < value.length; i++) {
269
- const auth: Array<string> = value[i];
270
- if (!Array.isArray(auth)) { throw new Error(`authorization[${ i }]: invalid array`); }
271
- if (auth.length !== 6) { throw new Error(`authorization[${ i }]: wrong length`); }
272
- if (!auth[1]) { throw new Error(`authorization[${ i }]: null address`); }
273
- result.push({
274
- address: <string>handleAddress(auth[1]),
275
- nonce: handleUint(auth[2], "nonce"),
276
- chainId: handleUint(auth[0], "chainId"),
277
- signature: Signature.from({
278
- yParity: <0 | 1>handleNumber(auth[3], "yParity"),
279
- r: zeroPadValue(auth[4], 32),
280
- s: zeroPadValue(auth[5], 32)
281
- })
282
- });
283
- }
284
- return result;
285
- } catch (error: any) {
286
- assertArgument(false, error.message, param, value);
287
- }
288
- }
289
-
290
- function handleNumber(_value: string, param: string): number {
291
- if (_value === "0x") { return 0; }
292
- return getNumber(_value, param);
293
- }
294
-
295
- function handleUint(_value: string, param: string): bigint {
296
- if (_value === "0x") { return BN_0; }
297
- const value = getBigInt(_value, param);
298
- assertArgument(value <= BN_MAX_UINT, "value exceeds uint size", param, value);
299
- return value;
300
- }
301
-
302
- function formatNumber(_value: BigNumberish, name: string): Uint8Array {
303
- const value = getBigInt(_value, "value");
304
- const result = toBeArray(value);
305
- assertArgument(result.length <= 32, `value too large`, `tx.${ name }`, value);
306
- return result;
307
- }
308
-
309
- function formatAccessList(value: AccessListish): Array<[ string, Array<string> ]> {
310
- return accessListify(value).map((set) => [ set.address, set.storageKeys ]);
311
- }
312
-
313
- function formatAuthorizationList(value: Array<Authorization>): Array<Array<string | Uint8Array>> {
314
- return value.map((a) => {
315
- return [
316
- formatNumber(a.chainId, "chainId"),
317
- a.address,
318
- formatNumber(a.nonce, "nonce"),
319
- formatNumber(a.signature.yParity, "yParity"),
320
- a.signature.r,
321
- a.signature.s
322
- ];
323
- });
324
- }
325
-
326
- function formatHashes(value: Array<string>, param: string): Array<string> {
327
- assertArgument(Array.isArray(value), `invalid ${ param }`, "value", value);
328
- for (let i = 0; i < value.length; i++) {
329
- assertArgument(isHexString(value[i], 32), "invalid ${ param } hash", `value[${ i }]`, value[i]);
330
- }
331
- return value;
332
- }
333
-
334
- function _parseLegacy(data: Uint8Array): TransactionLike {
335
- const fields: any = decodeRlp(data);
336
-
337
- assertArgument(Array.isArray(fields) && (fields.length === 9 || fields.length === 6),
338
- "invalid field count for legacy transaction", "data", data);
339
-
340
- const tx: TransactionLike = {
341
- type: 0,
342
- nonce: handleNumber(fields[0], "nonce"),
343
- gasPrice: handleUint(fields[1], "gasPrice"),
344
- gasLimit: handleUint(fields[2], "gasLimit"),
345
- to: handleAddress(fields[3]),
346
- value: handleUint(fields[4], "value"),
347
- data: hexlify(fields[5]),
348
- chainId: BN_0
349
- };
350
-
351
- // Legacy unsigned transaction
352
- if (fields.length === 6) { return tx; }
353
-
354
- const v = handleUint(fields[6], "v");
355
- const r = handleUint(fields[7], "r");
356
- const s = handleUint(fields[8], "s");
357
-
358
- if (r === BN_0 && s === BN_0) {
359
- // EIP-155 unsigned transaction
360
- tx.chainId = v;
361
-
362
- } else {
363
-
364
- // Compute the EIP-155 chain ID (or 0 for legacy)
365
- let chainId = (v - BN_35) / BN_2;
366
- if (chainId < BN_0) { chainId = BN_0; }
367
- tx.chainId = chainId
368
-
369
- // Signed Legacy Transaction
370
- assertArgument(chainId !== BN_0 || (v === BN_27 || v === BN_28), "non-canonical legacy v", "v", fields[6]);
371
-
372
- tx.signature = Signature.from({
373
- r: zeroPadValue(fields[7], 32),
374
- s: zeroPadValue(fields[8], 32),
375
- v
376
- });
377
-
378
- //tx.hash = keccak256(data);
379
- }
380
-
381
- return tx;
382
- }
383
-
384
- function _serializeLegacy(tx: Transaction, sig: null | Signature): string {
385
- const fields: Array<any> = [
386
- formatNumber(tx.nonce, "nonce"),
387
- formatNumber(tx.gasPrice || 0, "gasPrice"),
388
- formatNumber(tx.gasLimit, "gasLimit"),
389
- (tx.to || "0x"),
390
- formatNumber(tx.value, "value"),
391
- tx.data,
392
- ];
393
-
394
- let chainId = BN_0;
395
- if (tx.chainId != BN_0) {
396
- // A chainId was provided; if non-zero we'll use EIP-155
397
- chainId = getBigInt(tx.chainId, "tx.chainId");
398
-
399
- // We have a chainId in the tx and an EIP-155 v in the signature,
400
- // make sure they agree with each other
401
- assertArgument(!sig || sig.networkV == null || sig.legacyChainId === chainId,
402
- "tx.chainId/sig.v mismatch", "sig", sig);
403
-
404
- } else if (tx.signature) {
405
- // No explicit chainId, but EIP-155 have a derived implicit chainId
406
- const legacy = tx.signature.legacyChainId;
407
- if (legacy != null) { chainId = legacy; }
408
- }
409
-
410
- // Requesting an unsigned transaction
411
- if (!sig) {
412
- // We have an EIP-155 transaction (chainId was specified and non-zero)
413
- if (chainId !== BN_0) {
414
- fields.push(toBeArray(chainId));
415
- fields.push("0x");
416
- fields.push("0x");
417
- }
418
-
419
- return encodeRlp(fields);
420
- }
421
-
422
- // @TODO: We should probably check that tx.signature, chainId, and sig
423
- // match but that logic could break existing code, so schedule
424
- // this for the next major bump.
425
-
426
- // Compute the EIP-155 v
427
- let v = BigInt(27 + sig.yParity);
428
- if (chainId !== BN_0) {
429
- v = Signature.getChainIdV(chainId, sig.v);
430
- } else if (BigInt(sig.v) !== v) {
431
- assertArgument(false, "tx.chainId/sig.v mismatch", "sig", sig);
432
- }
433
-
434
- // Add the signature
435
- fields.push(toBeArray(v));
436
- fields.push(toBeArray(sig.r));
437
- fields.push(toBeArray(sig.s));
438
-
439
- return encodeRlp(fields);
440
- }
441
-
442
- function _parseEipSignature(tx: TransactionLike, fields: Array<string>): void {
443
- let yParity: number;
444
- try {
445
- yParity = handleNumber(fields[0], "yParity");
446
- if (yParity !== 0 && yParity !== 1) { throw new Error("bad yParity"); }
447
- } catch (error) {
448
- assertArgument(false, "invalid yParity", "yParity", fields[0]);
449
- }
450
-
451
- const r = zeroPadValue(fields[1], 32);
452
- const s = zeroPadValue(fields[2], 32);
453
-
454
- const signature = Signature.from({ r, s, yParity });
455
- tx.signature = signature;
456
- }
457
-
458
- function _parseEip1559(data: Uint8Array): TransactionLike {
459
- const fields: any = decodeRlp(getBytes(data).slice(1));
460
-
461
- assertArgument(Array.isArray(fields) && (fields.length === 9 || fields.length === 12),
462
- "invalid field count for transaction type: 2", "data", hexlify(data));
463
-
464
- const tx: TransactionLike = {
465
- type: 2,
466
- chainId: handleUint(fields[0], "chainId"),
467
- nonce: handleNumber(fields[1], "nonce"),
468
- maxPriorityFeePerGas: handleUint(fields[2], "maxPriorityFeePerGas"),
469
- maxFeePerGas: handleUint(fields[3], "maxFeePerGas"),
470
- gasPrice: null,
471
- gasLimit: handleUint(fields[4], "gasLimit"),
472
- to: handleAddress(fields[5]),
473
- value: handleUint(fields[6], "value"),
474
- data: hexlify(fields[7]),
475
- accessList: handleAccessList(fields[8], "accessList"),
476
- };
477
-
478
- // Unsigned EIP-1559 Transaction
479
- if (fields.length === 9) { return tx; }
480
-
481
- //tx.hash = keccak256(data);
482
-
483
- _parseEipSignature(tx, fields.slice(9));
484
-
485
- return tx;
486
- }
487
-
488
- function _serializeEip1559(tx: Transaction, sig: null | Signature): string {
489
- const fields: Array<any> = [
490
- formatNumber(tx.chainId, "chainId"),
491
- formatNumber(tx.nonce, "nonce"),
492
- formatNumber(tx.maxPriorityFeePerGas || 0, "maxPriorityFeePerGas"),
493
- formatNumber(tx.maxFeePerGas || 0, "maxFeePerGas"),
494
- formatNumber(tx.gasLimit, "gasLimit"),
495
- (tx.to || "0x"),
496
- formatNumber(tx.value, "value"),
497
- tx.data,
498
- formatAccessList(tx.accessList || [ ])
499
- ];
500
-
501
- if (sig) {
502
- fields.push(formatNumber(sig.yParity, "yParity"));
503
- fields.push(toBeArray(sig.r));
504
- fields.push(toBeArray(sig.s));
505
- }
506
-
507
- return concat([ "0x02", encodeRlp(fields)]);
508
- }
509
-
510
- function _parseEip2930(data: Uint8Array): TransactionLike {
511
- const fields: any = decodeRlp(getBytes(data).slice(1));
512
-
513
- assertArgument(Array.isArray(fields) && (fields.length === 8 || fields.length === 11),
514
- "invalid field count for transaction type: 1", "data", hexlify(data));
515
-
516
- const tx: TransactionLike = {
517
- type: 1,
518
- chainId: handleUint(fields[0], "chainId"),
519
- nonce: handleNumber(fields[1], "nonce"),
520
- gasPrice: handleUint(fields[2], "gasPrice"),
521
- gasLimit: handleUint(fields[3], "gasLimit"),
522
- to: handleAddress(fields[4]),
523
- value: handleUint(fields[5], "value"),
524
- data: hexlify(fields[6]),
525
- accessList: handleAccessList(fields[7], "accessList")
526
- };
527
-
528
- // Unsigned EIP-2930 Transaction
529
- if (fields.length === 8) { return tx; }
530
-
531
- //tx.hash = keccak256(data);
532
-
533
- _parseEipSignature(tx, fields.slice(8));
534
-
535
- return tx;
536
- }
537
-
538
- function _serializeEip2930(tx: Transaction, sig: null | Signature): string {
539
- const fields: any = [
540
- formatNumber(tx.chainId, "chainId"),
541
- formatNumber(tx.nonce, "nonce"),
542
- formatNumber(tx.gasPrice || 0, "gasPrice"),
543
- formatNumber(tx.gasLimit, "gasLimit"),
544
- (tx.to || "0x"),
545
- formatNumber(tx.value, "value"),
546
- tx.data,
547
- formatAccessList(tx.accessList || [ ])
548
- ];
549
-
550
- if (sig) {
551
- fields.push(formatNumber(sig.yParity, "recoveryParam"));
552
- fields.push(toBeArray(sig.r));
553
- fields.push(toBeArray(sig.s));
554
- }
555
-
556
- return concat([ "0x01", encodeRlp(fields)]);
557
- }
558
-
559
- function _parseEip4844(data: Uint8Array): TransactionLike {
560
- let fields: any = decodeRlp(getBytes(data).slice(1));
561
-
562
- let typeName = "3";
563
-
564
- let blobs: null | Array<Blob> = null;
565
-
566
- // Parse the network format
567
- if (fields.length === 4 && Array.isArray(fields[0])) {
568
- typeName = "3 (network format)";
569
- const fBlobs = fields[1], fCommits = fields[2], fProofs = fields[3];
570
- assertArgument(Array.isArray(fBlobs), "invalid network format: blobs not an array", "fields[1]", fBlobs);
571
- assertArgument(Array.isArray(fCommits), "invalid network format: commitments not an array", "fields[2]", fCommits);
572
- assertArgument(Array.isArray(fProofs), "invalid network format: proofs not an array", "fields[3]", fProofs);
573
- assertArgument(fBlobs.length === fCommits.length, "invalid network format: blobs/commitments length mismatch", "fields", fields);
574
- assertArgument(fBlobs.length === fProofs.length, "invalid network format: blobs/proofs length mismatch", "fields", fields);
575
-
576
- blobs = [ ];
577
- for (let i = 0; i < fields[1].length; i++) {
578
- blobs.push({
579
- data: fBlobs[i],
580
- commitment: fCommits[i],
581
- proof: fProofs[i],
582
- });
583
- }
584
-
585
- fields = fields[0];
586
- }
587
-
588
- assertArgument(Array.isArray(fields) && (fields.length === 11 || fields.length === 14),
589
- `invalid field count for transaction type: ${ typeName }`, "data", hexlify(data));
590
-
591
- const tx: TransactionLike = {
592
- type: 3,
593
- chainId: handleUint(fields[0], "chainId"),
594
- nonce: handleNumber(fields[1], "nonce"),
595
- maxPriorityFeePerGas: handleUint(fields[2], "maxPriorityFeePerGas"),
596
- maxFeePerGas: handleUint(fields[3], "maxFeePerGas"),
597
- gasPrice: null,
598
- gasLimit: handleUint(fields[4], "gasLimit"),
599
- to: handleAddress(fields[5]),
600
- value: handleUint(fields[6], "value"),
601
- data: hexlify(fields[7]),
602
- accessList: handleAccessList(fields[8], "accessList"),
603
- maxFeePerBlobGas: handleUint(fields[9], "maxFeePerBlobGas"),
604
- blobVersionedHashes: fields[10]
605
- };
606
-
607
- if (blobs) { tx.blobs = blobs; }
608
-
609
- assertArgument(tx.to != null, `invalid address for transaction type: ${ typeName }`, "data", data);
610
-
611
- assertArgument(Array.isArray(tx.blobVersionedHashes), "invalid blobVersionedHashes: must be an array", "data", data);
612
- for (let i = 0; i < tx.blobVersionedHashes.length; i++) {
613
- assertArgument(isHexString(tx.blobVersionedHashes[i], 32), `invalid blobVersionedHash at index ${ i }: must be length 32`, "data", data);
614
- }
615
-
616
- // Unsigned EIP-4844 Transaction
617
- if (fields.length === 11) { return tx; }
618
-
619
- // @TODO: Do we need to do this? This is only called internally
620
- // and used to verify hashes; it might save time to not do this
621
- //tx.hash = keccak256(concat([ "0x03", encodeRlp(fields) ]));
622
-
623
- _parseEipSignature(tx, fields.slice(11));
624
-
625
- return tx;
626
- }
627
-
628
- function _serializeEip4844(tx: Transaction, sig: null | Signature, blobs: null | Array<Blob>): string {
629
- const fields: Array<any> = [
630
- formatNumber(tx.chainId, "chainId"),
631
- formatNumber(tx.nonce, "nonce"),
632
- formatNumber(tx.maxPriorityFeePerGas || 0, "maxPriorityFeePerGas"),
633
- formatNumber(tx.maxFeePerGas || 0, "maxFeePerGas"),
634
- formatNumber(tx.gasLimit, "gasLimit"),
635
- (tx.to || ZeroAddress),
636
- formatNumber(tx.value, "value"),
637
- tx.data,
638
- formatAccessList(tx.accessList || [ ]),
639
- formatNumber(tx.maxFeePerBlobGas || 0, "maxFeePerBlobGas"),
640
- formatHashes(tx.blobVersionedHashes || [ ], "blobVersionedHashes")
641
- ];
642
-
643
- if (sig) {
644
- fields.push(formatNumber(sig.yParity, "yParity"));
645
- fields.push(toBeArray(sig.r));
646
- fields.push(toBeArray(sig.s));
647
-
648
- // We have blobs; return the network wrapped format
649
- if (blobs) {
650
- return concat([
651
- "0x03",
652
- encodeRlp([
653
- fields,
654
- blobs.map((b) => b.data),
655
- blobs.map((b) => b.commitment),
656
- blobs.map((b) => b.proof),
657
- ])
658
- ]);
659
- }
660
-
661
- }
662
-
663
- return concat([ "0x03", encodeRlp(fields)]);
664
- }
665
-
666
- function _parseEip7702(data: Uint8Array): TransactionLike {
667
- const fields: any = decodeRlp(getBytes(data).slice(1));
668
-
669
- assertArgument(Array.isArray(fields) && (fields.length === 10 || fields.length === 13),
670
- "invalid field count for transaction type: 4", "data", hexlify(data));
671
-
672
- const tx: TransactionLike = {
673
- type: 4,
674
- chainId: handleUint(fields[0], "chainId"),
675
- nonce: handleNumber(fields[1], "nonce"),
676
- maxPriorityFeePerGas: handleUint(fields[2], "maxPriorityFeePerGas"),
677
- maxFeePerGas: handleUint(fields[3], "maxFeePerGas"),
678
- gasPrice: null,
679
- gasLimit: handleUint(fields[4], "gasLimit"),
680
- to: handleAddress(fields[5]),
681
- value: handleUint(fields[6], "value"),
682
- data: hexlify(fields[7]),
683
- accessList: handleAccessList(fields[8], "accessList"),
684
- authorizationList: handleAuthorizationList(fields[9], "authorizationList"),
685
- };
686
-
687
- // Unsigned EIP-7702 Transaction
688
- if (fields.length === 10) { return tx; }
689
-
690
- _parseEipSignature(tx, fields.slice(10));
691
-
692
- return tx;
693
- }
694
-
695
- function _serializeEip7702(tx: Transaction, sig: null | Signature): string {
696
- const fields: Array<any> = [
697
- formatNumber(tx.chainId, "chainId"),
698
- formatNumber(tx.nonce, "nonce"),
699
- formatNumber(tx.maxPriorityFeePerGas || 0, "maxPriorityFeePerGas"),
700
- formatNumber(tx.maxFeePerGas || 0, "maxFeePerGas"),
701
- formatNumber(tx.gasLimit, "gasLimit"),
702
- (tx.to || "0x"),
703
- formatNumber(tx.value, "value"),
704
- tx.data,
705
- formatAccessList(tx.accessList || [ ]),
706
- formatAuthorizationList(tx.authorizationList || [ ])
707
- ];
708
-
709
- if (sig) {
710
- fields.push(formatNumber(sig.yParity, "yParity"));
711
- fields.push(toBeArray(sig.r));
712
- fields.push(toBeArray(sig.s));
713
- }
714
-
715
- return concat([ "0x04", encodeRlp(fields)]);
716
- }
717
-
718
- /**
719
- * A **Transaction** describes an operation to be executed on
720
- * Ethereum by an Externally Owned Account (EOA). It includes
721
- * who (the [[to]] address), what (the [[data]]) and how much (the
722
- * [[value]] in ether) the operation should entail.
723
- *
724
- * @example:
725
- * tx = new Transaction()
726
- * //_result:
727
- *
728
- * tx.data = "0x1234";
729
- * //_result:
730
- */
731
- export class Transaction implements TransactionLike<string> {
732
- #type: null | number;
733
- #to: null | string;
734
- #data: string;
735
- #nonce: number;
736
- #gasLimit: bigint;
737
- #gasPrice: null | bigint;
738
- #maxPriorityFeePerGas: null | bigint;
739
- #maxFeePerGas: null | bigint;
740
- #value: bigint;
741
- #chainId: bigint;
742
- #sig: null | Signature;
743
- #accessList: null | AccessList;
744
- #maxFeePerBlobGas: null | bigint;
745
- #blobVersionedHashes: null | Array<string>;
746
- #kzg: null | KzgLibrary;
747
- #blobs: null | Array<Blob>;
748
- #auths: null | Array<Authorization>;
749
-
750
- /**
751
- * The transaction type.
752
- *
753
- * If null, the type will be automatically inferred based on
754
- * explicit properties.
755
- */
756
- get type(): null | number { return this.#type; }
757
- set type(value: null | number | string) {
758
- switch (value) {
759
- case null:
760
- this.#type = null;
761
- break;
762
- case 0: case "legacy":
763
- this.#type = 0;
764
- break;
765
- case 1: case "berlin": case "eip-2930":
766
- this.#type = 1;
767
- break;
768
- case 2: case "london": case "eip-1559":
769
- this.#type = 2;
770
- break;
771
- case 3: case "cancun": case "eip-4844":
772
- this.#type = 3;
773
- break;
774
- case 4: case "pectra": case "eip-7702":
775
- this.#type = 4;
776
- break;
777
- default:
778
- assertArgument(false, "unsupported transaction type", "type", value);
779
- }
780
- }
781
-
782
- /**
783
- * The name of the transaction type.
784
- */
785
- get typeName(): null | string {
786
- switch (this.type) {
787
- case 0: return "legacy";
788
- case 1: return "eip-2930";
789
- case 2: return "eip-1559";
790
- case 3: return "eip-4844";
791
- case 4: return "eip-7702";
792
- }
793
-
794
- return null;
795
- }
796
-
797
- /**
798
- * The ``to`` address for the transaction or ``null`` if the
799
- * transaction is an ``init`` transaction.
800
- */
801
- get to(): null | string {
802
- const value = this.#to;
803
- if (value == null && this.type === 3) { return ZeroAddress; }
804
- return value;
805
- }
806
- set to(value: null | string) {
807
- this.#to = (value == null) ? null: getAddress(value);
808
- }
809
-
810
- /**
811
- * The transaction nonce.
812
- */
813
- get nonce(): number { return this.#nonce; }
814
- set nonce(value: BigNumberish) { this.#nonce = getNumber(value, "value"); }
815
-
816
- /**
817
- * The gas limit.
818
- */
819
- get gasLimit(): bigint { return this.#gasLimit; }
820
- set gasLimit(value: BigNumberish) { this.#gasLimit = getBigInt(value); }
821
-
822
- /**
823
- * The gas price.
824
- *
825
- * On legacy networks this defines the fee that will be paid. On
826
- * EIP-1559 networks, this should be ``null``.
827
- */
828
- get gasPrice(): null | bigint {
829
- const value = this.#gasPrice;
830
- if (value == null && (this.type === 0 || this.type === 1)) { return BN_0; }
831
- return value;
832
- }
833
- set gasPrice(value: null | BigNumberish) {
834
- this.#gasPrice = (value == null) ? null: getBigInt(value, "gasPrice");
835
- }
836
-
837
- /**
838
- * The maximum priority fee per unit of gas to pay. On legacy
839
- * networks this should be ``null``.
840
- */
841
- get maxPriorityFeePerGas(): null | bigint {
842
- const value = this.#maxPriorityFeePerGas;
843
- if (value == null) {
844
- if (this.type === 2 || this.type === 3) { return BN_0; }
845
- return null;
846
- }
847
- return value;
848
- }
849
- set maxPriorityFeePerGas(value: null | BigNumberish) {
850
- this.#maxPriorityFeePerGas = (value == null) ? null: getBigInt(value, "maxPriorityFeePerGas");
851
- }
852
-
853
- /**
854
- * The maximum total fee per unit of gas to pay. On legacy
855
- * networks this should be ``null``.
856
- */
857
- get maxFeePerGas(): null | bigint {
858
- const value = this.#maxFeePerGas;
859
- if (value == null) {
860
- if (this.type === 2 || this.type === 3) { return BN_0; }
861
- return null;
862
- }
863
- return value;
864
- }
865
- set maxFeePerGas(value: null | BigNumberish) {
866
- this.#maxFeePerGas = (value == null) ? null: getBigInt(value, "maxFeePerGas");
867
- }
868
-
869
- /**
870
- * The transaction data. For ``init`` transactions this is the
871
- * deployment code.
872
- */
873
- get data(): string { return this.#data; }
874
- set data(value: BytesLike) { this.#data = hexlify(value); }
875
-
876
- /**
877
- * The amount of ether (in wei) to send in this transactions.
878
- */
879
- get value(): bigint { return this.#value; }
880
- set value(value: BigNumberish) {
881
- this.#value = getBigInt(value, "value");
882
- }
883
-
884
- /**
885
- * The chain ID this transaction is valid on.
886
- */
887
- get chainId(): bigint { return this.#chainId; }
888
- set chainId(value: BigNumberish) { this.#chainId = getBigInt(value); }
889
-
890
- /**
891
- * If signed, the signature for this transaction.
892
- */
893
- get signature(): null | Signature { return this.#sig || null; }
894
- set signature(value: null | SignatureLike) {
895
- this.#sig = (value == null) ? null: Signature.from(value);
896
- }
897
-
898
- /**
899
- * The access list.
900
- *
901
- * An access list permits discounted (but pre-paid) access to
902
- * bytecode and state variable access within contract execution.
903
- */
904
- get accessList(): null | AccessList {
905
- const value = this.#accessList || null;
906
- if (value == null) {
907
- if (this.type === 1 || this.type === 2 || this.type === 3) {
908
- // @TODO: in v7, this should assign the value or become
909
- // a live object itself, otherwise mutation is inconsistent
910
- return [ ];
911
- }
912
- return null;
913
- }
914
- return value;
915
- }
916
- set accessList(value: null | AccessListish) {
917
- this.#accessList = (value == null) ? null: accessListify(value);
918
- }
919
-
920
- get authorizationList(): null | Array<Authorization> {
921
- const value = this.#auths || null;
922
- if (value == null) {
923
- if (this.type === 4) {
924
- // @TODO: in v7, this should become a live object itself,
925
- // otherwise mutation is inconsistent
926
- return [ ];
927
- }
928
- }
929
- return value;
930
- }
931
- set authorizationList(auths: null | Array<AuthorizationLike>) {
932
- this.#auths = (auths == null) ? null: auths.map((a) =>
933
- authorizationify(a));
934
- }
935
-
936
- /**
937
- * The max fee per blob gas for Cancun transactions.
938
- */
939
- get maxFeePerBlobGas(): null | bigint {
940
- const value = this.#maxFeePerBlobGas;
941
- if (value == null && this.type === 3) { return BN_0; }
942
- return value;
943
- }
944
- set maxFeePerBlobGas(value: null | BigNumberish) {
945
- this.#maxFeePerBlobGas = (value == null) ? null: getBigInt(value, "maxFeePerBlobGas");
946
- }
947
-
948
- /**
949
- * The BLOb versioned hashes for Cancun transactions.
950
- */
951
- get blobVersionedHashes(): null | Array<string> {
952
- // @TODO: Mutation is inconsistent; if unset, the returned value
953
- // cannot mutate the object, if set it can
954
- let value = this.#blobVersionedHashes;
955
- if (value == null && this.type === 3) { return [ ]; }
956
- return value;
957
- }
958
- set blobVersionedHashes(value: null | Array<string>) {
959
- if (value != null) {
960
- assertArgument(Array.isArray(value), "blobVersionedHashes must be an Array", "value", value);
961
- value = value.slice();
962
- for (let i = 0; i < value.length; i++) {
963
- assertArgument(isHexString(value[i], 32), "invalid blobVersionedHash", `value[${ i }]`, value[i]);
964
- }
965
- }
966
- this.#blobVersionedHashes = value;
967
- }
968
-
969
- /**
970
- * The BLObs for the Transaction, if any.
971
- *
972
- * If ``blobs`` is non-``null``, then the [[seriailized]]
973
- * will return the network formatted sidecar, otherwise it
974
- * will return the standard [[link-eip-2718]] payload. The
975
- * [[unsignedSerialized]] is unaffected regardless.
976
- *
977
- * When setting ``blobs``, either fully valid [[Blob]] objects
978
- * may be specified (i.e. correctly padded, with correct
979
- * committments and proofs) or a raw [[BytesLike]] may
980
- * be provided.
981
- *
982
- * If raw [[BytesLike]] are provided, the [[kzg]] property **must**
983
- * be already set. The blob will be correctly padded and the
984
- * [[KzgLibrary]] will be used to compute the committment and
985
- * proof for the blob.
986
- *
987
- * A BLOb is a sequence of field elements, each of which must
988
- * be within the BLS field modulo, so some additional processing
989
- * may be required to encode arbitrary data to ensure each 32 byte
990
- * field is within the valid range.
991
- *
992
- * Setting this automatically populates [[blobVersionedHashes]],
993
- * overwriting any existing values. Setting this to ``null``
994
- * does **not** remove the [[blobVersionedHashes]], leaving them
995
- * present.
996
- */
997
- get blobs(): null | Array<Blob> {
998
- if (this.#blobs == null) { return null; }
999
- return this.#blobs.map((b) => Object.assign({ }, b));
1000
- }
1001
- set blobs(_blobs: null | Array<BlobLike>) {
1002
- if (_blobs == null) {
1003
- this.#blobs = null;
1004
- return;
1005
- }
1006
-
1007
- const blobs: Array<Blob> = [ ];
1008
- const versionedHashes: Array<string> = [ ];
1009
- for (let i = 0; i < _blobs.length; i++) {
1010
- const blob = _blobs[i];
1011
-
1012
- if (isBytesLike(blob)) {
1013
- assert(this.#kzg, "adding a raw blob requires a KZG library", "UNSUPPORTED_OPERATION", {
1014
- operation: "set blobs()"
1015
- });
1016
-
1017
- let data = getBytes(blob);
1018
- assertArgument(data.length <= BLOB_SIZE, "blob is too large", `blobs[${ i }]`, blob);
1019
-
1020
- // Pad blob if necessary
1021
- if (data.length !== BLOB_SIZE) {
1022
- const padded = new Uint8Array(BLOB_SIZE);
1023
- padded.set(data);
1024
- data = padded;
1025
- }
1026
-
1027
- const commit = this.#kzg.blobToKzgCommitment(data);
1028
- const proof = hexlify(this.#kzg.computeBlobKzgProof(data, commit));
1029
-
1030
- blobs.push({
1031
- data: hexlify(data),
1032
- commitment: hexlify(commit),
1033
- proof
1034
- });
1035
- versionedHashes.push(getVersionedHash(1, commit));
1036
-
1037
- } else {
1038
- const commit = hexlify(blob.commitment);
1039
- blobs.push({
1040
- data: hexlify(blob.data),
1041
- commitment: commit,
1042
- proof: hexlify(blob.proof)
1043
- });
1044
- versionedHashes.push(getVersionedHash(1, commit));
1045
- }
1046
- }
1047
-
1048
- this.#blobs = blobs;
1049
- this.#blobVersionedHashes = versionedHashes;
1050
- }
1051
-
1052
- get kzg(): null | KzgLibrary { return this.#kzg; }
1053
- set kzg(kzg: null | KzgLibraryLike) {
1054
- if (kzg == null) {
1055
- this.#kzg = null;
1056
- } else {
1057
- this.#kzg = getKzgLibrary(kzg);
1058
- }
1059
- }
1060
-
1061
- /**
1062
- * Creates a new Transaction with default values.
1063
- */
1064
- constructor() {
1065
- this.#type = null;
1066
- this.#to = null;
1067
- this.#nonce = 0;
1068
- this.#gasLimit = BN_0;
1069
- this.#gasPrice = null;
1070
- this.#maxPriorityFeePerGas = null;
1071
- this.#maxFeePerGas = null;
1072
- this.#data = "0x";
1073
- this.#value = BN_0;
1074
- this.#chainId = BN_0;
1075
- this.#sig = null;
1076
- this.#accessList = null;
1077
- this.#maxFeePerBlobGas = null;
1078
- this.#blobVersionedHashes = null;
1079
- this.#kzg = null;
1080
- this.#blobs = null;
1081
- this.#auths = null;
1082
- }
1083
-
1084
- /**
1085
- * The transaction hash, if signed. Otherwise, ``null``.
1086
- */
1087
- get hash(): null | string {
1088
- if (this.signature == null) { return null; }
1089
- return keccak256(this.#getSerialized(true, false));
1090
- }
1091
-
1092
- /**
1093
- * The pre-image hash of this transaction.
1094
- *
1095
- * This is the digest that a [[Signer]] must sign to authorize
1096
- * this transaction.
1097
- */
1098
- get unsignedHash(): string {
1099
- return keccak256(this.unsignedSerialized);
1100
- }
1101
-
1102
- /**
1103
- * The sending address, if signed. Otherwise, ``null``.
1104
- */
1105
- get from(): null | string {
1106
- if (this.signature == null) { return null; }
1107
- return recoverAddress(this.unsignedHash, this.signature);
1108
- }
1109
-
1110
- /**
1111
- * The public key of the sender, if signed. Otherwise, ``null``.
1112
- */
1113
- get fromPublicKey(): null | string {
1114
- if (this.signature == null) { return null; }
1115
- return SigningKey.recoverPublicKey(this.unsignedHash, this.signature);
1116
- }
1117
-
1118
- /**
1119
- * Returns true if signed.
1120
- *
1121
- * This provides a Type Guard that properties requiring a signed
1122
- * transaction are non-null.
1123
- */
1124
- isSigned(): this is (Transaction & { type: number, typeName: string, from: string, signature: Signature }) {
1125
- return this.signature != null;
1126
- }
1127
-
1128
- #getSerialized(signed: boolean, sidecar: boolean): string {
1129
- assert(!signed || this.signature != null, "cannot serialize unsigned transaction; maybe you meant .unsignedSerialized", "UNSUPPORTED_OPERATION", { operation: ".serialized"});
1130
-
1131
- const sig = signed ? this.signature: null;
1132
- switch (this.inferType()) {
1133
- case 0:
1134
- return _serializeLegacy(this, sig);
1135
- case 1:
1136
- return _serializeEip2930(this, sig);
1137
- case 2:
1138
- return _serializeEip1559(this, sig);
1139
- case 3:
1140
- return _serializeEip4844(this, sig, sidecar ? this.blobs: null);
1141
- case 4:
1142
- return _serializeEip7702(this, sig);
1143
- }
1144
-
1145
- assert(false, "unsupported transaction type", "UNSUPPORTED_OPERATION", { operation: ".serialized" });
1146
- }
1147
-
1148
- /**
1149
- * The serialized transaction.
1150
- *
1151
- * This throws if the transaction is unsigned. For the pre-image,
1152
- * use [[unsignedSerialized]].
1153
- */
1154
- get serialized(): string {
1155
- return this.#getSerialized(true, true);
1156
- }
1157
-
1158
- /**
1159
- * The transaction pre-image.
1160
- *
1161
- * The hash of this is the digest which needs to be signed to
1162
- * authorize this transaction.
1163
- */
1164
- get unsignedSerialized(): string {
1165
- return this.#getSerialized(false, false);
1166
- }
1167
-
1168
- /**
1169
- * Return the most "likely" type; currently the highest
1170
- * supported transaction type.
1171
- */
1172
- inferType(): number {
1173
- const types = this.inferTypes();
1174
-
1175
- // Prefer London (EIP-1559) over Cancun (BLOb)
1176
- if (types.indexOf(2) >= 0) { return 2; }
1177
-
1178
- // Return the highest inferred type
1179
- return <number>(types.pop());
1180
- }
1181
-
1182
- /**
1183
- * Validates the explicit properties and returns a list of compatible
1184
- * transaction types.
1185
- */
1186
- inferTypes(): Array<number> {
1187
-
1188
- // Checks that there are no conflicting properties set
1189
- const hasGasPrice = this.gasPrice != null;
1190
- const hasFee = (this.maxFeePerGas != null || this.maxPriorityFeePerGas != null);
1191
- const hasAccessList = (this.accessList != null);
1192
- const hasBlob = (this.#maxFeePerBlobGas != null || this.#blobVersionedHashes);
1193
-
1194
- //if (hasGasPrice && hasFee) {
1195
- // throw new Error("transaction cannot have gasPrice and maxFeePerGas");
1196
- //}
1197
-
1198
- if (this.maxFeePerGas != null && this.maxPriorityFeePerGas != null) {
1199
- assert(this.maxFeePerGas >= this.maxPriorityFeePerGas, "priorityFee cannot be more than maxFee", "BAD_DATA", { value: this });
1200
- }
1201
-
1202
- //if (this.type === 2 && hasGasPrice) {
1203
- // throw new Error("eip-1559 transaction cannot have gasPrice");
1204
- //}
1205
-
1206
- assert(!hasFee || (this.type !== 0 && this.type !== 1), "transaction type cannot have maxFeePerGas or maxPriorityFeePerGas", "BAD_DATA", { value: this });
1207
- assert(this.type !== 0 || !hasAccessList, "legacy transaction cannot have accessList", "BAD_DATA", { value: this })
1208
-
1209
- const types: Array<number> = [ ];
1210
-
1211
- // Explicit type
1212
- if (this.type != null) {
1213
- types.push(this.type);
1214
-
1215
- } else {
1216
- if (this.authorizationList && this.authorizationList.length) {
1217
- types.push(4);
1218
- } else if (hasFee) {
1219
- types.push(2);
1220
- } else if (hasGasPrice) {
1221
- types.push(1);
1222
- if (!hasAccessList) { types.push(0); }
1223
- } else if (hasAccessList) {
1224
- types.push(1);
1225
- types.push(2);
1226
- } else if (hasBlob && this.to) {
1227
- types.push(3);
1228
- } else {
1229
- types.push(0);
1230
- types.push(1);
1231
- types.push(2);
1232
- types.push(3);
1233
- }
1234
- }
1235
-
1236
- types.sort();
1237
-
1238
- return types;
1239
- }
1240
-
1241
- /**
1242
- * Returns true if this transaction is a legacy transaction (i.e.
1243
- * ``type === 0``).
1244
- *
1245
- * This provides a Type Guard that the related properties are
1246
- * non-null.
1247
- */
1248
- isLegacy(): this is (Transaction & { type: 0, gasPrice: bigint }) {
1249
- return (this.type === 0);
1250
- }
1251
-
1252
- /**
1253
- * Returns true if this transaction is berlin hardform transaction (i.e.
1254
- * ``type === 1``).
1255
- *
1256
- * This provides a Type Guard that the related properties are
1257
- * non-null.
1258
- */
1259
- isBerlin(): this is (Transaction & { type: 1, gasPrice: bigint, accessList: AccessList }) {
1260
- return (this.type === 1);
1261
- }
1262
-
1263
- /**
1264
- * Returns true if this transaction is london hardform transaction (i.e.
1265
- * ``type === 2``).
1266
- *
1267
- * This provides a Type Guard that the related properties are
1268
- * non-null.
1269
- */
1270
- isLondon(): this is (Transaction & { type: 2, accessList: AccessList, maxFeePerGas: bigint, maxPriorityFeePerGas: bigint }) {
1271
- return (this.type === 2);
1272
- }
1273
-
1274
- /**
1275
- * Returns true if this transaction is an [[link-eip-4844]] BLOB
1276
- * transaction.
1277
- *
1278
- * This provides a Type Guard that the related properties are
1279
- * non-null.
1280
- */
1281
- isCancun(): this is (Transaction & { type: 3, to: string, accessList: AccessList, maxFeePerGas: bigint, maxPriorityFeePerGas: bigint, maxFeePerBlobGas: bigint, blobVersionedHashes: Array<string> }) {
1282
- return (this.type === 3);
1283
- }
1284
-
1285
- /**
1286
- * Create a copy of this transaciton.
1287
- */
1288
- clone(): Transaction {
1289
- return Transaction.from(this);
1290
- }
1291
-
1292
- /**
1293
- * Return a JSON-friendly object.
1294
- */
1295
- toJSON(): any {
1296
- const s = (v: null | bigint) => {
1297
- if (v == null) { return null; }
1298
- return v.toString();
1299
- };
1300
-
1301
- return {
1302
- type: this.type,
1303
- to: this.to,
1304
- // from: this.from,
1305
- data: this.data,
1306
- nonce: this.nonce,
1307
- gasLimit: s(this.gasLimit),
1308
- gasPrice: s(this.gasPrice),
1309
- maxPriorityFeePerGas: s(this.maxPriorityFeePerGas),
1310
- maxFeePerGas: s(this.maxFeePerGas),
1311
- value: s(this.value),
1312
- chainId: s(this.chainId),
1313
- sig: this.signature ? this.signature.toJSON(): null,
1314
- accessList: this.accessList
1315
- };
1316
- }
1317
-
1318
- /**
1319
- * Create a **Transaction** from a serialized transaction or a
1320
- * Transaction-like object.
1321
- */
1322
- static from(tx?: string | TransactionLike<string>): Transaction {
1323
- if (tx == null) { return new Transaction(); }
1324
-
1325
- if (typeof(tx) === "string") {
1326
- const payload = getBytes(tx);
1327
-
1328
- if (payload[0] >= 0x7f) { // @TODO: > vs >= ??
1329
- return Transaction.from(_parseLegacy(payload));
1330
- }
1331
-
1332
- switch(payload[0]) {
1333
- case 1: return Transaction.from(_parseEip2930(payload));
1334
- case 2: return Transaction.from(_parseEip1559(payload));
1335
- case 3: return Transaction.from(_parseEip4844(payload));
1336
- case 4: return Transaction.from(_parseEip7702(payload));
1337
- }
1338
- assert(false, "unsupported transaction type", "UNSUPPORTED_OPERATION", { operation: "from" });
1339
- }
1340
-
1341
- const result = new Transaction();
1342
- if (tx.type != null) { result.type = tx.type; }
1343
- if (tx.to != null) { result.to = tx.to; }
1344
- if (tx.nonce != null) { result.nonce = tx.nonce; }
1345
- if (tx.gasLimit != null) { result.gasLimit = tx.gasLimit; }
1346
- if (tx.gasPrice != null) { result.gasPrice = tx.gasPrice; }
1347
- if (tx.maxPriorityFeePerGas != null) { result.maxPriorityFeePerGas = tx.maxPriorityFeePerGas; }
1348
- if (tx.maxFeePerGas != null) { result.maxFeePerGas = tx.maxFeePerGas; }
1349
- if (tx.maxFeePerBlobGas != null) { result.maxFeePerBlobGas = tx.maxFeePerBlobGas; }
1350
- if (tx.data != null) { result.data = tx.data; }
1351
- if (tx.value != null) { result.value = tx.value; }
1352
- if (tx.chainId != null) { result.chainId = tx.chainId; }
1353
- if (tx.signature != null) { result.signature = Signature.from(tx.signature); }
1354
- if (tx.accessList != null) { result.accessList = tx.accessList; }
1355
- if (tx.authorizationList != null) {
1356
- result.authorizationList = tx.authorizationList;
1357
- }
1358
-
1359
- // This will get overwritten by blobs, if present
1360
- if (tx.blobVersionedHashes != null) { result.blobVersionedHashes = tx.blobVersionedHashes; }
1361
-
1362
- // Make sure we assign the kzg before assigning blobs, which
1363
- // require the library in the event raw blob data is provided.
1364
- if (tx.kzg != null) { result.kzg = tx.kzg; }
1365
- if (tx.blobs != null) { result.blobs = tx.blobs; }
1366
-
1367
- if (tx.hash != null) {
1368
- assertArgument(result.isSigned(), "unsigned transaction cannot define '.hash'", "tx", tx);
1369
- assertArgument(result.hash === tx.hash, "hash mismatch", "tx", tx);
1370
- }
1371
-
1372
- if (tx.from != null) {
1373
- assertArgument(result.isSigned(), "unsigned transaction cannot define '.from'", "tx", tx);
1374
- assertArgument(result.from.toLowerCase() === (tx.from || "").toLowerCase(), "from mismatch", "tx", tx);
1375
- }
1376
-
1377
- return result;
1378
- }
1379
- }
1
+
2
+ import { getAddress } from "../address/index.js";
3
+ import { ZeroAddress } from "../constants/addresses.js";
4
+ import {
5
+ keccak256, sha256, Signature, SigningKey
6
+ } from "../crypto/index.js";
7
+ import {
8
+ concat, decodeRlp, encodeRlp, getBytes, getBigInt, getNumber, hexlify,
9
+ assert, assertArgument, isBytesLike, isHexString, toBeArray, zeroPadValue
10
+ } from "../utils/index.js";
11
+
12
+ import { accessListify } from "./accesslist.js";
13
+ import { authorizationify } from "./authorization.js";
14
+ import { recoverAddress } from "./address.js";
15
+
16
+ import type { BigNumberish, BytesLike } from "../utils/index.js";
17
+ import type { SignatureLike } from "../crypto/index.js";
18
+
19
+ import type {
20
+ AccessList, AccessListish, Authorization, AuthorizationLike
21
+ } from "./index.js";
22
+
23
+
24
+ const BN_0 = BigInt(0);
25
+ const BN_2 = BigInt(2);
26
+ const BN_27 = BigInt(27)
27
+ const BN_28 = BigInt(28)
28
+ const BN_35 = BigInt(35);
29
+ const BN_MAX_UINT = BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
30
+
31
+ const BLOB_SIZE = 4096 * 32;
32
+
33
+ // The BLS Modulo; each field within a BLOb must be less than this
34
+ //const BLOB_BLS_MODULO = BigInt("0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001");
35
+
36
+ /**
37
+ * A **TransactionLike** is an object which is appropriate as a loose
38
+ * input for many operations which will populate missing properties of
39
+ * a transaction.
40
+ */
41
+ export interface TransactionLike<A = string> {
42
+ /**
43
+ * The type.
44
+ */
45
+ type?: null | number;
46
+
47
+ /**
48
+ * The recipient address or ``null`` for an ``init`` transaction.
49
+ */
50
+ to?: null | A;
51
+
52
+ /**
53
+ * The sender.
54
+ */
55
+ from?: null | A;
56
+
57
+ /**
58
+ * The nonce.
59
+ */
60
+ nonce?: null | number;
61
+
62
+ /**
63
+ * The maximum amount of gas that can be used.
64
+ */
65
+ gasLimit?: null | BigNumberish;
66
+
67
+ /**
68
+ * The gas price for legacy and berlin transactions.
69
+ */
70
+ gasPrice?: null | BigNumberish;
71
+
72
+ /**
73
+ * The maximum priority fee per gas for london transactions.
74
+ */
75
+ maxPriorityFeePerGas?: null | BigNumberish;
76
+
77
+ /**
78
+ * The maximum total fee per gas for london transactions.
79
+ */
80
+ maxFeePerGas?: null | BigNumberish;
81
+
82
+ /**
83
+ * The data.
84
+ */
85
+ data?: null | string;
86
+
87
+ /**
88
+ * The value (in wei) to send.
89
+ */
90
+ value?: null | BigNumberish;
91
+
92
+ /**
93
+ * The chain ID the transaction is valid on.
94
+ */
95
+ chainId?: null | BigNumberish;
96
+
97
+ /**
98
+ * The transaction hash.
99
+ */
100
+ hash?: null | string;
101
+
102
+ /**
103
+ * The signature provided by the sender.
104
+ */
105
+ signature?: null | SignatureLike;
106
+
107
+ /**
108
+ * The access list for berlin and london transactions.
109
+ */
110
+ accessList?: null | AccessListish;
111
+
112
+ /**
113
+ * The maximum fee per blob gas (see [[link-eip-4844]]).
114
+ */
115
+ maxFeePerBlobGas?: null | BigNumberish;
116
+
117
+ /**
118
+ * The versioned hashes (see [[link-eip-4844]]).
119
+ */
120
+ blobVersionedHashes?: null | Array<string>;
121
+
122
+ /**
123
+ * The blobs (if any) attached to this transaction (see [[link-eip-4844]]).
124
+ */
125
+ blobs?: null | Array<BlobLike>
126
+
127
+ /**
128
+ * An external library for computing the KZG commitments and
129
+ * proofs necessary for EIP-4844 transactions (see [[link-eip-4844]]).
130
+ *
131
+ * This is generally ``null``, unless you are creating BLOb
132
+ * transactions.
133
+ */
134
+ kzg?: null | KzgLibraryLike;
135
+
136
+ /**
137
+ * The [[link-eip-7702]] authorizations (if any).
138
+ */
139
+ authorizationList?: null | Array<Authorization>;
140
+ }
141
+
142
+ /**
143
+ * A full-valid BLOb object for [[link-eip-4844]] transactions.
144
+ *
145
+ * The commitment and proof should have been computed using a
146
+ * KZG library.
147
+ */
148
+ export interface Blob {
149
+ data: string;
150
+ proof: string;
151
+ commitment: string;
152
+ }
153
+
154
+ /**
155
+ * A BLOb object that can be passed for [[link-eip-4844]]
156
+ * transactions.
157
+ *
158
+ * It may have had its commitment and proof already provided
159
+ * or rely on an attached [[KzgLibrary]] to compute them.
160
+ */
161
+ export type BlobLike = BytesLike | {
162
+ data: BytesLike;
163
+ proof: BytesLike;
164
+ commitment: BytesLike;
165
+ };
166
+
167
+ /**
168
+ * A KZG Library with the necessary functions to compute
169
+ * BLOb commitments and proofs.
170
+ */
171
+ export interface KzgLibrary {
172
+ blobToKzgCommitment: (blob: Uint8Array) => Uint8Array;
173
+ computeBlobKzgProof: (blob: Uint8Array, commitment: Uint8Array) => Uint8Array;
174
+ }
175
+
176
+ /**
177
+ * A KZG Library with any of the various API configurations.
178
+ * As the library is still experimental and the API is not
179
+ * stable, depending on the version used the method names and
180
+ * signatures are still in flux.
181
+ *
182
+ * This allows any of the versions to be passed into Transaction
183
+ * while providing a stable external API.
184
+ */
185
+ export type KzgLibraryLike = KzgLibrary | {
186
+ // kzg-wasm >= 0.5.0
187
+ blobToKZGCommitment: (blob: string) => string;
188
+ computeBlobKZGProof: (blob: string, commitment: string) => string;
189
+ } | {
190
+ // micro-ecc-signer
191
+ blobToKzgCommitment: (blob: string) => string | Uint8Array;
192
+ computeBlobProof: (blob: string, commitment: string) => string | Uint8Array;
193
+ };
194
+
195
+ function getKzgLibrary(kzg: KzgLibraryLike): KzgLibrary {
196
+
197
+ const blobToKzgCommitment = (blob: Uint8Array) => {
198
+
199
+ if ("computeBlobProof" in kzg) {
200
+ // micro-ecc-signer; check for computeBlobProof since this API
201
+ // expects a string while the kzg-wasm below expects a Unit8Array
202
+
203
+ if ("blobToKzgCommitment" in kzg && typeof(kzg.blobToKzgCommitment) === "function") {
204
+ return getBytes(kzg.blobToKzgCommitment(hexlify(blob)))
205
+ }
206
+
207
+ } else if ("blobToKzgCommitment" in kzg && typeof(kzg.blobToKzgCommitment) === "function") {
208
+ // kzg-wasm <0.5.0; blobToKzgCommitment(Uint8Array) => Uint8Array
209
+
210
+ return getBytes(kzg.blobToKzgCommitment(blob));
211
+ }
212
+
213
+ // kzg-wasm >= 0.5.0; blobToKZGCommitment(string) => string
214
+ if ("blobToKZGCommitment" in kzg && typeof(kzg.blobToKZGCommitment) === "function") {
215
+ return getBytes(kzg.blobToKZGCommitment(hexlify(blob)));
216
+ }
217
+
218
+ assertArgument(false, "unsupported KZG library", "kzg", kzg);
219
+ };
220
+
221
+ const computeBlobKzgProof = (blob: Uint8Array, commitment: Uint8Array) => {
222
+
223
+ // micro-ecc-signer
224
+ if ("computeBlobProof" in kzg && typeof(kzg.computeBlobProof) === "function") {
225
+ return getBytes(kzg.computeBlobProof(hexlify(blob), hexlify(commitment)))
226
+ }
227
+
228
+ // kzg-wasm <0.5.0; computeBlobKzgProof(Uint8Array, Uint8Array) => Uint8Array
229
+ if ("computeBlobKzgProof" in kzg && typeof(kzg.computeBlobKzgProof) === "function") {
230
+ return kzg.computeBlobKzgProof(blob, commitment);
231
+ }
232
+
233
+ // kzg-wasm >= 0.5.0; computeBlobKZGProof(string, string) => string
234
+ if ("computeBlobKZGProof" in kzg && typeof(kzg.computeBlobKZGProof) === "function") {
235
+ return getBytes(kzg.computeBlobKZGProof(hexlify(blob), hexlify(commitment)));
236
+ }
237
+
238
+ assertArgument(false, "unsupported KZG library", "kzg", kzg);
239
+ };
240
+
241
+ return { blobToKzgCommitment, computeBlobKzgProof };
242
+ }
243
+
244
+ function getVersionedHash(version: number, hash: BytesLike): string {
245
+ let versioned = version.toString(16);
246
+ while (versioned.length < 2) { versioned = "0" + versioned; }
247
+ versioned += sha256(hash).substring(4);
248
+ return "0x" + versioned;
249
+ }
250
+
251
+ function handleAddress(value: string): null | string {
252
+ if (value === "0x") { return null; }
253
+ return getAddress(value);
254
+ }
255
+
256
+ function handleAccessList(value: any, param: string): AccessList {
257
+ try {
258
+ return accessListify(value);
259
+ } catch (error: any) {
260
+ assertArgument(false, error.message, param, value);
261
+ }
262
+ }
263
+
264
+ function handleAuthorizationList(value: any, param: string): Array<Authorization> {
265
+ try {
266
+ if (!Array.isArray(value)) { throw new Error("authorizationList: invalid array"); }
267
+ const result: Array<Authorization> = [ ];
268
+ for (let i = 0; i < value.length; i++) {
269
+ const auth: Array<string> = value[i];
270
+ if (!Array.isArray(auth)) { throw new Error(`authorization[${ i }]: invalid array`); }
271
+ if (auth.length !== 6) { throw new Error(`authorization[${ i }]: wrong length`); }
272
+ if (!auth[1]) { throw new Error(`authorization[${ i }]: null address`); }
273
+ result.push({
274
+ address: <string>handleAddress(auth[1]),
275
+ nonce: handleUint(auth[2], "nonce"),
276
+ chainId: handleUint(auth[0], "chainId"),
277
+ signature: Signature.from({
278
+ r: auth[4],
279
+ s: auth[5],
280
+ v: 1
281
+ })
282
+ });
283
+ }
284
+ return result;
285
+ } catch (error: any) {
286
+ assertArgument(false, error.message, param, value);
287
+ }
288
+ }
289
+
290
+ function handleNumber(_value: string, param: string): number {
291
+ if (_value === "0x") { return 0; }
292
+ return getNumber(_value, param);
293
+ }
294
+
295
+ function handleUint(_value: string, param: string): bigint {
296
+ if (_value === "0x") { return BN_0; }
297
+ const value = getBigInt(_value, param);
298
+ assertArgument(value <= BN_MAX_UINT, "value exceeds uint size", param, value);
299
+ return value;
300
+ }
301
+
302
+ function formatNumber(_value: BigNumberish, name: string): Uint8Array {
303
+ const value = getBigInt(_value, "value");
304
+ const result = toBeArray(value);
305
+ assertArgument(result.length <= 32, `value too large`, `tx.${ name }`, value);
306
+ return result;
307
+ }
308
+
309
+ function formatAccessList(value: AccessListish): Array<[ string, Array<string> ]> {
310
+ return accessListify(value).map((set) => [ set.address, set.storageKeys ]);
311
+ }
312
+
313
+ function formatAuthorizationList(value: Array<Authorization>): Array<Array<string | Uint8Array>> {
314
+ return value.map((a) => {
315
+ return [
316
+ formatNumber(a.chainId, "chainId"),
317
+ a.address,
318
+ formatNumber(a.nonce, "nonce"),
319
+ a.signature.r,
320
+ a.signature.s
321
+ ];
322
+ });
323
+ }
324
+
325
+ function formatHashes(value: Array<string>, param: string): Array<string> {
326
+ assertArgument(Array.isArray(value), `invalid ${ param }`, "value", value);
327
+ for (let i = 0; i < value.length; i++) {
328
+ assertArgument(isHexString(value[i], 32), "invalid ${ param } hash", `value[${ i }]`, value[i]);
329
+ }
330
+ return value;
331
+ }
332
+
333
+ function _parseLegacy(data: Uint8Array): TransactionLike {
334
+ const fields: any = decodeRlp(data);
335
+
336
+ assertArgument(Array.isArray(fields) && (fields.length === 9 || fields.length === 6),
337
+ "invalid field count for legacy transaction", "data", data);
338
+
339
+ const tx: TransactionLike = {
340
+ type: 0,
341
+ nonce: handleNumber(fields[0], "nonce"),
342
+ gasPrice: handleUint(fields[1], "gasPrice"),
343
+ gasLimit: handleUint(fields[2], "gasLimit"),
344
+ to: handleAddress(fields[3]),
345
+ value: handleUint(fields[4], "value"),
346
+ data: hexlify(fields[5]),
347
+ chainId: BN_0
348
+ };
349
+
350
+ // Legacy unsigned transaction
351
+ if (fields.length === 6) { return tx; }
352
+
353
+ const v = handleUint(fields[6], "v");
354
+ const r = handleUint(fields[7], "r");
355
+ const s = handleUint(fields[8], "s");
356
+
357
+ if (r === BN_0 && s === BN_0) {
358
+ // EIP-155 unsigned transaction
359
+ tx.chainId = v;
360
+
361
+ } else {
362
+
363
+ // Compute the EIP-155 chain ID (or 0 for legacy)
364
+ let chainId = (v - BN_35) / BN_2;
365
+ if (chainId < BN_0) { chainId = BN_0; }
366
+ tx.chainId = chainId
367
+
368
+ // Signed Legacy Transaction
369
+ assertArgument(chainId !== BN_0 || (v === BN_27 || v === BN_28), "non-canonical legacy v", "v", fields[6]);
370
+
371
+ tx.signature = Signature.from({
372
+ r: zeroPadValue(fields[7], 32),
373
+ s: zeroPadValue(fields[8], 32),
374
+ v
375
+ });
376
+
377
+ //tx.hash = keccak256(data);
378
+ }
379
+
380
+ return tx;
381
+ }
382
+
383
+ function _serializeLegacy(tx: Transaction, sig: null | Signature): string {
384
+ const fields: Array<any> = [
385
+ formatNumber(tx.nonce, "nonce"),
386
+ formatNumber(tx.gasPrice || 0, "gasPrice"),
387
+ formatNumber(tx.gasLimit, "gasLimit"),
388
+ (tx.to || "0x"),
389
+ formatNumber(tx.value, "value"),
390
+ tx.data,
391
+ ];
392
+
393
+ let chainId = BN_0;
394
+ if (tx.chainId != BN_0) {
395
+ // A chainId was provided; if non-zero we'll use EIP-155
396
+ chainId = getBigInt(tx.chainId, "tx.chainId");
397
+
398
+ } else if (tx.signature) {
399
+
400
+ }
401
+
402
+ // Requesting an unsigned transaction
403
+ if (!sig) {
404
+ // We have an EIP-155 transaction (chainId was specified and non-zero)
405
+ if (chainId !== BN_0) {
406
+ fields.push(toBeArray(chainId));
407
+ fields.push("0x");
408
+ fields.push("0x");
409
+ }
410
+
411
+ return encodeRlp(fields);
412
+ }
413
+
414
+ let v = sig.v;
415
+
416
+ // Add the signature
417
+ fields.push(toBeArray(v));
418
+ fields.push(toBeArray(sig.r));
419
+ fields.push(toBeArray(sig.s));
420
+
421
+ return encodeRlp(fields);
422
+ }
423
+
424
+ function _parseEipSignature(tx: TransactionLike, fields: Array<string>): void {
425
+ const r = fields[1];
426
+ const s = fields[2]
427
+
428
+ const signature = Signature.from({ r, s });
429
+ tx.signature = signature;
430
+ }
431
+
432
+ function _parseEip1559(data: Uint8Array): TransactionLike {
433
+ const fields: any = decodeRlp(getBytes(data).slice(1));
434
+
435
+ assertArgument(Array.isArray(fields) && (fields.length === 9 || fields.length === 12),
436
+ "invalid field count for transaction type: 2", "data", hexlify(data));
437
+
438
+ const tx: TransactionLike = {
439
+ type: 2,
440
+ chainId: handleUint(fields[0], "chainId"),
441
+ nonce: handleNumber(fields[1], "nonce"),
442
+ maxPriorityFeePerGas: handleUint(fields[2], "maxPriorityFeePerGas"),
443
+ maxFeePerGas: handleUint(fields[3], "maxFeePerGas"),
444
+ gasPrice: null,
445
+ gasLimit: handleUint(fields[4], "gasLimit"),
446
+ to: handleAddress(fields[5]),
447
+ value: handleUint(fields[6], "value"),
448
+ data: hexlify(fields[7]),
449
+ accessList: handleAccessList(fields[8], "accessList"),
450
+ };
451
+
452
+ // Unsigned EIP-1559 Transaction
453
+ if (fields.length === 9) { return tx; }
454
+
455
+ //tx.hash = keccak256(data);
456
+
457
+ _parseEipSignature(tx, fields.slice(9));
458
+
459
+ return tx;
460
+ }
461
+
462
+ function _serializeEip1559(tx: Transaction, sig: null | Signature): string {
463
+ const fields: Array<any> = [
464
+ formatNumber(tx.chainId, "chainId"),
465
+ formatNumber(tx.nonce, "nonce"),
466
+ formatNumber(tx.maxPriorityFeePerGas || 0, "maxPriorityFeePerGas"),
467
+ formatNumber(tx.maxFeePerGas || 0, "maxFeePerGas"),
468
+ formatNumber(tx.gasLimit, "gasLimit"),
469
+ (tx.to || "0x"),
470
+ formatNumber(tx.value, "value"),
471
+ tx.data,
472
+ formatAccessList(tx.accessList || [ ])
473
+ ];
474
+
475
+ if (sig) {
476
+ fields.push(toBeArray(sig.r));
477
+ fields.push(toBeArray(sig.s));
478
+ }
479
+
480
+ return concat([ "0x02", encodeRlp(fields)]);
481
+ }
482
+
483
+ function _parseEip2930(data: Uint8Array): TransactionLike {
484
+ const fields: any = decodeRlp(getBytes(data).slice(1));
485
+
486
+ assertArgument(Array.isArray(fields) && (fields.length === 8 || fields.length === 11),
487
+ "invalid field count for transaction type: 1", "data", hexlify(data));
488
+
489
+ const tx: TransactionLike = {
490
+ type: 1,
491
+ chainId: handleUint(fields[0], "chainId"),
492
+ nonce: handleNumber(fields[1], "nonce"),
493
+ gasPrice: handleUint(fields[2], "gasPrice"),
494
+ gasLimit: handleUint(fields[3], "gasLimit"),
495
+ to: handleAddress(fields[4]),
496
+ value: handleUint(fields[5], "value"),
497
+ data: hexlify(fields[6]),
498
+ accessList: handleAccessList(fields[7], "accessList")
499
+ };
500
+
501
+ // Unsigned EIP-2930 Transaction
502
+ if (fields.length === 8) { return tx; }
503
+
504
+ //tx.hash = keccak256(data);
505
+
506
+ _parseEipSignature(tx, fields.slice(8));
507
+
508
+ return tx;
509
+ }
510
+
511
+ function _serializeEip2930(tx: Transaction, sig: null | Signature): string {
512
+ const fields: any = [
513
+ formatNumber(tx.chainId, "chainId"),
514
+ formatNumber(tx.nonce, "nonce"),
515
+ formatNumber(tx.gasPrice || 0, "gasPrice"),
516
+ formatNumber(tx.gasLimit, "gasLimit"),
517
+ (tx.to || "0x"),
518
+ formatNumber(tx.value, "value"),
519
+ tx.data,
520
+ formatAccessList(tx.accessList || [ ])
521
+ ];
522
+
523
+ if (sig) {
524
+ fields.push(toBeArray(sig.r));
525
+ fields.push(toBeArray(sig.s));
526
+ }
527
+
528
+ return concat([ "0x01", encodeRlp(fields)]);
529
+ }
530
+
531
+ function _parseEip4844(data: Uint8Array): TransactionLike {
532
+ let fields: any = decodeRlp(getBytes(data).slice(1));
533
+
534
+ let typeName = "3";
535
+
536
+ let blobs: null | Array<Blob> = null;
537
+
538
+ // Parse the network format
539
+ if (fields.length === 4 && Array.isArray(fields[0])) {
540
+ typeName = "3 (network format)";
541
+ const fBlobs = fields[1], fCommits = fields[2], fProofs = fields[3];
542
+ assertArgument(Array.isArray(fBlobs), "invalid network format: blobs not an array", "fields[1]", fBlobs);
543
+ assertArgument(Array.isArray(fCommits), "invalid network format: commitments not an array", "fields[2]", fCommits);
544
+ assertArgument(Array.isArray(fProofs), "invalid network format: proofs not an array", "fields[3]", fProofs);
545
+ assertArgument(fBlobs.length === fCommits.length, "invalid network format: blobs/commitments length mismatch", "fields", fields);
546
+ assertArgument(fBlobs.length === fProofs.length, "invalid network format: blobs/proofs length mismatch", "fields", fields);
547
+
548
+ blobs = [ ];
549
+ for (let i = 0; i < fields[1].length; i++) {
550
+ blobs.push({
551
+ data: fBlobs[i],
552
+ commitment: fCommits[i],
553
+ proof: fProofs[i],
554
+ });
555
+ }
556
+
557
+ fields = fields[0];
558
+ }
559
+
560
+ assertArgument(Array.isArray(fields) && (fields.length === 11 || fields.length === 14),
561
+ `invalid field count for transaction type: ${ typeName }`, "data", hexlify(data));
562
+
563
+ const tx: TransactionLike = {
564
+ type: 3,
565
+ chainId: handleUint(fields[0], "chainId"),
566
+ nonce: handleNumber(fields[1], "nonce"),
567
+ maxPriorityFeePerGas: handleUint(fields[2], "maxPriorityFeePerGas"),
568
+ maxFeePerGas: handleUint(fields[3], "maxFeePerGas"),
569
+ gasPrice: null,
570
+ gasLimit: handleUint(fields[4], "gasLimit"),
571
+ to: handleAddress(fields[5]),
572
+ value: handleUint(fields[6], "value"),
573
+ data: hexlify(fields[7]),
574
+ accessList: handleAccessList(fields[8], "accessList"),
575
+ maxFeePerBlobGas: handleUint(fields[9], "maxFeePerBlobGas"),
576
+ blobVersionedHashes: fields[10]
577
+ };
578
+
579
+ if (blobs) { tx.blobs = blobs; }
580
+
581
+ assertArgument(tx.to != null, `invalid address for transaction type: ${ typeName }`, "data", data);
582
+
583
+ assertArgument(Array.isArray(tx.blobVersionedHashes), "invalid blobVersionedHashes: must be an array", "data", data);
584
+ for (let i = 0; i < tx.blobVersionedHashes.length; i++) {
585
+ assertArgument(isHexString(tx.blobVersionedHashes[i], 32), `invalid blobVersionedHash at index ${ i }: must be length 32`, "data", data);
586
+ }
587
+
588
+ // Unsigned EIP-4844 Transaction
589
+ if (fields.length === 11) { return tx; }
590
+
591
+ // @TODO: Do we need to do this? This is only called internally
592
+ // and used to verify hashes; it might save time to not do this
593
+ //tx.hash = keccak256(concat([ "0x03", encodeRlp(fields) ]));
594
+
595
+ _parseEipSignature(tx, fields.slice(11));
596
+
597
+ return tx;
598
+ }
599
+
600
+ function _serializeEip4844(tx: Transaction, sig: null | Signature, blobs: null | Array<Blob>): string {
601
+ const fields: Array<any> = [
602
+ formatNumber(tx.chainId, "chainId"),
603
+ formatNumber(tx.nonce, "nonce"),
604
+ formatNumber(tx.maxPriorityFeePerGas || 0, "maxPriorityFeePerGas"),
605
+ formatNumber(tx.maxFeePerGas || 0, "maxFeePerGas"),
606
+ formatNumber(tx.gasLimit, "gasLimit"),
607
+ (tx.to || ZeroAddress),
608
+ formatNumber(tx.value, "value"),
609
+ tx.data,
610
+ formatAccessList(tx.accessList || [ ]),
611
+ formatNumber(tx.maxFeePerBlobGas || 0, "maxFeePerBlobGas"),
612
+ formatHashes(tx.blobVersionedHashes || [ ], "blobVersionedHashes")
613
+ ];
614
+
615
+ if (sig) {
616
+ fields.push(toBeArray(sig.r));
617
+ fields.push(toBeArray(sig.s));
618
+
619
+ // We have blobs; return the network wrapped format
620
+ if (blobs) {
621
+ return concat([
622
+ "0x03",
623
+ encodeRlp([
624
+ fields,
625
+ blobs.map((b) => b.data),
626
+ blobs.map((b) => b.commitment),
627
+ blobs.map((b) => b.proof),
628
+ ])
629
+ ]);
630
+ }
631
+
632
+ }
633
+
634
+ return concat([ "0x03", encodeRlp(fields)]);
635
+ }
636
+
637
+ function _parseEip7702(data: Uint8Array): TransactionLike {
638
+ const fields: any = decodeRlp(getBytes(data).slice(1));
639
+
640
+ assertArgument(Array.isArray(fields) && (fields.length === 10 || fields.length === 13),
641
+ "invalid field count for transaction type: 4", "data", hexlify(data));
642
+
643
+ const tx: TransactionLike = {
644
+ type: 4,
645
+ chainId: handleUint(fields[0], "chainId"),
646
+ nonce: handleNumber(fields[1], "nonce"),
647
+ maxPriorityFeePerGas: handleUint(fields[2], "maxPriorityFeePerGas"),
648
+ maxFeePerGas: handleUint(fields[3], "maxFeePerGas"),
649
+ gasPrice: null,
650
+ gasLimit: handleUint(fields[4], "gasLimit"),
651
+ to: handleAddress(fields[5]),
652
+ value: handleUint(fields[6], "value"),
653
+ data: hexlify(fields[7]),
654
+ accessList: handleAccessList(fields[8], "accessList"),
655
+ authorizationList: handleAuthorizationList(fields[9], "authorizationList"),
656
+ };
657
+
658
+ // Unsigned EIP-7702 Transaction
659
+ if (fields.length === 10) { return tx; }
660
+
661
+ _parseEipSignature(tx, fields.slice(10));
662
+
663
+ return tx;
664
+ }
665
+
666
+ function _serializeEip7702(tx: Transaction, sig: null | Signature): string {
667
+ const fields: Array<any> = [
668
+ formatNumber(tx.chainId, "chainId"),
669
+ formatNumber(tx.nonce, "nonce"),
670
+ formatNumber(tx.maxPriorityFeePerGas || 0, "maxPriorityFeePerGas"),
671
+ formatNumber(tx.maxFeePerGas || 0, "maxFeePerGas"),
672
+ formatNumber(tx.gasLimit, "gasLimit"),
673
+ (tx.to || "0x"),
674
+ formatNumber(tx.value, "value"),
675
+ tx.data,
676
+ formatAccessList(tx.accessList || [ ]),
677
+ formatAuthorizationList(tx.authorizationList || [ ])
678
+ ];
679
+
680
+ if (sig) {
681
+ fields.push(toBeArray(sig.r));
682
+ fields.push(toBeArray(sig.s));
683
+ }
684
+
685
+ return concat([ "0x04", encodeRlp(fields)]);
686
+ }
687
+
688
+ /**
689
+ * A **Transaction** describes an operation to be executed on
690
+ * Ethereum by an Externally Owned Account (EOA). It includes
691
+ * who (the [[to]] address), what (the [[data]]) and how much (the
692
+ * [[value]] in ether) the operation should entail.
693
+ *
694
+ * @example:
695
+ * tx = new Transaction()
696
+ * //_result:
697
+ *
698
+ * tx.data = "0x1234";
699
+ * //_result:
700
+ */
701
+ export class Transaction implements TransactionLike<string> {
702
+ #type: null | number;
703
+ #to: null | string;
704
+ #data: string;
705
+ #nonce: number;
706
+ #gasLimit: bigint;
707
+ #gasPrice: null | bigint;
708
+ #maxPriorityFeePerGas: null | bigint;
709
+ #maxFeePerGas: null | bigint;
710
+ #value: bigint;
711
+ #chainId: bigint;
712
+ #sig: null | Signature;
713
+ #accessList: null | AccessList;
714
+ #maxFeePerBlobGas: null | bigint;
715
+ #blobVersionedHashes: null | Array<string>;
716
+ #kzg: null | KzgLibrary;
717
+ #blobs: null | Array<Blob>;
718
+ #auths: null | Array<Authorization>;
719
+
720
+ /**
721
+ * The transaction type.
722
+ *
723
+ * If null, the type will be automatically inferred based on
724
+ * explicit properties.
725
+ */
726
+ get type(): null | number { return this.#type; }
727
+ set type(value: null | number | string) {
728
+ switch (value) {
729
+ case null:
730
+ this.#type = null;
731
+ break;
732
+ case 0: case "legacy":
733
+ this.#type = 0;
734
+ break;
735
+ case 1: case "berlin": case "eip-2930":
736
+ this.#type = 1;
737
+ break;
738
+ case 2: case "london": case "eip-1559":
739
+ this.#type = 2;
740
+ break;
741
+ case 3: case "cancun": case "eip-4844":
742
+ this.#type = 3;
743
+ break;
744
+ case 4: case "pectra": case "eip-7702":
745
+ this.#type = 4;
746
+ break;
747
+ default:
748
+ assertArgument(false, "unsupported transaction type", "type", value);
749
+ }
750
+ }
751
+
752
+ /**
753
+ * The name of the transaction type.
754
+ */
755
+ get typeName(): null | string {
756
+ switch (this.type) {
757
+ case 0: return "legacy";
758
+ case 1: return "eip-2930";
759
+ case 2: return "eip-1559";
760
+ case 3: return "eip-4844";
761
+ case 4: return "eip-7702";
762
+ }
763
+
764
+ return null;
765
+ }
766
+
767
+ /**
768
+ * The ``to`` address for the transaction or ``null`` if the
769
+ * transaction is an ``init`` transaction.
770
+ */
771
+ get to(): null | string {
772
+ const value = this.#to;
773
+ if (value == null && this.type === 3) { return ZeroAddress; }
774
+ return value;
775
+ }
776
+ set to(value: null | string) {
777
+ this.#to = (value == null) ? null: getAddress(value);
778
+ }
779
+
780
+ /**
781
+ * The transaction nonce.
782
+ */
783
+ get nonce(): number { return this.#nonce; }
784
+ set nonce(value: BigNumberish) { this.#nonce = getNumber(value, "value"); }
785
+
786
+ /**
787
+ * The gas limit.
788
+ */
789
+ get gasLimit(): bigint { return this.#gasLimit; }
790
+ set gasLimit(value: BigNumberish) { this.#gasLimit = getBigInt(value); }
791
+
792
+ /**
793
+ * The gas price.
794
+ *
795
+ * On legacy networks this defines the fee that will be paid. On
796
+ * EIP-1559 networks, this should be ``null``.
797
+ */
798
+ get gasPrice(): null | bigint {
799
+ const value = this.#gasPrice;
800
+ if (value == null && (this.type === 0 || this.type === 1)) { return BN_0; }
801
+ return value;
802
+ }
803
+ set gasPrice(value: null | BigNumberish) {
804
+ this.#gasPrice = (value == null) ? null: getBigInt(value, "gasPrice");
805
+ }
806
+
807
+ /**
808
+ * The maximum priority fee per unit of gas to pay. On legacy
809
+ * networks this should be ``null``.
810
+ */
811
+ get maxPriorityFeePerGas(): null | bigint {
812
+ const value = this.#maxPriorityFeePerGas;
813
+ if (value == null) {
814
+ if (this.type === 2 || this.type === 3) { return BN_0; }
815
+ return null;
816
+ }
817
+ return value;
818
+ }
819
+ set maxPriorityFeePerGas(value: null | BigNumberish) {
820
+ this.#maxPriorityFeePerGas = (value == null) ? null: getBigInt(value, "maxPriorityFeePerGas");
821
+ }
822
+
823
+ /**
824
+ * The maximum total fee per unit of gas to pay. On legacy
825
+ * networks this should be ``null``.
826
+ */
827
+ get maxFeePerGas(): null | bigint {
828
+ const value = this.#maxFeePerGas;
829
+ if (value == null) {
830
+ if (this.type === 2 || this.type === 3) { return BN_0; }
831
+ return null;
832
+ }
833
+ return value;
834
+ }
835
+ set maxFeePerGas(value: null | BigNumberish) {
836
+ this.#maxFeePerGas = (value == null) ? null: getBigInt(value, "maxFeePerGas");
837
+ }
838
+
839
+ /**
840
+ * The transaction data. For ``init`` transactions this is the
841
+ * deployment code.
842
+ */
843
+ get data(): string { return this.#data; }
844
+ set data(value: BytesLike) { this.#data = hexlify(value); }
845
+
846
+ /**
847
+ * The amount of ether (in wei) to send in this transactions.
848
+ */
849
+ get value(): bigint { return this.#value; }
850
+ set value(value: BigNumberish) {
851
+ this.#value = getBigInt(value, "value");
852
+ }
853
+
854
+ /**
855
+ * The chain ID this transaction is valid on.
856
+ */
857
+ get chainId(): bigint { return this.#chainId; }
858
+ set chainId(value: BigNumberish) { this.#chainId = getBigInt(value); }
859
+
860
+ /**
861
+ * If signed, the signature for this transaction.
862
+ */
863
+ get signature(): null | Signature { return this.#sig || null; }
864
+ set signature(value: null | SignatureLike) {
865
+ this.#sig = (value == null) ? null: Signature.from(value);
866
+ }
867
+
868
+ /**
869
+ * The access list.
870
+ *
871
+ * An access list permits discounted (but pre-paid) access to
872
+ * bytecode and state variable access within contract execution.
873
+ */
874
+ get accessList(): null | AccessList {
875
+ const value = this.#accessList || null;
876
+ if (value == null) {
877
+ if (this.type === 1 || this.type === 2 || this.type === 3) {
878
+ // @TODO: in v7, this should assign the value or become
879
+ // a live object itself, otherwise mutation is inconsistent
880
+ return [ ];
881
+ }
882
+ return null;
883
+ }
884
+ return value;
885
+ }
886
+ set accessList(value: null | AccessListish) {
887
+ this.#accessList = (value == null) ? null: accessListify(value);
888
+ }
889
+
890
+ get authorizationList(): null | Array<Authorization> {
891
+ const value = this.#auths || null;
892
+ if (value == null) {
893
+ if (this.type === 4) {
894
+ // @TODO: in v7, this should become a live object itself,
895
+ // otherwise mutation is inconsistent
896
+ return [ ];
897
+ }
898
+ }
899
+ return value;
900
+ }
901
+ set authorizationList(auths: null | Array<AuthorizationLike>) {
902
+ this.#auths = (auths == null) ? null: auths.map((a) =>
903
+ authorizationify(a));
904
+ }
905
+
906
+ /**
907
+ * The max fee per blob gas for Cancun transactions.
908
+ */
909
+ get maxFeePerBlobGas(): null | bigint {
910
+ const value = this.#maxFeePerBlobGas;
911
+ if (value == null && this.type === 3) { return BN_0; }
912
+ return value;
913
+ }
914
+ set maxFeePerBlobGas(value: null | BigNumberish) {
915
+ this.#maxFeePerBlobGas = (value == null) ? null: getBigInt(value, "maxFeePerBlobGas");
916
+ }
917
+
918
+ /**
919
+ * The BLOb versioned hashes for Cancun transactions.
920
+ */
921
+ get blobVersionedHashes(): null | Array<string> {
922
+ // @TODO: Mutation is inconsistent; if unset, the returned value
923
+ // cannot mutate the object, if set it can
924
+ let value = this.#blobVersionedHashes;
925
+ if (value == null && this.type === 3) { return [ ]; }
926
+ return value;
927
+ }
928
+ set blobVersionedHashes(value: null | Array<string>) {
929
+ if (value != null) {
930
+ assertArgument(Array.isArray(value), "blobVersionedHashes must be an Array", "value", value);
931
+ value = value.slice();
932
+ for (let i = 0; i < value.length; i++) {
933
+ assertArgument(isHexString(value[i], 32), "invalid blobVersionedHash", `value[${ i }]`, value[i]);
934
+ }
935
+ }
936
+ this.#blobVersionedHashes = value;
937
+ }
938
+
939
+ /**
940
+ * The BLObs for the Transaction, if any.
941
+ *
942
+ * If ``blobs`` is non-``null``, then the [[seriailized]]
943
+ * will return the network formatted sidecar, otherwise it
944
+ * will return the standard [[link-eip-2718]] payload. The
945
+ * [[unsignedSerialized]] is unaffected regardless.
946
+ *
947
+ * When setting ``blobs``, either fully valid [[Blob]] objects
948
+ * may be specified (i.e. correctly padded, with correct
949
+ * committments and proofs) or a raw [[BytesLike]] may
950
+ * be provided.
951
+ *
952
+ * If raw [[BytesLike]] are provided, the [[kzg]] property **must**
953
+ * be already set. The blob will be correctly padded and the
954
+ * [[KzgLibrary]] will be used to compute the committment and
955
+ * proof for the blob.
956
+ *
957
+ * A BLOb is a sequence of field elements, each of which must
958
+ * be within the BLS field modulo, so some additional processing
959
+ * may be required to encode arbitrary data to ensure each 32 byte
960
+ * field is within the valid range.
961
+ *
962
+ * Setting this automatically populates [[blobVersionedHashes]],
963
+ * overwriting any existing values. Setting this to ``null``
964
+ * does **not** remove the [[blobVersionedHashes]], leaving them
965
+ * present.
966
+ */
967
+ get blobs(): null | Array<Blob> {
968
+ if (this.#blobs == null) { return null; }
969
+ return this.#blobs.map((b) => Object.assign({ }, b));
970
+ }
971
+ set blobs(_blobs: null | Array<BlobLike>) {
972
+ if (_blobs == null) {
973
+ this.#blobs = null;
974
+ return;
975
+ }
976
+
977
+ const blobs: Array<Blob> = [ ];
978
+ const versionedHashes: Array<string> = [ ];
979
+ for (let i = 0; i < _blobs.length; i++) {
980
+ const blob = _blobs[i];
981
+
982
+ if (isBytesLike(blob)) {
983
+ assert(this.#kzg, "adding a raw blob requires a KZG library", "UNSUPPORTED_OPERATION", {
984
+ operation: "set blobs()"
985
+ });
986
+
987
+ let data = getBytes(blob);
988
+ assertArgument(data.length <= BLOB_SIZE, "blob is too large", `blobs[${ i }]`, blob);
989
+
990
+ // Pad blob if necessary
991
+ if (data.length !== BLOB_SIZE) {
992
+ const padded = new Uint8Array(BLOB_SIZE);
993
+ padded.set(data);
994
+ data = padded;
995
+ }
996
+
997
+ const commit = this.#kzg.blobToKzgCommitment(data);
998
+ const proof = hexlify(this.#kzg.computeBlobKzgProof(data, commit));
999
+
1000
+ blobs.push({
1001
+ data: hexlify(data),
1002
+ commitment: hexlify(commit),
1003
+ proof
1004
+ });
1005
+ versionedHashes.push(getVersionedHash(1, commit));
1006
+
1007
+ } else {
1008
+ const commit = hexlify(blob.commitment);
1009
+ blobs.push({
1010
+ data: hexlify(blob.data),
1011
+ commitment: commit,
1012
+ proof: hexlify(blob.proof)
1013
+ });
1014
+ versionedHashes.push(getVersionedHash(1, commit));
1015
+ }
1016
+ }
1017
+
1018
+ this.#blobs = blobs;
1019
+ this.#blobVersionedHashes = versionedHashes;
1020
+ }
1021
+
1022
+ get kzg(): null | KzgLibrary { return this.#kzg; }
1023
+ set kzg(kzg: null | KzgLibraryLike) {
1024
+ if (kzg == null) {
1025
+ this.#kzg = null;
1026
+ } else {
1027
+ this.#kzg = getKzgLibrary(kzg);
1028
+ }
1029
+ }
1030
+
1031
+ /**
1032
+ * Creates a new Transaction with default values.
1033
+ */
1034
+ constructor() {
1035
+ this.#type = null;
1036
+ this.#to = null;
1037
+ this.#nonce = 0;
1038
+ this.#gasLimit = BN_0;
1039
+ this.#gasPrice = null;
1040
+ this.#maxPriorityFeePerGas = null;
1041
+ this.#maxFeePerGas = null;
1042
+ this.#data = "0x";
1043
+ this.#value = BN_0;
1044
+ this.#chainId = BN_0;
1045
+ this.#sig = null;
1046
+ this.#accessList = null;
1047
+ this.#maxFeePerBlobGas = null;
1048
+ this.#blobVersionedHashes = null;
1049
+ this.#kzg = null;
1050
+ this.#blobs = null;
1051
+ this.#auths = null;
1052
+ }
1053
+
1054
+ /**
1055
+ * The transaction hash, if signed. Otherwise, ``null``.
1056
+ */
1057
+ get hash(): null | string {
1058
+ if (this.signature == null) { return null; }
1059
+ return keccak256(this.#getSerialized(true, false));
1060
+ }
1061
+
1062
+ /**
1063
+ * The pre-image hash of this transaction.
1064
+ *
1065
+ * This is the digest that a [[Signer]] must sign to authorize
1066
+ * this transaction.
1067
+ */
1068
+ get unsignedHash(): string {
1069
+ return keccak256(this.unsignedSerialized);
1070
+ }
1071
+
1072
+ /**
1073
+ * The sending address, if signed. Otherwise, ``null``.
1074
+ */
1075
+ get from(): null | string {
1076
+ if (this.signature == null) { return null; }
1077
+ return recoverAddress(this.unsignedHash, this.signature);
1078
+ }
1079
+
1080
+ /**
1081
+ * The public key of the sender, if signed. Otherwise, ``null``.
1082
+ */
1083
+ get fromPublicKey(): null | string {
1084
+ if (this.signature == null) { return null; }
1085
+ return SigningKey.recoverPublicKey(this.unsignedHash, this.signature);
1086
+ }
1087
+
1088
+ /**
1089
+ * Returns true if signed.
1090
+ *
1091
+ * This provides a Type Guard that properties requiring a signed
1092
+ * transaction are non-null.
1093
+ */
1094
+ isSigned(): this is (Transaction & { type: number, typeName: string, from: string, signature: Signature }) {
1095
+ return this.signature != null;
1096
+ }
1097
+
1098
+ #getSerialized(signed: boolean, sidecar: boolean): string {
1099
+ assert(!signed || this.signature != null, "cannot serialize unsigned transaction; maybe you meant .unsignedSerialized", "UNSUPPORTED_OPERATION", { operation: ".serialized"});
1100
+
1101
+ const sig = signed ? this.signature: null;
1102
+ switch (this.inferType()) {
1103
+ case 0:
1104
+ return _serializeLegacy(this, sig);
1105
+ case 1:
1106
+ return _serializeEip2930(this, sig);
1107
+ case 2:
1108
+ return _serializeEip1559(this, sig);
1109
+ case 3:
1110
+ return _serializeEip4844(this, sig, sidecar ? this.blobs: null);
1111
+ case 4:
1112
+ return _serializeEip7702(this, sig);
1113
+ }
1114
+
1115
+ assert(false, "unsupported transaction type", "UNSUPPORTED_OPERATION", { operation: ".serialized" });
1116
+ }
1117
+
1118
+ /**
1119
+ * The serialized transaction.
1120
+ *
1121
+ * This throws if the transaction is unsigned. For the pre-image,
1122
+ * use [[unsignedSerialized]].
1123
+ */
1124
+ get serialized(): string {
1125
+ return this.#getSerialized(true, true);
1126
+ }
1127
+
1128
+ /**
1129
+ * The transaction pre-image.
1130
+ *
1131
+ * The hash of this is the digest which needs to be signed to
1132
+ * authorize this transaction.
1133
+ */
1134
+ get unsignedSerialized(): string {
1135
+ return this.#getSerialized(false, false);
1136
+ }
1137
+
1138
+ /**
1139
+ * Return the most "likely" type; currently the highest
1140
+ * supported transaction type.
1141
+ */
1142
+ inferType(): number {
1143
+ const types = this.inferTypes();
1144
+
1145
+ // Prefer London (EIP-1559) over Cancun (BLOb)
1146
+ if (types.indexOf(2) >= 0) { return 2; }
1147
+
1148
+ // Return the highest inferred type
1149
+ return <number>(types.pop());
1150
+ }
1151
+
1152
+ /**
1153
+ * Validates the explicit properties and returns a list of compatible
1154
+ * transaction types.
1155
+ */
1156
+ inferTypes(): Array<number> {
1157
+
1158
+ // Checks that there are no conflicting properties set
1159
+ const hasGasPrice = this.gasPrice != null;
1160
+ const hasFee = (this.maxFeePerGas != null || this.maxPriorityFeePerGas != null);
1161
+ const hasAccessList = (this.accessList != null);
1162
+ const hasBlob = (this.#maxFeePerBlobGas != null || this.#blobVersionedHashes);
1163
+
1164
+ //if (hasGasPrice && hasFee) {
1165
+ // throw new Error("transaction cannot have gasPrice and maxFeePerGas");
1166
+ //}
1167
+
1168
+ if (this.maxFeePerGas != null && this.maxPriorityFeePerGas != null) {
1169
+ assert(this.maxFeePerGas >= this.maxPriorityFeePerGas, "priorityFee cannot be more than maxFee", "BAD_DATA", { value: this });
1170
+ }
1171
+
1172
+ //if (this.type === 2 && hasGasPrice) {
1173
+ // throw new Error("eip-1559 transaction cannot have gasPrice");
1174
+ //}
1175
+
1176
+ assert(!hasFee || (this.type !== 0 && this.type !== 1), "transaction type cannot have maxFeePerGas or maxPriorityFeePerGas", "BAD_DATA", { value: this });
1177
+ assert(this.type !== 0 || !hasAccessList, "legacy transaction cannot have accessList", "BAD_DATA", { value: this })
1178
+
1179
+ const types: Array<number> = [ ];
1180
+
1181
+ // Explicit type
1182
+ if (this.type != null) {
1183
+ types.push(this.type);
1184
+
1185
+ } else {
1186
+ if (this.authorizationList && this.authorizationList.length) {
1187
+ types.push(4);
1188
+ } else if (hasFee) {
1189
+ types.push(2);
1190
+ } else if (hasGasPrice) {
1191
+ types.push(1);
1192
+ if (!hasAccessList) { types.push(0); }
1193
+ } else if (hasAccessList) {
1194
+ types.push(1);
1195
+ types.push(2);
1196
+ } else if (hasBlob && this.to) {
1197
+ types.push(3);
1198
+ } else {
1199
+ types.push(0);
1200
+ types.push(1);
1201
+ types.push(2);
1202
+ types.push(3);
1203
+ }
1204
+ }
1205
+
1206
+ types.sort();
1207
+
1208
+ return types;
1209
+ }
1210
+
1211
+ /**
1212
+ * Returns true if this transaction is a legacy transaction (i.e.
1213
+ * ``type === 0``).
1214
+ *
1215
+ * This provides a Type Guard that the related properties are
1216
+ * non-null.
1217
+ */
1218
+ isLegacy(): this is (Transaction & { type: 0, gasPrice: bigint }) {
1219
+ return (this.type === 0);
1220
+ }
1221
+
1222
+ /**
1223
+ * Returns true if this transaction is berlin hardform transaction (i.e.
1224
+ * ``type === 1``).
1225
+ *
1226
+ * This provides a Type Guard that the related properties are
1227
+ * non-null.
1228
+ */
1229
+ isBerlin(): this is (Transaction & { type: 1, gasPrice: bigint, accessList: AccessList }) {
1230
+ return (this.type === 1);
1231
+ }
1232
+
1233
+ /**
1234
+ * Returns true if this transaction is london hardform transaction (i.e.
1235
+ * ``type === 2``).
1236
+ *
1237
+ * This provides a Type Guard that the related properties are
1238
+ * non-null.
1239
+ */
1240
+ isLondon(): this is (Transaction & { type: 2, accessList: AccessList, maxFeePerGas: bigint, maxPriorityFeePerGas: bigint }) {
1241
+ return (this.type === 2);
1242
+ }
1243
+
1244
+ /**
1245
+ * Returns true if this transaction is an [[link-eip-4844]] BLOB
1246
+ * transaction.
1247
+ *
1248
+ * This provides a Type Guard that the related properties are
1249
+ * non-null.
1250
+ */
1251
+ isCancun(): this is (Transaction & { type: 3, to: string, accessList: AccessList, maxFeePerGas: bigint, maxPriorityFeePerGas: bigint, maxFeePerBlobGas: bigint, blobVersionedHashes: Array<string> }) {
1252
+ return (this.type === 3);
1253
+ }
1254
+
1255
+ /**
1256
+ * Create a copy of this transaciton.
1257
+ */
1258
+ clone(): Transaction {
1259
+ return Transaction.from(this);
1260
+ }
1261
+
1262
+ /**
1263
+ * Return a JSON-friendly object.
1264
+ */
1265
+ toJSON(): any {
1266
+ const s = (v: null | bigint) => {
1267
+ if (v == null) { return null; }
1268
+ return v.toString();
1269
+ };
1270
+
1271
+ return {
1272
+ type: this.type,
1273
+ to: this.to,
1274
+ // from: this.from,
1275
+ data: this.data,
1276
+ nonce: this.nonce,
1277
+ gasLimit: s(this.gasLimit),
1278
+ gasPrice: s(this.gasPrice),
1279
+ maxPriorityFeePerGas: s(this.maxPriorityFeePerGas),
1280
+ maxFeePerGas: s(this.maxFeePerGas),
1281
+ value: s(this.value),
1282
+ chainId: s(this.chainId),
1283
+ sig: this.signature ? this.signature.toJSON(): null,
1284
+ accessList: this.accessList
1285
+ };
1286
+ }
1287
+
1288
+ /**
1289
+ * Create a **Transaction** from a serialized transaction or a
1290
+ * Transaction-like object.
1291
+ */
1292
+ static from(tx?: string | TransactionLike<string>): Transaction {
1293
+ if (tx == null) { return new Transaction(); }
1294
+
1295
+ if (typeof(tx) === "string") {
1296
+ const payload = getBytes(tx);
1297
+
1298
+ if (payload[0] >= 0x7f) { // @TODO: > vs >= ??
1299
+ return Transaction.from(_parseLegacy(payload));
1300
+ }
1301
+
1302
+ switch(payload[0]) {
1303
+ case 1: return Transaction.from(_parseEip2930(payload));
1304
+ case 2: return Transaction.from(_parseEip1559(payload));
1305
+ case 3: return Transaction.from(_parseEip4844(payload));
1306
+ case 4: return Transaction.from(_parseEip7702(payload));
1307
+ }
1308
+ assert(false, "unsupported transaction type", "UNSUPPORTED_OPERATION", { operation: "from" });
1309
+ }
1310
+
1311
+ const result = new Transaction();
1312
+ if (tx.type != null) { result.type = tx.type; }
1313
+ if (tx.to != null) { result.to = tx.to; }
1314
+ if (tx.nonce != null) { result.nonce = tx.nonce; }
1315
+ if (tx.gasLimit != null) { result.gasLimit = tx.gasLimit; }
1316
+ if (tx.gasPrice != null) { result.gasPrice = tx.gasPrice; }
1317
+ if (tx.maxPriorityFeePerGas != null) { result.maxPriorityFeePerGas = tx.maxPriorityFeePerGas; }
1318
+ if (tx.maxFeePerGas != null) { result.maxFeePerGas = tx.maxFeePerGas; }
1319
+ if (tx.maxFeePerBlobGas != null) { result.maxFeePerBlobGas = tx.maxFeePerBlobGas; }
1320
+ if (tx.data != null) { result.data = tx.data; }
1321
+ if (tx.value != null) { result.value = tx.value; }
1322
+ if (tx.chainId != null) { result.chainId = tx.chainId; }
1323
+ if (tx.signature != null) { result.signature = Signature.from(tx.signature); }
1324
+ if (tx.accessList != null) { result.accessList = tx.accessList; }
1325
+ if (tx.authorizationList != null) {
1326
+ result.authorizationList = tx.authorizationList;
1327
+ }
1328
+
1329
+ // This will get overwritten by blobs, if present
1330
+ if (tx.blobVersionedHashes != null) { result.blobVersionedHashes = tx.blobVersionedHashes; }
1331
+
1332
+ // Make sure we assign the kzg before assigning blobs, which
1333
+ // require the library in the event raw blob data is provided.
1334
+ if (tx.kzg != null) { result.kzg = tx.kzg; }
1335
+ if (tx.blobs != null) { result.blobs = tx.blobs; }
1336
+
1337
+ if (tx.hash != null) {
1338
+ assertArgument(result.isSigned(), "unsigned transaction cannot define '.hash'", "tx", tx);
1339
+ assertArgument(result.hash === tx.hash, "hash mismatch", "tx", tx);
1340
+ }
1341
+
1342
+ if (tx.from != null) {
1343
+ assertArgument(result.isSigned(), "unsigned transaction cannot define '.from'", "tx", tx);
1344
+ assertArgument(result.from.toLowerCase() === (tx.from || "").toLowerCase(), "from mismatch", "tx", tx);
1345
+ }
1346
+
1347
+ return result;
1348
+ }
1349
+ }