Skip to content

Commit 093b51d

Browse files
committed
works for both nodejs and browser
1 parent c418ae2 commit 093b51d

File tree

6 files changed

+275
-248
lines changed

6 files changed

+275
-248
lines changed

dynamic-oracle/browser/src/index.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,38 @@ async function buttonClick() {
3131
const sessionSigs = await getSessionSigs(litNodeClient, ethersSigner);
3232
console.log("Got Session Signatures!");
3333

34+
const pkpPublicKey = await getPkpPublicKey(ethersSigner);
35+
3436
const litActionSignatures = await litNodeClient.executeJs({
3537
sessionSigs,
3638
code: litActionCode,
3739
jsParams: {
3840
url: "https://api.weather.gov/gridpoints/TOP/31,80/forecast",
39-
publicKey: await getPkpPublicKey(),
41+
publicKey: pkpPublicKey,
4042
sigName: "sig",
4143
},
4244
});
4345
console.log("litActionSignatures: ", litActionSignatures);
46+
47+
// verify the signature
48+
console.log("Verifying signature...");
49+
const sig = litActionSignatures.signatures.sig.signature;
50+
const messageSigned = JSON.stringify(
51+
litActionSignatures.response.messageSigned
52+
);
53+
// calculate the hash of the message
54+
// just like in the lit action
55+
const messageHash = ethers.utils.arrayify(
56+
ethers.utils.keccak256(ethers.utils.toUtf8Bytes(messageSigned))
57+
);
58+
const verified = ethers.utils.recoverPublicKey(messageHash, sig);
59+
console.log("verified: ", verified);
60+
console.log("sessionSigs.publicKey: ", "0x" + pkpPublicKey);
61+
if (verified === "0x" + pkpPublicKey) {
62+
console.log("Signature verified!");
63+
} else {
64+
console.log("Signature verification failed!");
65+
}
4466
} catch (error) {
4567
console.error(error);
4668
} finally {

dynamic-oracle/browser/src/litAction.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
export const litActionCode = `
22
(async () => {
33
const resp = await fetch(url).then((response) => response.json());
4-
const temp = resp.properties.periods[0].temperature;
4+
// console.log("resp: ", resp);
5+
const temperature = resp.properties.periods[0].temperature;
56
67
// 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 };
8+
// like the user's identity, or a nonce
9+
const messageToSign = { temperature, url };
910
const toSign = ethers.utils.arrayify(ethers.utils.keccak256(ethers.utils.toUtf8Bytes(JSON.stringify(messageToSign))));
1011
1112
// this requests a signature share from the Lit Node

0 commit comments

Comments
 (0)