|
| 1 | +import * as ethers from "ethers"; |
| 2 | +import { LitNodeClient } from "@lit-protocol/lit-node-client"; |
| 3 | +import { LIT_ABILITY } from "@lit-protocol/constants"; |
| 4 | +import { |
| 5 | + createSiweMessageWithRecaps, |
| 6 | + generateAuthSig, |
| 7 | + LitActionResource, |
| 8 | +} from "@lit-protocol/auth-helpers"; |
| 9 | + |
| 10 | +export const getEnv = (name: string): string => { |
| 11 | + // Browser environment |
| 12 | + if (typeof globalThis !== 'undefined' && 'window' in globalThis) { |
| 13 | + const envMap: Record<string, string | undefined> = { |
| 14 | + 'ETHEREUM_PRIVATE_KEY': process.env.NEXT_PUBLIC_ETHEREUM_PRIVATE_KEY |
| 15 | + }; |
| 16 | + const env = envMap[name]; |
| 17 | + if (env === undefined || env === "") |
| 18 | + throw new Error( |
| 19 | + `Browser: ${name} ENV is not defined, please define it in the .env file` |
| 20 | + ); |
| 21 | + return env; |
| 22 | + } |
| 23 | + |
| 24 | + // Node environment |
| 25 | + const env = process.env[name]; |
| 26 | + if (env === undefined || env === "") |
| 27 | + throw new Error( |
| 28 | + `Node: ${name} ENV is not defined, please define it in the .env file` |
| 29 | + ); |
| 30 | + return env; |
| 31 | +}; |
| 32 | + |
| 33 | +export const getSessionSigs = async ({ litNodeClient, ethersSigner }: { litNodeClient: LitNodeClient, ethersSigner: ethers.Wallet }) => { |
| 34 | + console.log("🔄 Getting Session Signatures..."); |
| 35 | + const sessionSigs = await litNodeClient.getSessionSigs({ |
| 36 | + chain: "ethereum", |
| 37 | + expiration: new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString(), // 24 hours |
| 38 | + resourceAbilityRequests: [ |
| 39 | + { |
| 40 | + resource: new LitActionResource("*"), |
| 41 | + ability: LIT_ABILITY.LitActionExecution, |
| 42 | + }, |
| 43 | + ], |
| 44 | + authNeededCallback: async ({ |
| 45 | + resourceAbilityRequests, |
| 46 | + expiration, |
| 47 | + uri, |
| 48 | + }) => { |
| 49 | + const toSign = await createSiweMessageWithRecaps({ |
| 50 | + uri: uri!, |
| 51 | + expiration: expiration!, |
| 52 | + resources: resourceAbilityRequests!, |
| 53 | + walletAddress: ethersSigner.address, |
| 54 | + nonce: await litNodeClient.getLatestBlockhash(), |
| 55 | + litNodeClient, |
| 56 | + }); |
| 57 | + |
| 58 | + return await generateAuthSig({ |
| 59 | + signer: ethersSigner, |
| 60 | + toSign, |
| 61 | + }); |
| 62 | + }, |
| 63 | + }); |
| 64 | + console.log("✅ Got Session Signatures"); |
| 65 | + return sessionSigs; |
| 66 | +}; |
0 commit comments