ox 0.14.66 → 0.14.68

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 (242) hide show
  1. package/CHANGELOG.md +42 -0
  2. package/_cjs/core/AbiEvent.js +45 -1
  3. package/_cjs/core/AbiEvent.js.map +1 -1
  4. package/_cjs/core/AbiParameters.js +6 -2
  5. package/_cjs/core/AbiParameters.js.map +1 -1
  6. package/_cjs/core/Address.js +24 -21
  7. package/_cjs/core/Address.js.map +1 -1
  8. package/_cjs/core/Base58.js +14 -4
  9. package/_cjs/core/Base58.js.map +1 -1
  10. package/_cjs/core/Blobs.js +26 -34
  11. package/_cjs/core/Blobs.js.map +1 -1
  12. package/_cjs/core/Bytes.js +56 -13
  13. package/_cjs/core/Bytes.js.map +1 -1
  14. package/_cjs/core/Cbor.js +33 -18
  15. package/_cjs/core/Cbor.js.map +1 -1
  16. package/_cjs/core/FrameReceipt.js +6 -2
  17. package/_cjs/core/FrameReceipt.js.map +1 -1
  18. package/_cjs/core/Hex.js +54 -21
  19. package/_cjs/core/Hex.js.map +1 -1
  20. package/_cjs/core/Json.js +15 -2
  21. package/_cjs/core/Json.js.map +1 -1
  22. package/_cjs/core/Rlp.js +138 -94
  23. package/_cjs/core/Rlp.js.map +1 -1
  24. package/_cjs/core/RpcTransport.js +3 -1
  25. package/_cjs/core/RpcTransport.js.map +1 -1
  26. package/_cjs/core/Siwe.js +2 -2
  27. package/_cjs/core/Siwe.js.map +1 -1
  28. package/_cjs/core/Transaction.js +6 -3
  29. package/_cjs/core/Transaction.js.map +1 -1
  30. package/_cjs/core/TransactionReceipt.js +12 -4
  31. package/_cjs/core/TransactionReceipt.js.map +1 -1
  32. package/_cjs/core/TransactionRequest.js +6 -2
  33. package/_cjs/core/TransactionRequest.js.map +1 -1
  34. package/_cjs/core/TxEnvelopeEip4844.js +1 -1
  35. package/_cjs/core/TxEnvelopeEip4844.js.map +1 -1
  36. package/_cjs/core/TypedData.js +19 -8
  37. package/_cjs/core/TypedData.js.map +1 -1
  38. package/_cjs/core/internal/abiParameters.js +59 -18
  39. package/_cjs/core/internal/abiParameters.js.map +1 -1
  40. package/_cjs/core/internal/address.js +32 -0
  41. package/_cjs/core/internal/address.js.map +1 -0
  42. package/_cjs/core/internal/base58.js +11 -9
  43. package/_cjs/core/internal/base58.js.map +1 -1
  44. package/_cjs/core/internal/bytes.js +13 -22
  45. package/_cjs/core/internal/bytes.js.map +1 -1
  46. package/_cjs/core/internal/caches.js +1 -0
  47. package/_cjs/core/internal/caches.js.map +1 -1
  48. package/_cjs/core/internal/cursor.js +32 -10
  49. package/_cjs/core/internal/cursor.js.map +1 -1
  50. package/_cjs/core/internal/hex.js +3 -3
  51. package/_cjs/core/internal/hex.js.map +1 -1
  52. package/_cjs/core/internal/lru.js +6 -5
  53. package/_cjs/core/internal/lru.js.map +1 -1
  54. package/_cjs/core/internal/promise.js +3 -3
  55. package/_cjs/core/internal/promise.js.map +1 -1
  56. package/_cjs/erc6492/SignatureErc6492.js +1 -1
  57. package/_cjs/erc6492/SignatureErc6492.js.map +1 -1
  58. package/_cjs/tempo/SignatureEnvelope.js +7 -38
  59. package/_cjs/tempo/SignatureEnvelope.js.map +1 -1
  60. package/_cjs/tempo/Transaction.js +13 -7
  61. package/_cjs/tempo/Transaction.js.map +1 -1
  62. package/_cjs/tempo/TransactionRequest.js +23 -19
  63. package/_cjs/tempo/TransactionRequest.js.map +1 -1
  64. package/_cjs/tempo/TxEnvelopeTempo.js +23 -4
  65. package/_cjs/tempo/TxEnvelopeTempo.js.map +1 -1
  66. package/_cjs/tempo/ZoneId.js +3 -0
  67. package/_cjs/tempo/ZoneId.js.map +1 -1
  68. package/_cjs/tempo/internal/virtualMasterPool.js +12 -3
  69. package/_cjs/tempo/internal/virtualMasterPool.js.map +1 -1
  70. package/_cjs/tempo/internal/webauthnData.js +57 -0
  71. package/_cjs/tempo/internal/webauthnData.js.map +1 -0
  72. package/_cjs/version.js +1 -1
  73. package/_esm/core/AbiEvent.js +69 -0
  74. package/_esm/core/AbiEvent.js.map +1 -1
  75. package/_esm/core/AbiParameters.js +8 -2
  76. package/_esm/core/AbiParameters.js.map +1 -1
  77. package/_esm/core/Address.js +27 -21
  78. package/_esm/core/Address.js.map +1 -1
  79. package/_esm/core/Base58.js +16 -4
  80. package/_esm/core/Base58.js.map +1 -1
  81. package/_esm/core/Blobs.js +38 -39
  82. package/_esm/core/Blobs.js.map +1 -1
  83. package/_esm/core/Bytes.js +66 -13
  84. package/_esm/core/Bytes.js.map +1 -1
  85. package/_esm/core/Cbor.js +33 -17
  86. package/_esm/core/Cbor.js.map +1 -1
  87. package/_esm/core/FrameReceipt.js +6 -2
  88. package/_esm/core/FrameReceipt.js.map +1 -1
  89. package/_esm/core/Hex.js +70 -21
  90. package/_esm/core/Hex.js.map +1 -1
  91. package/_esm/core/Json.js +21 -2
  92. package/_esm/core/Json.js.map +1 -1
  93. package/_esm/core/Rlp.js +146 -97
  94. package/_esm/core/Rlp.js.map +1 -1
  95. package/_esm/core/RpcTransport.js +4 -1
  96. package/_esm/core/RpcTransport.js.map +1 -1
  97. package/_esm/core/Siwe.js +2 -2
  98. package/_esm/core/Siwe.js.map +1 -1
  99. package/_esm/core/Transaction.js +6 -3
  100. package/_esm/core/Transaction.js.map +1 -1
  101. package/_esm/core/TransactionReceipt.js +12 -4
  102. package/_esm/core/TransactionReceipt.js.map +1 -1
  103. package/_esm/core/TransactionRequest.js +6 -2
  104. package/_esm/core/TransactionRequest.js.map +1 -1
  105. package/_esm/core/TxEnvelopeEip4844.js +1 -1
  106. package/_esm/core/TxEnvelopeEip4844.js.map +1 -1
  107. package/_esm/core/TypedData.js +24 -8
  108. package/_esm/core/TypedData.js.map +1 -1
  109. package/_esm/core/internal/abiParameters.js +78 -18
  110. package/_esm/core/internal/abiParameters.js.map +1 -1
  111. package/_esm/core/internal/address.js +41 -0
  112. package/_esm/core/internal/address.js.map +1 -0
  113. package/_esm/core/internal/base58.js +15 -9
  114. package/_esm/core/internal/base58.js.map +1 -1
  115. package/_esm/core/internal/bytes.js +14 -22
  116. package/_esm/core/internal/bytes.js.map +1 -1
  117. package/_esm/core/internal/caches.js +1 -0
  118. package/_esm/core/internal/caches.js.map +1 -1
  119. package/_esm/core/internal/cursor.js +37 -10
  120. package/_esm/core/internal/cursor.js.map +1 -1
  121. package/_esm/core/internal/hex.js +5 -3
  122. package/_esm/core/internal/hex.js.map +1 -1
  123. package/_esm/core/internal/lru.js +7 -5
  124. package/_esm/core/internal/lru.js.map +1 -1
  125. package/_esm/core/internal/promise.js +4 -3
  126. package/_esm/core/internal/promise.js.map +1 -1
  127. package/_esm/erc6492/SignatureErc6492.js +1 -1
  128. package/_esm/erc6492/SignatureErc6492.js.map +1 -1
  129. package/_esm/tempo/SignatureEnvelope.js +7 -41
  130. package/_esm/tempo/SignatureEnvelope.js.map +1 -1
  131. package/_esm/tempo/Transaction.js +15 -7
  132. package/_esm/tempo/Transaction.js.map +1 -1
  133. package/_esm/tempo/TransactionRequest.js +25 -19
  134. package/_esm/tempo/TransactionRequest.js.map +1 -1
  135. package/_esm/tempo/TxEnvelopeTempo.js +30 -4
  136. package/_esm/tempo/TxEnvelopeTempo.js.map +1 -1
  137. package/_esm/tempo/ZoneId.js +7 -1
  138. package/_esm/tempo/ZoneId.js.map +1 -1
  139. package/_esm/tempo/internal/virtualMasterPool.js +16 -3
  140. package/_esm/tempo/internal/virtualMasterPool.js.map +1 -1
  141. package/_esm/tempo/internal/webauthnData.js +79 -0
  142. package/_esm/tempo/internal/webauthnData.js.map +1 -0
  143. package/_esm/version.js +1 -1
  144. package/_types/core/AbiEvent.d.ts +36 -1
  145. package/_types/core/AbiEvent.d.ts.map +1 -1
  146. package/_types/core/AbiParameters.d.ts +1 -1
  147. package/_types/core/AbiParameters.d.ts.map +1 -1
  148. package/_types/core/Address.d.ts.map +1 -1
  149. package/_types/core/Base58.d.ts.map +1 -1
  150. package/_types/core/Blobs.d.ts +2 -3
  151. package/_types/core/Blobs.d.ts.map +1 -1
  152. package/_types/core/Bytes.d.ts +2 -0
  153. package/_types/core/Bytes.d.ts.map +1 -1
  154. package/_types/core/Cbor.d.ts +13 -6
  155. package/_types/core/Cbor.d.ts.map +1 -1
  156. package/_types/core/FrameReceipt.d.ts.map +1 -1
  157. package/_types/core/Hex.d.ts +3 -1
  158. package/_types/core/Hex.d.ts.map +1 -1
  159. package/_types/core/Json.d.ts.map +1 -1
  160. package/_types/core/Rlp.d.ts +38 -9
  161. package/_types/core/Rlp.d.ts.map +1 -1
  162. package/_types/core/RpcTransport.d.ts.map +1 -1
  163. package/_types/core/Siwe.d.ts.map +1 -1
  164. package/_types/core/Transaction.d.ts.map +1 -1
  165. package/_types/core/TransactionReceipt.d.ts.map +1 -1
  166. package/_types/core/TransactionRequest.d.ts.map +1 -1
  167. package/_types/core/TypedData.d.ts.map +1 -1
  168. package/_types/core/internal/abiParameters.d.ts +5 -5
  169. package/_types/core/internal/abiParameters.d.ts.map +1 -1
  170. package/_types/core/internal/address.d.ts +14 -0
  171. package/_types/core/internal/address.d.ts.map +1 -0
  172. package/_types/core/internal/base58.d.ts.map +1 -1
  173. package/_types/core/internal/bytes.d.ts +1 -10
  174. package/_types/core/internal/bytes.d.ts.map +1 -1
  175. package/_types/core/internal/caches.d.ts +1 -0
  176. package/_types/core/internal/caches.d.ts.map +1 -1
  177. package/_types/core/internal/cursor.d.ts +7 -2
  178. package/_types/core/internal/cursor.d.ts.map +1 -1
  179. package/_types/core/internal/hex.d.ts.map +1 -1
  180. package/_types/core/internal/lru.d.ts.map +1 -1
  181. package/_types/core/internal/promise.d.ts.map +1 -1
  182. package/_types/erc6492/SignatureErc6492.d.ts +1 -1
  183. package/_types/erc6492/SignatureErc6492.d.ts.map +1 -1
  184. package/_types/tempo/SignatureEnvelope.d.ts.map +1 -1
  185. package/_types/tempo/Transaction.d.ts.map +1 -1
  186. package/_types/tempo/TransactionRequest.d.ts.map +1 -1
  187. package/_types/tempo/TxEnvelopeTempo.d.ts.map +1 -1
  188. package/_types/tempo/ZoneId.d.ts +3 -1
  189. package/_types/tempo/ZoneId.d.ts.map +1 -1
  190. package/_types/tempo/internal/virtualMasterPool.d.ts.map +1 -1
  191. package/_types/tempo/internal/webauthnData.d.ts +26 -0
  192. package/_types/tempo/internal/webauthnData.d.ts.map +1 -0
  193. package/_types/version.d.ts +1 -1
  194. package/core/AbiEvent.ts +72 -0
  195. package/core/AbiParameters.ts +8 -1
  196. package/core/Address.ts +25 -20
  197. package/core/Base58.ts +15 -4
  198. package/core/Blobs.ts +47 -51
  199. package/core/Bytes.ts +68 -17
  200. package/core/Cbor.ts +33 -13
  201. package/core/FrameReceipt.ts +6 -2
  202. package/core/Hex.ts +79 -22
  203. package/core/Json.ts +23 -2
  204. package/core/Rlp.ts +184 -94
  205. package/core/RpcTransport.ts +4 -1
  206. package/core/Siwe.ts +2 -2
  207. package/core/Transaction.ts +6 -3
  208. package/core/TransactionReceipt.ts +13 -4
  209. package/core/TransactionRequest.ts +10 -6
  210. package/core/TxEnvelopeEip4844.ts +1 -1
  211. package/core/TypedData.ts +26 -8
  212. package/core/internal/abiParameters.ts +82 -23
  213. package/core/internal/address.ts +38 -0
  214. package/core/internal/base58.ts +14 -9
  215. package/core/internal/bytes.ts +13 -23
  216. package/core/internal/caches.ts +1 -0
  217. package/core/internal/cursor.ts +44 -16
  218. package/core/internal/hex.ts +5 -3
  219. package/core/internal/lru.ts +6 -7
  220. package/core/internal/promise.ts +5 -3
  221. package/erc6492/SignatureErc6492.ts +1 -1
  222. package/package.json +1 -1
  223. package/tempo/AccountSimulation.test.ts +1 -1
  224. package/tempo/SignatureEnvelope.test.ts +89 -1
  225. package/tempo/SignatureEnvelope.ts +7 -43
  226. package/tempo/SignaturePreAuthorized.test.ts +1 -1
  227. package/tempo/Transaction.ts +15 -9
  228. package/tempo/TransactionRequest.ts +31 -24
  229. package/tempo/TxEnvelopeTempo.test.ts +52 -0
  230. package/tempo/TxEnvelopeTempo.ts +32 -6
  231. package/tempo/ZoneId.test.ts +11 -0
  232. package/tempo/ZoneId.ts +7 -1
  233. package/tempo/internal/virtualMasterPool.ts +18 -4
  234. package/tempo/internal/webauthnData.ts +89 -0
  235. package/version.ts +1 -1
  236. package/_cjs/core/internal/uid.js +0 -17
  237. package/_cjs/core/internal/uid.js.map +0 -1
  238. package/_esm/core/internal/uid.js +0 -15
  239. package/_esm/core/internal/uid.js.map +0 -1
  240. package/_types/core/internal/uid.d.ts +0 -3
  241. package/_types/core/internal/uid.d.ts.map +0 -1
  242. package/core/internal/uid.ts +0 -15
package/core/Rlp.ts CHANGED
@@ -4,6 +4,9 @@ import * as Hex from './Hex.js'
4
4
  import * as Cursor from './internal/cursor.js'
5
5
  import type { ExactPartial, RecursiveArray } from './internal/types.js'
6
6
 
7
+ /** Maximum nesting depth permitted when decoding an RLP value. */
8
+ const depthLimit = 1_024
9
+
7
10
  /**
8
11
  * Decodes a Recursive-Length Prefix (RLP) value into a {@link ox#Bytes.Bytes} value.
9
12
  *
@@ -68,10 +71,12 @@ export function to<
68
71
  return value as Bytes.Bytes
69
72
  })()
70
73
 
71
- const cursor = Cursor.create(bytes, {
72
- recursiveReadLimit: Number.POSITIVE_INFINITY,
73
- })
74
- const result = decodeRlpCursor(cursor, to_)
74
+ const state: State = { bytes, position: 0 }
75
+ const result = decodeRlpCursor(state, to_)
76
+
77
+ // RLP payloads encode exactly one item (Yellow Paper, Appendix B).
78
+ if (state.position < bytes.length)
79
+ throw new TrailingBytesError({ count: bytes.length - state.position })
75
80
 
76
81
  return result as to.ReturnType<to>
77
82
  }
@@ -85,46 +90,65 @@ export declare namespace to {
85
90
  type ErrorType =
86
91
  | Bytes.fromHex.ErrorType
87
92
  | decodeRlpCursor.ErrorType
88
- | Cursor.create.ErrorType
89
93
  | Hex.InvalidLengthError
94
+ | TrailingBytesError
90
95
  | Errors.GlobalErrorType
91
96
  }
92
97
 
93
- /** @internal */
98
+ /**
99
+ * Decoder position over the encoded bytes. A `Cursor` satisfies it, but the
100
+ * decoder only reads `bytes` and moves `position`.
101
+ *
102
+ * @internal
103
+ */
104
+ type State = Pick<Cursor.Cursor, 'bytes' | 'position'>
94
105
 
95
106
  /** @internal */
96
107
  export function decodeRlpCursor<to extends 'Hex' | 'Bytes' = 'Hex'>(
97
- cursor: Cursor.Cursor,
108
+ cursor: State,
98
109
  to: to | 'Hex' | 'Bytes' | undefined = 'Hex',
110
+ depth = 0,
99
111
  ): decodeRlpCursor.ReturnType<to> {
100
- if (cursor.bytes.length === 0)
112
+ if (depth >= depthLimit)
113
+ throw new DepthLimitExceededError({ limit: depthLimit })
114
+
115
+ const { bytes } = cursor
116
+ if (bytes.length === 0)
101
117
  return (
102
- to === 'Hex' ? Hex.fromBytes(cursor.bytes) : cursor.bytes
118
+ to === 'Hex' ? Hex.fromBytes(bytes) : bytes
103
119
  ) as decodeRlpCursor.ReturnType<to>
104
120
 
105
- const prefix = cursor.readByte()
106
- if (prefix < 0x80) cursor.decrementPosition(1)
121
+ assertPosition(bytes, cursor.position)
122
+ const prefix = bytes[cursor.position]!
123
+ if (prefix >= 0x80) cursor.position++
107
124
 
108
125
  // bytes
109
126
  if (prefix < 0xc0) {
110
127
  const length = readLength(cursor, prefix, 0x80)
111
- const bytes = cursor.readBytes(length)
128
+ if (length > 0) assertPosition(bytes, cursor.position + length - 1)
129
+ const data = bytes.subarray(cursor.position, cursor.position + length)
130
+ cursor.position += length
112
131
  // A single byte below `0x80` must be its own encoding (no length
113
132
  // prefix); wrapping it in an explicit one-byte-length prefix is a
114
133
  // non-canonical alternate encoding of the same value (Yellow Paper,
115
134
  // Appendix B).
116
- if (prefix >= 0x80 && length === 1 && bytes[0]! < 0x80)
135
+ if (prefix >= 0x80 && length === 1 && data[0]! < 0x80)
117
136
  throw new NonCanonicalError({
118
- reason: `single byte \`0x${bytes[0]!.toString(16).padStart(2, '0')}\` is wrapped in an explicit length prefix instead of being encoded as itself`,
137
+ reason: `single byte \`0x${data[0]!.toString(16).padStart(2, '0')}\` is wrapped in an explicit length prefix instead of being encoded as itself`,
119
138
  })
120
139
  return (
121
- to === 'Hex' ? Hex.fromBytes(bytes) : bytes
140
+ to === 'Hex' ? Hex.fromBytes(data) : data
122
141
  ) as decodeRlpCursor.ReturnType<to>
123
142
  }
124
143
 
125
144
  // list
126
145
  const length = readLength(cursor, prefix, 0xc0)
127
- return readList(cursor, length, to) as {} as decodeRlpCursor.ReturnType<to>
146
+ return readList(
147
+ cursor,
148
+ length,
149
+ to,
150
+ depth + 1,
151
+ ) as {} as decodeRlpCursor.ReturnType<to>
128
152
  }
129
153
 
130
154
  /** @internal */
@@ -134,16 +158,14 @@ export declare namespace decodeRlpCursor {
134
158
  | Hex.fromBytes.ErrorType
135
159
  | readLength.ErrorType
136
160
  | readList.ErrorType
161
+ | Cursor.PositionOutOfBoundsError
162
+ | DepthLimitExceededError
137
163
  | NonCanonicalError
138
164
  | Errors.GlobalErrorType
139
165
  }
140
166
 
141
167
  /** @internal */
142
- export function readLength(
143
- cursor: Cursor.Cursor,
144
- prefix: number,
145
- offset: number,
146
- ) {
168
+ export function readLength(cursor: State, prefix: number, offset: number) {
147
169
  if (offset === 0x80 && prefix < 0x80) return 1
148
170
  if (prefix <= offset + 55) return prefix - offset
149
171
  // Long-form lengths must use the minimal number of length-bytes: a value
@@ -151,7 +173,7 @@ export function readLength(
151
173
  // a longer form is a non-canonical alternate encoding of the same value
152
174
  // (Yellow Paper, Appendix B).
153
175
  if (prefix === offset + 55 + 1) {
154
- const length = cursor.readUint8()
176
+ const length = readUint(cursor, 1)
155
177
  if (length <= 55)
156
178
  throw new NonCanonicalError({
157
179
  reason: `length \`${length}\` fits in the short form and must not use a one-byte length prefix`,
@@ -159,7 +181,7 @@ export function readLength(
159
181
  return length
160
182
  }
161
183
  if (prefix === offset + 55 + 2) {
162
- const length = cursor.readUint16()
184
+ const length = readUint(cursor, 2)
163
185
  if (length <= 0xff)
164
186
  throw new NonCanonicalError({
165
187
  reason: `length \`${length}\` fits in fewer length-bytes and must not use a two-byte length prefix`,
@@ -167,7 +189,7 @@ export function readLength(
167
189
  return length
168
190
  }
169
191
  if (prefix === offset + 55 + 3) {
170
- const length = cursor.readUint24()
192
+ const length = readUint(cursor, 3)
171
193
  if (length <= 0xffff)
172
194
  throw new NonCanonicalError({
173
195
  reason: `length \`${length}\` fits in fewer length-bytes and must not use a three-byte length prefix`,
@@ -175,7 +197,7 @@ export function readLength(
175
197
  return length
176
198
  }
177
199
  if (prefix === offset + 55 + 4) {
178
- const length = cursor.readUint32()
200
+ const length = readUint(cursor, 4)
179
201
  if (length <= 0xffffff)
180
202
  throw new NonCanonicalError({
181
203
  reason: `length \`${length}\` fits in fewer length-bytes and must not use a four-byte length prefix`,
@@ -187,30 +209,57 @@ export function readLength(
187
209
 
188
210
  /** @internal */
189
211
  export declare namespace readLength {
190
- type ErrorType = NonCanonicalError | Errors.BaseError | Errors.GlobalErrorType
212
+ type ErrorType =
213
+ | Cursor.PositionOutOfBoundsError
214
+ | NonCanonicalError
215
+ | Errors.BaseError
216
+ | Errors.GlobalErrorType
191
217
  }
192
218
 
193
219
  /** @internal */
194
220
  export function readList<to extends 'Hex' | 'Bytes'>(
195
- cursor: Cursor.Cursor,
221
+ cursor: State,
196
222
  length: number,
197
223
  to: to | 'Hex' | 'Bytes',
224
+ depth = 0,
198
225
  ) {
199
226
  const position = cursor.position
200
227
  const value: decodeRlpCursor.ReturnType<to>[] = []
201
228
  while (cursor.position - position < length)
202
- value.push(decodeRlpCursor(cursor, to))
229
+ value.push(decodeRlpCursor(cursor, to, depth))
230
+ // Items must consume exactly the declared list length.
231
+ if (cursor.position - position !== length)
232
+ throw new ListBoundaryExceededError({
233
+ consumed: cursor.position - position,
234
+ declared: length,
235
+ })
203
236
  return value
204
237
  }
205
238
 
206
239
  /** @internal */
207
240
  export declare namespace readList {
208
- type ErrorType = Errors.GlobalErrorType
241
+ type ErrorType =
242
+ | DepthLimitExceededError
243
+ | ListBoundaryExceededError
244
+ | Errors.GlobalErrorType
209
245
  }
210
246
 
211
- type Encodable = {
212
- length: number
213
- encode(cursor: Cursor.Cursor): void
247
+ function assertPosition(bytes: Bytes.Bytes, position: number) {
248
+ if (position < 0 || position > bytes.length - 1)
249
+ throw new Cursor.PositionOutOfBoundsError({
250
+ length: bytes.length,
251
+ position,
252
+ })
253
+ }
254
+
255
+ // Reads a big-endian unsigned integer of up to 4 bytes.
256
+ function readUint(cursor: State, size: number) {
257
+ const { bytes, position } = cursor
258
+ assertPosition(bytes, position + size - 1)
259
+ let value = 0
260
+ for (let i = 0; i < size; i++) value = value * 256 + bytes[position + i]!
261
+ cursor.position += size
262
+ return value
214
263
  }
215
264
 
216
265
  /**
@@ -237,12 +286,15 @@ export function from<as extends 'Hex' | 'Bytes'>(
237
286
  ): from.ReturnType<as> {
238
287
  const { as } = options
239
288
 
240
- const encodable = getEncodable(value)
241
- const cursor = Cursor.create(new Uint8Array(encodable.length))
242
- encodable.encode(cursor)
289
+ // Two passes without a per-node closure tree: `measure` decodes leaves and
290
+ // records list body lengths in visit order, and `write` reads both back in
291
+ // the same order into a pre-sized buffer.
292
+ const ctx: EncodeContext = { bodies: [], leaves: [] }
293
+ const bytes = new Uint8Array(measure(value, ctx))
294
+ write(value, bytes, ctx, { body: 0, leaf: 0, offset: 0 })
243
295
 
244
- if (as === 'Hex') return Hex.fromBytes(cursor.bytes) as from.ReturnType<as>
245
- return cursor.bytes as from.ReturnType<as>
296
+ if (as === 'Hex') return Hex.fromBytes(bytes) as from.ReturnType<as>
297
+ return bytes as from.ReturnType<as>
246
298
  }
247
299
 
248
300
  export declare namespace from {
@@ -256,7 +308,6 @@ export declare namespace from {
256
308
  | (as extends 'Hex' ? Hex.Hex : never)
257
309
 
258
310
  type ErrorType =
259
- | Cursor.create.ErrorType
260
311
  | Hex.fromBytes.ErrorType
261
312
  | Bytes.fromHex.ErrorType
262
313
  | Errors.GlobalErrorType
@@ -332,71 +383,77 @@ export declare namespace fromHex {
332
383
  // Internal
333
384
  /////////////////////////////////////////////////////////////////////////////////
334
385
 
335
- function getEncodable(
336
- bytes: RecursiveArray<Bytes.Bytes> | RecursiveArray<Hex.Hex>,
337
- ): Encodable {
338
- if (Array.isArray(bytes))
339
- return getEncodableList(bytes.map((x) => getEncodable(x)))
340
- return getEncodableBytes(bytes as any)
386
+ type EncodeContext = {
387
+ /** Body length of each list, in visit order. */
388
+ bodies: number[]
389
+ /** Bytes of each leaf, in visit order. */
390
+ leaves: Bytes.Bytes[]
341
391
  }
342
392
 
343
- function getEncodableList(list: Encodable[]): Encodable {
344
- const bodyLength = list.reduce((acc, x) => acc + x.length, 0)
345
-
346
- const sizeOfBodyLength = getSizeOfLength(bodyLength)
347
- const length = (() => {
393
+ function measure(
394
+ value: RecursiveArray<Bytes.Bytes> | RecursiveArray<Hex.Hex>,
395
+ ctx: EncodeContext,
396
+ ): number {
397
+ if (Array.isArray(value)) {
398
+ // Reserve this list's slot before its children's, in visit order.
399
+ const index = ctx.bodies.length
400
+ ctx.bodies.push(0)
401
+ let bodyLength = 0
402
+ for (let i = 0; i < value.length; i++) bodyLength += measure(value[i], ctx)
403
+ ctx.bodies[index] = bodyLength
348
404
  if (bodyLength <= 55) return 1 + bodyLength
349
- return 1 + sizeOfBodyLength + bodyLength
350
- })()
351
-
352
- return {
353
- length,
354
- encode(cursor: Cursor.Cursor) {
355
- if (bodyLength <= 55) {
356
- cursor.pushByte(0xc0 + bodyLength)
357
- } else {
358
- cursor.pushByte(0xc0 + 55 + sizeOfBodyLength)
359
- if (sizeOfBodyLength === 1) cursor.pushUint8(bodyLength)
360
- else if (sizeOfBodyLength === 2) cursor.pushUint16(bodyLength)
361
- else if (sizeOfBodyLength === 3) cursor.pushUint24(bodyLength)
362
- else cursor.pushUint32(bodyLength)
363
- }
364
- for (const { encode } of list) {
365
- encode(cursor)
366
- }
367
- },
405
+ return 1 + getSizeOfLength(bodyLength) + bodyLength
368
406
  }
369
- }
370
407
 
371
- function getEncodableBytes(bytesOrHex: Bytes.Bytes | Hex.Hex): Encodable {
372
408
  const bytes =
373
- typeof bytesOrHex === 'string' ? Bytes.fromHex(bytesOrHex) : bytesOrHex
374
-
409
+ typeof value === 'string'
410
+ ? Bytes.fromHex(value as Hex.Hex)
411
+ : (value as Bytes.Bytes)
412
+ ctx.leaves.push(bytes)
375
413
  const sizeOfBytesLength = getSizeOfLength(bytes.length)
376
- const length = (() => {
377
- if (bytes.length === 1 && bytes[0]! < 0x80) return 1
378
- if (bytes.length <= 55) return 1 + bytes.length
379
- return 1 + sizeOfBytesLength + bytes.length
380
- })()
414
+ if (bytes.length === 1 && bytes[0]! < 0x80) return 1
415
+ if (bytes.length <= 55) return 1 + bytes.length
416
+ return 1 + sizeOfBytesLength + bytes.length
417
+ }
381
418
 
382
- return {
383
- length,
384
- encode(cursor: Cursor.Cursor) {
385
- if (bytes.length === 1 && bytes[0]! < 0x80) {
386
- cursor.pushBytes(bytes)
387
- } else if (bytes.length <= 55) {
388
- cursor.pushByte(0x80 + bytes.length)
389
- cursor.pushBytes(bytes)
390
- } else {
391
- cursor.pushByte(0x80 + 55 + sizeOfBytesLength)
392
- if (sizeOfBytesLength === 1) cursor.pushUint8(bytes.length)
393
- else if (sizeOfBytesLength === 2) cursor.pushUint16(bytes.length)
394
- else if (sizeOfBytesLength === 3) cursor.pushUint24(bytes.length)
395
- else cursor.pushUint32(bytes.length)
396
- cursor.pushBytes(bytes)
397
- }
398
- },
419
+ function write(
420
+ value: RecursiveArray<Bytes.Bytes> | RecursiveArray<Hex.Hex>,
421
+ out: Bytes.Bytes,
422
+ ctx: EncodeContext,
423
+ state: { body: number; leaf: number; offset: number },
424
+ ) {
425
+ if (Array.isArray(value)) {
426
+ writePrefix(out, state, 0xc0, ctx.bodies[state.body++]!)
427
+ for (let i = 0; i < value.length; i++) write(value[i], out, ctx, state)
428
+ return
429
+ }
430
+
431
+ const bytes = ctx.leaves[state.leaf++]!
432
+ if (bytes.length === 1 && bytes[0]! < 0x80) {
433
+ out[state.offset++] = bytes[0]!
434
+ return
435
+ }
436
+ writePrefix(out, state, 0x80, bytes.length)
437
+ out.set(bytes, state.offset)
438
+ state.offset += bytes.length
439
+ }
440
+
441
+ function writePrefix(
442
+ out: Bytes.Bytes,
443
+ state: { offset: number },
444
+ offset: number,
445
+ length: number,
446
+ ) {
447
+ if (length <= 55) {
448
+ out[state.offset++] = offset + length
449
+ return
399
450
  }
451
+ const size = getSizeOfLength(length)
452
+ out[state.offset++] = offset + 55 + size
453
+ // Big-endian length in `size` bytes.
454
+ for (let i = size - 1, n = length; i >= 0; i--, n = Math.floor(n / 256))
455
+ out[state.offset + i] = n % 256
456
+ state.offset += size
400
457
  }
401
458
 
402
459
  function getSizeOfLength(length: number) {
@@ -407,6 +464,39 @@ function getSizeOfLength(length: number) {
407
464
  throw new Errors.BaseError('Length is too large.')
408
465
  }
409
466
 
467
+ /** Thrown when an RLP value nests deeper than the decode depth limit. */
468
+ export class DepthLimitExceededError extends Errors.BaseError {
469
+ override readonly name = 'Rlp.DepthLimitExceededError'
470
+
471
+ constructor({ limit }: { limit: number }) {
472
+ super(`RLP depth limit of \`${limit}\` exceeded.`)
473
+ }
474
+ }
475
+
476
+ /** Thrown when RLP list items overrun the list's declared length. */
477
+ export class ListBoundaryExceededError extends Errors.BaseError {
478
+ override readonly name = 'Rlp.ListBoundaryExceededError'
479
+
480
+ constructor({ consumed, declared }: { consumed: number; declared: number }) {
481
+ super(
482
+ `RLP list items consumed \`${consumed}\` bytes but the list declared a length of \`${declared}\`.`,
483
+ )
484
+ }
485
+ }
486
+
487
+ /** Thrown when an RLP payload contains bytes after the decoded item. */
488
+ export class TrailingBytesError extends Errors.BaseError {
489
+ override readonly name = 'Rlp.TrailingBytesError'
490
+
491
+ constructor({ count }: { count: number }) {
492
+ super(
493
+ `RLP payload encodes a single item, but \`${count}\` trailing ${
494
+ count === 1 ? 'byte remains' : 'bytes remain'
495
+ }.`,
496
+ )
497
+ }
498
+ }
499
+
410
500
  /** Thrown when an RLP payload uses a non-canonical alternate encoding of a value. */
411
501
  export class NonCanonicalError extends Errors.BaseError {
412
502
  override readonly name = 'Rlp.NonCanonicalError'
@@ -202,7 +202,10 @@ export class HttpError extends Errors.BaseError {
202
202
  metaMessages: [
203
203
  `Status: ${response.status}`,
204
204
  `URL: ${getUrl(url)}`,
205
- body ? `Body: ${JSON.stringify(body)}` : undefined,
205
+ // The request body is usually already JSON.
206
+ body
207
+ ? `Body: ${typeof body === 'string' ? body : JSON.stringify(body)}`
208
+ : undefined,
206
209
  ],
207
210
  })
208
211
  }
package/core/Siwe.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as Address from './Address.js'
2
2
  import * as Errors from './Errors.js'
3
+ import * as Hex from './Hex.js'
3
4
  import type { ExactPartial } from './internal/types.js'
4
- import { uid } from './internal/uid.js'
5
5
 
6
6
  export const domainRegex =
7
7
  /^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+(?:[a-zA-Z]{2,63}|[xX][nN]--[a-zA-Z0-9-]{0,58}[a-zA-Z0-9])(:[0-9]{1,5})?$/
@@ -285,7 +285,7 @@ export declare namespace createMessage {
285
285
  * @returns Random nonce.
286
286
  */
287
287
  export function generateNonce(): string {
288
- return uid(96)
288
+ return Hex.random(48).slice(2)
289
289
  }
290
290
 
291
291
  /**
@@ -389,8 +389,9 @@ export function fromRpc<
389
389
  if (transaction.maxPriorityFeePerGas)
390
390
  transaction_.maxPriorityFeePerGas = BigInt(transaction.maxPriorityFeePerGas)
391
391
  if (transaction.type)
392
- transaction_.type =
393
- (fromRpcType as any)[transaction.type] ?? transaction.type
392
+ transaction_.type = Object.hasOwn(fromRpcType, transaction.type)
393
+ ? (fromRpcType as any)[transaction.type]
394
+ : transaction.type
394
395
  if (signature) transaction_.v = Signature.yParityToV(signature.yParity)
395
396
 
396
397
  return transaction_ as never
@@ -467,7 +468,9 @@ export function toRpc<pending extends boolean = false>(
467
468
  rpc.transactionIndex = transaction.transactionIndex
468
469
  ? Hex.fromNumber(transaction.transactionIndex)
469
470
  : null
470
- rpc.type = (toRpcType as any)[transaction.type] ?? transaction.type
471
+ rpc.type = Object.hasOwn(toRpcType, transaction.type)
472
+ ? (toRpcType as any)[transaction.type]
473
+ : transaction.type
471
474
  rpc.value = Hex.fromNumber(transaction.value ?? 0n)
472
475
 
473
476
  if (transaction.type === 'eip8141' && transaction.nonceKeys !== undefined)
@@ -266,9 +266,14 @@ export function fromRpc<const receipt extends Rpc | null>(
266
266
  effectiveGasPrice: BigInt(receipt.effectiveGasPrice ?? 0n),
267
267
  gasUsed: BigInt(receipt.gasUsed ?? 0n),
268
268
  logs: receipt.logs.map((log) => Log.fromRpc(log, { pending: false })),
269
- status: fromRpcStatus[receipt.status],
269
+ status: Object.hasOwn(fromRpcStatus, receipt.status)
270
+ ? fromRpcStatus[receipt.status]
271
+ : (undefined as never),
270
272
  transactionIndex: Number(receipt.transactionIndex ?? 0),
271
- type: (fromRpcType as any)[receipt.type] || receipt.type,
273
+ type:
274
+ (Object.hasOwn(fromRpcType, receipt.type) &&
275
+ (fromRpcType as any)[receipt.type]) ||
276
+ receipt.type,
272
277
  } as never
273
278
  }
274
279
 
@@ -354,11 +359,15 @@ export function toRpc(receipt: TransactionReceipt): Rpc {
354
359
  logs: receipt.logs.map(Log.toRpc as never),
355
360
  logsBloom: receipt.logsBloom,
356
361
  root: receipt.root,
357
- status: toRpcStatus[receipt.status],
362
+ status: Object.hasOwn(toRpcStatus, receipt.status)
363
+ ? toRpcStatus[receipt.status]
364
+ : (undefined as never),
358
365
  to: receipt.to,
359
366
  transactionHash: receipt.transactionHash,
360
367
  transactionIndex: Hex.fromNumber(receipt.transactionIndex),
361
- type: (toRpcType as any)[receipt.type] ?? receipt.type,
368
+ type: Object.hasOwn(toRpcType, receipt.type)
369
+ ? (toRpcType as any)[receipt.type]
370
+ : receipt.type,
362
371
  }
363
372
  }
364
373
 
@@ -114,9 +114,11 @@ export function fromRpc(request: Rpc): TransactionRequest {
114
114
  request_.nonceKeys = FrameNonce.fromRpc(request.nonceKeys, request.nonce)
115
115
  if (typeof request.type !== 'undefined')
116
116
  request_.type =
117
- Transaction.fromRpcType[
118
- request.type as keyof typeof Transaction.fromRpcType
119
- ] || request.type
117
+ (Object.hasOwn(Transaction.fromRpcType, request.type) &&
118
+ Transaction.fromRpcType[
119
+ request.type as keyof typeof Transaction.fromRpcType
120
+ ]) ||
121
+ request.type
120
122
  if (typeof request.value !== 'undefined')
121
123
  request_.value = Hex.toBigInt(request.value)
122
124
 
@@ -222,9 +224,11 @@ export function toRpc(request: TransactionRequest): Rpc {
222
224
  if (typeof request.to !== 'undefined') request_rpc.to = request.to
223
225
  if (typeof request.type !== 'undefined')
224
226
  request_rpc.type =
225
- Transaction.toRpcType[
226
- request.type as keyof typeof Transaction.toRpcType
227
- ] || request.type
227
+ (Object.hasOwn(Transaction.toRpcType, request.type) &&
228
+ Transaction.toRpcType[
229
+ request.type as keyof typeof Transaction.toRpcType
230
+ ]) ||
231
+ request.type
228
232
  if (typeof request.value !== 'undefined')
229
233
  request_rpc.value = Hex.fromNumber(request.value)
230
234
 
@@ -79,9 +79,9 @@ export function assert(envelope: PartialBy<TxEnvelopeEip4844, 'type'>) {
79
79
  throw new Blobs.EmptyBlobVersionedHashesError()
80
80
  for (const hash of blobVersionedHashes) {
81
81
  const size = Hex.size(hash)
82
- const version = Hex.toNumber(Hex.slice(hash, 0, 1))
83
82
  if (size !== 32)
84
83
  throw new Blobs.InvalidVersionedHashSizeError({ hash, size })
84
+ const version = Hex.toNumber(Hex.slice(hash, 0, 1))
85
85
  if (version !== Kzg.versionedHashVersion)
86
86
  throw new Blobs.InvalidVersionedHashVersionError({
87
87
  hash,
package/core/TypedData.ts CHANGED
@@ -5,6 +5,7 @@ import * as Bytes from './Bytes.js'
5
5
  import * as Errors from './Errors.js'
6
6
  import * as Hash from './Hash.js'
7
7
  import * as Hex from './Hex.js'
8
+ import { caches } from './internal/caches.js'
8
9
  import type { Compute } from './internal/types.js'
9
10
  import * as Json from './Json.js'
10
11
  import * as Solidity from './Solidity.js'
@@ -169,7 +170,8 @@ export function assert<
169
170
 
170
171
  // Validate message types.
171
172
  if (primaryType !== 'EIP712Domain') {
172
- if (types[primaryType]) validateData(types[primaryType], message)
173
+ if (Object.hasOwn(types, primaryType) && types[primaryType])
174
+ validateData(types[primaryType], message)
173
175
  else throw new InvalidPrimaryTypeError({ primaryType, types })
174
176
  }
175
177
  }
@@ -355,7 +357,7 @@ export function encodeType(value: encodeType.Value): string {
355
357
 
356
358
  const deps = [primaryType, ...Array.from(unsortedDeps).sort()]
357
359
  for (const type of deps) {
358
- result += `${type}(${(types[type] ?? [])
360
+ result += `${type}(${((Object.hasOwn(types, type) && types[type]) || [])
359
361
  .map(({ name, type: t }) => `${t} ${name}`)
360
362
  .join(',')})`
361
363
  }
@@ -644,7 +646,7 @@ export function serialize<
644
646
 
645
647
  const message = (() => {
646
648
  if (primaryType === 'EIP712Domain') return undefined
647
- if (!types[primaryType]) return {}
649
+ if (!Object.hasOwn(types, primaryType) || !types[primaryType]) return {}
648
650
  return normalizeData(types[primaryType], message_)
649
651
  })()
650
652
 
@@ -790,7 +792,8 @@ export function encodeData(value: {
790
792
  const encodedTypes: AbiParameters.Parameter[] = [{ type: 'bytes32' }]
791
793
  const encodedValues: unknown[] = [hashType({ primaryType, types })]
792
794
 
793
- for (const field of types[primaryType] ?? []) {
795
+ const fields = (Object.hasOwn(types, primaryType) && types[primaryType]) || []
796
+ for (const field of fields) {
794
797
  const [type, value] = encodeField({
795
798
  types,
796
799
  name: field.name,
@@ -813,14 +816,25 @@ export declare namespace encodeData {
813
816
  | Errors.GlobalErrorType
814
817
  }
815
818
 
819
+ /** Longest encoded type kept in the type-hash cache. */
820
+ const typeHashCacheMaxLength = 4_096
821
+
816
822
  /** @internal */
817
823
  export function hashType(value: {
818
824
  primaryType: string
819
825
  types: TypedData
820
826
  }): Hex.Hex {
821
827
  const { primaryType, types } = value
822
- const encodedHashType = Hex.fromString(encodeType({ primaryType, types }))
823
- return Hash.keccak256(encodedHashType)
828
+ // Keyed by the encoded type, so equal struct definitions share one hash
829
+ // across calls, whichever object their types came from.
830
+ const type = encodeType({ primaryType, types })
831
+ const cached = caches.typeHash.get(type)
832
+ if (cached) return cached
833
+ const hash = Hash.keccak256(Hex.fromString(type))
834
+ // The cache is bounded by entries, not bytes, so only cache encoded types of
835
+ // ordinary size: retained memory then stays bounded whatever types are passed.
836
+ if (type.length <= typeHashCacheMaxLength) caches.typeHash.set(type, hash)
837
+ return hash
824
838
  }
825
839
 
826
840
  /** @internal */
@@ -841,7 +855,7 @@ export function encodeField(properties: {
841
855
  }): [type: AbiParameters.Parameter, value: Hex.Hex] {
842
856
  let { types, name, type, value } = properties
843
857
 
844
- if (types[type] !== undefined)
858
+ if (Object.hasOwn(types, type) && types[type] !== undefined)
845
859
  return [
846
860
  { type: 'bytes32' },
847
861
  Hash.keccak256(encodeData({ data: value, primaryType: type, types })),
@@ -907,7 +921,11 @@ export function findTypeDependencies(
907
921
  ): Set<string> {
908
922
  const { primaryType: primaryType_, types } = value
909
923
  const primaryType = primaryType_.replace(arraySuffixRegex, '')
910
- if (results.has(primaryType) || types[primaryType] === undefined)
924
+ if (
925
+ results.has(primaryType) ||
926
+ !Object.hasOwn(types, primaryType) ||
927
+ types[primaryType] === undefined
928
+ )
911
929
  return results
912
930
 
913
931
  results.add(primaryType)