transaction.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Transaction = void 0;
  4. const index_js_1 = require("../address/index.js");
  5. const addresses_js_1 = require("../constants/addresses.js");
  6. const index_js_2 = require("../crypto/index.js");
  7. const index_js_3 = require("../utils/index.js");
  8. const accesslist_js_1 = require("./accesslist.js");
  9. const address_js_1 = require("./address.js");
  10. const BN_0 = BigInt(0);
  11. const BN_2 = BigInt(2);
  12. const BN_27 = BigInt(27);
  13. const BN_28 = BigInt(28);
  14. const BN_35 = BigInt(35);
  15. const BN_MAX_UINT = BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
  16. const BLOB_SIZE = 4096 * 32;
  17. function getVersionedHash(version, hash) {
  18. let versioned = version.toString(16);
  19. while (versioned.length < 2) {
  20. versioned = "0" + versioned;
  21. }
  22. versioned += (0, index_js_2.sha256)(hash).substring(4);
  23. return "0x" + versioned;
  24. }
  25. function handleAddress(value) {
  26. if (value === "0x") {
  27. return null;
  28. }
  29. return (0, index_js_1.getAddress)(value);
  30. }
  31. function handleAccessList(value, param) {
  32. try {
  33. return (0, accesslist_js_1.accessListify)(value);
  34. }
  35. catch (error) {
  36. (0, index_js_3.assertArgument)(false, error.message, param, value);
  37. }
  38. }
  39. function handleNumber(_value, param) {
  40. if (_value === "0x") {
  41. return 0;
  42. }
  43. return (0, index_js_3.getNumber)(_value, param);
  44. }
  45. function handleUint(_value, param) {
  46. if (_value === "0x") {
  47. return BN_0;
  48. }
  49. const value = (0, index_js_3.getBigInt)(_value, param);
  50. (0, index_js_3.assertArgument)(value <= BN_MAX_UINT, "value exceeds uint size", param, value);
  51. return value;
  52. }
  53. function formatNumber(_value, name) {
  54. const value = (0, index_js_3.getBigInt)(_value, "value");
  55. const result = (0, index_js_3.toBeArray)(value);
  56. (0, index_js_3.assertArgument)(result.length <= 32, `value too large`, `tx.${name}`, value);
  57. return result;
  58. }
  59. function formatAccessList(value) {
  60. return (0, accesslist_js_1.accessListify)(value).map((set) => [set.address, set.storageKeys]);
  61. }
  62. function formatHashes(value, param) {
  63. (0, index_js_3.assertArgument)(Array.isArray(value), `invalid ${param}`, "value", value);
  64. for (let i = 0; i < value.length; i++) {
  65. (0, index_js_3.assertArgument)((0, index_js_3.isHexString)(value[i], 32), "invalid ${ param } hash", `value[${i}]`, value[i]);
  66. }
  67. return value;
  68. }
  69. function _parseLegacy(data) {
  70. const fields = (0, index_js_3.decodeRlp)(data);
  71. (0, index_js_3.assertArgument)(Array.isArray(fields) && (fields.length === 9 || fields.length === 6), "invalid field count for legacy transaction", "data", data);
  72. const tx = {
  73. type: 0,
  74. nonce: handleNumber(fields[0], "nonce"),
  75. gasPrice: handleUint(fields[1], "gasPrice"),
  76. gasLimit: handleUint(fields[2], "gasLimit"),
  77. to: handleAddress(fields[3]),
  78. value: handleUint(fields[4], "value"),
  79. data: (0, index_js_3.hexlify)(fields[5]),
  80. chainId: BN_0
  81. };
  82. // Legacy unsigned transaction
  83. if (fields.length === 6) {
  84. return tx;
  85. }
  86. const v = handleUint(fields[6], "v");
  87. const r = handleUint(fields[7], "r");
  88. const s = handleUint(fields[8], "s");
  89. if (r === BN_0 && s === BN_0) {
  90. // EIP-155 unsigned transaction
  91. tx.chainId = v;
  92. }
  93. else {
  94. // Compute the EIP-155 chain ID (or 0 for legacy)
  95. let chainId = (v - BN_35) / BN_2;
  96. if (chainId < BN_0) {
  97. chainId = BN_0;
  98. }
  99. tx.chainId = chainId;
  100. // Signed Legacy Transaction
  101. (0, index_js_3.assertArgument)(chainId !== BN_0 || (v === BN_27 || v === BN_28), "non-canonical legacy v", "v", fields[6]);
  102. tx.signature = index_js_2.Signature.from({
  103. r: (0, index_js_3.zeroPadValue)(fields[7], 32),
  104. s: (0, index_js_3.zeroPadValue)(fields[8], 32),
  105. v
  106. });
  107. //tx.hash = keccak256(data);
  108. }
  109. return tx;
  110. }
  111. function _serializeLegacy(tx, sig) {
  112. const fields = [
  113. formatNumber(tx.nonce, "nonce"),
  114. formatNumber(tx.gasPrice || 0, "gasPrice"),
  115. formatNumber(tx.gasLimit, "gasLimit"),
  116. (tx.to || "0x"),
  117. formatNumber(tx.value, "value"),
  118. tx.data,
  119. ];
  120. let chainId = BN_0;
  121. if (tx.chainId != BN_0) {
  122. // A chainId was provided; if non-zero we'll use EIP-155
  123. chainId = (0, index_js_3.getBigInt)(tx.chainId, "tx.chainId");
  124. // We have a chainId in the tx and an EIP-155 v in the signature,
  125. // make sure they agree with each other
  126. (0, index_js_3.assertArgument)(!sig || sig.networkV == null || sig.legacyChainId === chainId, "tx.chainId/sig.v mismatch", "sig", sig);
  127. }
  128. else if (tx.signature) {
  129. // No explicit chainId, but EIP-155 have a derived implicit chainId
  130. const legacy = tx.signature.legacyChainId;
  131. if (legacy != null) {
  132. chainId = legacy;
  133. }
  134. }
  135. // Requesting an unsigned transaction
  136. if (!sig) {
  137. // We have an EIP-155 transaction (chainId was specified and non-zero)
  138. if (chainId !== BN_0) {
  139. fields.push((0, index_js_3.toBeArray)(chainId));
  140. fields.push("0x");
  141. fields.push("0x");
  142. }
  143. return (0, index_js_3.encodeRlp)(fields);
  144. }
  145. // @TODO: We should probably check that tx.signature, chainId, and sig
  146. // match but that logic could break existing code, so schedule
  147. // this for the next major bump.
  148. // Compute the EIP-155 v
  149. let v = BigInt(27 + sig.yParity);
  150. if (chainId !== BN_0) {
  151. v = index_js_2.Signature.getChainIdV(chainId, sig.v);
  152. }
  153. else if (BigInt(sig.v) !== v) {
  154. (0, index_js_3.assertArgument)(false, "tx.chainId/sig.v mismatch", "sig", sig);
  155. }
  156. // Add the signature
  157. fields.push((0, index_js_3.toBeArray)(v));
  158. fields.push((0, index_js_3.toBeArray)(sig.r));
  159. fields.push((0, index_js_3.toBeArray)(sig.s));
  160. return (0, index_js_3.encodeRlp)(fields);
  161. }
  162. function _parseEipSignature(tx, fields) {
  163. let yParity;
  164. try {
  165. yParity = handleNumber(fields[0], "yParity");
  166. if (yParity !== 0 && yParity !== 1) {
  167. throw new Error("bad yParity");
  168. }
  169. }
  170. catch (error) {
  171. (0, index_js_3.assertArgument)(false, "invalid yParity", "yParity", fields[0]);
  172. }
  173. const r = (0, index_js_3.zeroPadValue)(fields[1], 32);
  174. const s = (0, index_js_3.zeroPadValue)(fields[2], 32);
  175. const signature = index_js_2.Signature.from({ r, s, yParity });
  176. tx.signature = signature;
  177. }
  178. function _parseEip1559(data) {
  179. const fields = (0, index_js_3.decodeRlp)((0, index_js_3.getBytes)(data).slice(1));
  180. (0, index_js_3.assertArgument)(Array.isArray(fields) && (fields.length === 9 || fields.length === 12), "invalid field count for transaction type: 2", "data", (0, index_js_3.hexlify)(data));
  181. const tx = {
  182. type: 2,
  183. chainId: handleUint(fields[0], "chainId"),
  184. nonce: handleNumber(fields[1], "nonce"),
  185. maxPriorityFeePerGas: handleUint(fields[2], "maxPriorityFeePerGas"),
  186. maxFeePerGas: handleUint(fields[3], "maxFeePerGas"),
  187. gasPrice: null,
  188. gasLimit: handleUint(fields[4], "gasLimit"),
  189. to: handleAddress(fields[5]),
  190. value: handleUint(fields[6], "value"),
  191. data: (0, index_js_3.hexlify)(fields[7]),
  192. accessList: handleAccessList(fields[8], "accessList"),
  193. };
  194. // Unsigned EIP-1559 Transaction
  195. if (fields.length === 9) {
  196. return tx;
  197. }
  198. //tx.hash = keccak256(data);
  199. _parseEipSignature(tx, fields.slice(9));
  200. return tx;
  201. }
  202. function _serializeEip1559(tx, sig) {
  203. const fields = [
  204. formatNumber(tx.chainId, "chainId"),
  205. formatNumber(tx.nonce, "nonce"),
  206. formatNumber(tx.maxPriorityFeePerGas || 0, "maxPriorityFeePerGas"),
  207. formatNumber(tx.maxFeePerGas || 0, "maxFeePerGas"),
  208. formatNumber(tx.gasLimit, "gasLimit"),
  209. (tx.to || "0x"),
  210. formatNumber(tx.value, "value"),
  211. tx.data,
  212. formatAccessList(tx.accessList || [])
  213. ];
  214. if (sig) {
  215. fields.push(formatNumber(sig.yParity, "yParity"));
  216. fields.push((0, index_js_3.toBeArray)(sig.r));
  217. fields.push((0, index_js_3.toBeArray)(sig.s));
  218. }
  219. return (0, index_js_3.concat)(["0x02", (0, index_js_3.encodeRlp)(fields)]);
  220. }
  221. function _parseEip2930(data) {
  222. const fields = (0, index_js_3.decodeRlp)((0, index_js_3.getBytes)(data).slice(1));
  223. (0, index_js_3.assertArgument)(Array.isArray(fields) && (fields.length === 8 || fields.length === 11), "invalid field count for transaction type: 1", "data", (0, index_js_3.hexlify)(data));
  224. const tx = {
  225. type: 1,
  226. chainId: handleUint(fields[0], "chainId"),
  227. nonce: handleNumber(fields[1], "nonce"),
  228. gasPrice: handleUint(fields[2], "gasPrice"),
  229. gasLimit: handleUint(fields[3], "gasLimit"),
  230. to: handleAddress(fields[4]),
  231. value: handleUint(fields[5], "value"),
  232. data: (0, index_js_3.hexlify)(fields[6]),
  233. accessList: handleAccessList(fields[7], "accessList")
  234. };
  235. // Unsigned EIP-2930 Transaction
  236. if (fields.length === 8) {
  237. return tx;
  238. }
  239. //tx.hash = keccak256(data);
  240. _parseEipSignature(tx, fields.slice(8));
  241. return tx;
  242. }
  243. function _serializeEip2930(tx, sig) {
  244. const fields = [
  245. formatNumber(tx.chainId, "chainId"),
  246. formatNumber(tx.nonce, "nonce"),
  247. formatNumber(tx.gasPrice || 0, "gasPrice"),
  248. formatNumber(tx.gasLimit, "gasLimit"),
  249. (tx.to || "0x"),
  250. formatNumber(tx.value, "value"),
  251. tx.data,
  252. formatAccessList(tx.accessList || [])
  253. ];
  254. if (sig) {
  255. fields.push(formatNumber(sig.yParity, "recoveryParam"));
  256. fields.push((0, index_js_3.toBeArray)(sig.r));
  257. fields.push((0, index_js_3.toBeArray)(sig.s));
  258. }
  259. return (0, index_js_3.concat)(["0x01", (0, index_js_3.encodeRlp)(fields)]);
  260. }
  261. function _parseEip4844(data) {
  262. let fields = (0, index_js_3.decodeRlp)((0, index_js_3.getBytes)(data).slice(1));
  263. let typeName = "3";
  264. let blobs = null;
  265. // Parse the network format
  266. if (fields.length === 4 && Array.isArray(fields[0])) {
  267. typeName = "3 (network format)";
  268. const fBlobs = fields[1], fCommits = fields[2], fProofs = fields[3];
  269. (0, index_js_3.assertArgument)(Array.isArray(fBlobs), "invalid network format: blobs not an array", "fields[1]", fBlobs);
  270. (0, index_js_3.assertArgument)(Array.isArray(fCommits), "invalid network format: commitments not an array", "fields[2]", fCommits);
  271. (0, index_js_3.assertArgument)(Array.isArray(fProofs), "invalid network format: proofs not an array", "fields[3]", fProofs);
  272. (0, index_js_3.assertArgument)(fBlobs.length === fCommits.length, "invalid network format: blobs/commitments length mismatch", "fields", fields);
  273. (0, index_js_3.assertArgument)(fBlobs.length === fProofs.length, "invalid network format: blobs/proofs length mismatch", "fields", fields);
  274. blobs = [];
  275. for (let i = 0; i < fields[1].length; i++) {
  276. blobs.push({
  277. data: fBlobs[i],
  278. commitment: fCommits[i],
  279. proof: fProofs[i],
  280. });
  281. }
  282. fields = fields[0];
  283. }
  284. (0, index_js_3.assertArgument)(Array.isArray(fields) && (fields.length === 11 || fields.length === 14), `invalid field count for transaction type: ${typeName}`, "data", (0, index_js_3.hexlify)(data));
  285. const tx = {
  286. type: 3,
  287. chainId: handleUint(fields[0], "chainId"),
  288. nonce: handleNumber(fields[1], "nonce"),
  289. maxPriorityFeePerGas: handleUint(fields[2], "maxPriorityFeePerGas"),
  290. maxFeePerGas: handleUint(fields[3], "maxFeePerGas"),
  291. gasPrice: null,
  292. gasLimit: handleUint(fields[4], "gasLimit"),
  293. to: handleAddress(fields[5]),
  294. value: handleUint(fields[6], "value"),
  295. data: (0, index_js_3.hexlify)(fields[7]),
  296. accessList: handleAccessList(fields[8], "accessList"),
  297. maxFeePerBlobGas: handleUint(fields[9], "maxFeePerBlobGas"),
  298. blobVersionedHashes: fields[10]
  299. };
  300. if (blobs) {
  301. tx.blobs = blobs;
  302. }
  303. (0, index_js_3.assertArgument)(tx.to != null, `invalid address for transaction type: ${typeName}`, "data", data);
  304. (0, index_js_3.assertArgument)(Array.isArray(tx.blobVersionedHashes), "invalid blobVersionedHashes: must be an array", "data", data);
  305. for (let i = 0; i < tx.blobVersionedHashes.length; i++) {
  306. (0, index_js_3.assertArgument)((0, index_js_3.isHexString)(tx.blobVersionedHashes[i], 32), `invalid blobVersionedHash at index ${i}: must be length 32`, "data", data);
  307. }
  308. // Unsigned EIP-4844 Transaction
  309. if (fields.length === 11) {
  310. return tx;
  311. }
  312. // @TODO: Do we need to do this? This is only called internally
  313. // and used to verify hashes; it might save time to not do this
  314. //tx.hash = keccak256(concat([ "0x03", encodeRlp(fields) ]));
  315. _parseEipSignature(tx, fields.slice(11));
  316. return tx;
  317. }
  318. function _serializeEip4844(tx, sig, blobs) {
  319. const fields = [
  320. formatNumber(tx.chainId, "chainId"),
  321. formatNumber(tx.nonce, "nonce"),
  322. formatNumber(tx.maxPriorityFeePerGas || 0, "maxPriorityFeePerGas"),
  323. formatNumber(tx.maxFeePerGas || 0, "maxFeePerGas"),
  324. formatNumber(tx.gasLimit, "gasLimit"),
  325. (tx.to || addresses_js_1.ZeroAddress),
  326. formatNumber(tx.value, "value"),
  327. tx.data,
  328. formatAccessList(tx.accessList || []),
  329. formatNumber(tx.maxFeePerBlobGas || 0, "maxFeePerBlobGas"),
  330. formatHashes(tx.blobVersionedHashes || [], "blobVersionedHashes")
  331. ];
  332. if (sig) {
  333. fields.push(formatNumber(sig.yParity, "yParity"));
  334. fields.push((0, index_js_3.toBeArray)(sig.r));
  335. fields.push((0, index_js_3.toBeArray)(sig.s));
  336. // We have blobs; return the network wrapped format
  337. if (blobs) {
  338. return (0, index_js_3.concat)([
  339. "0x03",
  340. (0, index_js_3.encodeRlp)([
  341. fields,
  342. blobs.map((b) => b.data),
  343. blobs.map((b) => b.commitment),
  344. blobs.map((b) => b.proof),
  345. ])
  346. ]);
  347. }
  348. }
  349. return (0, index_js_3.concat)(["0x03", (0, index_js_3.encodeRlp)(fields)]);
  350. }
  351. /**
  352. * A **Transaction** describes an operation to be executed on
  353. * Ethereum by an Externally Owned Account (EOA). It includes
  354. * who (the [[to]] address), what (the [[data]]) and how much (the
  355. * [[value]] in ether) the operation should entail.
  356. *
  357. * @example:
  358. * tx = new Transaction()
  359. * //_result:
  360. *
  361. * tx.data = "0x1234";
  362. * //_result:
  363. */
  364. class Transaction {
  365. #type;
  366. #to;
  367. #data;
  368. #nonce;
  369. #gasLimit;
  370. #gasPrice;
  371. #maxPriorityFeePerGas;
  372. #maxFeePerGas;
  373. #value;
  374. #chainId;
  375. #sig;
  376. #accessList;
  377. #maxFeePerBlobGas;
  378. #blobVersionedHashes;
  379. #kzg;
  380. #blobs;
  381. /**
  382. * The transaction type.
  383. *
  384. * If null, the type will be automatically inferred based on
  385. * explicit properties.
  386. */
  387. get type() { return this.#type; }
  388. set type(value) {
  389. switch (value) {
  390. case null:
  391. this.#type = null;
  392. break;
  393. case 0:
  394. case "legacy":
  395. this.#type = 0;
  396. break;
  397. case 1:
  398. case "berlin":
  399. case "eip-2930":
  400. this.#type = 1;
  401. break;
  402. case 2:
  403. case "london":
  404. case "eip-1559":
  405. this.#type = 2;
  406. break;
  407. case 3:
  408. case "cancun":
  409. case "eip-4844":
  410. this.#type = 3;
  411. break;
  412. default:
  413. (0, index_js_3.assertArgument)(false, "unsupported transaction type", "type", value);
  414. }
  415. }
  416. /**
  417. * The name of the transaction type.
  418. */
  419. get typeName() {
  420. switch (this.type) {
  421. case 0: return "legacy";
  422. case 1: return "eip-2930";
  423. case 2: return "eip-1559";
  424. case 3: return "eip-4844";
  425. }
  426. return null;
  427. }
  428. /**
  429. * The ``to`` address for the transaction or ``null`` if the
  430. * transaction is an ``init`` transaction.
  431. */
  432. get to() {
  433. const value = this.#to;
  434. if (value == null && this.type === 3) {
  435. return addresses_js_1.ZeroAddress;
  436. }
  437. return value;
  438. }
  439. set to(value) {
  440. this.#to = (value == null) ? null : (0, index_js_1.getAddress)(value);
  441. }
  442. /**
  443. * The transaction nonce.
  444. */
  445. get nonce() { return this.#nonce; }
  446. set nonce(value) { this.#nonce = (0, index_js_3.getNumber)(value, "value"); }
  447. /**
  448. * The gas limit.
  449. */
  450. get gasLimit() { return this.#gasLimit; }
  451. set gasLimit(value) { this.#gasLimit = (0, index_js_3.getBigInt)(value); }
  452. /**
  453. * The gas price.
  454. *
  455. * On legacy networks this defines the fee that will be paid. On
  456. * EIP-1559 networks, this should be ``null``.
  457. */
  458. get gasPrice() {
  459. const value = this.#gasPrice;
  460. if (value == null && (this.type === 0 || this.type === 1)) {
  461. return BN_0;
  462. }
  463. return value;
  464. }
  465. set gasPrice(value) {
  466. this.#gasPrice = (value == null) ? null : (0, index_js_3.getBigInt)(value, "gasPrice");
  467. }
  468. /**
  469. * The maximum priority fee per unit of gas to pay. On legacy
  470. * networks this should be ``null``.
  471. */
  472. get maxPriorityFeePerGas() {
  473. const value = this.#maxPriorityFeePerGas;
  474. if (value == null) {
  475. if (this.type === 2 || this.type === 3) {
  476. return BN_0;
  477. }
  478. return null;
  479. }
  480. return value;
  481. }
  482. set maxPriorityFeePerGas(value) {
  483. this.#maxPriorityFeePerGas = (value == null) ? null : (0, index_js_3.getBigInt)(value, "maxPriorityFeePerGas");
  484. }
  485. /**
  486. * The maximum total fee per unit of gas to pay. On legacy
  487. * networks this should be ``null``.
  488. */
  489. get maxFeePerGas() {
  490. const value = this.#maxFeePerGas;
  491. if (value == null) {
  492. if (this.type === 2 || this.type === 3) {
  493. return BN_0;
  494. }
  495. return null;
  496. }
  497. return value;
  498. }
  499. set maxFeePerGas(value) {
  500. this.#maxFeePerGas = (value == null) ? null : (0, index_js_3.getBigInt)(value, "maxFeePerGas");
  501. }
  502. /**
  503. * The transaction data. For ``init`` transactions this is the
  504. * deployment code.
  505. */
  506. get data() { return this.#data; }
  507. set data(value) { this.#data = (0, index_js_3.hexlify)(value); }
  508. /**
  509. * The amount of ether (in wei) to send in this transactions.
  510. */
  511. get value() { return this.#value; }
  512. set value(value) {
  513. this.#value = (0, index_js_3.getBigInt)(value, "value");
  514. }
  515. /**
  516. * The chain ID this transaction is valid on.
  517. */
  518. get chainId() { return this.#chainId; }
  519. set chainId(value) { this.#chainId = (0, index_js_3.getBigInt)(value); }
  520. /**
  521. * If signed, the signature for this transaction.
  522. */
  523. get signature() { return this.#sig || null; }
  524. set signature(value) {
  525. this.#sig = (value == null) ? null : index_js_2.Signature.from(value);
  526. }
  527. /**
  528. * The access list.
  529. *
  530. * An access list permits discounted (but pre-paid) access to
  531. * bytecode and state variable access within contract execution.
  532. */
  533. get accessList() {
  534. const value = this.#accessList || null;
  535. if (value == null) {
  536. if (this.type === 1 || this.type === 2 || this.type === 3) {
  537. // @TODO: in v7, this should assign the value or become
  538. // a live object itself, otherwise mutation is inconsistent
  539. return [];
  540. }
  541. return null;
  542. }
  543. return value;
  544. }
  545. set accessList(value) {
  546. this.#accessList = (value == null) ? null : (0, accesslist_js_1.accessListify)(value);
  547. }
  548. /**
  549. * The max fee per blob gas for Cancun transactions.
  550. */
  551. get maxFeePerBlobGas() {
  552. const value = this.#maxFeePerBlobGas;
  553. if (value == null && this.type === 3) {
  554. return BN_0;
  555. }
  556. return value;
  557. }
  558. set maxFeePerBlobGas(value) {
  559. this.#maxFeePerBlobGas = (value == null) ? null : (0, index_js_3.getBigInt)(value, "maxFeePerBlobGas");
  560. }
  561. /**
  562. * The BLOb versioned hashes for Cancun transactions.
  563. */
  564. get blobVersionedHashes() {
  565. // @TODO: Mutation is inconsistent; if unset, the returned value
  566. // cannot mutate the object, if set it can
  567. let value = this.#blobVersionedHashes;
  568. if (value == null && this.type === 3) {
  569. return [];
  570. }
  571. return value;
  572. }
  573. set blobVersionedHashes(value) {
  574. if (value != null) {
  575. (0, index_js_3.assertArgument)(Array.isArray(value), "blobVersionedHashes must be an Array", "value", value);
  576. value = value.slice();
  577. for (let i = 0; i < value.length; i++) {
  578. (0, index_js_3.assertArgument)((0, index_js_3.isHexString)(value[i], 32), "invalid blobVersionedHash", `value[${i}]`, value[i]);
  579. }
  580. }
  581. this.#blobVersionedHashes = value;
  582. }
  583. /**
  584. * The BLObs for the Transaction, if any.
  585. *
  586. * If ``blobs`` is non-``null``, then the [[seriailized]]
  587. * will return the network formatted sidecar, otherwise it
  588. * will return the standard [[link-eip-2718]] payload. The
  589. * [[unsignedSerialized]] is unaffected regardless.
  590. *
  591. * When setting ``blobs``, either fully valid [[Blob]] objects
  592. * may be specified (i.e. correctly padded, with correct
  593. * committments and proofs) or a raw [[BytesLike]] may
  594. * be provided.
  595. *
  596. * If raw [[BytesLike]] are provided, the [[kzg]] property **must**
  597. * be already set. The blob will be correctly padded and the
  598. * [[KzgLibrary]] will be used to compute the committment and
  599. * proof for the blob.
  600. *
  601. * A BLOb is a sequence of field elements, each of which must
  602. * be within the BLS field modulo, so some additional processing
  603. * may be required to encode arbitrary data to ensure each 32 byte
  604. * field is within the valid range.
  605. *
  606. * Setting this automatically populates [[blobVersionedHashes]],
  607. * overwriting any existing values. Setting this to ``null``
  608. * does **not** remove the [[blobVersionedHashes]], leaving them
  609. * present.
  610. */
  611. get blobs() {
  612. if (this.#blobs == null) {
  613. return null;
  614. }
  615. return this.#blobs.map((b) => Object.assign({}, b));
  616. }
  617. set blobs(_blobs) {
  618. if (_blobs == null) {
  619. this.#blobs = null;
  620. return;
  621. }
  622. const blobs = [];
  623. const versionedHashes = [];
  624. for (let i = 0; i < _blobs.length; i++) {
  625. const blob = _blobs[i];
  626. if ((0, index_js_3.isBytesLike)(blob)) {
  627. (0, index_js_3.assert)(this.#kzg, "adding a raw blob requires a KZG library", "UNSUPPORTED_OPERATION", {
  628. operation: "set blobs()"
  629. });
  630. let data = (0, index_js_3.getBytes)(blob);
  631. (0, index_js_3.assertArgument)(data.length <= BLOB_SIZE, "blob is too large", `blobs[${i}]`, blob);
  632. // Pad blob if necessary
  633. if (data.length !== BLOB_SIZE) {
  634. const padded = new Uint8Array(BLOB_SIZE);
  635. padded.set(data);
  636. data = padded;
  637. }
  638. const commit = this.#kzg.blobToKzgCommitment(data);
  639. const proof = (0, index_js_3.hexlify)(this.#kzg.computeBlobKzgProof(data, commit));
  640. blobs.push({
  641. data: (0, index_js_3.hexlify)(data),
  642. commitment: (0, index_js_3.hexlify)(commit),
  643. proof
  644. });
  645. versionedHashes.push(getVersionedHash(1, commit));
  646. }
  647. else {
  648. const commit = (0, index_js_3.hexlify)(blob.commitment);
  649. blobs.push({
  650. data: (0, index_js_3.hexlify)(blob.data),
  651. commitment: commit,
  652. proof: (0, index_js_3.hexlify)(blob.proof)
  653. });
  654. versionedHashes.push(getVersionedHash(1, commit));
  655. }
  656. }
  657. this.#blobs = blobs;
  658. this.#blobVersionedHashes = versionedHashes;
  659. }
  660. get kzg() { return this.#kzg; }
  661. set kzg(kzg) {
  662. this.#kzg = kzg;
  663. }
  664. /**
  665. * Creates a new Transaction with default values.
  666. */
  667. constructor() {
  668. this.#type = null;
  669. this.#to = null;
  670. this.#nonce = 0;
  671. this.#gasLimit = BN_0;
  672. this.#gasPrice = null;
  673. this.#maxPriorityFeePerGas = null;
  674. this.#maxFeePerGas = null;
  675. this.#data = "0x";
  676. this.#value = BN_0;
  677. this.#chainId = BN_0;
  678. this.#sig = null;
  679. this.#accessList = null;
  680. this.#maxFeePerBlobGas = null;
  681. this.#blobVersionedHashes = null;
  682. this.#blobs = null;
  683. this.#kzg = null;
  684. }
  685. /**
  686. * The transaction hash, if signed. Otherwise, ``null``.
  687. */
  688. get hash() {
  689. if (this.signature == null) {
  690. return null;
  691. }
  692. return (0, index_js_2.keccak256)(this.#getSerialized(true, false));
  693. }
  694. /**
  695. * The pre-image hash of this transaction.
  696. *
  697. * This is the digest that a [[Signer]] must sign to authorize
  698. * this transaction.
  699. */
  700. get unsignedHash() {
  701. return (0, index_js_2.keccak256)(this.unsignedSerialized);
  702. }
  703. /**
  704. * The sending address, if signed. Otherwise, ``null``.
  705. */
  706. get from() {
  707. if (this.signature == null) {
  708. return null;
  709. }
  710. return (0, address_js_1.recoverAddress)(this.unsignedHash, this.signature);
  711. }
  712. /**
  713. * The public key of the sender, if signed. Otherwise, ``null``.
  714. */
  715. get fromPublicKey() {
  716. if (this.signature == null) {
  717. return null;
  718. }
  719. return index_js_2.SigningKey.recoverPublicKey(this.unsignedHash, this.signature);
  720. }
  721. /**
  722. * Returns true if signed.
  723. *
  724. * This provides a Type Guard that properties requiring a signed
  725. * transaction are non-null.
  726. */
  727. isSigned() {
  728. return this.signature != null;
  729. }
  730. #getSerialized(signed, sidecar) {
  731. (0, index_js_3.assert)(!signed || this.signature != null, "cannot serialize unsigned transaction; maybe you meant .unsignedSerialized", "UNSUPPORTED_OPERATION", { operation: ".serialized" });
  732. const sig = signed ? this.signature : null;
  733. switch (this.inferType()) {
  734. case 0:
  735. return _serializeLegacy(this, sig);
  736. case 1:
  737. return _serializeEip2930(this, sig);
  738. case 2:
  739. return _serializeEip1559(this, sig);
  740. case 3:
  741. return _serializeEip4844(this, sig, sidecar ? this.blobs : null);
  742. }
  743. (0, index_js_3.assert)(false, "unsupported transaction type", "UNSUPPORTED_OPERATION", { operation: ".serialized" });
  744. }
  745. /**
  746. * The serialized transaction.
  747. *
  748. * This throws if the transaction is unsigned. For the pre-image,
  749. * use [[unsignedSerialized]].
  750. */
  751. get serialized() {
  752. return this.#getSerialized(true, true);
  753. }
  754. /**
  755. * The transaction pre-image.
  756. *
  757. * The hash of this is the digest which needs to be signed to
  758. * authorize this transaction.
  759. */
  760. get unsignedSerialized() {
  761. return this.#getSerialized(false, false);
  762. }
  763. /**
  764. * Return the most "likely" type; currently the highest
  765. * supported transaction type.
  766. */
  767. inferType() {
  768. const types = this.inferTypes();
  769. // Prefer London (EIP-1559) over Cancun (BLOb)
  770. if (types.indexOf(2) >= 0) {
  771. return 2;
  772. }
  773. // Return the highest inferred type
  774. return (types.pop());
  775. }
  776. /**
  777. * Validates the explicit properties and returns a list of compatible
  778. * transaction types.
  779. */
  780. inferTypes() {
  781. // Checks that there are no conflicting properties set
  782. const hasGasPrice = this.gasPrice != null;
  783. const hasFee = (this.maxFeePerGas != null || this.maxPriorityFeePerGas != null);
  784. const hasAccessList = (this.accessList != null);
  785. const hasBlob = (this.#maxFeePerBlobGas != null || this.#blobVersionedHashes);
  786. //if (hasGasPrice && hasFee) {
  787. // throw new Error("transaction cannot have gasPrice and maxFeePerGas");
  788. //}
  789. if (this.maxFeePerGas != null && this.maxPriorityFeePerGas != null) {
  790. (0, index_js_3.assert)(this.maxFeePerGas >= this.maxPriorityFeePerGas, "priorityFee cannot be more than maxFee", "BAD_DATA", { value: this });
  791. }
  792. //if (this.type === 2 && hasGasPrice) {
  793. // throw new Error("eip-1559 transaction cannot have gasPrice");
  794. //}
  795. (0, index_js_3.assert)(!hasFee || (this.type !== 0 && this.type !== 1), "transaction type cannot have maxFeePerGas or maxPriorityFeePerGas", "BAD_DATA", { value: this });
  796. (0, index_js_3.assert)(this.type !== 0 || !hasAccessList, "legacy transaction cannot have accessList", "BAD_DATA", { value: this });
  797. const types = [];
  798. // Explicit type
  799. if (this.type != null) {
  800. types.push(this.type);
  801. }
  802. else {
  803. if (hasFee) {
  804. types.push(2);
  805. }
  806. else if (hasGasPrice) {
  807. types.push(1);
  808. if (!hasAccessList) {
  809. types.push(0);
  810. }
  811. }
  812. else if (hasAccessList) {
  813. types.push(1);
  814. types.push(2);
  815. }
  816. else if (hasBlob && this.to) {
  817. types.push(3);
  818. }
  819. else {
  820. types.push(0);
  821. types.push(1);
  822. types.push(2);
  823. types.push(3);
  824. }
  825. }
  826. types.sort();
  827. return types;
  828. }
  829. /**
  830. * Returns true if this transaction is a legacy transaction (i.e.
  831. * ``type === 0``).
  832. *
  833. * This provides a Type Guard that the related properties are
  834. * non-null.
  835. */
  836. isLegacy() {
  837. return (this.type === 0);
  838. }
  839. /**
  840. * Returns true if this transaction is berlin hardform transaction (i.e.
  841. * ``type === 1``).
  842. *
  843. * This provides a Type Guard that the related properties are
  844. * non-null.
  845. */
  846. isBerlin() {
  847. return (this.type === 1);
  848. }
  849. /**
  850. * Returns true if this transaction is london hardform transaction (i.e.
  851. * ``type === 2``).
  852. *
  853. * This provides a Type Guard that the related properties are
  854. * non-null.
  855. */
  856. isLondon() {
  857. return (this.type === 2);
  858. }
  859. /**
  860. * Returns true if this transaction is an [[link-eip-4844]] BLOB
  861. * transaction.
  862. *
  863. * This provides a Type Guard that the related properties are
  864. * non-null.
  865. */
  866. isCancun() {
  867. return (this.type === 3);
  868. }
  869. /**
  870. * Create a copy of this transaciton.
  871. */
  872. clone() {
  873. return Transaction.from(this);
  874. }
  875. /**
  876. * Return a JSON-friendly object.
  877. */
  878. toJSON() {
  879. const s = (v) => {
  880. if (v == null) {
  881. return null;
  882. }
  883. return v.toString();
  884. };
  885. return {
  886. type: this.type,
  887. to: this.to,
  888. // from: this.from,
  889. data: this.data,
  890. nonce: this.nonce,
  891. gasLimit: s(this.gasLimit),
  892. gasPrice: s(this.gasPrice),
  893. maxPriorityFeePerGas: s(this.maxPriorityFeePerGas),
  894. maxFeePerGas: s(this.maxFeePerGas),
  895. value: s(this.value),
  896. chainId: s(this.chainId),
  897. sig: this.signature ? this.signature.toJSON() : null,
  898. accessList: this.accessList
  899. };
  900. }
  901. /**
  902. * Create a **Transaction** from a serialized transaction or a
  903. * Transaction-like object.
  904. */
  905. static from(tx) {
  906. if (tx == null) {
  907. return new Transaction();
  908. }
  909. if (typeof (tx) === "string") {
  910. const payload = (0, index_js_3.getBytes)(tx);
  911. if (payload[0] >= 0x7f) { // @TODO: > vs >= ??
  912. return Transaction.from(_parseLegacy(payload));
  913. }
  914. switch (payload[0]) {
  915. case 1: return Transaction.from(_parseEip2930(payload));
  916. case 2: return Transaction.from(_parseEip1559(payload));
  917. case 3: return Transaction.from(_parseEip4844(payload));
  918. }
  919. (0, index_js_3.assert)(false, "unsupported transaction type", "UNSUPPORTED_OPERATION", { operation: "from" });
  920. }
  921. const result = new Transaction();
  922. if (tx.type != null) {
  923. result.type = tx.type;
  924. }
  925. if (tx.to != null) {
  926. result.to = tx.to;
  927. }
  928. if (tx.nonce != null) {
  929. result.nonce = tx.nonce;
  930. }
  931. if (tx.gasLimit != null) {
  932. result.gasLimit = tx.gasLimit;
  933. }
  934. if (tx.gasPrice != null) {
  935. result.gasPrice = tx.gasPrice;
  936. }
  937. if (tx.maxPriorityFeePerGas != null) {
  938. result.maxPriorityFeePerGas = tx.maxPriorityFeePerGas;
  939. }
  940. if (tx.maxFeePerGas != null) {
  941. result.maxFeePerGas = tx.maxFeePerGas;
  942. }
  943. if (tx.maxFeePerBlobGas != null) {
  944. result.maxFeePerBlobGas = tx.maxFeePerBlobGas;
  945. }
  946. if (tx.data != null) {
  947. result.data = tx.data;
  948. }
  949. if (tx.value != null) {
  950. result.value = tx.value;
  951. }
  952. if (tx.chainId != null) {
  953. result.chainId = tx.chainId;
  954. }
  955. if (tx.signature != null) {
  956. result.signature = index_js_2.Signature.from(tx.signature);
  957. }
  958. if (tx.accessList != null) {
  959. result.accessList = tx.accessList;
  960. }
  961. // This will get overwritten by blobs, if present
  962. if (tx.blobVersionedHashes != null) {
  963. result.blobVersionedHashes = tx.blobVersionedHashes;
  964. }
  965. // Make sure we assign the kzg before assigning blobs, which
  966. // require the library in the event raw blob data is provided.
  967. if (tx.kzg != null) {
  968. result.kzg = tx.kzg;
  969. }
  970. if (tx.blobs != null) {
  971. result.blobs = tx.blobs;
  972. }
  973. if (tx.hash != null) {
  974. (0, index_js_3.assertArgument)(result.isSigned(), "unsigned transaction cannot define '.hash'", "tx", tx);
  975. (0, index_js_3.assertArgument)(result.hash === tx.hash, "hash mismatch", "tx", tx);
  976. }
  977. if (tx.from != null) {
  978. (0, index_js_3.assertArgument)(result.isSigned(), "unsigned transaction cannot define '.from'", "tx", tx);
  979. (0, index_js_3.assertArgument)(result.from.toLowerCase() === (tx.from || "").toLowerCase(), "from mismatch", "tx", tx);
  980. }
  981. return result;
  982. }
  983. }
  984. exports.Transaction = Transaction;
  985. //# sourceMappingURL=transaction.js.map