ox 0.9.16 → 0.10.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 (216) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/README.md +4 -4
  3. package/TxEnvelope/package.json +6 -0
  4. package/TxEnvelopeEip1559/package.json +6 -0
  5. package/TxEnvelopeEip2930/package.json +6 -0
  6. package/TxEnvelopeEip4844/package.json +6 -0
  7. package/TxEnvelopeEip7702/package.json +6 -0
  8. package/TxEnvelopeLegacy/package.json +6 -0
  9. package/_cjs/core/{TransactionEnvelope.js → TxEnvelope.js} +1 -1
  10. package/_cjs/core/TxEnvelope.js.map +1 -0
  11. package/_cjs/core/{TransactionEnvelopeEip1559.js → TxEnvelopeEip1559.js} +2 -2
  12. package/_cjs/core/TxEnvelopeEip1559.js.map +1 -0
  13. package/_cjs/core/{TransactionEnvelopeEip2930.js → TxEnvelopeEip2930.js} +2 -2
  14. package/_cjs/core/TxEnvelopeEip2930.js.map +1 -0
  15. package/_cjs/core/{TransactionEnvelopeEip4844.js → TxEnvelopeEip4844.js} +4 -4
  16. package/_cjs/core/TxEnvelopeEip4844.js.map +1 -0
  17. package/_cjs/core/{TransactionEnvelopeEip7702.js → TxEnvelopeEip7702.js} +4 -4
  18. package/_cjs/core/TxEnvelopeEip7702.js.map +1 -0
  19. package/_cjs/core/{TransactionEnvelopeLegacy.js → TxEnvelopeLegacy.js} +2 -2
  20. package/_cjs/core/TxEnvelopeLegacy.js.map +1 -0
  21. package/_cjs/core/WebAuthnP256.js +1 -1
  22. package/_cjs/core/WebAuthnP256.js.map +1 -1
  23. package/_cjs/erc8021/Attribution.js +36 -6
  24. package/_cjs/erc8021/Attribution.js.map +1 -1
  25. package/_cjs/index.docs.js +1 -0
  26. package/_cjs/index.docs.js.map +1 -1
  27. package/_cjs/index.js +7 -7
  28. package/_cjs/index.js.map +1 -1
  29. package/_cjs/tempo/AuthorizationTempo.js +101 -0
  30. package/_cjs/tempo/AuthorizationTempo.js.map +1 -0
  31. package/_cjs/tempo/KeyAuthorization.js +123 -0
  32. package/_cjs/tempo/KeyAuthorization.js.map +1 -0
  33. package/_cjs/tempo/PoolId.js +10 -0
  34. package/_cjs/tempo/PoolId.js.map +1 -0
  35. package/_cjs/tempo/SignatureEnvelope.js +394 -0
  36. package/_cjs/tempo/SignatureEnvelope.js.map +1 -0
  37. package/_cjs/tempo/Tick.js +77 -0
  38. package/_cjs/tempo/Tick.js.map +1 -0
  39. package/_cjs/tempo/TokenId.js +28 -0
  40. package/_cjs/tempo/TokenId.js.map +1 -0
  41. package/_cjs/tempo/TokenRole.js +26 -0
  42. package/_cjs/tempo/TokenRole.js.map +1 -0
  43. package/_cjs/tempo/Transaction.js +80 -0
  44. package/_cjs/tempo/Transaction.js.map +1 -0
  45. package/_cjs/tempo/TransactionReceipt.js +26 -0
  46. package/_cjs/tempo/TransactionReceipt.js.map +1 -0
  47. package/_cjs/tempo/TransactionRequest.js +53 -0
  48. package/_cjs/tempo/TransactionRequest.js.map +1 -0
  49. package/_cjs/tempo/TxEnvelopeTempo.js +267 -0
  50. package/_cjs/tempo/TxEnvelopeTempo.js.map +1 -0
  51. package/_cjs/tempo/index.js +15 -0
  52. package/_cjs/tempo/index.js.map +1 -0
  53. package/_cjs/version.js +1 -1
  54. package/_esm/core/Blobs.js +8 -8
  55. package/_esm/core/{TransactionEnvelope.js → TxEnvelope.js} +11 -11
  56. package/_esm/core/TxEnvelope.js.map +1 -0
  57. package/_esm/core/{TransactionEnvelopeEip1559.js → TxEnvelopeEip1559.js} +42 -42
  58. package/_esm/core/TxEnvelopeEip1559.js.map +1 -0
  59. package/_esm/core/{TransactionEnvelopeEip2930.js → TxEnvelopeEip2930.js} +43 -43
  60. package/_esm/core/TxEnvelopeEip2930.js.map +1 -0
  61. package/_esm/core/{TransactionEnvelopeEip4844.js → TxEnvelopeEip4844.js} +42 -42
  62. package/_esm/core/TxEnvelopeEip4844.js.map +1 -0
  63. package/_esm/core/{TransactionEnvelopeEip7702.js → TxEnvelopeEip7702.js} +40 -40
  64. package/_esm/core/TxEnvelopeEip7702.js.map +1 -0
  65. package/_esm/core/{TransactionEnvelopeLegacy.js → TxEnvelopeLegacy.js} +42 -42
  66. package/_esm/core/TxEnvelopeLegacy.js.map +1 -0
  67. package/_esm/core/WebAuthnP256.js +1 -1
  68. package/_esm/core/WebAuthnP256.js.map +1 -1
  69. package/_esm/erc8021/Attribution.js +58 -13
  70. package/_esm/erc8021/Attribution.js.map +1 -1
  71. package/_esm/index.docs.js +1 -0
  72. package/_esm/index.docs.js.map +1 -1
  73. package/_esm/index.js +192 -192
  74. package/_esm/index.js.map +1 -1
  75. package/_esm/tempo/AuthorizationTempo.js +664 -0
  76. package/_esm/tempo/AuthorizationTempo.js.map +1 -0
  77. package/_esm/tempo/KeyAuthorization.js +426 -0
  78. package/_esm/tempo/KeyAuthorization.js.map +1 -0
  79. package/_esm/tempo/PoolId.js +28 -0
  80. package/_esm/tempo/PoolId.js.map +1 -0
  81. package/_esm/tempo/SignatureEnvelope.js +660 -0
  82. package/_esm/tempo/SignatureEnvelope.js.map +1 -0
  83. package/_esm/tempo/Tick.js +147 -0
  84. package/_esm/tempo/Tick.js.map +1 -0
  85. package/_esm/tempo/TokenId.js +71 -0
  86. package/_esm/tempo/TokenId.js.map +1 -0
  87. package/_esm/tempo/TokenRole.js +40 -0
  88. package/_esm/tempo/TokenRole.js.map +1 -0
  89. package/_esm/tempo/Transaction.js +167 -0
  90. package/_esm/tempo/Transaction.js.map +1 -0
  91. package/_esm/tempo/TransactionReceipt.js +138 -0
  92. package/_esm/tempo/TransactionReceipt.js.map +1 -0
  93. package/_esm/tempo/TransactionRequest.js +99 -0
  94. package/_esm/tempo/TransactionRequest.js.map +1 -0
  95. package/_esm/tempo/TxEnvelopeTempo.js +607 -0
  96. package/_esm/tempo/TxEnvelopeTempo.js.map +1 -0
  97. package/_esm/tempo/index.js +298 -0
  98. package/_esm/tempo/index.js.map +1 -0
  99. package/_esm/version.js +1 -1
  100. package/_types/core/Blobs.d.ts +8 -8
  101. package/_types/core/{TransactionEnvelope.d.ts → TxEnvelope.d.ts} +11 -11
  102. package/_types/core/TxEnvelope.d.ts.map +1 -0
  103. package/_types/core/{TransactionEnvelopeEip1559.d.ts → TxEnvelopeEip1559.d.ts} +54 -54
  104. package/_types/core/TxEnvelopeEip1559.d.ts.map +1 -0
  105. package/_types/core/{TransactionEnvelopeEip2930.d.ts → TxEnvelopeEip2930.d.ts} +55 -55
  106. package/_types/core/TxEnvelopeEip2930.d.ts.map +1 -0
  107. package/_types/core/{TransactionEnvelopeEip4844.d.ts → TxEnvelopeEip4844.d.ts} +54 -54
  108. package/_types/core/TxEnvelopeEip4844.d.ts.map +1 -0
  109. package/_types/core/{TransactionEnvelopeEip7702.d.ts → TxEnvelopeEip7702.d.ts} +49 -49
  110. package/_types/core/TxEnvelopeEip7702.d.ts.map +1 -0
  111. package/_types/core/{TransactionEnvelopeLegacy.d.ts → TxEnvelopeLegacy.d.ts} +54 -54
  112. package/_types/core/TxEnvelopeLegacy.d.ts.map +1 -0
  113. package/_types/core/WebAuthnP256.d.ts +1 -1
  114. package/_types/core/WebAuthnP256.d.ts.map +1 -1
  115. package/_types/erc8021/Attribution.d.ts +20 -6
  116. package/_types/erc8021/Attribution.d.ts.map +1 -1
  117. package/_types/index.d.ts +192 -192
  118. package/_types/index.d.ts.map +1 -1
  119. package/_types/index.docs.d.ts +1 -0
  120. package/_types/index.docs.d.ts.map +1 -1
  121. package/_types/tempo/AuthorizationTempo.d.ts +688 -0
  122. package/_types/tempo/AuthorizationTempo.d.ts.map +1 -0
  123. package/_types/tempo/KeyAuthorization.d.ts +437 -0
  124. package/_types/tempo/KeyAuthorization.d.ts.map +1 -0
  125. package/_types/tempo/PoolId.d.ts +33 -0
  126. package/_types/tempo/PoolId.d.ts.map +1 -0
  127. package/_types/tempo/SignatureEnvelope.d.ts +438 -0
  128. package/_types/tempo/SignatureEnvelope.d.ts.map +1 -0
  129. package/_types/tempo/Tick.d.ts +120 -0
  130. package/_types/tempo/Tick.d.ts.map +1 -0
  131. package/_types/tempo/TokenId.d.ts +55 -0
  132. package/_types/tempo/TokenId.d.ts.map +1 -0
  133. package/_types/tempo/TokenRole.d.ts +29 -0
  134. package/_types/tempo/TokenRole.d.ts.map +1 -0
  135. package/_types/tempo/Transaction.d.ts +208 -0
  136. package/_types/tempo/Transaction.d.ts.map +1 -0
  137. package/_types/tempo/TransactionReceipt.d.ts +165 -0
  138. package/_types/tempo/TransactionReceipt.d.ts.map +1 -0
  139. package/_types/tempo/TransactionRequest.d.ts +89 -0
  140. package/_types/tempo/TransactionRequest.d.ts.map +1 -0
  141. package/_types/tempo/TxEnvelopeTempo.d.ts +551 -0
  142. package/_types/tempo/TxEnvelopeTempo.d.ts.map +1 -0
  143. package/_types/tempo/index.d.ts +300 -0
  144. package/_types/tempo/index.d.ts.map +1 -0
  145. package/_types/version.d.ts +1 -1
  146. package/core/Blobs.ts +8 -8
  147. package/core/{TransactionEnvelope.ts → TxEnvelope.ts} +10 -10
  148. package/core/{TransactionEnvelopeEip1559.ts → TxEnvelopeEip1559.ts} +60 -69
  149. package/core/{TransactionEnvelopeEip2930.ts → TxEnvelopeEip2930.ts} +61 -72
  150. package/core/{TransactionEnvelopeEip4844.ts → TxEnvelopeEip4844.ts} +62 -71
  151. package/core/{TransactionEnvelopeEip7702.ts → TxEnvelopeEip7702.ts} +58 -67
  152. package/core/{TransactionEnvelopeLegacy.ts → TxEnvelopeLegacy.ts} +59 -68
  153. package/core/WebAuthnP256.ts +3 -1
  154. package/erc8021/Attribution.ts +77 -15
  155. package/index.docs.ts +1 -0
  156. package/index.ts +192 -195
  157. package/package.json +91 -31
  158. package/tempo/AuthorizationTempo/package.json +6 -0
  159. package/tempo/AuthorizationTempo.test.ts +1293 -0
  160. package/tempo/AuthorizationTempo.ts +884 -0
  161. package/tempo/KeyAuthorization/package.json +6 -0
  162. package/tempo/KeyAuthorization.test.ts +1373 -0
  163. package/tempo/KeyAuthorization.ts +622 -0
  164. package/tempo/PoolId/package.json +6 -0
  165. package/tempo/PoolId.test.ts +33 -0
  166. package/tempo/PoolId.ts +42 -0
  167. package/tempo/SignatureEnvelope/package.json +6 -0
  168. package/tempo/SignatureEnvelope.test.ts +1877 -0
  169. package/tempo/SignatureEnvelope.ts +973 -0
  170. package/tempo/Tick/package.json +6 -0
  171. package/tempo/Tick.test.ts +281 -0
  172. package/tempo/Tick.ts +186 -0
  173. package/tempo/TokenId/package.json +6 -0
  174. package/tempo/TokenId.test.ts +40 -0
  175. package/tempo/TokenId.ts +80 -0
  176. package/tempo/TokenRole/package.json +6 -0
  177. package/tempo/TokenRole.test.ts +16 -0
  178. package/tempo/TokenRole.ts +45 -0
  179. package/tempo/Transaction/package.json +6 -0
  180. package/tempo/Transaction.test.ts +523 -0
  181. package/tempo/Transaction.ts +339 -0
  182. package/tempo/TransactionReceipt/package.json +6 -0
  183. package/tempo/TransactionReceipt.ts +200 -0
  184. package/tempo/TransactionRequest/package.json +6 -0
  185. package/tempo/TransactionRequest.ts +160 -0
  186. package/tempo/TxEnvelopeTempo/package.json +6 -0
  187. package/tempo/TxEnvelopeTempo.test.ts +1371 -0
  188. package/tempo/TxEnvelopeTempo.ts +972 -0
  189. package/tempo/e2e.test.ts +1387 -0
  190. package/tempo/index.ts +308 -0
  191. package/tempo/package.json +6 -0
  192. package/version.ts +1 -1
  193. package/TransactionEnvelope/package.json +0 -6
  194. package/TransactionEnvelopeEip1559/package.json +0 -6
  195. package/TransactionEnvelopeEip2930/package.json +0 -6
  196. package/TransactionEnvelopeEip4844/package.json +0 -6
  197. package/TransactionEnvelopeEip7702/package.json +0 -6
  198. package/TransactionEnvelopeLegacy/package.json +0 -6
  199. package/_cjs/core/TransactionEnvelope.js.map +0 -1
  200. package/_cjs/core/TransactionEnvelopeEip1559.js.map +0 -1
  201. package/_cjs/core/TransactionEnvelopeEip2930.js.map +0 -1
  202. package/_cjs/core/TransactionEnvelopeEip4844.js.map +0 -1
  203. package/_cjs/core/TransactionEnvelopeEip7702.js.map +0 -1
  204. package/_cjs/core/TransactionEnvelopeLegacy.js.map +0 -1
  205. package/_esm/core/TransactionEnvelope.js.map +0 -1
  206. package/_esm/core/TransactionEnvelopeEip1559.js.map +0 -1
  207. package/_esm/core/TransactionEnvelopeEip2930.js.map +0 -1
  208. package/_esm/core/TransactionEnvelopeEip4844.js.map +0 -1
  209. package/_esm/core/TransactionEnvelopeEip7702.js.map +0 -1
  210. package/_esm/core/TransactionEnvelopeLegacy.js.map +0 -1
  211. package/_types/core/TransactionEnvelope.d.ts.map +0 -1
  212. package/_types/core/TransactionEnvelopeEip1559.d.ts.map +0 -1
  213. package/_types/core/TransactionEnvelopeEip2930.d.ts.map +0 -1
  214. package/_types/core/TransactionEnvelopeEip4844.d.ts.map +0 -1
  215. package/_types/core/TransactionEnvelopeEip7702.d.ts.map +0 -1
  216. package/_types/core/TransactionEnvelopeLegacy.d.ts.map +0 -1
@@ -0,0 +1,622 @@
1
+ import type * as Address from '../core/Address.js'
2
+ import type * as Errors from '../core/Errors.js'
3
+ import * as Hash from '../core/Hash.js'
4
+ import * as Hex from '../core/Hex.js'
5
+ import type { Compute } from '../core/internal/types.js'
6
+ import * as Rlp from '../core/Rlp.js'
7
+ import * as SignatureEnvelope from './SignatureEnvelope.js'
8
+
9
+ /**
10
+ * Key authorization for provisioning access keys.
11
+ *
12
+ * Access keys allow a root key (e.g., a passkey) to delegate transaction signing to secondary
13
+ * keys with customizable permissions including expiry timestamps and per-TIP-20 token spending
14
+ * limits. This enables a user to sign transactions without repeated passkey prompts.
15
+ *
16
+ * The root key signs a `KeyAuthorization` to grant an access key permission to sign transactions
17
+ * on its behalf. The authorization is attached to a transaction (which can be signed by the access
18
+ * key itself), and the protocol validates the authorization before storing the key in the
19
+ * AccountKeychain precompile.
20
+ *
21
+ * Key authorization fields:
22
+ * - `address`: Address derived from the access key's public key (the "key ID")
23
+ * - `chainId`: Chain ID for replay protection (0 = valid on any chain)
24
+ * - `expiry`: Unix timestamp when the key expires (undefined = never expires)
25
+ * - `limits`: Per-TIP-20 token spending limits (only applies to `transfer()` and `approve()` calls)
26
+ * - `type`: Key type (`secp256k1`, `p256`, or `webAuthn`)
27
+ *
28
+ * [Access Keys Specification](https://docs.tempo.xyz/protocol/transactions/spec-tempo-transaction#access-keys)
29
+ */
30
+ export type KeyAuthorization<
31
+ signed extends boolean = boolean,
32
+ bigintType = bigint,
33
+ numberType = number,
34
+ > = {
35
+ /** Address derived from the public key of the key type. */
36
+ address: Address.Address
37
+ /** Chain ID for replay protection (0 = valid on any chain). */
38
+ chainId?: bigintType | undefined
39
+ /** Unix timestamp when key expires (0 = never expires). */
40
+ expiry?: numberType | undefined
41
+ /** TIP20 spending limits for this key. */
42
+ limits?: readonly TokenLimit<bigintType>[] | undefined
43
+ /** Key type. (secp256k1, P256, WebAuthn). */
44
+ type: SignatureEnvelope.Type
45
+ } & (signed extends true
46
+ ? { signature: SignatureEnvelope.SignatureEnvelope<bigintType, numberType> }
47
+ : {
48
+ signature?:
49
+ | SignatureEnvelope.SignatureEnvelope<bigintType, numberType>
50
+ | undefined
51
+ })
52
+
53
+ /** RPC representation of an {@link ox#KeyAuthorization.KeyAuthorization}. */
54
+ export type Rpc = Omit<
55
+ KeyAuthorization<false, Hex.Hex, Hex.Hex>,
56
+ 'address' | 'signature' | 'type'
57
+ > & {
58
+ keyId: Address.Address
59
+ keyType: SignatureEnvelope.Type
60
+ signature: SignatureEnvelope.SignatureEnvelopeRpc
61
+ }
62
+
63
+ /** Signed representation of a Key Authorization. */
64
+ export type Signed<bigintType = bigint, numberType = number> = KeyAuthorization<
65
+ true,
66
+ bigintType,
67
+ numberType
68
+ >
69
+
70
+ type BaseTuple = readonly [
71
+ chainId: Hex.Hex,
72
+ keyType: Hex.Hex,
73
+ keyId: Address.Address,
74
+ ]
75
+
76
+ /** Tuple representation of a Key Authorization. */
77
+ export type Tuple<signed extends boolean = boolean> = signed extends true
78
+ ? readonly [
79
+ authorization:
80
+ | BaseTuple
81
+ | readonly [...BaseTuple, expiry: Hex.Hex]
82
+ | readonly [
83
+ ...BaseTuple,
84
+ expiry: Hex.Hex,
85
+ limits: readonly [token: Address.Address, limit: Hex.Hex][],
86
+ ],
87
+ signature: Hex.Hex,
88
+ ]
89
+ : readonly [
90
+ authorization:
91
+ | BaseTuple
92
+ | readonly [...BaseTuple, expiry: Hex.Hex]
93
+ | readonly [
94
+ ...BaseTuple,
95
+ expiry: Hex.Hex,
96
+ limits: readonly [token: Address.Address, limit: Hex.Hex][],
97
+ ],
98
+ ]
99
+
100
+ /**
101
+ * Token spending limit for access keys.
102
+ *
103
+ * Defines a per-TIP-20 token spending limit for an access key. Limits deplete as tokens
104
+ * are spent and can be updated by the root key via `updateSpendingLimit()`.
105
+ *
106
+ * [Access Keys Specification](https://docs.tempo.xyz/protocol/transactions/spec-tempo-transaction#access-keys)
107
+ */
108
+ export type TokenLimit<bigintType = bigint> = {
109
+ /** Address of the TIP-20 token. */
110
+ token: Address.Address
111
+ /** Maximum spending amount for this token (enforced over the key's lifetime). */
112
+ limit: bigintType
113
+ }
114
+
115
+ /**
116
+ * Converts a Key Authorization object into a typed {@link ox#KeyAuthorization.KeyAuthorization}.
117
+ *
118
+ * Use this to create an unsigned key authorization, then sign it with the root key using
119
+ * {@link ox#KeyAuthorization.(getSignPayload:function)} and attach the signature. The signed authorization
120
+ * can be included in a {@link ox#TxEnvelopeTempo.TxEnvelopeTempo} via the
121
+ * `keyAuthorization` field to provision the access key on-chain.
122
+ *
123
+ * [Access Keys Specification](https://docs.tempo.xyz/protocol/transactions/spec-tempo-transaction#access-keys)
124
+ *
125
+ * @example
126
+ * ### Secp256k1 Key
127
+ *
128
+ * Standard Ethereum ECDSA key using the secp256k1 curve.
129
+ *
130
+ * ```ts twoslash
131
+ * import { Address, Secp256k1, Value } from 'ox'
132
+ * import { KeyAuthorization } from 'ox/tempo'
133
+ *
134
+ * const privateKey = Secp256k1.randomPrivateKey()
135
+ * const address = Address.fromPublicKey(Secp256k1.getPublicKey({ privateKey }))
136
+ *
137
+ * const authorization = KeyAuthorization.from({
138
+ * address,
139
+ * expiry: 1234567890,
140
+ * type: 'secp256k1',
141
+ * limits: [{
142
+ * token: '0x20c0000000000000000000000000000000000001',
143
+ * limit: Value.from('10', 6),
144
+ * }],
145
+ * })
146
+ * ```
147
+ *
148
+ * @example
149
+ * ### WebCryptoP256 Key
150
+ *
151
+ * ```ts twoslash
152
+ * import { Address, WebCryptoP256, Value } from 'ox'
153
+ * import { KeyAuthorization } from 'ox/tempo'
154
+ *
155
+ * const keyPair = await WebCryptoP256.createKeyPair()
156
+ * const address = Address.fromPublicKey(keyPair.publicKey)
157
+ *
158
+ * const authorization = KeyAuthorization.from({
159
+ * address,
160
+ * expiry: 1234567890,
161
+ * type: 'p256',
162
+ * limits: [{
163
+ * token: '0x20c0000000000000000000000000000000000001',
164
+ * limit: Value.from('10', 6),
165
+ * }],
166
+ * })
167
+ * ```
168
+ *
169
+ * @example
170
+ * ### Attaching Signatures (Secp256k1)
171
+ *
172
+ * Attach a signature to a Key Authorization using a Secp256k1 private key to
173
+ * authorize another Secp256k1 key on the account.
174
+ *
175
+ * ```ts twoslash
176
+ * import { Address, Secp256k1, Value } from 'ox'
177
+ * import { KeyAuthorization } from 'ox/tempo'
178
+ *
179
+ * const privateKey = '0x...'
180
+ * const address = Address.fromPublicKey(Secp256k1.getPublicKey({ privateKey }))
181
+ *
182
+ * const authorization = KeyAuthorization.from({
183
+ * address,
184
+ * expiry: 1234567890,
185
+ * type: 'secp256k1',
186
+ * limits: [{
187
+ * token: '0x20c0000000000000000000000000000000000001',
188
+ * limit: Value.from('10', 6),
189
+ * }],
190
+ * })
191
+ *
192
+ * const rootPrivateKey = '0x...'
193
+ * const signature = Secp256k1.sign({
194
+ * payload: KeyAuthorization.getSignPayload(authorization),
195
+ * privateKey: rootPrivateKey,
196
+ * })
197
+ *
198
+ * const authorization_signed = KeyAuthorization.from(authorization, { signature })
199
+ * ```
200
+ *
201
+ * @example
202
+ * ### Attaching Signatures (WebAuthn)
203
+ *
204
+ * Attach a signature to a Key Authorization using a WebAuthn credential to
205
+ * authorize a new WebCryptoP256 key on the account.
206
+ *
207
+ * ```ts twoslash
208
+ * // @noErrors
209
+ * import { Address, Value, WebCryptoP256, WebAuthnP256 } from 'ox'
210
+ * import { KeyAuthorization, SignatureEnvelope } from 'ox/tempo'
211
+ *
212
+ * const keyPair = await WebCryptoP256.createKeyPair()
213
+ * const address = Address.fromPublicKey(keyPair.publicKey)
214
+ *
215
+ * const authorization = KeyAuthorization.from({
216
+ * address,
217
+ * expiry: 1234567890,
218
+ * type: 'p256',
219
+ * limits: [{
220
+ * token: '0x20c0000000000000000000000000000000000001',
221
+ * limit: Value.from('10', 6),
222
+ * }],
223
+ * })
224
+ *
225
+ * const credential = await WebAuthnP256.createCredential({ name: 'Example' })
226
+ *
227
+ * const { metadata, signature } = await WebAuthnP256.sign({
228
+ * challenge: KeyAuthorization.getSignPayload(authorization),
229
+ * credentialId: credential.id,
230
+ * })
231
+ *
232
+ * const signatureEnvelope = SignatureEnvelope.from({ // [!code focus]
233
+ * signature, // [!code focus]
234
+ * publicKey: credential.publicKey, // [!code focus]
235
+ * metadata, // [!code focus]
236
+ * })
237
+ * const authorization_signed = KeyAuthorization.from(
238
+ * authorization,
239
+ * { signature: signatureEnvelope }, // [!code focus]
240
+ * )
241
+ * ```
242
+ *
243
+ * @param authorization - A Key Authorization tuple in object format.
244
+ * @param options - Key Authorization options.
245
+ * @returns The {@link ox#KeyAuthorization.KeyAuthorization}.
246
+ */
247
+ export function from<
248
+ const authorization extends KeyAuthorization | Rpc,
249
+ const signature extends SignatureEnvelope.from.Value | undefined = undefined,
250
+ >(
251
+ authorization: authorization | KeyAuthorization,
252
+ options: from.Options<signature> = {},
253
+ ): from.ReturnType<authorization, signature> {
254
+ if (typeof authorization.expiry === 'string')
255
+ return fromRpc(authorization as Rpc) as never
256
+ if (options.signature)
257
+ return {
258
+ ...authorization,
259
+ signature: SignatureEnvelope.from(options.signature),
260
+ } as never
261
+ return authorization as never
262
+ }
263
+
264
+ export declare namespace from {
265
+ type Options<
266
+ signature extends SignatureEnvelope.from.Value | undefined =
267
+ | SignatureEnvelope.from.Value
268
+ | undefined,
269
+ > = {
270
+ /** The {@link ox#SignatureEnvelope.SignatureEnvelope} to attach to the Key Authorization. */
271
+ signature?: signature | SignatureEnvelope.SignatureEnvelope | undefined
272
+ }
273
+
274
+ type ReturnType<
275
+ authorization extends KeyAuthorization | Rpc = KeyAuthorization,
276
+ signature extends SignatureEnvelope.from.Value | undefined =
277
+ | SignatureEnvelope.from.Value
278
+ | undefined,
279
+ > = Compute<
280
+ authorization extends Rpc
281
+ ? Signed
282
+ : authorization &
283
+ (signature extends SignatureEnvelope.from.Value
284
+ ? { signature: SignatureEnvelope.from.ReturnValue<signature> }
285
+ : {})
286
+ >
287
+
288
+ type ErrorType = Errors.GlobalErrorType
289
+ }
290
+
291
+ /**
292
+ * Converts an {@link ox#AuthorizationTempo.Rpc} to an {@link ox#AuthorizationTempo.AuthorizationTempo}.
293
+ *
294
+ * @example
295
+ * ```ts twoslash
296
+ * import { KeyAuthorization } from 'ox/tempo'
297
+ *
298
+ * const keyAuthorization = KeyAuthorization.fromRpc({
299
+ * expiry: '0x174876e800',
300
+ * keyId: '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c',
301
+ * keyType: 'secp256k1',
302
+ * limits: [{ token: '0x20c0000000000000000000000000000000000001', limit: '0xf4240' }],
303
+ * signature: {
304
+ * type: 'secp256k1',
305
+ * r: '0x635dc2033e60185bb36709c29c75d64ea51dfbd91c32ef4be198e4ceb169fb4d',
306
+ * s: '0x50c2667ac4c771072746acfdcf1f1483336dcca8bd2df47cd83175dbe60f0540',
307
+ * yParity: '0x0'
308
+ * },
309
+ * })
310
+ * ```
311
+ *
312
+ * @param authorization - The RPC-formatted Key Authorization.
313
+ * @returns A signed {@link ox#AuthorizationTempo.AuthorizationTempo}.
314
+ */
315
+ export function fromRpc(authorization: Rpc): Signed {
316
+ const { chainId = '0x0', keyId, expiry = 0, limits, keyType } = authorization
317
+ const signature = SignatureEnvelope.fromRpc(authorization.signature)
318
+ return {
319
+ address: keyId,
320
+ chainId: chainId === '0x' ? 0n : Hex.toBigInt(chainId),
321
+ expiry: Number(expiry),
322
+ limits: limits?.map((limit) => ({
323
+ token: limit.token,
324
+ limit: BigInt(limit.limit),
325
+ })),
326
+ signature,
327
+ type: keyType,
328
+ }
329
+ }
330
+
331
+ export declare namespace fromRpc {
332
+ type ErrorType = Errors.GlobalErrorType
333
+ }
334
+
335
+ /**
336
+ * Converts an {@link ox#KeyAuthorization.Tuple} to an {@link ox#KeyAuthorization.KeyAuthorization}.
337
+ *
338
+ * @example
339
+ * ```ts twoslash
340
+ * import { KeyAuthorization } from 'ox/tempo'
341
+ *
342
+ * const authorization = KeyAuthorization.fromTuple([
343
+ * [
344
+ * '0x',
345
+ * '0x00',
346
+ * '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c',
347
+ * '0x174876e800',
348
+ * [['0x20c0000000000000000000000000000000000001', '0xf4240']],
349
+ * ],
350
+ * '0x01a068a020a209d3d56c46f38cc50a33f704f4a9a10a59377f8dd762ac66910e9b907e865ad05c4035ab5792787d4a0297a43617ae897930a6fe4d822b8faea52064',
351
+ * ])
352
+ * ```
353
+ *
354
+ * @example
355
+ * Unsigned Key Authorization tuple (no signature):
356
+ *
357
+ * ```ts twoslash
358
+ * import { KeyAuthorization } from 'ox/tempo'
359
+ *
360
+ * const authorization = KeyAuthorization.fromTuple([
361
+ * [
362
+ * '0x',
363
+ * '0x00',
364
+ * '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c',
365
+ * '0x174876e800',
366
+ * [['0x20c0000000000000000000000000000000000001', '0xf4240']],
367
+ * ],
368
+ * ])
369
+ * ```
370
+ *
371
+ * @param tuple - The Key Authorization tuple.
372
+ * @returns The {@link ox#KeyAuthorization.KeyAuthorization}.
373
+ */
374
+ export function fromTuple<const tuple extends Tuple>(
375
+ tuple: tuple,
376
+ ): fromTuple.ReturnType<tuple> {
377
+ const [authorization, signatureSerialized] = tuple
378
+ const [chainId, keyType_hex, keyId, expiry, limits] = authorization
379
+ const keyType = (() => {
380
+ switch (keyType_hex) {
381
+ case '0x':
382
+ case '0x00':
383
+ return 'secp256k1'
384
+ case '0x01':
385
+ return 'p256'
386
+ case '0x02':
387
+ return 'webAuthn'
388
+ default:
389
+ throw new Error(`Invalid key type: ${keyType_hex}`)
390
+ }
391
+ })()
392
+ const args: KeyAuthorization = {
393
+ address: keyId,
394
+ expiry: typeof expiry !== 'undefined' ? Hex.toNumber(expiry) : undefined,
395
+ type: keyType,
396
+ ...(chainId !== '0x' ? { chainId: Hex.toBigInt(chainId) } : {}),
397
+ ...(typeof expiry !== 'undefined' ? { expiry: Hex.toNumber(expiry) } : {}),
398
+ ...(typeof limits !== 'undefined'
399
+ ? {
400
+ limits: limits.map(([token, limit]) => ({
401
+ token,
402
+ limit: BigInt(limit),
403
+ })),
404
+ }
405
+ : {}),
406
+ }
407
+ if (signatureSerialized)
408
+ args.signature = SignatureEnvelope.deserialize(signatureSerialized)
409
+ return from(args) as never
410
+ }
411
+
412
+ export declare namespace fromTuple {
413
+ type ReturnType<authorization extends Tuple = Tuple> = Compute<
414
+ KeyAuthorization<authorization extends Tuple<true> ? true : false>
415
+ >
416
+
417
+ type ErrorType = Errors.GlobalErrorType
418
+ }
419
+
420
+ /**
421
+ * Computes the sign payload for an {@link ox#KeyAuthorization.KeyAuthorization}.
422
+ *
423
+ * The root key must sign this payload to authorize the access key. The resulting signature
424
+ * is attached to the key authorization via {@link ox#KeyAuthorization.(from:function)} with the
425
+ * `signature` option.
426
+ *
427
+ * [Access Keys Specification](https://docs.tempo.xyz/protocol/transactions/spec-tempo-transaction#access-keys)
428
+ *
429
+ * @example
430
+ * ```ts twoslash
431
+ * import { Address, Secp256k1, Value } from 'ox'
432
+ * import { KeyAuthorization } from 'ox/tempo'
433
+ *
434
+ * const privateKey = '0x...'
435
+ * const address = Address.fromPublicKey(Secp256k1.getPublicKey({ privateKey }))
436
+ *
437
+ * const authorization = KeyAuthorization.from({
438
+ * address,
439
+ * expiry: 1234567890,
440
+ * type: 'secp256k1',
441
+ * limits: [{
442
+ * token: '0x20c0000000000000000000000000000000000001',
443
+ * limit: Value.from('10', 6),
444
+ * }],
445
+ * })
446
+ *
447
+ * const payload = KeyAuthorization.getSignPayload(authorization) // [!code focus]
448
+ * ```
449
+ *
450
+ * @param authorization - The {@link ox#KeyAuthorization.KeyAuthorization}.
451
+ * @returns The sign payload.
452
+ */
453
+ export function getSignPayload(authorization: KeyAuthorization): Hex.Hex {
454
+ return hash(authorization)
455
+ }
456
+
457
+ export declare namespace getSignPayload {
458
+ type ErrorType = hash.ErrorType | Errors.GlobalErrorType
459
+ }
460
+
461
+ /**
462
+ * Computes the hash for an {@link ox#KeyAuthorization.KeyAuthorization}.
463
+ *
464
+ * @example
465
+ * ```ts twoslash
466
+ * import { KeyAuthorization } from 'ox/tempo'
467
+ * import { Value } from 'ox'
468
+ *
469
+ * const authorization = KeyAuthorization.from({
470
+ * expiry: 1234567890,
471
+ * address: '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c',
472
+ * type: 'secp256k1',
473
+ * limits: [{
474
+ * token: '0x20c0000000000000000000000000000000000001',
475
+ * limit: Value.from('10', 6)
476
+ * }],
477
+ * })
478
+ *
479
+ * const hash = KeyAuthorization.hash(authorization) // [!code focus]
480
+ * ```
481
+ *
482
+ * @param authorization - The {@link ox#KeyAuthorization.KeyAuthorization}.
483
+ * @returns The hash.
484
+ */
485
+ export function hash(authorization: KeyAuthorization): Hex.Hex {
486
+ const [authorizationTuple] = toTuple(authorization)
487
+ const serialized = Rlp.fromHex(authorizationTuple)
488
+ return Hash.keccak256(serialized)
489
+ }
490
+
491
+ export declare namespace hash {
492
+ type ErrorType =
493
+ | toTuple.ErrorType
494
+ | Hash.keccak256.ErrorType
495
+ | Hex.concat.ErrorType
496
+ | Rlp.fromHex.ErrorType
497
+ | Errors.GlobalErrorType
498
+ }
499
+
500
+ /**
501
+ * Converts an {@link ox#KeyAuthorization.KeyAuthorization} to an {@link ox#KeyAuthorization.Rpc}.
502
+ *
503
+ * @example
504
+ * ```ts twoslash
505
+ * import { KeyAuthorization } from 'ox/tempo'
506
+ * import { Value } from 'ox'
507
+ *
508
+ * const authorization = KeyAuthorization.toRpc({
509
+ * expiry: 1234567890,
510
+ * address: '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c',
511
+ * type: 'secp256k1',
512
+ * limits: [{
513
+ * token: '0x20c0000000000000000000000000000000000001',
514
+ * limit: Value.from('10', 6)
515
+ * }],
516
+ * signature: {
517
+ * type: 'secp256k1',
518
+ * signature: {
519
+ * r: 44944627813007772897391531230081695102703289123332187696115181104739239197517n,
520
+ * s: 36528503505192438307355164441104001310566505351980369085208178712678799181120n,
521
+ * yParity: 0,
522
+ * },
523
+ * },
524
+ * })
525
+ * ```
526
+ *
527
+ * @param authorization - A Key Authorization.
528
+ * @returns An RPC-formatted Key Authorization.
529
+ */
530
+ export function toRpc(authorization: Signed): Rpc {
531
+ const {
532
+ address,
533
+ chainId = 0n,
534
+ expiry,
535
+ limits,
536
+ type,
537
+ signature,
538
+ } = authorization
539
+
540
+ return {
541
+ chainId: chainId === 0n ? '0x' : Hex.fromNumber(chainId),
542
+ expiry: typeof expiry === 'number' ? Hex.fromNumber(expiry) : undefined,
543
+ limits: limits?.map(({ token, limit }) => ({
544
+ token,
545
+ limit: Hex.fromNumber(limit),
546
+ })),
547
+ keyId: address,
548
+ signature: SignatureEnvelope.toRpc(signature),
549
+ keyType: type,
550
+ }
551
+ }
552
+
553
+ export declare namespace toRpc {
554
+ type ErrorType = Errors.GlobalErrorType
555
+ }
556
+
557
+ /**
558
+ * Converts an {@link ox#KeyAuthorization.KeyAuthorization} to an {@link ox#KeyAuthorization.Tuple}.
559
+ *
560
+ * @example
561
+ * ```ts twoslash
562
+ * import { KeyAuthorization } from 'ox/tempo'
563
+ * import { Value } from 'ox'
564
+ *
565
+ * const authorization = KeyAuthorization.from({
566
+ * expiry: 1234567890,
567
+ * address: '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c',
568
+ * type: 'secp256k1',
569
+ * limits: [{
570
+ * token: '0x20c0000000000000000000000000000000000001',
571
+ * limit: Value.from('10', 6)
572
+ * }],
573
+ * })
574
+ *
575
+ * const tuple = KeyAuthorization.toTuple(authorization) // [!code focus]
576
+ * // @log: [
577
+ * // @log: '0x174876e800',
578
+ * // @log: [['0x20c0000000000000000000000000000000000001', '0xf4240']],
579
+ * // @log: '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c',
580
+ * // @log: 'secp256k1',
581
+ * // @log: ]
582
+ * ```
583
+ *
584
+ * @param authorization - The {@link ox#KeyAuthorization.KeyAuthorization}.
585
+ * @returns A Tempo Key Authorization tuple.
586
+ */
587
+ export function toTuple<const authorization extends KeyAuthorization>(
588
+ authorization: authorization,
589
+ ): toTuple.ReturnType<authorization> {
590
+ const { address, chainId = 0n, expiry, limits } = authorization
591
+ const signature = authorization.signature
592
+ ? SignatureEnvelope.serialize(authorization.signature)
593
+ : undefined
594
+ const type = (() => {
595
+ switch (authorization.type) {
596
+ case 'secp256k1':
597
+ return '0x'
598
+ case 'p256':
599
+ return '0x01'
600
+ case 'webAuthn':
601
+ return '0x02'
602
+ default:
603
+ throw new Error(`Invalid key type: ${authorization.type}`)
604
+ }
605
+ })()
606
+ const authorizationTuple = [
607
+ chainId === 0n ? '0x' : Hex.fromNumber(chainId),
608
+ type,
609
+ address,
610
+ typeof expiry === 'number' ? Hex.fromNumber(expiry) : undefined,
611
+ limits?.map((limit) => [limit.token, Hex.fromNumber(limit.limit)]) ??
612
+ undefined,
613
+ ].filter(Boolean)
614
+ return [authorizationTuple, ...(signature ? [signature] : [])] as never
615
+ }
616
+
617
+ export declare namespace toTuple {
618
+ type ReturnType<authorization extends KeyAuthorization = KeyAuthorization> =
619
+ Compute<Tuple<authorization extends KeyAuthorization<true> ? true : false>>
620
+
621
+ type ErrorType = Errors.GlobalErrorType
622
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "type": "module",
3
+ "types": "../../_types/tempo/PoolId.d.ts",
4
+ "main": "../../_cjs/tempo/PoolId.js",
5
+ "module": "../../_esm/tempo/PoolId.js"
6
+ }
@@ -0,0 +1,33 @@
1
+ import { PoolId } from 'ox/tempo'
2
+ import { expect, test } from 'vitest'
3
+
4
+ test('from', () => {
5
+ // Test with bigint token IDs
6
+ const poolId1 = PoolId.from({
7
+ userToken: 0n,
8
+ validatorToken: 1n,
9
+ })
10
+ expect(poolId1).toMatchInlineSnapshot(
11
+ `"0x24fc92718dfd933b7f831893444e0dc6072ce0fff68198eaf48e86cb1f2ee2dc"`,
12
+ )
13
+
14
+ // Test with address token IDs
15
+ const poolId2 = PoolId.from({
16
+ userToken: '0x20c0000000000000000000000000000000000000',
17
+ validatorToken: '0x20c0000000000000000000000000000000000001',
18
+ })
19
+ expect(poolId2).toBe(poolId1)
20
+
21
+ // Test with mixed types
22
+ const poolId3 = PoolId.from({
23
+ userToken: 0n,
24
+ validatorToken: '0x20c0000000000000000000000000000000000001',
25
+ })
26
+ expect(poolId3).toBe(poolId1)
27
+
28
+ const poolId4 = PoolId.from({
29
+ userToken: '0x20c0000000000000000000000000000000000000',
30
+ validatorToken: 1n,
31
+ })
32
+ expect(poolId4).toBe(poolId1)
33
+ })
@@ -0,0 +1,42 @@
1
+ import * as Hash from '../core/Hash.js'
2
+ import * as Hex from '../core/Hex.js'
3
+ import * as TokenId from './TokenId.js'
4
+
5
+ /**
6
+ * Converts a user token and validator token to a pool ID.
7
+ *
8
+ * Pool IDs are deterministic keys derived from two token addresses (order-independent)
9
+ * used to identify trading pairs on Tempo's enshrined stablecoin DEX.
10
+ *
11
+ * [Stablecoin DEX Specification](https://docs.tempo.xyz/protocol/exchange/spec)
12
+ *
13
+ * @example
14
+ * ```ts twoslash
15
+ * import { PoolId } from 'ox/tempo'
16
+ *
17
+ * const poolId = PoolId.from({
18
+ * userToken: 1n,
19
+ * validatorToken: 2n,
20
+ * })
21
+ * ```
22
+ *
23
+ * @param value - User token and validator token.
24
+ * @returns The pool ID.
25
+ */
26
+ export function from(value: from.Value): Hex.Hex {
27
+ return Hash.keccak256(
28
+ Hex.concat(
29
+ Hex.padLeft(TokenId.toAddress(value.userToken), 32),
30
+ Hex.padLeft(TokenId.toAddress(value.validatorToken), 32),
31
+ ),
32
+ )
33
+ }
34
+
35
+ export declare namespace from {
36
+ export type Value = {
37
+ /** User token. */
38
+ userToken: TokenId.TokenIdOrAddress
39
+ /** Validator token. */
40
+ validatorToken: TokenId.TokenIdOrAddress
41
+ }
42
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "type": "module",
3
+ "types": "../../_types/tempo/SignatureEnvelope.d.ts",
4
+ "main": "../../_cjs/tempo/SignatureEnvelope.js",
5
+ "module": "../../_esm/tempo/SignatureEnvelope.js"
6
+ }