2026-06-02 00:28:22 +08:00
|
|
|
import { apiBase, getJSONAuth, postJSON } from "../api";
|
|
|
|
|
|
|
|
|
|
export type WalletNonceResponse = {
|
|
|
|
|
nonce: string;
|
|
|
|
|
message: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type WalletVerifyResponse = {
|
|
|
|
|
token: string;
|
|
|
|
|
wallet: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type WalletMeResponse = {
|
|
|
|
|
wallet: string;
|
|
|
|
|
role: "user";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type TokenPocketLoginRequest = {
|
|
|
|
|
actionId: string;
|
|
|
|
|
nonce: string;
|
|
|
|
|
message: string;
|
|
|
|
|
qrUrl: string;
|
|
|
|
|
expiresAt: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type TokenPocketLoginResult =
|
|
|
|
|
| {
|
|
|
|
|
status: "pending" | "expired" | "failed";
|
|
|
|
|
message?: string;
|
|
|
|
|
error?: string;
|
|
|
|
|
}
|
|
|
|
|
| {
|
|
|
|
|
status: "completed";
|
|
|
|
|
address: string;
|
|
|
|
|
message: string;
|
|
|
|
|
signature: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function requestWalletNonce(
|
|
|
|
|
address: string,
|
|
|
|
|
): Promise<WalletNonceResponse> {
|
|
|
|
|
return postJSON<WalletNonceResponse>("/api/auth/wallet/nonce", { address });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function verifyWalletSignature(params: {
|
|
|
|
|
address: string;
|
|
|
|
|
message: string;
|
|
|
|
|
signature: string;
|
|
|
|
|
}): Promise<WalletVerifyResponse> {
|
|
|
|
|
return postJSON<WalletVerifyResponse>("/api/auth/wallet/verify", params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchWalletMe(token: string): Promise<WalletMeResponse> {
|
|
|
|
|
return getJSONAuth<WalletMeResponse>("/api/auth/wallet/me", token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function createTokenPocketLoginRequest(): Promise<TokenPocketLoginRequest> {
|
|
|
|
|
return postJSON<TokenPocketLoginRequest>(
|
|
|
|
|
"/api/auth/wallet/tp-login-request",
|
|
|
|
|
{},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function fetchTokenPocketLoginResult(
|
|
|
|
|
actionId: string,
|
2026-06-02 01:00:13 +08:00
|
|
|
signal?: AbortSignal,
|
2026-06-02 00:28:22 +08:00
|
|
|
): Promise<TokenPocketLoginResult> {
|
|
|
|
|
const res = await fetch(
|
|
|
|
|
`${apiBase}/api/auth/wallet/tp-result?actionId=${encodeURIComponent(actionId)}`,
|
2026-06-02 01:00:13 +08:00
|
|
|
{ signal },
|
2026-06-02 00:28:22 +08:00
|
|
|
);
|
|
|
|
|
if (!res.ok) throw new Error(await res.text());
|
|
|
|
|
return res.json() as Promise<TokenPocketLoginResult>;
|
|
|
|
|
}
|