UNPKG

@lit-protocol/auth-helpers

Version:

Advanced authentication utilities for managing blockchain resource permissions and capabilities within the Lit Protocol ecosystem. Built on top of SIWE (Sign-In with Ethereum) and SIWE-RECAP for robust authentication flows.

86 lines 3.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createSiweMessageWithCapacityDelegation = exports.createSiweMessageWithRecaps = exports.createSiweMessage = void 0; const siwe_1 = require("siwe"); const constants_1 = require("@lit-protocol/constants"); const resources_1 = require("../resources"); const siwe_helper_1 = require("./siwe-helper"); /** * Creates a SIWE * @param { BaseSiweMessage } params - The parameters for creating the SIWE message. * @returns A promise that resolves to the created SIWE message as a string. * @throws An error if the walletAddress parameter is missing. */ const createSiweMessage = async (params) => { // -- validations if (!params.walletAddress) { throw new Error('walletAddress is required'); } const ONE_WEEK_FROM_NOW = new Date(Date.now() + 1000 * 60 * 60 * 24 * 7).toISOString(); const siweParams = { domain: params?.domain ?? 'localhost', address: params.walletAddress, statement: params?.statement ?? 'This is a test statement. You can put anything you want here.', uri: params?.uri ?? 'https://localhost/login', version: params?.version ?? '1', chainId: params?.chainId ?? 1, nonce: params.nonce, expirationTime: params?.expiration ?? ONE_WEEK_FROM_NOW, }; let siweMessage = new siwe_1.SiweMessage(siweParams); // -- create a message with capacity credits if ('dAppOwnerWallet' in params || // required param 'uses' in params || // optional 'delegateeAddresses' in params || // optional 'capacityTokenId' in params // optional ) { const ccParams = params; const capabilities = (0, siwe_helper_1.createCapacityCreditsResourceData)(ccParams); params.resources = [ { resource: new resources_1.LitRLIResource(ccParams.capacityTokenId ?? '*'), ability: constants_1.LIT_ABILITY.RateLimitIncreaseAuth, data: capabilities, }, ]; } // -- add recap resources if needed if (params.resources) { siweMessage = await (0, siwe_helper_1.addRecapToSiweMessage)({ siweMessage, resources: params.resources, litNodeClient: params.litNodeClient, }); } return siweMessage.prepareMessage(); }; exports.createSiweMessage = createSiweMessage; /** * Creates a SIWE message with recaps added to it. * * @param { WithRecap } params - The parameters for creating the SIWE message with recaps. * @returns A Promise that resolves to a string representing the SIWE message. */ const createSiweMessageWithRecaps = async (params) => { return (0, exports.createSiweMessage)({ ...params, }); }; exports.createSiweMessageWithRecaps = createSiweMessageWithRecaps; /** * Creates a SIWE message with capacity delegation. * @param { WithCapacityDelegation } params - The parameters for creating the SIWE message. * @returns A Promise that resolves to the created SIWE message. * @throws An error if litNodeClient is not provided. */ const createSiweMessageWithCapacityDelegation = async (params) => { if (!params.litNodeClient) { throw new Error('litNodeClient is required'); } return (0, exports.createSiweMessage)({ ...params, }); }; exports.createSiweMessageWithCapacityDelegation = createSiweMessageWithCapacityDelegation; //# sourceMappingURL=create-siwe-message.js.map