-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathuseRootWebAuthnAccount.ts
More file actions
114 lines (96 loc) · 3.48 KB
/
useRootWebAuthnAccount.ts
File metadata and controls
114 lines (96 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
'use client'
import { useQuery } from '@tanstack/react-query'
import type { WebAuthnP256 } from 'viem/tempo'
import { Account } from 'viem/tempo'
import { useConnection } from 'wagmi'
import { config, webAuthnRpId } from '../wagmi.config.ts'
type RootWebAuthnAccount = ReturnType<typeof Account.fromWebAuthnP256>
type RootWebAuthnCredential = WebAuthnP256.P256Credential
type RootWebAuthnAccountProvider = {
getAccount: (options: {
accessKey?: boolean | undefined
address?: `0x${string}` | undefined
signable?: boolean | undefined
}) => RootWebAuthnAccount
request: (args: { method: 'eth_accounts' }) => Promise<readonly `0x${string}`[]>
}
const rootWebAuthnAccountTimeoutMs = 30_000
export function useRootWebAuthnAccount() {
const { address, connector } = useConnection()
return useQuery({
enabled: Boolean(address && connector?.id === 'webAuthn'),
queryKey: ['root-webauthn-account', address],
queryFn: async () => {
if (!address) throw new Error('account address not ready')
if (!connector) throw new Error('connector not ready')
const provider = await connector.getProvider()
if (isRootWebAuthnAccountProvider(provider)) {
await waitForProviderAccount(
provider,
address as `0x${string}`,
rootWebAuthnAccountTimeoutMs,
)
return provider.getAccount({
accessKey: false,
address: address as `0x${string}`,
signable: true,
})
}
const credential = await waitForStoredCredential(
address as `0x${string}`,
rootWebAuthnAccountTimeoutMs,
)
return accountFromCredential(credential)
},
refetchOnReconnect: false,
refetchOnWindowFocus: false,
retry: 2,
retryDelay: 500,
staleTime: Number.POSITIVE_INFINITY,
})
}
function accountFromCredential(credential: RootWebAuthnCredential) {
return Account.fromWebAuthnP256(credential, webAuthnRpId ? { rpId: webAuthnRpId } : undefined)
}
function isRootWebAuthnAccountProvider(value: unknown): value is RootWebAuthnAccountProvider {
return Boolean(
value &&
typeof value === 'object' &&
'getAccount' in value &&
typeof value.getAccount === 'function' &&
'request' in value &&
typeof value.request === 'function',
)
}
async function waitForProviderAccount(
provider: RootWebAuthnAccountProvider,
address: `0x${string}`,
timeoutMs = 5_000,
) {
const deadline = Date.now() + timeoutMs
const normalizedAddress = address.toLowerCase()
while (Date.now() < deadline) {
const accounts = await provider.request({ method: 'eth_accounts' })
if (accounts.some((account) => account.toLowerCase() === normalizedAddress)) return
await new Promise((resolve) => setTimeout(resolve, 100))
}
throw new Error(`webauthn account ${address} not ready`)
}
async function waitForStoredCredential(
address: `0x${string}`,
timeoutMs = 5_000,
): Promise<RootWebAuthnCredential> {
const deadline = Date.now() + timeoutMs
const normalizedAddress = address.toLowerCase()
while (Date.now() < deadline) {
const credential = await config.storage?.getItem('webAuthn.activeCredential')
if (credential) {
const account = accountFromCredential(credential as RootWebAuthnCredential)
if (account.address.toLowerCase() === normalizedAddress) {
return credential as RootWebAuthnCredential
}
}
await new Promise((resolve) => setTimeout(resolve, 100))
}
throw new Error(`webauthn credential for ${address} not ready`)
}