-
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathKeyAuthorization.ts
More file actions
744 lines (711 loc) · 22.4 KB
/
KeyAuthorization.ts
File metadata and controls
744 lines (711 loc) · 22.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
import type * as Address from '../core/Address.js'
import type * as Errors from '../core/Errors.js'
import * as Hash from '../core/Hash.js'
import * as Hex from '../core/Hex.js'
import type { Compute } from '../core/internal/types.js'
import * as Rlp from '../core/Rlp.js'
import * as SignatureEnvelope from './SignatureEnvelope.js'
import * as TempoAddress from './TempoAddress.js'
/**
* Key authorization for provisioning access keys.
*
* Access keys allow a root key (e.g., a passkey) to delegate transaction signing to secondary
* keys with customizable permissions including expiry timestamps and per-TIP-20 token spending
* limits. This enables a user to sign transactions without repeated passkey prompts.
*
* The root key signs a `KeyAuthorization` to grant an access key permission to sign transactions
* on its behalf. The authorization is attached to a transaction (which can be signed by the access
* key itself), and the protocol validates the authorization before storing the key in the
* AccountKeychain precompile.
*
* Key authorization fields:
* - `address`: Address derived from the access key's public key (the "key ID")
* - `chainId`: Chain ID for replay protection (0 = valid on any chain)
* - `expiry`: Unix timestamp when the key expires (undefined = never expires)
* - `limits`: Per-TIP-20 token spending limits (only applies to `transfer()` and `approve()` calls)
* - `type`: Key type (`secp256k1`, `p256`, or `webAuthn`)
*
* [Access Keys Specification](https://docs.tempo.xyz/protocol/transactions/spec-tempo-transaction#access-keys)
*/
export type KeyAuthorization<
signed extends boolean = boolean,
bigintType = bigint,
numberType = number,
> = {
/** Address derived from the public key of the key type. */
address: Address.Address
/** Chain ID for replay protection. */
chainId: bigintType
/** Unix timestamp when key expires (0 = never expires). */
expiry?: numberType | null | undefined
/** TIP20 spending limits for this key. */
limits?: readonly TokenLimit<bigintType>[] | undefined
/** Key type. (secp256k1, P256, WebAuthn). */
type: SignatureEnvelope.Type
} & (signed extends true
? { signature: SignatureEnvelope.SignatureEnvelope<bigintType, numberType> }
: {
signature?:
| SignatureEnvelope.SignatureEnvelope<bigintType, numberType>
| undefined
})
/** RPC representation of an {@link ox#KeyAuthorization.KeyAuthorization}. */
export type Rpc = Omit<
KeyAuthorization<false, Hex.Hex, Hex.Hex>,
'address' | 'signature' | 'type'
> & {
keyId: Address.Address
keyType: SignatureEnvelope.Type
signature: SignatureEnvelope.SignatureEnvelopeRpc
}
/** Signed representation of a Key Authorization. */
export type Signed<bigintType = bigint, numberType = number> = KeyAuthorization<
true,
bigintType,
numberType
>
type BaseTuple = readonly [
chainId: Hex.Hex,
keyType: Hex.Hex,
keyId: Address.Address,
]
/** Tuple representation of a Key Authorization. */
export type Tuple<signed extends boolean = boolean> = signed extends true
? readonly [
authorization:
| BaseTuple
| readonly [...BaseTuple, expiry: Hex.Hex]
| readonly [
...BaseTuple,
expiry: Hex.Hex,
limits: readonly [token: Address.Address, limit: Hex.Hex][],
],
signature: Hex.Hex,
]
: readonly [
authorization:
| BaseTuple
| readonly [...BaseTuple, expiry: Hex.Hex]
| readonly [
...BaseTuple,
expiry: Hex.Hex,
limits: readonly [token: Address.Address, limit: Hex.Hex][],
],
]
/**
* Token spending limit for access keys.
*
* Defines a per-TIP-20 token spending limit for an access key. Limits deplete as tokens
* are spent and can be updated by the root key via `updateSpendingLimit()`.
*
* [Access Keys Specification](https://docs.tempo.xyz/protocol/transactions/spec-tempo-transaction#access-keys)
*/
export type TokenLimit<bigintType = bigint> = {
/** Address of the TIP-20 token. */
token: Address.Address
/** Maximum spending amount for this token (enforced over the key's lifetime). */
limit: bigintType
}
/**
* Converts a Key Authorization object into a typed {@link ox#KeyAuthorization.KeyAuthorization}.
*
* Use this to create an unsigned key authorization, then sign it with the root key using
* {@link ox#KeyAuthorization.(getSignPayload:function)} and attach the signature. The signed authorization
* can be included in a {@link ox#TxEnvelopeTempo.TxEnvelopeTempo} via the
* `keyAuthorization` field to provision the access key on-chain.
*
* [Access Keys Specification](https://docs.tempo.xyz/protocol/transactions/spec-tempo-transaction#access-keys)
*
* @example
* ### Secp256k1 Key
*
* Standard Ethereum ECDSA key using the secp256k1 curve.
*
* ```ts twoslash
* import { Address, Secp256k1, Value } from 'ox'
* import { KeyAuthorization } from 'ox/tempo'
*
* const privateKey = Secp256k1.randomPrivateKey()
* const address = Address.fromPublicKey(Secp256k1.getPublicKey({ privateKey }))
*
* const authorization = KeyAuthorization.from({
* address,
* chainId: 4217n,
* expiry: 1234567890,
* type: 'secp256k1',
* limits: [{
* token: '0x20c0000000000000000000000000000000000001',
* limit: Value.from('10', 6),
* }],
* })
* ```
*
* @example
* ### WebCryptoP256 Key
*
* ```ts twoslash
* import { Address, WebCryptoP256, Value } from 'ox'
* import { KeyAuthorization } from 'ox/tempo'
*
* const keyPair = await WebCryptoP256.createKeyPair()
* const address = Address.fromPublicKey(keyPair.publicKey)
*
* const authorization = KeyAuthorization.from({
* address,
* chainId: 4217n,
* expiry: 1234567890,
* type: 'p256',
* limits: [{
* token: '0x20c0000000000000000000000000000000000001',
* limit: Value.from('10', 6),
* }],
* })
* ```
*
* @example
* ### Attaching Signatures (Secp256k1)
*
* Attach a signature to a Key Authorization using a Secp256k1 private key to
* authorize another Secp256k1 key on the account.
*
* ```ts twoslash
* import { Address, Secp256k1, Value } from 'ox'
* import { KeyAuthorization } from 'ox/tempo'
*
* const privateKey = '0x...'
* const address = Address.fromPublicKey(Secp256k1.getPublicKey({ privateKey }))
*
* const authorization = KeyAuthorization.from({
* address,
* chainId: 4217n,
* expiry: 1234567890,
* type: 'secp256k1',
* limits: [{
* token: '0x20c0000000000000000000000000000000000001',
* limit: Value.from('10', 6),
* }],
* })
*
* const rootPrivateKey = '0x...'
* const signature = Secp256k1.sign({
* payload: KeyAuthorization.getSignPayload(authorization),
* privateKey: rootPrivateKey,
* })
*
* const authorization_signed = KeyAuthorization.from(authorization, { signature })
* ```
*
* @example
* ### Attaching Signatures (WebAuthn)
*
* Attach a signature to a Key Authorization using a WebAuthn credential to
* authorize a new WebCryptoP256 key on the account.
*
* ```ts twoslash
* // @noErrors
* import { Address, Value, WebCryptoP256, WebAuthnP256 } from 'ox'
* import { KeyAuthorization, SignatureEnvelope } from 'ox/tempo'
*
* const keyPair = await WebCryptoP256.createKeyPair()
* const address = Address.fromPublicKey(keyPair.publicKey)
*
* const authorization = KeyAuthorization.from({
* address,
* chainId: 4217n,
* expiry: 1234567890,
* type: 'p256',
* limits: [{
* token: '0x20c0000000000000000000000000000000000001',
* limit: Value.from('10', 6),
* }],
* })
*
* const credential = await WebAuthnP256.createCredential({ name: 'Example' })
*
* const { metadata, signature } = await WebAuthnP256.sign({
* challenge: KeyAuthorization.getSignPayload(authorization),
* credentialId: credential.id,
* })
*
* const signatureEnvelope = SignatureEnvelope.from({ // [!code focus]
* signature, // [!code focus]
* publicKey: credential.publicKey, // [!code focus]
* metadata, // [!code focus]
* })
* const authorization_signed = KeyAuthorization.from(
* authorization,
* { signature: signatureEnvelope }, // [!code focus]
* )
* ```
*
* @param authorization - A Key Authorization tuple in object format.
* @param options - Key Authorization options.
* @returns The {@link ox#KeyAuthorization.KeyAuthorization}.
*/
export function from<
const authorization extends KeyAuthorization | Rpc,
const signature extends SignatureEnvelope.from.Value | undefined = undefined,
>(
authorization:
| authorization
| KeyAuthorization
| (Omit<KeyAuthorization, 'address' | 'limits'> & {
address: TempoAddress.Address
limits?:
| readonly { token: TempoAddress.Address; limit: bigint }[]
| undefined
}),
options: from.Options<signature> = {},
): from.ReturnType<authorization, signature> {
if ('keyId' in authorization)
return fromRpc(authorization as Rpc) as never
const auth = authorization as KeyAuthorization & {
limits?: readonly { token: TempoAddress.Address; limit: bigint }[]
}
const resolved = {
...auth,
address: TempoAddress.resolve(auth.address as TempoAddress.Address),
...(auth.limits
? {
limits: auth.limits.map((l) => ({
...l,
token: TempoAddress.resolve(l.token as TempoAddress.Address),
})),
}
: {}),
}
if (options.signature)
return {
...resolved,
signature: SignatureEnvelope.from(options.signature),
} as never
return resolved as never
}
export declare namespace from {
type Options<
signature extends SignatureEnvelope.from.Value | undefined =
| SignatureEnvelope.from.Value
| undefined,
> = {
/** The {@link ox#SignatureEnvelope.SignatureEnvelope} to attach to the Key Authorization. */
signature?: signature | SignatureEnvelope.SignatureEnvelope | undefined
}
type ReturnType<
authorization extends KeyAuthorization | Rpc = KeyAuthorization,
signature extends SignatureEnvelope.from.Value | undefined =
| SignatureEnvelope.from.Value
| undefined,
> = Compute<
authorization extends Rpc
? Signed
: authorization &
(signature extends SignatureEnvelope.from.Value
? { signature: SignatureEnvelope.from.ReturnValue<signature> }
: {})
>
type ErrorType = Errors.GlobalErrorType
}
/**
* Converts an {@link ox#AuthorizationTempo.Rpc} to an {@link ox#AuthorizationTempo.AuthorizationTempo}.
*
* @example
* ```ts twoslash
* import { KeyAuthorization } from 'ox/tempo'
*
* const keyAuthorization = KeyAuthorization.fromRpc({
* expiry: '0x174876e800',
* keyId: '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c',
* keyType: 'secp256k1',
* limits: [{ token: '0x20c0000000000000000000000000000000000001', limit: '0xf4240' }],
* signature: {
* type: 'secp256k1',
* r: '0x635dc2033e60185bb36709c29c75d64ea51dfbd91c32ef4be198e4ceb169fb4d',
* s: '0x50c2667ac4c771072746acfdcf1f1483336dcca8bd2df47cd83175dbe60f0540',
* yParity: '0x0'
* },
* })
* ```
*
* @param authorization - The RPC-formatted Key Authorization.
* @returns A signed {@link ox#AuthorizationTempo.AuthorizationTempo}.
*/
export function fromRpc(authorization: Rpc): Signed {
const { chainId, keyId, expiry = 0, limits, keyType } = authorization
const signature = SignatureEnvelope.fromRpc(authorization.signature)
return {
address: keyId,
chainId: chainId === '0x' ? 0n : Hex.toBigInt(chainId),
expiry: Number(expiry),
limits: limits?.map((limit) => ({
token: limit.token,
limit: BigInt(limit.limit),
})),
signature,
type: keyType,
}
}
export declare namespace fromRpc {
type ErrorType = Errors.GlobalErrorType
}
/**
* Converts an {@link ox#KeyAuthorization.Tuple} to an {@link ox#KeyAuthorization.KeyAuthorization}.
*
* @example
* ```ts twoslash
* import { KeyAuthorization } from 'ox/tempo'
*
* const authorization = KeyAuthorization.fromTuple([
* [
* '0x',
* '0x00',
* '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c',
* '0x174876e800',
* [['0x20c0000000000000000000000000000000000001', '0xf4240']],
* ],
* '0x01a068a020a209d3d56c46f38cc50a33f704f4a9a10a59377f8dd762ac66910e9b907e865ad05c4035ab5792787d4a0297a43617ae897930a6fe4d822b8faea52064',
* ])
* ```
*
* @example
* Unsigned Key Authorization tuple (no signature):
*
* ```ts twoslash
* import { KeyAuthorization } from 'ox/tempo'
*
* const authorization = KeyAuthorization.fromTuple([
* [
* '0x',
* '0x00',
* '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c',
* '0x174876e800',
* [['0x20c0000000000000000000000000000000000001', '0xf4240']],
* ],
* ])
* ```
*
* @param tuple - The Key Authorization tuple.
* @returns The {@link ox#KeyAuthorization.KeyAuthorization}.
*/
export function fromTuple<const tuple extends Tuple>(
tuple: tuple,
): fromTuple.ReturnType<tuple> {
const [authorization, signatureSerialized] = tuple
const [chainId, keyType_hex, keyId, expiry, limits] = authorization
const keyType = (() => {
switch (keyType_hex) {
case '0x':
case '0x00':
return 'secp256k1'
case '0x01':
return 'p256'
case '0x02':
return 'webAuthn'
default:
throw new Error(`Invalid key type: ${keyType_hex}`)
}
})()
const args: KeyAuthorization = {
address: keyId,
expiry: typeof expiry !== 'undefined' ? hexToNumber(expiry) : undefined,
type: keyType,
chainId: chainId === '0x' ? 0n : Hex.toBigInt(chainId),
...(typeof expiry !== 'undefined' ? { expiry: hexToNumber(expiry) } : {}),
...(typeof limits !== 'undefined'
? {
limits: limits.map(([token, limit]) => ({
token,
limit: hexToBigint(limit),
})),
}
: {}),
}
if (signatureSerialized)
args.signature = SignatureEnvelope.deserialize(signatureSerialized)
return from(args) as never
}
export declare namespace fromTuple {
type ReturnType<authorization extends Tuple = Tuple> = Compute<
KeyAuthorization<authorization extends Tuple<true> ? true : false>
>
type ErrorType = Errors.GlobalErrorType
}
/**
* Computes the sign payload for an {@link ox#KeyAuthorization.KeyAuthorization}.
*
* The root key must sign this payload to authorize the access key. The resulting signature
* is attached to the key authorization via {@link ox#KeyAuthorization.(from:function)} with the
* `signature` option.
*
* [Access Keys Specification](https://docs.tempo.xyz/protocol/transactions/spec-tempo-transaction#access-keys)
*
* @example
* ```ts twoslash
* import { Address, Secp256k1, Value } from 'ox'
* import { KeyAuthorization } from 'ox/tempo'
*
* const privateKey = '0x...'
* const address = Address.fromPublicKey(Secp256k1.getPublicKey({ privateKey }))
*
* const authorization = KeyAuthorization.from({
* address,
* chainId: 4217n,
* expiry: 1234567890,
* type: 'secp256k1',
* limits: [{
* token: '0x20c0000000000000000000000000000000000001',
* limit: Value.from('10', 6),
* }],
* })
*
* const payload = KeyAuthorization.getSignPayload(authorization) // [!code focus]
* ```
*
* @param authorization - The {@link ox#KeyAuthorization.KeyAuthorization}.
* @returns The sign payload.
*/
export function getSignPayload(authorization: KeyAuthorization): Hex.Hex {
return hash(authorization)
}
export declare namespace getSignPayload {
type ErrorType = hash.ErrorType | Errors.GlobalErrorType
}
/**
* Deserializes an RLP-encoded {@link ox#KeyAuthorization.KeyAuthorization}.
*
* @example
* ```ts twoslash
* import { KeyAuthorization } from 'ox/tempo'
* import { Value } from 'ox'
*
* const authorization = KeyAuthorization.from({
* address: '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c',
* chainId: 4217n,
* expiry: 1234567890,
* type: 'secp256k1',
* limits: [{
* token: '0x20c0000000000000000000000000000000000001',
* limit: Value.from('10', 6)
* }],
* })
*
* const serialized = KeyAuthorization.serialize(authorization)
* const deserialized = KeyAuthorization.deserialize(serialized) // [!code focus]
* ```
*
* @param serialized - The RLP-encoded Key Authorization.
* @returns The {@link ox#KeyAuthorization.KeyAuthorization}.
*/
export function deserialize(serialized: Hex.Hex): KeyAuthorization {
const tuple = Rlp.toHex(serialized) as unknown as Tuple
return fromTuple(tuple)
}
export declare namespace deserialize {
type ErrorType =
| Rlp.toHex.ErrorType
| fromTuple.ErrorType
| Errors.GlobalErrorType
}
/**
* Computes the hash for an {@link ox#KeyAuthorization.KeyAuthorization}.
*
* @example
* ```ts twoslash
* import { KeyAuthorization } from 'ox/tempo'
* import { Value } from 'ox'
*
* const authorization = KeyAuthorization.from({
* address: '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c',
* chainId: 4217n,
* expiry: 1234567890,
* type: 'secp256k1',
* limits: [{
* token: '0x20c0000000000000000000000000000000000001',
* limit: Value.from('10', 6)
* }],
* })
*
* const hash = KeyAuthorization.hash(authorization) // [!code focus]
* ```
*
* @param authorization - The {@link ox#KeyAuthorization.KeyAuthorization}.
* @returns The hash.
*/
export function hash(authorization: KeyAuthorization): Hex.Hex {
const [authorizationTuple] = toTuple(authorization)
const serialized = Rlp.fromHex(authorizationTuple)
return Hash.keccak256(serialized)
}
export declare namespace hash {
type ErrorType =
| toTuple.ErrorType
| Hash.keccak256.ErrorType
| Hex.concat.ErrorType
| Rlp.fromHex.ErrorType
| Errors.GlobalErrorType
}
/**
* Serializes a {@link ox#KeyAuthorization.KeyAuthorization} to RLP-encoded hex.
*
* @example
* ```ts twoslash
* import { KeyAuthorization } from 'ox/tempo'
* import { Value } from 'ox'
*
* const authorization = KeyAuthorization.from({
* address: '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c',
* chainId: 4217n,
* expiry: 1234567890,
* type: 'secp256k1',
* limits: [{
* token: '0x20c0000000000000000000000000000000000001',
* limit: Value.from('10', 6)
* }],
* })
*
* const serialized = KeyAuthorization.serialize(authorization) // [!code focus]
* ```
*
* @param authorization - The {@link ox#KeyAuthorization.KeyAuthorization}.
* @returns The RLP-encoded Key Authorization.
*/
export function serialize(authorization: KeyAuthorization): Hex.Hex {
const tuple = toTuple(authorization)
return Rlp.fromHex(tuple as any)
}
export declare namespace serialize {
type ErrorType =
| toTuple.ErrorType
| Rlp.fromHex.ErrorType
| Errors.GlobalErrorType
}
/**
* Converts an {@link ox#KeyAuthorization.KeyAuthorization} to an {@link ox#KeyAuthorization.Rpc}.
*
* @example
* ```ts twoslash
* import { KeyAuthorization } from 'ox/tempo'
* import { Value } from 'ox'
*
* const authorization = KeyAuthorization.toRpc({
* address: '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c',
* chainId: 4217n,
* expiry: 1234567890,
* type: 'secp256k1',
* limits: [{
* token: '0x20c0000000000000000000000000000000000001',
* limit: Value.from('10', 6)
* }],
* signature: {
* type: 'secp256k1',
* signature: {
* r: 44944627813007772897391531230081695102703289123332187696115181104739239197517n,
* s: 36528503505192438307355164441104001310566505351980369085208178712678799181120n,
* yParity: 0,
* },
* },
* })
* ```
*
* @param authorization - A Key Authorization.
* @returns An RPC-formatted Key Authorization.
*/
export function toRpc(authorization: Signed): Rpc {
const { address, chainId, expiry, limits, type, signature } = authorization
return {
chainId: chainId === 0n ? '0x' : Hex.fromNumber(chainId),
expiry: typeof expiry === 'number' ? Hex.fromNumber(expiry) : null,
limits: limits?.map(({ token, limit }) => ({
token,
limit: Hex.fromNumber(limit),
})),
keyId: address,
signature: SignatureEnvelope.toRpc(signature),
keyType: type,
}
}
export declare namespace toRpc {
type ErrorType = Errors.GlobalErrorType
}
/**
* Converts an {@link ox#KeyAuthorization.KeyAuthorization} to an {@link ox#KeyAuthorization.Tuple}.
*
* @example
* ```ts twoslash
* import { KeyAuthorization } from 'ox/tempo'
* import { Value } from 'ox'
*
* const authorization = KeyAuthorization.from({
* address: '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c',
* chainId: 4217n,
* expiry: 1234567890,
* type: 'secp256k1',
* limits: [{
* token: '0x20c0000000000000000000000000000000000001',
* limit: Value.from('10', 6)
* }],
* })
*
* const tuple = KeyAuthorization.toTuple(authorization) // [!code focus]
* // @log: [
* // @log: '0x174876e800',
* // @log: [['0x20c0000000000000000000000000000000000001', '0xf4240']],
* // @log: '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c',
* // @log: 'secp256k1',
* // @log: ]
* ```
*
* @param authorization - The {@link ox#KeyAuthorization.KeyAuthorization}.
* @returns A Tempo Key Authorization tuple.
*/
export function toTuple<const authorization extends KeyAuthorization>(
authorization: authorization,
): toTuple.ReturnType<authorization> {
const { address, chainId, expiry, limits } = authorization
const signature = authorization.signature
? SignatureEnvelope.serialize(authorization.signature)
: undefined
const type = (() => {
switch (authorization.type) {
case 'secp256k1':
return '0x'
case 'p256':
return '0x01'
case 'webAuthn':
return '0x02'
default:
throw new Error(`Invalid key type: ${authorization.type}`)
}
})()
const limitsValue = limits?.map((limit) => [
limit.token,
bigintToHex(limit.limit),
])
const authorizationTuple = [
bigintToHex(chainId),
type,
address,
// expiry is required in the tuple when limits are present
typeof expiry === 'number' || limitsValue
? numberToHex(expiry ?? 0)
: undefined,
limitsValue,
].filter(Boolean)
return [authorizationTuple, ...(signature ? [signature] : [])] as never
}
export declare namespace toTuple {
type ReturnType<authorization extends KeyAuthorization = KeyAuthorization> =
Compute<Tuple<authorization extends KeyAuthorization<true> ? true : false>>
type ErrorType = Errors.GlobalErrorType
}
function bigintToHex(value: bigint): Hex.Hex {
return value === 0n ? '0x' : Hex.fromNumber(value)
}
function numberToHex(value: number): Hex.Hex {
return value === 0 ? '0x' : Hex.fromNumber(value)
}
function hexToBigint(hex: Hex.Hex): bigint {
return hex === '0x' ? 0n : BigInt(hex)
}
function hexToNumber(hex: Hex.Hex): number {
return hex === '0x' ? 0 : Hex.toNumber(hex)
}