Skip to content

Commit 9dd9ad3

Browse files
committed
Nodejs Readme and test file import reordering
1 parent ea74c97 commit 9dd9ad3

File tree

4 files changed

+69
-5
lines changed

4 files changed

+69
-5
lines changed
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
ETHEREUM_PRIVATE_KEY=
2-
SEPOLIA_RPC_URL=https://gateway.tenderly.co/public/sepolia
2+
LIT_CAPACITY_CREDIT_TOKEN_ID=
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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.

encryption-decryption/nodejs/test/decryptFileWithContractConditions.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { EvmContractConditions } from "@lit-protocol/types";
2+
import { LIT_RPC } from "@lit-protocol/constants";
13
import { expect, use } from "chai";
24
import * as ethers from "ethers";
35
import { readFileSync } from "fs";
@@ -7,8 +9,6 @@ import { getEnv } from "../src/utils";
79
import deployedAllowList from "./fixtures/deployed.json";
810
import { encryptFileWithContractConditions } from "../src/encryptFile";
911
import { decryptFileWithContractConditions } from "../src/decryptFile";
10-
import { EvmContractConditions } from "@lit-protocol/types";
11-
import { LIT_RPC } from "@lit-protocol/constants";
1212

1313
use(require("chai-json-schema"));
1414

encryption-decryption/nodejs/test/encryptFileWithContractConditions.spec.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { expect, use } from "chai";
2-
import * as ethers from "ethers";
32
import { readFileSync } from "fs";
43
import path from "path";
54

6-
import { getEnv } from "../src/utils";
75
import { encryptFileWithContractConditions } from "../src/encryptFile";
86
import deployedAllowList from "./fixtures/deployed.json";
97

0 commit comments

Comments
 (0)