From c71f37d22157b6ec09367e29d40ce1f53961d715 Mon Sep 17 00:00:00 2001 From: Samuel Andert Date: Fri, 21 Jul 2023 09:55:48 +0200 Subject: [PATCH] reducing the google example to google only --- example/google-auth/src/pages/index.tsx | 275 +----------------------- src/lib/NewProvider.svelte | 56 ----- 2 files changed, 6 insertions(+), 325 deletions(-) delete mode 100644 src/lib/NewProvider.svelte diff --git a/example/google-auth/src/pages/index.tsx b/example/google-auth/src/pages/index.tsx index 88824b4..6b3f272 100644 --- a/example/google-auth/src/pages/index.tsx +++ b/example/google-auth/src/pages/index.tsx @@ -5,32 +5,21 @@ import { LitAuthClient, BaseProvider, GoogleProvider, - EthWalletProvider, - WebAuthnProvider, isSignInRedirect, getProviderFromUrl, } from '@lit-protocol/lit-auth-client'; import { IRelayPKP, AuthMethod, SessionSigs } from '@lit-protocol/types'; import { ProviderType } from '@lit-protocol/constants'; -import { ethers } from 'ethers'; import { useRouter } from 'next/router'; -import { useConnect, useAccount, useDisconnect, Connector } from 'wagmi'; import {LitAccessControlConditionResource, LitAbility} from '@lit-protocol/auth-helpers'; +import { useConnect, useAccount, useDisconnect, Connector } from 'wagmi'; -// Local dev only: When using npm link, need to update encryption pkg to handle possible ipfs client init error -// let ipfsClient = null; -// try { -// ipfsClient = require("ipfs-http-client"); -// } catch {} enum Views { SIGN_IN = 'sign_in', HANDLE_REDIRECT = 'handle_redirect', REQUEST_AUTHSIG = 'request_authsig', - REGISTERING = 'webauthn_registering', - REGISTERED = 'webauthn_registered', - AUTHENTICATING = 'webauthn_authenticating', FETCHING = 'fetching', FETCHED = 'fetched', MINTING = 'minting', @@ -40,6 +29,7 @@ enum Views { ERROR = 'error', } + export default function Dashboard() { const redirectUri = 'http://localhost:3000'; @@ -57,12 +47,6 @@ export default function Dashboard() { const [currentPKP, setCurrentPKP] = useState(); const [sessionSigs, setSessionSigs] = useState(); - const [message, setMessage] = useState('Free the web!'); - const [signature, setSignature] = useState(); - const [recoveredAddress, setRecoveredAddress] = useState(); - const [verified, setVerified] = useState(false); - - // Use wagmi to connect one's eth wallet const { connectAsync, connectors } = useConnect({ onError(error) { console.error(error); @@ -71,21 +55,7 @@ export default function Dashboard() { }); const { isConnected, connector, address } = useAccount(); const { disconnectAsync } = useDisconnect(); - - /** - * Use wagmi to connect one's eth wallet and then request a signature from one's wallet - */ - async function handleConnectWallet(c: any) { - const { account, chain, connector } = await connectAsync(c); - try { - await authWithWallet(account, connector); - } catch (err) { - console.error(err); - setError(err); - setView(Views.ERROR); - } - } - + /** * Begin auth flow with Google */ @@ -97,114 +67,6 @@ export default function Dashboard() { await provider.signIn(); } - - /** - * Request a signature from one's wallet - */ - async function authWithWallet(address: string, connector: Connector) { - setView(Views.REQUEST_AUTHSIG); - - // Create a function to handle signing messages - const signer = await connector.getSigner(); - const signAuthSig = async (message: string) => { - const sig = await signer.signMessage(message); - return sig; - }; - - // Get auth sig - const provider = litAuthClient.getProvider(ProviderType.EthWallet); - const authMethod = await provider.authenticate({ - address, - signMessage: signAuthSig, - }); - setCurrentProviderType(ProviderType.EthWallet); - setAuthMethod(authMethod); - - // Fetch PKPs associated with eth wallet account - setView(Views.FETCHING); - const pkps: IRelayPKP[] = await provider.fetchPKPsThroughRelayer( - authMethod - ); - if (pkps.length > 0) { - setPKPs(pkps); - } - setView(Views.FETCHED); - } - - async function registerWithWebAuthn() { - setView(Views.REGISTERING); - - try { - // Register new PKP - const provider = litAuthClient.getProvider( - ProviderType.WebAuthn - ) as WebAuthnProvider; - setCurrentProviderType(ProviderType.WebAuthn); - const options = await provider.register(); - - // Verify registration and mint PKP through relayer - const txHash = await provider.verifyAndMintPKPThroughRelayer(options); - setView(Views.MINTING); - const response = await provider.relay.pollRequestUntilTerminalState( - txHash - ); - if (response.status !== 'Succeeded') { - throw new Error('Minting failed'); - } - const newPKP: IRelayPKP = { - tokenId: response.pkpTokenId!, - publicKey: response.pkpPublicKey!, - ethAddress: response.pkpEthAddress!, - }; - - // Add new PKP to list of PKPs - const morePKPs: IRelayPKP[] = [...pkps, newPKP]; - setCurrentPKP(newPKP); - setPKPs(morePKPs); - - setView(Views.REGISTERED); - } catch (err) { - console.error(err); - setError(err); - setView(Views.ERROR); - } - } - - async function authenticateWithWebAuthn() { - setView(Views.AUTHENTICATING); - - try { - const provider = litAuthClient.getProvider( - ProviderType.WebAuthn - ) as WebAuthnProvider; - const authMethod = await provider.authenticate(); - setAuthMethod(authMethod); - - // Authenticate with a WebAuthn credential and create session sigs with authentication data - setView(Views.CREATING_SESSION); - - const litResource = new LitAccessControlConditionResource('*'); - const sessionSigs = await provider.getSessionSigs({ - pkpPublicKey: currentPKP.publicKey, - authMethod, - sessionSigsParams: { - chain: 'ethereum', - resourceAbilityRequests: [{ - resource: litResource, - ability: LitAbility.PKPSigning - }], - }, - }); - setSessionSigs(sessionSigs); - - setView(Views.SESSION_CREATED); - } catch (err) { - console.error(err); - setAuthMethod(null); - setError(err); - setView(Views.ERROR); - } - } /** * Handle redirect from Lit login server */ @@ -315,55 +177,7 @@ export default function Dashboard() { } } - /** - * Sign a message with current PKP - */ - async function signMessageWithPKP() { - try { - const toSign = ethers.utils.arrayify(ethers.utils.hashMessage(message)); - const litActionCode = ` - const go = async () => { - // this requests a signature share from the Lit Node - // the signature share will be automatically returned in the response from the node - // and combined into a full signature by the LitJsSdk for you to use on the client - // all the params (toSign, publicKey, sigName) are passed in from the LitJsSdk.executeJs() function - const sigShare = await LitActions.signEcdsa({ toSign, publicKey, sigName }); - }; - go(); - `; - // Sign message - // @ts-ignore - complains about no authSig, but we don't need one for this action - const results = await litNodeClient.executeJs({ - code: litActionCode, - sessionSigs: sessionSigs, - jsParams: { - toSign: toSign, - publicKey: currentPKP.publicKey, - sigName: 'sig1', - }, - }); - // Get signature - const result = results.signatures['sig1']; - const signature = ethers.utils.joinSignature({ - r: '0x' + result.r, - s: '0x' + result.s, - v: result.recid, - }); - setSignature(signature); - // Get the address associated with the signature created by signing the message - const recoveredAddr = ethers.utils.verifyMessage(message, signature); - setRecoveredAddress(recoveredAddr); - // Check if the address associated with the signature is the same as the current PKP - const verified = - currentPKP.ethAddress.toLowerCase() === recoveredAddr.toLowerCase(); - setVerified(verified); - } catch (err) { - console.error(err); - setError(err); - setView(Views.ERROR); - } - } useEffect(() => { /** @@ -389,8 +203,6 @@ export default function Dashboard() { // Initialize providers litAuthClient.initProvider(ProviderType.Google); - litAuthClient.initProvider(ProviderType.EthWallet); - litAuthClient.initProvider(ProviderType.WebAuthn); setLitAuthClient(litAuthClient); } catch (err) { @@ -458,16 +270,7 @@ export default function Dashboard() { <> {isConnected ? ( <> - + - {connectors.map(connector => ( - - ))} - )} )} - {view === Views.HANDLE_REDIRECT && ( - <> -

Verifying your identity...

- - )} - {view === Views.REQUEST_AUTHSIG && ( - <> -

Check your wallet

- - )} - {view === Views.REGISTERING && ( - <> -

Register your passkey

-

Follow your browser's prompts to create a passkey.

- - )} - {view === Views.REGISTERED && ( - <> -

Minted!

-

- Authenticate with your newly registered passkey. Continue when - you're ready. -

- - - )} - {view === Views.AUTHENTICATING && ( - <> -

Authenticate with your passkey

-

Follow your browser's prompts to create a passkey.

- - )} + {view === Views.FETCHING && ( <>

Fetching your PKPs...

@@ -554,10 +310,6 @@ export default function Dashboard() { ))} -
- {/* Or mint another PKP */} -

or mint another one:

- ) : ( <> @@ -590,22 +342,7 @@ export default function Dashboard() {

{currentPKP.ethAddress}


-
-

Sign this message with your PKP:

-

{message}

- - - {signature && ( - <> -

Your signature:

-

{signature}

-

Recovered address:

-

{recoveredAddress}

-

Verified:

-

{verified ? 'true' : 'false'}

- - )} -
+ )} diff --git a/src/lib/NewProvider.svelte b/src/lib/NewProvider.svelte deleted file mode 100644 index df4fd80..0000000 --- a/src/lib/NewProvider.svelte +++ /dev/null @@ -1,56 +0,0 @@ -