ox 0.14.65 → 0.14.67

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 (173) hide show
  1. package/CHANGELOG.md +68 -0
  2. package/_cjs/core/AbiError.js +25 -4
  3. package/_cjs/core/AbiError.js.map +1 -1
  4. package/_cjs/core/AbiEvent.js +18 -3
  5. package/_cjs/core/AbiEvent.js.map +1 -1
  6. package/_cjs/core/AbiFunction.js +27 -3
  7. package/_cjs/core/AbiFunction.js.map +1 -1
  8. package/_cjs/core/Address.js +1 -0
  9. package/_cjs/core/Address.js.map +1 -1
  10. package/_cjs/core/Authorization.js +29 -2
  11. package/_cjs/core/Authorization.js.map +1 -1
  12. package/_cjs/core/Keystore.js +18 -0
  13. package/_cjs/core/Keystore.js.map +1 -1
  14. package/_cjs/core/Secp256k1.js +5 -1
  15. package/_cjs/core/Secp256k1.js.map +1 -1
  16. package/_cjs/core/Signature.js +4 -2
  17. package/_cjs/core/Signature.js.map +1 -1
  18. package/_cjs/core/Siwe.js +22 -3
  19. package/_cjs/core/Siwe.js.map +1 -1
  20. package/_cjs/core/TxEnvelopeEip1559.js +2 -1
  21. package/_cjs/core/TxEnvelopeEip1559.js.map +1 -1
  22. package/_cjs/core/TxEnvelopeEip2930.js +2 -1
  23. package/_cjs/core/TxEnvelopeEip2930.js.map +1 -1
  24. package/_cjs/core/TxEnvelopeEip7702.js +5 -1
  25. package/_cjs/core/TxEnvelopeEip7702.js.map +1 -1
  26. package/_cjs/core/TxEnvelopeLegacy.js +41 -8
  27. package/_cjs/core/TxEnvelopeLegacy.js.map +1 -1
  28. package/_cjs/core/TypedData.js +7 -9
  29. package/_cjs/core/TypedData.js.map +1 -1
  30. package/_cjs/core/Value.js +8 -3
  31. package/_cjs/core/Value.js.map +1 -1
  32. package/_cjs/core/internal/abiParameters.js +6 -2
  33. package/_cjs/core/internal/abiParameters.js.map +1 -1
  34. package/_cjs/core/internal/cursor.js +30 -1
  35. package/_cjs/core/internal/cursor.js.map +1 -1
  36. package/_cjs/erc4337/UserOperation.js +1 -1
  37. package/_cjs/erc4337/UserOperation.js.map +1 -1
  38. package/_cjs/erc7821/Execute.js +41 -3
  39. package/_cjs/erc7821/Execute.js.map +1 -1
  40. package/_cjs/tempo/KeyAuthorization.js +22 -1
  41. package/_cjs/tempo/KeyAuthorization.js.map +1 -1
  42. package/_cjs/tempo/SignatureEnvelope.js +44 -50
  43. package/_cjs/tempo/SignatureEnvelope.js.map +1 -1
  44. package/_cjs/tempo/TxEnvelopeTempo.js +34 -3
  45. package/_cjs/tempo/TxEnvelopeTempo.js.map +1 -1
  46. package/_cjs/tempo/internal/webauthnData.js +57 -0
  47. package/_cjs/tempo/internal/webauthnData.js.map +1 -0
  48. package/_cjs/version.js +1 -1
  49. package/_cjs/webauthn/Registration.js +2 -6
  50. package/_cjs/webauthn/Registration.js.map +1 -1
  51. package/_esm/core/AbiError.js +64 -3
  52. package/_esm/core/AbiError.js.map +1 -1
  53. package/_esm/core/AbiEvent.js +24 -3
  54. package/_esm/core/AbiEvent.js.map +1 -1
  55. package/_esm/core/AbiFunction.js +64 -3
  56. package/_esm/core/AbiFunction.js.map +1 -1
  57. package/_esm/core/Address.js +17 -1
  58. package/_esm/core/Address.js.map +1 -1
  59. package/_esm/core/Authorization.js +47 -2
  60. package/_esm/core/Authorization.js.map +1 -1
  61. package/_esm/core/Keystore.js +33 -2
  62. package/_esm/core/Keystore.js.map +1 -1
  63. package/_esm/core/Secp256k1.js +5 -1
  64. package/_esm/core/Secp256k1.js.map +1 -1
  65. package/_esm/core/Signature.js +4 -2
  66. package/_esm/core/Signature.js.map +1 -1
  67. package/_esm/core/Siwe.js +29 -3
  68. package/_esm/core/Siwe.js.map +1 -1
  69. package/_esm/core/TxEnvelopeEip1559.js +2 -1
  70. package/_esm/core/TxEnvelopeEip1559.js.map +1 -1
  71. package/_esm/core/TxEnvelopeEip2930.js +2 -1
  72. package/_esm/core/TxEnvelopeEip2930.js.map +1 -1
  73. package/_esm/core/TxEnvelopeEip7702.js +6 -1
  74. package/_esm/core/TxEnvelopeEip7702.js.map +1 -1
  75. package/_esm/core/TxEnvelopeLegacy.js +49 -9
  76. package/_esm/core/TxEnvelopeLegacy.js.map +1 -1
  77. package/_esm/core/TypedData.js +9 -9
  78. package/_esm/core/TypedData.js.map +1 -1
  79. package/_esm/core/Value.js +14 -3
  80. package/_esm/core/Value.js.map +1 -1
  81. package/_esm/core/internal/abiParameters.js +9 -2
  82. package/_esm/core/internal/abiParameters.js.map +1 -1
  83. package/_esm/core/internal/cursor.js +32 -0
  84. package/_esm/core/internal/cursor.js.map +1 -1
  85. package/_esm/erc4337/UserOperation.js +1 -1
  86. package/_esm/erc4337/UserOperation.js.map +1 -1
  87. package/_esm/erc7821/Execute.js +76 -2
  88. package/_esm/erc7821/Execute.js.map +1 -1
  89. package/_esm/index.js +1 -1
  90. package/_esm/tempo/KeyAuthorization.js +21 -0
  91. package/_esm/tempo/KeyAuthorization.js.map +1 -1
  92. package/_esm/tempo/SignatureEnvelope.js +53 -54
  93. package/_esm/tempo/SignatureEnvelope.js.map +1 -1
  94. package/_esm/tempo/TxEnvelopeTempo.js +56 -2
  95. package/_esm/tempo/TxEnvelopeTempo.js.map +1 -1
  96. package/_esm/tempo/internal/webauthnData.js +79 -0
  97. package/_esm/tempo/internal/webauthnData.js.map +1 -0
  98. package/_esm/version.js +1 -1
  99. package/_esm/webauthn/Registration.js +2 -6
  100. package/_esm/webauthn/Registration.js.map +1 -1
  101. package/_types/core/AbiError.d.ts +51 -5
  102. package/_types/core/AbiError.d.ts.map +1 -1
  103. package/_types/core/AbiEvent.d.ts +4 -2
  104. package/_types/core/AbiEvent.d.ts.map +1 -1
  105. package/_types/core/AbiFunction.d.ts +48 -2
  106. package/_types/core/AbiFunction.d.ts.map +1 -1
  107. package/_types/core/Address.d.ts +17 -2
  108. package/_types/core/Address.d.ts.map +1 -1
  109. package/_types/core/Authorization.d.ts +24 -2
  110. package/_types/core/Authorization.d.ts.map +1 -1
  111. package/_types/core/Keystore.d.ts +31 -2
  112. package/_types/core/Keystore.d.ts.map +1 -1
  113. package/_types/core/Secp256k1.d.ts.map +1 -1
  114. package/_types/core/Signature.d.ts +1 -1
  115. package/_types/core/Signature.d.ts.map +1 -1
  116. package/_types/core/Siwe.d.ts +4 -0
  117. package/_types/core/Siwe.d.ts.map +1 -1
  118. package/_types/core/TxEnvelopeEip1559.d.ts.map +1 -1
  119. package/_types/core/TxEnvelopeEip2930.d.ts.map +1 -1
  120. package/_types/core/TxEnvelopeEip7702.d.ts +1 -1
  121. package/_types/core/TxEnvelopeEip7702.d.ts.map +1 -1
  122. package/_types/core/TxEnvelopeLegacy.d.ts.map +1 -1
  123. package/_types/core/TypedData.d.ts +3 -1
  124. package/_types/core/TypedData.d.ts.map +1 -1
  125. package/_types/core/Value.d.ts.map +1 -1
  126. package/_types/core/internal/abiParameters.d.ts +1 -1
  127. package/_types/core/internal/abiParameters.d.ts.map +1 -1
  128. package/_types/core/internal/cursor.d.ts +13 -0
  129. package/_types/core/internal/cursor.d.ts.map +1 -1
  130. package/_types/erc7821/Execute.d.ts +46 -1
  131. package/_types/erc7821/Execute.d.ts.map +1 -1
  132. package/_types/index.d.ts +1 -1
  133. package/_types/tempo/KeyAuthorization.d.ts +9 -3
  134. package/_types/tempo/KeyAuthorization.d.ts.map +1 -1
  135. package/_types/tempo/SignatureEnvelope.d.ts +1 -1
  136. package/_types/tempo/SignatureEnvelope.d.ts.map +1 -1
  137. package/_types/tempo/TxEnvelopeTempo.d.ts +30 -1
  138. package/_types/tempo/TxEnvelopeTempo.d.ts.map +1 -1
  139. package/_types/tempo/internal/webauthnData.d.ts +26 -0
  140. package/_types/tempo/internal/webauthnData.d.ts.map +1 -0
  141. package/_types/version.d.ts +1 -1
  142. package/_types/webauthn/Registration.d.ts +2 -0
  143. package/_types/webauthn/Registration.d.ts.map +1 -1
  144. package/core/AbiError.ts +81 -7
  145. package/core/AbiEvent.ts +50 -11
  146. package/core/AbiFunction.ts +76 -4
  147. package/core/Address.ts +18 -1
  148. package/core/Authorization.ts +47 -4
  149. package/core/Keystore.ts +40 -3
  150. package/core/Secp256k1.ts +5 -1
  151. package/core/Signature.ts +7 -2
  152. package/core/Siwe.ts +29 -3
  153. package/core/TxEnvelopeEip1559.ts +4 -1
  154. package/core/TxEnvelopeEip2930.ts +4 -1
  155. package/core/TxEnvelopeEip7702.ts +9 -2
  156. package/core/TxEnvelopeLegacy.ts +56 -10
  157. package/core/TypedData.ts +10 -9
  158. package/core/Value.ts +14 -3
  159. package/core/internal/abiParameters.ts +8 -2
  160. package/core/internal/cursor.ts +36 -0
  161. package/erc4337/UserOperation.ts +1 -1
  162. package/erc7821/Execute.ts +84 -3
  163. package/index.ts +1 -1
  164. package/package.json +1 -1
  165. package/tempo/KeyAuthorization.test.ts +91 -0
  166. package/tempo/KeyAuthorization.ts +32 -3
  167. package/tempo/SignatureEnvelope.test.ts +300 -28
  168. package/tempo/SignatureEnvelope.ts +62 -54
  169. package/tempo/TxEnvelopeTempo.test.ts +310 -0
  170. package/tempo/TxEnvelopeTempo.ts +64 -1
  171. package/tempo/internal/webauthnData.ts +89 -0
  172. package/version.ts +1 -1
  173. package/webauthn/Registration.ts +4 -6
@@ -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
 
@@ -437,6 +437,20 @@ describe('assert', () => {
437
437
  expect(() => SignatureEnvelope.assert(signatureWithoutType)).not.toThrow()
438
438
  })
439
439
 
440
+ test('error: throws on keychain inner signature', () => {
441
+ for (const version of ['v1', 'v2'] as const)
442
+ expect(() =>
443
+ SignatureEnvelope.assert({
444
+ inner: signature_keychain_secp256k1,
445
+ type: 'keychain',
446
+ userAddress: '0x1234567890123456789012345678901234567890',
447
+ version,
448
+ }),
449
+ ).toThrowErrorMatchingInlineSnapshot(
450
+ `[SignatureEnvelope.InvalidApprovalError: Invalid owner approval: keychain signatures cannot wrap a keychain signature.]`,
451
+ )
452
+ })
453
+
440
454
  test('error: throws on invalid inner signature', () => {
441
455
  expect(() =>
442
456
  SignatureEnvelope.assert({
@@ -472,6 +486,23 @@ describe('assert', () => {
472
486
  `[SignatureEnvelope.CoercionError: Unable to coerce value (\`{"r":"0#__bigint","s":"0#__bigint"}\`) to a valid signature envelope.]`,
473
487
  )
474
488
  })
489
+
490
+ test('error: throws on unsupported type', () => {
491
+ expect(() =>
492
+ SignatureEnvelope.assert({ type: 'webauthn' } as any),
493
+ ).toThrowErrorMatchingInlineSnapshot(
494
+ `[SignatureEnvelope.CoercionError: Unable to coerce value (\`{"type":"webauthn"}\`) to a valid signature envelope.]`,
495
+ )
496
+ expect(() =>
497
+ SignatureEnvelope.assert({
498
+ inner: { type: 'bogus' },
499
+ type: 'keychain',
500
+ userAddress: '0x1234567890123456789012345678901234567890',
501
+ } as any),
502
+ ).toThrowErrorMatchingInlineSnapshot(
503
+ `[SignatureEnvelope.CoercionError: Unable to coerce value (\`{"type":"bogus"}\`) to a valid signature envelope.]`,
504
+ )
505
+ })
475
506
  })
476
507
 
477
508
  describe('deserialize', () => {
@@ -646,6 +677,42 @@ describe('deserialize', () => {
646
677
  )
647
678
  })
648
679
 
680
+ test('error: throws on WebAuthn signature above the maximum size', () => {
681
+ const authenticatorData = `0x${'00'.repeat(37)}` as const
682
+ // 37 (authenticatorData) + 1883 (clientDataJSON) + 128 = 2048 bytes
683
+ const max = {
684
+ ...signature_webauthn,
685
+ metadata: {
686
+ authenticatorData,
687
+ clientDataJSON: JSON.stringify({ challenge: 'a'.repeat(1867) }),
688
+ },
689
+ }
690
+ const serialized = SignatureEnvelope.serialize(max)
691
+ expect(Hex.size(serialized)).toMatchInlineSnapshot(`2049`)
692
+ expect(SignatureEnvelope.deserialize(serialized)).toMatchObject({
693
+ metadata: max.metadata,
694
+ })
695
+
696
+ expect(() =>
697
+ SignatureEnvelope.deserialize(
698
+ SignatureEnvelope.serialize({
699
+ ...max,
700
+ metadata: {
701
+ authenticatorData,
702
+ clientDataJSON: JSON.stringify({ challenge: 'a'.repeat(1868) }),
703
+ },
704
+ }),
705
+ ),
706
+ ).toThrow(
707
+ 'Invalid WebAuthn signature envelope size: expected at most 2048 bytes, got 2049 bytes',
708
+ )
709
+ expect(() =>
710
+ SignatureEnvelope.deserialize(`0x02${'41'.repeat(40_000)}`),
711
+ ).toThrow(
712
+ 'Invalid WebAuthn signature envelope size: expected at most 2048 bytes, got 40000 bytes',
713
+ )
714
+ })
715
+
649
716
  test('error: throws on invalid clientDataJSON', () => {
650
717
  // Create a signature with invalid JSON (not properly formatted)
651
718
  const invalidMetadata = {
@@ -668,6 +735,58 @@ describe('deserialize', () => {
668
735
  )
669
736
  })
670
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
+
671
790
  test('error: throws on unknown type identifier', () => {
672
791
  const unknownType = `0xff${'00'.repeat(129)}` as const
673
792
  expect(() =>
@@ -767,6 +886,48 @@ describe('deserialize', () => {
767
886
  })
768
887
  })
769
888
 
889
+ test('behavior: deserializes 65-byte inner starting with a keychain type as secp256k1', () => {
890
+ const userAddress = '0x1234567890123456789012345678901234567890'
891
+ const inner = `0x03${'11'.repeat(31)}${'22'.repeat(32)}1b` as const
892
+ expect(
893
+ SignatureEnvelope.deserialize(Hex.concat('0x04', userAddress, inner)),
894
+ ).toMatchInlineSnapshot(`
895
+ {
896
+ "inner": {
897
+ "signature": {
898
+ "r": 1387092735655350257678194091249907229492296691307152590056002308428126032145n,
899
+ "s": 15438945231642159389809464667825054380435997955418741871927677867721750618658n,
900
+ "yParity": 0,
901
+ },
902
+ "type": "secp256k1",
903
+ },
904
+ "type": "keychain",
905
+ "userAddress": "0x1234567890123456789012345678901234567890",
906
+ "version": "v2",
907
+ }
908
+ `)
909
+ })
910
+
911
+ test('error: throws on keychain inner signature', () => {
912
+ const userAddress = '0x1234567890123456789012345678901234567890'
913
+ const inner = SignatureEnvelope.serialize(signature_keychain_secp256k1)
914
+ for (const typeId of ['0x03', '0x04'] as const)
915
+ expect(() =>
916
+ SignatureEnvelope.deserialize(Hex.concat(typeId, userAddress, inner)),
917
+ ).toThrow(
918
+ 'Unable to deserialize signature envelope: keychain signatures cannot wrap a keychain signature',
919
+ )
920
+
921
+ // 2000 nested keychains
922
+ const nested = Hex.concat(
923
+ `0x${`03${userAddress.slice(2)}`.repeat(2000)}`,
924
+ Signature.toHex(signature_secp256k1),
925
+ )
926
+ expect(() => SignatureEnvelope.deserialize(nested)).toThrow(
927
+ SignatureEnvelope.InvalidSerializedError,
928
+ )
929
+ })
930
+
770
931
  test('error: throws on invalid keychain signature length', () => {
771
932
  // Keychain signature too short (must be at least 21 bytes: 1 type + 20 address)
772
933
  const invalidSig = `0x03${'00'.repeat(10)}` as const
@@ -1907,6 +2068,17 @@ describe('validate', () => {
1907
2068
  expect(SignatureEnvelope.validate({} as any)).toBe(false)
1908
2069
  })
1909
2070
 
2071
+ test('behavior: returns false for unsupported type', () => {
2072
+ expect(SignatureEnvelope.validate({ type: 'webauthn' } as any)).toBe(false)
2073
+ expect(
2074
+ SignatureEnvelope.validate({
2075
+ inner: { type: 'bogus' },
2076
+ type: 'keychain',
2077
+ userAddress: '0x1234567890123456789012345678901234567890',
2078
+ } as any),
2079
+ ).toBe(false)
2080
+ })
2081
+
1910
2082
  test('behavior: returns false for incomplete signature', () => {
1911
2083
  expect(
1912
2084
  SignatureEnvelope.validate({
@@ -2295,6 +2467,85 @@ describe('fromRpc', () => {
2295
2467
  type: 'webAuthn',
2296
2468
  })
2297
2469
  })
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
+
2507
+ test('error: throws on WebAuthn data above the maximum size', () => {
2508
+ const rpc = {
2509
+ pubKeyX: Hex.fromNumber(publicKey.x, { size: 32 }),
2510
+ pubKeyY: Hex.fromNumber(publicKey.y, { size: 32 }),
2511
+ r: Hex.fromNumber(p256Signature.r, { size: 32 }),
2512
+ s: Hex.fromNumber(p256Signature.s, { size: 32 }),
2513
+ type: 'webAuthn',
2514
+ } as const
2515
+ const authenticatorData = `0x${'00'.repeat(37)}` as const
2516
+ const clientDataJSON = JSON.stringify({ challenge: 'a'.repeat(1867) })
2517
+
2518
+ // 37 (authenticatorData) + 1883 (clientDataJSON) = 1920 bytes
2519
+ expect(
2520
+ SignatureEnvelope.fromRpc({
2521
+ ...rpc,
2522
+ webauthnData: Hex.concat(
2523
+ authenticatorData,
2524
+ Hex.fromString(clientDataJSON),
2525
+ ),
2526
+ }),
2527
+ ).toMatchObject({ metadata: { authenticatorData, clientDataJSON } })
2528
+
2529
+ expect(() =>
2530
+ SignatureEnvelope.fromRpc({
2531
+ ...rpc,
2532
+ webauthnData: Hex.concat(
2533
+ authenticatorData,
2534
+ Hex.fromString(JSON.stringify({ challenge: 'a'.repeat(1868) })),
2535
+ ),
2536
+ }),
2537
+ ).toThrow(
2538
+ 'Invalid WebAuthn data size: expected at most 1920 bytes, got 1921 bytes',
2539
+ )
2540
+ expect(() =>
2541
+ SignatureEnvelope.fromRpc({
2542
+ ...rpc,
2543
+ webauthnData: `0x${'41'.repeat(40_000)}`,
2544
+ }),
2545
+ ).toThrow(
2546
+ 'Invalid WebAuthn data size: expected at most 1920 bytes, got 40000 bytes',
2547
+ )
2548
+ })
2298
2549
  })
2299
2550
 
2300
2551
  describe('keychain', () => {
@@ -2425,6 +2676,36 @@ describe('fromRpc', () => {
2425
2676
 
2426
2677
  expect(envelope.keyId).toBe('0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c')
2427
2678
  })
2679
+
2680
+ test('error: throws on keychain inner signature', () => {
2681
+ const inner = {
2682
+ signature: {
2683
+ r: '0xa2bb71146c20ce932456c043ebb2973ed205e07cd32c35a60bdefca1285fd132',
2684
+ s: '0x7cba10692bccdbfba9a215418443c2903dbee6fe5cb55c91172e47efc607840e',
2685
+ type: 'secp256k1',
2686
+ yParity: '0x1',
2687
+ },
2688
+ userAddress: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
2689
+ } as const
2690
+ expect(() =>
2691
+ SignatureEnvelope.fromRpc({
2692
+ signature: { ...inner, type: 'keychain' },
2693
+ type: 'keychain',
2694
+ userAddress: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
2695
+ }),
2696
+ ).toThrowErrorMatchingInlineSnapshot(
2697
+ `[SignatureEnvelope.InvalidApprovalError: Invalid owner approval: keychain signatures cannot wrap a keychain signature.]`,
2698
+ )
2699
+ // inner keychain without an explicit type
2700
+ expect(() =>
2701
+ SignatureEnvelope.fromRpc({
2702
+ signature: inner as never,
2703
+ type: 'keychain',
2704
+ userAddress: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
2705
+ version: 'v1',
2706
+ }),
2707
+ ).toThrow(SignatureEnvelope.InvalidApprovalError)
2708
+ })
2428
2709
  })
2429
2710
  })
2430
2711
 
@@ -3006,18 +3287,21 @@ describe('configurable', () => {
3006
3287
  }),
3007
3288
  ).toThrowError(SignatureEnvelope.InvalidApprovalError)
3008
3289
  })
3009
- test('rejects V1 wrappers around recursively nested configurable delegates', () => {
3010
- let inner: SignatureEnvelope.SignatureEnvelope = envelope
3011
- for (let depth = 0; depth < 3; depth++) {
3012
- inner = { inner, type: 'keychain', userAddress: account, version: 'v2' }
3013
- const valid: SignatureEnvelope.Keychain = {
3290
+ test('rejects keychain wrappers around keychain-wrapped configurable delegates', () => {
3291
+ const inner: SignatureEnvelope.Keychain = {
3292
+ inner: envelope,
3293
+ type: 'keychain',
3294
+ userAddress: account,
3295
+ version: 'v2',
3296
+ }
3297
+ expect(SignatureEnvelope.validate(inner)).toBe(true)
3298
+ for (const version of ['v1', 'v2'] as const) {
3299
+ const invalid: SignatureEnvelope.Keychain = {
3014
3300
  inner,
3015
3301
  type: 'keychain',
3016
3302
  userAddress: account,
3017
- version: 'v2',
3303
+ version,
3018
3304
  }
3019
- const invalid = { ...valid, version: 'v1' } as const
3020
- expect(SignatureEnvelope.validate(valid)).toBe(true)
3021
3305
  expect(SignatureEnvelope.validate(invalid)).toBe(false)
3022
3306
  expect(() => SignatureEnvelope.serialize(invalid)).toThrowError(
3023
3307
  SignatureEnvelope.InvalidApprovalError,
@@ -3029,32 +3313,20 @@ describe('configurable', () => {
3029
3313
  SignatureEnvelope.fromRpc({
3030
3314
  type: 'keychain',
3031
3315
  userAddress: account,
3032
- version: 'v1',
3316
+ version,
3033
3317
  signature: SignatureEnvelope.toRpc(inner),
3034
3318
  }),
3035
3319
  ).toThrowError(SignatureEnvelope.InvalidApprovalError)
3036
3320
  expect(() =>
3037
3321
  SignatureEnvelope.deserialize(
3038
- Hex.concat('0x03', account, SignatureEnvelope.serialize(inner)),
3322
+ Hex.concat(
3323
+ version === 'v1' ? '0x03' : '0x04',
3324
+ account,
3325
+ SignatureEnvelope.serialize(inner),
3326
+ ),
3039
3327
  ),
3040
- ).toThrowError(SignatureEnvelope.InvalidApprovalError)
3041
- expect(() =>
3042
- SignatureEnvelope.serialize({ ...valid, inner: invalid }),
3043
- ).toThrowError(SignatureEnvelope.InvalidApprovalError)
3328
+ ).toThrowError(SignatureEnvelope.InvalidSerializedError)
3044
3329
  }
3045
- expect(
3046
- SignatureEnvelope.validate({
3047
- inner: {
3048
- inner: primitive,
3049
- type: 'keychain',
3050
- userAddress: account,
3051
- version: 'v2',
3052
- },
3053
- type: 'keychain',
3054
- userAddress: account,
3055
- version: 'v1',
3056
- }),
3057
- ).toBe(true)
3058
3330
  })
3059
3331
  test('RPC rejects 64-byte values that resemble primitive signatures', () => {
3060
3332
  expect(() =>
@@ -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'
@@ -27,6 +28,12 @@ const serializedKeychainType = '0x03'
27
28
  const serializedKeychainV2Type = '0x04'
28
29
  const serializedConfigurableType = '0x05'
29
30
 
31
+ /**
32
+ * Maximum WebAuthn signature size in bytes, excluding the type identifier
33
+ * (Tempo's `MAX_WEBAUTHN_SIGNATURE_LENGTH`).
34
+ */
35
+ const maxWebAuthnSize = 2048
36
+
30
37
  type EnvelopeOneOf<
31
38
  union extends object,
32
39
  keys extends PropertyKey,
@@ -307,7 +314,7 @@ export type Type = (typeof types)[number]
307
314
  * ```
308
315
  *
309
316
  * @param envelope - The signature envelope to assert.
310
- * @throws `CoercionError` if the envelope type cannot be determined.
317
+ * @throws `CoercionError` if the envelope type cannot be determined or is not supported.
311
318
  */
312
319
  export function assert(envelope: PartialBy<SignatureEnvelope, 'type'>): void {
313
320
  const type = getType(envelope)
@@ -362,7 +369,7 @@ export function assert(envelope: PartialBy<SignatureEnvelope, 'type'>): void {
362
369
 
363
370
  if (type === 'keychain') {
364
371
  const keychain = envelope as Keychain
365
- assertKeychainVersion(keychain)
372
+ assertKeychainInner(keychain)
366
373
  assert(keychain.inner)
367
374
  return
368
375
  }
@@ -372,6 +379,8 @@ export function assert(envelope: PartialBy<SignatureEnvelope, 'type'>): void {
372
379
  assertConfigurable(configurable)
373
380
  return
374
381
  }
382
+
383
+ throw new CoercionError({ envelope })
375
384
  }
376
385
 
377
386
  export declare namespace assert {
@@ -385,11 +394,13 @@ export declare namespace assert {
385
394
  | Errors.GlobalErrorType
386
395
  }
387
396
 
388
- function assertKeychainVersion(keychain: Keychain): void {
389
- if (keychain.version !== 'v1') return
390
- let inner = keychain.inner
391
- while (getType(inner) === 'keychain') inner = (inner as Keychain).inner
392
- if (getType(inner) === 'configurable')
397
+ function assertKeychainInner(keychain: Keychain): void {
398
+ const type = getType(keychain.inner)
399
+ if (type === 'keychain')
400
+ throw new InvalidApprovalError({
401
+ reason: 'keychain signatures cannot wrap a keychain signature',
402
+ })
403
+ if (keychain.version === 'v1' && type === 'configurable')
393
404
  throw new InvalidApprovalError({
394
405
  reason: 'configurable access keys require keychain V2',
395
406
  })
@@ -642,35 +653,25 @@ function deserialize_(value: Serialized): SignatureEnvelope {
642
653
  if (typeId === serializedWebAuthnType) {
643
654
  // WebAuthn: variable (webauthnData) + 32 (r) + 32 (s) + 32 (pubKeyX) + 32 (pubKeyY)
644
655
  // Minimum: 128 bytes (at least some authenticator data + signature components)
656
+ // Maximum: 2048 bytes
645
657
  if (dataSize < 128)
646
658
  throw new InvalidSerializedError({
647
659
  reason: `Invalid WebAuthn signature envelope size: expected at least 128 bytes, got ${dataSize} bytes`,
648
660
  serialized,
649
661
  })
662
+ if (dataSize > maxWebAuthnSize)
663
+ throw new InvalidSerializedError({
664
+ reason: `Invalid WebAuthn signature envelope size: expected at most ${maxWebAuthnSize} bytes, got ${dataSize} bytes`,
665
+ serialized,
666
+ })
650
667
 
651
668
  const webauthnDataSize = dataSize - 128
652
669
  const webauthnData = Hex.slice(data, 0, webauthnDataSize)
653
670
 
654
671
  // Parse webauthnData into authenticatorData and clientDataJSON
655
672
  // According to the Rust code, it's authenticatorData || clientDataJSON
656
- // We need to find the split point (minimum authenticatorData is 37 bytes)
657
- let authenticatorData: Hex.Hex | undefined
658
- let clientDataJSON: string | undefined
659
-
660
- // Try to find the JSON start (clientDataJSON should start with '{')
661
- for (let split = 37; split < webauthnDataSize; split++) {
662
- const potentialJson = Hex.toString(Hex.slice(webauthnData, split))
663
- if (potentialJson.startsWith('{') && potentialJson.endsWith('}')) {
664
- try {
665
- JSON.parse(potentialJson)
666
- authenticatorData = Hex.slice(webauthnData, 0, split)
667
- clientDataJSON = potentialJson
668
- break
669
- } catch {}
670
- }
671
- }
672
-
673
- if (!authenticatorData || !clientDataJSON)
673
+ const metadata = WebauthnData.split(webauthnData)
674
+ if (!metadata)
674
675
  throw new InvalidSerializedError({
675
676
  reason:
676
677
  'Unable to parse WebAuthn metadata: could not extract valid authenticatorData and clientDataJSON',
@@ -687,10 +688,7 @@ function deserialize_(value: Serialized): SignatureEnvelope {
687
688
  Hex.slice(data, webauthnDataSize + 96, webauthnDataSize + 128),
688
689
  ),
689
690
  },
690
- metadata: {
691
- authenticatorData,
692
- clientDataJSON,
693
- },
691
+ metadata,
694
692
  signature: {
695
693
  r: Hex.toBigInt(
696
694
  Hex.slice(data, webauthnDataSize, webauthnDataSize + 32),
@@ -708,11 +706,22 @@ function deserialize_(value: Serialized): SignatureEnvelope {
708
706
  typeId === serializedKeychainV2Type
709
707
  ) {
710
708
  const userAddress = Hex.slice(data, 0, 20)
711
- const inner = deserialize_(Hex.slice(data, 20))
709
+ const inner = Hex.slice(data, 20)
710
+ // Reject nested keychains before decoding their contents.
711
+ if (
712
+ Hex.size(inner) !== 65 &&
713
+ [serializedKeychainType, serializedKeychainV2Type].includes(
714
+ Hex.slice(inner, 0, 1),
715
+ )
716
+ )
717
+ throw new InvalidSerializedError({
718
+ reason: 'keychain signatures cannot wrap a keychain signature',
719
+ serialized,
720
+ })
712
721
 
713
722
  const envelope = {
714
723
  userAddress,
715
- inner,
724
+ inner: deserialize_(inner),
716
725
  type: 'keychain',
717
726
  version: typeId === serializedKeychainV2Type ? 'v2' : 'v1',
718
727
  } satisfies Keychain
@@ -1079,24 +1088,16 @@ export function fromRpc(envelope: SignatureEnvelopeRpc): SignatureEnvelope {
1079
1088
  const webauthnData = envelope.webauthnData
1080
1089
  const webauthnDataSize = Hex.size(webauthnData)
1081
1090
 
1082
- // Parse webauthnData into authenticatorData and clientDataJSON
1083
- let authenticatorData: Hex.Hex | undefined
1084
- let clientDataJSON: string | undefined
1085
-
1086
- // Try to find the JSON start (clientDataJSON should start with '{')
1087
- for (let split = 37; split < webauthnDataSize; split++) {
1088
- const potentialJson = Hex.toString(Hex.slice(webauthnData, split))
1089
- if (potentialJson.startsWith('{') && potentialJson.endsWith('}')) {
1090
- try {
1091
- JSON.parse(potentialJson)
1092
- authenticatorData = Hex.slice(webauthnData, 0, split)
1093
- clientDataJSON = potentialJson
1094
- break
1095
- } catch {}
1096
- }
1097
- }
1091
+ // The signature and public key take 128 bytes of the maximum size.
1092
+ if (webauthnDataSize > maxWebAuthnSize - 128)
1093
+ throw new InvalidSerializedError({
1094
+ reason: `Invalid WebAuthn data size: expected at most ${maxWebAuthnSize - 128} bytes, got ${webauthnDataSize} bytes`,
1095
+ serialized: webauthnData,
1096
+ })
1098
1097
 
1099
- if (!authenticatorData || !clientDataJSON)
1098
+ // Parse webauthnData into authenticatorData and clientDataJSON
1099
+ const metadata = WebauthnData.split(webauthnData)
1100
+ if (!metadata)
1100
1101
  throw new InvalidSerializedError({
1101
1102
  reason:
1102
1103
  'Unable to parse WebAuthn metadata: could not extract valid authenticatorData and clientDataJSON',
@@ -1104,10 +1105,7 @@ export function fromRpc(envelope: SignatureEnvelopeRpc): SignatureEnvelope {
1104
1105
  })
1105
1106
 
1106
1107
  return {
1107
- metadata: {
1108
- authenticatorData,
1109
- clientDataJSON,
1110
- },
1108
+ metadata,
1111
1109
  publicKey: {
1112
1110
  prefix: 4,
1113
1111
  x: Hex.toBigInt(envelope.pubKeyX),
@@ -1126,10 +1124,20 @@ export function fromRpc(envelope: SignatureEnvelopeRpc): SignatureEnvelope {
1126
1124
  ('userAddress' in envelope && 'signature' in envelope)
1127
1125
  ) {
1128
1126
  const keychain = envelope as KeychainRpc
1127
+ const { signature } = keychain
1128
+ // Reject nested keychains before decoding their contents.
1129
+ if (
1130
+ typeof signature === 'object' &&
1131
+ (signature.type === 'keychain' ||
1132
+ ('userAddress' in signature && 'signature' in signature))
1133
+ )
1134
+ throw new InvalidApprovalError({
1135
+ reason: 'keychain signatures cannot wrap a keychain signature',
1136
+ })
1129
1137
  const result = {
1130
1138
  type: 'keychain',
1131
1139
  userAddress: keychain.userAddress,
1132
- inner: fromRpc(keychain.signature),
1140
+ inner: fromRpc(signature),
1133
1141
  ...(keychain.keyId ? { keyId: keychain.keyId } : {}),
1134
1142
  ...(keychain.version ? { version: keychain.version } : {}),
1135
1143
  } satisfies Keychain
@@ -1460,7 +1468,7 @@ export function toRpc<const envelope extends toRpc.Input>(
1460
1468
 
1461
1469
  if (type === 'keychain') {
1462
1470
  const keychain = envelope as Keychain
1463
- assertKeychainVersion(keychain)
1471
+ assertKeychainInner(keychain)
1464
1472
  return {
1465
1473
  type: 'keychain',
1466
1474
  userAddress: keychain.userAddress,