forked from ritik4ever/stellar-portfolio-rebalancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwalletBoot.ts
More file actions
225 lines (202 loc) · 6.75 KB
/
Copy pathwalletBoot.ts
File metadata and controls
225 lines (202 loc) · 6.75 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
import { WalletError } from '../utils/walletAdapters'
export function isAuthServiceUnavailable(err: unknown): boolean {
const msg = String((err as { message?: string })?.message ?? '')
return (
msg.includes('503') ||
msg.toLowerCase().includes('service_unavailable') ||
msg.toLowerCase().includes('not configured')
)
}
export type WalletReconnectBootDeps = {
reconnect: () => Promise<string | null>
checkConsent: (userId: string) => Promise<boolean>
authLogin: (pk: string) => Promise<unknown>
getAutoReconnect: () => boolean
}
export type WalletReconnectBootResult =
| { outcome: 'no_wallet' }
| { outcome: 'needs_consent'; publicKey: string }
| { outcome: 'dashboard'; publicKey: string }
| { outcome: 'auth_failed'; message: string }
| { outcome: 'reconnect_failed'; message: string; walletErrorCode?: string }
export type WalletConnectBootState =
| { status: 'loading' }
| { status: 'connected'; publicKey: string }
| { status: 'not_installed'; message: string }
| { status: 'rejected'; message: string }
| { status: 'failed'; message: string }
export type WalletConnectBootDeps = {
checkExtension: () => Promise<boolean> | boolean
connect: () => Promise<void>
getAddress: () => Promise<string | null>
}
export async function runWalletConnectBoot(
deps: WalletConnectBootDeps,
): Promise<WalletConnectBootState> {
try {
const hasExtension = await deps.checkExtension()
if (!hasExtension) {
return {
status: 'not_installed',
message: 'Wallet extension is not installed.',
}
}
} catch {
return {
status: 'not_installed',
message: 'Wallet extension is not installed.',
}
}
try {
await deps.connect()
} catch (err: unknown) {
const walletErr = err instanceof WalletError ? err : null
if (walletErr?.code === 'USER_DECLINED') {
return {
status: 'rejected',
message: 'Wallet connection was rejected by the user.',
}
}
return {
status: 'failed',
message: 'Wallet connection failed. Please try again.',
}
}
try {
const publicKey = await deps.getAddress()
if (!publicKey) {
return {
status: 'failed',
message: 'Wallet connected but no address was returned.',
}
}
return { status: 'connected', publicKey }
} catch {
return {
status: 'failed',
message: 'Unable to read wallet address after connecting.',
}
}
}
export async function runWalletReconnectBoot(
deps: WalletReconnectBootDeps,
): Promise<WalletReconnectBootResult> {
if (!deps.getAutoReconnect()) {
return { outcome: 'no_wallet' }
}
try {
const pk = await deps.reconnect()
if (!pk) {
return { outcome: 'no_wallet' }
}
const accepted = await deps.checkConsent(pk)
if (accepted) {
try {
await deps.authLogin(pk)
} catch (authErr: unknown) {
if (isAuthServiceUnavailable(authErr)) {
return { outcome: 'dashboard', publicKey: pk }
}
return {
outcome: 'auth_failed',
message:
'Wallet reconnected but authentication failed. Please reconnect your wallet to try again.',
}
}
return { outcome: 'dashboard', publicKey: pk }
}
return { outcome: 'needs_consent', publicKey: pk }
} catch (err: unknown) {
if (err instanceof WalletError) {
if (err.code === 'USER_DECLINED') {
return {
outcome: 'reconnect_failed',
message: 'Wallet reconnect was declined. Please approve in your wallet.',
walletErrorCode: err.code,
}
}
return {
outcome: 'reconnect_failed',
message: 'Failed to reconnect wallet. Please try connecting again.',
walletErrorCode: err.code,
}
}
return {
outcome: 'reconnect_failed',
message: 'Failed to reconnect wallet. Please try connecting again.',
}
}
}
export type ConsentAcceptNavigation = {
targetView: 'dashboard'
publicKey: string
}
export function resolveConsentAcceptedNavigation(
pendingPublicKey: string | null,
): ConsentAcceptNavigation | null {
if (!pendingPublicKey) return null
return { targetView: 'dashboard', publicKey: pendingPublicKey }
}
// ─── Boot diagnostics ───────────────────────────────────────────────────
export type BootCheckStatus = 'loading' | 'passed' | 'failed'
export interface BootCheck {
id: string
label: string
status: BootCheckStatus
message?: string
}
export interface BootDiagnostics {
checks: BootCheck[]
allPassed: boolean
timestamp: number
}
export type BootDiagnosticsDeps = {
checkWallets: () => boolean | Promise<boolean>
checkApi: () => Promise<boolean>
}
export async function runBootDiagnostics(
deps: BootDiagnosticsDeps,
): Promise<BootDiagnostics> {
const checks: BootCheck[] = []
try {
const hasWallets = await deps.checkWallets()
checks.push({
id: 'wallet-detection',
label: 'Wallet extension',
status: hasWallets ? 'passed' : 'failed',
message: hasWallets
? 'Stellar wallet detected'
: 'No Stellar wallet extension found. Install Freighter, Rabet, xBull, or Hana.',
})
} catch {
checks.push({
id: 'wallet-detection',
label: 'Wallet extension',
status: 'failed',
message: 'Wallet detection check failed.',
})
}
try {
const apiReachable = await deps.checkApi()
checks.push({
id: 'api-reachability',
label: 'API reachability',
status: apiReachable ? 'passed' : 'failed',
message: apiReachable
? 'Backend API is reachable'
: 'Cannot reach the backend API. The app may have limited functionality.',
})
} catch {
checks.push({
id: 'api-reachability',
label: 'API reachability',
status: 'failed',
message: 'API reachability check failed.',
})
}
return {
checks,
allPassed: checks.every((c) => c.status === 'passed'),
timestamp: Date.now(),
}
}