|
| 1 | +# File Encryption/Decryption Using the Lit SDK in Node.js |
| 2 | + |
| 3 | +This code example demonstrates how the Lit SDK can be used to encrypt and decrypt file data within a Node.js application. |
| 4 | + |
| 5 | +## Running this Example |
| 6 | + |
| 7 | +### Install the Dependencies |
| 8 | + |
| 9 | +In this directory, `encryption-decryption/nodejs`, run `yarn` to install the project dependencies. |
| 10 | + |
| 11 | +### Setting Up the `.env` File |
| 12 | + |
| 13 | +Make a copy of the provided `.env.example` file and name it `.env`: |
| 14 | + |
| 15 | +``` |
| 16 | +cp .env.example .env |
| 17 | +``` |
| 18 | + |
| 19 | +Within the `.env` file there are two ENVs |
| 20 | + |
| 21 | +1. `ETHEREUM_PRIVATE_KEY` - **Required** Will be used to generate an Ethers.js wallet to perform the signing of transactions |
| 22 | +2. `LIT_CAPACITY_CREDIT_TOKEN_ID` - **Optional** |
| 23 | + - If provided, this [CapacityCredit](https://developer.litprotocol.com/paying-for-lit/capacity-credits) will be used to create an AuthSig to pay for usage the Lit network |
| 24 | + - If not provided, a new CapacityCredit will be minted and used to run this example. Please make sure that your wallet has enough `tstLPX` to pay for execution of the Lit Action |
| 25 | + |
| 26 | +Your `.env` file should look like: |
| 27 | + |
| 28 | +``` |
| 29 | +ETHEREUM_PRIVATE_KEY=YourPrivateKey |
| 30 | +LIT_CAPACITY_CREDIT_TOKEN_ID=yourCapacityCreditTokenId |
| 31 | +``` |
| 32 | + |
| 33 | +### Starting the Example |
| 34 | + |
| 35 | +In this directory, `encryption-decryption/nodejs`, run `yarn test:encrypt` to test the encryption of a file, and `yarn test:decrypt` to test the decryption of a file. These commands will run the tests in the `test` directory, which are for the `encryptFile.ts` and `decryptFile.ts` files in the `src` directory. |
| 36 | + |
| 37 | +`encryptFile.ts`: |
| 38 | + |
| 39 | +1. Will take a blob created from a file and contract conditions to encrypt the file. |
| 40 | +2. Connects to the Lit network |
| 41 | +3. Encrypts the file and returns the `ciphertext` and `dataToEncryptHash` |
| 42 | + |
| 43 | +`decryptFile.ts`: |
| 44 | + |
| 45 | +1. Connects to the Lit network and LitContracts client |
| 46 | +2. Mints a new CapacityCredit if a CapacityCredit tokenId is not provided in the `.env` file and creates a capacityDelegationAuthSig to pay for use of the Lit network, decrypting data in this case |
| 47 | +3. Generates session signatures with the ability to decrypt the file |
| 48 | +4. Decrypts the file and returns a decrypted file buffer |
| 49 | + |
| 50 | +However the tests created in the `test` directory are more complicated. |
| 51 | + |
| 52 | +`encryptFileWithContractConditions.spec.ts` |
| 53 | + |
| 54 | +``` |
| 55 | +yarn test:encrypt |
| 56 | +``` |
| 57 | + |
| 58 | +Encrypt the file with specific `EVMContractConditions`. These conditions check the [deployedAllowList](./test/fixtures/deployed.json) contract address on the Chronicle Yellowstone network and calls the `isOnAllowList` function, expecting to return true if the address of the Ethereum account is on the allow list. |
| 59 | + |
| 60 | +`decryptFileWithContractConditions.spec.ts` |
| 61 | + |
| 62 | +``` |
| 63 | +yarn test:decrypt |
| 64 | +``` |
| 65 | + |
| 66 | +Decrypts the file, first checking if the address is on the allow list, and if not, adding it to the allow list. Once the address is on the allow list, the file can be successfully decrypted. |
0 commit comments