This repository was archived by the owner on Mar 27, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
49 lines (45 loc) · 1.37 KB
/
auth.ts
File metadata and controls
49 lines (45 loc) · 1.37 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
import { kv } from "@vercel/kv";
import { v4 } from "uuid";
import { cookies } from "next/headers";
export async function getAddress(id?: string): Promise<string> {
const sessionId = id ?? cookies().get("session")?.value;
let wallet: any;
if (!sessionId) {
wallet = await createWallet();
} else {
wallet = await kv.get(sessionId);
}
if (!wallet) {
throw new Error("Wallet not found");
}
return wallet.walletAddress;
}
async function getAddressByUsershare(
userShare: string
): Promise<{ walletAddress: string }> {
return fetch(process.env.MODULAR_CLOUD_WALLET_API + `/v1/wallet/address`, {
method: "POST",
body: JSON.stringify({ userShare }),
}).then((res) => res.json());
}
export async function createWallet() {
const { userShare } = await fetch(
process.env.MODULAR_CLOUD_WALLET_API + `/v1/wallet`,
{
method: "POST",
}
).then((res) => res.json());
const { walletAddress } = await getAddressByUsershare(userShare);
const id = v4();
await kv.set(id, JSON.stringify({ userShare, walletAddress }));
return { userShare, walletAddress, id };
}
export async function signMessage(
userShare: string,
message: string
): Promise<{ signature: string }> {
return fetch(process.env.MODULAR_CLOUD_WALLET_API + `/v1/sign-message`, {
method: "POST",
body: JSON.stringify({ userShare, message }),
}).then((res) => res.json());
}