|
| 1 | +# Encrypting Data with Sign-in With Solana |
| 2 | + |
| 3 | +This example demonstrates how to use Lit Protocol to encrypt and decrypt data based on authenticated Solana public keys. It leverages three key components: |
| 4 | + |
| 5 | +1. Lit Access Control Conditions |
| 6 | +2. Sign-in With Solana (SIWS) messages (following [Phantom's specification](https://github.com/phantom/sign-in-with-solana/tree/main)) |
| 7 | +3. Lit Actions |
| 8 | + |
| 9 | +By combining these technologies, we create a secure system for data decryption tied to Solana wallet authentication. |
| 10 | + |
| 11 | +This code example has a [corresponding doc page](https://developer.litprotocol.com/sdk/access-control/solana/siws-encryption) that covers the implementation in more detail, this repository acts as a reference implementation for you to use as a guide for restricting data decryption to specific Solana public keys in your project. |
| 12 | + |
| 13 | +## Prerequisites |
| 14 | + |
| 15 | +- An Ethereum private key |
| 16 | + - This private key will be used to: |
| 17 | + - Mint a Lit Capacity Credit if none was specific in the project's `.env` file |
| 18 | + - In order to pay for this, the corresponding Ethereum account must have Lit Test Tokens. If you do not have any, you can get some from [the faucet](https://chronicle-yellowstone-faucet.getlit.dev/) |
| 19 | + - Create a Lit Capacity Credit delegation Auth Sig |
| 20 | + - Mint a PKP if none was specific in the project's `.env` file |
| 21 | +- This code example uses Node.js, Yarn, and Deno please have these installed before running the example |
| 22 | +- The code example also expects the [Phantom wallet browser extension](https://chromewebstore.google.com/detail/phantom/bfnaelmomeimhlpmgjnjophhpkkoljpa?hl=en) to be installed and setup with a Solana wallet |
| 23 | + |
| 24 | +## Installation and Setup |
| 25 | + |
| 26 | +1. Clone the repository |
| 27 | +2. `cd` into the code example directory: `cd siws-session-sigs/browser` |
| 28 | +3. Install the dependencies: `yarn` |
| 29 | +4. Create and fill in the `.env` file: `cp .env.example .env` |
| 30 | + - `VITE_ETHEREUM_PRIVATE_KEY`: **Required** This is the Ethereum private key that will be used to mint a Lit Capacity Credit and create Lit Session Signatures |
| 31 | + - `VITE_LIT_CAPACITY_CREDIT_TOKEN_ID`: **Optional** This is the ID of the Lit Capacity Credit to use for the PKP delegation Auth Sig |
| 32 | + - `VITE_LIT_PKP_PUBLIC_KEY`: **Optional** This is the public key of the PKP to use for generating Session Signatures |
| 33 | + - `VITE_LIT_PKP_TOKEN_ID`: **Optional** This is the ID of the PKP to use for generating Session Signatures |
| 34 | +5. Build the Lit Action file: `yarn build:lit-action` |
| 35 | +6. Start the development server: `yarn dev` |
| 36 | + |
| 37 | +## Executing the Example |
| 38 | + |
| 39 | +1. Open the app in your browser: http://localhost:5173 |
| 40 | +2. Open the JavaScript browser console |
| 41 | +3. Click the `Select Wallet` button and connect your Phantom wallet |
| 42 | +4. Click the `Sign In` button to sign the SIWS message |
| 43 | +5. Enter some text in the encryption input field and click the `Encrypt` button |
| 44 | +6. Click the `Decrypt` button to decrypt the data |
| 45 | + |
| 46 | +The following diagram provides an overview of how this code example works: |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +### Expected Output |
| 51 | + |
| 52 | +After clicking the `Sign In` button, the code example will submit your signed SIWS message to the Lit network to be authenticated using a Lit Action and will generate Session Signatures using the minted PKP if the signing Solana public key is authorized to use the PKP. |
| 53 | + |
| 54 | +After successful execution, you should see `✅ Got Session sigs` in the JavaScript console and `Session Sigs generated successfully` on the web page: |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +## Specific Files to Reference |
| 59 | + |
| 60 | +- [App.tsx](./src/App.tsx): Contains the frontend code and logic for the example |
| 61 | +- [SignInButton.tsx](./src/SignInButton.tsx): Contains the code for the `Sign In` button that creates and submits the SIWS message to the browser wallet extension |
| 62 | +- [litSiws.ts](./src/litSiws.ts): Contains the code for: |
| 63 | + - Minting a Capacity Credit if none was specified in the `.env` file |
| 64 | + - Generating the Capacity Credit delegation Auth Sig |
| 65 | + - Minting a PKP if none was specified in the `.env` file |
| 66 | + - Adding the permitted Auth Methods to the PKP |
| 67 | + - Executing the Lit Action to authenticate the SIWS message and generate Session Signatures |
| 68 | +- [litActionSessionSigs.ts](./src/litActionSessionSigs.ts): Contains the Lit Action code that authenticates the SIWS message, checks the PKPs permitted Auth Methods, and signals the Lit Network to generate Session Signatures |
0 commit comments