forked from Movalabs-crew/mova-store
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfreighter.ts
More file actions
95 lines (85 loc) · 2.68 KB
/
Copy pathfreighter.ts
File metadata and controls
95 lines (85 loc) · 2.68 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
import {
getAddress,
getNetwork,
isConnected,
requestAccess,
signTransaction,
} from "@stellar/freighter-api";
import { NETWORK, NETWORK_PASSPHRASE } from "./config";
// ---------------------------------------------------------------------------
// Freighter wallet helpers.
// ---------------------------------------------------------------------------
export class WalletError extends Error {
code: string;
constructor(message: string, code = "WALLET_ERROR") {
super(message);
this.name = "WalletError";
this.code = code;
}
}
export async function freighterAvailable(): Promise<boolean> {
try {
const res = await isConnected();
return Boolean(res.isConnected);
} catch {
return false;
}
}
export async function connectWallet(): Promise<string> {
if (!(await freighterAvailable())) {
throw new WalletError(
"Freighter is not installed. Install the Freighter wallet extension to pay with USDC.",
"FREIGHTER_NOT_FOUND"
);
}
const res = await requestAccess();
if (res.error) {
throw new WalletError(res.error.toString(), "FREIGHTER_REQUEST_DENIED");
}
return res.address;
}
export async function currentAddress(): Promise<string | null> {
if (!(await freighterAvailable())) return null;
const res = await getAddress();
if (res.error || !res.address) return null;
return res.address;
}
export async function ensureNetwork(): Promise<string> {
const res = await getNetwork();
if (res.error) {
throw new WalletError(res.error.toString(), "FREIGHTER_NETWORK_ERROR");
}
const detected = res.network;
// Freighter reports networks as "TESTNET" / "PUBLIC" / "STANDALONE".
if (
(NETWORK === "testnet" && detected.toUpperCase() !== "TESTNET") ||
(NETWORK === "mainnet" && detected.toUpperCase() !== "PUBLIC")
) {
throw new WalletError(
`Freighter is on "${detected}" but this store expects "${NETWORK}". ` +
"Switch your Freighter network and try again.",
"WRONG_NETWORK"
);
}
return detected;
}
export async function signWithFreighter(
transactionXdr: string,
publicKey: string
): Promise<string> {
const res = await signTransaction(transactionXdr, {
networkPassphrase: NETWORK_PASSPHRASE,
address: publicKey,
});
if (res.error) {
throw new WalletError(res.error.toString(), "FREIGHTER_SIGN_ERROR");
}
if (!res.signedTxXdr) {
throw new WalletError("Freighter did not return a signed transaction.", "FREIGHTER_SIGN_ERROR");
}
return res.signedTxXdr;
}
export function shortAddress(address: string, chars = 6): string {
if (!address || address.length <= chars * 2 + 3) return address ?? "";
return `${address.slice(0, chars)}…${address.slice(-chars)}`;
}