-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathstellar.ts
More file actions
83 lines (77 loc) · 2.43 KB
/
stellar.ts
File metadata and controls
83 lines (77 loc) · 2.43 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
import { StellarWalletsKit, Networks } from "@creit.tech/stellar-wallets-kit";
// TODO: See docs/ISSUES.md — "Wallet Connection"
let kit: StellarWalletsKit | null = null;
export function getWalletsKit(): StellarWalletsKit {
if (!kit) {
kit = new StellarWalletsKit({
network:
(process.env.NEXT_PUBLIC_STELLAR_NETWORK as Networks) ??
Networks.TESTNET,
selectedWalletId: "freighter",
});
}
return kit;
}
/**
* Opens the wallet-select modal and returns the connected public key.
* Resolves once the user selects a wallet and the address is retrieved.
*/
export async function connectWallet(): Promise<string> {
const kit = getWalletsKit();
return new Promise((resolve, reject) => {
kit.openModal({
onWalletSelected: async (wallet) => {
try {
kit.setWallet(wallet.id);
const { address } = await kit.getAddress();
resolve(address);
} catch (e) {
reject(e);
}
},
});
});
if (process.env.NEXT_PUBLIC_E2E === "true") return "GD...CLIENT";
const walletsKit = getWalletsKit();
return new Promise<string>((resolve, reject) => {
walletsKit.openModal({
onWalletSelected: async () => {
try {
walletsKit.closeModal();
const { address } = await walletsKit.getAddress();
resolve(address);
} catch (err) {
reject(err);
}
},
});
});
}
export async function getConnectedWalletAddress(): Promise<string | null> {
if (process.env.NEXT_PUBLIC_E2E === "true") return "GD...CLIENT";
try {
const { address } = await getWalletsKit().getAddress();
return address ?? null;
} catch {
return null;
}
}
/**
* Signs an XDR transaction string via the connected wallet.
* Returns the signed XDR string ready for submission to the Soroban RPC.
*/
export async function signTransaction(xdr: string): Promise<string> {
const kit = getWalletsKit();
const address = localStorage.getItem("wallet_address");
if (!address) throw new Error("Wallet not connected");
const { signedTxXdr } = await kit.signTransaction(xdr, {
publicKey: address,
if (process.env.NEXT_PUBLIC_E2E === "true") return xdr;
const walletsKit = getWalletsKit();
const networkPassphrase =
(process.env.NEXT_PUBLIC_STELLAR_NETWORK as Networks) ?? Networks.TESTNET;
const { signedTxXdr } = await walletsKit.signTransaction(xdr, {
networkPassphrase,
});
return signedTxXdr;
}