tempo.ts 0.1.5 → 0.2.1

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 (297) hide show
  1. package/README.md +33 -2
  2. package/dist/chains.d.ts +509 -115
  3. package/dist/chains.d.ts.map +1 -1
  4. package/dist/chains.js +20 -9
  5. package/dist/chains.js.map +1 -1
  6. package/dist/ox/Order.d.ts +92 -0
  7. package/dist/ox/Order.d.ts.map +1 -0
  8. package/dist/ox/Order.js +88 -0
  9. package/dist/ox/Order.js.map +1 -0
  10. package/dist/ox/OrdersFilters.d.ts +72 -0
  11. package/dist/ox/OrdersFilters.d.ts.map +1 -0
  12. package/dist/ox/OrdersFilters.js +100 -0
  13. package/dist/ox/OrdersFilters.js.map +1 -0
  14. package/dist/ox/Pagination.d.ts +128 -0
  15. package/dist/ox/Pagination.d.ts.map +1 -0
  16. package/dist/ox/Pagination.js +78 -0
  17. package/dist/ox/Pagination.js.map +1 -0
  18. package/dist/ox/PoolId.d.ts +18 -0
  19. package/dist/ox/PoolId.d.ts.map +1 -0
  20. package/dist/ox/PoolId.js +13 -0
  21. package/dist/ox/PoolId.js.map +1 -0
  22. package/dist/ox/RpcSchema.d.ts +32 -0
  23. package/dist/ox/RpcSchema.d.ts.map +1 -0
  24. package/dist/ox/RpcSchema.js +2 -0
  25. package/dist/ox/RpcSchema.js.map +1 -0
  26. package/dist/ox/SignatureEnvelope.d.ts +1 -1
  27. package/dist/ox/SignatureEnvelope.d.ts.map +1 -1
  28. package/dist/ox/SignatureEnvelope.js.map +1 -1
  29. package/dist/{viem → ox}/Tick.d.ts +4 -0
  30. package/dist/ox/Tick.d.ts.map +1 -0
  31. package/dist/ox/Tick.js.map +1 -0
  32. package/dist/ox/Transaction.d.ts.map +1 -1
  33. package/dist/ox/Transaction.js +2 -1
  34. package/dist/ox/Transaction.js.map +1 -1
  35. package/dist/ox/TransactionEnvelopeAA.d.ts +6 -6
  36. package/dist/ox/TransactionEnvelopeAA.d.ts.map +1 -1
  37. package/dist/ox/TransactionEnvelopeAA.js +4 -2
  38. package/dist/ox/TransactionEnvelopeAA.js.map +1 -1
  39. package/dist/ox/TransactionRequest.d.ts +4 -0
  40. package/dist/ox/TransactionRequest.d.ts.map +1 -1
  41. package/dist/ox/TransactionRequest.js.map +1 -1
  42. package/dist/ox/index.d.ts +6 -0
  43. package/dist/ox/index.d.ts.map +1 -1
  44. package/dist/ox/index.js +6 -0
  45. package/dist/ox/index.js.map +1 -1
  46. package/dist/prool/Instance.d.ts.map +1 -1
  47. package/dist/prool/Instance.js +20 -4
  48. package/dist/prool/Instance.js.map +1 -1
  49. package/dist/viem/Abis.d.ts +1469 -1082
  50. package/dist/viem/Abis.d.ts.map +1 -1
  51. package/dist/viem/Abis.js +932 -671
  52. package/dist/viem/Abis.js.map +1 -1
  53. package/dist/viem/Account.d.ts +150 -0
  54. package/dist/viem/Account.d.ts.map +1 -0
  55. package/dist/viem/Account.js +221 -0
  56. package/dist/viem/Account.js.map +1 -0
  57. package/dist/viem/Actions/amm.d.ts +144 -161
  58. package/dist/viem/Actions/amm.d.ts.map +1 -1
  59. package/dist/viem/Actions/amm.js +109 -163
  60. package/dist/viem/Actions/amm.js.map +1 -1
  61. package/dist/viem/Actions/dex.d.ts +920 -664
  62. package/dist/viem/Actions/dex.d.ts.map +1 -1
  63. package/dist/viem/Actions/dex.js +129 -30
  64. package/dist/viem/Actions/dex.js.map +1 -1
  65. package/dist/viem/Actions/faucet.d.ts +34 -0
  66. package/dist/viem/Actions/faucet.d.ts.map +1 -0
  67. package/dist/viem/Actions/faucet.js +33 -0
  68. package/dist/viem/Actions/faucet.js.map +1 -0
  69. package/dist/viem/Actions/fee.d.ts +16 -30
  70. package/dist/viem/Actions/fee.d.ts.map +1 -1
  71. package/dist/viem/Actions/fee.js +13 -13
  72. package/dist/viem/Actions/fee.js.map +1 -1
  73. package/dist/viem/Actions/index.d.ts +2 -0
  74. package/dist/viem/Actions/index.d.ts.map +1 -1
  75. package/dist/viem/Actions/index.js +2 -0
  76. package/dist/viem/Actions/index.js.map +1 -1
  77. package/dist/viem/Actions/policy.d.ts +46 -46
  78. package/dist/viem/Actions/policy.js +46 -46
  79. package/dist/viem/Actions/reward.d.ts +3236 -0
  80. package/dist/viem/Actions/reward.d.ts.map +1 -0
  81. package/dist/viem/Actions/reward.js +725 -0
  82. package/dist/viem/Actions/reward.js.map +1 -0
  83. package/dist/viem/Actions/token.d.ts +4399 -2750
  84. package/dist/viem/Actions/token.d.ts.map +1 -1
  85. package/dist/viem/Actions/token.js +361 -482
  86. package/dist/viem/Actions/token.js.map +1 -1
  87. package/dist/viem/Addresses.d.ts +1 -2
  88. package/dist/viem/Addresses.d.ts.map +1 -1
  89. package/dist/viem/Addresses.js +1 -2
  90. package/dist/viem/Addresses.js.map +1 -1
  91. package/dist/viem/Chain.d.ts +38 -12
  92. package/dist/viem/Chain.d.ts.map +1 -1
  93. package/dist/viem/Chain.js +27 -18
  94. package/dist/viem/Chain.js.map +1 -1
  95. package/dist/viem/Decorator.d.ts +1009 -428
  96. package/dist/viem/Decorator.d.ts.map +1 -1
  97. package/dist/viem/Decorator.js +17 -5
  98. package/dist/viem/Decorator.js.map +1 -1
  99. package/dist/viem/Formatters.d.ts +8 -1
  100. package/dist/viem/Formatters.d.ts.map +1 -1
  101. package/dist/viem/Formatters.js +17 -0
  102. package/dist/viem/Formatters.js.map +1 -1
  103. package/dist/viem/P256.d.ts +2 -0
  104. package/dist/viem/P256.d.ts.map +1 -0
  105. package/dist/viem/P256.js +2 -0
  106. package/dist/viem/P256.js.map +1 -0
  107. package/dist/viem/Secp256k1.d.ts +2 -0
  108. package/dist/viem/Secp256k1.d.ts.map +1 -0
  109. package/dist/viem/Secp256k1.js +2 -0
  110. package/dist/viem/Secp256k1.js.map +1 -0
  111. package/dist/viem/TokenIds.d.ts +1 -2
  112. package/dist/viem/TokenIds.d.ts.map +1 -1
  113. package/dist/viem/TokenIds.js +1 -2
  114. package/dist/viem/TokenIds.js.map +1 -1
  115. package/dist/viem/Transaction.d.ts +1 -1
  116. package/dist/viem/Transaction.d.ts.map +1 -1
  117. package/dist/viem/Transaction.js +46 -5
  118. package/dist/viem/Transaction.js.map +1 -1
  119. package/dist/viem/WebAuthnP256.d.ts +79 -0
  120. package/dist/viem/WebAuthnP256.d.ts.map +1 -0
  121. package/dist/viem/WebAuthnP256.js +95 -0
  122. package/dist/viem/WebAuthnP256.js.map +1 -0
  123. package/dist/viem/WebCryptoP256.d.ts +2 -0
  124. package/dist/viem/WebCryptoP256.d.ts.map +1 -0
  125. package/dist/viem/WebCryptoP256.js +2 -0
  126. package/dist/viem/WebCryptoP256.js.map +1 -0
  127. package/dist/viem/index.d.ts +6 -3
  128. package/dist/viem/index.d.ts.map +1 -1
  129. package/dist/viem/index.js +6 -3
  130. package/dist/viem/index.js.map +1 -1
  131. package/dist/viem/internal/account.d.ts +24 -0
  132. package/dist/viem/internal/account.d.ts.map +1 -0
  133. package/dist/viem/internal/account.js +68 -0
  134. package/dist/viem/internal/account.js.map +1 -0
  135. package/dist/viem/internal/types.d.ts +10 -0
  136. package/dist/viem/internal/types.d.ts.map +1 -1
  137. package/dist/wagmi/Actions/amm.d.ts +428 -0
  138. package/dist/wagmi/Actions/amm.d.ts.map +1 -0
  139. package/dist/wagmi/Actions/amm.js +472 -0
  140. package/dist/wagmi/Actions/amm.js.map +1 -0
  141. package/dist/wagmi/Actions/dex.d.ts +908 -0
  142. package/dist/wagmi/Actions/dex.d.ts.map +1 -0
  143. package/dist/wagmi/Actions/dex.js +1023 -0
  144. package/dist/wagmi/Actions/dex.js.map +1 -0
  145. package/dist/wagmi/Actions/faucet.d.ts +35 -0
  146. package/dist/wagmi/Actions/faucet.d.ts.map +1 -0
  147. package/dist/wagmi/Actions/faucet.js +33 -0
  148. package/dist/wagmi/Actions/faucet.js.map +1 -0
  149. package/dist/wagmi/Actions/fee.d.ts +111 -0
  150. package/dist/wagmi/Actions/fee.d.ts.map +1 -0
  151. package/dist/wagmi/Actions/fee.js +126 -0
  152. package/dist/wagmi/Actions/fee.js.map +1 -0
  153. package/dist/wagmi/Actions/index.d.ts +7 -0
  154. package/dist/wagmi/Actions/index.d.ts.map +1 -0
  155. package/dist/wagmi/Actions/index.js +7 -0
  156. package/dist/wagmi/Actions/index.js.map +1 -0
  157. package/dist/wagmi/Actions/reward.d.ts +348 -0
  158. package/dist/wagmi/Actions/reward.d.ts.map +1 -0
  159. package/dist/wagmi/Actions/reward.js +388 -0
  160. package/dist/wagmi/Actions/reward.js.map +1 -0
  161. package/dist/wagmi/Actions/token.d.ts +1546 -0
  162. package/dist/wagmi/Actions/token.d.ts.map +1 -0
  163. package/dist/wagmi/Actions/token.js +1712 -0
  164. package/dist/wagmi/Actions/token.js.map +1 -0
  165. package/dist/wagmi/Connector.d.ts +81 -0
  166. package/dist/wagmi/Connector.d.ts.map +1 -0
  167. package/dist/wagmi/Connector.js +261 -0
  168. package/dist/wagmi/Connector.js.map +1 -0
  169. package/dist/wagmi/Hooks/amm.d.ts +421 -0
  170. package/dist/wagmi/Hooks/amm.d.ts.map +1 -0
  171. package/dist/wagmi/Hooks/amm.js +504 -0
  172. package/dist/wagmi/Hooks/amm.js.map +1 -0
  173. package/dist/wagmi/Hooks/dex.d.ts +816 -0
  174. package/dist/wagmi/Hooks/dex.d.ts.map +1 -0
  175. package/dist/wagmi/Hooks/dex.js +973 -0
  176. package/dist/wagmi/Hooks/dex.js.map +1 -0
  177. package/dist/wagmi/Hooks/faucet.d.ts +39 -0
  178. package/dist/wagmi/Hooks/faucet.d.ts.map +1 -0
  179. package/dist/wagmi/Hooks/faucet.js +40 -0
  180. package/dist/wagmi/Hooks/faucet.js.map +1 -0
  181. package/dist/wagmi/Hooks/fee.d.ts +97 -0
  182. package/dist/wagmi/Hooks/fee.d.ts.map +1 -0
  183. package/dist/wagmi/Hooks/fee.js +109 -0
  184. package/dist/wagmi/Hooks/fee.js.map +1 -0
  185. package/dist/wagmi/Hooks/index.d.ts +7 -0
  186. package/dist/wagmi/Hooks/index.d.ts.map +1 -0
  187. package/dist/wagmi/Hooks/index.js +7 -0
  188. package/dist/wagmi/Hooks/index.js.map +1 -0
  189. package/dist/wagmi/Hooks/reward.d.ts +307 -0
  190. package/dist/wagmi/Hooks/reward.d.ts.map +1 -0
  191. package/dist/wagmi/Hooks/reward.js +349 -0
  192. package/dist/wagmi/Hooks/reward.js.map +1 -0
  193. package/dist/wagmi/Hooks/token.d.ts +1388 -0
  194. package/dist/wagmi/Hooks/token.d.ts.map +1 -0
  195. package/dist/wagmi/Hooks/token.js +1657 -0
  196. package/dist/wagmi/Hooks/token.js.map +1 -0
  197. package/dist/wagmi/index.d.ts +4 -0
  198. package/dist/wagmi/index.d.ts.map +1 -0
  199. package/dist/wagmi/index.js +4 -0
  200. package/dist/wagmi/index.js.map +1 -0
  201. package/package.json +54 -10
  202. package/src/chains.ts +21 -9
  203. package/src/ox/Order.test.ts +78 -0
  204. package/src/ox/Order.ts +125 -0
  205. package/src/ox/OrdersFilters.test.ts +182 -0
  206. package/src/ox/OrdersFilters.ts +125 -0
  207. package/src/ox/Pagination.test.ts +162 -0
  208. package/src/ox/Pagination.ts +164 -0
  209. package/src/ox/PoolId.test.ts +33 -0
  210. package/src/ox/PoolId.ts +27 -0
  211. package/src/ox/RpcSchema.ts +35 -0
  212. package/src/ox/SignatureEnvelope.ts +3 -1
  213. package/src/{viem → ox}/Tick.test.ts +1 -1
  214. package/src/{viem → ox}/Tick.ts +5 -0
  215. package/src/ox/Transaction.test.ts +1 -1
  216. package/src/ox/Transaction.ts +2 -1
  217. package/src/ox/TransactionEnvelopeAA.test.ts +239 -96
  218. package/src/ox/TransactionEnvelopeAA.ts +9 -7
  219. package/src/ox/TransactionRequest.ts +4 -0
  220. package/src/ox/index.ts +6 -0
  221. package/src/prool/Instance.ts +51 -37
  222. package/src/prool/internal/chain.json +104 -52
  223. package/src/tsconfig.json +9 -0
  224. package/src/viem/Abis.ts +972 -710
  225. package/src/viem/Account.ts +279 -0
  226. package/src/viem/Actions/__snapshots__/dex.test.ts.snap +850 -0
  227. package/src/viem/Actions/amm.test.ts +173 -169
  228. package/src/viem/Actions/amm.ts +131 -203
  229. package/src/viem/Actions/dex.test.ts +563 -484
  230. package/src/viem/Actions/dex.ts +203 -30
  231. package/src/viem/Actions/faucet.ts +50 -0
  232. package/src/viem/Actions/fee.test.ts +23 -34
  233. package/src/viem/Actions/fee.ts +20 -13
  234. package/src/viem/Actions/index.ts +2 -0
  235. package/src/viem/Actions/policy.test.ts +19 -33
  236. package/src/viem/Actions/policy.ts +46 -46
  237. package/src/viem/Actions/reward.test.ts +457 -0
  238. package/src/viem/Actions/reward.ts +999 -0
  239. package/src/viem/Actions/token.test.ts +453 -287
  240. package/src/viem/Actions/token.ts +605 -693
  241. package/src/viem/Addresses.ts +1 -2
  242. package/src/viem/Chain.bench-d.ts +12 -0
  243. package/src/viem/Chain.ts +70 -20
  244. package/src/viem/Decorator.bench-d.ts +1 -1
  245. package/src/viem/Decorator.test.ts +3 -1
  246. package/src/viem/Decorator.ts +1049 -442
  247. package/src/viem/Formatters.ts +31 -5
  248. package/src/viem/P256.ts +1 -0
  249. package/src/viem/Secp256k1.ts +1 -0
  250. package/src/viem/TokenIds.ts +1 -2
  251. package/src/viem/Transaction.ts +53 -7
  252. package/src/viem/WebAuthnP256.ts +140 -0
  253. package/src/viem/WebCryptoP256.ts +1 -0
  254. package/src/viem/e2e.test.ts +1126 -297
  255. package/src/viem/index.ts +6 -3
  256. package/src/viem/internal/account.ts +107 -0
  257. package/src/viem/internal/types.ts +9 -0
  258. package/src/wagmi/Actions/__snapshots__/dex.test.ts.snap +310 -0
  259. package/src/wagmi/Actions/amm.test.ts +198 -0
  260. package/src/wagmi/Actions/amm.ts +691 -0
  261. package/src/wagmi/Actions/dex.test.ts +1507 -0
  262. package/src/wagmi/Actions/dex.ts +1640 -0
  263. package/src/wagmi/Actions/faucet.ts +46 -0
  264. package/src/wagmi/Actions/fee.test.ts +63 -0
  265. package/src/wagmi/Actions/fee.ts +208 -0
  266. package/src/wagmi/Actions/index.ts +6 -0
  267. package/src/wagmi/Actions/reward.test.ts +210 -0
  268. package/src/wagmi/Actions/reward.ts +632 -0
  269. package/src/wagmi/Actions/token.test.ts +1308 -0
  270. package/src/wagmi/Actions/token.ts +2613 -0
  271. package/src/wagmi/Connector.test.ts +53 -0
  272. package/src/wagmi/Connector.ts +390 -0
  273. package/src/wagmi/Hooks/__snapshots__/dex.test.ts.snap +457 -0
  274. package/src/wagmi/Hooks/amm.test.ts +424 -0
  275. package/src/wagmi/Hooks/amm.ts +806 -0
  276. package/src/wagmi/Hooks/dex.test.ts +1017 -0
  277. package/src/wagmi/Hooks/dex.ts +1685 -0
  278. package/src/wagmi/Hooks/faucet.ts +76 -0
  279. package/src/wagmi/Hooks/fee.test.ts +166 -0
  280. package/src/wagmi/Hooks/fee.ts +206 -0
  281. package/src/wagmi/Hooks/index.ts +6 -0
  282. package/src/wagmi/Hooks/reward.test.ts +219 -0
  283. package/src/wagmi/Hooks/reward.ts +672 -0
  284. package/src/wagmi/Hooks/token.test.ts +1670 -0
  285. package/src/wagmi/Hooks/token.ts +2906 -0
  286. package/src/wagmi/index.ts +3 -0
  287. package/src/wagmi/internal/types.ts +16 -0
  288. package/dist/viem/Client.d.ts +0 -27
  289. package/dist/viem/Client.d.ts.map +0 -1
  290. package/dist/viem/Client.js +0 -28
  291. package/dist/viem/Client.js.map +0 -1
  292. package/dist/viem/Tick.d.ts.map +0 -1
  293. package/dist/viem/Tick.js.map +0 -1
  294. package/src/viem/Client.bench-d.ts +0 -8
  295. package/src/viem/Client.test.ts +0 -178
  296. package/src/viem/Client.ts +0 -91
  297. /package/dist/{viem → ox}/Tick.js +0 -0
@@ -0,0 +1,53 @@
1
+ import { expect, test, vi } from 'vitest'
2
+ import { cdp } from 'vitest/browser'
3
+ import { useAccount, useConnect } from 'wagmi'
4
+ import { renderHook } from '../../test/wagmi/config.js'
5
+ import { webAuthn } from './Connector.js'
6
+
7
+ async function setupWebAuthn() {
8
+ const client = cdp()
9
+ await client.send('WebAuthn.enable')
10
+ const result = await client.send('WebAuthn.addVirtualAuthenticator', {
11
+ options: {
12
+ protocol: 'ctap2',
13
+ transport: 'internal',
14
+ hasResidentKey: true,
15
+ hasUserVerification: true,
16
+ isUserVerified: true,
17
+ automaticPresenceSimulation: true,
18
+ },
19
+ })
20
+ return async () => {
21
+ const client = cdp()
22
+ await client.send('WebAuthn.removeVirtualAuthenticator', {
23
+ authenticatorId: result.authenticatorId,
24
+ })
25
+ await client.send('WebAuthn.disable')
26
+ }
27
+ }
28
+
29
+ test('connect', async (context) => {
30
+ const cleanup = await setupWebAuthn()
31
+ context.onTestFinished(async () => await cleanup())
32
+
33
+ const { result } = await renderHook(() => ({
34
+ useAccount: useAccount(),
35
+ useConnect: useConnect(),
36
+ }))
37
+
38
+ expect(result.current.useAccount.address).not.toBeDefined()
39
+ expect(result.current.useAccount.status).toEqual('disconnected')
40
+
41
+ result.current.useConnect.connect({
42
+ connector: webAuthn(),
43
+ capabilities: { createAccount: { label: 'Test Account' } },
44
+ })
45
+
46
+ await vi.waitFor(() =>
47
+ expect(result.current.useAccount.isConnected).toBeTruthy(),
48
+ )
49
+
50
+ expect(result.current.useAccount.address).toBeDefined()
51
+ expect(result.current.useAccount.address).toMatch(/^0x[a-fA-F0-9]{40}$/)
52
+ expect(result.current.useAccount.status).toEqual('connected')
53
+ })
@@ -0,0 +1,390 @@
1
+ import * as Address from 'ox/Address'
2
+ import * as Bytes from 'ox/Bytes'
3
+ import { Account, WebAuthnP256 } from 'tempo.ts/viem'
4
+ import {
5
+ createClient,
6
+ type EIP1193Provider,
7
+ getAddress,
8
+ type Hex,
9
+ type LocalAccount,
10
+ SwitchChainError,
11
+ } from 'viem'
12
+ import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts'
13
+ import { ChainNotConfiguredError, createConnector } from 'wagmi'
14
+
15
+ /**
16
+ * Connector for a Secp256k1 EOA.
17
+ *
18
+ * WARNING: NOT RECOMMENDED FOR PRODUCTION USAGE.
19
+ * This connector stores private keys in clear text, and are bound to the session
20
+ * length of the storage used.
21
+ *
22
+ * @returns Connector.
23
+ */
24
+ export function dangerous_secp256k1(
25
+ options: dangerous_secp256k1.Parameters = {},
26
+ ) {
27
+ let account: LocalAccount | undefined
28
+
29
+ type Properties = {
30
+ connect<withCapabilities extends boolean = false>(parameters: {
31
+ capabilities?:
32
+ | {
33
+ createAccount?: boolean | undefined
34
+ }
35
+ | undefined
36
+ chainId?: number | undefined
37
+ isReconnecting?: boolean | undefined
38
+ withCapabilities?: withCapabilities | boolean | undefined
39
+ }): Promise<{
40
+ accounts: readonly Address.Address[]
41
+ chainId: number
42
+ }>
43
+ }
44
+ type Provider = Pick<EIP1193Provider, 'request'>
45
+ type StorageItem = {
46
+ 'secp256k1.activeAddress': Address.Address
47
+ 'secp256k1.lastActiveAddress': Address.Address
48
+ [key: `secp256k1.${string}.privateKey`]: Hex
49
+ }
50
+
51
+ return createConnector<Provider, Properties, StorageItem>((config) => ({
52
+ id: 'secp256k1',
53
+ name: 'EOA (Secp256k1)',
54
+ type: 'secp256k1',
55
+ async setup() {
56
+ const address = await config.storage?.getItem('secp256k1.activeAddress')
57
+ const privateKey = await config.storage?.getItem(
58
+ `secp256k1.${address}.privateKey`,
59
+ )
60
+ if (privateKey) account = privateKeyToAccount(privateKey)
61
+ else if (
62
+ address &&
63
+ options.account &&
64
+ Address.isEqual(address, options.account.address)
65
+ )
66
+ account = options.account
67
+ },
68
+ async connect(parameters = {}) {
69
+ const address = await (async () => {
70
+ if (
71
+ 'capabilities' in parameters &&
72
+ parameters.capabilities?.createAccount
73
+ ) {
74
+ const privateKey = generatePrivateKey()
75
+ const account = privateKeyToAccount(privateKey)
76
+ const address = account.address
77
+ await config.storage?.setItem(
78
+ `secp256k1.${address}.privateKey`,
79
+ privateKey,
80
+ )
81
+ await config.storage?.setItem('secp256k1.activeAddress', address)
82
+ await config.storage?.setItem('secp256k1.lastActiveAddress', address)
83
+ return address
84
+ }
85
+
86
+ const address = await config.storage?.getItem(
87
+ 'secp256k1.lastActiveAddress',
88
+ )
89
+ const privateKey = await config.storage?.getItem(
90
+ `secp256k1.${address}.privateKey`,
91
+ )
92
+
93
+ if (privateKey) account = privateKeyToAccount(privateKey)
94
+ else if (options.account) {
95
+ account = options.account
96
+ await config.storage?.setItem(
97
+ 'secp256k1.lastActiveAddress',
98
+ account.address,
99
+ )
100
+ }
101
+
102
+ if (!account) throw new Error('account not found.')
103
+
104
+ await config.storage?.setItem(
105
+ 'secp256k1.activeAddress',
106
+ account.address,
107
+ )
108
+ return account.address
109
+ })()
110
+
111
+ const chainId = parameters.chainId ?? config.chains[0]?.id
112
+ if (!chainId) throw new ChainNotConfiguredError()
113
+
114
+ return {
115
+ accounts: (parameters.withCapabilities
116
+ ? [{ address }]
117
+ : [address]) as never,
118
+ chainId,
119
+ }
120
+ },
121
+ async disconnect() {
122
+ await config.storage?.removeItem('secp256k1.activeAddress')
123
+ account = undefined
124
+ },
125
+ async getAccounts() {
126
+ if (!account) return []
127
+ return [getAddress(account.address)]
128
+ },
129
+ async getChainId() {
130
+ return config.chains[0]?.id!
131
+ },
132
+ async isAuthorized() {
133
+ try {
134
+ const accounts = await this.getAccounts()
135
+ return !!accounts.length
136
+ } catch (error) {
137
+ console.error(
138
+ 'Connector.secp256k1: Failed to check authorization',
139
+ error,
140
+ )
141
+ return false
142
+ }
143
+ },
144
+ async switchChain({ chainId }) {
145
+ const chain = config.chains.find((chain) => chain.id === chainId)
146
+ if (!chain) throw new SwitchChainError(new ChainNotConfiguredError())
147
+ return chain
148
+ },
149
+ onAccountsChanged() {},
150
+ onChainChanged(chain) {
151
+ const chainId = Number(chain)
152
+ config.emitter.emit('change', { chainId })
153
+ },
154
+ async onDisconnect() {
155
+ config.emitter.emit('disconnect')
156
+ account = undefined
157
+ },
158
+ async getClient({ chainId } = {}) {
159
+ const chain =
160
+ config.chains.find((x) => x.id === chainId) ?? config.chains[0]
161
+ if (!chain) throw new ChainNotConfiguredError()
162
+
163
+ const transports = config.transports
164
+ if (!transports) throw new ChainNotConfiguredError()
165
+
166
+ const transport = transports[chain.id]
167
+ if (!transport) throw new ChainNotConfiguredError()
168
+
169
+ return createClient({
170
+ account,
171
+ chain,
172
+ transport,
173
+ })
174
+ },
175
+ async getProvider({ chainId } = {}) {
176
+ const { request } = await this.getClient!({ chainId })
177
+ return { request }
178
+ },
179
+ }))
180
+ }
181
+
182
+ export declare namespace dangerous_secp256k1 {
183
+ export type Parameters = {
184
+ account?: LocalAccount | undefined
185
+ }
186
+ }
187
+
188
+ /**
189
+ * Connector for a WebAuthn EOA.
190
+ *
191
+ * @returns Connector.
192
+ */
193
+ export function webAuthn(options: webAuthn.Parameters = {}) {
194
+ let account: Account.Account | undefined
195
+
196
+ type Properties = {
197
+ connect<withCapabilities extends boolean = false>(parameters: {
198
+ chainId?: number | undefined
199
+ capabilities?:
200
+ | {
201
+ createAccount?:
202
+ | boolean
203
+ | {
204
+ label?: string | undefined
205
+ }
206
+ | undefined
207
+ }
208
+ | undefined
209
+ isReconnecting?: boolean | undefined
210
+ withCapabilities?: withCapabilities | boolean | undefined
211
+ }): Promise<{ accounts: readonly Address.Address[]; chainId: number }>
212
+ }
213
+ type Provider = Pick<EIP1193Provider, 'request'>
214
+ type StorageItem = {
215
+ 'webAuthn.activeCredential': WebAuthnP256.P256Credential
216
+ 'webAuthn.lastActiveCredential': WebAuthnP256.P256Credential
217
+ [key: `webAuthn.${string}.publicKey`]: Hex
218
+ }
219
+
220
+ return createConnector<Provider, Properties, StorageItem>((config) => ({
221
+ id: 'webAuthn',
222
+ name: 'EOA (WebAuthn)',
223
+ type: 'webAuthn',
224
+ async setup() {
225
+ const credential = await config.storage?.getItem(
226
+ 'webAuthn.activeCredential',
227
+ )
228
+ if (!credential) return
229
+ account = Account.fromWebAuthnP256(credential)
230
+ },
231
+ async connect(parameters = {}) {
232
+ account ??= await (async () => {
233
+ let credential: WebAuthnP256.P256Credential | undefined
234
+
235
+ if (
236
+ 'capabilities' in parameters &&
237
+ parameters.capabilities?.createAccount
238
+ ) {
239
+ // Create credential (sign up)
240
+ const createOptions =
241
+ typeof parameters.capabilities?.createAccount === 'boolean'
242
+ ? {}
243
+ : parameters.capabilities?.createAccount
244
+ const challenge = await options.createOptions?.getChallenge?.()
245
+ credential = await WebAuthnP256.createCredential({
246
+ ...(options.createOptions ?? {}),
247
+ challenge: challenge
248
+ ? new Uint8Array(Bytes.fromHex(challenge))
249
+ : undefined,
250
+ label:
251
+ createOptions.label ??
252
+ options.createOptions?.label ??
253
+ `Account ${new Date().toISOString().split('T')[0]}`,
254
+ })
255
+ config.storage?.setItem(
256
+ `webAuthn.${credential.id}.publicKey`,
257
+ credential.publicKey,
258
+ )
259
+ } else {
260
+ // Load credential (log in)
261
+ credential = (await config.storage?.getItem(
262
+ 'webAuthn.activeCredential',
263
+ )) as WebAuthnP256.P256Credential | undefined
264
+
265
+ // If no active credential, load (last active, if present) credential from keychain.
266
+ const lastActiveCredential = await config.storage?.getItem(
267
+ 'webAuthn.lastActiveCredential',
268
+ )
269
+ credential ??= await WebAuthnP256.getCredential({
270
+ ...(options.getOptions ?? {}),
271
+ credentialId: lastActiveCredential?.id,
272
+ async getPublicKey(credential) {
273
+ {
274
+ const publicKey =
275
+ await options.getOptions?.getPublicKey?.(credential)
276
+ if (publicKey) return publicKey
277
+ }
278
+
279
+ {
280
+ const publicKey = await config.storage?.getItem(
281
+ `webAuthn.${credential.id}.publicKey`,
282
+ )
283
+ if (!publicKey) throw new Error('publicKey not found')
284
+ return publicKey as Hex
285
+ }
286
+ },
287
+ })
288
+ }
289
+
290
+ config.storage?.setItem('webAuthn.activeCredential', credential)
291
+ config.storage?.setItem('webAuthn.lastActiveCredential', credential)
292
+ return Account.fromWebAuthnP256(credential)
293
+ })()
294
+
295
+ const address = getAddress(account.address)
296
+
297
+ const chainId = parameters.chainId ?? config.chains[0]?.id
298
+ if (!chainId) throw new ChainNotConfiguredError()
299
+
300
+ return {
301
+ accounts: (parameters.withCapabilities
302
+ ? [{ address }]
303
+ : [address]) as never,
304
+ chainId,
305
+ }
306
+ },
307
+ async disconnect() {
308
+ await config.storage?.removeItem('webAuthn.activeCredential')
309
+ account = undefined
310
+ },
311
+ async getAccounts() {
312
+ if (!account) return []
313
+ return [getAddress(account.address)]
314
+ },
315
+ async getChainId() {
316
+ return config.chains[0]?.id!
317
+ },
318
+ async isAuthorized() {
319
+ try {
320
+ const accounts = await this.getAccounts()
321
+ return !!accounts.length
322
+ } catch (error) {
323
+ console.error(
324
+ 'Connector.webAuthn: Failed to check authorization',
325
+ error,
326
+ )
327
+ return false
328
+ }
329
+ },
330
+ async switchChain({ chainId }) {
331
+ const chain = config.chains.find((chain) => chain.id === chainId)
332
+ if (!chain) throw new SwitchChainError(new ChainNotConfiguredError())
333
+ return chain
334
+ },
335
+ onAccountsChanged() {},
336
+ onChainChanged(chain) {
337
+ const chainId = Number(chain)
338
+ config.emitter.emit('change', { chainId })
339
+ },
340
+ async onDisconnect() {
341
+ config.emitter.emit('disconnect')
342
+ account = undefined
343
+ },
344
+ async getClient({ chainId } = {}) {
345
+ const chain =
346
+ config.chains.find((x) => x.id === chainId) ?? config.chains[0]
347
+ if (!chain) throw new ChainNotConfiguredError()
348
+
349
+ const transports = config.transports
350
+ if (!transports) throw new ChainNotConfiguredError()
351
+
352
+ const transport = transports[chain.id]
353
+ if (!transport) throw new ChainNotConfiguredError()
354
+
355
+ return createClient({
356
+ account,
357
+ chain,
358
+ transport,
359
+ })
360
+ },
361
+ async getProvider({ chainId } = {}) {
362
+ const { request } = await this.getClient!({ chainId })
363
+ return { request }
364
+ },
365
+ }))
366
+ }
367
+
368
+ export declare namespace webAuthn {
369
+ export type Parameters = {
370
+ /** Options for WebAuthn registration. */
371
+ createOptions?:
372
+ | (Pick<
373
+ WebAuthnP256.createCredential.Parameters,
374
+ 'createFn' | 'label' | 'rpId' | 'userId' | 'timeout'
375
+ > & {
376
+ /** Function to fetch a challenge to sign over at registration. */
377
+ getChallenge?: (() => Promise<Hex>) | undefined
378
+ })
379
+ | undefined
380
+ /** Options for WebAuthn authentication. */
381
+ getOptions?:
382
+ | (Pick<WebAuthnP256.getCredential.Parameters, 'getFn' | 'rpId'> & {
383
+ /** Function to fetch the public key for a credential. */
384
+ getPublicKey?:
385
+ | WebAuthnP256.getCredential.Parameters['getPublicKey']
386
+ | undefined
387
+ })
388
+ | undefined
389
+ }
390
+ }