Skip to content

Commit 9d623bc

Browse files
authored
Merge pull request #62 from abdoolyaro/patch-1
Implement permanent wallet connection flow
2 parents e2894b2 + f535b27 commit 9d623bc

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

‎frontend/lib/wallet‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import freighter from "@stellar/freighter-api";
2+
3+
export type WalletType = "freighter" | "lobstr" | "generated";
4+
5+
export interface ConnectedWallet {
6+
publicKey: string;
7+
type: WalletType;
8+
}
9+
10+
const STORAGE_KEY = "bridgelet_wallet";
11+
12+
// Save wallet to localStorage so it survives page refreshes
13+
export function persistWallet(wallet: ConnectedWallet): void {
14+
localStorage.setItem(STORAGE_KEY, JSON.stringify(wallet));
15+
}
16+
17+
// Load previously saved wallet on page load
18+
export function loadPersistedWallet(): ConnectedWallet | null {
19+
try {
20+
const raw = localStorage.getItem(STORAGE_KEY);
21+
if (!raw) return null;
22+
return JSON.parse(raw) as ConnectedWallet;
23+
} catch {
24+
return null;
25+
}
26+
}
27+
28+
export function clearPersistedWallet(): void {
29+
localStorage.removeItem(STORAGE_KEY);
30+
}
31+
32+
// Connect via Freighter browser extension
33+
export async function connectFreighter(): Promise<ConnectedWallet> {
34+
const isAvailable = await freighter.isConnected();
35+
if (!isAvailable) {
36+
throw new Error(
37+
"Freighter extension not found. Please install it from freighter.app and refresh."
38+
);
39+
}
40+
41+
// Request access — this opens the Freighter popup
42+
await freighter.requestAccess();
43+
44+
const { publicKey } = await freighter.getPublicKey();
45+
if (!publicKey) {
46+
throw new Error("Freighter did not return a public key. Did you approve the request?");
47+
}
48+
49+
return { publicKey, type: "freighter" };
50+
}
51+
52+
// LOBSTR is mobile-only, so on desktop we deeplink and poll for a result
53+
// The user scans a QR or taps the link, approves, and we read back their key
54+
// For MVP: we just open the LOBSTR website and ask them to paste their address
55+
export async function connectLobstr(): Promise<ConnectedWallet> {
56+
// LOBSTR doesn't have a JS SDK for web connection like Freighter does.
57+
// The flow here is: open LOBSTR, the user copies their public key, pastes it back.
58+
// This function returns a placeholder — the UI handles the paste step.
59+
throw new Error("USE_PASTE_FLOW");
60+
}
61+
62+
// Generate a brand-new Stellar keypair for users who have no wallet at all
63+
export async function generateNewWallet(): Promise<{
64+
wallet: ConnectedWallet;
65+
secretKey: string;
66+
}> {
67+
// We use the Stellar SDK dynamically to avoid SSR issues
68+
const { Keypair } = await import("@stellar/stellar-sdk");
69+
const keypair = Keypair.random();
70+
return {
71+
wallet: { publicKey: keypair.publicKey(), type: "generated" },
72+
secretKey: keypair.secret(),
73+
};
74+
}

0 commit comments

Comments
 (0)