|
| 1 | +import { LitNodeClient, encryptString } from "@lit-protocol/lit-node-client"; |
| 2 | +import { LitNetwork, LIT_RPC } from "@lit-protocol/constants"; |
| 3 | +import { |
| 4 | + LitAbility, |
| 5 | + LitActionResource, |
| 6 | + LitPKPResource, |
| 7 | +} from "@lit-protocol/auth-helpers"; |
| 8 | +import { LitContracts } from "@lit-protocol/contracts-sdk"; |
| 9 | +import { AccessControlConditions } from "@lit-protocol/types"; |
| 10 | +import { EthWalletProvider } from "@lit-protocol/lit-auth-client"; |
| 11 | +import { LIT_NETWORKS_KEYS } from "@lit-protocol/types"; |
| 12 | +import { api } from "@lit-protocol/wrapped-keys"; |
| 13 | +import { getEncryptedKey } from "@lit-protocol/wrapped-keys/src/lib/api"; |
| 14 | +import fs from "node:fs"; |
| 15 | +import * as ethers from "ethers"; |
| 16 | + |
| 17 | +import { getEnv, mintPkp } from "./utils"; |
| 18 | +const litActionCode = fs.readFileSync("src/litAction.bundle.js", "utf8"); |
| 19 | + |
| 20 | +const { generatePrivateKey } = api; |
| 21 | + |
| 22 | +const ETHEREUM_PRIVATE_KEY = getEnv("ETHEREUM_PRIVATE_KEY"); |
| 23 | +const OPENAI_API_KEY = getEnv("OPENAI_API_KEY"); |
| 24 | +const LIT_PKP_PUBLIC_KEY = process.env["LIT_PKP_PUBLIC_KEY"]; |
| 25 | +const LIT_NETWORK = process.env["LIT_NETWORK"] as LIT_NETWORKS_KEYS || LitNetwork.DatilDev; |
| 26 | + |
| 27 | +export const solanaOpenAI = async () => { |
| 28 | + let litNodeClient: LitNodeClient; |
| 29 | + let pkpInfo: { |
| 30 | + tokenId?: string; |
| 31 | + publicKey?: string; |
| 32 | + ethAddress?: string; |
| 33 | + } = { |
| 34 | + publicKey: LIT_PKP_PUBLIC_KEY, |
| 35 | + }; |
| 36 | + |
| 37 | + try { |
| 38 | + const ethersWallet = new ethers.Wallet( |
| 39 | + ETHEREUM_PRIVATE_KEY, |
| 40 | + new ethers.providers.JsonRpcProvider(LIT_RPC.CHRONICLE_YELLOWSTONE) |
| 41 | + ); |
| 42 | + |
| 43 | + console.log("🔄 Connecting to the Lit network..."); |
| 44 | + litNodeClient = new LitNodeClient({ |
| 45 | + litNetwork: LIT_NETWORK, |
| 46 | + debug: false, |
| 47 | + }); |
| 48 | + await litNodeClient.connect(); |
| 49 | + console.log("✅ Connected to the Lit network"); |
| 50 | + |
| 51 | + console.log("🔄 Connecting LitContracts client to network..."); |
| 52 | + const litContracts = new LitContracts({ |
| 53 | + signer: ethersWallet, |
| 54 | + network: LIT_NETWORK, |
| 55 | + debug: false, |
| 56 | + }); |
| 57 | + await litContracts.connect(); |
| 58 | + console.log("✅ Connected LitContracts client to network"); |
| 59 | + |
| 60 | + if (LIT_PKP_PUBLIC_KEY === undefined || LIT_PKP_PUBLIC_KEY === "") { |
| 61 | + console.log("🔄 PKP wasn't provided, minting a new one..."); |
| 62 | + pkpInfo = (await mintPkp(ethersWallet)) as { |
| 63 | + tokenId?: string; |
| 64 | + publicKey?: string; |
| 65 | + ethAddress?: string; |
| 66 | + }; |
| 67 | + console.log("✅ PKP successfully minted"); |
| 68 | + console.log(`ℹ️ PKP token ID: ${pkpInfo.tokenId}`); |
| 69 | + console.log(`ℹ️ PKP public key: ${pkpInfo.publicKey}`); |
| 70 | + console.log(`ℹ️ PKP ETH address: ${pkpInfo.ethAddress}`); |
| 71 | + } else { |
| 72 | + console.log(`ℹ️ Using provided PKP: ${LIT_PKP_PUBLIC_KEY}`); |
| 73 | + pkpInfo = { |
| 74 | + publicKey: LIT_PKP_PUBLIC_KEY, |
| 75 | + ethAddress: ethers.utils.computeAddress(`0x${LIT_PKP_PUBLIC_KEY}`), |
| 76 | + }; |
| 77 | + } |
| 78 | + |
| 79 | + console.log("🔄 Creating AuthMethod using the ethersSigner..."); |
| 80 | + const authMethod = await EthWalletProvider.authenticate({ |
| 81 | + signer: ethersWallet, |
| 82 | + litNodeClient, |
| 83 | + }); |
| 84 | + console.log("✅ Finished creating the AuthMethod"); |
| 85 | + |
| 86 | + console.log("🔄 Getting the Session Signatures..."); |
| 87 | + const pkpSessionSigs = await litNodeClient.getPkpSessionSigs({ |
| 88 | + pkpPublicKey: pkpInfo.publicKey!, |
| 89 | + chain: "ethereum", |
| 90 | + authMethods: [authMethod], |
| 91 | + expiration: new Date(Date.now() + 1000 * 60 * 10).toISOString(), // 10 minutes |
| 92 | + resourceAbilityRequests: [ |
| 93 | + { |
| 94 | + resource: new LitActionResource("*"), |
| 95 | + ability: LitAbility.LitActionExecution, |
| 96 | + }, |
| 97 | + { |
| 98 | + resource: new LitPKPResource("*"), |
| 99 | + ability: LitAbility.PKPSigning, |
| 100 | + }, |
| 101 | + ], |
| 102 | + }); |
| 103 | + console.log("✅ Generated the Session Signatures"); |
| 104 | + |
| 105 | + console.log("🔄 Generating wrapped key..."); |
| 106 | + const response = await generatePrivateKey({ |
| 107 | + pkpSessionSigs, |
| 108 | + network: "solana", |
| 109 | + memo: "This is a Dev Guide code example testing Solana key", |
| 110 | + litNodeClient, |
| 111 | + }); |
| 112 | + console.log(`✅ Generated wrapped key with id: ${response.id} and public key: ${response.generatedPublicKey}`); |
| 113 | + |
| 114 | + const { |
| 115 | + ciphertext: solanaCipherText, |
| 116 | + dataToEncryptHash: solanaDataToEncryptHash, |
| 117 | + } = await getEncryptedKey({ |
| 118 | + pkpSessionSigs, |
| 119 | + litNodeClient, |
| 120 | + id: response.id, |
| 121 | + }); |
| 122 | + |
| 123 | + const accessControlConditions: AccessControlConditions = [ |
| 124 | + { |
| 125 | + contractAddress: "", |
| 126 | + standardContractType: "", |
| 127 | + chain: "ethereum", |
| 128 | + method: "", |
| 129 | + parameters: [":userAddress"], |
| 130 | + returnValueTest: { |
| 131 | + comparator: "=", |
| 132 | + value: pkpInfo.ethAddress!, |
| 133 | + }, |
| 134 | + }, |
| 135 | + ]; |
| 136 | + |
| 137 | + const { |
| 138 | + ciphertext: apiKeyCipherText, |
| 139 | + dataToEncryptHash: apiKeyDataToEncryptHash, |
| 140 | + } = await encryptString( |
| 141 | + { |
| 142 | + accessControlConditions: accessControlConditions, |
| 143 | + dataToEncrypt: OPENAI_API_KEY, |
| 144 | + }, |
| 145 | + litNodeClient |
| 146 | + ); |
| 147 | + |
| 148 | + const prompt = "Should I buy DogeCoin?"; |
| 149 | + |
| 150 | + console.log("🔄 Executing the Lit Action..."); |
| 151 | + const litActionResponse = await litNodeClient.executeJs({ |
| 152 | + sessionSigs: pkpSessionSigs, |
| 153 | + code: litActionCode, |
| 154 | + jsParams: { |
| 155 | + accessControlConditions, |
| 156 | + solanaCipherText, |
| 157 | + solanaDataToEncryptHash, |
| 158 | + apiKeyCipherText, |
| 159 | + apiKeyDataToEncryptHash, |
| 160 | + prompt |
| 161 | + }, |
| 162 | + }); |
| 163 | + console.log("✅ Executed the Lit Action"); |
| 164 | + console.log(litActionResponse); |
| 165 | + |
| 166 | + return litActionResponse; |
| 167 | + } catch (error) { |
| 168 | + console.error(error); |
| 169 | + } finally { |
| 170 | + litNodeClient!.disconnect(); |
| 171 | + } |
| 172 | +}; |
0 commit comments