Skip to content

Instantly share code, notes, and snippets.

@masha256
Created April 2, 2022 19:45
Show Gist options
  • Save masha256/e9db6e8f8ef462d2d570afaba2c543bf to your computer and use it in GitHub Desktop.
Save masha256/e9db6e8f8ef462d2d570afaba2c543bf to your computer and use it in GitHub Desktop.
const ethers = require('ethers');
const LitSDK = require('lit-js-sdk/build/index.node.js');
const lit = new LitSDK.LitNodeClient();
await lit.connect();
const chain = 'mumbai';
const saveAuthSig = {
sig: "0x<signature1>",
derivedVia: "web3.eth.personal.sign",
signedMessage:
"Sign-in Message",
address: "0x<address1>",
};
const accessControlConditions = [
{
contractAddress: '',
standardContractType: '',
chain,
method: '',
parameters: [
':userAddress'
],
returnValueTest: {
comparator: '=',
value: '0x<address1>'
}
},
{"operator": "or"},
{
contractAddress: '',
standardContractType: '',
chain,
method: '',
parameters: [
':userAddress'
],
returnValueTest: {
comparator: '=',
value: '0x<address2>'
}
},
];
const { encryptedString, symmetricKey } = await LitSDK.encryptString("this is a secret message");
const encryptedSymmetricKey = await lit.saveEncryptionKey({
accessControlConditions,
symmetricKey,
authSig: saveAuthSig,
chain,
});
const saveEncryptedString = ethers.utils.hexlify(new Uint8Array(await encryptedString.arrayBuffer()));
const saveEncryptedSymmetricKey = ethers.utils.hexlify(encryptedSymmetricKey);
console.log(`encryptedString: ${saveEncryptedString}`);
console.log(`encryptedSymmetricKey: ${saveEncryptedSymmetricKey}`);
console.log(`DECRYPTING...`);
const restoreAuthSigProvider = {
sig: "0x<signature2>",
derivedVia: "web3.eth.personal.sign",
signedMessage:
"Sign-in Message",
address: "0x<address2>",
};
const decryptSymmetricKey = await lit.getEncryptionKey({
accessControlConditions,
toDecrypt: saveEncryptedSymmetricKey.substr(2), // strip leading '0x'
chain,
authSig: restoreAuthSigProvider,
});
const decryptedString = await LitSDK.decryptString(
new Blob([ethers.utils.arrayify(saveEncryptedString).buffer]),
decryptSymmetricKey);
console.log(`Decrypt result: ${decryptedString}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment