Setup the Project
Smart Contract
- Install hardhat:
yarn add hardhat
- Init hardhat to create the boilerplate for a Basic project (with Javascript):
npx hardhat init
- Install Openzepplin:
yarn add @openzeppelin/contracts
- Test deploy the sample smart contract on 2 separate terminals:
npx hardhat node
npx hardhat run scripts/deploy.js --network localhost
Now that we have our hardhat working & the sample smart contract is deployed correctly, let's setup our Lit SDK, which we will use to encrypt & decrypt the metadata.
Lit JS SDK V2
You can use the Lit JS SDK V2 to encrypt and store any static content. This could be a file, a string, or anything that won't change (we're going to encrypt an input string). You have to store the content and metadata yourself (we're storing that on a blockchain network), but Lit will store who is allowed to decrypt it and enforce this (aka key management).
- Install Lit JS SDK V2:
yarn add @lit-protocol/lit-node-client
- Create a Lit class which will have all the encryption & decryption functions we require:
import * as LitJsSdk from "@lit-protocol/lit-node-client";
const client = new LitJsSdk.LitNodeClient();
class Lit {
litNodeClient;
async connect() {
await client.connect();
this.litNodeClient = client;
}
}
client.connect()
will return a promise that resolves when you are connected to the Lit Network. You may also listen for the lit-ready
event.
In this code example, the litNodeClient
is set as a global variable for use throughout the web app.