Skip to content

Commit 7ef9120

Browse files
Merge pull request #89 from LIT-Protocol/wyatt/v2
WIP v2 sync
2 parents 2e49d60 + cdd0848 commit 7ef9120

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+6335
-575
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ETHEREUM_PRIVATE_KEY=
2+
SOLANA_PRIVATE_KEY=
3+
LIT_ACTION_CAPACITY_CREDIT_TOKEN_ID=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
.env
11+
node_modules
12+
dist
13+
dist-ssr
14+
*.local
15+
16+
# Editor directories and files
17+
.vscode/*
18+
!.vscode/extensions.json
19+
.idea
20+
.DS_Store
21+
*.suo
22+
*.ntvs*
23+
*.njsproj
24+
*.sln
25+
*.sw?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"$schema": "https://json.schemastore.org/mocharc.json",
3+
"require": "tsx"
4+
}
+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"tasks": {
3+
"bundle": "deno run --allow-read --allow-write --allow-env --allow-net --allow-run esbuild.js"
4+
}
5+
}

encryption/solana/encrypt-string/deno.lock

+75
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
globalThis.process = {
2+
env: {},
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as esbuild from 'https://deno.land/x/[email protected]/mod.js';
2+
import { denoPlugins } from 'jsr:@luca/esbuild-deno-loader@^0.10.3';
3+
4+
esbuild.build({
5+
plugins: [...denoPlugins()],
6+
entryPoints: [
7+
'src/litActions/litActionSessionSigs.ts',
8+
'src/litActions/litActionDecrypt.ts',
9+
],
10+
outdir: 'src/litActions/dist/',
11+
bundle: true,
12+
platform: 'node',
13+
format: 'esm',
14+
target: 'esnext',
15+
minify: false,
16+
inject: ['./esbuild-shims.js'],
17+
});
18+
await esbuild.stop();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "solana-encrypt-string",
3+
"version": "0.1.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"type": "module",
7+
"engines": {
8+
"deno": ">=1.42.0"
9+
},
10+
"scripts": {
11+
"build:lit-action": "deno task bundle",
12+
"test": "yarn build:lit-action && dotenvx run -- mocha test/**/*.spec.ts"
13+
},
14+
"dependencies": {
15+
"@dotenvx/dotenvx": "^0.44.1",
16+
"@lit-protocol/auth-helpers": "^7.0.2",
17+
"@lit-protocol/constants": "^7.0.2",
18+
"@lit-protocol/contracts-sdk": "^7.0.2",
19+
"@lit-protocol/lit-node-client": "^7.0.2",
20+
"@solana/web3.js": "^1.98.0",
21+
"bs58": "^6.0.0",
22+
"ethers": "v5",
23+
"tweetnacl": "^1.0.3",
24+
"typestub-ipfs-only-hash": "^4.0.0"
25+
},
26+
"devDependencies": {
27+
"@esbuild-plugins/node-globals-polyfill": "^0.2.3",
28+
"@esbuild-plugins/node-modules-polyfill": "^0.2.2",
29+
"@lit-protocol/types": "^7.0.2",
30+
"@types/chai": "^4.3.16",
31+
"@types/chai-json-schema": "^1.4.10",
32+
"@types/mocha": "^10.0.6",
33+
"chai": "^5.1.1",
34+
"chai-json-schema": "^1.5.1",
35+
"esbuild": "^0.24.0",
36+
"mocha": "^10.4.0",
37+
"tsc": "^2.0.4",
38+
"tsx": "^4.12.0",
39+
"typescript": "^5.4.5"
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "solana-encrypt-string",
3+
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
4+
"projectType": "library",
5+
"sourceRoot": "encryption/solana/encrypt-string/src",
6+
"// targets": "to see all targets run: nx show project encrypt-string --web",
7+
"targets": {}
8+
}

0 commit comments

Comments
 (0)