Skip to content

Instantly share code, notes, and snippets.

@hansy
Created August 2, 2024 23:41
Show Gist options
  • Save hansy/ddd58af9f38150c86ed4837beedf6496 to your computer and use it in GitHub Desktop.
Save hansy/ddd58af9f38150c86ed4837beedf6496 to your computer and use it in GitHub Desktop.
Run Lit Action
import { privateKeyToAccount } from "viem/accounts";
(async () => {
try {
const lit = new LitServer();
const userAccount = privateKeyToAccount(
`0x${process.env.PRIVATE_KEY}`
);
const sessionSigs = await lit.generateSessionSigs(userAccount);
console.log(sessionSigs);
const litActionCode = `
const go = async () => {
console.log("The answer to the universe is 42.");
};
go();
`;
const litActionResult = await lit.client.executeJs({
code: litActionCode,
sessionSigs,
jsParams: {},
});
console.log("Lit Action Result:", litActionResult.response);
process.exit(0);
} catch (e) {
console.error(e);
process.exit(1);
}
})();
import type {
EncryptStringRequest,
DecryptRequest,
EncryptResponse,
UnifiedAccessControlConditions,
LitNodeClientConfig,
AuthCallbackParams,
LitResourceAbilityRequest,
AuthSig,
} from "@lit-protocol/types";
import * as LitSDK from "@lit-protocol/lit-node-client";
import {
createSiweMessageWithRecaps,
LitAccessControlConditionResource,
LitAbility,
LitActionResource,
LitPKPResource,
} from "@lit-protocol/auth-helpers";
import { LitNetwork } from "@lit-protocol/constants";
import { baseSepolia } from "viem/chains";
import { Account, WalletClient } from "viem";
const APP_CHAIN = baseSepolia;
const LIT_CHAIN_NAME = "baseSepolia";
const CLIENT_OPTIONS: LitNodeClientConfig = {
alertWhenUnauthorized: false,
litNetwork: LitNetwork.DatilDev,
debug: false,
};
export class LitWeb {
client: LitSDK.LitNodeClientNodeJs | LitSDK.LitNodeClient | undefined =
undefined;
async connect() {
if (this.client) {
return this.client;
}
try {
const client = new LitSDK.LitNodeClient(CLIENT_OPTIONS);
await client.connect();
this.client = client;
} catch (e) {
console.error("Unable to initialize Lit client", e);
throw new Error("Unable to initialize Lit client");
}
}
async disconnect() {
if (this.client) {
await this.client.disconnect();
}
}
async createAuthSig(
params: AuthCallbackParams,
account: Account
): Promise<AuthSig> {
await this.connect();
if (!this.client) {
throw new Error("Unable to initialize Lit client");
}
try {
const address = account.address;
const preparedMessage = await createSiweMessageWithRecaps({
uri: String(params.uri),
expiration: String(params.expiration),
resources: params.resourceAbilityRequests,
walletAddress: address as `0x${string}`,
nonce: params.nonce,
litNodeClient: this.client,
statement: params.statement,
chainId: APP_CHAIN.id,
domain: process.env.NEXT_PUBLIC_HOST,
});
const signature = await account.signMessage({
message: preparedMessage,
});
return {
sig: signature,
derivedVia: "ethereum.web3.auth",
signedMessage: preparedMessage,
address: address as string,
};
// return await generateAuthSig({
// // @ts-ignore
// signer: wallet,
// toSign: preparedMessage,
// });
} catch (e) {
console.error(e);
return Promise.reject("Error signing message");
}
}
}
export class LitServer extends LitWeb {
async connect() {
if (this.client) {
return this.client;
}
try {
const client = new LitSDK.LitNodeClientNodeJs(CLIENT_OPTIONS);
await client.connect();
this.client = client;
} catch (e) {
console.error("Unable to initialize Lit client", e);
throw new Error("Unable to initialize Lit client");
}
}
async generateSessionSigs(
account: Account,
capacityDelegationAuthSig?: AuthSig
) {
await this.connect();
if (!this.client) {
throw new Error("Unable to initialize Lit client");
}
try {
return await this.client.getSessionSigs({
chain: LIT_CHAIN_NAME,
resourceAbilityRequests: [
{
resource: new LitActionResource("*"),
ability: LitAbility.LitActionExecution,
},
{
resource: new LitPKPResource("*"),
ability: LitAbility.PKPSigning,
},
],
authNeededCallback: async (params) => {
return await this.createAuthSig(params, account);
},
capacityDelegationAuthSig,
});
} catch (e) {
return Promise.reject(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment