forked from MergeFi/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwallet.ts
More file actions
43 lines (39 loc) · 1.21 KB
/
Copy pathwallet.ts
File metadata and controls
43 lines (39 loc) · 1.21 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
import {
isConnected as freighterIsConnected,
isAllowed,
setAllowed,
getAddress,
signTransaction as freighterSignTransaction,
} from "@stellar/freighter-api";
import { STELLAR_NETWORK } from "./config";
export interface WalletConnection {
address: string;
network: string;
}
export async function isFreighterInstalled(): Promise<boolean> {
const result = await freighterIsConnected();
return !result.error && result.isConnected;
}
export async function connectWallet(): Promise<WalletConnection> {
const allowed = await isAllowed();
if (!allowed.isAllowed) {
const granted = await setAllowed();
if (granted.error || !granted.isAllowed) {
throw new Error("Wallet access was not granted.");
}
}
const { address, error } = await getAddress();
if (error || !address) {
throw new Error(error?.message ?? "Unable to read wallet address.");
}
return { address, network: STELLAR_NETWORK };
}
export async function signTransaction(xdr: string, address: string) {
return freighterSignTransaction(xdr, {
address,
networkPassphrase:
STELLAR_NETWORK === "PUBLIC"
? "Public Global Stellar Network ; September 2015"
: "Test SDF Network ; September 2015",
});
}