forked from Vero-protocol/vero-guardian-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfreighter.ts
More file actions
114 lines (94 loc) · 3.3 KB
/
Copy pathfreighter.ts
File metadata and controls
114 lines (94 loc) · 3.3 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
// src/lib/wallets/freighter.ts
'use client';
import * as freighterApi from '@stellar/freighter-api';
import type { StellarWalletProvider } from './types';
import { getWalletErrorMessage } from './utils';
declare global {
interface Window {
freighter?: unknown;
}
}
interface FreighterResultError {
message?: string;
}
export interface FreighterWalletWatcher {
watch: (callback: (params: { address?: string; error?: FreighterResultError }) => void) => {
error?: FreighterResultError;
};
stop: () => void;
}
interface FreighterApiCompat {
getPublicKey?: () => Promise<string>;
isConnected?: () => Promise<{ isConnected: boolean; error?: FreighterResultError }>;
getAddress?: () => Promise<{ address: string; error?: FreighterResultError }>;
requestAccess?: () => Promise<{ address: string; error?: FreighterResultError }>;
WatchWalletChanges?: new (timeout?: number) => FreighterWalletWatcher;
}
export const freighterClient = freighterApi as FreighterApiCompat;
export function isFreighterAvailable(): boolean {
return typeof window !== 'undefined' && Boolean(window.freighter);
}
/** Request access from Freighter and return the granted public key. */
export async function requestFreighterPublicKey(): Promise<string> {
if (typeof freighterClient.requestAccess === 'function') {
const access = await freighterClient.requestAccess();
if (access.error) {
throw new Error(
getWalletErrorMessage(access.error, 'Freighter could not grant wallet access. Open Freighter and try again.')
);
}
if (!access.address) {
throw new Error('Freighter did not return a wallet address. Unlock Freighter and try again.');
}
return access.address;
}
if (typeof freighterClient.getPublicKey === 'function') {
const publicKey = await freighterClient.getPublicKey();
if (!publicKey) {
throw new Error('Freighter did not return a wallet public key');
}
return publicKey;
}
throw new Error('Freighter wallet API is unavailable');
}
/** Read the currently connected Freighter address, or null when disconnected. */
export async function readCurrentFreighterPublicKey(): Promise<string | null> {
if (!isFreighterAvailable()) {
return null;
}
if (
typeof freighterClient.isConnected === 'function' &&
typeof freighterClient.getAddress === 'function'
) {
const connection = await freighterClient.isConnected();
if (connection.error) {
throw new Error(
getWalletErrorMessage(connection.error, 'Unable to verify Freighter wallet connection.')
);
}
if (!connection.isConnected) {
return null;
}
const address = await freighterClient.getAddress();
if (address.error) {
throw new Error(getWalletErrorMessage(address.error, 'Unable to read Freighter wallet address.'));
}
return address.address || null;
}
if (typeof freighterClient.getPublicKey === 'function') {
const publicKey = await freighterClient.getPublicKey();
return publicKey || null;
}
return null;
}
export const freighterProvider: StellarWalletProvider = {
id: 'freighter',
name: 'Freighter',
isAvailable: isFreighterAvailable,
connect: async () => {
if (!isFreighterAvailable()) {
throw new Error('Freighter wallet is not installed');
}
return requestFreighterPublicKey();
},
};