UNPKG

@samudai_xyz/web3-sdk

Version:

## All in one web3 integrations for Samudai

89 lines (88 loc) 3.47 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.LitProtocol = void 0; const lit_js_sdk_1 = __importDefault(require("lit-js-sdk")); const enums_1 = require("../utils/enums"); const tokenConditions_1 = require("../lib/tokenConditions"); class LitProtocol { constructor() { this.accessControlConditions = []; this.resourceId = { baseUrl: '', path: '', orgId: '', role: '', extraData: '', }; this.init = async (chain, contractAddress, typeOfGating, baseUrl, path, memberId, tokenId) => { try { if (typeOfGating === enums_1.TokenGatingType.ERC20) { this.accessControlConditions = (0, tokenConditions_1.getERC20TokenGating)(contractAddress, chain); } else if (typeOfGating === enums_1.TokenGatingType.ERC721) { this.accessControlConditions = (0, tokenConditions_1.getERC721TokenGating)(contractAddress, chain, tokenId); } else if (typeOfGating === enums_1.TokenGatingType.ERC1155) { this.accessControlConditions = (0, tokenConditions_1.getERC1155TokenGating)(contractAddress, chain, tokenId); } else { throw new Error('Invalid token gating type'); } this.resourceId = { baseUrl: baseUrl, path: path, orgId: '', role: '', extraData: memberId, }; const client = new lit_js_sdk_1.default.LitNodeClient({ alertWhenUnauthorized: false, }); await client.connect(); const authSig = await lit_js_sdk_1.default.checkAndSignAuthMessage({ chain: chain }); await client.saveSigningCondition({ accessControlConditions: this.accessControlConditions, chain, authSig, resourceId: this.resourceId, }); const jwt = await client.getSignedToken({ accessControlConditions: this.accessControlConditions, chain, authSig, resourceId: this.resourceId, }); return jwt; } catch (err) { throw err; } }; this.verifyLit = async (jwt, memberId) => { try { if (!jwt) { return false; } else { const { verified, payload } = lit_js_sdk_1.default.verifyJwt({ jwt }); if (payload.baseUrl !== this.resourceId.baseUrl || payload.path !== this.resourceId.path || payload.extraData !== memberId) { return false; } else { return true; } } } catch (err) { console.log(err); return false; } }; } } exports.LitProtocol = LitProtocol;