From 433206b22254b538fa2ef42fb207875e7457a4ab Mon Sep 17 00:00:00 2001 From: Diana Savatina Date: Thu, 19 Dec 2024 14:39:00 +0000 Subject: [PATCH] feat: WalletConnect integration, part 9, getAccounts --- .../WalletConnect/useHandleWcRequest.tsx | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/apps/web/src/components/WalletConnect/useHandleWcRequest.tsx b/apps/web/src/components/WalletConnect/useHandleWcRequest.tsx index c5186cd0ff..8ccc3389b0 100644 --- a/apps/web/src/components/WalletConnect/useHandleWcRequest.tsx +++ b/apps/web/src/components/WalletConnect/useHandleWcRequest.tsx @@ -1,4 +1,5 @@ import { SigningType } from "@airgap/beacon-wallet"; +import { useToast } from "@chakra-ui/react"; import { useDynamicModalContext } from "@umami/components"; import { type ImplicitAccount, estimate, toAccountOperations } from "@umami/core"; import { @@ -9,7 +10,7 @@ import { walletKit, } from "@umami/state"; import { WalletConnectError } from "@umami/utils"; -import { formatJsonRpcError } from "@walletconnect/jsonrpc-utils"; +import { formatJsonRpcError, formatJsonRpcResult } from "@walletconnect/jsonrpc-utils"; import { type SessionTypes, type SignClientTypes, type Verify } from "@walletconnect/types"; import { type SdkErrorKey, getSdkError } from "@walletconnect/utils"; @@ -21,7 +22,6 @@ import { type SignHeaderProps, type SignPayloadProps, } from "../SendFlow/utils"; - /** * @returns a function that handles a beacon message and opens a modal with the appropriate content * @@ -34,6 +34,7 @@ export const useHandleWcRequest = () => { const getAccount = useGetOwnedAccountSafe(); const getImplicitAccount = useGetImplicitAccount(); const findNetwork = useFindNetwork(); + const toast = useToast(); return async ( event: { @@ -57,11 +58,28 @@ export const useHandleWcRequest = () => { switch (request.method) { case "tezos_getAccounts": { - throw new WalletConnectError( - "Getting accounts is not supported yet", - "WC_METHOD_UNSUPPORTED", - session - ); + const wcPeers = walletKit.getActiveSessions(); + if (!(topic in wcPeers)) { + throw new WalletConnectError(`Unknown session ${topic}`, "UNAUTHORIZED_EVENT", null); + } + const session = wcPeers[topic]; + const accountPkh = session.namespaces.tezos.accounts[0].split(":")[2]; + const signer = getImplicitAccount(accountPkh); + const publicKey = signer.pk; + const response = formatJsonRpcResult(id, [ + { + algo: "ed25519", // the only supported curve + address: accountPkh, + pubkey: publicKey, + }, + ]); + await walletKit.respondSessionRequest({ topic, response }); + + toast({ + description: "Successfully provided the requested account data", + status: "success", + }); + return; } case "tezos_sign": {