如何使用 Lit Protocol 解密
基于 Lit Protocol 实现隐私数据分享,用户可以给满足指定条件的地址分享自己的隐私数据。
分享条件为指定的 EOA 地址可以访问。开发者在获得用户授权后,可以使用特定的 EOA 地址对接口返回内容进行解密。
例如,开发者的地址为:0xfd7daf19b5719623e7378450a2bab4403f240004 (私钥为:89a1a8c7d5f6ab952e02c5c6a71f48d60fbe06c21ec021374907d93d644b1236,对应的ApiKey:581c6c4fa0b54912b00088aa563342a4。注:该ApiKey仅用于开发者测试体验),下面为如何使用Lit Protocol 的 JS-SDK 对接口信息进行解密:
安装依赖
npm install @lit-protocol/lit-node-client base64-to-uint8array示例代码
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 = "接口返回的加密信息";
    const encryptedSymmetricKey = "接口返回的加密信息";
    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