|
| 1 | +# Encrypting a String for a Specific Solana Address |
| 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 [related doc page](https://developer.litprotocol.com/sdk/access-control/solana/siws-encryption) that covers a browser based implementation of this example 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 |
| 21 | +- A Solana private key |
| 22 | + - This private key will be used to sign the SIWS message |
| 23 | +- This code example uses Node.js, Yarn, and Deno please have these installed before running the example |
| 24 | + |
| 25 | +## Installation and Setup |
| 26 | + |
| 27 | +1. Clone the repository |
| 28 | +2. `cd` into the code example directory: `cd encryption/solana/encrypt-string` |
| 29 | +3. Install the dependencies: `yarn` |
| 30 | +4. Create and fill in the `.env` file: `cp .env.example .env` |
| 31 | + - `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 |
| 32 | + - `SOLANA_PRIVATE_KEY`: **Required** This is the Solana private key that will be used to sign the SIWS message |
| 33 | + - `LIT_ACTION_CAPACITY_CREDIT_TOKEN_ID`: **Optional** This is the ID of the Lit Capacity Credit to use for the PKP delegation Auth Sig |
| 34 | + |
| 35 | +## Executing the Example |
| 36 | + |
| 37 | +`yarn test` will execute [index.spec.ts](./test/index.spec.ts) which will run the code example and output the results to the terminal |
| 38 | + |
| 39 | +## Expected Output |
| 40 | + |
| 41 | +After running the test file, you should see the following output in your terminal: |
| 42 | + |
| 43 | +```bash |
| 44 | +yarn run v1.22.22 |
| 45 | +$ yarn build:lit-action && dotenvx run -- mocha test/**/*.spec.ts |
| 46 | +$ deno task bundle |
| 47 | +Task bundle deno run --allow-read --allow-write --allow-env --allow-net --allow-run esbuild.js |
| 48 | +[ [email protected]] injecting env (3) from .env |
| 49 | + |
| 50 | + Testing Lit decryption using a Solana SIWS message |
| 51 | +🔄 Getting Ethers signer... |
| 52 | +✅ Got Ethers signer |
| 53 | +🔄 Connecting LitNodeClient to the datil-dev network... |
| 54 | +✅ Connected LitNodeClient to the datil-dev network |
| 55 | +✅ Connected LitNodeClient to the datil-dev network |
| 56 | +🔄 Connecting LitContracts Client to the datil-dev network... |
| 57 | +✅ Connected LitContracts Client to the datil-dev network |
| 58 | +ℹ️ Using existing Lit Capacity Credit Token ID: 692 |
| 59 | +🔄 Minting PKP... |
| 60 | +✅ Minted PKP |
| 61 | +ℹ️ Minted PKP with token id: 17833928440929751777545130185334438733119955474517814532227088965326387869956 |
| 62 | +ℹ️ Minted PKP with public key: 0x0477b972154af49622dcc64673d32a2bb55059604e56ad1c0787b6be6a83f7d085e3e48bf0abc92914bbe613c6ed73b358e9ebcc40653b4f522fc68f15a8ca0749 |
| 63 | +ℹ️ Minted PKP with ETH address: 0xAb1B809A272521317D850eB7eB371C3175F0cD8f |
| 64 | +🔄 Creating capacityDelegationAuthSig... |
| 65 | +✅ Capacity Delegation Auth Sig created |
| 66 | +🔄 Generating access control conditions... |
| 67 | +✅ Generated access control conditions |
| 68 | +🔄 Encrypting the string... |
| 69 | +✅ Encrypted the string |
| 70 | +ℹ️ ciphertext: ti48snWgqNcoGN37St4orx/LBclc7nZPHxTnRGndIaNcKFgKg0M+M85ovCF39p9kH3SftJYpxHY6q85cZOUxHc9rYL5hxGUEBWBBIKy5I7g4UsA1IzTLaPUEtKpdUK1HaWH8XRg0WGKMWsknmsJrCDS55uzpsBPrMJZypUVKWfdqrHKdcOX20/IC |
| 71 | +ℹ️ dataToEncryptHash: 5ad8f16e45a2f21c693ea4e9376e46424abbf8f74838a5bd8f6173c54ba2e87a |
| 72 | +🔄 Generating SIWS message... |
| 73 | +✅ Generated SIWS message: |
| 74 | +localhost wants you to sign in with your Solana account: |
| 75 | +5ZS9h2jYtKVrPM19JSdgdaEE4UweSEQGgtwmfuFyqLan |
| 76 | + |
| 77 | +This is a test statement, replace this with whatever you want |
| 78 | + |
| 79 | +URI: http://localhost:3000 |
| 80 | +Expiration Time: 2024-12-18T03:00:42.887Z |
| 81 | +🔄 Signing SIWS message... |
| 82 | +✅ Signed SIWS message: 517Vr82ZUMmeCavpTebGrmqdP8aFpwZcr1DkT7MEnewuDsfcgHj16g6kCi1rrm4KdJC6AMTPvrDNKGRFnuYqUWq1 |
| 83 | +🔄 Getting session sigs... |
| 84 | +✅ Got session sigs |
| 85 | +🔄 Decrypting data... |
| 86 | +✅ Decrypted data |
| 87 | + ✔ should decrypt the string (4390ms) |
| 88 | + |
| 89 | + |
| 90 | + 1 passing (4s) |
| 91 | + |
| 92 | +✨ Done in 7.22s. |
| 93 | +``` |
| 94 | + |
| 95 | +## Specific Files to Reference |
| 96 | + |
| 97 | +- [index.ts](./src/index.ts): Contains the code for the example |
| 98 | +- [index.spec.ts](./test/index.spec.ts): Contains the code for the test file that runs the example |
| 99 | +- [utils.ts](./src/utils.ts): Contains the code for the utility functions used in the example |
| 100 | +- [litActionSessionSigs.ts](./src/litActions/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 |
| 101 | +- [litActionDecrypt.ts](./src/litActions/litActionDecrypt.ts): Contains the Lit Action code that makes the decryption request to the Lit Network |
| 102 | +- [common.ts](./src/litActions/common.ts): Contains helpers functions for generating SIWS messages and verifying SIWS signatures for the Lit Actions to use |
0 commit comments