|
| 1 | +<!-- omit in toc --> |
| 2 | +# Authenticating a Session with Lit |
| 3 | + |
| 4 | +In order to use your [Lit Resources and their corresponding abilities](../README.md#lit-resources-and-abilities), you need to authenticate yourself with the Lit Network. After your identity has been authenticated, the Lit nodes can then verify whether you have the necessary permissions to access a given Lit Resource and its associated abilities when you make requests to the Lit Network (such as decrypting data, signing transactions with a PKP, or executing a Lit Action). |
| 5 | + |
| 6 | +As covered [here](../README.md#session-signatures) in the Getting Started `README`, Sessions are the means by which you authenticate with the Lit Network to interact with Lit Resources securely without repeatedly signing transactions with your wallet. This guide will walk you through the process of authenticating a session, which involves: |
| 7 | + |
| 8 | +1. Generating a Session Key Pair |
| 9 | +2. Creating an Authentication Signature to authorize your Session Key to use Lit Resources you have access to |
| 10 | +3. Requesting the Lit network to generate Session Signatures for your Session, authorizing it to perform actions with the specified Lit Resources |
| 11 | + |
| 12 | +<!-- omit in toc --> |
| 13 | +## Table of Contents |
| 14 | + |
| 15 | +- [Prerequisites](#prerequisites) |
| 16 | +- [Running the Code Example](#running-the-code-example) |
| 17 | + - [Requirements](#requirements) |
| 18 | + - [Steps](#steps) |
| 19 | + - [Expected Output](#expected-output) |
| 20 | +- [Understanding the Code](#understanding-the-code) |
| 21 | + - [Creating an Ethers Signer](#creating-an-ethers-signer) |
| 22 | + - [Requesting Session Signatures](#requesting-session-signatures) |
| 23 | + |
| 24 | + |
| 25 | +## Prerequisites |
| 26 | + |
| 27 | +- Understanding of Lit core terminology and concepts covered [here](../README.md#core-terminology) |
| 28 | +- Understanding of the [Connecting to the Lit Network](../connecting-to-lit/README.md) guide |
| 29 | + |
| 30 | +## Running the Code Example |
| 31 | + |
| 32 | +### Requirements |
| 33 | + |
| 34 | +- [Node.js](https://nodejs.org/en) |
| 35 | +- [Yarn](https://yarnpkg.com/getting-started) |
| 36 | +- `@lit-protocol/constants` |
| 37 | +- `@lit-protocol/lit-node-client` |
| 38 | +- `@lit-protocol/auth-helpers` |
| 39 | + |
| 40 | +### Steps |
| 41 | + |
| 42 | +1. `yarn` to install the dependencies |
| 43 | +2. `yarn test` to run the code example |
| 44 | + |
| 45 | +### Expected Output |
| 46 | + |
| 47 | +After running the code example, you should see output in your terminal indicating that a connection to the Lit Network was successfully established, and that session signatures were successfully generated for the requested session: |
| 48 | + |
| 49 | +```bash |
| 50 | +[Lit-JS-SDK v6.11.0] [2024-11-15T05:58:06.815Z] [DEBUG] [core] signatures: { |
| 51 | + 'https://15.235.83.220:7470': { |
| 52 | + sig: 'cadda38603f3cc62b83f043ede4fd758a9aee7363aa8c0b510658aa43d0e955942e63629029117f89973999956c4d2c20f666cc7af124661ebaeb1d4cf7bc00f', |
| 53 | + derivedVia: 'litSessionSignViaNacl', |
| 54 | + signedMessage: `{"sessionKey":"963d6551d36a8b5be53d8959d45141764dbfd3c00649f56bc00433ef09db75f5","resourceAbilityRequests":[{"resource":{"resource":"*","resourcePrefix":"lit-accesscontrolcondition"},"ability":"access-control-condition-decryption"}],"capabilities":[{"sig":"0x3a18598d9dbcb4c3f588d3948803f34397790f6b3ea1620913e353a002fb5526032aad72400635419346ca91a9bcfcd36cbdfc5b0f5080ecc48ded6ccfe6da7b1b","derivedVia":"web3.eth.personal.sign","signedMessage":"localhost wants you to sign in with your Ethereum account:\\n0xA89543a7145C68E52a4D584f1ceb123605131211\\n\\nThis is a test statement. You can put anything you want here. I further authorize the stated URI to perform the following actions on my behalf: (1) 'Threshold': 'Decryption' for 'lit-accesscontrolcondition://*'.\\n\\nURI: lit:session:963d6551d36a8b5be53d8959d45141764dbfd3c00649f56bc00433ef09db75f5\\nVersion: 1\\nChain ID: 1\\nNonce: 0x23f22526f00d01dc505e291881a44a8c74664f76cdaa2662b8af1ae54c9b4725\\nIssued At: 2024-11-15T05:58:06.757Z\\nExpiration Time: 2024-11-15T06:08:06.728Z\\nResources:\\n- urn:recap:eyJhdHQiOnsibGl0LWFjY2Vzc2NvbnRyb2xjb25kaXRpb246Ly8qIjp7IlRocmVzaG9sZC9EZWNyeXB0aW9uIjpbe31dfX0sInByZiI6W119","address":"0xA89543a7145C68E52a4D584f1ceb123605131211"}],"issuedAt":"2024-11-15T05:58:06.778Z","expiration":"2024-11-15T06:08:06.728Z","nodeAddress":"https://15.235.83.220:7470"}`, |
| 55 | + address: '963d6551d36a8b5be53d8959d45141764dbfd3c00649f56bc00433ef09db75f5', |
| 56 | + algo: 'ed25519' |
| 57 | + }, |
| 58 | + 'https://15.235.83.220:7472': { |
| 59 | + sig: 'fccb3a82b02f1f3c9ba66fc8b3a61b0404875baf95449c76727529970105b3d96a2de9869a6cefd7c510ee8ecc55d7345e2b469f30ae59511579c703d2ab3a0c', |
| 60 | + derivedVia: 'litSessionSignViaNacl', |
| 61 | + signedMessage: `{"sessionKey":"963d6551d36a8b5be53d8959d45141764dbfd3c00649f56bc00433ef09db75f5","resourceAbilityRequests":[{"resource":{"resource":"*","resourcePrefix":"lit-accesscontrolcondition"},"ability":"access-control-condition-decryption"}],"capabilities":[{"sig":"0x3a18598d9dbcb4c3f588d3948803f34397790f6b3ea1620913e353a002fb5526032aad72400635419346ca91a9bcfcd36cbdfc5b0f5080ecc48ded6ccfe6da7b1b","derivedVia":"web3.eth.personal.sign","signedMessage":"localhost wants you to sign in with your Ethereum account:\\n0xA89543a7145C68E52a4D584f1ceb123605131211\\n\\nThis is a test statement. You can put anything you want here. I further authorize the stated URI to perform the following actions on my behalf: (1) 'Threshold': 'Decryption' for 'lit-accesscontrolcondition://*'.\\n\\nURI: lit:session:963d6551d36a8b5be53d8959d45141764dbfd3c00649f56bc00433ef09db75f5\\nVersion: 1\\nChain ID: 1\\nNonce: 0x23f22526f00d01dc505e291881a44a8c74664f76cdaa2662b8af1ae54c9b4725\\nIssued At: 2024-11-15T05:58:06.757Z\\nExpiration Time: 2024-11-15T06:08:06.728Z\\nResources:\\n- urn:recap:eyJhdHQiOnsibGl0LWFjY2Vzc2NvbnRyb2xjb25kaXRpb246Ly8qIjp7IlRocmVzaG9sZC9EZWNyeXB0aW9uIjpbe31dfX0sInByZiI6W119","address":"0xA89543a7145C68E52a4D584f1ceb123605131211"}],"issuedAt":"2024-11-15T05:58:06.778Z","expiration":"2024-11-15T06:08:06.728Z","nodeAddress":"https://15.235.83.220:7472"}`, |
| 62 | + address: '963d6551d36a8b5be53d8959d45141764dbfd3c00649f56bc00433ef09db75f5', |
| 63 | + algo: 'ed25519' |
| 64 | + }, |
| 65 | + 'https://15.235.83.220:7471': { |
| 66 | + sig: 'b1e0d8fd8d09c3392b540126418ee9a8d8dbbabdd4d0c368832d612b1f51dadcd5f02bf5ca19dbd0251a3986ad5ddb514fc1f7e1753f8a6a979adf45fbbbd901', |
| 67 | + derivedVia: 'litSessionSignViaNacl', |
| 68 | + signedMessage: `{"sessionKey":"963d6551d36a8b5be53d8959d45141764dbfd3c00649f56bc00433ef09db75f5","resourceAbilityRequests":[{"resource":{"resource":"*","resourcePrefix":"lit-accesscontrolcondition"},"ability":"access-control-condition-decryption"}],"capabilities":[{"sig":"0x3a18598d9dbcb4c3f588d3948803f34397790f6b3ea1620913e353a002fb5526032aad72400635419346ca91a9bcfcd36cbdfc5b0f5080ecc48ded6ccfe6da7b1b","derivedVia":"web3.eth.personal.sign","signedMessage":"localhost wants you to sign in with your Ethereum account:\\n0xA89543a7145C68E52a4D584f1ceb123605131211\\n\\nThis is a test statement. You can put anything you want here. I further authorize the stated URI to perform the following actions on my behalf: (1) 'Threshold': 'Decryption' for 'lit-accesscontrolcondition://*'.\\n\\nURI: lit:session:963d6551d36a8b5be53d8959d45141764dbfd3c00649f56bc00433ef09db75f5\\nVersion: 1\\nChain ID: 1\\nNonce: 0x23f22526f00d01dc505e291881a44a8c74664f76cdaa2662b8af1ae54c9b4725\\nIssued At: 2024-11-15T05:58:06.757Z\\nExpiration Time: 2024-11-15T06:08:06.728Z\\nResources:\\n- urn:recap:eyJhdHQiOnsibGl0LWFjY2Vzc2NvbnRyb2xjb25kaXRpb246Ly8qIjp7IlRocmVzaG9sZC9EZWNyeXB0aW9uIjpbe31dfX0sInByZiI6W119","address":"0xA89543a7145C68E52a4D584f1ceb123605131211"}],"issuedAt":"2024-11-15T05:58:06.778Z","expiration":"2024-11-15T06:08:06.728Z","nodeAddress":"https://15.235.83.220:7471"}`, |
| 69 | + address: '963d6551d36a8b5be53d8959d45141764dbfd3c00649f56bc00433ef09db75f5', |
| 70 | + algo: 'ed25519' |
| 71 | + } |
| 72 | +} |
| 73 | +[Lit-JS-SDK v6.11.0] [2024-11-15T05:58:06.816Z] [DEBUG] [core] The request time is at: 2024-11-15T05:58:06.816Z |
| 74 | +* Outer expiration: |
| 75 | + * Issued at: 2024-11-15T05:58:06.778Z |
| 76 | + * Expiration: 2024-11-15T06:08:06.728Z |
| 77 | + * Duration: 9 minutes, 59.950 seconds |
| 78 | + * Status: ✅ Not expired (valid for 9 minutes, 59.912 seconds) |
| 79 | +* Capabilities: |
| 80 | + * Capability 1 (web3.eth.personal.sign): |
| 81 | + * Issued at: 2024-11-15T05:58:06.757Z |
| 82 | + * Expiration: 2024-11-15T06:08:06.728Z |
| 83 | + * Duration: 9 minutes, 59.971 seconds |
| 84 | + * Status: ✅ Not expired (valid for 9 minutes, 59.912 seconds) |
| 85 | + * Attenuation: |
| 86 | + * lit-accesscontrolcondition://* |
| 87 | + * Threshold/Decryption |
| 88 | +``` |
| 89 | + |
| 90 | +## Understanding the Code |
| 91 | + |
| 92 | +The following code from [./src/index.ts](./src/index.ts) does the following: |
| 93 | + |
| 94 | +### Creating an Ethers Signer |
| 95 | + |
| 96 | +```typescript |
| 97 | +const ethersSigner = new ethers.Wallet( |
| 98 | + ETHEREUM_PRIVATE_KEY, |
| 99 | + new ethers.providers.JsonRpcProvider(LIT_RPC.CHRONICLE_YELLOWSTONE) |
| 100 | +); |
| 101 | +``` |
| 102 | + |
| 103 | +The wallet that corresponds to `ETHEREUM_PRIVATE_KEY` would be your dev wallet that has permission to decrypt some data, sign with a PKP that it controls, or would be paying for the execution of a Lit Action (payment is not required for the Lit DatilDev network, so you don't need to worry about it for the hackathon). |
| 104 | + |
| 105 | +As we'll cover further below, the `ethersSigner` is used to generate an Authentication Signature which is a [ERC-5573](https://eips.ethereum.org/EIPS/eip-5573) message that specifies what Lit Resources and corresponding abilities the Session will be authorized to use. |
| 106 | + |
| 107 | +### Requesting Session Signatures |
| 108 | + |
| 109 | +After connecting an instance of `LitNodeClient` to the Lit Network, the code calls the `getSessionSigs` method to request the Lit network to generate session signatures for the session. |
| 110 | + |
| 111 | +This method takes in an object with the following properties: |
| 112 | + |
| 113 | +- **chain**: Corresponds to the signature schema and message format you're requesting the session to be authenticated with. This should almost always be `ethereum`. |
| 114 | +- **expiration**: The date and time at which the session will no longer be valid. |
| 115 | + - It's recommended that you set this value to be as soon as possible, to minimize the amount of time that the session is valid for in case it gets compromised. |
| 116 | +- **resourceAbilityRequests**: An array of objects that specify the Lit Resources and abilities you're requesting the session to be authorized to use. |
| 117 | +- **authNeededCallback**: A function that you implement that generates an Authentication Signature for the session. |
| 118 | + |
| 119 | +As a result of executing `getSessionSigs`, the Lit SDK will generate the ephemeral session key pair, execute the `authNeededCallback` function to generate an Authentication Signature, and submit the request to the Lit Network to generate session signatures. |
| 120 | + |
| 121 | +Upon receiving the request, each Lit node in the network will independently authenticate the Authentication Signature, deriving the corresponding Ethereum address, and use that address to check if it has the authority to delegate the specified Lit Resources and abilities to the session. |
| 122 | + |
| 123 | +If a node determines that the Authentication Signature is valid and authorized to delegate the `resourceAbilityRequests` to the session, it will generate a Session Signature indicating that the session is authenticated and authorized to use the specified Lit Resources and abilities. |
| 124 | + |
| 125 | +After a threshold of nodes have generated Session Signatures, the Lit Network will send the Session Signatures back to your client. You will then use these Session Signatures to interact with the Lit Network, as we'll cover in other code examples in this guide. |
0 commit comments