ox 1.8.15 → 1.8.16

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 (160) hide show
  1. package/CHANGELOG.md +66 -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 +4 -2
  7. package/dist/core/AbiEvent.d.ts.map +1 -1
  8. package/dist/core/AbiEvent.js +62 -36
  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/Address.d.ts +17 -2
  15. package/dist/core/Address.d.ts.map +1 -1
  16. package/dist/core/Address.js +17 -1
  17. package/dist/core/Address.js.map +1 -1
  18. package/dist/core/Authorization.d.ts +24 -2
  19. package/dist/core/Authorization.d.ts.map +1 -1
  20. package/dist/core/Authorization.js +29 -2
  21. package/dist/core/Authorization.js.map +1 -1
  22. package/dist/core/Keystore.d.ts +31 -2
  23. package/dist/core/Keystore.d.ts.map +1 -1
  24. package/dist/core/Keystore.js +28 -2
  25. package/dist/core/Keystore.js.map +1 -1
  26. package/dist/core/Secp256k1.d.ts.map +1 -1
  27. package/dist/core/Secp256k1.js +4 -1
  28. package/dist/core/Secp256k1.js.map +1 -1
  29. package/dist/core/Signature.d.ts +1 -1
  30. package/dist/core/Signature.d.ts.map +1 -1
  31. package/dist/core/Signature.js +4 -2
  32. package/dist/core/Signature.js.map +1 -1
  33. package/dist/core/Siwe.d.ts +4 -0
  34. package/dist/core/Siwe.d.ts.map +1 -1
  35. package/dist/core/Siwe.js +29 -3
  36. package/dist/core/Siwe.js.map +1 -1
  37. package/dist/core/TxEnvelopeEip1559.d.ts.map +1 -1
  38. package/dist/core/TxEnvelopeEip1559.js +2 -1
  39. package/dist/core/TxEnvelopeEip1559.js.map +1 -1
  40. package/dist/core/TxEnvelopeEip2930.d.ts.map +1 -1
  41. package/dist/core/TxEnvelopeEip2930.js +2 -1
  42. package/dist/core/TxEnvelopeEip2930.js.map +1 -1
  43. package/dist/core/TxEnvelopeEip7702.d.ts +1 -1
  44. package/dist/core/TxEnvelopeEip7702.d.ts.map +1 -1
  45. package/dist/core/TxEnvelopeEip7702.js +6 -1
  46. package/dist/core/TxEnvelopeEip7702.js.map +1 -1
  47. package/dist/core/TxEnvelopeLegacy.d.ts.map +1 -1
  48. package/dist/core/TxEnvelopeLegacy.js +49 -7
  49. package/dist/core/TxEnvelopeLegacy.js.map +1 -1
  50. package/dist/core/TypedData.d.ts +3 -1
  51. package/dist/core/TypedData.d.ts.map +1 -1
  52. package/dist/core/TypedData.js +9 -9
  53. package/dist/core/TypedData.js.map +1 -1
  54. package/dist/core/Value.d.ts.map +1 -1
  55. package/dist/core/Value.js +14 -3
  56. package/dist/core/Value.js.map +1 -1
  57. package/dist/core/internal/abiParameters.d.ts +1 -1
  58. package/dist/core/internal/abiParameters.d.ts.map +1 -1
  59. package/dist/core/internal/abiParameters.js +7 -2
  60. package/dist/core/internal/abiParameters.js.map +1 -1
  61. package/dist/core/internal/cursor.d.ts +13 -0
  62. package/dist/core/internal/cursor.d.ts.map +1 -1
  63. package/dist/core/internal/cursor.js +27 -0
  64. package/dist/core/internal/cursor.js.map +1 -1
  65. package/dist/erc4337/UserOperation.d.ts.map +1 -1
  66. package/dist/erc4337/UserOperation.js +1 -3
  67. package/dist/erc4337/UserOperation.js.map +1 -1
  68. package/dist/erc7821/Execute.d.ts +45 -0
  69. package/dist/erc7821/Execute.d.ts.map +1 -1
  70. package/dist/erc7821/Execute.js +70 -2
  71. package/dist/erc7821/Execute.js.map +1 -1
  72. package/dist/index.d.ts +1 -1
  73. package/dist/index.js +1 -1
  74. package/dist/tempo/KeyAuthorization.d.ts +9 -3
  75. package/dist/tempo/KeyAuthorization.d.ts.map +1 -1
  76. package/dist/tempo/KeyAuthorization.js +16 -0
  77. package/dist/tempo/KeyAuthorization.js.map +1 -1
  78. package/dist/tempo/SignatureEnvelope.d.ts +1 -1
  79. package/dist/tempo/SignatureEnvelope.d.ts.map +1 -1
  80. package/dist/tempo/SignatureEnvelope.js +46 -13
  81. package/dist/tempo/SignatureEnvelope.js.map +1 -1
  82. package/dist/tempo/TransactionRequest.d.ts +1 -1
  83. package/dist/tempo/TransactionRequest.d.ts.map +1 -1
  84. package/dist/tempo/TransactionRequest.js +5 -2
  85. package/dist/tempo/TransactionRequest.js.map +1 -1
  86. package/dist/tempo/TxEnvelopeTempo.d.ts +30 -1
  87. package/dist/tempo/TxEnvelopeTempo.d.ts.map +1 -1
  88. package/dist/tempo/TxEnvelopeTempo.js +51 -2
  89. package/dist/tempo/TxEnvelopeTempo.js.map +1 -1
  90. package/dist/webauthn/Registration.d.ts +2 -0
  91. package/dist/webauthn/Registration.d.ts.map +1 -1
  92. package/dist/webauthn/Registration.js +2 -6
  93. package/dist/webauthn/Registration.js.map +1 -1
  94. package/dist/zod/tempo/KeyAuthorization.d.ts.map +1 -1
  95. package/dist/zod/tempo/KeyAuthorization.js +4 -2
  96. package/dist/zod/tempo/KeyAuthorization.js.map +1 -1
  97. package/dist/zod/tempo/SignatureEnvelope.d.ts.map +1 -1
  98. package/dist/zod/tempo/SignatureEnvelope.js +24 -14
  99. package/dist/zod/tempo/SignatureEnvelope.js.map +1 -1
  100. package/package.json +1 -1
  101. package/src/core/AbiError.ts +82 -8
  102. package/src/core/AbiEvent.ts +90 -45
  103. package/src/core/AbiFunction.ts +76 -4
  104. package/src/core/Address.ts +18 -1
  105. package/src/core/Authorization.ts +34 -4
  106. package/src/core/Keystore.ts +40 -3
  107. package/src/core/Secp256k1.ts +4 -1
  108. package/src/core/Signature.ts +7 -3
  109. package/src/core/Siwe.ts +29 -3
  110. package/src/core/TxEnvelopeEip1559.ts +4 -1
  111. package/src/core/TxEnvelopeEip2930.ts +4 -1
  112. package/src/core/TxEnvelopeEip7702.ts +9 -2
  113. package/src/core/TxEnvelopeLegacy.ts +58 -7
  114. package/src/core/TypedData.ts +10 -9
  115. package/src/core/Value.ts +14 -3
  116. package/src/core/_test/AbiError.test.ts +105 -2
  117. package/src/core/_test/AbiEvent.fuzz.ts +1 -1
  118. package/src/core/_test/AbiEvent.test-d.ts +46 -0
  119. package/src/core/_test/AbiEvent.test.ts +563 -11
  120. package/src/core/_test/AbiFunction.test.ts +74 -2
  121. package/src/core/_test/AbiParameters.adversarial.fuzz.ts +1 -0
  122. package/src/core/_test/AbiParameters.decode.test.ts +110 -0
  123. package/src/core/_test/AbiParameters.encode.test.ts +26 -1
  124. package/src/core/_test/Address.test.ts +41 -0
  125. package/src/core/_test/Authorization.test.ts +130 -0
  126. package/src/core/_test/Keystore.test.ts +65 -1
  127. package/src/core/_test/Secp256k1.test.ts +36 -0
  128. package/src/core/_test/Signature.test.ts +102 -0
  129. package/src/core/_test/Siwe.test.ts +123 -0
  130. package/src/core/_test/TransactionEnvelopeEip1559.test.ts +28 -0
  131. package/src/core/_test/TransactionEnvelopeEip2930.test.ts +28 -0
  132. package/src/core/_test/TransactionEnvelopeEip7702.test.ts +133 -0
  133. package/src/core/_test/TransactionEnvelopeLegacy.test.ts +322 -1
  134. package/src/core/_test/TypedData.test.ts +178 -0
  135. package/src/core/_test/Value.test.ts +21 -0
  136. package/src/core/_test/internal/cursor.test.ts +35 -0
  137. package/src/core/internal/abiParameters.ts +7 -2
  138. package/src/core/internal/cursor.ts +36 -0
  139. package/src/erc4337/UserOperation.ts +3 -4
  140. package/src/erc4337/_test/UserOperation.test.ts +59 -1
  141. package/src/erc7821/Execute.ts +83 -2
  142. package/src/erc7821/_test/Execute.test.ts +217 -1
  143. package/src/index.ts +1 -1
  144. package/src/tempo/KeyAuthorization.test.ts +91 -0
  145. package/src/tempo/KeyAuthorization.ts +32 -3
  146. package/src/tempo/SignatureEnvelope.test.ts +211 -27
  147. package/src/tempo/SignatureEnvelope.ts +55 -11
  148. package/src/tempo/TransactionRequest.test.ts +30 -0
  149. package/src/tempo/TransactionRequest.ts +7 -2
  150. package/src/tempo/TxEnvelopeTempo.test.ts +310 -0
  151. package/src/tempo/TxEnvelopeTempo.ts +64 -1
  152. package/src/tempo/configurableAccounts.e2e.test.ts +1 -1
  153. package/src/version.ts +1 -1
  154. package/src/webauthn/Registration.ts +4 -6
  155. package/src/webauthn/_test/Registration.browser.test.ts +14 -0
  156. package/src/webauthn/_test/Registration.test.ts +60 -0
  157. package/src/zod/tempo/KeyAuthorization.ts +12 -5
  158. package/src/zod/tempo/SignatureEnvelope.ts +31 -16
  159. package/src/zod/tempo/_test/KeyAuthorization.test.ts +36 -0
  160. package/src/zod/tempo/_test/SignatureEnvelope.test.ts +101 -3
@@ -261,6 +261,7 @@ export type Scope = {
261
261
  * - `[...]` = only listed recipients allowed
262
262
  *
263
263
  * Only valid for constrained selectors: `transfer`, `approve`, `transferWithMemo`.
264
+ * A non-empty list requires `selector`.
264
265
  */
265
266
  recipients?: readonly Address.Address[] | undefined
266
267
  /**
@@ -470,6 +471,7 @@ export function from<
470
471
  }
471
472
  if (auth.witness !== undefined) assertWitness(auth.witness)
472
473
  if (auth.signature) assertSignature(auth.signature)
474
+ if (auth.scopes) assertScopes(auth.scopes)
473
475
  const resolved = {
474
476
  ...auth,
475
477
  ...(auth.scopes
@@ -523,7 +525,10 @@ export declare namespace from {
523
525
  : {})
524
526
  >
525
527
 
526
- type ErrorType = InvalidSignatureTypeError | Errors.GlobalErrorType
528
+ type ErrorType =
529
+ | InvalidScopeError
530
+ | InvalidSignatureTypeError
531
+ | Errors.GlobalErrorType
527
532
  }
528
533
 
529
534
  /**
@@ -985,6 +990,7 @@ export function toRpc(authorization: toRpc.Input): Rpc {
985
990
  // Group flat scopes by address into nested allowedCalls wire format
986
991
  const allowedCalls = (() => {
987
992
  if (!scopes) return undefined
993
+ assertScopes(scopes)
988
994
  const grouped = new Map<string, RpcSelectorRule[]>()
989
995
  for (const scope of scopes) {
990
996
  const key = scope.address as string
@@ -1032,7 +1038,10 @@ export declare namespace toRpc {
1032
1038
  /** Numberish input accepted by {@link ox#KeyAuthorization.(toRpc:function)}. */
1033
1039
  type Input = Signed<Hex.Hex | bigint | number, Hex.Hex | number>
1034
1040
 
1035
- type ErrorType = InvalidSignatureTypeError | Errors.GlobalErrorType
1041
+ type ErrorType =
1042
+ | InvalidScopeError
1043
+ | InvalidSignatureTypeError
1044
+ | Errors.GlobalErrorType
1036
1045
  }
1037
1046
 
1038
1047
  /**
@@ -1110,6 +1119,7 @@ export function toTuple<const authorization extends KeyAuthorization>(
1110
1119
  // Group flat scopes by address for wire format
1111
1120
  const callsValue = (() => {
1112
1121
  if (!scopes) return undefined
1122
+ assertScopes(scopes)
1113
1123
  const grouped = new Map<
1114
1124
  string,
1115
1125
  [Hex.Hex, (readonly Address.Address[])[]][]
@@ -1179,7 +1189,10 @@ export declare namespace toTuple {
1179
1189
  type ReturnType<authorization extends KeyAuthorization = KeyAuthorization> =
1180
1190
  Compute<Tuple<authorization extends KeyAuthorization<true> ? true : false>>
1181
1191
 
1182
- type ErrorType = InvalidSignatureTypeError | Errors.GlobalErrorType
1192
+ type ErrorType =
1193
+ | InvalidScopeError
1194
+ | InvalidSignatureTypeError
1195
+ | Errors.GlobalErrorType
1183
1196
  }
1184
1197
 
1185
1198
  function bigintToHex(value: bigint): Hex.Hex {
@@ -1217,6 +1230,12 @@ function assertWitness(witness: Hex.Hex): void {
1217
1230
  if (Hex.size(witness) !== 32) throw new InvalidWitnessSizeError(witness)
1218
1231
  }
1219
1232
 
1233
+ function assertScopes(scopes: readonly Scope[]): void {
1234
+ for (const scope of scopes)
1235
+ if (!scope.selector && scope.recipients && scope.recipients.length > 0)
1236
+ throw new InvalidScopeError(scope)
1237
+ }
1238
+
1220
1239
  function assertSignature<numberType>(
1221
1240
  signature: SignatureEnvelope.SignatureEnvelope<numberType>,
1222
1241
  ): asserts signature is
@@ -1260,6 +1279,16 @@ export class InvalidSignatureTypeError extends Error {
1260
1279
  }
1261
1280
  }
1262
1281
 
1282
+ /** Thrown when a call scope has a recipient allowlist but no selector. */
1283
+ export class InvalidScopeError extends Error {
1284
+ override readonly name = 'KeyAuthorization.InvalidScopeError'
1285
+ constructor(scope: Scope) {
1286
+ super(
1287
+ `Scope for \`${scope.address}\` has \`recipients\` but no \`selector\`; a recipient allowlist requires a selector.`,
1288
+ )
1289
+ }
1290
+ }
1291
+
1263
1292
  /**
1264
1293
  * Thrown when a configurable account key grant omits its parent account binding.
1265
1294
  *
@@ -453,6 +453,20 @@ describe('assert', () => {
453
453
  expect(() => SignatureEnvelope.assert(signatureWithoutType)).not.toThrow()
454
454
  })
455
455
 
456
+ test('error: throws on keychain inner signature', () => {
457
+ for (const version of ['v1', 'v2'] as const)
458
+ expect(() =>
459
+ SignatureEnvelope.assert({
460
+ inner: signature_keychain_secp256k1,
461
+ type: 'keychain',
462
+ userAddress: '0x1234567890123456789012345678901234567890',
463
+ version,
464
+ }),
465
+ ).toThrowErrorMatchingInlineSnapshot(
466
+ `[SignatureEnvelope.InvalidApprovalError: Invalid owner approval: keychain signatures cannot wrap a keychain signature.]`,
467
+ )
468
+ })
469
+
456
470
  test('error: throws on invalid inner signature', () => {
457
471
  expect(() =>
458
472
  SignatureEnvelope.assert({
@@ -488,6 +502,23 @@ describe('assert', () => {
488
502
  `[SignatureEnvelope.CoercionError: Unable to coerce value (\`{"r":"0x0000000000000000000000000000000000000000000000000000000000000000","s":"0x0000000000000000000000000000000000000000000000000000000000000000"}\`) to a valid signature envelope.]`,
489
503
  )
490
504
  })
505
+
506
+ test('error: throws on unsupported type', () => {
507
+ expect(() =>
508
+ SignatureEnvelope.assert({ type: 'webauthn' } as any),
509
+ ).toThrowErrorMatchingInlineSnapshot(
510
+ `[SignatureEnvelope.CoercionError: Unable to coerce value (\`{"type":"webauthn"}\`) to a valid signature envelope.]`,
511
+ )
512
+ expect(() =>
513
+ SignatureEnvelope.assert({
514
+ inner: { type: 'bogus' },
515
+ type: 'keychain',
516
+ userAddress: '0x1234567890123456789012345678901234567890',
517
+ } as any),
518
+ ).toThrowErrorMatchingInlineSnapshot(
519
+ `[SignatureEnvelope.CoercionError: Unable to coerce value (\`{"type":"bogus"}\`) to a valid signature envelope.]`,
520
+ )
521
+ })
491
522
  })
492
523
 
493
524
  describe('deserialize', () => {
@@ -662,6 +693,42 @@ describe('deserialize', () => {
662
693
  )
663
694
  })
664
695
 
696
+ test('error: throws on WebAuthn signature above the maximum size', () => {
697
+ const authenticatorData = `0x${'00'.repeat(37)}` as const
698
+ // 37 (authenticatorData) + 1883 (clientDataJSON) + 128 = 2048 bytes
699
+ const max = {
700
+ ...signature_webauthn,
701
+ metadata: {
702
+ authenticatorData,
703
+ clientDataJSON: JSON.stringify({ challenge: 'a'.repeat(1867) }),
704
+ },
705
+ }
706
+ const serialized = SignatureEnvelope.serialize(max)
707
+ expect(Hex.size(serialized)).toMatchInlineSnapshot(`2049`)
708
+ expect(SignatureEnvelope.deserialize(serialized)).toMatchObject({
709
+ metadata: max.metadata,
710
+ })
711
+
712
+ expect(() =>
713
+ SignatureEnvelope.deserialize(
714
+ SignatureEnvelope.serialize({
715
+ ...max,
716
+ metadata: {
717
+ authenticatorData,
718
+ clientDataJSON: JSON.stringify({ challenge: 'a'.repeat(1868) }),
719
+ },
720
+ }),
721
+ ),
722
+ ).toThrow(
723
+ 'Invalid WebAuthn signature envelope size: expected at most 2048 bytes, got 2049 bytes',
724
+ )
725
+ expect(() =>
726
+ SignatureEnvelope.deserialize(`0x02${'41'.repeat(40_000)}`),
727
+ ).toThrow(
728
+ 'Invalid WebAuthn signature envelope size: expected at most 2048 bytes, got 40000 bytes',
729
+ )
730
+ })
731
+
665
732
  test('error: throws on invalid clientDataJSON', () => {
666
733
  // Create a signature with invalid JSON (not properly formatted)
667
734
  const invalidMetadata = {
@@ -783,6 +850,48 @@ describe('deserialize', () => {
783
850
  })
784
851
  })
785
852
 
853
+ test('behavior: deserializes 65-byte inner starting with a keychain type as secp256k1', () => {
854
+ const userAddress = '0x1234567890123456789012345678901234567890'
855
+ const inner = `0x03${'11'.repeat(31)}${'22'.repeat(32)}1b` as const
856
+ expect(
857
+ SignatureEnvelope.deserialize(Hex.concat('0x04', userAddress, inner)),
858
+ ).toMatchInlineSnapshot(`
859
+ {
860
+ "inner": {
861
+ "signature": {
862
+ "r": "0x0311111111111111111111111111111111111111111111111111111111111111",
863
+ "s": "0x2222222222222222222222222222222222222222222222222222222222222222",
864
+ "yParity": 0,
865
+ },
866
+ "type": "secp256k1",
867
+ },
868
+ "type": "keychain",
869
+ "userAddress": "0x1234567890123456789012345678901234567890",
870
+ "version": "v2",
871
+ }
872
+ `)
873
+ })
874
+
875
+ test('error: throws on keychain inner signature', () => {
876
+ const userAddress = '0x1234567890123456789012345678901234567890'
877
+ const inner = SignatureEnvelope.serialize(signature_keychain_secp256k1)
878
+ for (const typeId of ['0x03', '0x04'] as const)
879
+ expect(() =>
880
+ SignatureEnvelope.deserialize(Hex.concat(typeId, userAddress, inner)),
881
+ ).toThrow(
882
+ 'Unable to deserialize signature envelope: keychain signatures cannot wrap a keychain signature',
883
+ )
884
+
885
+ // 2000 nested keychains
886
+ const nested = Hex.concat(
887
+ `0x${`03${userAddress.slice(2)}`.repeat(2000)}`,
888
+ Signature.toHex(signature_secp256k1),
889
+ )
890
+ expect(() => SignatureEnvelope.deserialize(nested)).toThrow(
891
+ SignatureEnvelope.InvalidSerializedError,
892
+ )
893
+ })
894
+
786
895
  test('error: throws on invalid keychain signature length', () => {
787
896
  // Keychain signature too short (must be at least 21 bytes: 1 type + 20 address)
788
897
  const invalidSig = `0x03${'00'.repeat(10)}` as const
@@ -1931,6 +2040,17 @@ describe('validate', () => {
1931
2040
  expect(SignatureEnvelope.validate({} as any)).toBe(false)
1932
2041
  })
1933
2042
 
2043
+ test('behavior: returns false for unsupported type', () => {
2044
+ expect(SignatureEnvelope.validate({ type: 'webauthn' } as any)).toBe(false)
2045
+ expect(
2046
+ SignatureEnvelope.validate({
2047
+ inner: { type: 'bogus' },
2048
+ type: 'keychain',
2049
+ userAddress: '0x1234567890123456789012345678901234567890',
2050
+ } as any),
2051
+ ).toBe(false)
2052
+ })
2053
+
1934
2054
  test('behavior: returns false for incomplete signature', () => {
1935
2055
  expect(
1936
2056
  SignatureEnvelope.validate({
@@ -2319,6 +2439,49 @@ describe('fromRpc', () => {
2319
2439
  type: 'webAuthn',
2320
2440
  })
2321
2441
  })
2442
+
2443
+ test('error: throws on WebAuthn data above the maximum size', () => {
2444
+ const rpc = {
2445
+ pubKeyX: publicKey.x,
2446
+ pubKeyY: publicKey.y,
2447
+ r: p256Signature.r,
2448
+ s: p256Signature.s,
2449
+ type: 'webAuthn',
2450
+ } as const
2451
+ const authenticatorData = `0x${'00'.repeat(37)}` as const
2452
+ const clientDataJSON = JSON.stringify({ challenge: 'a'.repeat(1867) })
2453
+
2454
+ // 37 (authenticatorData) + 1883 (clientDataJSON) = 1920 bytes
2455
+ expect(
2456
+ SignatureEnvelope.fromRpc({
2457
+ ...rpc,
2458
+ webauthnData: Hex.concat(
2459
+ authenticatorData,
2460
+ Hex.fromString(clientDataJSON),
2461
+ ),
2462
+ }),
2463
+ ).toMatchObject({ metadata: { authenticatorData, clientDataJSON } })
2464
+
2465
+ expect(() =>
2466
+ SignatureEnvelope.fromRpc({
2467
+ ...rpc,
2468
+ webauthnData: Hex.concat(
2469
+ authenticatorData,
2470
+ Hex.fromString(JSON.stringify({ challenge: 'a'.repeat(1868) })),
2471
+ ),
2472
+ }),
2473
+ ).toThrow(
2474
+ 'Invalid WebAuthn data size: expected at most 1920 bytes, got 1921 bytes',
2475
+ )
2476
+ expect(() =>
2477
+ SignatureEnvelope.fromRpc({
2478
+ ...rpc,
2479
+ webauthnData: `0x${'41'.repeat(40_000)}`,
2480
+ }),
2481
+ ).toThrow(
2482
+ 'Invalid WebAuthn data size: expected at most 1920 bytes, got 40000 bytes',
2483
+ )
2484
+ })
2322
2485
  })
2323
2486
 
2324
2487
  describe('keychain', () => {
@@ -2449,6 +2612,36 @@ describe('fromRpc', () => {
2449
2612
 
2450
2613
  expect(envelope.keyId).toBe('0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c')
2451
2614
  })
2615
+
2616
+ test('error: throws on keychain inner signature', () => {
2617
+ const inner = {
2618
+ signature: {
2619
+ r: '0xa2bb71146c20ce932456c043ebb2973ed205e07cd32c35a60bdefca1285fd132',
2620
+ s: '0x7cba10692bccdbfba9a215418443c2903dbee6fe5cb55c91172e47efc607840e',
2621
+ type: 'secp256k1',
2622
+ yParity: '0x1',
2623
+ },
2624
+ userAddress: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
2625
+ } as const
2626
+ expect(() =>
2627
+ SignatureEnvelope.fromRpc({
2628
+ signature: { ...inner, type: 'keychain' },
2629
+ type: 'keychain',
2630
+ userAddress: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
2631
+ }),
2632
+ ).toThrowErrorMatchingInlineSnapshot(
2633
+ `[SignatureEnvelope.InvalidApprovalError: Invalid owner approval: keychain signatures cannot wrap a keychain signature.]`,
2634
+ )
2635
+ // inner keychain without an explicit type
2636
+ expect(() =>
2637
+ SignatureEnvelope.fromRpc({
2638
+ signature: inner as never,
2639
+ type: 'keychain',
2640
+ userAddress: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
2641
+ version: 'v1',
2642
+ }),
2643
+ ).toThrow(SignatureEnvelope.InvalidApprovalError)
2644
+ })
2452
2645
  })
2453
2646
  })
2454
2647
 
@@ -3034,18 +3227,21 @@ describe('configurable', () => {
3034
3227
  }),
3035
3228
  ).toThrowError(SignatureEnvelope.InvalidApprovalError)
3036
3229
  })
3037
- test('rejects V1 wrappers around recursively nested configurable delegates', () => {
3038
- let inner: SignatureEnvelope.SignatureEnvelope = envelope
3039
- for (let depth = 0; depth < 3; depth++) {
3040
- inner = { inner, type: 'keychain', userAddress: account, version: 'v2' }
3041
- const valid: SignatureEnvelope.Keychain = {
3230
+ test('rejects keychain wrappers around keychain-wrapped configurable delegates', () => {
3231
+ const inner: SignatureEnvelope.Keychain = {
3232
+ inner: envelope,
3233
+ type: 'keychain',
3234
+ userAddress: account,
3235
+ version: 'v2',
3236
+ }
3237
+ expect(SignatureEnvelope.validate(inner)).toBe(true)
3238
+ for (const version of ['v1', 'v2'] as const) {
3239
+ const invalid: SignatureEnvelope.Keychain = {
3042
3240
  inner,
3043
3241
  type: 'keychain',
3044
3242
  userAddress: account,
3045
- version: 'v2',
3243
+ version,
3046
3244
  }
3047
- const invalid = { ...valid, version: 'v1' } as const
3048
- expect(SignatureEnvelope.validate(valid)).toBe(true)
3049
3245
  expect(SignatureEnvelope.validate(invalid)).toBe(false)
3050
3246
  expect(() => SignatureEnvelope.serialize(invalid)).toThrowError(
3051
3247
  SignatureEnvelope.InvalidApprovalError,
@@ -3057,32 +3253,20 @@ describe('configurable', () => {
3057
3253
  SignatureEnvelope.fromRpc({
3058
3254
  type: 'keychain',
3059
3255
  userAddress: account,
3060
- version: 'v1',
3256
+ version,
3061
3257
  signature: SignatureEnvelope.toRpc(inner),
3062
3258
  }),
3063
3259
  ).toThrowError(SignatureEnvelope.InvalidApprovalError)
3064
3260
  expect(() =>
3065
3261
  SignatureEnvelope.deserialize(
3066
- Hex.concat('0x03', account, SignatureEnvelope.serialize(inner)),
3262
+ Hex.concat(
3263
+ version === 'v1' ? '0x03' : '0x04',
3264
+ account,
3265
+ SignatureEnvelope.serialize(inner),
3266
+ ),
3067
3267
  ),
3068
- ).toThrowError(SignatureEnvelope.InvalidApprovalError)
3069
- expect(() =>
3070
- SignatureEnvelope.serialize({ ...valid, inner: invalid }),
3071
- ).toThrowError(SignatureEnvelope.InvalidApprovalError)
3268
+ ).toThrowError(SignatureEnvelope.InvalidSerializedError)
3072
3269
  }
3073
- expect(
3074
- SignatureEnvelope.validate({
3075
- inner: {
3076
- inner: primitive,
3077
- type: 'keychain',
3078
- userAddress: account,
3079
- version: 'v2',
3080
- },
3081
- type: 'keychain',
3082
- userAddress: account,
3083
- version: 'v1',
3084
- }),
3085
- ).toBe(true)
3086
3270
  })
3087
3271
  test('RPC rejects 64-byte values that resemble primitive signatures', () => {
3088
3272
  expect(() =>
@@ -27,6 +27,12 @@ const serializedKeychainType = '0x03'
27
27
  const serializedKeychainV2Type = '0x04'
28
28
  const serializedConfigurableType = '0x05'
29
29
 
30
+ /**
31
+ * Maximum WebAuthn signature size in bytes, excluding the type identifier
32
+ * (Tempo's `MAX_WEBAUTHN_SIGNATURE_LENGTH`).
33
+ */
34
+ const maxWebAuthnSize = 2048
35
+
30
36
  /** Serialized magic identifier for Tempo signature envelopes. */
31
37
  export const magicBytes =
32
38
  '0x7777777777777777777777777777777777777777777777777777777777777777' // 32 "T"s
@@ -271,7 +277,7 @@ export type Type = (typeof types)[number]
271
277
  * ```
272
278
  *
273
279
  * @param envelope - The signature envelope to assert.
274
- * @throws `CoercionError` if the envelope type cannot be determined.
280
+ * @throws `CoercionError` if the envelope type cannot be determined or is not supported.
275
281
  */
276
282
  export function assert(envelope: PartialBy<SignatureEnvelope, 'type'>): void {
277
283
  const type = getType(envelope)
@@ -326,7 +332,7 @@ export function assert(envelope: PartialBy<SignatureEnvelope, 'type'>): void {
326
332
 
327
333
  if (type === 'keychain') {
328
334
  const keychain = envelope as Keychain
329
- assertKeychainVersion(keychain)
335
+ assertKeychainInner(keychain)
330
336
  assert(keychain.inner)
331
337
  return
332
338
  }
@@ -336,6 +342,8 @@ export function assert(envelope: PartialBy<SignatureEnvelope, 'type'>): void {
336
342
  assertConfigurable(configurable)
337
343
  return
338
344
  }
345
+
346
+ throw new CoercionError({ envelope })
339
347
  }
340
348
 
341
349
  export declare namespace assert {
@@ -349,11 +357,13 @@ export declare namespace assert {
349
357
  | Errors.GlobalErrorType
350
358
  }
351
359
 
352
- function assertKeychainVersion(keychain: Keychain): void {
353
- if (keychain.version !== 'v1') return
354
- let inner = keychain.inner
355
- while (getType(inner) === 'keychain') inner = (inner as Keychain).inner
356
- if (getType(inner) === 'configurable')
360
+ function assertKeychainInner(keychain: Keychain): void {
361
+ const type = getType(keychain.inner)
362
+ if (type === 'keychain')
363
+ throw new InvalidApprovalError({
364
+ reason: 'keychain signatures cannot wrap a keychain signature',
365
+ })
366
+ if (keychain.version === 'v1' && type === 'configurable')
357
367
  throw new InvalidApprovalError({
358
368
  reason: 'configurable access keys require keychain V2',
359
369
  })
@@ -614,11 +624,17 @@ function deserialize_(value: Serialized): SignatureEnvelope {
614
624
  if (typeId === serializedWebAuthnType) {
615
625
  // WebAuthn: variable (webauthnData) + 32 (r) + 32 (s) + 32 (pubKeyX) + 32 (pubKeyY)
616
626
  // Minimum: 128 bytes (at least some authenticator data + signature components)
627
+ // Maximum: 2048 bytes
617
628
  if (dataSize < 128)
618
629
  throw new InvalidSerializedError({
619
630
  reason: `Invalid WebAuthn signature envelope size: expected at least 128 bytes, got ${dataSize} bytes`,
620
631
  serialized,
621
632
  })
633
+ if (dataSize > maxWebAuthnSize)
634
+ throw new InvalidSerializedError({
635
+ reason: `Invalid WebAuthn signature envelope size: expected at most ${maxWebAuthnSize} bytes, got ${dataSize} bytes`,
636
+ serialized,
637
+ })
622
638
 
623
639
  const webauthnDataSize = dataSize - 128
624
640
  const webauthnData = Hex.slice(data, 0, webauthnDataSize)
@@ -672,11 +688,22 @@ function deserialize_(value: Serialized): SignatureEnvelope {
672
688
  typeId === serializedKeychainV2Type
673
689
  ) {
674
690
  const userAddress = Hex.slice(data, 0, 20)
675
- const inner = deserialize_(Hex.slice(data, 20))
691
+ const inner = Hex.slice(data, 20)
692
+ // Reject nested keychains before decoding their contents.
693
+ if (
694
+ Hex.size(inner) !== 65 &&
695
+ [serializedKeychainType, serializedKeychainV2Type].includes(
696
+ Hex.slice(inner, 0, 1),
697
+ )
698
+ )
699
+ throw new InvalidSerializedError({
700
+ reason: 'keychain signatures cannot wrap a keychain signature',
701
+ serialized,
702
+ })
676
703
 
677
704
  const envelope = {
678
705
  userAddress,
679
- inner,
706
+ inner: deserialize_(inner),
680
707
  type: 'keychain',
681
708
  version: typeId === serializedKeychainV2Type ? 'v2' : 'v1',
682
709
  } satisfies Keychain
@@ -1060,6 +1087,13 @@ export function fromRpc(envelope: SignatureEnvelopeRpc): SignatureEnvelope {
1060
1087
  const webauthnData = envelope.webauthnData
1061
1088
  const webauthnDataSize = Hex.size(webauthnData)
1062
1089
 
1090
+ // The signature and public key take 128 bytes of the maximum size.
1091
+ if (webauthnDataSize > maxWebAuthnSize - 128)
1092
+ throw new InvalidSerializedError({
1093
+ reason: `Invalid WebAuthn data size: expected at most ${maxWebAuthnSize - 128} bytes, got ${webauthnDataSize} bytes`,
1094
+ serialized: webauthnData,
1095
+ })
1096
+
1063
1097
  // Parse webauthnData into authenticatorData and clientDataJSON
1064
1098
  let authenticatorData: Hex.Hex | undefined
1065
1099
  let clientDataJSON: string | undefined
@@ -1107,10 +1141,20 @@ export function fromRpc(envelope: SignatureEnvelopeRpc): SignatureEnvelope {
1107
1141
  ('userAddress' in envelope && 'signature' in envelope)
1108
1142
  ) {
1109
1143
  const keychain = envelope as KeychainRpc
1144
+ const { signature } = keychain
1145
+ // Reject nested keychains before decoding their contents.
1146
+ if (
1147
+ typeof signature === 'object' &&
1148
+ (signature.type === 'keychain' ||
1149
+ ('userAddress' in signature && 'signature' in signature))
1150
+ )
1151
+ throw new InvalidApprovalError({
1152
+ reason: 'keychain signatures cannot wrap a keychain signature',
1153
+ })
1110
1154
  const result = {
1111
1155
  type: 'keychain',
1112
1156
  userAddress: keychain.userAddress,
1113
- inner: fromRpc(keychain.signature),
1157
+ inner: fromRpc(signature),
1114
1158
  ...(keychain.keyId ? { keyId: keychain.keyId } : {}),
1115
1159
  ...(keychain.version ? { version: keychain.version } : {}),
1116
1160
  } satisfies Keychain
@@ -1453,7 +1497,7 @@ export function toRpc<const envelope extends toRpc.Input>(
1453
1497
 
1454
1498
  if (type === 'keychain') {
1455
1499
  const keychain = envelope as Keychain
1456
- assertKeychainVersion(keychain)
1500
+ assertKeychainInner(keychain)
1457
1501
  return {
1458
1502
  type: 'keychain',
1459
1503
  userAddress: keychain.userAddress,
@@ -186,6 +186,36 @@ describe('fromRpc', () => {
186
186
  const request = TransactionRequest.fromRpc({})
187
187
  expect(request).toMatchInlineSnapshot('{}')
188
188
  })
189
+
190
+ test('behavior: fee payer signature', () => {
191
+ const feePayerSignature = {
192
+ r: '0x0000000000000000000000000000000000000000000000000000000000000001',
193
+ s: '0x0000000000000000000000000000000000000000000000000000000000000001',
194
+ } as const
195
+ expect(
196
+ TransactionRequest.fromRpc({
197
+ feePayerSignature: { ...feePayerSignature, yParity: '0x1' },
198
+ type: '0x76',
199
+ }),
200
+ ).toMatchInlineSnapshot(`
201
+ {
202
+ "feePayerSignature": {
203
+ "r": "0x0000000000000000000000000000000000000000000000000000000000000001",
204
+ "s": "0x0000000000000000000000000000000000000000000000000000000000000001",
205
+ "yParity": 1,
206
+ },
207
+ "type": "tempo",
208
+ }
209
+ `)
210
+ expect(() =>
211
+ TransactionRequest.fromRpc({
212
+ feePayerSignature: { ...feePayerSignature, yParity: '0x2' },
213
+ type: '0x76',
214
+ }),
215
+ ).toThrowErrorMatchingInlineSnapshot(
216
+ `[Signature.InvalidYParityError: Value \`2\` is an invalid y-parity value. Y-parity must be 0 or 1.]`,
217
+ )
218
+ })
189
219
  })
190
220
 
191
221
  describe('toRpc', () => {
@@ -123,8 +123,11 @@ export function fromRpc(request: Rpc): TransactionRequest {
123
123
  )
124
124
  if (request.signature)
125
125
  request_.signature = SignatureEnvelope.fromRpc(request.signature)
126
- if (request.feePayerSignature)
127
- request_.feePayerSignature = Signature.fromRpc(request.feePayerSignature)
126
+ if (request.feePayerSignature) {
127
+ const feePayerSignature = Signature.fromRpc(request.feePayerSignature)
128
+ Signature.assert(feePayerSignature, { recovered: true })
129
+ request_.feePayerSignature = feePayerSignature
130
+ }
128
131
  if (request.calls)
129
132
  request_.calls = request.calls.map((call) => {
130
133
  const mapped: Call<bigint> = {
@@ -170,6 +173,8 @@ export declare namespace fromRpc {
170
173
  | Hex.toNumber.ErrorType
171
174
  | Hex.toBigInt.ErrorType
172
175
  | AccountSimulation.fromRpc.ErrorType
176
+ | Signature.assert.ErrorType
177
+ | Signature.fromRpc.ErrorType
173
178
  | Errors.GlobalErrorType
174
179
  }
175
180