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
149 lines (135 loc) · 5.23 KB
/
Copy pathuse-evm-wallet.ts
File metadata and controls
149 lines (135 loc) · 5.23 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
import { useCallback, useEffect, useState } from 'react'
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 stored = window.localStorage.getItem('evm_wallet_address')
if (stored) {
setGlobalEvmWalletState({ address: stored, connected: 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)
}
}
}
window.localStorage.setItem('evm_wallet_address', addr)
setGlobalEvmWalletState({ address: addr, connected: true })
// Wallet-as-login: SIWE handshake with backend
try {
const API_BASE = (process.env.NEXT_PUBLIC_API_BASE || 'http://localhost:3001').replace(/\/$/, '')
const nonceRes = await fetch(`${API_BASE}/api/auth/wallet/nonce`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ walletAddress: addr }),
})
if (nonceRes.ok) {
const { nonce, chainId, domain } = await nonceRes.json()
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}`
const sig = await eth.request({ method: 'personal_sign', params: [msg, addr] })
const connectRes = await fetch(`${API_BASE}/api/auth/wallet/connect`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ walletAddress: addr, signature: sig, message: msg }),
})
if (connectRes.ok) {
const json = await connectRes.json()
if (json?.token) {
window.localStorage.setItem('evm_auth_token', json.token)
}
}
}
} catch (e: any) {
console.warn('Wallet login handshake failed:', e?.message || e)
}
return addr
} catch (err) {
console.error('Failed to connect EVM wallet:', err)
return null
}
}, [])
const disconnect = useCallback(async () => {
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,
}
}