ox 1.8.15 → 1.8.18

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 (292) hide show
  1. package/CHANGELOG.md +108 -0
  2. package/dist/core/AbiError.d.ts +52 -6
  3. package/dist/core/AbiError.d.ts.map +1 -1
  4. package/dist/core/AbiError.js +60 -4
  5. package/dist/core/AbiError.js.map +1 -1
  6. package/dist/core/AbiEvent.d.ts +5 -3
  7. package/dist/core/AbiEvent.d.ts.map +1 -1
  8. package/dist/core/AbiEvent.js +89 -58
  9. package/dist/core/AbiEvent.js.map +1 -1
  10. package/dist/core/AbiFunction.d.ts +48 -2
  11. package/dist/core/AbiFunction.d.ts.map +1 -1
  12. package/dist/core/AbiFunction.js +59 -3
  13. package/dist/core/AbiFunction.js.map +1 -1
  14. package/dist/core/AbiItem.d.ts.map +1 -1
  15. package/dist/core/AbiItem.js +15 -5
  16. package/dist/core/AbiItem.js.map +1 -1
  17. package/dist/core/AbiParameters.d.ts +1 -1
  18. package/dist/core/AbiParameters.d.ts.map +1 -1
  19. package/dist/core/AbiParameters.js +8 -2
  20. package/dist/core/AbiParameters.js.map +1 -1
  21. package/dist/core/Address.d.ts +17 -2
  22. package/dist/core/Address.d.ts.map +1 -1
  23. package/dist/core/Address.js +17 -1
  24. package/dist/core/Address.js.map +1 -1
  25. package/dist/core/Authorization.d.ts +24 -2
  26. package/dist/core/Authorization.d.ts.map +1 -1
  27. package/dist/core/Authorization.js +29 -2
  28. package/dist/core/Authorization.js.map +1 -1
  29. package/dist/core/Base58.d.ts.map +1 -1
  30. package/dist/core/Base58.js +16 -4
  31. package/dist/core/Base58.js.map +1 -1
  32. package/dist/core/Blobs.d.ts +2 -3
  33. package/dist/core/Blobs.d.ts.map +1 -1
  34. package/dist/core/Blobs.js +38 -43
  35. package/dist/core/Blobs.js.map +1 -1
  36. package/dist/core/Bytes.d.ts.map +1 -1
  37. package/dist/core/Bytes.js +10 -2
  38. package/dist/core/Bytes.js.map +1 -1
  39. package/dist/core/Cbor.d.ts +13 -6
  40. package/dist/core/Cbor.d.ts.map +1 -1
  41. package/dist/core/Cbor.js +31 -18
  42. package/dist/core/Cbor.js.map +1 -1
  43. package/dist/core/Ens.js +2 -2
  44. package/dist/core/Ens.js.map +1 -1
  45. package/dist/core/FrameReceipt.d.ts.map +1 -1
  46. package/dist/core/FrameReceipt.js +4 -1
  47. package/dist/core/FrameReceipt.js.map +1 -1
  48. package/dist/core/Hex.d.ts.map +1 -1
  49. package/dist/core/Hex.js +34 -31
  50. package/dist/core/Hex.js.map +1 -1
  51. package/dist/core/Keystore.d.ts +31 -2
  52. package/dist/core/Keystore.d.ts.map +1 -1
  53. package/dist/core/Keystore.js +28 -2
  54. package/dist/core/Keystore.js.map +1 -1
  55. package/dist/core/Rlp.d.ts +1 -1
  56. package/dist/core/Rlp.d.ts.map +1 -1
  57. package/dist/core/Rlp.js +107 -0
  58. package/dist/core/Rlp.js.map +1 -1
  59. package/dist/core/RpcTransport.d.ts.map +1 -1
  60. package/dist/core/RpcTransport.js +3 -1
  61. package/dist/core/RpcTransport.js.map +1 -1
  62. package/dist/core/Secp256k1.d.ts.map +1 -1
  63. package/dist/core/Secp256k1.js +4 -1
  64. package/dist/core/Secp256k1.js.map +1 -1
  65. package/dist/core/Signature.d.ts +1 -1
  66. package/dist/core/Signature.d.ts.map +1 -1
  67. package/dist/core/Signature.js +4 -2
  68. package/dist/core/Signature.js.map +1 -1
  69. package/dist/core/Siwe.d.ts +4 -0
  70. package/dist/core/Siwe.d.ts.map +1 -1
  71. package/dist/core/Siwe.js +29 -3
  72. package/dist/core/Siwe.js.map +1 -1
  73. package/dist/core/Transaction.d.ts.map +1 -1
  74. package/dist/core/Transaction.js +4 -2
  75. package/dist/core/Transaction.js.map +1 -1
  76. package/dist/core/TransactionReceipt.d.ts.map +1 -1
  77. package/dist/core/TransactionReceipt.js +8 -2
  78. package/dist/core/TransactionReceipt.js.map +1 -1
  79. package/dist/core/TxEnvelopeEip1559.d.ts.map +1 -1
  80. package/dist/core/TxEnvelopeEip1559.js +2 -1
  81. package/dist/core/TxEnvelopeEip1559.js.map +1 -1
  82. package/dist/core/TxEnvelopeEip2930.d.ts.map +1 -1
  83. package/dist/core/TxEnvelopeEip2930.js +2 -1
  84. package/dist/core/TxEnvelopeEip2930.js.map +1 -1
  85. package/dist/core/TxEnvelopeEip7702.d.ts +1 -1
  86. package/dist/core/TxEnvelopeEip7702.d.ts.map +1 -1
  87. package/dist/core/TxEnvelopeEip7702.js +6 -1
  88. package/dist/core/TxEnvelopeEip7702.js.map +1 -1
  89. package/dist/core/TxEnvelopeLegacy.d.ts.map +1 -1
  90. package/dist/core/TxEnvelopeLegacy.js +49 -7
  91. package/dist/core/TxEnvelopeLegacy.js.map +1 -1
  92. package/dist/core/TypedData.d.ts +3 -1
  93. package/dist/core/TypedData.d.ts.map +1 -1
  94. package/dist/core/TypedData.js +23 -11
  95. package/dist/core/TypedData.js.map +1 -1
  96. package/dist/core/Value.d.ts.map +1 -1
  97. package/dist/core/Value.js +14 -3
  98. package/dist/core/Value.js.map +1 -1
  99. package/dist/core/internal/abiParameters.d.ts +3 -3
  100. package/dist/core/internal/abiParameters.d.ts.map +1 -1
  101. package/dist/core/internal/abiParameters.js +99 -69
  102. package/dist/core/internal/abiParameters.js.map +1 -1
  103. package/dist/core/internal/base58.d.ts.map +1 -1
  104. package/dist/core/internal/base58.js +9 -3
  105. package/dist/core/internal/base58.js.map +1 -1
  106. package/dist/core/internal/caches.d.ts +6 -0
  107. package/dist/core/internal/caches.d.ts.map +1 -1
  108. package/dist/core/internal/caches.js +3 -1
  109. package/dist/core/internal/caches.js.map +1 -1
  110. package/dist/core/internal/codec/hex.d.ts +13 -0
  111. package/dist/core/internal/codec/hex.d.ts.map +1 -1
  112. package/dist/core/internal/codec/hex.js +25 -2
  113. package/dist/core/internal/codec/hex.js.map +1 -1
  114. package/dist/core/internal/codec/int.d.ts.map +1 -1
  115. package/dist/core/internal/codec/int.js +48 -32
  116. package/dist/core/internal/codec/int.js.map +1 -1
  117. package/dist/core/internal/cursor.d.ts +24 -4
  118. package/dist/core/internal/cursor.d.ts.map +1 -1
  119. package/dist/core/internal/cursor.js +62 -11
  120. package/dist/core/internal/cursor.js.map +1 -1
  121. package/dist/core/internal/lru.d.ts +11 -12
  122. package/dist/core/internal/lru.d.ts.map +1 -1
  123. package/dist/core/internal/lru.js +22 -32
  124. package/dist/core/internal/lru.js.map +1 -1
  125. package/dist/core/internal/promise.d.ts.map +1 -1
  126. package/dist/core/internal/promise.js +13 -7
  127. package/dist/core/internal/promise.js.map +1 -1
  128. package/dist/erc4337/UserOperation.d.ts.map +1 -1
  129. package/dist/erc4337/UserOperation.js +1 -3
  130. package/dist/erc4337/UserOperation.js.map +1 -1
  131. package/dist/erc6492/SignatureErc6492.d.ts +1 -1
  132. package/dist/erc6492/SignatureErc6492.d.ts.map +1 -1
  133. package/dist/erc6492/SignatureErc6492.js +1 -1
  134. package/dist/erc6492/SignatureErc6492.js.map +1 -1
  135. package/dist/erc7821/Execute.d.ts +45 -0
  136. package/dist/erc7821/Execute.d.ts.map +1 -1
  137. package/dist/erc7821/Execute.js +70 -2
  138. package/dist/erc7821/Execute.js.map +1 -1
  139. package/dist/index.d.ts +1 -1
  140. package/dist/index.js +1 -1
  141. package/dist/tempo/KeyAuthorization.d.ts +9 -3
  142. package/dist/tempo/KeyAuthorization.d.ts.map +1 -1
  143. package/dist/tempo/KeyAuthorization.js +16 -0
  144. package/dist/tempo/KeyAuthorization.js.map +1 -1
  145. package/dist/tempo/SignatureEnvelope.d.ts +1 -1
  146. package/dist/tempo/SignatureEnvelope.d.ts.map +1 -1
  147. package/dist/tempo/SignatureEnvelope.js +53 -54
  148. package/dist/tempo/SignatureEnvelope.js.map +1 -1
  149. package/dist/tempo/TransactionReceipt.d.ts.map +1 -1
  150. package/dist/tempo/TransactionReceipt.js +4 -1
  151. package/dist/tempo/TransactionReceipt.js.map +1 -1
  152. package/dist/tempo/TransactionRequest.d.ts +1 -1
  153. package/dist/tempo/TransactionRequest.d.ts.map +1 -1
  154. package/dist/tempo/TransactionRequest.js +5 -2
  155. package/dist/tempo/TransactionRequest.js.map +1 -1
  156. package/dist/tempo/TxEnvelopeTempo.d.ts +30 -1
  157. package/dist/tempo/TxEnvelopeTempo.d.ts.map +1 -1
  158. package/dist/tempo/TxEnvelopeTempo.js +51 -2
  159. package/dist/tempo/TxEnvelopeTempo.js.map +1 -1
  160. package/dist/tempo/ZoneId.d.ts +3 -1
  161. package/dist/tempo/ZoneId.d.ts.map +1 -1
  162. package/dist/tempo/ZoneId.js +7 -1
  163. package/dist/tempo/ZoneId.js.map +1 -1
  164. package/dist/tempo/internal/virtualMasterPool.d.ts.map +1 -1
  165. package/dist/tempo/internal/virtualMasterPool.js +17 -6
  166. package/dist/tempo/internal/virtualMasterPool.js.map +1 -1
  167. package/dist/tempo/internal/webauthnData.d.ts +23 -0
  168. package/dist/tempo/internal/webauthnData.d.ts.map +1 -0
  169. package/dist/tempo/internal/webauthnData.js +75 -0
  170. package/dist/tempo/internal/webauthnData.js.map +1 -0
  171. package/dist/webauthn/Registration.d.ts +2 -0
  172. package/dist/webauthn/Registration.d.ts.map +1 -1
  173. package/dist/webauthn/Registration.js +2 -6
  174. package/dist/webauthn/Registration.js.map +1 -1
  175. package/dist/zod/TypedData.d.ts +4 -4
  176. package/dist/zod/tempo/KeyAuthorization.d.ts.map +1 -1
  177. package/dist/zod/tempo/KeyAuthorization.js +4 -2
  178. package/dist/zod/tempo/KeyAuthorization.js.map +1 -1
  179. package/dist/zod/tempo/SignatureEnvelope.d.ts.map +1 -1
  180. package/dist/zod/tempo/SignatureEnvelope.js +29 -29
  181. package/dist/zod/tempo/SignatureEnvelope.js.map +1 -1
  182. package/package.json +2 -2
  183. package/src/core/AbiError.ts +82 -8
  184. package/src/core/AbiEvent.bench.ts +45 -0
  185. package/src/core/AbiEvent.ts +120 -68
  186. package/src/core/AbiFunction.bench.ts +16 -0
  187. package/src/core/AbiFunction.ts +76 -4
  188. package/src/core/AbiItem.ts +13 -4
  189. package/src/core/AbiParameters.bench.ts +33 -0
  190. package/src/core/AbiParameters.ts +8 -1
  191. package/src/core/Address.ts +18 -1
  192. package/src/core/Authorization.ts +34 -4
  193. package/src/core/Base58.bench.ts +28 -0
  194. package/src/core/Base58.ts +15 -4
  195. package/src/core/Blobs.bench.ts +40 -0
  196. package/src/core/Blobs.ts +47 -55
  197. package/src/core/Bytes.bench.ts +34 -0
  198. package/src/core/Bytes.ts +10 -2
  199. package/src/core/Cbor.ts +36 -14
  200. package/src/core/Ens.ts +2 -2
  201. package/src/core/FrameReceipt.ts +4 -1
  202. package/src/core/Hex.bench.ts +17 -0
  203. package/src/core/Hex.ts +29 -32
  204. package/src/core/Keystore.ts +40 -3
  205. package/src/core/Rlp.ts +139 -1
  206. package/src/core/Rlp_tx.bench.ts +55 -0
  207. package/src/core/RpcTransport.ts +3 -1
  208. package/src/core/Secp256k1.ts +4 -1
  209. package/src/core/Signature.ts +7 -3
  210. package/src/core/Siwe.ts +29 -3
  211. package/src/core/Transaction.ts +4 -2
  212. package/src/core/TransactionReceipt.ts +8 -2
  213. package/src/core/TxEnvelopeEip1559.ts +4 -1
  214. package/src/core/TxEnvelopeEip2930.ts +4 -1
  215. package/src/core/TxEnvelopeEip7702.ts +9 -2
  216. package/src/core/TxEnvelopeLegacy.ts +58 -7
  217. package/src/core/TypedData.bench.ts +50 -0
  218. package/src/core/TypedData.ts +24 -11
  219. package/src/core/Value.ts +14 -3
  220. package/src/core/_test/AbiError.test.ts +105 -2
  221. package/src/core/_test/AbiEvent.fuzz.ts +1 -1
  222. package/src/core/_test/AbiEvent.test-d.ts +46 -0
  223. package/src/core/_test/AbiEvent.test.ts +646 -11
  224. package/src/core/_test/AbiFunction.test.ts +79 -6
  225. package/src/core/_test/AbiParameters.adversarial.fuzz.ts +1 -0
  226. package/src/core/_test/AbiParameters.decode.test.ts +143 -0
  227. package/src/core/_test/AbiParameters.encode.test.ts +36 -1
  228. package/src/core/_test/AbiParameters.test.ts +15 -0
  229. package/src/core/_test/Address.test.ts +41 -0
  230. package/src/core/_test/Authorization.test.ts +130 -0
  231. package/src/core/_test/Bytes.test.ts +33 -0
  232. package/src/core/_test/Caches.test.ts +20 -1
  233. package/src/core/_test/Cbor.test.ts +26 -0
  234. package/src/core/_test/FrameReceipt.test.ts +13 -0
  235. package/src/core/_test/Keystore.test.ts +65 -1
  236. package/src/core/_test/Rlp.fuzz.ts +55 -1
  237. package/src/core/_test/Rlp.test.ts +64 -0
  238. package/src/core/_test/RpcTransport.test.ts +15 -2
  239. package/src/core/_test/Secp256k1.test.ts +36 -0
  240. package/src/core/_test/Signature.test.ts +102 -0
  241. package/src/core/_test/Siwe.test.ts +123 -0
  242. package/src/core/_test/Transaction.test.ts +17 -0
  243. package/src/core/_test/TransactionEnvelopeEip1559.test.ts +28 -0
  244. package/src/core/_test/TransactionEnvelopeEip2930.test.ts +28 -0
  245. package/src/core/_test/TransactionEnvelopeEip7702.test.ts +133 -0
  246. package/src/core/_test/TransactionEnvelopeLegacy.test.ts +322 -1
  247. package/src/core/_test/TransactionReceipt.test.ts +41 -0
  248. package/src/core/_test/TypedData.test.ts +197 -0
  249. package/src/core/_test/Value.test.ts +21 -0
  250. package/src/core/_test/internal/cursor.test.ts +71 -0
  251. package/src/core/internal/abiParameters.ts +123 -81
  252. package/src/core/internal/base58.ts +9 -3
  253. package/src/core/internal/caches.ts +3 -1
  254. package/src/core/internal/codec/hex.ts +27 -2
  255. package/src/core/internal/codec/int.ts +45 -32
  256. package/src/core/internal/cursor.ts +84 -19
  257. package/src/core/internal/lru.ts +21 -35
  258. package/src/core/internal/promise.ts +11 -5
  259. package/src/erc4337/UserOperation.ts +3 -4
  260. package/src/erc4337/_test/UserOperation.test.ts +59 -1
  261. package/src/erc6492/SignatureErc6492.ts +1 -1
  262. package/src/erc6492/_test/SignatureErc6492.test.ts +48 -1
  263. package/src/erc7821/Execute.ts +83 -2
  264. package/src/erc7821/_test/Execute.test.ts +217 -1
  265. package/src/index.ts +1 -1
  266. package/src/tempo/KeyAuthorization.test.ts +91 -0
  267. package/src/tempo/KeyAuthorization.ts +32 -3
  268. package/src/tempo/SignatureEnvelope.test.ts +300 -28
  269. package/src/tempo/SignatureEnvelope.ts +62 -54
  270. package/src/tempo/TransactionReceipt.test.ts +8 -0
  271. package/src/tempo/TransactionReceipt.ts +4 -1
  272. package/src/tempo/TransactionRequest.test.ts +30 -0
  273. package/src/tempo/TransactionRequest.ts +7 -2
  274. package/src/tempo/TxEnvelopeTempo.test.ts +310 -0
  275. package/src/tempo/TxEnvelopeTempo.ts +64 -1
  276. package/src/tempo/VirtualMaster.bench.ts +7 -0
  277. package/src/tempo/ZoneId.test.ts +11 -0
  278. package/src/tempo/ZoneId.ts +7 -1
  279. package/src/tempo/configurableAccounts.e2e.test.ts +1 -1
  280. package/src/tempo/internal/virtualMasterPool.ts +20 -6
  281. package/src/tempo/internal/webauthnData.ts +84 -0
  282. package/src/version.ts +1 -1
  283. package/src/webauthn/Registration.ts +4 -6
  284. package/src/webauthn/_test/Registration.browser.test.ts +14 -0
  285. package/src/webauthn/_test/Registration.test.ts +60 -0
  286. package/src/zod/tempo/KeyAuthorization.ts +12 -5
  287. package/src/zod/tempo/SignatureEnvelope.ts +38 -32
  288. package/src/zod/tempo/_test/KeyAuthorization.test.ts +36 -0
  289. package/src/zod/tempo/_test/SignatureEnvelope.test.ts +138 -4
  290. package/src/core/_test/internal/lru.test.ts +0 -38
  291. package/src/core/_test/internal/uid.test.ts +0 -7
  292. package/src/core/internal/uid.ts +0 -15
@@ -2,7 +2,7 @@ import * as abitype from 'abitype'
2
2
  import type * as Abi from './Abi.js'
3
3
  import * as AbiItem from './AbiItem.js'
4
4
  import * as AbiParameters from './AbiParameters.js'
5
- import type * as Errors from './Errors.js'
5
+ import * as Errors from './Errors.js'
6
6
  import * as Hex from './Hex.js'
7
7
  import type * as internal from './internal/abiFunction.js'
8
8
  import type * as AbiItem_internal from './internal/abiItem.js'
@@ -200,9 +200,17 @@ export function decodeData(
200
200
 
201
201
  if (Hex.size(data) < 4) throw new AbiItem.InvalidSelectorSizeError({ data })
202
202
 
203
- const item = overloads
204
- ? fromAbi([abiFunction, ...overloads], data as never)
205
- : abiFunction
203
+ // Hex is case-insensitive. Selectors are lowercase.
204
+ const selector = Hex.slice(data, 0, 4).toLowerCase() as Hex.Hex
205
+ const item = [abiFunction, ...(overloads ?? [])].find(
206
+ (item) => getSelector(item) === selector,
207
+ )
208
+ if (!item)
209
+ throw new SelectorMismatchError({
210
+ abiFunction,
211
+ actual: selector,
212
+ expected: getSelector(abiFunction),
213
+ })
206
214
 
207
215
  if (item.inputs.length === 0) return undefined
208
216
  if (Hex.size(data) <= 4)
@@ -255,9 +263,11 @@ export declare namespace decodeData {
255
263
 
256
264
  type ErrorType =
257
265
  | fromAbi.ErrorType
266
+ | getSelector.ErrorType
258
267
  | AbiParameters.decode.ErrorType
259
268
  | Hex.size.ErrorType
260
269
  | Hex.slice.ErrorType
270
+ | SelectorMismatchError
261
271
  | Errors.GlobalErrorType
262
272
  }
263
273
 
@@ -1095,3 +1105,65 @@ export function getSelector(abiItem: string | AbiFunction): Hex.Hex {
1095
1105
  export declare namespace getSelector {
1096
1106
  type ErrorType = AbiItem.getSelector.ErrorType | Errors.GlobalErrorType
1097
1107
  }
1108
+
1109
+ /**
1110
+ * Thrown when the selector of the provided data does not match the selector of the function, or of one of its overloads.
1111
+ *
1112
+ * @example
1113
+ * ```ts twoslash
1114
+ * // @noErrors
1115
+ * import { AbiFunction } from 'ox'
1116
+ *
1117
+ * const transfer = AbiFunction.from(
1118
+ * 'function transfer(address to, uint256 amount)'
1119
+ * )
1120
+ *
1121
+ * AbiFunction.decodeData(
1122
+ * transfer,
1123
+ * '0x095ea7b3000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa960450000000000000000000000000000000000000000000000000000000000000001'
1124
+ * )
1125
+ * // @error: AbiFunction.SelectorMismatchError: Selector "0x095ea7b3" does not match the expected selector "0xa9059cbb".
1126
+ * // @error: Function: function transfer(address to, uint256 amount)
1127
+ * // @error: Selector: 0xa9059cbb
1128
+ * ```
1129
+ *
1130
+ * ### Solution
1131
+ *
1132
+ * Ensure that the data was encoded for the function.
1133
+ *
1134
+ * ```ts twoslash
1135
+ * import { AbiFunction } from 'ox'
1136
+ *
1137
+ * const transfer = AbiFunction.from(
1138
+ * 'function transfer(address to, uint256 amount)'
1139
+ * )
1140
+ *
1141
+ * AbiFunction.decodeData(
1142
+ * transfer,
1143
+ * '0xa9059cbb000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa960450000000000000000000000000000000000000000000000000000000000000001'
1144
+ * )
1145
+ * ```
1146
+ */
1147
+ export class SelectorMismatchError extends Errors.BaseError {
1148
+ override readonly name = 'AbiFunction.SelectorMismatchError'
1149
+
1150
+ constructor({
1151
+ abiFunction,
1152
+ actual,
1153
+ expected,
1154
+ }: {
1155
+ abiFunction: AbiFunction
1156
+ actual: Hex.Hex
1157
+ expected: Hex.Hex
1158
+ }) {
1159
+ super(
1160
+ `Selector "${actual}" does not match the expected selector "${expected}".`,
1161
+ {
1162
+ metaMessages: [
1163
+ `Function: ${format(abiFunction)}`,
1164
+ `Selector: ${expected}`,
1165
+ ],
1166
+ },
1167
+ )
1168
+ }
1169
+ }
@@ -642,13 +642,22 @@ export function getSignatureHash(
642
642
  })()
643
643
  if (typeof abiItem !== 'string' && 'hash' in abiItem && abiItem.hash)
644
644
  return abiItem.hash as Hex.Hex
645
+ // Keyed by the item itself first, which skips formatting and normalizing the
646
+ // signature on every call. ABI items are treated as immutable here: an item
647
+ // mutated after it was hashed keeps its first hash.
648
+ if (typeof abiItem === 'object') {
649
+ const cached = caches.signatureHashByItem.get(abiItem)
650
+ if (cached) return cached
651
+ }
645
652
  // Keyed by the normalized signature, so equal signatures share one hash
646
653
  // regardless of how the ABI item was written.
647
654
  const signature = getSignature(abiItem)
648
- const cached = caches.signatureHash.get(signature)
649
- if (cached) return cached
650
- const hash = Hash.keccak256(Hex.fromString(signature))
651
- caches.signatureHash.set(signature, hash)
655
+ let hash = caches.signatureHash.get(signature)
656
+ if (!hash) {
657
+ hash = Hash.keccak256(Hex.fromString(signature))
658
+ caches.signatureHash.set(signature, hash)
659
+ }
660
+ if (typeof abiItem === 'object') caches.signatureHashByItem.set(abiItem, hash)
652
661
  return hash
653
662
  }
654
663
 
@@ -16,6 +16,27 @@ const address_100_encoded = AbiParameters.encode(address_100, [
16
16
  address_100_values,
17
17
  ])
18
18
 
19
+ const address_1000 = Array.from(
20
+ { length: 1000 },
21
+ (_, i) => `0x${i.toString(16).padStart(40, '0')}` as `0x${string}`,
22
+ )
23
+
24
+ const string_100 = AbiParameters.from('string[]')
25
+ const string_100_values = Array.from({ length: 100 }, (_, i) =>
26
+ `string ${i} `.repeat(4),
27
+ )
28
+ const string_100_encoded = AbiParameters.encode(string_100, [string_100_values])
29
+
30
+ // Multicall3 `aggregate3` calldata arguments: 100 `balanceOf` calls.
31
+ const aggregate3 = AbiParameters.from(
32
+ '(address target, bool allowFailure, bytes callData)[]',
33
+ )
34
+ const aggregate3_values = Array.from({ length: 100 }, (_, i) => ({
35
+ allowFailure: i % 2 === 0,
36
+ callData: `0x70a08231${i.toString(16).padStart(64, '0')}` as `0x${string}`,
37
+ target: `0x${(i + 1).toString(16).padStart(40, '0')}` as `0x${string}`,
38
+ }))
39
+
19
40
  const string_param = AbiParameters.from('string')
20
41
  const string_value = 'a'.repeat(1024)
21
42
  const string_encoded = AbiParameters.encode(string_param, [string_value])
@@ -54,6 +75,14 @@ describe('AbiParameters.encode', () => {
54
75
  AbiParameters.encode(address_100, [address_100_values])
55
76
  })
56
77
 
78
+ bench('(address[1000])', () => {
79
+ AbiParameters.encode(address_100, [address_1000])
80
+ })
81
+
82
+ bench('aggregate3 (100 calls)', () => {
83
+ AbiParameters.encode(aggregate3, [aggregate3_values])
84
+ })
85
+
57
86
  bench('(string) 1KB', () => {
58
87
  AbiParameters.encode(string_param, [string_value])
59
88
  })
@@ -76,6 +105,10 @@ describe('AbiParameters.decode', () => {
76
105
  AbiParameters.decode(string_param, string_encoded)
77
106
  })
78
107
 
108
+ bench('(string[100])', () => {
109
+ AbiParameters.decode(string_100, string_100_encoded)
110
+ })
111
+
79
112
  bench('nested (tuple, tuple, tuple)', () => {
80
113
  AbiParameters.decode(tuple_nested, tuple_nested_encoded)
81
114
  })
@@ -276,6 +276,7 @@ export function encodePacked<
276
276
  export namespace encodePacked {
277
277
  export type ErrorType =
278
278
  | Hex.concat.ErrorType
279
+ | Hex.InvalidLengthError
279
280
  | LengthMismatchError
280
281
  | Errors.GlobalErrorType
281
282
 
@@ -302,7 +303,13 @@ export namespace encodePacked {
302
303
  ) as Address.Address
303
304
  }
304
305
  if (type === 'string') return Hex.fromString(value as string)
305
- if (type === 'bytes') return value as Hex.Hex
306
+ if (type === 'bytes') {
307
+ const hex = value as Hex.Hex
308
+ // An unpaired nibble has no byte to belong to, so reject it instead of
309
+ // guessing which half of a byte it is.
310
+ if (hex.length % 2 !== 0) throw new Hex.InvalidLengthError(hex)
311
+ return hex
312
+ }
306
313
  if (type === 'bool')
307
314
  return Hex.padLeft(Hex.fromBoolean(value as boolean), isArray ? 32 : 1)
308
315
 
@@ -204,7 +204,22 @@ export declare namespace from {
204
204
  * // @log: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'
205
205
  * ```
206
206
  *
207
- * @param publicKey - The ECDSA public key to convert to an {@link ox#Address.Address}.
207
+ * @example
208
+ * ### Compressed Public Keys
209
+ *
210
+ * The address is derived from both coordinates, so a compressed public key throws.
211
+ *
212
+ * ```ts twoslash
213
+ * import { Address, PublicKey } from 'ox'
214
+ *
215
+ * const publicKey = PublicKey.from(
216
+ * '0x038318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed75'
217
+ * )
218
+ * const address = Address.fromPublicKey(publicKey)
219
+ * // @error: PublicKey.InvalidError: Value `{"prefix":3,"x":"0x8318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed75"}` is not a valid public key.
220
+ * ```
221
+ *
222
+ * @param publicKey - The uncompressed ECDSA public key to convert to an {@link ox#Address.Address}.
208
223
  * @param options - Conversion options.
209
224
  * @returns The {@link ox#Address.Address} corresponding to the public key.
210
225
  */
@@ -212,6 +227,7 @@ export function fromPublicKey(
212
227
  publicKey: PublicKey.PublicKey,
213
228
  options: fromPublicKey.Options = {},
214
229
  ): Address {
230
+ PublicKey.assert(publicKey, { compressed: false })
215
231
  const address = Hash.keccak256(
216
232
  `0x${PublicKey.toHex(publicKey).slice(4)}`,
217
233
  ).substring(26)
@@ -230,6 +246,7 @@ export declare namespace fromPublicKey {
230
246
 
231
247
  type ErrorType =
232
248
  | Hash.keccak256.ErrorType
249
+ | PublicKey.assert.ErrorType
233
250
  | PublicKey.toHex.ErrorType
234
251
  | Errors.GlobalErrorType
235
252
  }
@@ -1,5 +1,5 @@
1
1
  import type * as Address from './Address.js'
2
- import type * as Errors from './Errors.js'
2
+ import * as Errors from './Errors.js'
3
3
  import * as Hash from './Hash.js'
4
4
  import * as Hex from './Hex.js'
5
5
  import * as Quantity from './internal/quantity.js'
@@ -187,7 +187,7 @@ export function fromRpc(authorization: Rpc): Signed {
187
187
 
188
188
  return {
189
189
  address,
190
- chainId: Number(chainId),
190
+ chainId: Hex.toNumber(chainId),
191
191
  nonce: BigInt(nonce),
192
192
  ...signature,
193
193
  }
@@ -276,10 +276,14 @@ export declare namespace fromRpcList {
276
276
  export function fromTuple<const tuple extends Tuple>(
277
277
  tuple: tuple,
278
278
  ): fromTuple.ReturnType<tuple> {
279
+ if (tuple.length !== 3 && tuple.length !== 6)
280
+ throw new InvalidError(
281
+ 'Expected [chainId, address, nonce] or [chainId, address, nonce, yParity, r, s].',
282
+ )
279
283
  const [chainId, address, nonce, yParity, r, s] = tuple
280
284
  let args = {
281
285
  address,
282
- chainId: chainId === '0x' ? 0 : Number(chainId),
286
+ chainId: chainId === '0x' ? 0 : Hex.toNumber(chainId),
283
287
  nonce: nonce === '0x' ? 0n : BigInt(nonce),
284
288
  }
285
289
  if (yParity && r && s) {
@@ -302,7 +306,7 @@ export declare namespace fromTuple {
302
306
  Authorization<authorization extends Tuple<true> ? true : false>
303
307
  >
304
308
 
305
- type ErrorType = Errors.GlobalErrorType
309
+ type ErrorType = InvalidError | Errors.GlobalErrorType
306
310
  }
307
311
 
308
312
  /**
@@ -693,3 +697,29 @@ function assertYParity(yParity: number) {
693
697
  value: `${yParity}`,
694
698
  })
695
699
  }
700
+
701
+ /**
702
+ * Thrown when an EIP-7702 Authorization tuple is structurally invalid.
703
+ *
704
+ * @example
705
+ * ```ts twoslash
706
+ * // @noErrors
707
+ * import { Authorization } from 'ox'
708
+ *
709
+ * Authorization.fromTuple([
710
+ * '0x1',
711
+ * '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c',
712
+ * '0x3',
713
+ * '0x1'
714
+ * ])
715
+ * // @error: Authorization.InvalidError: Invalid authorization.
716
+ * // @error: Details: Expected [chainId, address, nonce] or [chainId, address, nonce, yParity, r, s].
717
+ * ```
718
+ */
719
+ export class InvalidError extends Errors.BaseError {
720
+ override readonly name = 'Authorization.InvalidError'
721
+
722
+ constructor(details: string) {
723
+ super('Invalid authorization.', { details })
724
+ }
725
+ }
@@ -0,0 +1,28 @@
1
+ import { bench, describe } from 'vp/test'
2
+ import * as Base58 from './Base58.js'
3
+ import * as Bytes from './Bytes.js'
4
+
5
+ const bytes32 = Bytes.fromHex(`0x00${'7f3a'.repeat(15)}c1`)
6
+ const bytes256 = Bytes.fromHex(`0x${'9e51'.repeat(128)}`)
7
+ const string32 = Base58.fromBytes(bytes32)
8
+ const string256 = Base58.fromBytes(bytes256)
9
+
10
+ describe('Base58.fromBytes', () => {
11
+ bench('32 bytes', () => {
12
+ Base58.fromBytes(bytes32)
13
+ })
14
+
15
+ bench('256 bytes', () => {
16
+ Base58.fromBytes(bytes256)
17
+ })
18
+ })
19
+
20
+ describe('Base58.toBytes', () => {
21
+ bench('32 bytes', () => {
22
+ Base58.toBytes(string32)
23
+ })
24
+
25
+ bench('256 bytes', () => {
26
+ Base58.toBytes(string256)
27
+ })
28
+ })
@@ -110,10 +110,14 @@ export declare namespace toBytes {
110
110
  export function toHex(value: string): Hex.Hex {
111
111
  if (value.length === 0) return '0x'
112
112
 
113
- let integer = BigInt(0)
113
+ let integer = 0n
114
114
  let pad = 0
115
115
  let checkPad = true
116
116
 
117
+ // Accumulate up to nine digits as a number, since 58^9 is below 2^53, and
118
+ // fold each chunk into the BigInt at once.
119
+ let chunk = 0
120
+ let chunkBase = 1
117
121
  for (let i = 0; i < value.length; i++) {
118
122
  const char = value[i]!
119
123
 
@@ -122,12 +126,19 @@ export function toHex(value: string): Hex.Hex {
122
126
  else checkPad = false
123
127
 
124
128
  // check for invalid characters
125
- if (typeof internal.alphabetToInteger[char] !== 'bigint')
129
+ const digit = internal.alphabetToInteger[char]
130
+ if (typeof digit !== 'bigint')
126
131
  throw new InvalidCharacterError({ character: char })
127
132
 
128
- integer = integer * 58n
129
- integer = integer + internal.alphabetToInteger[char]!
133
+ chunk = chunk * 58 + Number(digit)
134
+ chunkBase *= 58
135
+ if (chunkBase === 7_427_658_739_644_928) {
136
+ integer = integer * 7_427_658_739_644_928n + BigInt(chunk)
137
+ chunk = 0
138
+ chunkBase = 1
139
+ }
130
140
  }
141
+ if (chunkBase > 1) integer = integer * BigInt(chunkBase) + BigInt(chunk)
131
142
 
132
143
  let body = integer === 0n ? '' : integer.toString(16)
133
144
  if ((body.length & 1) !== 0) body = `0${body}`
@@ -0,0 +1,40 @@
1
+ import { bench, describe } from 'vp/test'
2
+ import * as Blobs from './Blobs.js'
3
+ import * as Bytes from './Bytes.js'
4
+
5
+ const small = Bytes.random(1_000)
6
+ const full = Uint8Array.from(
7
+ { length: Blobs.maxBytesPerTransaction },
8
+ (_, i) => (i * 31) & 0x7f,
9
+ )
10
+ // Every byte is `0x80`, the terminator byte, so a decoder that rescans the
11
+ // rest of the blob for another terminator on each one goes quadratic.
12
+ const terminators = new Uint8Array(100_000).fill(0x80)
13
+
14
+ const smallBlobs = Blobs.from(small)
15
+ const fullBlobs = Blobs.from(full)
16
+ const terminatorBlobs = Blobs.from(terminators)
17
+
18
+ describe('Blobs.from', () => {
19
+ bench('1 KB', () => {
20
+ Blobs.from(small)
21
+ })
22
+
23
+ bench('6 blobs (max data)', () => {
24
+ Blobs.from(full)
25
+ })
26
+ })
27
+
28
+ describe('Blobs.to', () => {
29
+ bench('1 KB', () => {
30
+ Blobs.to(smallBlobs)
31
+ })
32
+
33
+ bench('6 blobs (max data)', () => {
34
+ Blobs.to(fullBlobs)
35
+ })
36
+
37
+ bench('100 KB of 0x80 bytes', () => {
38
+ Blobs.to(terminatorBlobs)
39
+ })
40
+ })
package/src/core/Blobs.ts CHANGED
@@ -3,7 +3,6 @@ import * as Bytes from './Bytes.js'
3
3
  import * as Errors from './Errors.js'
4
4
  import * as Hash from './Hash.js'
5
5
  import * as Hex from './Hex.js'
6
- import * as Cursor from './internal/cursor.js'
7
6
  import type { Compute } from './internal/types.js'
8
7
  import * as Kzg from './Kzg.js'
9
8
 
@@ -290,43 +289,40 @@ export function from<
290
289
  size: size_,
291
290
  })
292
291
 
293
- const blobs = []
292
+ const blobs: Bytes.Bytes[] = []
294
293
 
295
294
  let active = true
296
295
  let position = 0
297
296
  while (active) {
298
- const blob = Cursor.create(new Uint8Array(bytesPerBlob))
299
-
300
- let size = 0
301
- while (size < fieldElementsPerBlob) {
302
- const bytes = data_.slice(position, position + (bytesPerFieldElement - 1))
303
-
304
- // Push a zero byte so the field element doesn't overflow the BLS modulus.
305
- blob.pushByte(0x00)
306
-
307
- // Push the current segment of data bytes.
308
- blob.pushBytes(bytes)
309
-
310
- // If we detect that the current segment of data bytes is less than 31 bytes,
311
- // we can stop processing and push a terminator byte to indicate the end of the blob.
312
- if (bytes.length < 31) {
313
- blob.pushByte(0x80)
297
+ const blob = new Uint8Array(bytesPerBlob)
298
+
299
+ // Each field element is a zero byte, so it doesn't overflow the BLS
300
+ // modulus, followed by 31 data bytes. The zero byte is already in place.
301
+ // Copying byte by byte beats a `subarray` view plus `set` at this size, and
302
+ // allocates nothing.
303
+ for (
304
+ let offset = 1;
305
+ offset < bytesPerBlob;
306
+ offset += bytesPerFieldElement
307
+ ) {
308
+ const length = Math.min(31, size_ - position)
309
+ for (let i = 0; i < length; i++) blob[offset + i] = data_[position + i]!
310
+
311
+ // A segment shorter than 31 bytes ends the data: push a terminator byte
312
+ // to mark it.
313
+ if (length < 31) {
314
+ blob[offset + length] = 0x80
314
315
  active = false
315
316
  break
316
317
  }
317
318
 
318
- size++
319
319
  position += 31
320
320
  }
321
321
 
322
322
  blobs.push(blob)
323
323
  }
324
324
 
325
- return (
326
- as === 'Bytes'
327
- ? blobs.map((x) => x.bytes)
328
- : blobs.map((x) => Hex.fromBytes(x.bytes))
329
- ) as never
325
+ return (as === 'Bytes' ? blobs : blobs.map((x) => Hex.fromBytes(x))) as never
330
326
  }
331
327
 
332
328
  export declare namespace from {
@@ -344,7 +340,6 @@ export declare namespace from {
344
340
  | EmptyBlobError
345
341
  | Bytes.fromHex.ErrorType
346
342
  | Hex.fromBytes.ErrorType
347
- | Cursor.create.ErrorType
348
343
  | Bytes.size.ErrorType
349
344
  | Errors.GlobalErrorType
350
345
  }
@@ -394,39 +389,37 @@ export function to<
394
389
  : blobs
395
390
  ) as Bytes.Bytes[]
396
391
 
397
- const length = blobs_.reduce((length, blob) => length + blob.length, 0)
398
- const data = Cursor.create(new Uint8Array(length))
399
- let active = true
392
+ // Only the final blob contains a terminator: the last `0x80` byte in it, when
393
+ // that byte is data rather than the zero byte leading a field element.
394
+ // Preceding blobs contain only data.
395
+ const last = blobs_[blobs_.length - 1]
396
+ const terminator = last ? last.lastIndexOf(0x80) : -1
397
+ const lastEnd =
398
+ terminator % bytesPerFieldElement > 0 ? terminator : (last?.length ?? 0)
400
399
 
401
- for (const [index, blob] of blobs_.entries()) {
402
- const cursor = Cursor.create(blob)
403
- // Only the final blob contains a terminator; preceding blobs contain only data.
404
- const isLastBlob = index === blobs_.length - 1
405
- while (active && cursor.position < blob.length) {
406
- // First byte will be a zero 0x00 byte – we can skip.
407
- cursor.incrementPosition(1)
408
-
409
- let consume = 31
410
- if (blob.length - cursor.position < 31)
411
- consume = blob.length - cursor.position
412
-
413
- for (let i = 0; i < consume; i++) {
414
- const byte = cursor.readByte()
415
- const isTerminator =
416
- isLastBlob &&
417
- byte === 0x80 &&
418
- !cursor.inspectBytes(cursor.remaining).includes(0x80)
419
- if (isTerminator) {
420
- active = false
421
- break
422
- }
423
- data.pushByte(byte)
424
- }
400
+ // Each field element contributes all of its bytes but the leading zero byte.
401
+ const dataSize = (end: number) =>
402
+ Math.floor(end / bytesPerFieldElement) * 31 +
403
+ Math.max(0, (end % bytesPerFieldElement) - 1)
404
+
405
+ let length = dataSize(lastEnd)
406
+ for (let index = 0; index < blobs_.length - 1; index++)
407
+ length += dataSize(blobs_[index]!.length)
408
+
409
+ const data = new Uint8Array(length)
410
+ let position = 0
411
+ for (let index = 0; index < blobs_.length; index++) {
412
+ const blob = blobs_[index]!
413
+ const end = index === blobs_.length - 1 ? lastEnd : blob.length
414
+ // Skip the zero byte leading each field element. Copying byte by byte
415
+ // beats a `subarray` view plus `set` for 31 bytes, and allocates nothing.
416
+ for (let offset = 1; offset < end; offset += bytesPerFieldElement) {
417
+ const stop = Math.min(offset + 31, end)
418
+ for (let i = offset; i < stop; i++) data[position++] = blob[i]!
425
419
  }
426
420
  }
427
421
 
428
- const trimmedData = data.bytes.slice(0, data.position)
429
- return (to_ === 'Hex' ? Hex.fromBytes(trimmedData) : trimmedData) as never
422
+ return (to_ === 'Hex' ? Hex.fromBytes(data) : data) as never
430
423
  }
431
424
 
432
425
  export declare namespace to {
@@ -437,7 +430,6 @@ export declare namespace to {
437
430
  type ErrorType =
438
431
  | Hex.fromBytes.ErrorType
439
432
  | Bytes.fromHex.ErrorType
440
- | Cursor.create.ErrorType
441
433
  | Errors.GlobalErrorType
442
434
  }
443
435
 
@@ -21,6 +21,8 @@ const bytes32 = (() => {
21
21
  return b
22
22
  })()
23
23
  const bytes32Zero = new Uint8Array(32)
24
+ const bytes32Small = Bytes.fromNumber(1_234_567n, { size: 32 })
25
+ const bytes32Wei = Bytes.fromNumber(42n * 10n ** 18n, { size: 32 })
24
26
 
25
27
  describe('Bytes.fromHex', () => {
26
28
  bench('32 bytes', () => {
@@ -106,9 +108,31 @@ describe('Bytes.toBigInt', () => {
106
108
  Bytes.toBigInt(bytes32Zero)
107
109
  })
108
110
 
111
+ bench('32 bytes (small value)', () => {
112
+ Bytes.toBigInt(bytes32Small)
113
+ })
114
+
115
+ bench('32 bytes (wei value)', () => {
116
+ Bytes.toBigInt(bytes32Wei)
117
+ })
118
+
109
119
  bench('32 bytes, size: 32', () => {
110
120
  Bytes.toBigInt(bytes32, { size: 32 })
111
121
  })
122
+
123
+ bench('32-byte word holding a small value', () => {
124
+ Bytes.toBigInt(bytes32Small)
125
+ })
126
+
127
+ bench('32 bytes, signed', () => {
128
+ Bytes.toBigInt(bytes32, { signed: true })
129
+ })
130
+ })
131
+
132
+ describe('Bytes.toNumber', () => {
133
+ bench('32-byte word holding a small value', () => {
134
+ Bytes.toNumber(bytes32Small)
135
+ })
112
136
  })
113
137
 
114
138
  describe('Bytes.padLeft', () => {
@@ -119,3 +143,13 @@ describe('Bytes.padLeft', () => {
119
143
  Bytes.padLeft(small.subarray(0, 1), 32)
120
144
  })
121
145
  })
146
+
147
+ describe('Bytes.validate', () => {
148
+ bench('valid', () => {
149
+ Bytes.validate(small)
150
+ })
151
+
152
+ bench('invalid', () => {
153
+ Bytes.validate('0xdeadbeef')
154
+ })
155
+ })
package/src/core/Bytes.ts CHANGED
@@ -745,9 +745,17 @@ export declare namespace trimRight {
745
745
  * @returns `true` if the value is {@link ox#Bytes.Bytes}, otherwise `false`.
746
746
  */
747
747
  export function validate(value: unknown): value is Bytes {
748
+ // The checks `assert` makes, without building and catching an error.
749
+ if (!value || typeof value !== 'object') return false
750
+ // A revoked Proxy, or a trap or getter, can throw from these checks; that
751
+ // is invalid input too.
748
752
  try {
749
- assert(value)
750
- return true
753
+ if (value instanceof Uint8Array) return true
754
+ return (
755
+ 'BYTES_PER_ELEMENT' in value &&
756
+ value.BYTES_PER_ELEMENT === 1 &&
757
+ value.constructor.name === 'Uint8Array'
758
+ )
751
759
  } catch {
752
760
  return false
753
761
  }