viem 1.18.8 → 1.19.0

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 (153) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/README.md +1 -1
  3. package/_cjs/actions/test/dumpState.js +10 -0
  4. package/_cjs/actions/test/dumpState.js.map +1 -0
  5. package/_cjs/actions/test/loadState.js +11 -0
  6. package/_cjs/actions/test/loadState.js.map +1 -0
  7. package/_cjs/chains/definitions/lukso.js +31 -0
  8. package/_cjs/chains/definitions/lukso.js.map +1 -0
  9. package/_cjs/chains/index.js +5 -3
  10. package/_cjs/chains/index.js.map +1 -1
  11. package/_cjs/clients/decorators/test.js +4 -0
  12. package/_cjs/clients/decorators/test.js.map +1 -1
  13. package/_cjs/constants/bytes.js +5 -0
  14. package/_cjs/constants/bytes.js.map +1 -0
  15. package/_cjs/constants/strings.js +5 -0
  16. package/_cjs/constants/strings.js.map +1 -0
  17. package/_cjs/errors/node.js +11 -11
  18. package/_cjs/errors/node.js.map +1 -1
  19. package/_cjs/errors/rpc.js +18 -18
  20. package/_cjs/errors/rpc.js.map +1 -1
  21. package/_cjs/index.js +10 -6
  22. package/_cjs/index.js.map +1 -1
  23. package/_cjs/utils/contract/extractFunctionParts.js.map +1 -1
  24. package/_cjs/utils/formatters/transaction.js +14 -0
  25. package/_cjs/utils/formatters/transaction.js.map +1 -1
  26. package/_cjs/utils/hash/getFunctionSignature.js +9 -9
  27. package/_cjs/utils/hash/getFunctionSignature.js.map +1 -1
  28. package/_cjs/utils/hash/normalizeSignature.js +48 -0
  29. package/_cjs/utils/hash/normalizeSignature.js.map +1 -0
  30. package/_cjs/utils/signature/hashMessage.js +2 -1
  31. package/_cjs/utils/signature/hashMessage.js.map +1 -1
  32. package/_cjs/utils/transaction/serializeTransaction.js +20 -4
  33. package/_cjs/utils/transaction/serializeTransaction.js.map +1 -1
  34. package/_esm/actions/test/dumpState.js +26 -0
  35. package/_esm/actions/test/dumpState.js.map +1 -0
  36. package/_esm/actions/test/loadState.js +27 -0
  37. package/_esm/actions/test/loadState.js.map +1 -0
  38. package/_esm/chains/definitions/lukso.js +28 -0
  39. package/_esm/chains/definitions/lukso.js.map +1 -0
  40. package/_esm/chains/index.js +1 -0
  41. package/_esm/chains/index.js.map +1 -1
  42. package/_esm/clients/decorators/test.js +4 -0
  43. package/_esm/clients/decorators/test.js.map +1 -1
  44. package/_esm/constants/bytes.js +2 -0
  45. package/_esm/constants/bytes.js.map +1 -0
  46. package/_esm/constants/strings.js +2 -0
  47. package/_esm/constants/strings.js.map +1 -0
  48. package/_esm/errors/node.js +22 -11
  49. package/_esm/errors/node.js.map +1 -1
  50. package/_esm/errors/rpc.js +36 -18
  51. package/_esm/errors/rpc.js.map +1 -1
  52. package/_esm/index.js +2 -0
  53. package/_esm/index.js.map +1 -1
  54. package/_esm/utils/contract/extractFunctionParts.js +4 -0
  55. package/_esm/utils/contract/extractFunctionParts.js.map +1 -1
  56. package/_esm/utils/formatters/transaction.js +16 -0
  57. package/_esm/utils/formatters/transaction.js.map +1 -1
  58. package/_esm/utils/hash/getFunctionSignature.js +9 -9
  59. package/_esm/utils/hash/getFunctionSignature.js.map +1 -1
  60. package/_esm/utils/hash/normalizeSignature.js +51 -0
  61. package/_esm/utils/hash/normalizeSignature.js.map +1 -0
  62. package/_esm/utils/signature/hashMessage.js +2 -1
  63. package/_esm/utils/signature/hashMessage.js.map +1 -1
  64. package/_esm/utils/transaction/serializeTransaction.js +20 -6
  65. package/_esm/utils/transaction/serializeTransaction.js.map +1 -1
  66. package/_types/actions/test/dumpState.d.ts +31 -0
  67. package/_types/actions/test/dumpState.d.ts.map +1 -0
  68. package/_types/actions/test/loadState.d.ts +34 -0
  69. package/_types/actions/test/loadState.d.ts.map +1 -0
  70. package/_types/chains/celo/formatters.d.ts +24 -13
  71. package/_types/chains/celo/formatters.d.ts.map +1 -1
  72. package/_types/chains/definitions/base.d.ts +18 -10
  73. package/_types/chains/definitions/base.d.ts.map +1 -1
  74. package/_types/chains/definitions/baseGoerli.d.ts +18 -10
  75. package/_types/chains/definitions/baseGoerli.d.ts.map +1 -1
  76. package/_types/chains/definitions/baseSepolia.d.ts +18 -10
  77. package/_types/chains/definitions/baseSepolia.d.ts.map +1 -1
  78. package/_types/chains/definitions/celo.d.ts +24 -13
  79. package/_types/chains/definitions/celo.d.ts.map +1 -1
  80. package/_types/chains/definitions/celoAlfajores.d.ts +24 -13
  81. package/_types/chains/definitions/celoAlfajores.d.ts.map +1 -1
  82. package/_types/chains/definitions/celoCannoli.d.ts +24 -13
  83. package/_types/chains/definitions/celoCannoli.d.ts.map +1 -1
  84. package/_types/chains/definitions/lukso.d.ts +27 -0
  85. package/_types/chains/definitions/lukso.d.ts.map +1 -0
  86. package/_types/chains/definitions/optimism.d.ts +18 -10
  87. package/_types/chains/definitions/optimism.d.ts.map +1 -1
  88. package/_types/chains/definitions/optimismGoerli.d.ts +18 -10
  89. package/_types/chains/definitions/optimismGoerli.d.ts.map +1 -1
  90. package/_types/chains/definitions/optimismSepolia.d.ts +18 -10
  91. package/_types/chains/definitions/optimismSepolia.d.ts.map +1 -1
  92. package/_types/chains/definitions/zkSync.d.ts +11 -0
  93. package/_types/chains/definitions/zkSync.d.ts.map +1 -1
  94. package/_types/chains/definitions/zkSyncTestnet.d.ts +11 -0
  95. package/_types/chains/definitions/zkSyncTestnet.d.ts.map +1 -1
  96. package/_types/chains/definitions/zora.d.ts +18 -10
  97. package/_types/chains/definitions/zora.d.ts.map +1 -1
  98. package/_types/chains/definitions/zoraSepolia.d.ts +18 -10
  99. package/_types/chains/definitions/zoraSepolia.d.ts.map +1 -1
  100. package/_types/chains/definitions/zoraTestnet.d.ts +18 -10
  101. package/_types/chains/definitions/zoraTestnet.d.ts.map +1 -1
  102. package/_types/chains/index.d.ts +1 -0
  103. package/_types/chains/index.d.ts.map +1 -1
  104. package/_types/chains/optimism/formatters.d.ts +18 -10
  105. package/_types/chains/optimism/formatters.d.ts.map +1 -1
  106. package/_types/chains/zksync/formatters.d.ts +11 -0
  107. package/_types/chains/zksync/formatters.d.ts.map +1 -1
  108. package/_types/clients/decorators/test.d.ts +41 -0
  109. package/_types/clients/decorators/test.d.ts.map +1 -1
  110. package/_types/constants/bytes.d.ts +2 -0
  111. package/_types/constants/bytes.d.ts.map +1 -0
  112. package/_types/constants/strings.d.ts +2 -0
  113. package/_types/constants/strings.d.ts.map +1 -0
  114. package/_types/index.d.ts +2 -0
  115. package/_types/index.d.ts.map +1 -1
  116. package/_types/types/eip1193.d.ts +16 -0
  117. package/_types/types/eip1193.d.ts.map +1 -1
  118. package/_types/types/rpc.d.ts +2 -2
  119. package/_types/types/rpc.d.ts.map +1 -1
  120. package/_types/types/transaction.d.ts +10 -1
  121. package/_types/types/transaction.d.ts.map +1 -1
  122. package/_types/types/utils.d.ts +8 -0
  123. package/_types/types/utils.d.ts.map +1 -1
  124. package/_types/utils/contract/extractFunctionParts.d.ts +4 -0
  125. package/_types/utils/contract/extractFunctionParts.d.ts.map +1 -1
  126. package/_types/utils/formatters/transaction.d.ts +1 -1
  127. package/_types/utils/formatters/transaction.d.ts.map +1 -1
  128. package/_types/utils/formatters/transactionRequest.d.ts +1 -1
  129. package/_types/utils/hash/getFunctionSignature.d.ts +4 -5
  130. package/_types/utils/hash/getFunctionSignature.d.ts.map +1 -1
  131. package/_types/utils/hash/normalizeSignature.d.ts +6 -0
  132. package/_types/utils/hash/normalizeSignature.d.ts.map +1 -0
  133. package/_types/utils/signature/hashMessage.d.ts.map +1 -1
  134. package/_types/utils/transaction/serializeTransaction.d.ts.map +1 -1
  135. package/actions/test/dumpState.ts +44 -0
  136. package/actions/test/loadState.ts +47 -0
  137. package/chains/definitions/lukso.ts +28 -0
  138. package/chains/index.ts +1 -0
  139. package/clients/decorators/test.ts +50 -0
  140. package/constants/bytes.ts +2 -0
  141. package/constants/strings.ts +1 -0
  142. package/index.ts +2 -0
  143. package/package.json +1 -1
  144. package/types/eip1193.ts +16 -0
  145. package/types/rpc.ts +15 -6
  146. package/types/transaction.ts +10 -1
  147. package/types/utils.ts +11 -0
  148. package/utils/contract/extractFunctionParts.ts +4 -0
  149. package/utils/formatters/transaction.ts +17 -2
  150. package/utils/hash/getFunctionSignature.ts +11 -21
  151. package/utils/hash/normalizeSignature.ts +64 -0
  152. package/utils/signature/hashMessage.ts +2 -1
  153. package/utils/transaction/serializeTransaction.ts +18 -12
@@ -0,0 +1,47 @@
1
+ import type {
2
+ TestClient,
3
+ TestClientMode,
4
+ } from '../../clients/createTestClient.js'
5
+ import type { Transport } from '../../clients/transports/createTransport.js'
6
+ import type { ErrorType } from '../../errors/utils.js'
7
+ import type { Account } from '../../types/account.js'
8
+ import type { Chain } from '../../types/chain.js'
9
+ import type { Hex } from '../../types/misc.js'
10
+ import type { RequestErrorType } from '../../utils/buildRequest.js'
11
+
12
+ export type LoadStateParameters = { state: Hex }
13
+ export type LoadStateReturnType = void
14
+ export type LoadStateErrorType = RequestErrorType | ErrorType
15
+
16
+ /**
17
+ * Adds state previously dumped with `dumpState` to the current chain.
18
+ *
19
+ * - Docs: https://viem.sh/docs/actions/test/loadState.html
20
+ *
21
+ * @param client - Client to use
22
+ * @param parameters - {@link LoadStateParameters}
23
+ *
24
+ * @example
25
+ * import { createTestClient, http } from 'viem'
26
+ * import { foundry } from 'viem/chains'
27
+ * import { loadState } from 'viem/test'
28
+ *
29
+ * const client = createTestClient({
30
+ * mode: 'anvil',
31
+ * chain: 'foundry',
32
+ * transport: http(),
33
+ * })
34
+ * await loadState(client, { state: '0x...' })
35
+ */
36
+ export async function loadState<
37
+ TChain extends Chain | undefined,
38
+ TAccount extends Account | undefined,
39
+ >(
40
+ client: TestClient<TestClientMode, Transport, TChain, TAccount, false>,
41
+ { state }: LoadStateParameters,
42
+ ): Promise<LoadStateReturnType> {
43
+ await client.request({
44
+ method: `${client.mode}_loadState`,
45
+ params: [state],
46
+ })
47
+ }
@@ -0,0 +1,28 @@
1
+ import { defineChain } from '../../utils/chain/defineChain.js'
2
+
3
+ export const lukso = /*#__PURE__*/ defineChain({
4
+ id: 42,
5
+ network: 'lukso',
6
+ name: 'LUKSO',
7
+ nativeCurrency: {
8
+ name: 'LUKSO',
9
+ symbol: 'LYX',
10
+ decimals: 18,
11
+ },
12
+ rpcUrls: {
13
+ default: {
14
+ http: ['https://rpc.mainnet.lukso.network'],
15
+ webSocket: ['wss://ws-rpc.mainnet.lukso.network'],
16
+ },
17
+ public: {
18
+ http: ['https://rpc.mainnet.lukso.network'],
19
+ webSocket: ['wss://ws-rpc.mainnet.lukso.network'],
20
+ },
21
+ },
22
+ blockExplorers: {
23
+ default: {
24
+ name: 'LUKSO Mainnet Explorer',
25
+ url: 'https://explorer.execution.mainnet.lukso.network',
26
+ },
27
+ },
28
+ })
package/chains/index.ts CHANGED
@@ -72,6 +72,7 @@ export { kromaSepolia } from './definitions/kromaSepolia.js'
72
72
  export { linea } from './definitions/linea.js'
73
73
  export { lineaTestnet } from './definitions/lineaTestnet.js'
74
74
  export { localhost } from './definitions/localhost.js'
75
+ export { lukso } from './definitions/lukso.js'
75
76
  export { mainnet } from './definitions/mainnet.js'
76
77
  export { manta } from './definitions/manta.js'
77
78
  export { mantaTestnet } from './definitions/mantaTestnet.js'
@@ -2,6 +2,10 @@ import {
2
2
  type DropTransactionParameters,
3
3
  dropTransaction,
4
4
  } from '../../actions/test/dropTransaction.js'
5
+ import {
6
+ type DumpStateReturnType,
7
+ dumpState,
8
+ } from '../../actions/test/dumpState.js'
5
9
  import {
6
10
  type GetAutomineReturnType,
7
11
  getAutomine,
@@ -26,6 +30,11 @@ import {
26
30
  type InspectTxpoolReturnType,
27
31
  inspectTxpool,
28
32
  } from '../../actions/test/inspectTxpool.js'
33
+ import {
34
+ type LoadStateParameters,
35
+ type LoadStateReturnType,
36
+ loadState,
37
+ } from '../../actions/test/loadState.js'
29
38
  import { type MineParameters, mine } from '../../actions/test/mine.js'
30
39
  import { removeBlockTimestampInterval } from '../../actions/test/removeBlockTimestampInterval.js'
31
40
  import { type ResetParameters, reset } from '../../actions/test/reset.js'
@@ -113,6 +122,26 @@ export type TestActions = {
113
122
  * })
114
123
  */
115
124
  dropTransaction: (args: DropTransactionParameters) => Promise<void>
125
+ /**
126
+ * Serializes the current state (including contracts code, contract's storage,
127
+ * accounts properties, etc.) into a savable data blob.
128
+ *
129
+ * - Docs: https://viem.sh/docs/actions/test/dumpState.html
130
+ *
131
+ * @param client - Client to use
132
+ *
133
+ * @example
134
+ * import { createTestClient, http } from 'viem'
135
+ * import { foundry } from 'viem/chains'
136
+ *
137
+ * const client = createTestClient({
138
+ * mode: 'anvil',
139
+ * chain: 'foundry',
140
+ * transport: http(),
141
+ * })
142
+ * await client.dumpState()
143
+ */
144
+ dumpState: () => Promise<DumpStateReturnType>
116
145
  /**
117
146
  * Returns the automatic mining status of the node.
118
147
  *
@@ -231,6 +260,25 @@ export type TestActions = {
231
260
  * const data = await client.inspectTxpool()
232
261
  */
233
262
  inspectTxpool: () => Promise<InspectTxpoolReturnType>
263
+ /**
264
+ * Adds state previously dumped with `dumpState` to the current chain.
265
+ *
266
+ * - Docs: https://viem.sh/docs/actions/test/loadState.html
267
+ *
268
+ * @param client - Client to use
269
+ *
270
+ * @example
271
+ * import { createTestClient, http } from 'viem'
272
+ * import { foundry } from 'viem/chains'
273
+ *
274
+ * const client = createTestClient({
275
+ * mode: 'anvil',
276
+ * chain: 'foundry',
277
+ * transport: http(),
278
+ * })
279
+ * await client.loadState({ state: '0x...' })
280
+ */
281
+ loadState: (args: LoadStateParameters) => Promise<LoadStateReturnType>
234
282
  /**
235
283
  * Mine a specified number of blocks.
236
284
  *
@@ -689,12 +737,14 @@ export function testActions<TMode extends TestClientMode>({
689
737
  }))
690
738
  return {
691
739
  dropTransaction: (args) => dropTransaction(client, args),
740
+ dumpState: () => dumpState(client),
692
741
  getAutomine: () => getAutomine(client),
693
742
  getTxpoolContent: () => getTxpoolContent(client),
694
743
  getTxpoolStatus: () => getTxpoolStatus(client),
695
744
  impersonateAccount: (args) => impersonateAccount(client, args),
696
745
  increaseTime: (args) => increaseTime(client, args),
697
746
  inspectTxpool: () => inspectTxpool(client),
747
+ loadState: (args) => loadState(client, args),
698
748
  mine: (args) => mine(client, args),
699
749
  removeBlockTimestampInterval: () => removeBlockTimestampInterval(client),
700
750
  reset: (args) => reset(client, args),
@@ -0,0 +1,2 @@
1
+ export const zeroHash =
2
+ '0x0000000000000000000000000000000000000000000000000000000000000000' as const
@@ -0,0 +1 @@
1
+ export const presignMessagePrefix = '\x19Ethereum Signed Message:\n'
package/index.ts CHANGED
@@ -582,6 +582,8 @@ export {
582
582
  minInt248,
583
583
  minInt256,
584
584
  } from './constants/number.js'
585
+ export { zeroHash } from './constants/bytes.js'
586
+ export { presignMessagePrefix } from './constants/strings.js'
585
587
  export {
586
588
  AbiConstructorNotFoundError,
587
589
  type AbiConstructorNotFoundErrorType,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "viem",
3
3
  "description": "TypeScript Interface for Ethereum",
4
- "version": "1.18.8",
4
+ "version": "1.19.0",
5
5
  "main": "./_cjs/index.js",
6
6
  "module": "./_esm/index.js",
7
7
  "types": "./_types/index.d.ts",
package/types/eip1193.ts CHANGED
@@ -707,6 +707,14 @@ export type TestRpcSchema<TMode extends string> = [
707
707
  Parameters: [hash: Hash]
708
708
  ReturnType: void
709
709
  },
710
+ /**
711
+ * @description Serializes the current state (including contracts code, contract's storage, accounts properties, etc.) into a savable data blob.
712
+ */
713
+ {
714
+ Method: `${TMode}_dumpState`
715
+ Parameters?: undefined
716
+ ReturnType: Hex
717
+ },
710
718
  /**
711
719
  * @description Turn on call traces for transactions that are returned to the user when they execute a transaction (instead of just txhash/receipt).
712
720
  */
@@ -733,6 +741,14 @@ export type TestRpcSchema<TMode extends string> = [
733
741
  Parameters?: undefined
734
742
  ReturnType: boolean
735
743
  },
744
+ /**
745
+ * @description Adds state previously dumped with `dumpState` to the current chain.
746
+ */
747
+ {
748
+ Method: `${TMode}_loadState`
749
+ Parameters?: [Hex]
750
+ ReturnType: void
751
+ },
736
752
  /**
737
753
  * @description Advance the block number of the network by a certain number of blocks
738
754
  * @link https://hardhat.org/hardhat-network/docs/reference#hardhat_mine
package/types/rpc.ts CHANGED
@@ -17,7 +17,7 @@ import type {
17
17
  TransactionRequestEIP2930,
18
18
  TransactionRequestLegacy,
19
19
  } from './transaction.js'
20
- import type { UnionOmit } from './utils.js'
20
+ import type { UnionOmit, UnionPartialBy } from './utils.js'
21
21
 
22
22
  export type Index = `0x${string}`
23
23
  export type Quantity = `0x${string}`
@@ -46,9 +46,18 @@ export type RpcTransactionRequest =
46
46
  | TransactionRequestLegacy<Quantity, Index, '0x0'>
47
47
  | TransactionRequestEIP2930<Quantity, Index, '0x1'>
48
48
  | TransactionRequestEIP1559<Quantity, Index, '0x2'>
49
- export type RpcTransaction<TPending extends boolean = boolean> = UnionOmit<
50
- | TransactionLegacy<Quantity, Index, TPending, '0x0'>
51
- | TransactionEIP2930<Quantity, Index, TPending, '0x1'>
52
- | TransactionEIP1559<Quantity, Index, TPending, '0x2'>,
53
- 'typeHex'
49
+ export type RpcTransaction<
50
+ TPending extends boolean = boolean,
51
+ // `yParity` is optional on the RPC type as some nodes do not return it
52
+ // for 1559 & 2930 transactions (they should!).
53
+ _optionalProperties extends 'yParity' = 'yParity',
54
+ _excludedProperties extends string = 'typeHex',
55
+ > = UnionOmit<
56
+ UnionPartialBy<
57
+ | TransactionLegacy<Quantity, Index, TPending, '0x0'>
58
+ | TransactionEIP2930<Quantity, Index, TPending, '0x1'>
59
+ | TransactionEIP1559<Quantity, Index, TPending, '0x2'>,
60
+ _optionalProperties
61
+ >,
62
+ _excludedProperties
54
63
  >
@@ -80,16 +80,21 @@ export type TransactionBase<
80
80
  v: TQuantity
81
81
  /** Value in wei sent with this transaction */
82
82
  value: TQuantity
83
+ /** The parity of the y-value of the secp256k1 signature. */
84
+ yParity: TIndex
83
85
  }
84
86
  export type TransactionLegacy<
85
87
  TQuantity = bigint,
86
88
  TIndex = number,
87
89
  TPending extends boolean = boolean,
88
90
  TType = 'legacy',
89
- > = TransactionBase<TQuantity, TIndex, TPending> &
91
+ > = Omit<TransactionBase<TQuantity, TIndex, TPending>, 'yParity'> &
90
92
  FeeValuesLegacy<TQuantity> & {
93
+ /** EIP-2930 Access List. */
91
94
  accessList?: never
95
+ /** Chain ID that this transaction is valid on. */
92
96
  chainId?: TIndex
97
+ yParity?: never
93
98
  type: TType
94
99
  }
95
100
  export type TransactionEIP2930<
@@ -99,7 +104,9 @@ export type TransactionEIP2930<
99
104
  TType = 'eip2930',
100
105
  > = TransactionBase<TQuantity, TIndex, TPending> &
101
106
  FeeValuesLegacy<TQuantity> & {
107
+ /** EIP-2930 Access List. */
102
108
  accessList: AccessList
109
+ /** Chain ID that this transaction is valid on. */
103
110
  chainId: TIndex
104
111
  type: TType
105
112
  }
@@ -110,7 +117,9 @@ export type TransactionEIP1559<
110
117
  TType = 'eip1559',
111
118
  > = TransactionBase<TQuantity, TIndex, TPending> &
112
119
  FeeValuesEIP1559<TQuantity> & {
120
+ /** EIP-2930 Access List. */
113
121
  accessList: AccessList
122
+ /** Chain ID that this transaction is valid on. */
114
123
  chainId: TIndex
115
124
  type: TType
116
125
  }
package/types/utils.ts CHANGED
@@ -194,6 +194,17 @@ export type UnionOmit<T, K extends keyof any> = T extends any
194
194
  */
195
195
  export type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>
196
196
 
197
+ /**
198
+ * @description Creates a type that is a partial of T, but with the required keys K.
199
+ *
200
+ * @example
201
+ * PartialBy<{ a: string, b: number } | { a: string, b: undefined, c: number }, 'a'>
202
+ * => { a?: string, b: number } | { a?: string, b: undefined, c: number }
203
+ */
204
+ export type UnionPartialBy<T, K extends keyof T> = T extends any
205
+ ? PartialBy<T, K>
206
+ : never
207
+
197
208
  /**
198
209
  * @description Combines members of an intersection into a readable type.
199
210
  *
@@ -4,6 +4,7 @@ const paramsRegex = /((function|event)\s)?(.*)(\((.*)\))/
4
4
 
5
5
  export type ExtractFunctionPartsErrorType = ErrorType
6
6
 
7
+ /** @deprecated – use `parseAbiItem` from `abitype`. */
7
8
  export function extractFunctionParts(def: string) {
8
9
  const parts = def.match(paramsRegex)
9
10
  const type = parts?.[2] || undefined
@@ -14,12 +15,14 @@ export function extractFunctionParts(def: string) {
14
15
 
15
16
  export type ExtractFunctionNameErrorType = ErrorType
16
17
 
18
+ /** @deprecated – use `parseAbiItem` from `abitype`. */
17
19
  export function extractFunctionName(def: string) {
18
20
  return extractFunctionParts(def).name
19
21
  }
20
22
 
21
23
  export type ExtractFunctionParamsErrorType = ErrorType
22
24
 
25
+ /** @deprecated – use `parseAbiItem` from `abitype`. */
23
26
  export function extractFunctionParams(def: string) {
24
27
  const params = extractFunctionParts(def).params
25
28
  const splitParams = params?.split(',').map((x) => x.trim().split(' '))
@@ -32,6 +35,7 @@ export function extractFunctionParams(def: string) {
32
35
 
33
36
  export type ExtractFunctionTypeErrorType = ErrorType
34
37
 
38
+ /** @deprecated – use `parseAbiItem` from `abitype`. */
35
39
  export function extractFunctionType(def: string) {
36
40
  return extractFunctionParts(def).type
37
41
  }
@@ -68,18 +68,33 @@ export function formatTransaction(transaction: Partial<RpcTransaction>) {
68
68
  typeHex: transaction.type ? transaction.type : undefined,
69
69
  value: transaction.value ? BigInt(transaction.value) : undefined,
70
70
  v: transaction.v ? BigInt(transaction.v) : undefined,
71
- }
71
+ } as Transaction
72
+
73
+ transaction_.yParity = (() => {
74
+ // If `yParity` is provided, we will use it.
75
+ if (transaction.yParity) return Number(transaction.yParity)
76
+
77
+ // If no `yParity` provided, try derive from `v`.
78
+ if (typeof transaction_.v === 'bigint') {
79
+ if (transaction_.v === 0n || transaction_.v === 27n) return 0
80
+ if (transaction_.v === 1n || transaction_.v === 28n) return 1
81
+ if (transaction_.v >= 35n) return transaction_.v % 2n === 0n ? 1 : 0
82
+ }
83
+
84
+ return undefined
85
+ })()
72
86
 
73
87
  if (transaction_.type === 'legacy') {
74
88
  delete transaction_.accessList
75
89
  delete transaction_.maxFeePerGas
76
90
  delete transaction_.maxPriorityFeePerGas
91
+ delete transaction_.yParity
77
92
  }
78
93
  if (transaction_.type === 'eip2930') {
79
94
  delete transaction_.maxFeePerGas
80
95
  delete transaction_.maxPriorityFeePerGas
81
96
  }
82
- return transaction_ as Transaction
97
+ return transaction_
83
98
  }
84
99
 
85
100
  export type DefineTransactionErrorType = DefineFormatterErrorType | ErrorType
@@ -1,29 +1,19 @@
1
- import type { AbiFunction } from 'abitype'
1
+ import { type AbiFunction, formatAbiItem } from 'abitype'
2
2
 
3
3
  import type { ErrorType } from '../../errors/utils.js'
4
4
  import {
5
- type FormatAbiItemErrorType,
6
- formatAbiItem,
7
- } from '../abi/formatAbiItem.js'
8
- import {
9
- type ExtractFunctionNameErrorType,
10
- type ExtractFunctionParamsErrorType,
11
- extractFunctionName,
12
- extractFunctionParams,
13
- } from '../contract/extractFunctionParts.js'
5
+ type NormalizeSignatureErrorType,
6
+ normalizeSignature,
7
+ } from './normalizeSignature.js'
14
8
 
15
9
  export type GetFunctionSignatureErrorType =
16
- | ExtractFunctionNameErrorType
17
- | ExtractFunctionParamsErrorType
18
- | FormatAbiItemErrorType
10
+ | NormalizeSignatureErrorType
19
11
  | ErrorType
20
12
 
21
- export const getFunctionSignature = (fn: string | AbiFunction) => {
22
- if (typeof fn === 'string') {
23
- const name = extractFunctionName(fn)
24
- const params = extractFunctionParams(fn) || []
25
- return `${name}(${params.map(({ type }) => type).join(',')})`
26
- }
27
-
28
- return formatAbiItem(fn)
13
+ export const getFunctionSignature = (fn_: string | AbiFunction) => {
14
+ const fn = (() => {
15
+ if (typeof fn_ === 'string') return fn_
16
+ return formatAbiItem(fn_)
17
+ })()
18
+ return normalizeSignature(fn)
29
19
  }
@@ -0,0 +1,64 @@
1
+ import { BaseError } from '../../errors/base.js'
2
+ import type { ErrorType } from '../../errors/utils.js'
3
+
4
+ export type NormalizeSignatureParameters = string
5
+ export type NormalizeSignatureReturnType = string
6
+ export type NormalizeSignatureErrorType = ErrorType
7
+
8
+ export function normalizeSignature(
9
+ signature: NormalizeSignatureParameters,
10
+ ): NormalizeSignatureReturnType {
11
+ let active = true
12
+ let current = ''
13
+ let level = 0
14
+ let result = ''
15
+ let valid = false
16
+
17
+ for (let i = 0; i < signature.length; i++) {
18
+ const char = signature[i]
19
+
20
+ // If the character is a separator, we want to reactivate.
21
+ if (['(', ')', ','].includes(char)) active = true
22
+
23
+ // If the character is a "level" token, we want to increment/decrement.
24
+ if (char === '(') level++
25
+ if (char === ')') level--
26
+
27
+ // If we aren't active, we don't want to mutate the result.
28
+ if (!active) continue
29
+
30
+ // If level === 0, we are at the definition level.
31
+ if (level === 0) {
32
+ if (char === ' ' && ['event', 'function', ''].includes(result))
33
+ result = ''
34
+ else {
35
+ result += char
36
+
37
+ // If we are at the end of the definition, we must be finished.
38
+ if (char === ')') {
39
+ valid = true
40
+ break
41
+ }
42
+ }
43
+
44
+ continue
45
+ }
46
+
47
+ // Ignore spaces
48
+ if (char === ' ') {
49
+ // If the previous character is a separator, and the current section isn't empty, we want to deactivate.
50
+ if (signature[i - 1] !== ',' && current !== ',' && current !== ',(') {
51
+ current = ''
52
+ active = false
53
+ }
54
+ continue
55
+ }
56
+
57
+ result += char
58
+ current += char
59
+ }
60
+
61
+ if (!valid) throw new BaseError('Unable to normalize signature.')
62
+
63
+ return result
64
+ }
@@ -1,3 +1,4 @@
1
+ import { presignMessagePrefix } from '../../constants/strings.js'
1
2
  import type { ErrorType } from '../../errors/utils.js'
2
3
  import type { ByteArray, Hex, SignableMessage } from '../../types/misc.js'
3
4
  import { type ConcatErrorType, concat } from '../data/concat.js'
@@ -32,7 +33,7 @@ export function hashMessage<TTo extends To = 'hex'>(
32
33
  return toBytes(message.raw)
33
34
  })()
34
35
  const prefixBytes = stringToBytes(
35
- `\x19Ethereum Signed Message:\n${messageBytes.length}`,
36
+ `${presignMessagePrefix}${messageBytes.length}`,
36
37
  )
37
38
  return keccak256(concat([prefixBytes, messageBytes]), to_)
38
39
  }
@@ -121,12 +121,15 @@ function serializeTransactionEIP1559(
121
121
  serializedAccessList,
122
122
  ]
123
123
 
124
- if (signature)
125
- serializedTransaction.push(
126
- signature.v === 27n ? '0x' : toHex(1), // yParity
127
- trim(signature.r),
128
- trim(signature.s),
129
- )
124
+ if (signature) {
125
+ const yParity = (() => {
126
+ if (signature.v === 0n) return '0x'
127
+ if (signature.v === 1n) return toHex(1)
128
+
129
+ return signature.v === 27n ? '0x' : toHex(1)
130
+ })()
131
+ serializedTransaction.push(yParity, trim(signature.r), trim(signature.s))
132
+ }
130
133
 
131
134
  return concatHex([
132
135
  '0x02',
@@ -165,12 +168,15 @@ function serializeTransactionEIP2930(
165
168
  serializedAccessList,
166
169
  ]
167
170
 
168
- if (signature)
169
- serializedTransaction.push(
170
- signature.v === 27n ? '0x' : toHex(1), // yParity
171
- signature.r,
172
- signature.s,
173
- )
171
+ if (signature) {
172
+ const yParity = (() => {
173
+ if (signature.v === 0n) return '0x'
174
+ if (signature.v === 1n) return toHex(1)
175
+
176
+ return signature.v === 27n ? '0x' : toHex(1)
177
+ })()
178
+ serializedTransaction.push(yParity, trim(signature.r), trim(signature.s))
179
+ }
174
180
 
175
181
  return concatHex([
176
182
  '0x01',