tempo.ts 0.11.0 → 0.12.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 (196) hide show
  1. package/CHANGELOG.md +67 -4
  2. package/README.md +3 -34
  3. package/dist/server/Handler.d.ts +14 -14
  4. package/dist/server/Handler.d.ts.map +1 -1
  5. package/dist/server/Handler.js +16 -17
  6. package/dist/server/Handler.js.map +1 -1
  7. package/dist/wagmi/Actions/amm.d.ts +51 -51
  8. package/dist/wagmi/Actions/amm.d.ts.map +1 -1
  9. package/dist/wagmi/Actions/amm.js +37 -37
  10. package/dist/wagmi/Actions/amm.js.map +1 -1
  11. package/dist/wagmi/Actions/dex.d.ts +129 -129
  12. package/dist/wagmi/Actions/dex.d.ts.map +1 -1
  13. package/dist/wagmi/Actions/dex.js +73 -73
  14. package/dist/wagmi/Actions/dex.js.map +1 -1
  15. package/dist/wagmi/Actions/faucet.d.ts +9 -9
  16. package/dist/wagmi/Actions/faucet.d.ts.map +1 -1
  17. package/dist/wagmi/Actions/faucet.js +7 -7
  18. package/dist/wagmi/Actions/faucet.js.map +1 -1
  19. package/dist/wagmi/Actions/fee.d.ts +17 -17
  20. package/dist/wagmi/Actions/fee.d.ts.map +1 -1
  21. package/dist/wagmi/Actions/fee.js +10 -10
  22. package/dist/wagmi/Actions/fee.js.map +1 -1
  23. package/dist/wagmi/Actions/nonce.d.ts +9 -79
  24. package/dist/wagmi/Actions/nonce.d.ts.map +1 -1
  25. package/dist/wagmi/Actions/nonce.js +7 -89
  26. package/dist/wagmi/Actions/nonce.js.map +1 -1
  27. package/dist/wagmi/Actions/policy.d.ts +69 -70
  28. package/dist/wagmi/Actions/policy.d.ts.map +1 -1
  29. package/dist/wagmi/Actions/policy.js +43 -43
  30. package/dist/wagmi/Actions/policy.js.map +1 -1
  31. package/dist/wagmi/Actions/reward.d.ts +51 -51
  32. package/dist/wagmi/Actions/reward.d.ts.map +1 -1
  33. package/dist/wagmi/Actions/reward.js +31 -31
  34. package/dist/wagmi/Actions/reward.js.map +1 -1
  35. package/dist/wagmi/Actions/token.d.ts +238 -238
  36. package/dist/wagmi/Actions/token.d.ts.map +1 -1
  37. package/dist/wagmi/Actions/token.js +136 -136
  38. package/dist/wagmi/Actions/token.js.map +1 -1
  39. package/dist/wagmi/Connector.d.ts +2 -1
  40. package/dist/wagmi/Connector.d.ts.map +1 -1
  41. package/dist/wagmi/Connector.js +83 -22
  42. package/dist/wagmi/Connector.js.map +1 -1
  43. package/dist/wagmi/Hooks/nonce.d.ts +1 -52
  44. package/dist/wagmi/Hooks/nonce.d.ts.map +1 -1
  45. package/dist/wagmi/Hooks/nonce.js +1 -70
  46. package/dist/wagmi/Hooks/nonce.js.map +1 -1
  47. package/dist/wagmi/Hooks/policy.d.ts +0 -1
  48. package/dist/wagmi/Hooks/policy.d.ts.map +1 -1
  49. package/dist/wagmi/Hooks/policy.js.map +1 -1
  50. package/dist/wagmi/KeyManager.d.ts +6 -3
  51. package/dist/wagmi/KeyManager.d.ts.map +1 -1
  52. package/dist/wagmi/KeyManager.js +9 -4
  53. package/dist/wagmi/KeyManager.js.map +1 -1
  54. package/package.json +2 -2
  55. package/src/server/Handler.test.ts +2 -2
  56. package/src/server/Handler.ts +16 -17
  57. package/src/wagmi/Actions/amm.ts +63 -63
  58. package/src/wagmi/Actions/dex.test.ts +1 -1
  59. package/src/wagmi/Actions/dex.ts +153 -153
  60. package/src/wagmi/Actions/faucet.ts +11 -11
  61. package/src/wagmi/Actions/fee.ts +20 -20
  62. package/src/wagmi/Actions/nonce.test.ts +1 -64
  63. package/src/wagmi/Actions/nonce.ts +10 -142
  64. package/src/wagmi/Actions/policy.ts +83 -85
  65. package/src/wagmi/Actions/reward.ts +64 -61
  66. package/src/wagmi/Actions/token.ts +287 -283
  67. package/src/wagmi/Connector.ts +105 -31
  68. package/src/wagmi/Hooks/dex.test.ts +1 -1
  69. package/src/wagmi/Hooks/fee.test.ts +0 -6
  70. package/src/wagmi/Hooks/nonce.test.ts +1 -66
  71. package/src/wagmi/Hooks/nonce.ts +1 -114
  72. package/src/wagmi/Hooks/policy.ts +0 -2
  73. package/src/wagmi/KeyManager.ts +18 -5
  74. package/dist/viem/Abis.d.ts +0 -2649
  75. package/dist/viem/Abis.d.ts.map +0 -1
  76. package/dist/viem/Abis.js +0 -1677
  77. package/dist/viem/Abis.js.map +0 -1
  78. package/dist/viem/Account.d.ts +0 -244
  79. package/dist/viem/Account.d.ts.map +0 -1
  80. package/dist/viem/Account.js +0 -382
  81. package/dist/viem/Account.js.map +0 -1
  82. package/dist/viem/Actions/amm.d.ts +0 -1991
  83. package/dist/viem/Actions/amm.d.ts.map +0 -1
  84. package/dist/viem/Actions/amm.js +0 -814
  85. package/dist/viem/Actions/amm.js.map +0 -1
  86. package/dist/viem/Actions/dex.d.ts +0 -3900
  87. package/dist/viem/Actions/dex.d.ts.map +0 -1
  88. package/dist/viem/Actions/dex.js +0 -1414
  89. package/dist/viem/Actions/dex.js.map +0 -1
  90. package/dist/viem/Actions/faucet.d.ts +0 -69
  91. package/dist/viem/Actions/faucet.d.ts.map +0 -1
  92. package/dist/viem/Actions/faucet.js +0 -73
  93. package/dist/viem/Actions/faucet.js.map +0 -1
  94. package/dist/viem/Actions/fee.d.ts +0 -360
  95. package/dist/viem/Actions/fee.d.ts.map +0 -1
  96. package/dist/viem/Actions/fee.js +0 -237
  97. package/dist/viem/Actions/fee.js.map +0 -1
  98. package/dist/viem/Actions/nonce.d.ts +0 -257
  99. package/dist/viem/Actions/nonce.d.ts.map +0 -1
  100. package/dist/viem/Actions/nonce.js +0 -228
  101. package/dist/viem/Actions/nonce.js.map +0 -1
  102. package/dist/viem/Actions/policy.d.ts +0 -1680
  103. package/dist/viem/Actions/policy.d.ts.map +0 -1
  104. package/dist/viem/Actions/policy.js +0 -875
  105. package/dist/viem/Actions/policy.js.map +0 -1
  106. package/dist/viem/Actions/reward.d.ts +0 -2422
  107. package/dist/viem/Actions/reward.d.ts.map +0 -1
  108. package/dist/viem/Actions/reward.js +0 -651
  109. package/dist/viem/Actions/reward.js.map +0 -1
  110. package/dist/viem/Actions/token.d.ts +0 -16007
  111. package/dist/viem/Actions/token.d.ts.map +0 -1
  112. package/dist/viem/Actions/token.js +0 -2936
  113. package/dist/viem/Actions/token.js.map +0 -1
  114. package/dist/viem/Addresses.d.ts +0 -9
  115. package/dist/viem/Addresses.d.ts.map +0 -1
  116. package/dist/viem/Addresses.js +0 -9
  117. package/dist/viem/Addresses.js.map +0 -1
  118. package/dist/viem/Chain.d.ts +0 -451
  119. package/dist/viem/Chain.d.ts.map +0 -1
  120. package/dist/viem/Chain.js +0 -96
  121. package/dist/viem/Chain.js.map +0 -1
  122. package/dist/viem/Formatters.d.ts +0 -10
  123. package/dist/viem/Formatters.d.ts.map +0 -1
  124. package/dist/viem/Formatters.js +0 -104
  125. package/dist/viem/Formatters.js.map +0 -1
  126. package/dist/viem/Storage.d.ts +0 -24
  127. package/dist/viem/Storage.d.ts.map +0 -1
  128. package/dist/viem/Storage.js +0 -68
  129. package/dist/viem/Storage.js.map +0 -1
  130. package/dist/viem/Transaction.d.ts +0 -76
  131. package/dist/viem/Transaction.d.ts.map +0 -1
  132. package/dist/viem/Transaction.js +0 -176
  133. package/dist/viem/Transaction.js.map +0 -1
  134. package/dist/viem/Transport.d.ts +0 -33
  135. package/dist/viem/Transport.d.ts.map +0 -1
  136. package/dist/viem/Transport.js +0 -138
  137. package/dist/viem/Transport.js.map +0 -1
  138. package/dist/viem/WebAuthnP256.d.ts +0 -82
  139. package/dist/viem/WebAuthnP256.d.ts.map +0 -1
  140. package/dist/viem/WebAuthnP256.js +0 -97
  141. package/dist/viem/WebAuthnP256.js.map +0 -1
  142. package/dist/viem/WebCryptoP256.d.ts +0 -2
  143. package/dist/viem/WebCryptoP256.d.ts.map +0 -1
  144. package/dist/viem/WebCryptoP256.js +0 -2
  145. package/dist/viem/WebCryptoP256.js.map +0 -1
  146. package/dist/viem/internal/types.d.ts +0 -20
  147. package/dist/viem/internal/types.d.ts.map +0 -1
  148. package/dist/viem/internal/types.js +0 -2
  149. package/dist/viem/internal/types.js.map +0 -1
  150. package/dist/viem/internal/utils.d.ts +0 -14
  151. package/dist/viem/internal/utils.d.ts.map +0 -1
  152. package/dist/viem/internal/utils.js +0 -33
  153. package/dist/viem/internal/utils.js.map +0 -1
  154. package/src/chains.ts +0 -54
  155. package/src/viem/Abis.ts +0 -1688
  156. package/src/viem/Account.test.ts +0 -444
  157. package/src/viem/Account.ts +0 -601
  158. package/src/viem/Actions/account.test.ts +0 -414
  159. package/src/viem/Actions/account.ts +0 -106
  160. package/src/viem/Actions/amm.test.ts +0 -381
  161. package/src/viem/Actions/amm.ts +0 -1227
  162. package/src/viem/Actions/dex.test.ts +0 -1549
  163. package/src/viem/Actions/dex.ts +0 -2150
  164. package/src/viem/Actions/faucet.ts +0 -121
  165. package/src/viem/Actions/fee.test.ts +0 -259
  166. package/src/viem/Actions/fee.ts +0 -372
  167. package/src/viem/Actions/index.ts +0 -9
  168. package/src/viem/Actions/nonce.test.ts +0 -206
  169. package/src/viem/Actions/nonce.ts +0 -347
  170. package/src/viem/Actions/policy.test.ts +0 -534
  171. package/src/viem/Actions/policy.ts +0 -1335
  172. package/src/viem/Actions/reward.test.ts +0 -434
  173. package/src/viem/Actions/reward.ts +0 -944
  174. package/src/viem/Actions/token.test.ts +0 -3029
  175. package/src/viem/Actions/token.ts +0 -4458
  176. package/src/viem/Addresses.ts +0 -9
  177. package/src/viem/Chain.bench-d.ts +0 -12
  178. package/src/viem/Chain.test.ts +0 -168
  179. package/src/viem/Chain.ts +0 -157
  180. package/src/viem/Decorator.bench-d.ts +0 -11
  181. package/src/viem/Decorator.test.ts +0 -39
  182. package/src/viem/Decorator.ts +0 -3179
  183. package/src/viem/Formatters.ts +0 -164
  184. package/src/viem/P256.ts +0 -1
  185. package/src/viem/Secp256k1.ts +0 -1
  186. package/src/viem/Storage.ts +0 -110
  187. package/src/viem/TokenIds.ts +0 -1
  188. package/src/viem/Transaction.ts +0 -382
  189. package/src/viem/Transport.ts +0 -191
  190. package/src/viem/WebAuthnP256.ts +0 -146
  191. package/src/viem/WebCryptoP256.ts +0 -1
  192. package/src/viem/e2e.test.ts +0 -1602
  193. package/src/viem/index.ts +0 -30
  194. package/src/viem/internal/types.ts +0 -69
  195. package/src/viem/internal/utils.ts +0 -58
  196. package/src/wagmi/internal/types.ts +0 -16
@@ -1,601 +0,0 @@
1
- import * as Address from 'ox/Address'
2
- import * as Hex from 'ox/Hex'
3
- import * as P256 from 'ox/P256'
4
- import * as PublicKey from 'ox/PublicKey'
5
- import * as Secp256k1 from 'ox/Secp256k1'
6
- import * as Signature from 'ox/Signature'
7
- import { KeyAuthorization, SignatureEnvelope } from 'ox/tempo'
8
- import * as WebAuthnP256 from 'ox/WebAuthnP256'
9
- import * as WebCryptoP256 from 'ox/WebCryptoP256'
10
- import type { LocalAccount, RequiredBy, Account as viem_Account } from 'viem'
11
- import {
12
- hashAuthorization,
13
- hashMessage,
14
- hashTypedData,
15
- keccak256,
16
- parseAccount,
17
- } from 'viem/utils'
18
- import type { OneOf } from '../internal/types.js'
19
- import * as Storage from './Storage.js'
20
- import * as Transaction from './Transaction.js'
21
-
22
- type StorageSchema = {
23
- pendingKeyAuthorization?: KeyAuthorization.Signed | undefined
24
- }
25
-
26
- export type Account_base<source extends string = string> = RequiredBy<
27
- LocalAccount<source>,
28
- 'sign' | 'signAuthorization'
29
- > & {
30
- /** Key type. */
31
- keyType: SignatureEnvelope.Type
32
- /** Account storage. */
33
- storage: Storage.Storage<StorageSchema>
34
- }
35
-
36
- export type RootAccount = Account_base<'root'> & {
37
- /** Assign key authorization to the next transaction. */
38
- assignKeyAuthorization: (
39
- keyAuthorization: KeyAuthorization.Signed,
40
- ) => Promise<void>
41
- /** Sign key authorization. */
42
- signKeyAuthorization: (
43
- key: Pick<AccessKeyAccount, 'accessKeyAddress' | 'keyType'>,
44
- parameters?: Pick<KeyAuthorization.KeyAuthorization, 'expiry' | 'limits'>,
45
- ) => Promise<KeyAuthorization.Signed>
46
- }
47
-
48
- export type AccessKeyAccount = Account_base<'accessKey'> & {
49
- /** Access key ID. */
50
- accessKeyAddress: Address.Address
51
- }
52
-
53
- export type Account = OneOf<RootAccount | AccessKeyAccount>
54
-
55
- /**
56
- * Instantiates an Account from a headless WebAuthn credential (P256 private key).
57
- *
58
- * @example
59
- * ```ts
60
- * import { Account } from 'tempo.ts/viem'
61
- *
62
- * const account = Account.fromHeadlessWebAuthn('0x...')
63
- * ```
64
- *
65
- * @param privateKey P256 private key.
66
- * @returns Account.
67
- */
68
- export function fromHeadlessWebAuthn<
69
- const options extends fromHeadlessWebAuthn.Options,
70
- >(
71
- privateKey: Hex.Hex,
72
- options: options | fromHeadlessWebAuthn.Options,
73
- ): fromHeadlessWebAuthn.ReturnValue<options> {
74
- const { access, rpId, origin, storage } = options
75
-
76
- const publicKey = P256.getPublicKey({ privateKey })
77
-
78
- return from({
79
- access,
80
- keyType: 'webAuthn',
81
- publicKey,
82
- async sign({ hash }) {
83
- const { metadata, payload } = WebAuthnP256.getSignPayload({
84
- ...options,
85
- challenge: hash,
86
- rpId,
87
- origin,
88
- })
89
- const signature = P256.sign({
90
- payload,
91
- privateKey,
92
- hash: true,
93
- })
94
- return SignatureEnvelope.serialize({
95
- metadata,
96
- signature,
97
- publicKey,
98
- type: 'webAuthn',
99
- })
100
- },
101
- storage,
102
- }) as never
103
- }
104
-
105
- export declare namespace fromHeadlessWebAuthn {
106
- export type Options = Omit<
107
- WebAuthnP256.getSignPayload.Options,
108
- 'challenge' | 'rpId' | 'origin'
109
- > &
110
- Pick<from.Parameters, 'access' | 'storage'> & {
111
- rpId: string
112
- origin: string
113
- }
114
-
115
- export type ReturnValue<options extends Options = Options> =
116
- from.ReturnValue<options>
117
- }
118
-
119
- /**
120
- * Instantiates an Account from a P256 private key.
121
- *
122
- * @example
123
- * ```ts
124
- * import { Account } from 'tempo.ts/viem'
125
- *
126
- * const account = Account.fromP256('0x...')
127
- * ```
128
- *
129
- * @param privateKey P256 private key.
130
- * @returns Account.
131
- */
132
- export function fromP256<const options extends fromP256.Options>(
133
- privateKey: Hex.Hex,
134
- options: options | fromP256.Options = {},
135
- ): fromP256.ReturnValue<options> {
136
- const { access, storage } = options
137
- const publicKey = P256.getPublicKey({ privateKey })
138
-
139
- return from({
140
- access,
141
- keyType: 'p256',
142
- publicKey,
143
- async sign({ hash }) {
144
- const signature = P256.sign({ payload: hash, privateKey })
145
- return SignatureEnvelope.serialize({
146
- signature,
147
- publicKey,
148
- type: 'p256',
149
- })
150
- },
151
- storage,
152
- }) as never
153
- }
154
-
155
- export declare namespace fromP256 {
156
- export type Options = Pick<from.Parameters, 'access' | 'storage'>
157
-
158
- export type ReturnValue<options extends Options = Options> =
159
- from.ReturnValue<options>
160
- }
161
-
162
- /**
163
- * Instantiates an Account from a Secp256k1 private key.
164
- *
165
- * @example
166
- * ```ts
167
- * import { Account } from 'tempo.ts/viem'
168
- *
169
- * const account = Account.fromSecp256k1('0x...')
170
- * ```
171
- *
172
- * @param privateKey Secp256k1 private key.
173
- * @returns Account.
174
- */
175
- // TODO: this function will be redundant when Viem migrates to Ox.
176
- export function fromSecp256k1<const options extends fromSecp256k1.Options>(
177
- privateKey: Hex.Hex,
178
- options: options | fromSecp256k1.Options = {},
179
- ): fromSecp256k1.ReturnValue<options> {
180
- const { access, storage } = options
181
- const publicKey = Secp256k1.getPublicKey({ privateKey })
182
-
183
- return from({
184
- access,
185
- keyType: 'secp256k1',
186
- publicKey,
187
- async sign(parameters) {
188
- const { hash } = parameters
189
- const signature = Secp256k1.sign({ payload: hash, privateKey })
190
- return Signature.toHex(signature)
191
- },
192
- storage,
193
- }) as never
194
- }
195
-
196
- export declare namespace fromSecp256k1 {
197
- export type Options = Pick<from.Parameters, 'access' | 'storage'>
198
-
199
- export type ReturnValue<options extends Options = Options> =
200
- from.ReturnValue<options>
201
- }
202
-
203
- /**
204
- * Instantiates an Account from a WebAuthn credential.
205
- *
206
- * @example
207
- *
208
- * ### Create Passkey + Instantiate Account
209
- *
210
- * Create a credential with `WebAuthnP256.createCredential` and then instantiate
211
- * a Viem Account with `Account.fromWebAuthnP256`.
212
- *
213
- * It is highly recommended to store the credential's public key in an external store
214
- * for future use (ie. for future calls to `WebAuthnP256.getCredential`).
215
- *
216
- * ```ts
217
- * import { Account, WebAuthnP256 } from 'tempo.ts/viem'
218
- * import { publicKeyStore } from './store'
219
- *
220
- * // 1. Create credential
221
- * const credential = await WebAuthnP256.createCredential({ name: 'Example' })
222
- *
223
- * // 2. Instantiate account
224
- * const account = Account.fromWebAuthnP256(credential)
225
- *
226
- * // 3. Store public key
227
- * await publicKeyStore.set(credential.id, credential.publicKey)
228
- *
229
- * ```
230
- *
231
- * @example
232
- *
233
- * ### Get Credential + Instantiate Account
234
- *
235
- * Gets a credential from `WebAuthnP256.getCredential` and then instantiates
236
- * an account with `Account.fromWebAuthnP256`.
237
- *
238
- * The `getPublicKey` function is required to fetch the public key paired with the credential
239
- * from an external store. The public key is required to derive the account's address.
240
- *
241
- * ```ts
242
- * import { Account, WebAuthnP256 } from 'tempo.ts/viem'
243
- * import { publicKeyStore } from './store'
244
- *
245
- * // 1. Get credential
246
- * const credential = await WebAuthnP256.getCredential({
247
- * async getPublicKey(credential) {
248
- * // 2. Get public key from external store.
249
- * return await publicKeyStore.get(credential.id)
250
- * }
251
- * })
252
- *
253
- * // 3. Instantiate account
254
- * const account = Account.fromWebAuthnP256(credential)
255
- * ```
256
- *
257
- * @param credential WebAuthnP256 credential.
258
- * @returns Account.
259
- */
260
- export function fromWebAuthnP256(
261
- credential: fromWebAuthnP256.Credential,
262
- options: fromWebAuthnP256.Options = {},
263
- ): fromWebAuthnP256.ReturnValue {
264
- const { id } = credential
265
- const { storage } = options
266
- const publicKey = PublicKey.fromHex(credential.publicKey)
267
- return from({
268
- keyType: 'webAuthn',
269
- publicKey,
270
- async sign({ hash }) {
271
- const { metadata, signature } = await WebAuthnP256.sign({
272
- ...options,
273
- challenge: hash,
274
- credentialId: id,
275
- })
276
- return SignatureEnvelope.serialize({
277
- publicKey,
278
- metadata,
279
- signature,
280
- type: 'webAuthn',
281
- })
282
- },
283
- storage,
284
- })
285
- }
286
-
287
- export declare namespace fromWebAuthnP256 {
288
- export type Credential = {
289
- id: WebAuthnP256.P256Credential['id']
290
- publicKey: Hex.Hex
291
- }
292
-
293
- export type Options = {
294
- getFn?: WebAuthnP256.sign.Options['getFn'] | undefined
295
- rpId?: WebAuthnP256.sign.Options['rpId'] | undefined
296
- storage?: from.Parameters['storage'] | undefined
297
- }
298
-
299
- export type ReturnValue = from.ReturnValue
300
- }
301
-
302
- /**
303
- * Instantiates an Account from a P256 private key.
304
- *
305
- * @example
306
- * ```ts
307
- * import { Account } from 'tempo.ts/viem'
308
- * import { WebCryptoP256 } from 'ox'
309
- *
310
- * const keyPair = await WebCryptoP256.createKeyPair()
311
- *
312
- * const account = Account.fromWebCryptoP256(keyPair)
313
- * ```
314
- *
315
- * @param keyPair WebCryptoP256 key pair.
316
- * @returns Account.
317
- */
318
- export function fromWebCryptoP256<
319
- const options extends fromWebCryptoP256.Options,
320
- >(
321
- keyPair: Awaited<ReturnType<typeof WebCryptoP256.createKeyPair>>,
322
- options: options | fromWebCryptoP256.Options = {},
323
- ): fromWebCryptoP256.ReturnValue<options> {
324
- const { access, storage } = options
325
- const { publicKey, privateKey } = keyPair
326
-
327
- return from({
328
- access,
329
- keyType: 'p256',
330
- publicKey,
331
- async sign({ hash }) {
332
- const signature = await WebCryptoP256.sign({ payload: hash, privateKey })
333
- return SignatureEnvelope.serialize({
334
- signature,
335
- prehash: true,
336
- publicKey,
337
- type: 'p256',
338
- })
339
- },
340
- storage,
341
- }) as never
342
- }
343
-
344
- export declare namespace fromWebCryptoP256 {
345
- export type Options = Pick<from.Parameters, 'access' | 'storage'>
346
-
347
- export type ReturnValue<options extends Options = Options> =
348
- from.ReturnValue<options>
349
- }
350
-
351
- export async function signKeyAuthorization(
352
- account: LocalAccount,
353
- parameters: signKeyAuthorization.Parameters,
354
- ): Promise<signKeyAuthorization.ReturnValue> {
355
- const { key, expiry, limits } = parameters
356
- const { accessKeyAddress, keyType: type } = key
357
-
358
- const signature = await account.sign!({
359
- hash: KeyAuthorization.getSignPayload({
360
- address: accessKeyAddress,
361
- expiry,
362
- limits,
363
- type,
364
- }),
365
- })
366
- return KeyAuthorization.from({
367
- address: accessKeyAddress,
368
- expiry,
369
- limits,
370
- signature: SignatureEnvelope.from(signature),
371
- type,
372
- })
373
- }
374
-
375
- export declare namespace signKeyAuthorization {
376
- type Parameters = Pick<
377
- KeyAuthorization.KeyAuthorization,
378
- 'expiry' | 'limits'
379
- > & {
380
- key: Pick<AccessKeyAccount, 'accessKeyAddress' | 'keyType'>
381
- }
382
-
383
- type ReturnValue = KeyAuthorization.Signed
384
- }
385
-
386
- /** @internal */
387
- // biome-ignore lint/correctness/noUnusedVariables: _
388
- function fromBase(parameters: fromBase.Parameters): Account_base {
389
- const {
390
- keyType = 'secp256k1',
391
- parentAddress,
392
- source = 'privateKey',
393
- } = parameters
394
-
395
- const address = parentAddress ?? Address.fromPublicKey(parameters.publicKey)
396
- const publicKey = PublicKey.toHex(parameters.publicKey, {
397
- includePrefix: false,
398
- })
399
-
400
- const storage = Storage.from<StorageSchema>(
401
- parameters.storage ?? Storage.memory(),
402
- { key: `tempo.ts:${address.toLowerCase()}` },
403
- )
404
-
405
- async function sign({ hash }: { hash: Hex.Hex }) {
406
- const signature = await parameters.sign({ hash })
407
- if (parentAddress)
408
- return SignatureEnvelope.serialize(
409
- SignatureEnvelope.from({
410
- userAddress: parentAddress,
411
- inner: SignatureEnvelope.from(signature),
412
- type: 'keychain',
413
- }),
414
- )
415
-
416
- return signature
417
- }
418
-
419
- return {
420
- address: Address.checksum(address),
421
- keyType,
422
- sign,
423
- async signAuthorization(parameters) {
424
- const { chainId, nonce } = parameters
425
- const address = parameters.contractAddress ?? parameters.address
426
- const signature = await sign({
427
- hash: hashAuthorization({ address, chainId, nonce }),
428
- })
429
- const envelope = SignatureEnvelope.from(signature)
430
- if (envelope.type !== 'secp256k1')
431
- throw new Error(
432
- 'Unsupported signature type. Expected `secp256k1` but got `' +
433
- envelope.type +
434
- '`.',
435
- )
436
- const { r, s, yParity } = envelope.signature
437
- return {
438
- address,
439
- chainId,
440
- nonce,
441
- r: Hex.fromNumber(r, { size: 32 }),
442
- s: Hex.fromNumber(s, { size: 32 }),
443
- yParity,
444
- }
445
- },
446
- async signMessage(parameters) {
447
- const { message } = parameters
448
- const signature = await sign({ hash: hashMessage(message) })
449
- const envelope = SignatureEnvelope.from(signature)
450
- return SignatureEnvelope.serialize(envelope)
451
- },
452
- async signTransaction(transaction, options) {
453
- const { serializer = Transaction.serialize } = options ?? {}
454
-
455
- const keyAuthorization =
456
- (await storage?.getItem('pendingKeyAuthorization')) ?? undefined
457
- if (keyAuthorization && !(transaction as any).keyAuthorization) {
458
- ;(transaction as any).keyAuthorization = keyAuthorization
459
- await storage.removeItem('pendingKeyAuthorization')
460
- }
461
-
462
- const signature = await sign({
463
- hash: keccak256(await serializer(transaction)),
464
- })
465
- const envelope = SignatureEnvelope.from(signature)
466
- return await serializer(transaction, envelope as never)
467
- },
468
- async signTypedData(typedData) {
469
- const signature = await sign({ hash: hashTypedData(typedData) })
470
- const envelope = SignatureEnvelope.from(signature)
471
- return SignatureEnvelope.serialize(envelope)
472
- },
473
- publicKey,
474
- source,
475
- storage,
476
- type: 'local',
477
- }
478
- }
479
-
480
- declare namespace fromBase {
481
- export type Parameters = {
482
- /** Parent address. */
483
- parentAddress?: Address.Address | undefined
484
- /** Public key. */
485
- publicKey: PublicKey.PublicKey
486
- /** Key type. */
487
- keyType?: SignatureEnvelope.Type | undefined
488
- /** Sign function. */
489
- sign: NonNullable<LocalAccount['sign']>
490
- /** Source. */
491
- source?: string | undefined
492
- /**
493
- * Account storage.
494
- * Used for access key management, and pending key authorizations.
495
- * @default `Storage.memory()`
496
- */
497
- storage?: Storage.Storage | undefined
498
- }
499
-
500
- export type ReturnValue = Account_base
501
- }
502
-
503
- /** @internal */
504
- // biome-ignore lint/correctness/noUnusedVariables: _
505
- function fromRoot(parameters: fromRoot.Parameters): RootAccount {
506
- const account = fromBase(parameters)
507
- return {
508
- ...account,
509
- source: 'root',
510
- async assignKeyAuthorization(keyAuthorization) {
511
- account.storage.setItem('pendingKeyAuthorization', keyAuthorization)
512
- },
513
- async signKeyAuthorization(key, parameters = {}) {
514
- const { expiry, limits } = parameters
515
- const { accessKeyAddress, keyType: type } = key
516
-
517
- const signature = await account.sign({
518
- hash: KeyAuthorization.getSignPayload({
519
- address: accessKeyAddress,
520
- expiry,
521
- limits,
522
- type,
523
- }),
524
- })
525
- const keyAuthorization = KeyAuthorization.from({
526
- address: accessKeyAddress,
527
- expiry,
528
- limits,
529
- signature: SignatureEnvelope.from(signature),
530
- type,
531
- })
532
- return keyAuthorization
533
- },
534
- }
535
- }
536
-
537
- declare namespace fromRoot {
538
- export type Parameters = fromBase.Parameters
539
-
540
- export type ReturnValue = RootAccount
541
- }
542
-
543
- // biome-ignore lint/correctness/noUnusedVariables: _
544
- function fromAccessKey(parameters: fromAccessKey.Parameters): AccessKeyAccount {
545
- const { access } = parameters
546
- const { address: parentAddress } = parseAccount(access)
547
- const account = fromBase({ ...parameters, parentAddress })
548
- return {
549
- ...account,
550
- accessKeyAddress: Address.fromPublicKey(parameters.publicKey),
551
- source: 'accessKey',
552
- }
553
- }
554
-
555
- declare namespace fromAccessKey {
556
- export type Parameters = fromBase.Parameters & {
557
- /**
558
- * Parent account to access.
559
- * If defined, this account will act as an "access key", and use
560
- * the parent account's address as the keychain address.
561
- */
562
- access: viem_Account | Address.Address
563
- }
564
-
565
- export type ReturnValue = AccessKeyAccount
566
- }
567
-
568
- // biome-ignore lint/correctness/noUnusedVariables: _
569
- function from<const parameters extends from.Parameters>(
570
- parameters: parameters | from.Parameters,
571
- ): from.ReturnValue<parameters> {
572
- const { access } = parameters
573
- if (access) return fromAccessKey(parameters) as never
574
- return fromRoot(parameters) as never
575
- }
576
-
577
- declare namespace from {
578
- export type Parameters = OneOf<fromRoot.Parameters | fromAccessKey.Parameters>
579
-
580
- export type ReturnValue<
581
- parameters extends {
582
- access?: fromAccessKey.Parameters['access'] | undefined
583
- } = {
584
- access?: fromAccessKey.Parameters['access'] | undefined
585
- },
586
- > = parameters extends {
587
- access: fromAccessKey.Parameters['access']
588
- }
589
- ? AccessKeyAccount
590
- : RootAccount
591
- }
592
-
593
- // Export types required for inference.
594
- export {
595
- /** @deprecated */
596
- KeyAuthorization as z_KeyAuthorization,
597
- /** @deprecated */
598
- SignatureEnvelope as z_SignatureEnvelope,
599
- /** @deprecated */
600
- TxEnvelopeTempo as z_TxEnvelopeTempo,
601
- } from 'ox/tempo'