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
@@ -9,7 +9,7 @@ import {
9
9
  WebAuthnP256,
10
10
  WebCryptoP256,
11
11
  } from 'ox'
12
- import { describe, expect, test } from 'vitest'
12
+ import { describe, expect, onTestFinished, test, vi } from 'vitest'
13
13
  import * as AccountConfig from './AccountConfig.js'
14
14
  import * as SignatureEnvelope from './SignatureEnvelope.js'
15
15
 
@@ -735,6 +735,58 @@ describe('deserialize', () => {
735
735
  )
736
736
  })
737
737
 
738
+ test('behavior: splits webauthnData at the object that ends it', () => {
739
+ const metadata = {
740
+ // Ends with bytes that look like the start of a JSON object.
741
+ authenticatorData: Hex.concat(
742
+ signature_webauthn.metadata.authenticatorData,
743
+ Hex.fromString('{"a":"}\\'),
744
+ ),
745
+ // Braces, an escaped quote and an escaped backslash inside strings.
746
+ clientDataJSON: JSON.stringify({
747
+ challenge: '{"}\\',
748
+ nested: { origin: '\\"' },
749
+ }),
750
+ }
751
+ expect(
752
+ SignatureEnvelope.deserialize(
753
+ SignatureEnvelope.serialize({ ...signature_webauthn, metadata }),
754
+ ),
755
+ ).toMatchObject({ metadata })
756
+ })
757
+
758
+ test('behavior: decodes and parses webauthnData at most once', () => {
759
+ // Each opening brace starts an object that only fails to parse past its
760
+ // end, so a search that tries every split offset reads all of it each time.
761
+ const clientDataJSON = `${'{"a":'.repeat(313)}${'}'.repeat(313)}`
762
+ const serialized = SignatureEnvelope.serialize({
763
+ ...signature_webauthn,
764
+ metadata: {
765
+ authenticatorData: `0x${'00'.repeat(37)}` as const,
766
+ clientDataJSON,
767
+ },
768
+ })
769
+
770
+ const decode = vi.spyOn(TextDecoder.prototype, 'decode')
771
+ const parse = vi.spyOn(JSON, 'parse')
772
+ onTestFinished(() => {
773
+ vi.restoreAllMocks()
774
+ })
775
+
776
+ expect(() => SignatureEnvelope.deserialize(serialized)).toThrow(
777
+ 'Unable to parse WebAuthn metadata',
778
+ )
779
+ expect(
780
+ decode.mock.calls.reduce(
781
+ (size, [input]) => size + (input?.byteLength ?? 0),
782
+ 0,
783
+ ),
784
+ ).toBeLessThanOrEqual(clientDataJSON.length)
785
+ expect(
786
+ parse.mock.calls.reduce((size, [text]) => size + text.length, 0),
787
+ ).toBeLessThanOrEqual(clientDataJSON.length)
788
+ })
789
+
738
790
  test('error: throws on unknown type identifier', () => {
739
791
  const unknownType = `0xff${'00'.repeat(129)}` as const
740
792
  expect(() =>
@@ -2416,6 +2468,42 @@ describe('fromRpc', () => {
2416
2468
  })
2417
2469
  })
2418
2470
 
2471
+ test('behavior: decodes and parses webauthnData at most once', () => {
2472
+ // Each opening brace starts an object that only fails to parse past its
2473
+ // end, so a search that tries every split offset reads all of it each time.
2474
+ const clientDataJSON = `${'{"a":'.repeat(313)}${'}'.repeat(313)}`
2475
+ const rpc = {
2476
+ pubKeyX: Hex.fromNumber(publicKey.x, { size: 32 }),
2477
+ pubKeyY: Hex.fromNumber(publicKey.y, { size: 32 }),
2478
+ r: Hex.fromNumber(p256Signature.r, { size: 32 }),
2479
+ s: Hex.fromNumber(p256Signature.s, { size: 32 }),
2480
+ type: 'webAuthn',
2481
+ webauthnData: Hex.concat(
2482
+ `0x${'00'.repeat(37)}`,
2483
+ Hex.fromString(clientDataJSON),
2484
+ ),
2485
+ } as const
2486
+
2487
+ const decode = vi.spyOn(TextDecoder.prototype, 'decode')
2488
+ const parse = vi.spyOn(JSON, 'parse')
2489
+ onTestFinished(() => {
2490
+ vi.restoreAllMocks()
2491
+ })
2492
+
2493
+ expect(() => SignatureEnvelope.fromRpc(rpc)).toThrow(
2494
+ 'Unable to parse WebAuthn metadata',
2495
+ )
2496
+ expect(
2497
+ decode.mock.calls.reduce(
2498
+ (size, [input]) => size + (input?.byteLength ?? 0),
2499
+ 0,
2500
+ ),
2501
+ ).toBeLessThanOrEqual(clientDataJSON.length)
2502
+ expect(
2503
+ parse.mock.calls.reduce((size, [text]) => size + text.length, 0),
2504
+ ).toBeLessThanOrEqual(clientDataJSON.length)
2505
+ })
2506
+
2419
2507
  test('error: throws on WebAuthn data above the maximum size', () => {
2420
2508
  const rpc = {
2421
2509
  pubKeyX: Hex.fromNumber(publicKey.x, { size: 32 }),
@@ -19,6 +19,7 @@ import * as Signature from '../core/Signature.js'
19
19
  import type * as WebAuthnP256 from '../core/WebAuthnP256.js'
20
20
  import * as ox_WebAuthnP256 from '../core/WebAuthnP256.js'
21
21
  import * as AccountConfig from './AccountConfig.js'
22
+ import * as WebauthnData from './internal/webauthnData.js'
22
23
 
23
24
  /** Signature type identifiers for encoding/decoding */
24
25
  const serializedP256Type = '0x01'
@@ -669,24 +670,8 @@ function deserialize_(value: Serialized): SignatureEnvelope {
669
670
 
670
671
  // Parse webauthnData into authenticatorData and clientDataJSON
671
672
  // According to the Rust code, it's authenticatorData || clientDataJSON
672
- // We need to find the split point (minimum authenticatorData is 37 bytes)
673
- let authenticatorData: Hex.Hex | undefined
674
- let clientDataJSON: string | undefined
675
-
676
- // Try to find the JSON start (clientDataJSON should start with '{')
677
- for (let split = 37; split < webauthnDataSize; split++) {
678
- const potentialJson = Hex.toString(Hex.slice(webauthnData, split))
679
- if (potentialJson.startsWith('{') && potentialJson.endsWith('}')) {
680
- try {
681
- JSON.parse(potentialJson)
682
- authenticatorData = Hex.slice(webauthnData, 0, split)
683
- clientDataJSON = potentialJson
684
- break
685
- } catch {}
686
- }
687
- }
688
-
689
- if (!authenticatorData || !clientDataJSON)
673
+ const metadata = WebauthnData.split(webauthnData)
674
+ if (!metadata)
690
675
  throw new InvalidSerializedError({
691
676
  reason:
692
677
  'Unable to parse WebAuthn metadata: could not extract valid authenticatorData and clientDataJSON',
@@ -703,10 +688,7 @@ function deserialize_(value: Serialized): SignatureEnvelope {
703
688
  Hex.slice(data, webauthnDataSize + 96, webauthnDataSize + 128),
704
689
  ),
705
690
  },
706
- metadata: {
707
- authenticatorData,
708
- clientDataJSON,
709
- },
691
+ metadata,
710
692
  signature: {
711
693
  r: Hex.toBigInt(
712
694
  Hex.slice(data, webauthnDataSize, webauthnDataSize + 32),
@@ -1114,23 +1096,8 @@ export function fromRpc(envelope: SignatureEnvelopeRpc): SignatureEnvelope {
1114
1096
  })
1115
1097
 
1116
1098
  // Parse webauthnData into authenticatorData and clientDataJSON
1117
- let authenticatorData: Hex.Hex | undefined
1118
- let clientDataJSON: string | undefined
1119
-
1120
- // Try to find the JSON start (clientDataJSON should start with '{')
1121
- for (let split = 37; split < webauthnDataSize; split++) {
1122
- const potentialJson = Hex.toString(Hex.slice(webauthnData, split))
1123
- if (potentialJson.startsWith('{') && potentialJson.endsWith('}')) {
1124
- try {
1125
- JSON.parse(potentialJson)
1126
- authenticatorData = Hex.slice(webauthnData, 0, split)
1127
- clientDataJSON = potentialJson
1128
- break
1129
- } catch {}
1130
- }
1131
- }
1132
-
1133
- if (!authenticatorData || !clientDataJSON)
1099
+ const metadata = WebauthnData.split(webauthnData)
1100
+ if (!metadata)
1134
1101
  throw new InvalidSerializedError({
1135
1102
  reason:
1136
1103
  'Unable to parse WebAuthn metadata: could not extract valid authenticatorData and clientDataJSON',
@@ -1138,10 +1105,7 @@ export function fromRpc(envelope: SignatureEnvelopeRpc): SignatureEnvelope {
1138
1105
  })
1139
1106
 
1140
1107
  return {
1141
- metadata: {
1142
- authenticatorData,
1143
- clientDataJSON,
1144
- },
1108
+ metadata,
1145
1109
  publicKey: {
1146
1110
  prefix: 4,
1147
1111
  x: Hex.toBigInt(envelope.pubKeyX),
@@ -225,7 +225,7 @@ describe('unwrap', () => {
225
225
  ).toThrowErrorMatchingInlineSnapshot(`
226
226
  [SignaturePreAuthorized.InvalidWrappedSignatureError: Value \`0xf8fab85604f39fd6e51aad88f6f4ce6ab8827279cfffb922663dc3ccd3e78382806b81f811192a9afa9f38335c19a3897a2676008ab611d8a61c73da6988f81a2cc37c5c712f67280aeb732e45688c9347458092b772306c6c1cf8a0f85b821079809472989d24e6e1f0bbe29a5f304d0db21a318c012e84713fb300dad99420c00000000000000000000000000000000000018398968080a0e55f870744098e6baf2f15e0faa6f8f9de283280f26f416dee8dcf9189a553c5b841c026ad99f18042ef3ac0d7a88ffbcd910eb4ccaf435b1acf684f6eb63425909642b07095e6f94edbf489ac1909caa41b0e3eee6971862939e8403c1ab4e617ca1b001165116511651165116511651165116511651165116511651165116511651165\` is an invalid pre-authorized signature.
227
227
 
228
- Details: unexpected bytes after the RLP list]
228
+ Details: RLP payload encodes a single item, but \`1\` trailing byte remains.]
229
229
  `)
230
230
  })
231
231
 
@@ -193,18 +193,22 @@ export function fromRpc<
193
193
  ): transaction extends Rpc<pending> ? Transaction<pending> : null {
194
194
  if (!transaction) return null as never
195
195
 
196
+ // Leave out `aaAuthorizationList` rather than deleting it afterwards, and
197
+ // only copy the transaction when it is present.
198
+ const { aaAuthorizationList } = transaction
196
199
  const transaction_ = ox_Transaction.fromRpc(
197
- transaction as ox_Transaction.Rpc<pending>,
200
+ (aaAuthorizationList
201
+ ? (({ aaAuthorizationList: _, ...rest }) => rest)(transaction)
202
+ : transaction) as ox_Transaction.Rpc<pending>,
198
203
  ) as Transaction<pending>
199
204
 
200
- transaction_.type = fromRpcType[transaction.type as keyof typeof fromRpcType]
205
+ transaction_.type = Object.hasOwn(fromRpcType, transaction.type)
206
+ ? fromRpcType[transaction.type as keyof typeof fromRpcType]
207
+ : (undefined as never)
201
208
 
202
- if (transaction.aaAuthorizationList) {
203
- transaction_.authorizationList = AuthorizationTempo.fromRpcList(
204
- transaction.aaAuthorizationList,
205
- )
206
- delete (transaction_ as any).aaAuthorizationList
207
- }
209
+ if (aaAuthorizationList)
210
+ transaction_.authorizationList =
211
+ AuthorizationTempo.fromRpcList(aaAuthorizationList)
208
212
  if (transaction.calls)
209
213
  transaction_.calls = transaction.calls.map((call) => ({
210
214
  to: call.to,
@@ -297,7 +301,9 @@ export function toRpc<pending extends boolean = false>(
297
301
  transaction as ox_Transaction.Transaction<pending>,
298
302
  ) as Rpc<pending>
299
303
 
300
- rpc.type = toRpcType[transaction.type as keyof typeof toRpcType]
304
+ rpc.type = Object.hasOwn(toRpcType, transaction.type)
305
+ ? toRpcType[transaction.type as keyof typeof toRpcType]
306
+ : (undefined as never)
301
307
 
302
308
  if (transaction.authorizationList)
303
309
  rpc.aaAuthorizationList = AuthorizationTempo.toRpcList(
@@ -102,9 +102,11 @@ export function fromRpc(request: Rpc): TransactionRequest {
102
102
 
103
103
  if (typeof request.type !== 'undefined')
104
104
  request_.type =
105
- Transaction.fromRpcType[
106
- request.type as keyof typeof Transaction.fromRpcType
107
- ] || request_.type
105
+ (Object.hasOwn(Transaction.fromRpcType, request.type) &&
106
+ Transaction.fromRpcType[
107
+ request.type as keyof typeof Transaction.fromRpcType
108
+ ]) ||
109
+ request_.type
108
110
 
109
111
  if (request.authorizationList)
110
112
  request_.authorizationList = AuthorizationTempo.fromRpcList(
@@ -208,10 +210,31 @@ export declare namespace fromRpc {
208
210
  * @returns An RPC request.
209
211
  */
210
212
  export function toRpc(request: TransactionRequest): Rpc {
211
- const request_rpc = ox_TransactionRequest.toRpc({
212
- ...request,
213
- authorizationList: undefined,
214
- }) as Rpc
213
+ const tempo =
214
+ typeof request.accountSimulation !== 'undefined' ||
215
+ typeof request.calls !== 'undefined' ||
216
+ typeof request.feePayer !== 'undefined' ||
217
+ typeof request.feeToken !== 'undefined' ||
218
+ typeof request.keyAuthorization !== 'undefined' ||
219
+ typeof request.nonceKey !== 'undefined' ||
220
+ typeof request.validBefore !== 'undefined' ||
221
+ typeof request.validAfter !== 'undefined' ||
222
+ request.type === 'tempo'
223
+
224
+ // A Tempo request carries `data`, `to` and `value` in `calls`, so leave
225
+ // them out rather than deleting them afterwards.
226
+ const request_rpc = ox_TransactionRequest.toRpc(
227
+ tempo
228
+ ? {
229
+ ...request,
230
+ authorizationList: undefined,
231
+ data: undefined,
232
+ input: undefined,
233
+ to: undefined,
234
+ value: undefined,
235
+ }
236
+ : { ...request, authorizationList: undefined },
237
+ ) as Rpc
215
238
 
216
239
  if (request.authorizationList)
217
240
  request_rpc.authorizationList = AuthorizationTempo.toRpcList(
@@ -258,23 +281,7 @@ export function toRpc(request: TransactionRequest): Rpc {
258
281
  })()
259
282
  if (nonceKey) request_rpc.nonceKey = nonceKey
260
283
 
261
- if (
262
- typeof request.accountSimulation !== 'undefined' ||
263
- typeof request.calls !== 'undefined' ||
264
- typeof request.feePayer !== 'undefined' ||
265
- typeof request.feeToken !== 'undefined' ||
266
- typeof request.keyAuthorization !== 'undefined' ||
267
- typeof request.nonceKey !== 'undefined' ||
268
- typeof request.validBefore !== 'undefined' ||
269
- typeof request.validAfter !== 'undefined' ||
270
- request.type === 'tempo'
271
- ) {
272
- request_rpc.type = Transaction.toRpcType.tempo
273
- delete request_rpc.data
274
- delete request_rpc.input
275
- delete request_rpc.to
276
- delete request_rpc.value
277
- }
284
+ if (tempo) request_rpc.type = Transaction.toRpcType.tempo
278
285
 
279
286
  return request_rpc
280
287
  }
@@ -618,6 +618,58 @@ describe('deserialize', () => {
618
618
  expect(deserialized.feePayerSignature).toEqual(feePayerSig)
619
619
  })
620
620
 
621
+ describe('sender marker', () => {
622
+ const signed = TxEnvelopeTempo.serialize(
623
+ { ...transaction, feePayerSignature: null },
624
+ {
625
+ signature: Secp256k1.sign({
626
+ payload: TxEnvelopeTempo.getSignPayload({
627
+ ...transaction,
628
+ feePayerSignature: null,
629
+ }),
630
+ privateKey,
631
+ }),
632
+ },
633
+ )
634
+
635
+ test('ignores sender and marker after the list', () => {
636
+ const serialized = Hex.concat(
637
+ signed,
638
+ address,
639
+ '0xfeefeefeefee',
640
+ ) as TxEnvelopeTempo.Serialized
641
+ expect(TxEnvelopeTempo.deserialize(serialized)).toEqual(
642
+ TxEnvelopeTempo.deserialize(signed),
643
+ )
644
+ })
645
+
646
+ test('behavior: marker after other trailing bytes', () => {
647
+ const serialized = Hex.concat(
648
+ signed,
649
+ '0x00',
650
+ address,
651
+ '0xfeefeefeefee',
652
+ ) as TxEnvelopeTempo.Serialized
653
+ expect(() =>
654
+ TxEnvelopeTempo.deserialize(serialized),
655
+ ).toThrowErrorMatchingInlineSnapshot(
656
+ `[Rlp.TrailingBytesError: RLP payload encodes a single item, but \`27\` trailing bytes remain.]`,
657
+ )
658
+ })
659
+
660
+ test('behavior: marker without a sender', () => {
661
+ const serialized = Hex.concat(
662
+ signed,
663
+ '0xfeefeefeefee',
664
+ ) as TxEnvelopeTempo.Serialized
665
+ expect(() =>
666
+ TxEnvelopeTempo.deserialize(serialized),
667
+ ).toThrowErrorMatchingInlineSnapshot(
668
+ `[Rlp.TrailingBytesError: RLP payload encodes a single item, but \`6\` trailing bytes remain.]`,
669
+ )
670
+ })
671
+ })
672
+
621
673
  test('feePayerSignature null', () => {
622
674
  const transaction_feePayer = TxEnvelopeTempo.from({
623
675
  ...transaction,
@@ -1,5 +1,6 @@
1
1
  import * as AccessList from '../core/AccessList.js'
2
2
  import * as Address from '../core/Address.js'
3
+ import * as Bytes from '../core/Bytes.js'
3
4
  import * as Errors from '../core/Errors.js'
4
5
  import * as Hash from '../core/Hash.js'
5
6
  import * as Hex from '../core/Hex.js'
@@ -276,7 +277,9 @@ export declare namespace assert {
276
277
  * @returns Deserialized Transaction Envelope.
277
278
  */
278
279
  export function deserialize(serialized: Serialized): Compute<TxEnvelopeTempo> {
279
- const transactionArray = Rlp.toHex(Hex.slice(serialized, 1))
280
+ const transactionArray = Rlp.toHex(
281
+ withoutSenderMarker(Hex.slice(serialized, 1)),
282
+ )
280
283
 
281
284
  const [
282
285
  chainId,
@@ -388,13 +391,13 @@ export function deserialize(serialized: Serialized): Compute<TxEnvelopeTempo> {
388
391
  feePayerSignatureOrSender !== '0x' &&
389
392
  feePayerSignatureOrSender !== undefined
390
393
  ) {
391
- if (
392
- feePayerSignatureOrSender === '0x00' ||
394
+ // A fee payer signature is a tuple, while a sender is an address string.
395
+ const sender =
396
+ typeof feePayerSignatureOrSender === 'string' &&
393
397
  Address.validate(feePayerSignatureOrSender)
394
- ) {
398
+ if (feePayerSignatureOrSender === '0x00' || sender) {
395
399
  transaction.feePayerSignature = null
396
- if (Address.validate(feePayerSignatureOrSender))
397
- transaction.from = feePayerSignatureOrSender
400
+ if (sender) transaction.from = feePayerSignatureOrSender
398
401
  } else
399
402
  transaction.feePayerSignature = Signature.fromTuple(
400
403
  feePayerSignatureOrSender as never,
@@ -435,6 +438,29 @@ export declare namespace deserialize {
435
438
  type ErrorType = Errors.GlobalErrorType
436
439
  }
437
440
 
441
+ /** Bytes after the RLP list in `rlp ‖ sender ‖ 0xfeefeefeefee`. */
442
+ const senderMarkerSize = 20 + 6
443
+
444
+ // Removes the `sender ‖ 0xfeefeefeefee` that follows the RLP list when a signed
445
+ // transaction is passed to a fee payer (e.g. by viem). The suffix is removed
446
+ // only when the list ends exactly before it, so any other trailing bytes are
447
+ // still rejected by `Rlp.toHex`.
448
+ function withoutSenderMarker(payload: Hex.Hex): Hex.Hex {
449
+ const size = Hex.size(payload)
450
+ if (size <= senderMarkerSize) return payload
451
+ if (payload.slice(-12).toLowerCase() !== 'feefeefeefee') return payload
452
+
453
+ // Read the list header (a prefix and up to 4 length bytes).
454
+ const header = Bytes.fromHex(Hex.slice(payload, 0, 5))
455
+ const prefix = header[0]!
456
+ if (prefix < 0xc0) return payload
457
+ const cursor = { bytes: header, position: 1 }
458
+ const length = Rlp.readLength(cursor, prefix, 0xc0)
459
+ if (cursor.position + length + senderMarkerSize !== size) return payload
460
+
461
+ return Hex.slice(payload, 0, -senderMarkerSize)
462
+ }
463
+
438
464
  /**
439
465
  * Converts an arbitrary transaction object into a Tempo Transaction Envelope.
440
466
  *
@@ -13,6 +13,17 @@ describe('fromChainId', () => {
13
13
  )
14
14
  })
15
15
 
16
+ test('other source chains', () => {
17
+ // Source chain 31318, zone 1: 31318 * 2^32 + 1.
18
+ expect(ZoneId.fromChainId(134_509_785_776_129)).toMatchInlineSnapshot(`1`)
19
+ expect(
20
+ ZoneId.fromChainId(134_509_785_776_129, 42_431),
21
+ ).toMatchInlineSnapshot(`1`)
22
+ expect(ZoneId.fromChainId(2 ** 32 + 4_294_967_295)).toMatchInlineSnapshot(
23
+ `4294967295`,
24
+ )
25
+ })
26
+
16
27
  test('unsupported source id', () => {
17
28
  expect(() =>
18
29
  // @ts-expect-error Unsupported source ID.
package/tempo/ZoneId.ts CHANGED
@@ -13,6 +13,9 @@ const chainIdConfig = {
13
13
 
14
14
  const defaultSourceId = 4_217
15
15
 
16
+ /** Chain IDs at or above this value carry their source chain ID in the high 32 bits. */
17
+ const genericChainIdBase = 2 ** 32
18
+
16
19
  /**
17
20
  * Base offset for deriving Presto zone chain IDs.
18
21
  */
@@ -24,7 +27,9 @@ export type SourceId = keyof typeof chainIdConfig
24
27
  /**
25
28
  * Derives a zone ID from a zone chain ID.
26
29
  *
27
- * Zone chain IDs use the base assigned to their Tempo source chain.
30
+ * Zone chain IDs use the base assigned to their Tempo source chain. Zones of other Tempo chains
31
+ * (such as devnets) encode the source chain ID in the high 32 bits and the zone ID in the low 32
32
+ * bits, so the zone ID is read from the chain ID alone.
28
33
  *
29
34
  * @example
30
35
  * ```ts twoslash
@@ -42,6 +47,7 @@ export function fromChainId(
42
47
  chainId: number,
43
48
  sourceId: SourceId = defaultSourceId,
44
49
  ): number {
50
+ if (chainId >= genericChainIdBase) return chainId % genericChainIdBase
45
51
  return chainId - getChainIdConfig(sourceId).base
46
52
  }
47
53
 
@@ -26,7 +26,7 @@ export type Pool = {
26
26
  ): { postMessage(data: unknown): void; terminate(): void }
27
27
  }
28
28
 
29
- let cachedPool: Pool | undefined
29
+ let cachedPool: Promise<Pool | undefined> | undefined
30
30
 
31
31
  /**
32
32
  * Whether the current runtime is Node.js.
@@ -42,10 +42,24 @@ const isNode =
42
42
  *
43
43
  * @internal
44
44
  */
45
- export async function resolve(): Promise<Pool | undefined> {
45
+ export function resolve(): Promise<Pool | undefined> {
46
+ // The promise is cached rather than its result, so calls made while the
47
+ // pool resolves share it instead of each building a pool (and, in browsers,
48
+ // a Blob URL that is never revoked). A failed attempt, or one that found no
49
+ // pool, is discarded so a later call can retry.
46
50
  if (cachedPool) return cachedPool
47
- cachedPool = isNode ? await resolveNode() : await resolveBrowser()
48
- return cachedPool
51
+ const promise = (isNode ? resolveNode() : resolveBrowser()).then(
52
+ (pool) => {
53
+ if (!pool && cachedPool === promise) cachedPool = undefined
54
+ return pool
55
+ },
56
+ (error) => {
57
+ if (cachedPool === promise) cachedPool = undefined
58
+ throw error
59
+ },
60
+ )
61
+ cachedPool = promise
62
+ return promise
49
63
  }
50
64
 
51
65
  /**
@@ -0,0 +1,89 @@
1
+ import * as Bytes from '../../core/Bytes.js'
2
+ import * as Hex from '../../core/Hex.js'
3
+
4
+ /** Minimum size of `authenticatorData`: rpIdHash (32) + flags (1) + signCount (4). */
5
+ const minAuthenticatorDataSize = 37
6
+
7
+ const backslash = 0x5c
8
+ const closeBrace = 0x7d
9
+ const openBrace = 0x7b
10
+ const quote = 0x22
11
+
12
+ /**
13
+ * Splits WebAuthn data (`authenticatorData || clientDataJSON`) into its parts.
14
+ *
15
+ * `clientDataJSON` is the JSON object that ends at the last byte, so its
16
+ * opening brace is the one that pairs with the last byte. One backward pass
17
+ * finds that brace and one `JSON.parse` validates the object, which keeps the
18
+ * work linear in the size of the data.
19
+ *
20
+ * @param webauthnData - The concatenated WebAuthn data.
21
+ * @returns The parts, or `undefined` when the data does not end with a JSON
22
+ * object that starts at or after byte 37.
23
+ *
24
+ * @internal
25
+ */
26
+ export function split(webauthnData: Hex.Hex): split.ReturnType | undefined {
27
+ if (Hex.size(webauthnData) <= minAuthenticatorDataSize) return undefined
28
+
29
+ // `clientDataJSON` starts after the minimum `authenticatorData`.
30
+ const bytes = Bytes.fromHex(Hex.slice(webauthnData, minAuthenticatorDataSize))
31
+ const start = objectStart(bytes)
32
+ if (start === -1) return undefined
33
+
34
+ const clientDataJSON = Bytes.toString(bytes.subarray(start))
35
+ try {
36
+ JSON.parse(clientDataJSON)
37
+ } catch {
38
+ return undefined
39
+ }
40
+ return {
41
+ authenticatorData: Hex.slice(
42
+ webauthnData,
43
+ 0,
44
+ minAuthenticatorDataSize + start,
45
+ ),
46
+ clientDataJSON,
47
+ }
48
+ }
49
+
50
+ /** @internal */
51
+ export declare namespace split {
52
+ type ReturnType = {
53
+ /** Authenticator data. */
54
+ authenticatorData: Hex.Hex
55
+ /** Client data JSON. */
56
+ clientDataJSON: string
57
+ }
58
+ }
59
+
60
+ /**
61
+ * Finds the opening brace that pairs with a closing brace in the last byte,
62
+ * or returns `-1`. Scans bytes rather than characters: UTF-8 never encodes a
63
+ * multi-byte character with an ASCII byte, so each structural byte is a
64
+ * structural character.
65
+ *
66
+ * @internal
67
+ */
68
+ function objectStart(bytes: Bytes.Bytes) {
69
+ if (bytes[bytes.length - 1] !== closeBrace) return -1
70
+
71
+ let depth = 0
72
+ let inString = false
73
+ for (let i = bytes.length - 1; i >= 0; i--) {
74
+ const byte = bytes[i]
75
+ if (byte === quote) {
76
+ if (inString) {
77
+ // A quote inside a string follows an odd number of backslashes.
78
+ let escapes = 0
79
+ while (bytes[i - 1 - escapes] === backslash) escapes++
80
+ i -= escapes
81
+ if (escapes % 2 === 1) continue
82
+ }
83
+ inString = !inString
84
+ } else if (inString) continue
85
+ else if (byte === closeBrace) depth++
86
+ else if (byte === openBrace && --depth === 0) return i
87
+ }
88
+ return -1
89
+ }
package/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  /** @internal */
2
- export const version = '0.14.66'
2
+ export const version = '0.14.68'
@@ -1,17 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.uid = uid;
4
- const size = 256;
5
- let index = size;
6
- let buffer;
7
- function uid(length = 11) {
8
- if (!buffer || index + length > size * 2) {
9
- buffer = '';
10
- index = 0;
11
- for (let i = 0; i < size; i++) {
12
- buffer += ((256 + Math.random() * 256) | 0).toString(16).substring(1);
13
- }
14
- }
15
- return buffer.substring(index, index++ + length);
16
- }
17
- //# sourceMappingURL=uid.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"uid.js","sourceRoot":"","sources":["../../../core/internal/uid.ts"],"names":[],"mappings":";;AAKA,kBASC;AAdD,MAAM,IAAI,GAAG,GAAG,CAAA;AAChB,IAAI,KAAK,GAAG,IAAI,CAAA;AAChB,IAAI,MAAc,CAAA;AAGlB,SAAgB,GAAG,CAAC,MAAM,GAAG,EAAE;IAC7B,IAAI,CAAC,MAAM,IAAI,KAAK,GAAG,MAAM,GAAG,IAAI,GAAG,CAAC,EAAE,CAAC;QACzC,MAAM,GAAG,EAAE,CAAA;QACX,KAAK,GAAG,CAAC,CAAA;QACT,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC;YAC9B,MAAM,IAAI,CAAC,CAAC,GAAG,GAAG,IAAI,CAAC,MAAM,EAAE,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAA;QACvE,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,KAAK,EAAE,GAAG,MAAM,CAAC,CAAA;AAClD,CAAC"}
@@ -1,15 +0,0 @@
1
- const size = 256;
2
- let index = size;
3
- let buffer;
4
- /** @internal */
5
- export function uid(length = 11) {
6
- if (!buffer || index + length > size * 2) {
7
- buffer = '';
8
- index = 0;
9
- for (let i = 0; i < size; i++) {
10
- buffer += ((256 + Math.random() * 256) | 0).toString(16).substring(1);
11
- }
12
- }
13
- return buffer.substring(index, index++ + length);
14
- }
15
- //# sourceMappingURL=uid.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"uid.js","sourceRoot":"","sources":["../../../core/internal/uid.ts"],"names":[],"mappings":"AAAA,MAAM,IAAI,GAAG,GAAG,CAAA;AAChB,IAAI,KAAK,GAAG,IAAI,CAAA;AAChB,IAAI,MAAc,CAAA;AAElB,gBAAgB;AAChB,MAAM,UAAU,GAAG,CAAC,MAAM,GAAG,EAAE;IAC7B,IAAI,CAAC,MAAM,IAAI,KAAK,GAAG,MAAM,GAAG,IAAI,GAAG,CAAC,EAAE,CAAC;QACzC,MAAM,GAAG,EAAE,CAAA;QACX,KAAK,GAAG,CAAC,CAAA;QACT,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC;YAC9B,MAAM,IAAI,CAAC,CAAC,GAAG,GAAG,IAAI,CAAC,MAAM,EAAE,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAA;QACvE,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,KAAK,EAAE,GAAG,MAAM,CAAC,CAAA;AAClD,CAAC"}
@@ -1,3 +0,0 @@
1
- /** @internal */
2
- export declare function uid(length?: number): string;
3
- //# sourceMappingURL=uid.d.ts.map