ox 1.8.14 → 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 (172) hide show
  1. package/CHANGELOG.md +72 -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/SignaturePreAuthorized.d.ts +192 -0
  83. package/dist/tempo/SignaturePreAuthorized.d.ts.map +1 -0
  84. package/dist/tempo/SignaturePreAuthorized.js +299 -0
  85. package/dist/tempo/SignaturePreAuthorized.js.map +1 -0
  86. package/dist/tempo/TransactionRequest.d.ts +1 -1
  87. package/dist/tempo/TransactionRequest.d.ts.map +1 -1
  88. package/dist/tempo/TransactionRequest.js +5 -2
  89. package/dist/tempo/TransactionRequest.js.map +1 -1
  90. package/dist/tempo/TxEnvelopeTempo.d.ts +30 -1
  91. package/dist/tempo/TxEnvelopeTempo.d.ts.map +1 -1
  92. package/dist/tempo/TxEnvelopeTempo.js +51 -2
  93. package/dist/tempo/TxEnvelopeTempo.js.map +1 -1
  94. package/dist/tempo/index.d.ts +26 -0
  95. package/dist/tempo/index.d.ts.map +1 -1
  96. package/dist/tempo/index.js +26 -0
  97. package/dist/tempo/index.js.map +1 -1
  98. package/dist/webauthn/Registration.d.ts +2 -0
  99. package/dist/webauthn/Registration.d.ts.map +1 -1
  100. package/dist/webauthn/Registration.js +2 -6
  101. package/dist/webauthn/Registration.js.map +1 -1
  102. package/dist/zod/tempo/KeyAuthorization.d.ts.map +1 -1
  103. package/dist/zod/tempo/KeyAuthorization.js +4 -2
  104. package/dist/zod/tempo/KeyAuthorization.js.map +1 -1
  105. package/dist/zod/tempo/SignatureEnvelope.d.ts.map +1 -1
  106. package/dist/zod/tempo/SignatureEnvelope.js +24 -14
  107. package/dist/zod/tempo/SignatureEnvelope.js.map +1 -1
  108. package/package.json +6 -1
  109. package/src/core/AbiError.ts +82 -8
  110. package/src/core/AbiEvent.ts +90 -45
  111. package/src/core/AbiFunction.ts +76 -4
  112. package/src/core/Address.ts +18 -1
  113. package/src/core/Authorization.ts +34 -4
  114. package/src/core/Keystore.ts +40 -3
  115. package/src/core/Secp256k1.ts +4 -1
  116. package/src/core/Signature.ts +7 -3
  117. package/src/core/Siwe.ts +29 -3
  118. package/src/core/TxEnvelopeEip1559.ts +4 -1
  119. package/src/core/TxEnvelopeEip2930.ts +4 -1
  120. package/src/core/TxEnvelopeEip7702.ts +9 -2
  121. package/src/core/TxEnvelopeLegacy.ts +58 -7
  122. package/src/core/TypedData.ts +10 -9
  123. package/src/core/Value.ts +14 -3
  124. package/src/core/_test/AbiError.test.ts +105 -2
  125. package/src/core/_test/AbiEvent.fuzz.ts +1 -1
  126. package/src/core/_test/AbiEvent.test-d.ts +46 -0
  127. package/src/core/_test/AbiEvent.test.ts +563 -11
  128. package/src/core/_test/AbiFunction.test.ts +74 -2
  129. package/src/core/_test/AbiParameters.adversarial.fuzz.ts +1 -0
  130. package/src/core/_test/AbiParameters.decode.test.ts +110 -0
  131. package/src/core/_test/AbiParameters.encode.test.ts +26 -1
  132. package/src/core/_test/Address.test.ts +41 -0
  133. package/src/core/_test/Authorization.test.ts +130 -0
  134. package/src/core/_test/Keystore.test.ts +65 -1
  135. package/src/core/_test/Secp256k1.test.ts +36 -0
  136. package/src/core/_test/Signature.test.ts +102 -0
  137. package/src/core/_test/Siwe.test.ts +123 -0
  138. package/src/core/_test/TransactionEnvelopeEip1559.test.ts +28 -0
  139. package/src/core/_test/TransactionEnvelopeEip2930.test.ts +28 -0
  140. package/src/core/_test/TransactionEnvelopeEip7702.test.ts +133 -0
  141. package/src/core/_test/TransactionEnvelopeLegacy.test.ts +322 -1
  142. package/src/core/_test/TypedData.test.ts +178 -0
  143. package/src/core/_test/Value.test.ts +21 -0
  144. package/src/core/_test/internal/cursor.test.ts +35 -0
  145. package/src/core/internal/abiParameters.ts +7 -2
  146. package/src/core/internal/cursor.ts +36 -0
  147. package/src/erc4337/UserOperation.ts +3 -4
  148. package/src/erc4337/_test/UserOperation.test.ts +59 -1
  149. package/src/erc7821/Execute.ts +83 -2
  150. package/src/erc7821/_test/Execute.test.ts +217 -1
  151. package/src/index.ts +1 -1
  152. package/src/tempo/KeyAuthorization.test.ts +91 -0
  153. package/src/tempo/KeyAuthorization.ts +32 -3
  154. package/src/tempo/SignatureEnvelope.test.ts +211 -27
  155. package/src/tempo/SignatureEnvelope.ts +55 -11
  156. package/src/tempo/SignaturePreAuthorized.test-d.ts +29 -0
  157. package/src/tempo/SignaturePreAuthorized.test.ts +410 -0
  158. package/src/tempo/SignaturePreAuthorized.ts +370 -0
  159. package/src/tempo/TransactionRequest.test.ts +30 -0
  160. package/src/tempo/TransactionRequest.ts +7 -2
  161. package/src/tempo/TxEnvelopeTempo.test.ts +310 -0
  162. package/src/tempo/TxEnvelopeTempo.ts +64 -1
  163. package/src/tempo/configurableAccounts.e2e.test.ts +1 -1
  164. package/src/tempo/index.ts +26 -0
  165. package/src/version.ts +1 -1
  166. package/src/webauthn/Registration.ts +4 -6
  167. package/src/webauthn/_test/Registration.browser.test.ts +14 -0
  168. package/src/webauthn/_test/Registration.test.ts +60 -0
  169. package/src/zod/tempo/KeyAuthorization.ts +12 -5
  170. package/src/zod/tempo/SignatureEnvelope.ts +31 -16
  171. package/src/zod/tempo/_test/KeyAuthorization.test.ts +36 -0
  172. package/src/zod/tempo/_test/SignatureEnvelope.test.ts +101 -3
@@ -0,0 +1,370 @@
1
+ import * as Errors from '../core/Errors.js'
2
+ import * as Hex from '../core/Hex.js'
3
+ import * as Rlp from '../core/Rlp.js'
4
+ import * as KeyAuthorization from './KeyAuthorization.js'
5
+ import * as SignatureEnvelope from './SignatureEnvelope.js'
6
+
7
+ /** Unwrapped [TIP-1165](https://tips.sh/1165) pre-authorized signature. */
8
+ export type Unwrapped = {
9
+ /** Root key's signed authorization of the access key. */
10
+ keyAuthorization: KeyAuthorization.Signed
11
+ /** V2 keychain signature by the access key. */
12
+ signature: SignatureEnvelope.Keychain
13
+ }
14
+
15
+ /** Wrapped [TIP-1165](https://tips.sh/1165) pre-authorized signature. */
16
+ export type Wrapped = Hex.Hex
17
+
18
+ /**
19
+ * Magic bytes used to identify [TIP-1165](https://tips.sh/1165) pre-authorized signatures.
20
+ */
21
+ export const magicBytes =
22
+ '0x1165116511651165116511651165116511651165116511651165116511651165' as const
23
+
24
+ /**
25
+ * Asserts that a value is a valid pre-authorized signature.
26
+ *
27
+ * A wrapped value must end with {@link ox#SignaturePreAuthorized.magicBytes} and decode strictly.
28
+ * An unwrapped value must hold a V2 keychain signature with a primitive inner
29
+ * signature, and a signed key authorization.
30
+ *
31
+ * @example
32
+ * ```ts twoslash
33
+ * import { SignaturePreAuthorized } from 'ox/tempo'
34
+ *
35
+ * SignaturePreAuthorized.assert('0xdeadbeef')
36
+ * // @error: InvalidWrappedSignatureError: Value `0xdeadbeef` is an invalid pre-authorized signature.
37
+ * ```
38
+ *
39
+ * @param value - The value to assert.
40
+ */
41
+ export function assert(value: Unwrapped | Wrapped): void {
42
+ if (typeof value === 'string') {
43
+ unwrap(value)
44
+ return
45
+ }
46
+ if (value === null || typeof value !== 'object')
47
+ throw new InvalidUnwrappedSignatureError({
48
+ reason: 'expected an object',
49
+ value,
50
+ })
51
+ const { keyAuthorization, signature } = value
52
+ assertKeychain(signature, value)
53
+ if (!keyAuthorization || typeof keyAuthorization !== 'object')
54
+ throw new InvalidUnwrappedSignatureError({
55
+ reason: 'missing key authorization',
56
+ value,
57
+ })
58
+ if (!keyAuthorization.signature)
59
+ throw new InvalidUnwrappedSignatureError({
60
+ reason: 'key authorization is not signed',
61
+ value,
62
+ })
63
+ // The discriminants alone don't make an encodable value: require both
64
+ // constituents to serialize.
65
+ const reason = (() => {
66
+ try {
67
+ SignatureEnvelope.assert(signature.inner)
68
+ SignatureEnvelope.serialize(signature)
69
+ } catch {
70
+ return 'malformed keychain signature'
71
+ }
72
+ try {
73
+ SignatureEnvelope.assert(keyAuthorization.signature)
74
+ KeyAuthorization.serialize(keyAuthorization)
75
+ } catch {
76
+ return 'malformed signed key authorization'
77
+ }
78
+ return undefined
79
+ })()
80
+ if (reason) throw new InvalidUnwrappedSignatureError({ reason, value })
81
+ }
82
+
83
+ export declare namespace assert {
84
+ type ErrorType =
85
+ | InvalidWrappedSignatureError
86
+ | InvalidUnwrappedSignatureError
87
+ | Errors.GlobalErrorType
88
+ }
89
+
90
+ /**
91
+ * Instantiates a [TIP-1165](https://tips.sh/1165) pre-authorized signature from its wrapped or unwrapped form.
92
+ *
93
+ * @example
94
+ * ```ts twoslash
95
+ * // @noErrors
96
+ * import { SignaturePreAuthorized } from 'ox/tempo'
97
+ *
98
+ * // Instantiate from serialized format. // [!code focus]
99
+ * const unwrapped = SignaturePreAuthorized.from('0x...') // [!code focus]
100
+ * // @log: { keyAuthorization: { ... }, signature: { ... } } // [!code focus]
101
+ *
102
+ * // Instantiate from constituent parts. // [!code focus]
103
+ * const unwrapped = SignaturePreAuthorized.from({
104
+ * // [!code focus]
105
+ * keyAuthorization, // [!code focus]
106
+ * signature // [!code focus]
107
+ * }) // [!code focus]
108
+ * ```
109
+ *
110
+ * @param value - Wrapped or unwrapped pre-authorized signature.
111
+ * @returns Unwrapped pre-authorized signature.
112
+ */
113
+ export function from(value: Unwrapped | Wrapped): Unwrapped {
114
+ if (typeof value === 'string') return unwrap(value)
115
+ assert(value)
116
+ return value
117
+ }
118
+
119
+ export declare namespace from {
120
+ type ReturnType = Unwrapped
121
+
122
+ type ErrorType = assert.ErrorType | unwrap.ErrorType | Errors.GlobalErrorType
123
+ }
124
+
125
+ /**
126
+ * Parses a [TIP-1165](https://tips.sh/1165) pre-authorized signature into its keychain signature and signed key authorization.
127
+ *
128
+ * Decoding is strict: the value must end with {@link ox#SignaturePreAuthorized.magicBytes},
129
+ * and the rest must be one canonical RLP list of exactly two items with no trailing bytes.
130
+ *
131
+ * @example
132
+ * ```ts twoslash
133
+ * // @noErrors
134
+ * import { SignaturePreAuthorized } from 'ox/tempo'
135
+ *
136
+ * const { keyAuthorization, signature } =
137
+ * SignaturePreAuthorized.unwrap('0x...')
138
+ * ```
139
+ *
140
+ * @param wrapped - Wrapped pre-authorized signature to parse.
141
+ * @returns Unwrapped pre-authorized signature.
142
+ */
143
+ export function unwrap(wrapped: Wrapped): Unwrapped {
144
+ if (
145
+ typeof wrapped !== 'string' ||
146
+ !Hex.validate(wrapped, { even: true, strict: true }) ||
147
+ Hex.size(wrapped) <= Hex.size(magicBytes) ||
148
+ Hex.slice(wrapped, -Hex.size(magicBytes)).toLowerCase() !== magicBytes
149
+ )
150
+ throw new InvalidWrappedSignatureError(wrapped)
151
+
152
+ try {
153
+ const body = Hex.slice(wrapped, 0, -Hex.size(magicBytes))
154
+ const decoded = Rlp.toHex(body)
155
+ if (!Array.isArray(decoded) || decoded.length !== 2)
156
+ throw new Errors.BaseError('expected an RLP list of exactly two items')
157
+ // The RLP decoder ignores trailing input, so require the list to span
158
+ // the whole body.
159
+ if (Rlp.fromHex(decoded).toLowerCase() !== body.toLowerCase())
160
+ throw new Errors.BaseError('unexpected bytes after the RLP list')
161
+ const [signatureSerialized, keyAuthorizationTuple] = decoded
162
+ if (
163
+ typeof signatureSerialized !== 'string' ||
164
+ !Hex.validate(signatureSerialized)
165
+ )
166
+ throw new Errors.BaseError('keychain signature is not an RLP string')
167
+ if (!Array.isArray(keyAuthorizationTuple))
168
+ throw new Errors.BaseError('signed key authorization is not an RLP list')
169
+ if (keyAuthorizationTuple.length !== 2)
170
+ throw new Errors.BaseError('key authorization is not signed')
171
+
172
+ const signature = SignatureEnvelope.deserialize(signatureSerialized)
173
+ assertKeychain(signature, wrapped)
174
+ // Deserialization normalizes some inputs (such as a trailing envelope
175
+ // magic). Require the exact bytes back so the result encodes the input.
176
+ if (
177
+ SignatureEnvelope.serialize(signature).toLowerCase() !==
178
+ signatureSerialized.toLowerCase()
179
+ )
180
+ throw new Errors.BaseError('keychain signature is not canonical')
181
+
182
+ const keyAuthorization = KeyAuthorization.fromTuple(
183
+ keyAuthorizationTuple as unknown as KeyAuthorization.Tuple<true>,
184
+ ) as KeyAuthorization.Signed
185
+ if (
186
+ KeyAuthorization.serialize(keyAuthorization).toLowerCase() !==
187
+ Rlp.fromHex(keyAuthorizationTuple).toLowerCase()
188
+ )
189
+ throw new Errors.BaseError('signed key authorization is not canonical')
190
+
191
+ return { keyAuthorization, signature }
192
+ } catch (error) {
193
+ if (error instanceof InvalidWrappedSignatureError) throw error
194
+ throw new InvalidWrappedSignatureError(wrapped, { cause: error as Error })
195
+ }
196
+ }
197
+
198
+ export declare namespace unwrap {
199
+ type ErrorType = InvalidWrappedSignatureError | Errors.GlobalErrorType
200
+ }
201
+
202
+ /**
203
+ * Serializes a [TIP-1165](https://tips.sh/1165) pre-authorized signature:
204
+ * `rlp([keychain_signature, signed_key_authorization]) ‖ magic`.
205
+ *
206
+ * The signature must be a V2 keychain signature whose inner signature is
207
+ * secp256k1, P256, or WebAuthn.
208
+ *
209
+ * @example
210
+ * ```ts twoslash
211
+ * import { Hash, Hex, Secp256k1 } from 'ox'
212
+ * import {
213
+ * KeyAuthorization,
214
+ * SignatureEnvelope,
215
+ * SignaturePreAuthorized
216
+ * } from 'ox/tempo'
217
+ *
218
+ * const account = '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'
219
+ * const accessKey = Secp256k1.randomPrivateKey()
220
+ * const rootKey = Secp256k1.randomPrivateKey()
221
+ *
222
+ * const authorization = KeyAuthorization.from({
223
+ * address: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
224
+ * chainId: 4217n,
225
+ * type: 'secp256k1'
226
+ * })
227
+ * const keyAuthorization = KeyAuthorization.from(
228
+ * authorization,
229
+ * {
230
+ * signature: SignatureEnvelope.from(
231
+ * Secp256k1.sign({
232
+ * payload:
233
+ * KeyAuthorization.getSignPayload(authorization),
234
+ * privateKey: rootKey
235
+ * })
236
+ * )
237
+ * }
238
+ * )
239
+ *
240
+ * const hash = Hash.keccak256(Hex.fromString('hello'))
241
+ * const signature = SignatureEnvelope.from({
242
+ * inner: SignatureEnvelope.from(
243
+ * Secp256k1.sign({
244
+ * payload: Hash.keccak256(
245
+ * Hex.concat('0x04', hash, account)
246
+ * ),
247
+ * privateKey: accessKey
248
+ * })
249
+ * ),
250
+ * userAddress: account
251
+ * })
252
+ *
253
+ * const wrapped = SignaturePreAuthorized.wrap({
254
+ * // [!code focus]
255
+ * keyAuthorization, // [!code focus]
256
+ * signature // [!code focus]
257
+ * }) // [!code focus]
258
+ * ```
259
+ *
260
+ * @param value - Unwrapped pre-authorized signature.
261
+ * @returns Wrapped pre-authorized signature.
262
+ */
263
+ export function wrap(value: Unwrapped): Wrapped {
264
+ assert(value)
265
+ const { keyAuthorization, signature } = value
266
+ return Hex.concat(
267
+ Rlp.fromHex([
268
+ SignatureEnvelope.serialize(signature),
269
+ KeyAuthorization.toTuple(keyAuthorization) as never,
270
+ ]),
271
+ magicBytes,
272
+ )
273
+ }
274
+
275
+ export declare namespace wrap {
276
+ type ErrorType =
277
+ | assert.ErrorType
278
+ | SignatureEnvelope.serialize.ErrorType
279
+ | KeyAuthorization.toTuple.ErrorType
280
+ | Rlp.fromHex.ErrorType
281
+ | Hex.concat.ErrorType
282
+ | Errors.GlobalErrorType
283
+ }
284
+
285
+ /**
286
+ * Validates a wrapped pre-authorized signature. Returns `true` if it decodes strictly, `false` otherwise.
287
+ *
288
+ * This checks the encoding only. It does not verify either signature or read chain state.
289
+ *
290
+ * @example
291
+ * ```ts twoslash
292
+ * import { SignaturePreAuthorized } from 'ox/tempo'
293
+ *
294
+ * const valid = SignaturePreAuthorized.validate('0xdeadbeef')
295
+ * // @log: false
296
+ * ```
297
+ *
298
+ * @param wrapped - The wrapped signature to validate.
299
+ * @returns `true` if the wrapped signature is valid, `false` otherwise.
300
+ */
301
+ export function validate(wrapped: Wrapped): boolean {
302
+ try {
303
+ unwrap(wrapped)
304
+ return true
305
+ } catch {
306
+ return false
307
+ }
308
+ }
309
+
310
+ export declare namespace validate {
311
+ type ErrorType = Errors.GlobalErrorType
312
+ }
313
+
314
+ function assertKeychain(
315
+ signature: SignatureEnvelope.SignatureEnvelope | undefined,
316
+ value: unknown,
317
+ ): asserts signature is SignatureEnvelope.Keychain {
318
+ const reason = (() => {
319
+ if (!signature || typeof signature !== 'object')
320
+ return 'missing keychain signature'
321
+ if (signature.type !== 'keychain')
322
+ return `expected a keychain signature, got \`${signature.type}\``
323
+ if (signature.version === 'v1')
324
+ return 'keychain V1 signatures are not supported'
325
+ const type = (() => {
326
+ try {
327
+ return SignatureEnvelope.getType(signature.inner)
328
+ } catch {
329
+ return undefined
330
+ }
331
+ })()
332
+ if (type !== 'secp256k1' && type !== 'p256' && type !== 'webAuthn')
333
+ return `expected a secp256k1, p256, or webAuthn inner signature, got \`${type}\``
334
+ return undefined
335
+ })()
336
+ if (!reason) return
337
+ if (typeof value === 'string')
338
+ throw new InvalidWrappedSignatureError(value, {
339
+ cause: new Errors.BaseError(reason),
340
+ })
341
+ throw new InvalidUnwrappedSignatureError({ reason, value })
342
+ }
343
+
344
+ /** Thrown when a wrapped pre-authorized signature is invalid. */
345
+ export class InvalidWrappedSignatureError extends Errors.BaseError<
346
+ Error | undefined
347
+ > {
348
+ override readonly name = 'SignaturePreAuthorized.InvalidWrappedSignatureError'
349
+
350
+ constructor(wrapped: unknown, options: { cause?: Error | undefined } = {}) {
351
+ super(
352
+ `Value \`${wrapped}\` is an invalid pre-authorized signature.`,
353
+ options,
354
+ )
355
+ }
356
+ }
357
+
358
+ /** Thrown when an unwrapped pre-authorized signature is malformed. */
359
+ export class InvalidUnwrappedSignatureError extends Errors.BaseError {
360
+ override readonly name =
361
+ 'SignaturePreAuthorized.InvalidUnwrappedSignatureError'
362
+
363
+ /** The malformed value. */
364
+ readonly value: unknown
365
+
366
+ constructor({ reason, value }: { reason: string; value: unknown }) {
367
+ super(`Value is an invalid unwrapped pre-authorized signature: ${reason}.`)
368
+ this.value = value
369
+ }
370
+ }
@@ -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