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