Use Lit Protocol to Decrypt

Using the Lit Protocol for private data sharing, users can share their private data with addresses that meet specific conditions.

The sharing condition is that the specified EOA (Externally Owned Account) address can access it. After obtaining user authorization, developers can use a specific EOA address to decrypt the content returned by the interface.

For example, if the developer's address is: 0xfd7daf19b5719623e7378450a2bab4403f240004 (the private key is: 89a1a8c7d5f6ab952e02c5c6a71f48d60fbe06c21ec021374907d93d644b1236: ,the ApiKey is: 581c6c4fa0b54912b00088aa563342a4. Note:This ApiKey is only used for developer testing experience), below is how to use the Lit Protocol's JS-SDK to decrypt the interface information:

  • Install dependencies

npm install @lit-protocol/lit-node-client base64-to-uint8array
  • Sample code

import LitJsSdk from '@lit-protocol/lit-node-client';
import * as u8a from "uint8arrays";
import ethers from "ethers";
import siwe from "siwe";
import toUint8Array  from 'base64-to-uint8array'


const decryptString = async () => {
    const encryptedString = "encrypted information returned by the API";
    const encryptedSymmetricKey = "encrypted information returned by the API";
    const chain = 'ethereum';
    const authSig = await signAuthMessage();
    const litNodeClient = new LitJsSdk.LitNodeClient();
    await litNodeClient.connect();
    const accessControlConditions = [
        {
            contractAddress: '',
            standardContractType: '',
            chain,
            method: '',
            parameters: [
                ':userAddress',
            ],
            returnValueTest: {
                comparator: '=',
                value: '0xfd7daf19b5719623e7378450a2bab4403f240004'
            }
        }
    ]

// <String> toDecrypt
    const toDecrypt = LitJsSdk.uint8arrayToString(toUint8Array(encryptedSymmetricKey), 'base16');

// <Uint8Array(32)> _symmetricKey 
    const _symmetricKey = await litNodeClient.getEncryptionKey({
        accessControlConditions,
        toDecrypt,
        chain,
        authSig
    })

// <String> decryptedString
    let decryptedString;
    const blob = await LitJsSdk.base64StringToBlob(encryptedString);

    try {
        decryptedString = await LitJsSdk.decryptString(
            blob,
            _symmetricKey
        );
    } catch (e) {
        console.log(e);
    }

    console.info("decryptedString:", decryptedString);
}

/**
 * Get auth signature using siwe
 * @returns
 */
const signAuthMessage = async () => {

    // Replace this with you private key
    const privKey = "89a1a8c7d5f6ab952e02c5c6a71f48d60fbe06c21ec021374907d93d644b1236";
    const privKeyBuffer = u8a.fromString(privKey, "base16");
    const wallet = new ethers.Wallet(privKeyBuffer);

    const domain = "localhost";
    const origin = "https://localhost/login";
    const statement =
        "This is a test statement.  You can put anything you want here.";

    const siweMessage = new siwe.SiweMessage({
        domain,
        address: wallet.address,
        statement,
        uri: origin,
        version: "1",
        chainId: "1",
    });

    const messageToSign = siweMessage.prepareMessage();

    const signature = await wallet.signMessage(messageToSign);

    console.log("signature", signature);

    const recoveredAddress = ethers.utils.verifyMessage(messageToSign, signature);

    const authSig = {
        sig: signature,
        derivedVia: "web3.eth.personal.sign",
        signedMessage: messageToSign,
        address: recoveredAddress,
    };

    return authSig;
}

decryptString();

Last updated