cool, it works

This commit is contained in:
Chris Cassano 2022-11-10 20:45:46 -08:00
parent cdfb5c2be6
commit 56a1b69243
4 changed files with 116 additions and 29075 deletions

29057
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -9,11 +9,25 @@ import PKPHelper from "./abis/PKPHelper.json";
import PKPNFT from "./abis/PKPNFT.json";
import ContractAddresses from "./abis/deployed-contracts.json";
window.LitJsSdk = LitJsSdk;
window.ethers = ethers;
function App() {
const [pkpEthAddress, setPkpEthAddress] = useState(null);
const [googleCredentialResponse, setGoogleCredentialResponse] =
useState(null);
const [pkpPublicKey, setPkpPublicKey] = useState(null);
const [status, setStatus] = useState("");
const handleLoggedInToGoogle = async (credentialResponse) => {
setStatus("Logged in to Google");
console.log("got this response from google sign in: ", credentialResponse);
setGoogleCredentialResponse(credentialResponse);
mintPkp(credentialResponse);
};
const mintPkp = async (credentialResponse) => {
setStatus("Minting PKP...");
// mint a PKP for the user
// A Web3Provider wraps a standard Web3 provider, which is
// what MetaMask injects as window.ethereum into each page
@ -66,26 +80,106 @@ function App() {
console.log("minted PKP with eth address: ", ethAddress);
const pkpPublicKey = await pkpContract.getPubkey(tokenIdFromEvent);
setPkpPublicKey(pkpPublicKey);
};
const handleEncryptThenDecrypt = async () => {
setStatus("Encrypting then decrypting...");
var unifiedAccessControlConditions = [
{
conditionType: "evmBasic",
contractAddress: "",
standardContractType: "",
chain: "ethereum",
method: "eth_getBalance",
parameters: [":userAddress", "latest"],
returnValueTest: {
comparator: ">=",
value: "0",
},
},
];
// this will be fired if auth is needed. we can use this to prompt the user to sign in
const authNeededCallback = async ({
chain,
resources,
expiration,
uri,
litNodeClient,
}) => {
console.log("authNeededCallback fired");
const sessionSig = await litNodeClient.signSessionKey({
sessionKey: uri,
authMethods: [
{
authMethodType: 6,
accessToken: googleCredentialResponse.credential,
},
],
pkpPublicKey,
expiration,
resources,
chain,
});
console.log("got session sig from node and PKP: ", sessionSig);
return sessionSig;
};
// get the user a session with it
const litNodeClient = new LitJsSdk.LitNodeClient();
await litNodeClient.connect();
const fakeSessionKey = "lit:session:0x1234567890";
const sessionSig = await litNodeClient.signSessionKey({
sessionKey: fakeSessionKey,
authMethods: [
{
authMethodType: 6,
accessToken: credentialResponse.credential,
},
],
pkpPublicKey,
const sessionSigs = await litNodeClient.getSessionSigs({
expiration: new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString(), // 24 hours
chain: "ethereum",
resources: [`litEncryptionCondition://*`],
switchChain: false,
authNeededCallback,
});
console.log("sessionSig: ", sessionSig);
console.log("sessionSigs before saving encryption key: ", sessionSigs);
const { encryptedZip, symmetricKey } = await LitJsSdk.zipAndEncryptString(
"this is a secret message"
);
const encryptedSymmetricKey = await litNodeClient.saveEncryptionKey({
unifiedAccessControlConditions,
symmetricKey,
sessionSigs,
});
const hashOfKey = await LitJsSdk.hashEncryptionKey({
encryptedSymmetricKey,
});
console.log("encrypted symmetric key", encryptedSymmetricKey);
const retrievedSymmKey = await litNodeClient.getEncryptionKey({
unifiedAccessControlConditions,
toDecrypt: LitJsSdk.uint8arrayToString(encryptedSymmetricKey, "base16"),
sessionSigs,
});
const decryptedFiles = await LitJsSdk.decryptZip(
encryptedZip,
retrievedSymmKey
);
const decryptedString = await decryptedFiles["string.txt"].async("text");
console.log("decrypted string", decryptedString);
setStatus("Success!");
};
return (
<div className="App">
<div style={{ height: 50 }} />
<h1>{status}</h1>
<div style={{ height: 100 }} />
<h3>
Step 1: log in with Google. You will mint a PKP and obtain a session
sig.
</h3>
<GoogleLogin
onSuccess={handleLoggedInToGoogle}
onError={() => {
@ -95,6 +189,11 @@ function App() {
/>
<div style={{ height: 100 }} />
{pkpEthAddress && <div>PKP Eth Address: {pkpEthAddress}</div>}
<div style={{ height: 100 }} />
<h3>Step 2: Use Lit</h3>
<button onClick={handleEncryptThenDecrypt}>
Encrypt then Decrypt with Lit
</button>
</div>
);
}

View File

@ -2,9 +2,8 @@
"version": "v1",
"packages": {
"lit-js-sdk": {
"signature": "24bc7659933f393b3330aee609498caf",
"file": true,
"replaced": "^1.1.234"
"signature": "c71561d0d1f9e965d7937ed22756007f",
"file": true
}
}
}

View File

@ -6885,7 +6885,7 @@ lit-connect-modal@^0.1.10:
micromodal "^0.4.10"
"lit-js-sdk@file:.yalc/lit-js-sdk":
version "1.2.25"
version "1.2.29"
dependencies:
"@ethersproject/bytes" "^5.5.0"
"@ethersproject/contracts" "^5.2.0"
@ -7229,9 +7229,9 @@ node-fetch@2.6.7:
whatwg-url "^5.0.0"
node-fetch@^3.2.3:
version "3.2.10"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-3.2.10.tgz#e8347f94b54ae18b57c9c049ef641cef398a85c8"
integrity sha512-MhuzNwdURnZ1Cp4XTazr69K0BTizsBroX7Zx3UgDSVcZYKF/6p0CBe4EUb/hLqmzVhl0UpYfgRljQ4yxE+iCxA==
version "3.3.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-3.3.0.tgz#37e71db4ecc257057af828d523a7243d651d91e4"
integrity sha512-BKwRP/O0UvoMKp7GNdwPlObhYGB5DQqwhEDQlNKuoqwVYSxkSZCSbHjnFFmUEtwSKRPU4kNK8PbDYYitwaE3QA==
dependencies:
data-uri-to-buffer "^4.0.0"
fetch-blob "^3.1.4"