Skip to content

Commit

Permalink
Merge pull request #6 from moleculeprotocol/sdk-next
Browse files Browse the repository at this point in the history
lit 5.1.0
  • Loading branch information
elmariachi111 authored May 22, 2024
2 parents 0c35379 + 97b8d6a commit 8a6aa69
Show file tree
Hide file tree
Showing 4 changed files with 1,130 additions and 1,221 deletions.
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@
"@chakra-ui/react": "^2.8.2",
"@emotion/react": "^11.11.4",
"@emotion/styled": "^11.11.5",
"@lit-protocol/constants": "2.2.62",
"@lit-protocol/lit-node-client": "2.2.62",
"@lit-protocol/auth-helpers": "^5.1.0",
"@lit-protocol/constants": "^5.1.0",
"@lit-protocol/contracts-sdk": "^5.1.0",
"@lit-protocol/lit-node-client": "^5.1.0",
"framer-motion": "^11.2.4",
"react": "^18",
"react-dom": "^18",
"next": "^13",
"pino-pretty": "^11.0.0"
"pino-pretty": "^11.0.0",
"react": "^18",
"react-dom": "^18"
},
"devDependencies": {
"typescript": "^5",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"eslint": "^8",
"eslint-config-next": "^13"
"eslint-config-next": "^13",
"typescript": "^5"
}
}
62 changes: 33 additions & 29 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,57 @@ import { Button, HStack, Heading, Input, Text, Textarea, VStack } from "@chakra-
import { LitNodeClient } from "@lit-protocol/lit-node-client";
import { useCallback, useEffect, useState } from "react";
import { Radio, RadioGroup } from '@chakra-ui/react'
import * as LitJsSdk from "@lit-protocol/lit-node-client";
import { LitNetwork } from "@lit-protocol/constants";

export default function Home() {
const [litNetwork, setLitNetwork] = useState<"serrano" | "jalapeno">("jalapeno");
const [litNetwork, setLitNetwork] = useState<LitNetwork.Cayenne | LitNetwork.Habanero | LitNetwork.Manzano>(LitNetwork.Cayenne);
const [client, setClient] = useState<LitNodeClient>();
const [message, setMessage] = useState("Hello World");
const [nonce, setNonce] = useState(new Date().toISOString());
const [error, setError] = useState("");

const [encryptionResult, setEncryptionResult] = useState<{
encryptedString: Blob;
encryptedSymmetricKey: string;
}>();

const [decryptionResult, setDecryptionResult] = useState<{
decryptedString: string;
}>();
const [encryptionResult, setEncryptionResult] = useState<{ ciphertext: string, dataToEncryptHash: string }>();
const [decryptionResult, setDecryptionResult] = useState<string>();

useEffect(() => {
setClient(undefined)
const _client = new LitNodeClient({
litNetwork,
debug: true,
});
_client.connect().then(() => {
(async() => {
setClient(undefined)
const _client = new LitNodeClient({
litNetwork,
debug: true,
});
//https://developer.litprotocol.com/v3/sdk/installation#sdk-installed-for-client-side-usage
await _client.disconnect();
await _client.connect()
setClient(_client);
});
const nonce = await _client.getLatestBlockhash();
setNonce(nonce)
})();
}, [litNetwork]);

const handleEncrypt = useCallback(async () => {
if (!client) throw new Error("No client");
try {
const res = await encrypt(client, message);
const res = await encrypt(client, message, nonce);
setEncryptionResult(res);
} catch (error: any) {
setError(error.message);
}
}, [client]);
}, [client, message, nonce]);

const handleDecrypt = useCallback(async () => {
if (!client) throw new Error("No client");
try {
if (!encryptionResult?.encryptedSymmetricKey)
throw new Error("No encryptedSymmetricKey");
if (!encryptionResult?.ciphertext)
throw new Error("No encrypted data");

const res = await decrypt(client, encryptionResult);
setDecryptionResult(res);
const decryptedString = await decrypt(client, nonce, encryptionResult);
setDecryptionResult(decryptedString);
} catch (error: any) {
setError(error.message);
}
}, [client, encryptionResult]);
}, [client, encryptionResult, nonce]);

return (
<VStack>
Expand All @@ -63,8 +65,9 @@ export default function Home() {
<Heading size="md">Encrypt Message</Heading>
<RadioGroup onChange={(v) => setLitNetwork(v as typeof litNetwork)} value={litNetwork}>
<HStack >
<Radio value='jalapeno'>Jalapeno</Radio>
<Radio value='serrano'>Serrano</Radio>
<Radio value='cayenne'>Cayenne</Radio>
<Radio value='manzano'>Manzano</Radio>
<Radio value='habanero'>Habenero</Radio>
</HStack>
</RadioGroup>

Expand All @@ -73,23 +76,24 @@ export default function Home() {
Encrypt String
</Button>

{encryptionResult?.encryptedSymmetricKey && (
{encryptionResult?.ciphertext && (
<>
<Text color="green">Message encrypted</Text>
<Textarea defaultValue={encryptionResult.encryptedSymmetricKey} />
<Input isDisabled value={encryptionResult.dataToEncryptHash} />
<Textarea defaultValue={encryptionResult.ciphertext} />
</>
)}

<Heading size="md">Decrypt Message</Heading>
<Button
colorScheme="green"
isDisabled={!encryptionResult?.encryptedSymmetricKey}
isDisabled={!encryptionResult?.ciphertext}
onClick={handleDecrypt}
>
Decrypt String
</Button>
{decryptionResult && (
<Text color="green">{decryptionResult.decryptedString}</Text>
<Text color="green">{decryptionResult}</Text>
)}

<Button
Expand Down
87 changes: 46 additions & 41 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

import {
checkAndSignAuthMessage,
decryptString,
decryptToString,
encryptString,
LitNodeClient,
uint8arrayToString,
} from "@lit-protocol/lit-node-client";
import { UnifiedAccessControlConditions,AccsEVMParams } from "@lit-protocol/types";
import {
UnifiedAccessControlConditions,
AccsEVMParams,
} from "@lit-protocol/types";

const chain = "sepolia";

const evmContractConditions: AccsEVMParams[] = [{
const evmContractConditions: AccsEVMParams[] = [
{
conditionType: "evmContract",
chain,
contractAddress: "0xe0D404C22228b03D5b8a715Cb569C4944BC5A27A",
Expand Down Expand Up @@ -41,64 +45,65 @@ const evmContractConditions: AccsEVMParams[] = [{
comparator: ">=",
value: "0",
},
}
]
},
];

const unifiedAccessControlConditions: UnifiedAccessControlConditions = evmContractConditions;
const unifiedAccessControlConditions: UnifiedAccessControlConditions =
evmContractConditions;

export async function encrypt(client: LitNodeClient, message: string) {
const authSig = await checkAndSignAuthMessage({ chain, walletConnectProjectId: process.env.NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID});
const { encryptedString, symmetricKey } = await encryptString(message);
export async function encrypt(
client: LitNodeClient,
message: string,
nonce: string
) {
const authSig = await checkAndSignAuthMessage({
chain,
walletConnectProjectId: process.env.NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID,
nonce,
});
const { ciphertext, dataToEncryptHash } = await encryptString(
{
chain,
authSig,
unifiedAccessControlConditions: unifiedAccessControlConditions,
dataToEncrypt: message,
},
client
);

if (!encryptedString) {
if (!ciphertext) {
throw new Error("Could not encrypt");
}

const encryptedSymmetricKey = await client.saveEncryptionKey({
//accessControlConditions: unifiedAccessControlConditions,
unifiedAccessControlConditions: unifiedAccessControlConditions,
chain,
authSig,
symmetricKey,
});

return {
encryptedString,
encryptedSymmetricKey: uint8arrayToString(encryptedSymmetricKey, "base16"),
ciphertext,
dataToEncryptHash,
};
}

export async function decrypt(
client: LitNodeClient,
nonce: string,
encrypted: {
encryptedString: Blob;
encryptedSymmetricKey: string;
ciphertext: string;
dataToEncryptHash: string;
}
) {
const authSig = await checkAndSignAuthMessage({ chain });
const authSig = await checkAndSignAuthMessage({ chain, nonce });

const decryptedSymmetricKey = await client.getEncryptionKey({
//accessControlConditions: evmContractConditions,
unifiedAccessControlConditions: unifiedAccessControlConditions,
chain,
authSig,
toDecrypt: encrypted.encryptedSymmetricKey,
});

if (!decryptedSymmetricKey) {
throw new Error("Could not decrypt symmetric key");
}

const decryptedString = await decryptString(
encrypted.encryptedString,
decryptedSymmetricKey
const decryptedString = await decryptToString(
{
chain,
authSig,
unifiedAccessControlConditions: unifiedAccessControlConditions,
...encrypted
},
client
);

if (!decryptedString) {
throw new Error("Could not decrypt string");
}

return {
decryptedString,
};
return decryptedString;
}
Loading

0 comments on commit 8a6aa69

Please sign in to comment.