UNPKG

@parcl-finance/product-sdk

Version:

TypeScript SDK for interacting with Parcl's product APIs

105 lines 3.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Auth = void 0; const anchor_1 = require("@project-serum/anchor"); const httpClient_1 = require("./httpClient"); const constants_1 = require("./constants"); class Auth { baseUrl; headers; client; expressClient; constructor(baseUrl, env) { this.headers = {}; this.baseUrl = baseUrl.concat("/auth"); this.client = new httpClient_1.HttpClient(this.baseUrl, this.headers); this.expressClient = new httpClient_1.HttpClient((0, constants_1.getDefaultExpressApiUrl)(env), this.headers); } async loginMessage(address) { const { message } = await this.client.get({ path: "/login/message", params: { address, }, }); return message; } async getLoginMessage(address) { const { message } = await this.expressClient.get({ path: "/auth/login", params: { address, }, }); return message; } async login({ address, message, signature }) { if (signature.byteLength !== 64) { throw new Error("Signature must be 64 bytes long"); } const { info } = await this.client.post({ path: "/login", body: { address, message, signature: anchor_1.utils.bytes.bs58.encode(signature), }, }); return info; } async getAccessTokens({ address, message, signature, }) { if (signature.byteLength !== 64) { throw new Error("Signature must be 64 bytes long"); } const { accessToken, refreshToken } = await this.expressClient.post({ path: "/auth/login", body: { address, message, signature: anchor_1.utils.bytes.bs58.encode(signature), }, }); return { accessToken, refreshToken }; } async getHardwareWalletAccessTokens({ address, tx, }) { if (!tx.verifySignatures()) { throw new Error("User did not sign tx for hardware wallet login"); } const { accessToken, refreshToken } = await this.expressClient.post({ path: "/auth/hardware-wallet-login", body: { address, tx: tx.serialize().toString("base64"), }, }); return { accessToken, refreshToken, }; } async loginHardwareWallet({ address, tx, }) { if (!tx.verifySignatures()) { throw new Error("User did not sign tx for hardware wallet login"); } const { info } = await this.client.post({ path: "/login/hardware-wallet", body: { address, tx: tx.serialize().toString("base64"), }, }); return info; } async challenge(address, accessToken) { const { isAuthenticated } = await this.client.post({ path: "/challenge", body: { address, }, accessToken, }); return isAuthenticated; } } exports.Auth = Auth; //# sourceMappingURL=auth.js.map