|
| 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 | +} |
0 commit comments