forked from ZyntariHQ/Invoisio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-evm-wallet.ts
More file actions
230 lines (214 loc) · 8.65 KB
/
Copy pathuse-evm-wallet.ts
File metadata and controls
230 lines (214 loc) · 8.65 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import { useCallback, useEffect, useState } from 'react'
import api from '@/lib/api'
export type EvmWalletState = {
address: string | null
connected: boolean
}
// Global store to share wallet state across all hook instances
let globalEvmWalletState: EvmWalletState = { address: null, connected: false }
const subscribers = new Set<(s: EvmWalletState) => void>()
const setGlobalEvmWalletState = (next: EvmWalletState) => {
globalEvmWalletState = next
subscribers.forEach((fn) => fn(globalEvmWalletState))
}
export function useEvmWallet() {
const [evmWallet, setEvmWallet] = useState<EvmWalletState>(globalEvmWalletState)
// Subscribe to global state changes so all components stay in sync
useEffect(() => {
const sub = (s: EvmWalletState) => setEvmWallet(s)
subscribers.add(sub)
return () => {
subscribers.delete(sub)
}
}, [])
useEffect(() => {
if (typeof window === 'undefined') return
const addr = window.localStorage.getItem('evm_wallet_address')
const token = window.localStorage.getItem('evm_auth_token')
if (addr) {
// Only consider connected when a valid auth token exists
setGlobalEvmWalletState({ address: addr, connected: Boolean(token) })
}
}, [])
// Reflect server-side session state on load
useEffect(() => {
let cancelled = false
const checkStatus = async () => {
try {
const token = typeof window !== 'undefined' ? window.localStorage.getItem('evm_auth_token') : null
// Only call status when we actually have an auth token
if (!token) {
return
}
const status = await api.auth.wallet.status()
const addr = (status as any)?.walletAddress || (typeof window !== 'undefined' ? window.localStorage.getItem('evm_wallet_address') : null)
const connected = Boolean((status as any)?.connected ?? addr)
if (!cancelled) {
if (connected && addr) {
if ((status as any)?.token && typeof window !== 'undefined') {
window.localStorage.setItem('evm_auth_token', (status as any).token)
}
if (typeof window !== 'undefined') {
window.localStorage.setItem('evm_wallet_address', addr)
}
setGlobalEvmWalletState({ address: addr, connected: true })
} else {
if (typeof window !== 'undefined') {
window.localStorage.removeItem('evm_wallet_address')
window.localStorage.removeItem('evm_auth_token')
}
setGlobalEvmWalletState({ address: null, connected: false })
}
}
} catch (e) {
// Leave client state as-is on failure
}
}
checkStatus()
return () => { cancelled = true }
}, [])
const connect = useCallback(async () => {
try {
if (!(window as any).ethereum) {
throw new Error('No EVM wallet provider found')
}
const eth = (window as any).ethereum
// Request accounts
const accounts: string[] = await eth.request({ method: 'eth_requestAccounts' })
const addr = accounts?.[0]
if (!addr) throw new Error('No account returned from provider')
// Ensure Base Sepolia network (chainId 84532)
const targetChainIdHex = '0x14A34' // 84532
const currentChainIdHex: string = await eth.request({ method: 'eth_chainId' })
if (currentChainIdHex?.toLowerCase() !== targetChainIdHex.toLowerCase()) {
try {
await eth.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: targetChainIdHex }],
})
} catch (switchErr: any) {
// If chain is not added, add Base Sepolia then switch
if (switchErr?.code === 4902 || /not added/i.test(String(switchErr?.message))) {
await eth.request({
method: 'wallet_addEthereumChain',
params: [
{
chainId: targetChainIdHex,
chainName: 'Base Sepolia',
nativeCurrency: { name: 'ETH', symbol: 'ETH', decimals: 18 },
rpcUrls: ['https://sepolia.base.org'],
blockExplorerUrls: ['https://sepolia.basescan.org'],
},
],
})
await eth.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: targetChainIdHex }],
})
} else {
console.warn('Chain switch failed:', switchErr)
}
}
}
// Persist address but do not mark as connected until auth succeeds
window.localStorage.setItem('evm_wallet_address', addr)
setGlobalEvmWalletState({ address: addr, connected: false })
// Wallet-as-login: SIWE handshake with backend
try {
const { nonce, chainId, domain } = await api.auth.wallet.nonce(addr)
const issuedAt = new Date().toISOString()
const origin = typeof window !== 'undefined' ? window.location.origin : 'http://localhost:3000'
const d = domain || new URL(origin).hostname
const msg =
`${d} wants you to sign in with your Ethereum account:\n${addr}\n\n` +
`URI: ${origin}\n` +
`Version: 1\n` +
`Chain ID: ${chainId ?? 84532}\n` +
`Nonce: ${nonce}\n` +
`Issued At: ${issuedAt}`
// Try multiple signing parameter orders for broad wallet compatibility
const toHex = (s: string) => '0x' + Array.from(new TextEncoder().encode(s)).map((b) => b.toString(16).padStart(2, '0')).join('')
let sig: string | undefined
try {
sig = await eth.request({ method: 'personal_sign', params: [msg, addr] })
} catch (e1: any) {
try {
sig = await eth.request({ method: 'personal_sign', params: [addr, msg] })
} catch (e2: any) {
try {
sig = await eth.request({ method: 'personal_sign', params: [toHex(msg), addr] })
} catch (e3: any) {
try {
sig = await eth.request({ method: 'eth_sign', params: [addr, toHex(msg)] })
} catch (e4: any) {
console.warn('Signing failed using multiple methods:', e1?.message || e1, e2?.message || e2, e3?.message || e3, e4?.message || e4)
throw e4 || e3 || e2 || e1
}
}
}
}
if (!sig) throw new Error('Signature not obtained')
const connectRes = await api.auth.wallet.connect({ walletAddress: addr, signature: sig, message: msg })
if (connectRes?.token) {
window.localStorage.setItem('evm_auth_token', connectRes.token)
// Verify token works against a guarded endpoint
try {
const st = await api.auth.wallet.status()
if (st && st.connected) {
setGlobalEvmWalletState({ address: addr, connected: true })
return addr
} else {
// Guard rejected: clear token and treat as not connected
window.localStorage.removeItem('evm_auth_token')
setGlobalEvmWalletState({ address: addr, connected: false })
throw new Error('Authentication failed: status check not connected')
}
} catch (e) {
window.localStorage.removeItem('evm_auth_token')
setGlobalEvmWalletState({ address: addr, connected: false })
throw e
}
} else {
// No token returned -> treat as not connected for app features
window.localStorage.removeItem('evm_auth_token')
setGlobalEvmWalletState({ address: addr, connected: false })
throw new Error('Authentication token not issued')
}
} catch (e: any) {
console.warn('Wallet login handshake failed:', e?.message || e)
window.localStorage.removeItem('evm_auth_token')
setGlobalEvmWalletState({ address: addr, connected: false })
throw e
}
} catch (err) {
console.error('Failed to connect EVM wallet:', err)
return null
}
}, [])
const disconnect = useCallback(async () => {
try {
const addr = globalEvmWalletState.address
if (addr) {
await api.auth.wallet.disconnect(addr)
}
} catch (e) {
// ignore
}
if (typeof window !== 'undefined') {
window.localStorage.removeItem('evm_wallet_address')
window.localStorage.removeItem('evm_auth_token')
}
setGlobalEvmWalletState({ address: null, connected: false })
}, [])
const isConnected = evmWallet.connected
const address = evmWallet.address
const displayAddress = address ? `${address.slice(0, 6)}…${address.slice(-4)}` : ''
return {
evmWallet,
connect,
disconnect,
isConnected,
address,
displayAddress,
}
}