|
| 1 | +# Sign-in With Solana Authentication |
| 2 | + |
| 3 | +This code example demonstrates how to authenticate a Sign-in With Solana (SIWS) message using a Lit Action and Phantom's [SIWS specification](https://github.com/phantom/sign-in-with-solana/tree/main?tab=readme-ov-file). With this functionality, you can enforce that only specific Solana public keys (aka. addresses) are able to perform certain actions like decrypting data encrypted using Lit, signing transactions using a specific PKP and more. |
| 4 | + |
| 5 | +This code example has a [corresponding doc page](https://developer.litprotocol.com/sdk/authentication/authenticating-siws) that covers the implementation in more detail, this repository acts as a reference implementation for you to use as a guide for authenticating SIWS messages in your project. |
| 6 | + |
| 7 | +## Prerequisites |
| 8 | + |
| 9 | +- An Ethereum private key |
| 10 | + - This private key will be used to: |
| 11 | + - Mint a Lit Capacity Credit if none was specific in the project's `.env` file |
| 12 | + - 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/) |
| 13 | + - Create a Lit Capacity Credit delegation Auth Sig |
| 14 | +- This code example uses Node.js and Yarn, please have these installed before running the example |
| 15 | +- 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 |
| 16 | + |
| 17 | +## Installation and Setup |
| 18 | + |
| 19 | +1. Clone the repository |
| 20 | +2. `cd` into the code example directory: `cd siws-session-sigs/browser` |
| 21 | +3. Install the dependencies: `yarn` |
| 22 | +4. Create and fill in the `.env` file: `cp .env.example .env` |
| 23 | + - `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 |
| 24 | + - `VITE_LIT_CAPACITY_CREDIT_TOKEN_ID`: **Optional** This is the ID of the Lit Capacity Credit to use for the PKP delegation Auth Sig |
| 25 | + - `VITE_LIT_NETWORK`: **Optional** This is the Lit Network to use for the Lit Contracts and Lit Node Clients |
| 26 | +5. Build the Lit Action file: `yarn build:lit-action` |
| 27 | +6. Start the development server: `yarn dev` |
| 28 | + |
| 29 | +## Executing the Example |
| 30 | + |
| 31 | +1. Open the app in your browser: http://localhost:5173 |
| 32 | +2. Open the JavaScript browser console |
| 33 | +3. Click the `Select Wallet` button and connect your Phantom wallet |
| 34 | +4. Click the `Sign In` button to sign the SIWS message |
| 35 | + |
| 36 | +The following diagram provides an overview of how this code example works: |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +### Expected Output |
| 41 | + |
| 42 | +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 return the authenticated Solana public key. |
| 43 | + |
| 44 | +After successful execution, you should see `✅ Authenticated SIWS message` in the JavaScript console and the authenticated Solana public key on the web page: |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +## Specific Files to Reference |
| 49 | + |
| 50 | +- [App.tsx](./src/App.tsx): Contains the frontend code and logic for the example |
| 51 | +- [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 |
| 52 | +- [litSiws.ts](./src/litSiws.ts): Contains the code for: |
| 53 | + - Minting a Capacity Credit if none was specified in the `.env` file |
| 54 | + - Generating the Capacity Credit delegation Auth Sig |
| 55 | + - Executing the Lit Action to authenticate the SIWS message |
| 56 | +- [litActionSiwsAuth.ts](./src/litActionSiwsAuth.ts): Contains the Lit Action code that authenticates the SIWS message |
0 commit comments