Skip to content

Commit c418ae2

Browse files
committed
i think it should work
1 parent cfd12cf commit c418ae2

19 files changed

+14159
-1
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/**/*/node_modules
2-
/**/*/.env
2+
/**/*/.env
3+
.DS_Store

dynamic-oracle/browser/.env.example

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PKP_PUBLIC_KEY=

dynamic-oracle/browser/.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.env
2+
.cache
3+
dist
4+
node_modules

dynamic-oracle/browser/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Running this Example
2+
3+
1. `yarn`
4+
2. `yarn start`
5+
3. Click the `Click Me` button
6+
4. Connect your wallet
7+
5. Sign a message to generate a SessionSig
8+
6. The PKP signed message will be in the JavaScript console

dynamic-oracle/browser/package.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "lit-action-using-fetch-browser",
3+
"version": "0.1.0",
4+
"description": "Example of executing a Lit Action that makes a API request using Fetch",
5+
"source": "src/index.html",
6+
"license": "MIT",
7+
"scripts": {
8+
"start": "parcel ./src/index.html"
9+
},
10+
"dependencies": {
11+
"@dotenvx/dotenvx": "^0.37.1",
12+
"@lit-protocol/auth-browser": "^6.2.2",
13+
"@lit-protocol/auth-helpers": "^6.2.2",
14+
"@lit-protocol/constants": "^6.2.2",
15+
"@lit-protocol/contracts-sdk": "^6.2.2",
16+
"@lit-protocol/lit-node-client": "^6.2.2",
17+
"ethers": "5.7.2"
18+
},
19+
"devDependencies": {
20+
"parcel-bundler": "^1.12.5"
21+
}
22+
}

dynamic-oracle/browser/src/index.html

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Lit Session Signature Example</title>
7+
</head>
8+
<body>
9+
<button id="myButton">Click Me</button>
10+
<script src="./index.js"></script>
11+
</body>
12+
</html>

dynamic-oracle/browser/src/index.js

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import { LitNodeClient } from "@lit-protocol/lit-node-client";
2+
import { LitNetwork } from "@lit-protocol/constants";
3+
import {
4+
createSiweMessageWithRecaps,
5+
generateAuthSig,
6+
LitAbility,
7+
LitActionResource,
8+
LitPKPResource,
9+
} from "@lit-protocol/auth-helpers";
10+
import { LitContracts } from "@lit-protocol/contracts-sdk";
11+
import { disconnectWeb3 } from "@lit-protocol/auth-browser";
12+
import * as ethers from "ethers";
13+
14+
import { litActionCode } from "./litAction";
15+
16+
document.addEventListener("DOMContentLoaded", () => {
17+
document.getElementById("myButton").addEventListener("click", buttonClick);
18+
});
19+
20+
async function buttonClick() {
21+
try {
22+
console.log("Clicked");
23+
24+
const provider = new ethers.providers.Web3Provider(window.ethereum);
25+
await provider.send("eth_requestAccounts", []);
26+
const ethersSigner = provider.getSigner();
27+
console.log("Connected account:", await ethersSigner.getAddress());
28+
29+
const litNodeClient = await getLitNodeClient();
30+
31+
const sessionSigs = await getSessionSigs(litNodeClient, ethersSigner);
32+
console.log("Got Session Signatures!");
33+
34+
const litActionSignatures = await litNodeClient.executeJs({
35+
sessionSigs,
36+
code: litActionCode,
37+
jsParams: {
38+
url: "https://api.weather.gov/gridpoints/TOP/31,80/forecast",
39+
publicKey: await getPkpPublicKey(),
40+
sigName: "sig",
41+
},
42+
});
43+
console.log("litActionSignatures: ", litActionSignatures);
44+
} catch (error) {
45+
console.error(error);
46+
} finally {
47+
disconnectWeb3();
48+
}
49+
}
50+
51+
async function getLitNodeClient() {
52+
const litNodeClient = new LitNodeClient({
53+
litNetwork: LitNetwork.DatilDev,
54+
});
55+
56+
console.log("Connecting litNodeClient to network...");
57+
await litNodeClient.connect();
58+
59+
console.log("litNodeClient connected!");
60+
return litNodeClient;
61+
}
62+
63+
async function getPkpPublicKey(ethersSigner) {
64+
if (
65+
process.env.PKP_PUBLIC_KEY !== undefined &&
66+
process.env.PKP_PUBLIC_KEY !== ""
67+
)
68+
return process.env.PKP_PUBLIC_KEY;
69+
70+
const pkp = await mintPkp(ethersSigner);
71+
console.log("Minted PKP!", pkp);
72+
return pkp.publicKey;
73+
}
74+
75+
async function mintPkp(ethersSigner) {
76+
console.log("Minting new PKP...");
77+
const litContracts = new LitContracts({
78+
signer: ethersSigner,
79+
network: LitNetwork.DatilDev,
80+
});
81+
82+
await litContracts.connect();
83+
84+
return (await litContracts.pkpNftContractUtils.write.mint()).pkp;
85+
}
86+
87+
async function getSessionSigs(litNodeClient, ethersSigner) {
88+
console.log("Getting Session Signatures...");
89+
return litNodeClient.getSessionSigs({
90+
chain: "ethereum",
91+
expiration: new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString(), // 24 hours
92+
resourceAbilityRequests: [
93+
{
94+
resource: new LitActionResource("*"),
95+
ability: LitAbility.LitActionExecution,
96+
},
97+
{
98+
resource: new LitPKPResource("*"),
99+
ability: LitAbility.PKPSigning,
100+
},
101+
],
102+
authNeededCallback: getAuthNeededCallback(litNodeClient, ethersSigner),
103+
});
104+
}
105+
106+
function getAuthNeededCallback(litNodeClient, ethersSigner) {
107+
return async ({ resourceAbilityRequests, expiration, uri }) => {
108+
const toSign = await createSiweMessageWithRecaps({
109+
uri,
110+
expiration,
111+
resources: resourceAbilityRequests,
112+
walletAddress: await ethersSigner.getAddress(),
113+
nonce: await litNodeClient.getLatestBlockhash(),
114+
litNodeClient,
115+
});
116+
117+
const authSig = await generateAuthSig({
118+
signer: ethersSigner,
119+
toSign,
120+
});
121+
122+
return authSig;
123+
};
124+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export const litActionCode = `
2+
(async () => {
3+
const resp = await fetch(url).then((response) => response.json());
4+
const temp = resp.properties.periods[0].temperature;
5+
6+
// add any other data you want to include in the signature here
7+
// like the user's identity, or a timestamp
8+
const messageToSign = { temp, url };
9+
const toSign = ethers.utils.arrayify(ethers.utils.keccak256(ethers.utils.toUtf8Bytes(JSON.stringify(messageToSign))));
10+
11+
// this requests a signature share from the Lit Node
12+
// the signature share will be automatically returned in the HTTP response from the node
13+
// all the params (toSign, publicKey, sigName) are passed in from the LitJsSdk.executeJs() function
14+
const sigShare = await Lit.Actions.signEcdsa({ toSign, publicKey, sigName });
15+
Lit.Actions.setResponse({response: JSON.stringify({messageSigned: messageToSign})});
16+
})();
17+
`;

0 commit comments

Comments
 (0)