viem 0.2.0-0-2-0.20230320T235312 → 0.2.0-0-2-0.20230328T054201

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 (72) hide show
  1. package/accounts/package.json +4 -0
  2. package/dist/abi.d.ts +3 -3
  3. package/dist/abi.js +2 -2
  4. package/dist/abi.mjs +1 -1
  5. package/dist/accounts/index.d.ts +90 -0
  6. package/dist/accounts/index.js +152 -0
  7. package/dist/accounts/index.js.map +1 -0
  8. package/dist/accounts/index.mjs +152 -0
  9. package/dist/accounts/index.mjs.map +1 -0
  10. package/dist/{chain-c4f924cf.d.ts → chain-40443542.d.ts} +1 -1
  11. package/dist/{chain-b2e88e30.d.ts → chain-4af995bf.d.ts} +121 -85
  12. package/dist/chains.d.ts +3 -3
  13. package/dist/chains.js +63 -63
  14. package/dist/chains.mjs +1 -1
  15. package/dist/chunk-43HQD3IL.js +31 -0
  16. package/dist/chunk-43HQD3IL.js.map +1 -0
  17. package/dist/{chunk-4DIN6KDM.mjs → chunk-DFMQLPHS.mjs} +2 -2
  18. package/dist/chunk-DFMQLPHS.mjs.map +1 -0
  19. package/dist/{chunk-BLB7NERO.js → chunk-FZPFAAOJ.js} +19 -19
  20. package/dist/chunk-FZPFAAOJ.js.map +1 -0
  21. package/dist/{chunk-KOAJUBTN.mjs → chunk-HYEOQX7O.mjs} +802 -193
  22. package/dist/chunk-HYEOQX7O.mjs.map +1 -0
  23. package/dist/{chunk-T4AH4Y6Y.js → chunk-OW7TKT7L.js} +812 -203
  24. package/dist/chunk-OW7TKT7L.js.map +1 -0
  25. package/dist/chunk-UK6FAD4V.mjs +31 -0
  26. package/dist/chunk-UK6FAD4V.mjs.map +1 -0
  27. package/dist/contract.d.ts +9 -8
  28. package/dist/contract.js +2 -2
  29. package/dist/contract.mjs +1 -1
  30. package/dist/{createClient-d4f1dea1.d.ts → createClient-1f19be8d.d.ts} +23 -9
  31. package/dist/{createPublicClient-3a714a8d.d.ts → createPublicClient-fd8b1349.d.ts} +166 -126
  32. package/dist/{eip1193-cd3e872a.d.ts → eip1193-210ab776.d.ts} +1 -1
  33. package/dist/{encodeFunctionResult-750c9055.d.ts → encodeFunctionResult-a252fba1.d.ts} +1 -1
  34. package/dist/{encodePacked-de74201f.d.ts → encodePacked-a9464e97.d.ts} +1 -1
  35. package/dist/ens.d.ts +6 -5
  36. package/dist/ens.js +3 -3
  37. package/dist/ens.mjs +2 -2
  38. package/dist/ethers.d.ts +5 -4
  39. package/dist/ethers.js +7 -5
  40. package/dist/ethers.js.map +1 -1
  41. package/dist/ethers.mjs +7 -5
  42. package/dist/ethers.mjs.map +1 -1
  43. package/dist/{formatAbiItem-fdbdda39.d.ts → formatAbiItem-6ed70dbd.d.ts} +1 -1
  44. package/dist/{getAbiItem-9d709e3b.d.ts → getAbiItem-1ff0f41a.d.ts} +1 -1
  45. package/dist/index.d.ts +22 -15
  46. package/dist/index.js +130 -103
  47. package/dist/index.js.map +1 -1
  48. package/dist/index.mjs +72 -45
  49. package/dist/index.mjs.map +1 -1
  50. package/dist/parseAccount-8e131c73.d.ts +6 -0
  51. package/dist/{parseGwei-f930d155.d.ts → parseGwei-5e2a8e87.d.ts} +61 -18
  52. package/dist/public.d.ts +6 -5
  53. package/dist/public.js +2 -2
  54. package/dist/public.mjs +1 -1
  55. package/dist/test-e28db435.d.ts +198 -0
  56. package/dist/test.d.ts +11 -11
  57. package/dist/test.js +2 -2
  58. package/dist/test.mjs +1 -1
  59. package/dist/{account-07d04222.d.ts → typedData-848139b1.d.ts} +46 -27
  60. package/dist/utils/index.d.ts +17 -35
  61. package/dist/utils/index.js +18 -2
  62. package/dist/utils/index.mjs +19 -3
  63. package/dist/wallet.d.ts +6 -5
  64. package/dist/wallet.js +2 -2
  65. package/dist/wallet.mjs +1 -1
  66. package/dist/window.d.ts +2 -2
  67. package/package.json +9 -1
  68. package/dist/chunk-4DIN6KDM.mjs.map +0 -1
  69. package/dist/chunk-BLB7NERO.js.map +0 -1
  70. package/dist/chunk-KOAJUBTN.mjs.map +0 -1
  71. package/dist/chunk-T4AH4Y6Y.js.map +0 -1
  72. package/dist/test-c70126a6.d.ts +0 -206
@@ -20,12 +20,12 @@ import "abitype";
20
20
  var package_default = {
21
21
  name: "viem",
22
22
  description: "TypeScript Interface for Ethereum",
23
- version: "0.1.21",
23
+ version: "0.1.23",
24
24
  scripts: {
25
25
  anvil: "source .env && anvil --fork-url $VITE_ANVIL_FORK_URL --fork-block-number $VITE_ANVIL_BLOCK_NUMBER --block-time $VITE_ANVIL_BLOCK_TIME",
26
26
  bench: "vitest bench --no-threads",
27
27
  "bench:ci": "CI=true vitest bench --no-threads",
28
- build: "tsup",
28
+ build: "tsup && size-limit",
29
29
  changeset: "changeset",
30
30
  "changeset:release": "pnpm build && changeset publish",
31
31
  "changeset:version": "changeset version && pnpm install --lockfile-only",
@@ -53,6 +53,7 @@ var package_default = {
53
53
  entry: [
54
54
  "src/index.ts",
55
55
  "src/abi.ts",
56
+ "src/accounts/index.ts",
56
57
  "src/chains.ts",
57
58
  "src/contract.ts",
58
59
  "src/ens.ts",
@@ -66,6 +67,7 @@ var package_default = {
66
67
  },
67
68
  files: [
68
69
  "/abi",
70
+ "/accounts",
69
71
  "/dist",
70
72
  "/chains",
71
73
  "/contract",
@@ -88,6 +90,11 @@ var package_default = {
88
90
  module: "./dist/abi.mts",
89
91
  default: "./dist/abi.js"
90
92
  },
93
+ "./accounts": {
94
+ types: "./dist/accounts/index.d.ts",
95
+ module: "./dist/accounts/index.mts",
96
+ default: "./dist/accounts/index.js"
97
+ },
91
98
  "./chains": {
92
99
  types: "./dist/chains.d.ts",
93
100
  module: "./dist/chains.mts",
@@ -142,6 +149,8 @@ var package_default = {
142
149
  dependencies: {
143
150
  "@noble/hashes": "^1.1.2",
144
151
  "@noble/secp256k1": "^1.7.1",
152
+ "@scure/bip32": "^1.2.0",
153
+ "@scure/bip39": "^1.1.1",
145
154
  "@wagmi/chains": "~0.2.11",
146
155
  abitype: "~0.7.1",
147
156
  "idna-uts46-hx": "^4.1.2",
@@ -154,6 +163,7 @@ var package_default = {
154
163
  "@adraffy/ens-normalize": "^1.8.9",
155
164
  "@changesets/changelog-github": "^0.4.5",
156
165
  "@changesets/cli": "^2.23.2",
166
+ "@size-limit/preset-big-lib": "^8.2.4",
157
167
  "@types/dedent": "^0.7.0",
158
168
  "@types/fs-extra": "^9.0.13",
159
169
  "@types/node": "^17.0.45",
@@ -171,6 +181,7 @@ var package_default = {
171
181
  rimraf: "^4.1.2",
172
182
  rome: "^11.0.0",
173
183
  "simple-git-hooks": "^2.8.1",
184
+ "size-limit": "^8.2.4",
174
185
  tsup: "^6.6.0",
175
186
  typescript: "^4.9.4",
176
187
  vite: "^4.1.4",
@@ -189,12 +200,17 @@ var package_default = {
189
200
  "wallet",
190
201
  "web3"
191
202
  ],
203
+ "size-limit": [
204
+ {
205
+ path: "dist/index.js"
206
+ }
207
+ ],
192
208
  "simple-git-hooks": {
193
209
  "pre-commit": "pnpm format && pnpm lint:fix"
194
210
  },
195
211
  pnpm: {
196
212
  patchedDependencies: {
197
- "vitepress@1.0.0-alpha.49": "patches/vitepress@1.0.0-alpha.49.patch"
213
+ "vitepress@1.0.0-alpha.61": "patches/vitepress@1.0.0-alpha.61.patch"
198
214
  },
199
215
  peerDependencyRules: {
200
216
  ignoreMissing: [
@@ -613,6 +629,12 @@ var ChainNotFoundError = class extends BaseError {
613
629
  __publicField(this, "name", "ChainNotFoundError");
614
630
  }
615
631
  };
632
+ var InvalidChainIdError = class extends BaseError {
633
+ constructor({ chainId }) {
634
+ super(`Chain ID "${chainId}" is invalid.`);
635
+ __publicField(this, "name", "InvalidChainIdError");
636
+ }
637
+ };
616
638
 
617
639
  // src/constants/abis.ts
618
640
  var multicall3Abi = [
@@ -727,6 +749,69 @@ var FeeConflictError = class extends BaseError {
727
749
  __publicField(this, "name", "FeeConflictError");
728
750
  }
729
751
  };
752
+ var InvalidLegacyVError = class extends BaseError {
753
+ constructor({ v }) {
754
+ super(`Invalid \`v\` value "${v}". Expected 27 or 28.`);
755
+ __publicField(this, "name", "InvalidLegacyVError");
756
+ }
757
+ };
758
+ var InvalidSerializableTransactionError = class extends BaseError {
759
+ constructor({ transaction }) {
760
+ super("Cannot infer a transaction type from provided transaction.", {
761
+ metaMessages: [
762
+ "Provided Transaction:",
763
+ "{",
764
+ prettyPrint(transaction),
765
+ "}",
766
+ "",
767
+ "To infer the type, either provide:",
768
+ "- a `type` to the Transaction, or",
769
+ "- an EIP-1559 Transaction with `maxFeePerGas`, or",
770
+ "- an EIP-2930 Transaction with `gasPrice` & `accessList`, or",
771
+ "- a Legacy Transaction with `gasPrice`"
772
+ ]
773
+ });
774
+ __publicField(this, "name", "InvalidSerializableTransactionError");
775
+ }
776
+ };
777
+ var InvalidSerializedTransactionTypeError = class extends BaseError {
778
+ constructor({ serializedType }) {
779
+ super(`Serialized transaction type "${serializedType}" is invalid.`);
780
+ __publicField(this, "name", "InvalidSerializedTransactionType");
781
+ __publicField(this, "serializedType");
782
+ this.serializedType = serializedType;
783
+ }
784
+ };
785
+ var InvalidSerializedTransactionError = class extends BaseError {
786
+ constructor({
787
+ attributes,
788
+ serializedTransaction,
789
+ type
790
+ }) {
791
+ const missing = Object.entries(attributes).map(([key, value]) => typeof value === "undefined" ? key : void 0).filter(Boolean);
792
+ super(`Invalid serialized transaction of type "${type}" was provided.`, {
793
+ metaMessages: [
794
+ `Serialized Transaction: "${serializedTransaction}"`,
795
+ missing.length > 0 ? `Missing Attributes: ${missing.join(", ")}` : ""
796
+ ].filter(Boolean)
797
+ });
798
+ __publicField(this, "name", "InvalidSerializedTransactionError");
799
+ __publicField(this, "serializedTransaction");
800
+ __publicField(this, "type");
801
+ this.serializedTransaction = serializedTransaction;
802
+ this.type = type;
803
+ }
804
+ };
805
+ var InvalidStorageKeySizeError = class extends BaseError {
806
+ constructor({ storageKey }) {
807
+ super(
808
+ `Size for storage key "${storageKey}" is invalid. Expected 32 bytes. Got ${Math.floor(
809
+ (storageKey.length - 2) / 2
810
+ )} bytes.`
811
+ );
812
+ __publicField(this, "name", "InvalidStorageKeySizeError");
813
+ }
814
+ };
730
815
  var TransactionExecutionError = class extends BaseError {
731
816
  constructor(cause, {
732
817
  account,
@@ -1842,7 +1927,7 @@ function parse2(value) {
1842
1927
  function format2(bytes, to) {
1843
1928
  if (Array.isArray(bytes))
1844
1929
  return bytes.map((b) => format2(b, to));
1845
- return to === "hex" ? trim(bytesToHex(bytes)) : bytes;
1930
+ return to === "hex" ? bytesToHex(bytes) : bytes;
1846
1931
  }
1847
1932
  function rlpToBytes(bytes, offset = 0) {
1848
1933
  if (bytes.length === 0)
@@ -1952,6 +2037,11 @@ var getEventSelector = (event) => hashFunction(event);
1952
2037
  // src/utils/hash/getFunctionSelector.ts
1953
2038
  var getFunctionSelector = (fn) => slice(hashFunction(fn), 0, 4);
1954
2039
 
2040
+ // src/utils/hash/isHash.ts
2041
+ function isHash(hash2) {
2042
+ return hash2.startsWith("0x") && size(hash2) === 32;
2043
+ }
2044
+
1955
2045
  // src/utils/address/isAddress.ts
1956
2046
  var addressRegex = /^0x[a-fA-F0-9]{40}$/;
1957
2047
  function isAddress(address) {
@@ -2859,26 +2949,7 @@ function formatAbiItemWithArgs({
2859
2949
  ).join(", ")})`;
2860
2950
  }
2861
2951
 
2862
- // src/utils/account.ts
2863
- function getAccount(source) {
2864
- if (typeof source === "string") {
2865
- if (!isAddress(source))
2866
- throw new InvalidAddressError({ address: source });
2867
- return {
2868
- address: source,
2869
- type: "json-rpc"
2870
- };
2871
- }
2872
- if (!isAddress(source.address))
2873
- throw new InvalidAddressError({ address: source.address });
2874
- return {
2875
- address: source.address,
2876
- signMessage: source.signMessage,
2877
- signTransaction: source.signTransaction,
2878
- signTypedData: source.signTypedData,
2879
- type: "local"
2880
- };
2881
- }
2952
+ // src/accounts/utils/parseAccount.ts
2882
2953
  function parseAccount(account) {
2883
2954
  if (typeof account === "string")
2884
2955
  return { address: account, type: "json-rpc" };
@@ -3549,13 +3620,59 @@ ${messageBytes.length}`
3549
3620
  return keccak256(concat([prefixBytes, messageBytes]), to_);
3550
3621
  }
3551
3622
 
3623
+ // src/utils/typedData.ts
3624
+ function validateTypedData({
3625
+ domain,
3626
+ message,
3627
+ primaryType,
3628
+ types: types_
3629
+ }) {
3630
+ const types = types_;
3631
+ const validateData = (struct, value_) => {
3632
+ for (const param of struct) {
3633
+ const { name, type: type_ } = param;
3634
+ const type = type_;
3635
+ const value = value_[name];
3636
+ const integerMatch = type.match(integerRegex);
3637
+ if (integerMatch && (typeof value === "number" || typeof value === "bigint")) {
3638
+ const [_type, base, size_] = integerMatch;
3639
+ numberToHex(value, {
3640
+ signed: base === "int",
3641
+ size: parseInt(size_) / 8
3642
+ });
3643
+ }
3644
+ if (type === "address" && typeof value === "string" && !isAddress(value))
3645
+ throw new InvalidAddressError({ address: value });
3646
+ const bytesMatch = type.match(bytesRegex);
3647
+ if (bytesMatch) {
3648
+ const [_type, size_] = bytesMatch;
3649
+ if (size_ && size(value) !== parseInt(size_))
3650
+ throw new BytesSizeMismatchError({
3651
+ expectedSize: parseInt(size_),
3652
+ givenSize: size(value)
3653
+ });
3654
+ }
3655
+ const struct2 = types[type];
3656
+ if (struct2)
3657
+ validateData(struct2, value);
3658
+ }
3659
+ };
3660
+ if (types["EIP712Domain"] && domain)
3661
+ validateData(types["EIP712Domain"], domain);
3662
+ if (primaryType !== "EIP712Domain") {
3663
+ const type = types[primaryType];
3664
+ validateData(type, message);
3665
+ }
3666
+ }
3667
+
3552
3668
  // src/utils/signature/hashTypedData.ts
3553
3669
  function hashTypedData({
3554
- domain,
3670
+ domain: domain_,
3555
3671
  message,
3556
3672
  primaryType,
3557
3673
  types: types_
3558
3674
  }) {
3675
+ const domain = typeof domain_ === "undefined" ? {} : domain_;
3559
3676
  const types = {
3560
3677
  EIP712Domain: [
3561
3678
  domain?.name && { name: "name", type: "string" },
@@ -3569,6 +3686,12 @@ function hashTypedData({
3569
3686
  ].filter(Boolean),
3570
3687
  ...types_
3571
3688
  };
3689
+ validateTypedData({
3690
+ domain,
3691
+ message,
3692
+ primaryType,
3693
+ types
3694
+ });
3572
3695
  let parts = ["0x1901"];
3573
3696
  if (domain)
3574
3697
  parts.push(
@@ -3707,14 +3830,14 @@ function encodeField({
3707
3830
  }
3708
3831
 
3709
3832
  // src/utils/signature/recoverAddress.ts
3710
- import { recoverPublicKey } from "@noble/secp256k1";
3711
- function recoverAddress({
3833
+ async function recoverAddress({
3712
3834
  hash: hash2,
3713
3835
  signature
3714
3836
  }) {
3715
3837
  const signatureHex = isHex(signature) ? signature : toHex(signature);
3716
3838
  const hashHex = isHex(hash2) ? hash2 : toHex(hash2);
3717
3839
  const v = hexToNumber(`0x${signatureHex.slice(130)}`);
3840
+ const { recoverPublicKey } = await import("@noble/secp256k1");
3718
3841
  const publicKey = toHex(
3719
3842
  recoverPublicKey(
3720
3843
  hashHex.substring(2),
@@ -3727,7 +3850,7 @@ function recoverAddress({
3727
3850
  }
3728
3851
 
3729
3852
  // src/utils/signature/recoverMessageAddress.ts
3730
- function recoverMessageAddress({
3853
+ async function recoverMessageAddress({
3731
3854
  message,
3732
3855
  signature
3733
3856
  }) {
@@ -3735,7 +3858,7 @@ function recoverMessageAddress({
3735
3858
  }
3736
3859
 
3737
3860
  // src/utils/signature/recoverTypedDataAddress.ts
3738
- function recoverTypedDataAddress({
3861
+ async function recoverTypedDataAddress({
3739
3862
  domain,
3740
3863
  message,
3741
3864
  primaryType,
@@ -3754,19 +3877,19 @@ function recoverTypedDataAddress({
3754
3877
  }
3755
3878
 
3756
3879
  // src/utils/signature/verifyMessage.ts
3757
- function verifyMessage({
3880
+ async function verifyMessage({
3758
3881
  address,
3759
3882
  message,
3760
3883
  signature
3761
3884
  }) {
3762
3885
  return isAddressEqual(
3763
3886
  getAddress(address),
3764
- recoverMessageAddress({ message, signature })
3887
+ await recoverMessageAddress({ message, signature })
3765
3888
  );
3766
3889
  }
3767
3890
 
3768
3891
  // src/utils/signature/verifyTypedData.ts
3769
- function verifyTypedData({
3892
+ async function verifyTypedData({
3770
3893
  address,
3771
3894
  domain,
3772
3895
  message,
@@ -3776,7 +3899,7 @@ function verifyTypedData({
3776
3899
  }) {
3777
3900
  return isAddressEqual(
3778
3901
  getAddress(address),
3779
- recoverTypedDataAddress({
3902
+ await recoverTypedDataAddress({
3780
3903
  domain,
3781
3904
  message,
3782
3905
  primaryType,
@@ -3808,6 +3931,32 @@ function assertRequest(args) {
3808
3931
  throw new TipAboveFeeCapError({ maxFeePerGas, maxPriorityFeePerGas });
3809
3932
  }
3810
3933
 
3934
+ // src/utils/transaction/getSerializedTransactionType.ts
3935
+ function getSerializedTransactionType(serializedTransaction) {
3936
+ const serializedType = sliceHex(serializedTransaction, 0, 1);
3937
+ if (serializedType === "0x02")
3938
+ return "eip1559";
3939
+ if (serializedType === "0x01")
3940
+ return "eip2930";
3941
+ if (serializedType !== "0x" && hexToNumber(serializedType) >= 192)
3942
+ return "legacy";
3943
+ throw new InvalidSerializedTransactionTypeError({ serializedType });
3944
+ }
3945
+
3946
+ // src/utils/transaction/getTransactionType.ts
3947
+ function getTransactionType(transaction) {
3948
+ if (transaction.type)
3949
+ return transaction.type;
3950
+ if ("maxFeePerGas" in transaction || "maxPriorityFeePerGas" in transaction)
3951
+ return "eip1559";
3952
+ if ("gasPrice" in transaction) {
3953
+ if ("accessList" in transaction)
3954
+ return "eip2930";
3955
+ return "legacy";
3956
+ }
3957
+ throw new InvalidSerializableTransactionError({ transaction });
3958
+ }
3959
+
3811
3960
  // src/actions/public/call.ts
3812
3961
  async function call(client, args) {
3813
3962
  const {
@@ -4575,6 +4724,7 @@ async function waitForTransactionReceipt(client, {
4575
4724
  const unwatch = watchBlockNumber(client, {
4576
4725
  emitMissed: true,
4577
4726
  emitOnBegin: true,
4727
+ poll: true,
4578
4728
  pollingInterval,
4579
4729
  async onBlockNumber(blockNumber) {
4580
4730
  const done = async (fn) => {
@@ -4667,47 +4817,79 @@ function watchBlockNumber(client, {
4667
4817
  emitMissed = false,
4668
4818
  onBlockNumber,
4669
4819
  onError,
4820
+ poll: poll_,
4670
4821
  pollingInterval = client.pollingInterval
4671
4822
  }) {
4672
- const observerId = JSON.stringify([
4673
- "watchBlockNumber",
4674
- client.uid,
4675
- emitOnBegin,
4676
- emitMissed,
4677
- pollingInterval
4678
- ]);
4823
+ const enablePolling = typeof poll_ !== "undefined" ? poll_ : client.transport.type !== "webSocket";
4679
4824
  let prevBlockNumber;
4680
- return observe(
4681
- observerId,
4682
- { onBlockNumber, onError },
4683
- (emit) => poll(
4684
- async () => {
4685
- try {
4686
- const blockNumber = await getBlockNumber(client, { maxAge: 0 });
4687
- if (prevBlockNumber) {
4688
- if (blockNumber === prevBlockNumber)
4689
- return;
4690
- if (blockNumber - prevBlockNumber > 1 && emitMissed) {
4691
- for (let i = prevBlockNumber + 1n; i < blockNumber; i++) {
4692
- emit.onBlockNumber(i, prevBlockNumber);
4693
- prevBlockNumber = i;
4825
+ const pollBlockNumber = () => {
4826
+ const observerId = JSON.stringify([
4827
+ "watchBlockNumber",
4828
+ client.uid,
4829
+ emitOnBegin,
4830
+ emitMissed,
4831
+ pollingInterval
4832
+ ]);
4833
+ return observe(
4834
+ observerId,
4835
+ { onBlockNumber, onError },
4836
+ (emit) => poll(
4837
+ async () => {
4838
+ try {
4839
+ const blockNumber = await getBlockNumber(client, { maxAge: 0 });
4840
+ if (prevBlockNumber) {
4841
+ if (blockNumber === prevBlockNumber)
4842
+ return;
4843
+ if (blockNumber - prevBlockNumber > 1 && emitMissed) {
4844
+ for (let i = prevBlockNumber + 1n; i < blockNumber; i++) {
4845
+ emit.onBlockNumber(i, prevBlockNumber);
4846
+ prevBlockNumber = i;
4847
+ }
4694
4848
  }
4695
4849
  }
4850
+ if (!prevBlockNumber || blockNumber > prevBlockNumber) {
4851
+ emit.onBlockNumber(blockNumber, prevBlockNumber);
4852
+ prevBlockNumber = blockNumber;
4853
+ }
4854
+ } catch (err) {
4855
+ emit.onError?.(err);
4696
4856
  }
4697
- if (!prevBlockNumber || blockNumber > prevBlockNumber) {
4698
- emit.onBlockNumber(blockNumber, prevBlockNumber);
4857
+ },
4858
+ {
4859
+ emitOnBegin,
4860
+ interval: pollingInterval
4861
+ }
4862
+ )
4863
+ );
4864
+ };
4865
+ const subscribeBlockNumber = () => {
4866
+ let active = true;
4867
+ let unsubscribe = () => active = false;
4868
+ (async () => {
4869
+ try {
4870
+ const { unsubscribe: unsubscribe_ } = await client.transport.subscribe({
4871
+ params: ["newHeads"],
4872
+ onData(data) {
4873
+ if (!active)
4874
+ return;
4875
+ const blockNumber = hexToBigInt(data.result?.number);
4876
+ onBlockNumber(blockNumber, prevBlockNumber);
4699
4877
  prevBlockNumber = blockNumber;
4878
+ },
4879
+ onError(error) {
4880
+ onError?.(error);
4700
4881
  }
4701
- } catch (err) {
4702
- emit.onError?.(err);
4703
- }
4704
- },
4705
- {
4706
- emitOnBegin,
4707
- interval: pollingInterval
4882
+ });
4883
+ unsubscribe = unsubscribe_;
4884
+ if (!active)
4885
+ unsubscribe();
4886
+ } catch (err) {
4887
+ onError?.(err);
4708
4888
  }
4709
- )
4710
- );
4889
+ })();
4890
+ return unsubscribe;
4891
+ };
4892
+ return enablePolling ? pollBlockNumber() : subscribeBlockNumber();
4711
4893
  }
4712
4894
 
4713
4895
  // src/actions/public/watchBlocks.ts
@@ -4718,61 +4900,93 @@ function watchBlocks(client, {
4718
4900
  onBlock,
4719
4901
  onError,
4720
4902
  includeTransactions = false,
4903
+ poll: poll_,
4721
4904
  pollingInterval = client.pollingInterval
4722
4905
  }) {
4723
- const observerId = JSON.stringify([
4724
- "watchBlocks",
4725
- client.uid,
4726
- emitMissed,
4727
- emitOnBegin,
4728
- includeTransactions,
4729
- pollingInterval
4730
- ]);
4906
+ const enablePolling = typeof poll_ !== "undefined" ? poll_ : client.transport.type !== "webSocket";
4731
4907
  let prevBlock;
4732
- return observe(
4733
- observerId,
4734
- { onBlock, onError },
4735
- (emit) => poll(
4736
- async () => {
4737
- try {
4738
- const block = await getBlock(client, {
4739
- blockTag,
4740
- includeTransactions
4741
- });
4742
- if (block.number && prevBlock?.number) {
4743
- if (block.number === prevBlock.number)
4744
- return;
4745
- if (block.number - prevBlock.number > 1 && emitMissed) {
4746
- for (let i = prevBlock?.number + 1n; i < block.number; i++) {
4747
- const block2 = await getBlock(client, {
4748
- blockNumber: i,
4749
- includeTransactions
4750
- });
4751
- emit.onBlock(block2, prevBlock);
4752
- prevBlock = block2;
4908
+ const pollBlocks = () => {
4909
+ const observerId = JSON.stringify([
4910
+ "watchBlocks",
4911
+ client.uid,
4912
+ emitMissed,
4913
+ emitOnBegin,
4914
+ includeTransactions,
4915
+ pollingInterval
4916
+ ]);
4917
+ return observe(
4918
+ observerId,
4919
+ { onBlock, onError },
4920
+ (emit) => poll(
4921
+ async () => {
4922
+ try {
4923
+ const block = await getBlock(client, {
4924
+ blockTag,
4925
+ includeTransactions
4926
+ });
4927
+ if (block.number && prevBlock?.number) {
4928
+ if (block.number === prevBlock.number)
4929
+ return;
4930
+ if (block.number - prevBlock.number > 1 && emitMissed) {
4931
+ for (let i = prevBlock?.number + 1n; i < block.number; i++) {
4932
+ const block2 = await getBlock(client, {
4933
+ blockNumber: i,
4934
+ includeTransactions
4935
+ });
4936
+ emit.onBlock(block2, prevBlock);
4937
+ prevBlock = block2;
4938
+ }
4753
4939
  }
4754
4940
  }
4941
+ if (
4942
+ // If no previous block exists, emit.
4943
+ !prevBlock?.number || // If the block tag is "pending" with no block number, emit.
4944
+ blockTag === "pending" && !block?.number || // If the next block number is greater than the previous block number, emit.
4945
+ // We don't want to emit blocks in the past.
4946
+ block.number && block.number > prevBlock.number
4947
+ ) {
4948
+ emit.onBlock(block, prevBlock);
4949
+ prevBlock = block;
4950
+ }
4951
+ } catch (err) {
4952
+ emit.onError?.(err);
4755
4953
  }
4756
- if (
4757
- // If no previous block exists, emit.
4758
- !prevBlock?.number || // If the block tag is "pending" with no block number, emit.
4759
- blockTag === "pending" && !block?.number || // If the next block number is greater than the previous block number, emit.
4760
- // We don't want to emit blocks in the past.
4761
- block.number && block.number > prevBlock.number
4762
- ) {
4763
- emit.onBlock(block, prevBlock);
4954
+ },
4955
+ {
4956
+ emitOnBegin,
4957
+ interval: pollingInterval
4958
+ }
4959
+ )
4960
+ );
4961
+ };
4962
+ const subscribeBlocks = () => {
4963
+ let active = true;
4964
+ let unsubscribe = () => active = false;
4965
+ (async () => {
4966
+ try {
4967
+ const { unsubscribe: unsubscribe_ } = await client.transport.subscribe({
4968
+ params: ["newHeads"],
4969
+ onData(data) {
4970
+ if (!active)
4971
+ return;
4972
+ const block = data.result;
4973
+ onBlock(block, prevBlock);
4764
4974
  prevBlock = block;
4975
+ },
4976
+ onError(error) {
4977
+ onError?.(error);
4765
4978
  }
4766
- } catch (err) {
4767
- emit.onError?.(err);
4768
- }
4769
- },
4770
- {
4771
- emitOnBegin,
4772
- interval: pollingInterval
4979
+ });
4980
+ unsubscribe = unsubscribe_;
4981
+ if (!active)
4982
+ unsubscribe();
4983
+ } catch (err) {
4984
+ onError?.(err);
4773
4985
  }
4774
- )
4775
- );
4986
+ })();
4987
+ return unsubscribe;
4988
+ };
4989
+ return enablePolling ? pollBlocks() : subscribeBlocks();
4776
4990
  }
4777
4991
 
4778
4992
  // src/actions/public/watchContractEvent.ts
@@ -4943,50 +5157,81 @@ function watchPendingTransactions(client, {
4943
5157
  batch = true,
4944
5158
  onError,
4945
5159
  onTransactions,
5160
+ poll: poll_,
4946
5161
  pollingInterval = client.pollingInterval
4947
5162
  }) {
4948
- const observerId = JSON.stringify([
4949
- "watchPendingTransactions",
4950
- client.uid,
4951
- batch,
4952
- pollingInterval
4953
- ]);
4954
- return observe(observerId, { onTransactions, onError }, (emit) => {
4955
- let filter;
4956
- const unwatch = poll(
4957
- async () => {
4958
- try {
4959
- if (!filter) {
4960
- try {
4961
- filter = await createPendingTransactionFilter(client);
4962
- return;
4963
- } catch (err) {
4964
- unwatch();
4965
- throw err;
5163
+ const enablePolling = typeof poll_ !== "undefined" ? poll_ : client.transport.type !== "webSocket";
5164
+ const pollPendingTransactions = () => {
5165
+ const observerId = JSON.stringify([
5166
+ "watchPendingTransactions",
5167
+ client.uid,
5168
+ batch,
5169
+ pollingInterval
5170
+ ]);
5171
+ return observe(observerId, { onTransactions, onError }, (emit) => {
5172
+ let filter;
5173
+ const unwatch = poll(
5174
+ async () => {
5175
+ try {
5176
+ if (!filter) {
5177
+ try {
5178
+ filter = await createPendingTransactionFilter(client);
5179
+ return;
5180
+ } catch (err) {
5181
+ unwatch();
5182
+ throw err;
5183
+ }
4966
5184
  }
5185
+ const hashes = await getFilterChanges(client, { filter });
5186
+ if (hashes.length === 0)
5187
+ return;
5188
+ if (batch)
5189
+ emit.onTransactions(hashes);
5190
+ else
5191
+ hashes.forEach((hash2) => emit.onTransactions([hash2]));
5192
+ } catch (err) {
5193
+ emit.onError?.(err);
4967
5194
  }
4968
- const hashes = await getFilterChanges(client, { filter });
4969
- if (hashes.length === 0)
4970
- return;
4971
- if (batch)
4972
- emit.onTransactions(hashes);
4973
- else
4974
- hashes.forEach((hash2) => emit.onTransactions([hash2]));
4975
- } catch (err) {
4976
- emit.onError?.(err);
5195
+ },
5196
+ {
5197
+ emitOnBegin: true,
5198
+ interval: pollingInterval
4977
5199
  }
4978
- },
4979
- {
4980
- emitOnBegin: true,
4981
- interval: pollingInterval
5200
+ );
5201
+ return async () => {
5202
+ if (filter)
5203
+ await uninstallFilter(client, { filter });
5204
+ unwatch();
5205
+ };
5206
+ });
5207
+ };
5208
+ const subscribePendingTransactions = () => {
5209
+ let active = true;
5210
+ let unsubscribe = () => active = false;
5211
+ (async () => {
5212
+ try {
5213
+ const { unsubscribe: unsubscribe_ } = await client.transport.subscribe({
5214
+ params: ["newPendingTransactions"],
5215
+ onData(data) {
5216
+ if (!active)
5217
+ return;
5218
+ const transaction = data.result;
5219
+ onTransactions([transaction]);
5220
+ },
5221
+ onError(error) {
5222
+ onError?.(error);
5223
+ }
5224
+ });
5225
+ unsubscribe = unsubscribe_;
5226
+ if (!active)
5227
+ unsubscribe();
5228
+ } catch (err) {
5229
+ onError?.(err);
4982
5230
  }
4983
- );
4984
- return async () => {
4985
- if (filter)
4986
- await uninstallFilter(client, { filter });
4987
- unwatch();
4988
- };
4989
- });
5231
+ })();
5232
+ return unsubscribe;
5233
+ };
5234
+ return enablePolling ? pollPendingTransactions() : subscribePendingTransactions();
4990
5235
  }
4991
5236
 
4992
5237
  // src/actions/test/dropTransaction.ts
@@ -5377,7 +5622,7 @@ async function signMessage(client, {
5377
5622
  const account = parseAccount(account_);
5378
5623
  const message_ = message || data;
5379
5624
  if (account.type === "local")
5380
- return account.signMessage(message_);
5625
+ return account.signMessage({ message: message_ });
5381
5626
  return client.request({
5382
5627
  method: "personal_sign",
5383
5628
  params: [toHex(message_), account.address]
@@ -5432,47 +5677,6 @@ async function signTypedData(client, {
5432
5677
  params: [account.address, typedData]
5433
5678
  });
5434
5679
  }
5435
- function validateTypedData({
5436
- domain,
5437
- message,
5438
- primaryType,
5439
- types: types_
5440
- }) {
5441
- const types = types_;
5442
- const validateData = (struct, value_) => {
5443
- for (const param of struct) {
5444
- const { name, type: type_ } = param;
5445
- const type2 = type_;
5446
- const value = value_[name];
5447
- const integerMatch = type2.match(integerRegex);
5448
- if (integerMatch && (typeof value === "number" || typeof value === "bigint")) {
5449
- const [_type, base, size_] = integerMatch;
5450
- numberToHex(value, {
5451
- signed: base === "int",
5452
- size: parseInt(size_) / 8
5453
- });
5454
- }
5455
- if (type2 === "address" && typeof value === "string" && !isAddress(value))
5456
- throw new InvalidAddressError({ address: value });
5457
- const bytesMatch = type2.match(bytesRegex);
5458
- if (bytesMatch) {
5459
- const [_type, size_] = bytesMatch;
5460
- if (size_ && size(value) !== parseInt(size_))
5461
- throw new BytesSizeMismatchError({
5462
- expectedSize: parseInt(size_),
5463
- givenSize: size(value)
5464
- });
5465
- }
5466
- const struct2 = types[type2];
5467
- if (struct2)
5468
- validateData(struct2, value);
5469
- }
5470
- };
5471
- if (types["EIP712Domain"] && domain)
5472
- validateData(types["EIP712Domain"], domain);
5473
- const type = types[primaryType];
5474
- validateData(type, message);
5475
- }
5476
5680
 
5477
5681
  // src/actions/wallet/switchChain.ts
5478
5682
  async function switchChain(client, { id: id2 }) {
@@ -5593,6 +5797,400 @@ async function prepareRequest(client, args) {
5593
5797
  return request;
5594
5798
  }
5595
5799
 
5800
+ // src/utils/transaction/assertTransaction.ts
5801
+ function assertTransactionEIP1559(transaction) {
5802
+ const { chainId, maxPriorityFeePerGas, gasPrice, maxFeePerGas, to } = transaction;
5803
+ if (chainId <= 0)
5804
+ throw new InvalidChainIdError({ chainId });
5805
+ if (to && !isAddress(to))
5806
+ throw new InvalidAddressError({ address: to });
5807
+ if (gasPrice)
5808
+ throw new BaseError(
5809
+ "`gasPrice` is not a valid EIP-1559 Transaction attribute."
5810
+ );
5811
+ if (maxFeePerGas && maxFeePerGas > 2n ** 256n - 1n)
5812
+ throw new FeeCapTooHighError({ maxFeePerGas });
5813
+ if (maxPriorityFeePerGas && maxFeePerGas && maxPriorityFeePerGas > maxFeePerGas)
5814
+ throw new TipAboveFeeCapError({ maxFeePerGas, maxPriorityFeePerGas });
5815
+ }
5816
+ function assertTransactionEIP2930(transaction) {
5817
+ const { chainId, maxPriorityFeePerGas, gasPrice, maxFeePerGas, to } = transaction;
5818
+ if (chainId <= 0)
5819
+ throw new InvalidChainIdError({ chainId });
5820
+ if (to && !isAddress(to))
5821
+ throw new InvalidAddressError({ address: to });
5822
+ if (maxPriorityFeePerGas || maxFeePerGas)
5823
+ throw new BaseError(
5824
+ "`maxFeePerGas`/`maxPriorityFeePerGas` is not a valid EIP-2930 Transaction attribute."
5825
+ );
5826
+ if (gasPrice && gasPrice > 2n ** 256n - 1n)
5827
+ throw new FeeCapTooHighError({ maxFeePerGas: gasPrice });
5828
+ }
5829
+ function assertTransactionLegacy(transaction) {
5830
+ const {
5831
+ chainId,
5832
+ maxPriorityFeePerGas,
5833
+ gasPrice,
5834
+ maxFeePerGas,
5835
+ to,
5836
+ accessList
5837
+ } = transaction;
5838
+ if (to && !isAddress(to))
5839
+ throw new InvalidAddressError({ address: to });
5840
+ if (typeof chainId !== "undefined" && chainId <= 0)
5841
+ throw new InvalidChainIdError({ chainId });
5842
+ if (maxPriorityFeePerGas || maxFeePerGas)
5843
+ throw new BaseError(
5844
+ "`maxFeePerGas`/`maxPriorityFeePerGas` is not a valid Legacy Transaction attribute."
5845
+ );
5846
+ if (gasPrice && gasPrice > 2n ** 256n - 1n)
5847
+ throw new FeeCapTooHighError({ maxFeePerGas: gasPrice });
5848
+ if (accessList)
5849
+ throw new BaseError(
5850
+ "`accessList` is not a valid Legacy Transaction attribute."
5851
+ );
5852
+ }
5853
+
5854
+ // src/utils/transaction/parseTransaction.ts
5855
+ function parseTransaction(serializedTransaction) {
5856
+ const type = getSerializedTransactionType(serializedTransaction);
5857
+ if (type === "eip1559")
5858
+ return parseTransactionEIP1559(
5859
+ serializedTransaction
5860
+ );
5861
+ if (type === "eip2930")
5862
+ return parseTransactionEIP2930(
5863
+ serializedTransaction
5864
+ );
5865
+ return parseTransactionLegacy(
5866
+ serializedTransaction
5867
+ );
5868
+ }
5869
+ function parseTransactionEIP1559(serializedTransaction) {
5870
+ const transactionArray = fromRlp(
5871
+ `0x${serializedTransaction.slice(4)}`,
5872
+ "hex"
5873
+ );
5874
+ const [
5875
+ chainId,
5876
+ nonce,
5877
+ maxPriorityFeePerGas,
5878
+ maxFeePerGas,
5879
+ gas,
5880
+ to,
5881
+ value,
5882
+ data,
5883
+ accessList,
5884
+ v,
5885
+ r,
5886
+ s
5887
+ ] = transactionArray;
5888
+ if (!(transactionArray.length === 9 || transactionArray.length === 12))
5889
+ throw new InvalidSerializedTransactionError({
5890
+ attributes: {
5891
+ chainId,
5892
+ nonce,
5893
+ maxPriorityFeePerGas,
5894
+ maxFeePerGas,
5895
+ gas,
5896
+ to,
5897
+ value,
5898
+ data,
5899
+ accessList,
5900
+ ...transactionArray.length > 9 ? {
5901
+ v,
5902
+ r,
5903
+ s
5904
+ } : {}
5905
+ },
5906
+ serializedTransaction,
5907
+ type: "eip1559"
5908
+ });
5909
+ let transaction = {
5910
+ chainId: hexToNumber(chainId),
5911
+ type: "eip1559"
5912
+ };
5913
+ if (isHex(to) && to !== "0x")
5914
+ transaction.to = to;
5915
+ if (isHex(gas) && gas !== "0x")
5916
+ transaction.gas = hexToBigInt(gas);
5917
+ if (isHex(data) && data !== "0x")
5918
+ transaction.data = data;
5919
+ if (isHex(nonce) && nonce !== "0x")
5920
+ transaction.nonce = hexToNumber(nonce);
5921
+ if (isHex(value) && value !== "0x")
5922
+ transaction.value = hexToBigInt(value);
5923
+ if (isHex(maxFeePerGas) && maxFeePerGas !== "0x")
5924
+ transaction.maxFeePerGas = hexToBigInt(maxFeePerGas);
5925
+ if (isHex(maxPriorityFeePerGas) && maxPriorityFeePerGas !== "0x")
5926
+ transaction.maxPriorityFeePerGas = hexToBigInt(maxPriorityFeePerGas);
5927
+ if (accessList.length !== 0 && accessList !== "0x")
5928
+ transaction.accessList = parseAccessList(accessList);
5929
+ assertTransactionEIP1559(transaction);
5930
+ const signature = transactionArray.length === 12 ? parseEIP155Signature(transactionArray) : void 0;
5931
+ return { ...signature, ...transaction };
5932
+ }
5933
+ function parseTransactionEIP2930(serializedTransaction) {
5934
+ const transactionArray = fromRlp(
5935
+ `0x${serializedTransaction.slice(4)}`,
5936
+ "hex"
5937
+ );
5938
+ const [chainId, nonce, gasPrice, gas, to, value, data, accessList, v, r, s] = transactionArray;
5939
+ if (!(transactionArray.length === 8 || transactionArray.length === 11))
5940
+ throw new InvalidSerializedTransactionError({
5941
+ attributes: {
5942
+ chainId,
5943
+ nonce,
5944
+ gasPrice,
5945
+ gas,
5946
+ to,
5947
+ value,
5948
+ data,
5949
+ accessList,
5950
+ ...transactionArray.length > 8 ? {
5951
+ v,
5952
+ r,
5953
+ s
5954
+ } : {}
5955
+ },
5956
+ serializedTransaction,
5957
+ type: "eip2930"
5958
+ });
5959
+ let transaction = {
5960
+ chainId: hexToNumber(chainId),
5961
+ type: "eip2930"
5962
+ };
5963
+ if (isHex(to) && to !== "0x")
5964
+ transaction.to = to;
5965
+ if (isHex(gas) && gas !== "0x")
5966
+ transaction.gas = hexToBigInt(gas);
5967
+ if (isHex(data) && data !== "0x")
5968
+ transaction.data = data;
5969
+ if (isHex(nonce) && nonce !== "0x")
5970
+ transaction.nonce = hexToNumber(nonce);
5971
+ if (isHex(value) && value !== "0x")
5972
+ transaction.value = hexToBigInt(value);
5973
+ if (isHex(gasPrice) && gasPrice !== "0x")
5974
+ transaction.gasPrice = hexToBigInt(gasPrice);
5975
+ if (accessList.length !== 0 && accessList !== "0x")
5976
+ transaction.accessList = parseAccessList(accessList);
5977
+ assertTransactionEIP2930(transaction);
5978
+ const signature = transactionArray.length === 11 ? parseEIP155Signature(transactionArray) : void 0;
5979
+ return { ...signature, ...transaction };
5980
+ }
5981
+ function parseTransactionLegacy(serializedTransaction) {
5982
+ const transactionArray = fromRlp(serializedTransaction, "hex");
5983
+ const [nonce, gasPrice, gas, to, value, data, chainIdOrV_, r, s] = transactionArray;
5984
+ if (!(transactionArray.length === 6 || transactionArray.length === 9))
5985
+ throw new InvalidSerializedTransactionError({
5986
+ attributes: {
5987
+ nonce,
5988
+ gasPrice,
5989
+ gas,
5990
+ to,
5991
+ value,
5992
+ data,
5993
+ ...transactionArray.length > 6 ? {
5994
+ v: chainIdOrV_,
5995
+ r,
5996
+ s
5997
+ } : {}
5998
+ },
5999
+ serializedTransaction,
6000
+ type: "legacy"
6001
+ });
6002
+ let transaction = {
6003
+ type: "legacy"
6004
+ };
6005
+ if (isHex(to) && to !== "0x")
6006
+ transaction.to = to;
6007
+ if (isHex(gas) && gas !== "0x")
6008
+ transaction.gas = hexToBigInt(gas);
6009
+ if (isHex(data) && data !== "0x")
6010
+ transaction.data = data;
6011
+ if (isHex(nonce) && nonce !== "0x")
6012
+ transaction.nonce = hexToNumber(nonce);
6013
+ if (isHex(value) && value !== "0x")
6014
+ transaction.value = hexToBigInt(value);
6015
+ if (isHex(gasPrice) && gasPrice !== "0x")
6016
+ transaction.gasPrice = hexToBigInt(gasPrice);
6017
+ assertTransactionLegacy(transaction);
6018
+ if (transactionArray.length === 6)
6019
+ return transaction;
6020
+ const chainIdOrV = isHex(chainIdOrV_) && chainIdOrV_ !== "0x" ? hexToBigInt(chainIdOrV_) : 0n;
6021
+ if (s === "0x" && r === "0x") {
6022
+ if (chainIdOrV > 0)
6023
+ transaction.chainId = Number(chainIdOrV);
6024
+ return transaction;
6025
+ }
6026
+ const v = chainIdOrV;
6027
+ let chainId = Number((v - 35n) / 2n);
6028
+ if (chainId > 0)
6029
+ transaction.chainId = chainId;
6030
+ else if (v !== 27n && v !== 28n)
6031
+ throw new InvalidLegacyVError({ v });
6032
+ transaction.v = v;
6033
+ transaction.s = s;
6034
+ transaction.r = r;
6035
+ return transaction;
6036
+ }
6037
+ function parseAccessList(accessList_) {
6038
+ let accessList = [];
6039
+ for (let i = 0; i < accessList_.length; i++) {
6040
+ const [address, storageKeys] = accessList_[i];
6041
+ if (!isAddress(address))
6042
+ throw new InvalidAddressError({ address });
6043
+ accessList.push({
6044
+ address,
6045
+ storageKeys: storageKeys.map((key) => isHash(key) ? key : trim(key))
6046
+ });
6047
+ }
6048
+ return accessList;
6049
+ }
6050
+ function parseEIP155Signature(transactionArray) {
6051
+ const signature = transactionArray.slice(-3);
6052
+ const v = signature[0] === "0x" || hexToBigInt(signature[0]) === 0n ? 27n : 28n;
6053
+ return {
6054
+ r: padHex(signature[1], { size: 32 }),
6055
+ s: padHex(signature[2], { size: 32 }),
6056
+ v,
6057
+ yParity: v === 27n ? 0 : 1
6058
+ };
6059
+ }
6060
+
6061
+ // src/utils/transaction/serializeTransaction.ts
6062
+ function serializeTransaction(transaction, signature) {
6063
+ const type = getTransactionType(transaction);
6064
+ if (type === "eip1559")
6065
+ return serializeTransactionEIP1559(
6066
+ transaction,
6067
+ signature
6068
+ );
6069
+ if (type === "eip2930")
6070
+ return serializeTransactionEIP2930(
6071
+ transaction,
6072
+ signature
6073
+ );
6074
+ return serializeTransactionLegacy(
6075
+ transaction,
6076
+ signature
6077
+ );
6078
+ }
6079
+ function serializeTransactionEIP1559(transaction, signature) {
6080
+ const {
6081
+ chainId,
6082
+ gas,
6083
+ nonce,
6084
+ to,
6085
+ value,
6086
+ maxFeePerGas,
6087
+ maxPriorityFeePerGas,
6088
+ accessList,
6089
+ data
6090
+ } = transaction;
6091
+ assertTransactionEIP1559(transaction);
6092
+ const serializedAccessList = serializeAccessList(accessList);
6093
+ const serializedTransaction = [
6094
+ toHex(chainId),
6095
+ typeof nonce !== "undefined" ? toHex(nonce) : "0x",
6096
+ typeof maxPriorityFeePerGas !== "undefined" ? toHex(maxPriorityFeePerGas) : "0x",
6097
+ typeof maxFeePerGas !== "undefined" ? toHex(maxFeePerGas) : "0x",
6098
+ typeof gas !== "undefined" ? toHex(gas) : "0x",
6099
+ to ?? "0x",
6100
+ typeof value !== "undefined" ? toHex(value) : "0x",
6101
+ data ?? "0x",
6102
+ serializedAccessList
6103
+ ];
6104
+ if (signature)
6105
+ serializedTransaction.push(
6106
+ signature.v === 27n ? "0x" : toHex(1),
6107
+ // yParity
6108
+ signature.r,
6109
+ signature.s
6110
+ );
6111
+ return concatHex([
6112
+ "0x02",
6113
+ toRlp(serializedTransaction)
6114
+ ]);
6115
+ }
6116
+ function serializeTransactionEIP2930(transaction, signature) {
6117
+ const { chainId, gas, data, nonce, to, value, accessList, gasPrice } = transaction;
6118
+ assertTransactionEIP2930(transaction);
6119
+ const serializedAccessList = serializeAccessList(accessList);
6120
+ const serializedTransaction = [
6121
+ toHex(chainId),
6122
+ typeof nonce !== "undefined" ? toHex(nonce) : "0x",
6123
+ typeof gasPrice !== "undefined" ? toHex(gasPrice) : "0x",
6124
+ typeof gas !== "undefined" ? toHex(gas) : "0x",
6125
+ to ?? "0x",
6126
+ typeof value !== "undefined" ? toHex(value) : "0x",
6127
+ data ?? "0x",
6128
+ serializedAccessList
6129
+ ];
6130
+ if (signature)
6131
+ serializedTransaction.push(
6132
+ signature.v === 27n ? "0x" : toHex(1),
6133
+ // yParity
6134
+ signature.r,
6135
+ signature.s
6136
+ );
6137
+ return concatHex([
6138
+ "0x01",
6139
+ toRlp(serializedTransaction)
6140
+ ]);
6141
+ }
6142
+ function serializeTransactionLegacy(transaction, signature) {
6143
+ const { chainId = 0, gas, data, nonce, to, value, gasPrice } = transaction;
6144
+ assertTransactionLegacy(transaction);
6145
+ let serializedTransaction = [
6146
+ typeof nonce !== "undefined" ? toHex(nonce) : "0x",
6147
+ typeof gasPrice !== "undefined" ? toHex(gasPrice) : "0x",
6148
+ typeof gas !== "undefined" ? toHex(gas) : "0x",
6149
+ to ?? "0x",
6150
+ typeof value !== "undefined" ? toHex(value) : "0x",
6151
+ data ?? "0x"
6152
+ ];
6153
+ if (signature) {
6154
+ let v = 27n + (signature.v === 27n ? 0n : 1n);
6155
+ if (chainId > 0)
6156
+ v = BigInt(chainId * 2) + BigInt(35n + signature.v - 27n);
6157
+ else if (signature.v !== v)
6158
+ throw new InvalidLegacyVError({ v: signature.v });
6159
+ serializedTransaction = [
6160
+ ...serializedTransaction,
6161
+ toHex(v),
6162
+ signature.r,
6163
+ signature.s
6164
+ ];
6165
+ } else if (chainId > 0) {
6166
+ serializedTransaction = [
6167
+ ...serializedTransaction,
6168
+ toHex(chainId),
6169
+ "0x",
6170
+ "0x"
6171
+ ];
6172
+ }
6173
+ return toRlp(serializedTransaction);
6174
+ }
6175
+ function serializeAccessList(accessList) {
6176
+ if (!accessList || accessList.length === 0)
6177
+ return [];
6178
+ let serializedAccessList = [];
6179
+ for (let i = 0; i < accessList.length; i++) {
6180
+ const { address, storageKeys } = accessList[i];
6181
+ for (let j = 0; j < storageKeys.length; j++) {
6182
+ if (storageKeys[j].length - 2 !== 64) {
6183
+ throw new InvalidStorageKeySizeError({ storageKey: storageKeys[j] });
6184
+ }
6185
+ }
6186
+ if (!isAddress(address)) {
6187
+ throw new InvalidAddressError({ address });
6188
+ }
6189
+ serializedAccessList.push([address, storageKeys]);
6190
+ }
6191
+ return serializedAccessList;
6192
+ }
6193
+
5596
6194
  // src/utils/unit/formatUnits.ts
5597
6195
  function formatUnits(value, decimals) {
5598
6196
  let display = value.toString();
@@ -5648,11 +6246,13 @@ export {
5648
6246
  InvalidAddressError,
5649
6247
  BlockNotFoundError,
5650
6248
  ChainDoesNotSupportContract,
6249
+ InvalidChainIdError,
5651
6250
  multicall3Abi,
5652
6251
  panicReasons,
5653
6252
  etherUnits,
5654
6253
  gweiUnits,
5655
6254
  weiUnits,
6255
+ InvalidLegacyVError,
5656
6256
  TransactionExecutionError,
5657
6257
  TransactionNotFoundError,
5658
6258
  TransactionReceiptNotFoundError,
@@ -5747,7 +6347,9 @@ export {
5747
6347
  keccak256,
5748
6348
  getEventSelector,
5749
6349
  getFunctionSelector,
6350
+ isHash,
5750
6351
  isAddress,
6352
+ checksumAddress,
5751
6353
  getAddress,
5752
6354
  getContractAddress2 as getContractAddress,
5753
6355
  getCreateAddress,
@@ -5775,7 +6377,6 @@ export {
5775
6377
  parseAbiItem,
5776
6378
  parseAbiParameter,
5777
6379
  parseAbiParameters,
5778
- getAccount,
5779
6380
  parseAccount,
5780
6381
  isDeterministicError,
5781
6382
  buildRequest,
@@ -5802,6 +6403,7 @@ export {
5802
6403
  getSocket,
5803
6404
  rpc,
5804
6405
  hashMessage,
6406
+ validateTypedData,
5805
6407
  hashTypedData,
5806
6408
  recoverAddress,
5807
6409
  recoverMessageAddress,
@@ -5809,6 +6411,8 @@ export {
5809
6411
  verifyMessage,
5810
6412
  verifyTypedData,
5811
6413
  assertRequest,
6414
+ getSerializedTransactionType,
6415
+ getTransactionType,
5812
6416
  call,
5813
6417
  simulateContract,
5814
6418
  createPendingTransactionFilter,
@@ -5886,9 +6490,14 @@ export {
5886
6490
  parseUnits,
5887
6491
  parseGwei,
5888
6492
  prepareRequest,
6493
+ assertTransactionEIP1559,
6494
+ assertTransactionEIP2930,
6495
+ assertTransactionLegacy,
6496
+ parseTransaction,
6497
+ serializeTransaction,
5889
6498
  formatUnits,
5890
6499
  formatEther,
5891
6500
  formatGwei,
5892
6501
  parseEther
5893
6502
  };
5894
- //# sourceMappingURL=chunk-KOAJUBTN.mjs.map
6503
+ //# sourceMappingURL=chunk-HYEOQX7O.mjs.map