Skip to content

Commit 65709ce

Browse files
committed
feat: init example
1 parent 2c42057 commit 65709ce

15 files changed

+433
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"wrapped-keys/nodejs",
3434
"hacker-guides/encryption/encrypt-file",
3535
"hacker-guides/encryption/encrypt-large-file",
36-
"hacker-guides/encryption/encrypt-string"
36+
"hacker-guides/encryption/encrypt-string",
37+
"sign-as-action/nodejs"
3738
],
3839
"scripts": {
3940
"install:all": "yarn workspaces focus --all",

sign-as-action/nodejs/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ETHEREUM_PRIVATE_KEY=
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"$schema": "https://json.schemastore.org/mocharc.json",
3+
"require": "tsx"
4+
}

sign-as-action/nodejs/README.md

Whitespace-only changes.

sign-as-action/nodejs/package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "sign-as-action-nodejs",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"type": "module",
7+
"scripts": {
8+
"test": "npx @dotenvx/dotenvx run -- mocha test/**/*.spec.ts"
9+
},
10+
"dependencies": {
11+
"@dotenvx/dotenvx": "^0.44.1",
12+
"@lit-protocol/auth-helpers": "^7.0.4",
13+
"@lit-protocol/constants": "^7.0.4",
14+
"@lit-protocol/contracts-sdk": "^7.0.4",
15+
"@lit-protocol/lit-node-client": "^7.0.4",
16+
"ethers": "5.7.2"
17+
},
18+
"devDependencies": {
19+
"@types/chai": "^4.3.16",
20+
"@types/chai-json-schema": "^1.4.10",
21+
"@types/mocha": "^10.0.6",
22+
"chai": "4.5.0",
23+
"chai-json-schema": "^1.5.1",
24+
"ipfs-only-hash": "^4.0.0",
25+
"mocha": "^10.4.0",
26+
"tsc": "^2.0.4",
27+
"tsx": "^4.12.0",
28+
"typescript": "^5.4.5"
29+
}
30+
}

sign-as-action/nodejs/project.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "sign-as-action-nodejs",
3+
"root": "sign-as-action/nodejs",
4+
"targets": {
5+
"test-lit": {
6+
"executor": "nx:run-commands",
7+
"options": {
8+
"command": "cd sign-as-action/nodejs && yarn && yarn test"
9+
}
10+
}
11+
}
12+
}

sign-as-action/nodejs/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from "./signAsAction";
2+
export * from "./verifyActionSignature";
3+
export * from './signAsActionLitAction';
4+
export * from './verifyActionSignatureLitAction';
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import * as ethers from "ethers";
2+
import { LitNodeClient } from "@lit-protocol/lit-node-client";
3+
import { LIT_RPC, LIT_NETWORK } from "@lit-protocol/constants";
4+
5+
import { getEnv, getSessionSigs } from "./utils";
6+
import { signAsActionLitAction } from "./signAsActionLitAction";
7+
8+
const ETHEREUM_PRIVATE_KEY = getEnv("ETHEREUM_PRIVATE_KEY");
9+
10+
export const signAsAction = async ({ toSign }: { toSign: Uint8Array }) => {
11+
let litNodeClient: LitNodeClient;
12+
13+
try {
14+
const ethersSigner = new ethers.Wallet(
15+
ETHEREUM_PRIVATE_KEY,
16+
new ethers.providers.JsonRpcProvider(LIT_RPC.CHRONICLE_YELLOWSTONE)
17+
);
18+
19+
console.log("🔄 Connecting to Lit network...");
20+
litNodeClient = new LitNodeClient({
21+
litNetwork: LIT_NETWORK.DatilDev,
22+
debug: false,
23+
});
24+
await litNodeClient.connect();
25+
console.log("✅ Connected to Lit network");
26+
27+
const sessionSigs = await getSessionSigs({ litNodeClient, ethersSigner });
28+
29+
console.log("🔄 Executing signAsAction Lit Action...");
30+
const litActionSignature = await litNodeClient.executeJs({
31+
sessionSigs,
32+
code: signAsActionLitAction,
33+
jsParams: {
34+
toSign,
35+
sigName: "sig",
36+
signingScheme: "EcdsaK256Sha256",
37+
},
38+
});
39+
console.log("✅ Executed signAsAction Lit Action");
40+
41+
return litActionSignature;
42+
} catch (error) {
43+
console.error(error);
44+
} finally {
45+
litNodeClient!.disconnect();
46+
}
47+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @ts-nocheck
2+
const _signAsActionLitAction = async () => {
3+
const signature = await Lit.Actions.signAsAction({
4+
toSign,
5+
sigName,
6+
signingScheme,
7+
});
8+
Lit.Actions.setResponse({ response: signature });
9+
};
10+
11+
export const signAsActionLitAction = `(${_signAsActionLitAction.toString()})();`;

sign-as-action/nodejs/src/utils.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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

Comments
 (0)