Using Lit to Connect OpenAI and Solana
Connect Lit, Solana, and OpenAI.
In this post, we'll demonstrate how you can use Lit to securely integrate OpenAI and Solana. Specifically, we'll provide an example of using a Wrapped Key on the Solana blockchain to sign a response generated using the OpenAI API within a Lit Action.
Use Cases
By combining OpenAI's powerful language models with the security and decentralization of the Solana blockchain, you can build a variety of innovative applications:
- AI-Powered Autonomous Agents: Create AI-powered bots that operate on the blockchain, providing assistance or executing tasks without exposing sensitive API or private keys. Using Lit's threshold-based Programmable Key Pairs (PKPs) and Trusted Execution Environments (TEE), enable autonomous AI agents to interact with off-chain data and services to perform on-chain actions. PKPs enable AI agents to operate with decentralized identities, ensuring no single party has direct control over the agent's wallet or decision-making capabilities.
- All sensitive operations and keys remain protected within the TEE present in each Lit node
- PKPs serve as secure identities for AI agents on ECDSA-based chains (Ethereum, Polygon, etc.)
- Can interact with both blockchain and traditional web services
- Execute complex multi-step operations and make on-chain decisions autonomously
- Agents can coordinate actions across multiple protocols
- Private Compute and Data Processing: Encrypting data and executing LLM prompts within Lit’s TEE, and returning the re-encrypted result. This enables the ability of LLM to perform computation over private data, so long as the data being sent to the LLM API isn't exposed in transit or in its execution.
- The encryption process uses standardized cryptographic protocols to secure data before it enters the TEE
- The TEE provides hardware-level isolation, ensuring that even the node operators cannot access the decrypted data
- All computations, including the LLM API calls, happen within this secure environment
- Results are re-encrypted before leaving the TEE, maintaining end-to-end encryption
- This enables secure processing of sensitive information like medical records, financial data, or personal information
- Proofs for Training and Inference: Generate cryptographic proofs that data was produced by an LLM through Lit Actions and PKPs. By restricting PKP signing permissions to specific IPFS CID hashes containing the LLM API calls and verification logic, you can cryptographically guarantee the authenticity of LLM-generated content. This same logic could also be used to sign training data being passed to an LLM, ensuring authenticity of the data and verifying it’s source.
- PKPs (Programmable Key Pairs) act as cryptographic validators
- The Lit Action contains the verification logic and is immutably stored on IPFS
- The signing process creates a verifiable chain of custody for the LLM output
- The proof system enables third parties to verify the authenticity of LLM-generated content
- This is particularly useful for audit trails, compliance requirements, and preventing unauthorized modifications
Key Concepts
Before we dive into the code, let's briefly introduce some key concepts:
- Lit Actions: JavaScript functions that run on the Lit network within each Lit node. They allow you to perform operations like signing transactions or encrypting/decrypting data privately and securely.
- Programmable Key Pairs (PKPs): Threshold keys generated collectively by the Lit nodes that can be used to sign messages and transactions.
- Wrapped Keys: Cryptographic keys that are encrypted and stored securely within Lit, only accessible under certain conditions defined using Access Control Conditions (ACCs).
Sample Integration
To demonstrate what is possible using Solana, OpenAI, and Lit within a single application, we’ve created a simple integration in this GitHub Repository. Below is an overview of how the code is organized.
Overview
- Initialize Ethereum Wallet
- Import your Ethereum private key.
- Connect to the Lit RPC endpoint (Chronicle Yellowstone).
- Connect to Lit Network
- Use
LitNodeClient
to connect to the Lit network (datil-dev
in this example).
- Use
- Initialize Lit Contracts
- Connect the
LitContracts
client to the Lit network.
- Connect the
- Mint a PKP (If Not Provided)
- If you haven't provided a PKP, the script mints a new one.
- The minted PKP will be used to create new Wrapped Keys.
- Create AuthMethod
- Use your Ethereum wallet to create an
AuthMethod
. - This method is used for authentication in generating Session Signatures for the PKP.
- Use your Ethereum wallet to create an
- Generate a Solana Wrapped Key
- Create a new Solana Wrapped Key. This Wrapped Key is owned by the PKP.
- Retrieve the encrypted private key of the Wrapped Key.
- Set Up Access Control Conditions (ACCs)
- ACCs are defined to encrypt your OpenAI API key securely.
- The ACCs ensure the API key is not revealed and is only accessible under specified conditions.
- Prepare the Prompt
- Write a simple prompt that you want the AI to answer.
- Run the Lit Action
- Provide the encrypted Solana Wrapped Key and OpenAI API key metadata, along with your prompt.
- The Lit Action executes within the TEE.
Lit Action Overview
Within the Lit Action, the following steps occur:
- Decrypt Wrapped Keys
- The Solana Wrapped Key and OpenAI API key are decrypted securely.
- Process the Solana Private Key
- The salt is removed from the decrypted Solana private key.
- Interact with OpenAI
- The prompt is sent to OpenAI's API.
- The response is received securely within the TEE.
- Generate a Solana Key Pair
- A Solana key pair is generated from the decrypted private key.
- Sign the OpenAI Response
- The Solana key pair signs the OpenAI response.
- This signature can be used to verify the authenticity of the response.
- Validate the Signature
- Ensure the signature is valid and corresponds to the original response.
Conclusion
By following this tutorial, you've learned how to securely integrate OpenAI's API with the Solana blockchain using Lit Actions. This approach ensures that sensitive keys remain private and secure while enabling powerful interactions between AI, off-chain data, and blockchain applications.
Next Steps
- Explore More with Lit Protocol: Dive deeper into Lit Actions, PKPs, and Wrapped Keys.
- Build Your Own dApp: Use this example as a starting point to build your decentralized application.
- Contribute: Join the community on Telegram and contribute to the Lit Protocol ecosystem.