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