From 5b7c49fd581ba596c5de1dc6c2b74f33b0b64c96 Mon Sep 17 00:00:00 2001 From: Samuel Andert Date: Mon, 24 Jul 2023 12:54:59 +0200 Subject: [PATCH] feat(wallet): Refactor wallet service for enhanced modularity and clarity - Abstracted RPC interface to allow dynamic input for connection flexibility. - Added `signSession` function to retrieve `authSig` efficiently. - Improved logging and error handling to provide clearer feedback. - Centralized `authSig` retrieval for better code maintainability. This refactor ensures the wallet service is more adaptable to varying use cases and improves overall code readability. --- src/lib/components/Wallet.svelte | 33 +++++++-------- src/lib/services/wallet/README.md | 36 ++++++++++++++++ src/lib/services/wallet/wallet.ts | 68 +++++++++++++++++++++++++++++++ src/routes/+page.svelte | 10 ++++- 4 files changed, 129 insertions(+), 18 deletions(-) create mode 100644 src/lib/services/wallet/README.md create mode 100644 src/lib/services/wallet/wallet.ts diff --git a/src/lib/components/Wallet.svelte b/src/lib/components/Wallet.svelte index e93c836..ded63eb 100644 --- a/src/lib/components/Wallet.svelte +++ b/src/lib/components/Wallet.svelte @@ -1,21 +1,22 @@ - -
- - - - - -
+{#if pkpWallet} +
+ PKP Wallet: {pkpWallet.address} +
+{/if} diff --git a/src/lib/services/wallet/README.md b/src/lib/services/wallet/README.md new file mode 100644 index 0000000..9b35781 --- /dev/null +++ b/src/lib/services/wallet/README.md @@ -0,0 +1,36 @@ +# `wallet.ts` Service Documentation + +The `wallet.ts` service provides utilities for working with the PKPEthersWallet, including connecting and retrieving the authentication signature. + +## Dependencies + +- `getStoredAuthSig` from `'$lib/services/authWithMetamask/authWithMetamask'` +- `PKPEthersWallet` from `'@lit-protocol/pkp-ethers'` +- `createMessage` from `'$lib/services/messages'` + +## Variables + +- **isWalletConnected**: A boolean flag that keeps track of whether the wallet is currently connected or not. + +## Functions + +### `connectWallet(pkpPubKey: string, rpcURL: string = 'https://rpc.gnosischain.com/')` + +#### Parameters: +- **pkpPubKey**: The public key used to connect to the wallet. +- **rpcURL** (Optional): The RPC URL for the connection. Defaults to `'https://rpc.gnosischain.com/'`. + +#### Returns: +- A promise which resolves to an instance of `PKPEthersWallet` if the wallet is successfully connected. If the wallet is already connected or there's an error in getting the `authSig`, it returns `null`. + +#### Description: +This function first checks if the wallet is already connected. If it's not, it attempts to retrieve the `authSig` from local storage. If successful, it connects to the wallet using the provided `pkpPubKey` and `rpcURL`. Once the wallet is successfully connected, it sends a success message and sets the `isWalletConnected` flag to `true`. + +### `signSession()` + +#### Returns: +- The `authSig` from local storage if it exists, or `null` otherwise. + +#### Description: +This function retrieves the `authSig` from local storage and returns it. If the `authSig` does not exist in local storage, an error message is sent, and the function returns `null`. + diff --git a/src/lib/services/wallet/wallet.ts b/src/lib/services/wallet/wallet.ts new file mode 100644 index 0000000..e5cd07a --- /dev/null +++ b/src/lib/services/wallet/wallet.ts @@ -0,0 +1,68 @@ +import { getStoredAuthSig } from '$lib/services/authWithMetamask/authWithMetamask'; +import { PKPEthersWallet } from '@lit-protocol/pkp-ethers'; +import { createMessage } from '$lib/services/messages'; + +// Use a variable to keep track of whether the wallet has been connected or not +let isWalletConnected = false; + +export async function connectWallet(pkpPubKey: string, rpcURL: string = 'https://rpc.gnosischain.com/'): Promise { + if (isWalletConnected) { + createMessage({ + text: 'Attempted to connect PKPEthersWallet, but it is already connected.', + sender: '$lib/services/wallet/wallet.ts', + type: 'INFO' + }); + return null; + } + + const authSig = getStoredAuthSig(); + + if (!authSig) { + createMessage({ + text: 'No authSig parsed from local storage.', + sender: '$lib/services/wallet/wallet.ts', + type: 'ERROR' + }); + alert('No authSig parsed from local storage'); + return null; + } + + createMessage({ + text: `Connecting PKPEthersWallet with provided pkpPubKey using RPC: ${rpcURL}`, + sender: '$lib/services/wallet/wallet.ts', + type: 'SYSTEM' + }); + + const pkpWallet = new PKPEthersWallet({ + controllerAuthSig: authSig, + pkpPubKey, + rpc: rpcURL + }); + + await pkpWallet.init(); + + createMessage({ + text: 'PKPEthersWallet connected successfully.', + sender: '$lib/services/wallet/wallet.ts', + type: 'SUCCESS' + }); + + isWalletConnected = true; + + return pkpWallet; +} + +// Function to return the authSig +export function signSession(): string | null { + const authSig = getStoredAuthSig(); + if (!authSig) { + createMessage({ + text: 'No authSig found in local storage.', + sender: '$lib/services/wallet/wallet.ts', + type: 'ERROR' + }); + return null; + } + + return authSig; +} diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 046c999..b4f8df5 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -7,14 +7,20 @@ "login", "main" "footer"; - grid-template-rows: 150px 1fr auto; + grid-template-rows: 1fr 1fr auto; `, children: [ { id: 1, componentName: 'Login', props: {}, - slot: 'login' + slot: 'login', + children: [ + { + id: 5, + componentName: 'Wallet' + } + ] }, { id: 2,