Skip to content

Instantly share code, notes, and snippets.

@kay-is
Created December 15, 2022 15:49
Show Gist options
  • Save kay-is/a500f818f35fd5d8429c2316112cabae to your computer and use it in GitHub Desktop.
Save kay-is/a500f818f35fd5d8429c2316112cabae to your computer and use it in GitHub Desktop.
Using Lit Protocol with Web3Auth
<!DOCTYPE html>
<title>Using Lit Protocol with Web3Auth</title>
<script src="https://unpkg.com/@web3auth/modal@3.3.0/dist/modal.umd.min.js"></script>
<script src="https://unpkg.com/@web3auth/base@3.3.0/dist/base.umd.min.js"></script>
<script src="https://unpkg.com/ethers@5.7.2/dist/ethers.umd.js"></script>
<script src="https://jscdn.litgateway.com/index.web.js"></script>
<button id="encryptButton">Encrypt</button>
<script type="module">
const { Web3Auth } = window.Modal
const { CHAIN_NAMESPACES } = window.Base
const web3auth = new Web3Auth({
clientId: "...",
chainConfig: {
chainNamespace: CHAIN_NAMESPACES.EIP155,
chainId: "0x1",
rpcTarget: "https://rpc.ankr.com/eth",
},
})
await web3auth.initModal()
const output = document.getElementById("output")
await web3auth.connect()
const provider = new ethers.providers.Web3Provider(web3auth.provider)
const userAddress = (await provider.getSigner().getAddress()).toLowerCase()
const litNodeClient = new LitJsSdk.LitNodeClient()
await litNodeClient.connect()
document
.getElementById("encryptButton")
.addEventListener("click", async () => {
const network = await provider.getNetwork()
const authSig = await LitJsSdk.signAndSaveAuthMessage({
web3: provider,
account: userAddress,
chainId: network.chainId,
})
const litKeyConfig = {
chain: "ethereum",
authSig,
accessControlConditions: [
{
standardContractType: "ERC721",
chain: "ethereum",
contractAddress: "...",
method: "balanceOf",
parameters: [":userAddress"],
returnValueTest: { comparator: ">", value: "0" },
},
{ operator: "or" },
{
contractAddress: "",
standardContractType: "",
chain: "ethereum",
method: "",
parameters: [":userAddress"],
returnValueTest: { comparator: "=", value: userAddress },
},
],
}
const { encryptedString, symmetricKey } = await LitJsSdk.encryptString(
"this is a secret message"
)
const encryptedSymmetricKey = await litNodeClient.saveEncryptionKey({
...litKeyConfig,
symmetricKey,
})
const decryptedSymmetricKey = await litNodeClient.getEncryptionKey({
...litKeyConfig,
toDecrypt: LitJsSdk.uint8arrayToString(encryptedSymmetricKey, "base16"),
})
const decryptedString = await LitJsSdk.decryptString(
encryptedString,
decryptedSymmetricKey
)
console.log(decryptedString)
})
</script>
@kay-is
Copy link
Author

kay-is commented Dec 15, 2022

Requires a Web3Auth account.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment