From 405c56488085a3df5184351219d62a803c7c27e1 Mon Sep 17 00:00:00 2001 From: Samuel Andert Date: Mon, 28 Aug 2023 10:19:42 +0200 Subject: [PATCH] added session persistance --- package.json | 1 + pnpm-lock.yaml | 7 +++ src/lib/GoogleAuth.svelte | 113 +++++++++++++++++++++++++++----------- 3 files changed, 88 insertions(+), 33 deletions(-) diff --git a/package.json b/package.json index a498d3d..7b94e5f 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "@sveltejs/adapter-auto": "^2.0.0", "@sveltejs/kit": "^1.5.0", "@types/cookie": "^0.5.1", + "@types/js-cookie": "^3.0.3", "@types/jsonwebtoken": "^9.0.2", "concurrently": "^7.6.0", "svelte": "^3.54.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4dfa47e..0fdcd40 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -87,6 +87,9 @@ devDependencies: '@types/cookie': specifier: ^0.5.1 version: 0.5.1 + '@types/js-cookie': + specifier: ^3.0.3 + version: 3.0.3 '@types/jsonwebtoken': specifier: ^9.0.2 version: 9.0.2 @@ -4465,6 +4468,10 @@ packages: '@types/istanbul-lib-report': 3.0.0 dev: false + /@types/js-cookie@3.0.3: + resolution: {integrity: sha512-Xe7IImK09HP1sv2M/aI+48a20VX+TdRJucfq4vfRVy6nWN8PYPOEnlMRSgxJAgYQIXJVL8dZ4/ilAM7dWNaOww==} + dev: true + /@types/json-schema@7.0.11: resolution: {integrity: sha512-wOuvG1SN4Us4rez+tylwwwCV1psiNVOkJeM3AUWUNWg/jDQY2+HE/444y5gc+jBmRqASOm2Oeh5c1axHobwRKQ==} dev: false diff --git a/src/lib/GoogleAuth.svelte b/src/lib/GoogleAuth.svelte index 0411a8f..6874ddb 100644 --- a/src/lib/GoogleAuth.svelte +++ b/src/lib/GoogleAuth.svelte @@ -12,17 +12,36 @@ const redirectUri = "http://localhost:3000/"; - let sessionSigs, error, currentPKP, authMethod, provider; - let messageToSign = { user: "Sam", loggedIn: true, signature: "" }; + let sessionSigs = null; + let litNodeClient, error, currentPKP, authMethod, provider; + let messageToSign = { user: "Sam", loggedIn: true }; let status = "Initializing..."; let jsonObjectToVerify = null; let pkps: IRelayPKP[] = []; let view = "SIGN_IN"; + let sessionSigsObject; - onMount(() => { - initialize(); + onMount(async () => { + litNodeClient = new LitNodeClient({ litNetwork: "serrano" }); + await litNodeClient.connect(); + const sessionSigsLocalStorage = localStorage.getItem("google-signature"); + const currentPKPLocalStorage = localStorage.getItem("current-pkp"); + if (sessionSigsLocalStorage && currentPKPLocalStorage) { + sessionSigs = JSON.parse(sessionSigsLocalStorage); + currentPKP = JSON.parse(currentPKPLocalStorage); + } else { + initialize(); + } + + if (sessionSigsLocalStorage) { + sessionSigsObject = JSON.parse(sessionSigsLocalStorage); + } }); + $: if (sessionSigs) { + view = "READY"; + } + async function initialize() { status = "Connecting to Google provider..."; try { @@ -46,7 +65,7 @@ status = "Reconnected to Google provider."; } await provider.signIn(); - status = "Signed in with Google."; + status = "Signing in with Google..."; } catch (err) { setError(err); } @@ -58,13 +77,13 @@ authMethod = await provider.authenticate(); status = "Authenticated successfully."; pkps = await provider.fetchPKPsThroughRelayer(authMethod); - status = "Fetched PKPs."; + status = "Fetching your Google PKP..."; if (pkps.length === 0) { - status = "No PKPs found. Minting..."; + status = "No PKPs found. Minting new PKP..."; await mint(); } else { - // Update the view to 'PKP' to show the available PKPs - view = "PKP"; + // Use the first PKP directly + await createSession(pkps[0]); } } catch (err) { setError(err); @@ -81,9 +100,13 @@ async function createSession(pkp: IRelayPKP) { try { currentPKP = pkp; // Assign the selected PKP to currentPKP - sessionSigs = await createLitSession(provider, pkp.publicKey, authMethod); + createLitSession(provider, pkp.publicKey, authMethod).then((sigs) => { + sessionSigs = sigs; + // Store sessionSigs and currentPKP in localStorage + localStorage.setItem("google-signature", JSON.stringify(sessionSigs)); + localStorage.setItem("current-pkp", JSON.stringify(currentPKP)); + }); status = "Session created successfully."; - view = "READY"; } catch (err) { setError(err); } @@ -104,11 +127,11 @@ const toSign = ethers.getBytes(ethers.hashMessage(jsonString)); const litActionCode = ` - const go = async () => { - const sigShare = await LitActions.signEcdsa({ toSign, publicKey, sigName }); - }; - go(); - `; + const go = async () => { + const sigShare = await LitActions.signEcdsa({ toSign, publicKey, sigName }); + }; + go(); + `; // Sign message const results = await litNodeClient.executeJs({ @@ -134,10 +157,6 @@ jsonObjectToVerify = { ...messageToSign }; - console.log( - "JSON object to sign: " + JSON.stringify(jsonObjectToVerify, null, 2) - ); - // Display the signed JSON status = JSON.stringify(messageToSign, null, 2); @@ -173,21 +192,9 @@ Sign in with Google {/if} - {#if view === "PKP"} -
-

Select a PKP

- -
- {/if} {#if view === "READY"}
+

Your PKP Address: {currentPKP.ethAddress}

Ready to sign

{#if messageToSign} @@ -199,5 +206,45 @@

Status

{status}

+
+ Session Signature + {#if sessionSigsObject} +
+

Session Signatures

+ {#each Object.keys(sessionSigsObject) as nodeAddress} +
+

{nodeAddress}

+

Signature: {sessionSigsObject[nodeAddress].sig}

+

Derived Via: {sessionSigsObject[nodeAddress].derivedVia}

+

Address: {sessionSigsObject[nodeAddress].address}

+

Algorithm: {sessionSigsObject[nodeAddress].algo}

+

Signed Message

+
{sessionSigsObject[nodeAddress].signedMessage}
+
+ {/each} +
+ {/if} +
+ +