Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions siws-encryption-lit-action-logic/browser/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VITE_ETHEREUM_PRIVATE_KEY=
18 changes: 18 additions & 0 deletions siws-encryption-lit-action-logic/browser/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
root: true,
env: { browser: true, es2020: true },
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:react-hooks/recommended",
],
ignorePatterns: ["dist", ".eslintrc.cjs"],
parser: "@typescript-eslint/parser",
plugins: ["react-refresh"],
rules: {
"react-refresh/only-export-components": [
"warn",
{ allowConstantExport: true },
],
},
};
24 changes: 24 additions & 0 deletions siws-encryption-lit-action-logic/browser/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
64 changes: 64 additions & 0 deletions siws-encryption-lit-action-logic/browser/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Encrypting Data with Sign-in With Solana, with custom Lit Action logic and conditions

This example demonstrates how to use Lit Protocol to encrypt and decrypt data based on authenticated Solana public keys, and custom Lit Action logic. The user is authenticated using SIWS, and then the Lit Action can run any additional checks using this authed user. In this example, these custom checks are not implemented, and you should implement them (here)[src/litActions/litActionSessionSigs.ts#L43].

This example leverages two key components:

1. Sign-in With Solana (SIWS) messages (following [Phantom's specification](https://github.com/phantom/sign-in-with-solana/tree/main))
2. Lit Actions

By combining these technologies, we create a secure system for data decryption tied to Solana wallet authentication, with custom Lit Action logic for additional checks.

## Prerequisites

- An Ethereum private key
- This private key will be used to:
- Run the Lit Action code. This private key does not require a balance or gas on any chain. However, to run Lit Actions on datil-test, and datil-prod, you do need to pay for each request to the Lit nodes. The easiest way to do this is to use the (Payment Delegation Database Contract system)[https://developer.litprotocol.com/paying-for-lit/payment-delegation-db] and add this wallet as a "Payee". In this case, the wallet will not need any Lit Test Tokens. Instead, the Lit Nodes will check if this wallet has a corresponding "Payer" that has enough Lit Test Tokens, and deduct payment from the Payer's balance.
- This code example uses Node.js, Yarn, and Deno please have these installed before running the example
- 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

## Installation and Setup

1. Clone the repository
2. `cd` into the code example directory: `cd siws-session-sigs/browser`
3. Install the dependencies: `yarn`
4. Create and fill in the `.env` file: `cp .env.example .env`
- `VITE_ETHEREUM_PRIVATE_KEY`: **Required** This is the Ethereum private key that will be used to run the Lit Action code. This private key will be exposed on the frontend, so it should not be a private key for a wallet with any real funds.
5. Build the Lit Action file: `yarn build:lit-action`
6. Start the development server: `yarn dev`

[!IMPORTANT]
The Lit Action code is compiled into the browser bundle, so you will need to rebuild the Lit Action code whenever you make changes to it. Run `yarn build:lit-action` to rebuild the Lit Action code any time you change the code in the `src/litActions/litActionDecrypt.ts` file.

## Executing the Example

1. Open the app in your browser: http://localhost:5173
2. Open the JavaScript browser console
3. Click the `Select Wallet` button and connect your Phantom wallet
4. Click the `Sign In` button to sign the SIWS message
5. Enter some text in the encryption input field and click the `Encrypt` button
6. Click the `Decrypt` button to decrypt the data

The following diagram provides an overview of how this code example works:

![Code Example Overview](./src/assets/siws-session-signatures.png)

### Expected Output

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.

After successful execution, you should see `✅ Got Session sigs` in the JavaScript console and `Session Sigs generated successfully` on the web page:

![Successful execution](./src/assets/successful-execution.png)

## Specific Files to Reference

- [App.tsx](./src/App.tsx): Contains the frontend code and logic for the example
- [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
- [litSiws.ts](./src/litSiws.ts): Contains the code for:
- Minting a Capacity Credit if none was specified in the `.env` file
- Generating the Capacity Credit delegation Auth Sig
- Minting a PKP if none was specified in the `.env` file
- Adding the permitted Auth Methods to the PKP
- Executing the Lit Action to authenticate the SIWS message and generate Session Signatures
- [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
5 changes: 5 additions & 0 deletions siws-encryption-lit-action-logic/browser/deno.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"tasks": {
"bundle": "deno run --allow-read --allow-write --allow-env --allow-net --allow-run esbuild.js"
}
}
88 changes: 88 additions & 0 deletions siws-encryption-lit-action-logic/browser/deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions siws-encryption-lit-action-logic/browser/esbuild-shims.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
globalThis.process = {
env: {},
};
15 changes: 15 additions & 0 deletions siws-encryption-lit-action-logic/browser/esbuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as esbuild from "https://deno.land/x/esbuild@v0.20.1/mod.js";
import { denoPlugins } from "jsr:@luca/esbuild-deno-loader@^0.10.3";

esbuild.build({
plugins: [...denoPlugins()],
entryPoints: ["src/litActions/litActionDecrypt.ts"],
outdir: "src/litActions/dist/",
bundle: true,
platform: "browser",
format: "esm",
target: "esnext",
minify: false,
inject: ["./esbuild-shims.js"],
});
await esbuild.stop();
27 changes: 27 additions & 0 deletions siws-encryption-lit-action-logic/browser/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
<style>
body,
html {
margin: 0;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
#root {
width: 100%;
text-align: center;
}
</style>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
53 changes: 53 additions & 0 deletions siws-encryption-lit-action-logic/browser/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "siws-encryption-lit-action-logic-browser",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc -b && vite build",
"build:lit-action": "deno task bundle",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
"dependencies": {
"@esbuild-plugins/node-globals-polyfill": "^0.2.3",
"@esbuild-plugins/node-modules-polyfill": "^0.2.2",
"@lit-protocol/auth-browser": "^7.0.4",
"@lit-protocol/auth-helpers": "^7.0.4",
"@lit-protocol/constants": "^7.0.4",
"@lit-protocol/contracts-sdk": "^7.0.4",
"@lit-protocol/lit-node-client": "^7.0.4",
"@simplewebauthn/browser": "^10.0.0",
"@solana/wallet-adapter-base": "^0.9.23",
"@solana/wallet-adapter-react": "^0.15.35",
"@solana/wallet-adapter-react-ui": "^0.9.35",
"@solana/wallet-adapter-wallets": "^0.19.32",
"@solana/wallet-standard-features": "^1.2.0",
"@solana/wallet-standard-util": "^1.1.1",
"@solana/web3.js": "1.95.3",
"@vitejs/plugin-react-swc": "^3.7.0",
"ethers": "v5",
"ipfs-only-hash": "^4.0.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"rollup-plugin-polyfill-node": "^0.13.0",
"vite-plugin-node-polyfills": "^0.22.0"
},
"devDependencies": {
"@lit-protocol/types": "^7.0.4",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@typescript-eslint/eslint-plugin": "^7.13.1",
"@typescript-eslint/parser": "^7.13.1",
"@vitejs/plugin-react": "^4.3.1",
"esbuild": "^0.24.0",
"eslint": "^8.57.0",
"eslint-plugin-react-hooks": "^4.6.2",
"eslint-plugin-react-refresh": "^0.4.7",
"rollup": "^4.28.0",
"typescript": "^5.5.3",
"typestub-ipfs-only-hash": "^4.0.0",
"vite": "^5.3.1"
}
}
6 changes: 6 additions & 0 deletions siws-encryption-lit-action-logic/browser/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "siws-encryption-browser",
"root": "siws-encryption/browser",
"targets": {
}
}
1 change: 1 addition & 0 deletions siws-encryption-lit-action-logic/browser/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions siws-encryption-lit-action-logic/browser/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#root {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}

.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.react:hover {
filter: drop-shadow(0 0 2em #61dafbaa);
}

@keyframes logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}

@media (prefers-reduced-motion: no-preference) {
a:nth-of-type(2) .logo {
animation: logo-spin infinite 20s linear;
}
}

.card {
padding: 2em;
}

.read-the-docs {
color: #888;
}
Loading
Loading