tempo.ts 0.7.6 → 0.8.0

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 (129) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/dist/chains.d.ts +6 -20
  3. package/dist/chains.d.ts.map +1 -1
  4. package/dist/chains.js +14 -15
  5. package/dist/chains.js.map +1 -1
  6. package/dist/ox/KeyAuthorization.d.ts +356 -0
  7. package/dist/ox/KeyAuthorization.d.ts.map +1 -0
  8. package/dist/ox/KeyAuthorization.js +360 -0
  9. package/dist/ox/KeyAuthorization.js.map +1 -0
  10. package/dist/ox/SignatureEnvelope.d.ts +21 -6
  11. package/dist/ox/SignatureEnvelope.d.ts.map +1 -1
  12. package/dist/ox/SignatureEnvelope.js +43 -3
  13. package/dist/ox/SignatureEnvelope.js.map +1 -1
  14. package/dist/ox/Transaction.d.ts +5 -1
  15. package/dist/ox/Transaction.d.ts.map +1 -1
  16. package/dist/ox/Transaction.js +5 -0
  17. package/dist/ox/Transaction.js.map +1 -1
  18. package/dist/ox/TransactionEnvelopeAA.d.ts +9 -0
  19. package/dist/ox/TransactionEnvelopeAA.d.ts.map +1 -1
  20. package/dist/ox/TransactionEnvelopeAA.js +17 -4
  21. package/dist/ox/TransactionEnvelopeAA.js.map +1 -1
  22. package/dist/ox/TransactionRequest.d.ts +7 -1
  23. package/dist/ox/TransactionRequest.d.ts.map +1 -1
  24. package/dist/ox/TransactionRequest.js +12 -0
  25. package/dist/ox/TransactionRequest.js.map +1 -1
  26. package/dist/ox/index.d.ts +1 -0
  27. package/dist/ox/index.d.ts.map +1 -1
  28. package/dist/ox/index.js +1 -0
  29. package/dist/ox/index.js.map +1 -1
  30. package/dist/prool/Instance.js +1 -1
  31. package/dist/prool/Instance.js.map +1 -1
  32. package/{src/prool/internal → dist/prool}/chain.json +4 -2
  33. package/dist/viem/Abis.d.ts +319 -6
  34. package/dist/viem/Abis.d.ts.map +1 -1
  35. package/dist/viem/Abis.js +199 -7
  36. package/dist/viem/Abis.js.map +1 -1
  37. package/dist/viem/Account.d.ts +103 -14
  38. package/dist/viem/Account.d.ts.map +1 -1
  39. package/dist/viem/Account.js +177 -23
  40. package/dist/viem/Account.js.map +1 -1
  41. package/dist/viem/Actions/account.d.ts.map +1 -1
  42. package/dist/viem/Actions/account.js +4 -5
  43. package/dist/viem/Actions/account.js.map +1 -1
  44. package/dist/viem/Actions/amm.d.ts +72 -0
  45. package/dist/viem/Actions/amm.d.ts.map +1 -1
  46. package/dist/viem/Actions/dex.d.ts +156 -4
  47. package/dist/viem/Actions/dex.d.ts.map +1 -1
  48. package/dist/viem/Actions/fee.d.ts +4 -0
  49. package/dist/viem/Actions/fee.d.ts.map +1 -1
  50. package/dist/viem/Actions/reward.d.ts +78 -0
  51. package/dist/viem/Actions/reward.d.ts.map +1 -1
  52. package/dist/viem/Actions/token.d.ts +585 -0
  53. package/dist/viem/Actions/token.d.ts.map +1 -1
  54. package/dist/viem/Actions/token.js +2 -2
  55. package/dist/viem/Actions/token.js.map +1 -1
  56. package/dist/viem/Addresses.d.ts +1 -1
  57. package/dist/viem/Addresses.d.ts.map +1 -1
  58. package/dist/viem/Addresses.js +1 -1
  59. package/dist/viem/Addresses.js.map +1 -1
  60. package/dist/viem/Chain.d.ts +35 -0
  61. package/dist/viem/Chain.d.ts.map +1 -1
  62. package/dist/viem/Chain.js +37 -0
  63. package/dist/viem/Chain.js.map +1 -1
  64. package/dist/viem/Formatters.d.ts.map +1 -1
  65. package/dist/viem/Formatters.js +8 -7
  66. package/dist/viem/Formatters.js.map +1 -1
  67. package/dist/viem/Storage.d.ts +1 -0
  68. package/dist/viem/Storage.d.ts.map +1 -1
  69. package/dist/viem/Storage.js +21 -0
  70. package/dist/viem/Storage.js.map +1 -1
  71. package/dist/viem/TokenIds.d.ts +1 -1
  72. package/dist/viem/TokenIds.d.ts.map +1 -1
  73. package/dist/viem/TokenIds.js +1 -1
  74. package/dist/viem/TokenIds.js.map +1 -1
  75. package/dist/viem/Transaction.d.ts +9 -1
  76. package/dist/viem/Transaction.d.ts.map +1 -1
  77. package/dist/viem/Transaction.js +2 -1
  78. package/dist/viem/Transaction.js.map +1 -1
  79. package/dist/viem/WebAuthnP256.d.ts +4 -1
  80. package/dist/viem/WebAuthnP256.d.ts.map +1 -1
  81. package/dist/viem/WebAuthnP256.js +3 -1
  82. package/dist/viem/WebAuthnP256.js.map +1 -1
  83. package/dist/wagmi/Connector.d.ts +25 -8
  84. package/dist/wagmi/Connector.d.ts.map +1 -1
  85. package/dist/wagmi/Connector.js +120 -27
  86. package/dist/wagmi/Connector.js.map +1 -1
  87. package/package.json +3 -2
  88. package/src/chains.ts +14 -15
  89. package/src/ox/KeyAuthorization.test.ts +1332 -0
  90. package/src/ox/KeyAuthorization.ts +542 -0
  91. package/src/ox/SignatureEnvelope.test.ts +624 -0
  92. package/src/ox/SignatureEnvelope.ts +89 -9
  93. package/src/ox/Transaction.test.ts +214 -0
  94. package/src/ox/Transaction.ts +13 -1
  95. package/src/ox/TransactionEnvelopeAA.test.ts +164 -4
  96. package/src/ox/TransactionEnvelopeAA.ts +36 -3
  97. package/src/ox/TransactionRequest.ts +22 -1
  98. package/src/ox/e2e.test.ts +612 -5
  99. package/src/ox/index.ts +1 -0
  100. package/src/prool/Instance.ts +1 -1
  101. package/src/prool/chain.json +238 -0
  102. package/src/server/Handler.test.ts +20 -36
  103. package/src/viem/Abis.ts +200 -7
  104. package/src/viem/Account.test.ts +444 -0
  105. package/src/viem/Account.ts +355 -42
  106. package/src/viem/Actions/account.ts +3 -5
  107. package/src/viem/Actions/amm.test.ts +4 -4
  108. package/src/viem/Actions/token.test.ts +8 -8
  109. package/src/viem/Actions/token.ts +2 -2
  110. package/src/viem/Addresses.ts +1 -1
  111. package/src/viem/Chain.test.ts +168 -0
  112. package/src/viem/Chain.ts +37 -1
  113. package/src/viem/Formatters.ts +8 -7
  114. package/src/viem/Storage.ts +22 -0
  115. package/src/viem/TokenIds.ts +1 -1
  116. package/src/viem/Transaction.ts +14 -2
  117. package/src/viem/WebAuthnP256.ts +8 -2
  118. package/src/viem/e2e.test.ts +299 -96
  119. package/src/wagmi/Actions/amm.test.ts +2 -2
  120. package/src/wagmi/Connector.test.ts +1 -1
  121. package/src/wagmi/Connector.ts +184 -54
  122. package/src/wagmi/Hooks/amm.test.ts +4 -4
  123. package/src/wagmi/Hooks/fee.test.ts +10 -4
  124. package/src/wagmi/Hooks/token.test.ts +0 -488
  125. package/dist/viem/internal/account.d.ts +0 -21
  126. package/dist/viem/internal/account.d.ts.map +0 -1
  127. package/dist/viem/internal/account.js +0 -61
  128. package/dist/viem/internal/account.js.map +0 -1
  129. package/src/viem/internal/account.ts +0 -89
@@ -0,0 +1,542 @@
1
+ import type * as Address from 'ox/Address'
2
+ import type * as Errors from 'ox/Errors'
3
+ import * as Hash from 'ox/Hash'
4
+ import * as Hex from 'ox/Hex'
5
+ import * as Rlp from 'ox/Rlp'
6
+ import type { Compute } from '../internal/types.js'
7
+ import * as SignatureEnvelope from './SignatureEnvelope.js'
8
+
9
+ const defaultExpiry = 0xffffffffffff
10
+
11
+ /**
12
+ * Key authorization for provisioning access keys.
13
+ *
14
+ * Used in TransactionEnvelopeAA to add a new key to the keychain precompile.
15
+ * The transaction must be signed by the root key to authorize adding this access key.
16
+ */
17
+ export type KeyAuthorization<
18
+ signed extends boolean = boolean,
19
+ bigintType = bigint,
20
+ numberType = number,
21
+ > = {
22
+ /** Address derived from the public key of the key type. */
23
+ address: Address.Address
24
+ /** Chain ID for replay protection (0 = valid on any chain). */
25
+ chainId?: bigintType | undefined
26
+ /** Unix timestamp when key expires (0 = never expires). */
27
+ expiry?: numberType | undefined
28
+ /** TIP20 spending limits for this key. */
29
+ limits?: readonly TokenLimit<bigintType>[] | undefined
30
+ /** Key type. (secp256k1, P256, WebAuthn). */
31
+ type: SignatureEnvelope.Type
32
+ } & (signed extends true
33
+ ? { signature: SignatureEnvelope.SignatureEnvelope<bigintType, numberType> }
34
+ : {
35
+ signature?:
36
+ | SignatureEnvelope.SignatureEnvelope<bigintType, numberType>
37
+ | undefined
38
+ })
39
+
40
+ /** RPC representation of an {@link ox#KeyAuthorization.KeyAuthorization}. */
41
+ export type Rpc = Omit<
42
+ KeyAuthorization<false, Hex.Hex, Hex.Hex>,
43
+ 'address' | 'signature' | 'type'
44
+ > & {
45
+ keyId: Address.Address
46
+ signature: SignatureEnvelope.SignatureEnvelopeRpc
47
+ keyType: SignatureEnvelope.Type
48
+ }
49
+
50
+ /** Signed representation of a Key Authorization. */
51
+ export type Signed<bigintType = bigint, numberType = number> = KeyAuthorization<
52
+ true,
53
+ bigintType,
54
+ numberType
55
+ >
56
+
57
+ type BaseTuple = readonly [
58
+ chainId: Hex.Hex,
59
+ keyType: Hex.Hex,
60
+ keyId: Address.Address,
61
+ ]
62
+
63
+ /** Tuple representation of a Key Authorization. */
64
+ export type Tuple<signed extends boolean = boolean> = signed extends true
65
+ ? readonly [
66
+ authorization:
67
+ | BaseTuple
68
+ | readonly [...BaseTuple, expiry: Hex.Hex]
69
+ | readonly [
70
+ ...BaseTuple,
71
+ expiry: Hex.Hex,
72
+ limits: readonly [token: Address.Address, limit: Hex.Hex][],
73
+ ],
74
+ signature: Hex.Hex,
75
+ ]
76
+ : readonly [
77
+ authorization:
78
+ | BaseTuple
79
+ | readonly [...BaseTuple, expiry: Hex.Hex]
80
+ | readonly [
81
+ ...BaseTuple,
82
+ expiry: Hex.Hex,
83
+ limits: readonly [token: Address.Address, limit: Hex.Hex][],
84
+ ],
85
+ ]
86
+
87
+ /**
88
+ * Token spending limit for access keys.
89
+ *
90
+ * Defines a per-token spending limit for an access key provisioned via a key authorization.
91
+ * This limit is enforced by the keychain precompile when the key is used.
92
+ */
93
+ export type TokenLimit<bigintType = bigint> = {
94
+ /** Address of the TIP-20 token. */
95
+ token: Address.Address
96
+ /** Maximum spending amount for this token (enforced over the key's lifetime). */
97
+ limit: bigintType
98
+ }
99
+
100
+ /**
101
+ * Converts a Key Authorization object into a typed {@link ox#KeyAuthorization.KeyAuthorization}.
102
+ *
103
+ * @example
104
+ * A Key Authorization can be instantiated from a Key Authorization tuple in object format.
105
+ *
106
+ * ```ts twoslash
107
+ * import { KeyAuthorization } from 'tempo.ts/ox'
108
+ * import { Value } from 'ox'
109
+ *
110
+ * const authorization = KeyAuthorization.from({
111
+ * expiry: 1234567890,
112
+ * keyId: '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c',
113
+ * keyType: 'secp256k1',
114
+ * limits: [{
115
+ * token: '0x20c0000000000000000000000000000000000001',
116
+ * limit: Value.from('10', 6)
117
+ * }],
118
+ * })
119
+ * ```
120
+ *
121
+ * @example
122
+ * ### Attaching Signatures
123
+ *
124
+ * A {@link ox#SignatureEnvelope.SignatureEnvelope} can be attached with the `signature` option. The example below demonstrates signing
125
+ * a Key Authorization with {@link ox#Secp256k1.(sign:function)}.
126
+ *
127
+ * ```ts twoslash
128
+ * import { KeyAuthorization } from 'tempo.ts/ox'
129
+ * import { Secp256k1 } from 'ox'
130
+ *
131
+ * const authorization = KeyAuthorization.from({
132
+ * expiry: 1234567890,
133
+ * keyId: '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c',
134
+ * keyType: 'secp256k1',
135
+ * limits: [{
136
+ * token: '0x20c0000000000000000000000000000000000001',
137
+ * limit: Value.from('10', 6)
138
+ * }],
139
+ * })
140
+ *
141
+ * const signature = Secp256k1.sign({
142
+ * payload: KeyAuthorization.getSignPayload(authorization),
143
+ * privateKey: '0x...',
144
+ * })
145
+ *
146
+ * const authorization_signed = KeyAuthorization.from(authorization, { signature }) // [!code focus]
147
+ * ```
148
+ *
149
+ * @param authorization - A Key Authorization tuple in object format.
150
+ * @param options - Key Authorization options.
151
+ * @returns The {@link ox#KeyAuthorization.KeyAuthorization}.
152
+ */
153
+ export function from<
154
+ const authorization extends KeyAuthorization | Rpc,
155
+ const signature extends
156
+ | SignatureEnvelope.SignatureEnvelope
157
+ | undefined = undefined,
158
+ >(
159
+ authorization: authorization | KeyAuthorization,
160
+ options: from.Options<signature> = {},
161
+ ): from.ReturnType<authorization, signature> {
162
+ if (typeof authorization.expiry === 'string')
163
+ return fromRpc(authorization as Rpc) as never
164
+ if (options.signature)
165
+ return { ...authorization, signature: options.signature } as never
166
+ return authorization as never
167
+ }
168
+
169
+ export declare namespace from {
170
+ type Options<
171
+ signature extends SignatureEnvelope.SignatureEnvelope | undefined =
172
+ | SignatureEnvelope.SignatureEnvelope
173
+ | undefined,
174
+ > = {
175
+ /** The {@link ox#SignatureEnvelope.SignatureEnvelope} to attach to the Key Authorization. */
176
+ signature?: signature | SignatureEnvelope.SignatureEnvelope | undefined
177
+ }
178
+
179
+ type ReturnType<
180
+ authorization extends KeyAuthorization | Rpc = KeyAuthorization,
181
+ signature extends SignatureEnvelope.SignatureEnvelope | undefined =
182
+ | SignatureEnvelope.SignatureEnvelope
183
+ | undefined,
184
+ > = Compute<
185
+ authorization extends Rpc
186
+ ? Signed
187
+ : authorization &
188
+ (signature extends SignatureEnvelope.SignatureEnvelope
189
+ ? { signature: SignatureEnvelope.from.ReturnValue<signature> }
190
+ : {})
191
+ >
192
+
193
+ type ErrorType = Errors.GlobalErrorType
194
+ }
195
+
196
+ /**
197
+ * Converts an {@link ox#AuthorizationAA.Rpc} to an {@link ox#AuthorizationAA.AuthorizationAA}.
198
+ *
199
+ * @example
200
+ * ```ts twoslash
201
+ * import { KeyAuthorization } from 'tempo.ts/ox'
202
+ *
203
+ * const keyAuthorization = KeyAuthorization.fromRpc({
204
+ * expiry: '0x174876e800',
205
+ * keyId: '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c',
206
+ * keyType: 'secp256k1',
207
+ * limits: [{ token: '0x20c0000000000000000000000000000000000001', limit: '0xf4240' }],
208
+ * signature: {
209
+ * type: 'secp256k1',
210
+ * r: '0x635dc2033e60185bb36709c29c75d64ea51dfbd91c32ef4be198e4ceb169fb4d',
211
+ * s: '0x50c2667ac4c771072746acfdcf1f1483336dcca8bd2df47cd83175dbe60f0540',
212
+ * yParity: '0x0'
213
+ * },
214
+ * })
215
+ * ```
216
+ *
217
+ * @param authorization - The RPC-formatted Key Authorization.
218
+ * @returns A signed {@link ox#AuthorizationAA.AuthorizationAA}.
219
+ */
220
+ export function fromRpc(authorization: Rpc): Signed {
221
+ const { chainId = '0x0', keyId, expiry = 0, limits, keyType } = authorization
222
+ const signature = SignatureEnvelope.fromRpc(authorization.signature)
223
+ return {
224
+ address: keyId,
225
+ chainId: chainId === '0x' ? 0n : Hex.toBigInt(chainId),
226
+ expiry: Number(expiry),
227
+ limits: limits?.map((limit) => ({
228
+ token: limit.token,
229
+ limit: BigInt(limit.limit),
230
+ })),
231
+ signature,
232
+ type: keyType,
233
+ }
234
+ }
235
+
236
+ export declare namespace fromRpc {
237
+ type ErrorType = Errors.GlobalErrorType
238
+ }
239
+
240
+ /**
241
+ * Converts an {@link ox#KeyAuthorization.Tuple} to an {@link ox#KeyAuthorization.KeyAuthorization}.
242
+ *
243
+ * @example
244
+ * ```ts twoslash
245
+ * import { KeyAuthorization } from 'tempo.ts/ox'
246
+ *
247
+ * const authorization = KeyAuthorization.fromTuple([
248
+ * '0x174876e800',
249
+ * [['0x20c0000000000000000000000000000000000001', '0xf4240']],
250
+ * 'secp256k1',
251
+ * '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c',
252
+ * '0x01a068a020a209d3d56c46f38cc50a33f704f4a9a10a59377f8dd762ac66910e9b907e865ad05c4035ab5792787d4a0297a43617ae897930a6fe4d822b8faea52064'
253
+ * ])
254
+ * // @log: {
255
+ * // @log: expiry: 1234567890,
256
+ * // @log: keyId: '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c',
257
+ * // @log: keyType: 'secp256k1',
258
+ * // @log: limits: [{ token: '0x20c0000000000000000000000000000000000001', limit: '0xf4240' }],
259
+ * // @log: signature: {
260
+ * // @log: type: 'secp256k1',
261
+ * // @log: r: '0x68a020a209d3d56c46f38cc50a33f704f4a9a10a59377f8dd762ac66910e9b90',
262
+ * // @log: s: '0x7e865ad05c4035ab5792787d4a0297a43617ae897930a6fe4d822b8faea52064',
263
+ * // @log: yParity: 0,
264
+ * // @log: },
265
+ * // @log: }
266
+ * ```
267
+ *
268
+ * @example
269
+ * It is also possible to append a serialized SignatureEnvelope to the end of a Key Authorization tuple.
270
+ *
271
+ * ```ts twoslash
272
+ * import { KeyAuthorization } from 'tempo.ts/ox'
273
+ *
274
+ * const authorization = KeyAuthorization.fromTuple([
275
+ * '0x174876e800',
276
+ * [['0x20c0000000000000000000000000000000000001', '0xf4240']],
277
+ * 'secp256k1',
278
+ * '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c',
279
+ * '0x01a068a020a209d3d56c46f38cc50a33f704f4a9a10a59377f8dd762ac66910e9b907e865ad05c4035ab5792787d4a0297a43617ae897930a6fe4d822b8faea52064',
280
+ * ])
281
+ * // @log: {
282
+ * // @log: expiry: 1234567890,
283
+ * // @log: keyId: '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c',
284
+ * // @log: keyType: 'secp256k1',
285
+ * // @log: limits: [{ token: '0x20c0000000000000000000000000000000000001', limit: '0xf4240' }],
286
+ * // @log: signature: {
287
+ * // @log: type: 'secp256k1',
288
+ * // @log: r: '0x68a020a209d3d56c46f38cc50a33f704f4a9a10a59377f8dd762ac66910e9b90',
289
+ * // @log: s: '0x7e865ad05c4035ab5792787d4a0297a43617ae897930a6fe4d822b8faea52064',
290
+ * // @log: yParity: 0,
291
+ * // @log: },
292
+ * // @log: }
293
+ * ```
294
+ *
295
+ * @param tuple - The Key Authorization tuple.
296
+ * @returns The {@link ox#KeyAuthorization.KeyAuthorization}.
297
+ */
298
+ export function fromTuple<const tuple extends Tuple>(
299
+ tuple: tuple,
300
+ ): fromTuple.ReturnType<tuple> {
301
+ const [authorization, signatureSerialized] = tuple
302
+ const [chainId, keyType_hex, keyId, expiry, limits] = authorization
303
+ const keyType = (() => {
304
+ switch (keyType_hex) {
305
+ case '0x':
306
+ case '0x00':
307
+ return 'secp256k1'
308
+ case '0x01':
309
+ return 'p256'
310
+ case '0x02':
311
+ return 'webAuthn'
312
+ default:
313
+ throw new Error(`Invalid key type: ${keyType_hex}`)
314
+ }
315
+ })()
316
+ const args: KeyAuthorization = {
317
+ address: keyId,
318
+ expiry: typeof expiry !== 'undefined' ? Hex.toNumber(expiry) : undefined,
319
+ type: keyType,
320
+ ...(chainId !== '0x' ? { chainId: Hex.toBigInt(chainId) } : {}),
321
+ ...(typeof expiry !== 'undefined' ? { expiry: Hex.toNumber(expiry) } : {}),
322
+ ...(typeof limits !== 'undefined'
323
+ ? {
324
+ limits: limits.map(([token, limit]) => ({
325
+ token,
326
+ limit: BigInt(limit),
327
+ })),
328
+ }
329
+ : {}),
330
+ }
331
+ if (signatureSerialized)
332
+ args.signature = SignatureEnvelope.deserialize(signatureSerialized)
333
+ return from(args) as never
334
+ }
335
+
336
+ export declare namespace fromTuple {
337
+ type ReturnType<authorization extends Tuple = Tuple> = Compute<
338
+ KeyAuthorization<authorization extends Tuple<true> ? true : false>
339
+ >
340
+
341
+ type ErrorType = Errors.GlobalErrorType
342
+ }
343
+
344
+ /**
345
+ * Computes the sign payload for an {@link ox#KeyAuthorization.KeyAuthorization}.
346
+ *
347
+ * @example
348
+ * The example below demonstrates computing the sign payload for an {@link ox#KeyAuthorization.KeyAuthorization}. This payload
349
+ * can then be passed to signing functions like {@link ox#Secp256k1.(sign:function)}.
350
+ *
351
+ * ```ts twoslash
352
+ * import { KeyAuthorization } from 'tempo.ts/ox'
353
+ * import { Secp256k1 } from 'ox'
354
+ *
355
+ * const authorization = KeyAuthorization.from({
356
+ * expiry: 1234567890,
357
+ * keyId: '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c',
358
+ * keyType: 'secp256k1',
359
+ * limits: [{
360
+ * token: '0x20c0000000000000000000000000000000000001',
361
+ * limit: Value.from('10', 6)
362
+ * }],
363
+ * })
364
+ *
365
+ * const payload = KeyAuthorization.getSignPayload(authorization) // [!code focus]
366
+ *
367
+ * const signature = Secp256k1.sign({
368
+ * payload,
369
+ * privateKey: '0x...',
370
+ * })
371
+ * ```
372
+ *
373
+ * @param authorization - The {@link ox#KeyAuthorization.KeyAuthorization}.
374
+ * @returns The sign payload.
375
+ */
376
+ export function getSignPayload(authorization: KeyAuthorization): Hex.Hex {
377
+ return hash(authorization)
378
+ }
379
+
380
+ export declare namespace getSignPayload {
381
+ type ErrorType = hash.ErrorType | Errors.GlobalErrorType
382
+ }
383
+
384
+ /**
385
+ * Computes the hash for an {@link ox#KeyAuthorization.KeyAuthorization}.
386
+ *
387
+ * @example
388
+ * ```ts twoslash
389
+ * import { KeyAuthorization } from 'tempo.ts/ox'
390
+ * import { Value } from 'ox'
391
+ *
392
+ * const authorization = KeyAuthorization.from({
393
+ * expiry: 1234567890,
394
+ * keyId: '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c',
395
+ * keyType: 'secp256k1',
396
+ * limits: [{
397
+ * token: '0x20c0000000000000000000000000000000000001',
398
+ * limit: Value.from('10', 6)
399
+ * }],
400
+ * })
401
+ *
402
+ * const hash = KeyAuthorization.hash(authorization) // [!code focus]
403
+ * ```
404
+ *
405
+ * @param authorization - The {@link ox#KeyAuthorization.KeyAuthorization}.
406
+ * @returns The hash.
407
+ */
408
+ export function hash(authorization: KeyAuthorization): Hex.Hex {
409
+ const [authorizationTuple] = toTuple(authorization)
410
+ const serialized = Rlp.fromHex(authorizationTuple)
411
+ return Hash.keccak256(serialized)
412
+ }
413
+
414
+ export declare namespace hash {
415
+ type ErrorType =
416
+ | toTuple.ErrorType
417
+ | Hash.keccak256.ErrorType
418
+ | Hex.concat.ErrorType
419
+ | Rlp.fromHex.ErrorType
420
+ | Errors.GlobalErrorType
421
+ }
422
+
423
+ /**
424
+ * Converts an {@link ox#KeyAuthorization.KeyAuthorization} to an {@link ox#KeyAuthorization.Rpc}.
425
+ *
426
+ * @example
427
+ * ```ts twoslash
428
+ * import { KeyAuthorization } from 'tempo.ts/ox'
429
+ *
430
+ * const authorization = KeyAuthorization.toRpc({
431
+ * expiry: 1234567890,
432
+ * keyId: '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c',
433
+ * keyType: 'secp256k1',
434
+ * limits: [{
435
+ * token: '0x20c0000000000000000000000000000000000001',
436
+ * limit: Value.from('10', 6)
437
+ * }],
438
+ * signature: {
439
+ * type: 'secp256k1',
440
+ * r: 44944627813007772897391531230081695102703289123332187696115181104739239197517n,
441
+ * s: 36528503505192438307355164441104001310566505351980369085208178712678799181120n,
442
+ * yParity: 0,
443
+ * },
444
+ * })
445
+ * ```
446
+ *
447
+ * @param authorization - A Key Authorization.
448
+ * @returns An RPC-formatted Key Authorization.
449
+ */
450
+ export function toRpc(authorization: Signed): Rpc {
451
+ const {
452
+ address,
453
+ chainId = 0n,
454
+ expiry = defaultExpiry,
455
+ limits = [],
456
+ type,
457
+ signature,
458
+ } = authorization
459
+
460
+ return {
461
+ chainId: chainId === 0n ? '0x' : Hex.fromNumber(chainId),
462
+ expiry: Hex.fromNumber(expiry),
463
+ limits: limits.map(({ token, limit }) => ({
464
+ token,
465
+ limit: Hex.fromNumber(limit),
466
+ })),
467
+ keyId: address,
468
+ signature: SignatureEnvelope.toRpc(signature),
469
+ keyType: type,
470
+ }
471
+ }
472
+
473
+ export declare namespace toRpc {
474
+ type ErrorType = Errors.GlobalErrorType
475
+ }
476
+
477
+ /**
478
+ * Converts an {@link ox#KeyAuthorization.KeyAuthorization} to an {@link ox#KeyAuthorization.Tuple}.
479
+ *
480
+ * @example
481
+ * ```ts twoslash
482
+ * import { KeyAuthorization } from 'tempo.ts/ox'
483
+ * import { Value } from 'ox'
484
+ *
485
+ * const authorization = KeyAuthorization.from({
486
+ * expiry: 1234567890,
487
+ * keyId: '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c',
488
+ * keyType: 'secp256k1',
489
+ * limits: [{
490
+ * token: '0x20c0000000000000000000000000000000000001',
491
+ * limit: Value.from('10', 6)
492
+ * }],
493
+ * })
494
+ *
495
+ * const tuple = KeyAuthorization.toTuple(authorization) // [!code focus]
496
+ * // @log: [
497
+ * // @log: '0x174876e800',
498
+ * // @log: [['0x20c0000000000000000000000000000000000001', '0xf4240']],
499
+ * // @log: '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c',
500
+ * // @log: 'secp256k1',
501
+ * // @log: ]
502
+ * ```
503
+ *
504
+ * @param authorization - The {@link ox#AuthorizationAA.AuthorizationAA}.
505
+ * @returns An [EIP-7702](https://eips.ethereum.org/EIPS/eip-7702) AA Authorization tuple.
506
+ */
507
+ export function toTuple<const authorization extends KeyAuthorization>(
508
+ authorization: authorization,
509
+ ): toTuple.ReturnType<authorization> {
510
+ const { address, chainId = 0n, expiry, limits } = authorization
511
+ const signature = authorization.signature
512
+ ? SignatureEnvelope.serialize(authorization.signature)
513
+ : undefined
514
+ const type = (() => {
515
+ switch (authorization.type) {
516
+ case 'secp256k1':
517
+ return '0x'
518
+ case 'p256':
519
+ return '0x01'
520
+ case 'webAuthn':
521
+ return '0x02'
522
+ default:
523
+ throw new Error(`Invalid key type: ${authorization.type}`)
524
+ }
525
+ })()
526
+ const authorizationTuple = [
527
+ chainId === 0n ? '0x' : Hex.fromNumber(chainId),
528
+ type,
529
+ address,
530
+ typeof expiry === 'number' ? Hex.fromNumber(expiry) : undefined,
531
+ limits?.map((limit) => [limit.token, Hex.fromNumber(limit.limit)]) ??
532
+ undefined,
533
+ ].filter(Boolean)
534
+ return [authorizationTuple, ...(signature ? [signature] : [])] as never
535
+ }
536
+
537
+ export declare namespace toTuple {
538
+ type ReturnType<authorization extends KeyAuthorization = KeyAuthorization> =
539
+ Compute<Tuple<authorization extends KeyAuthorization<true> ? true : false>>
540
+
541
+ type ErrorType = Errors.GlobalErrorType
542
+ }