From 689bd4c2bb50b6a6fd082a4d4d2d77da84ebbf49 Mon Sep 17 00:00:00 2001 From: Diana Savatina Date: Thu, 19 Dec 2024 14:39:00 +0000 Subject: [PATCH] tezos_getAccounts --- .../WalletConnect/useHandleWcRequest.tsx | 42 ++++++++++++++++--- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/apps/web/src/components/WalletConnect/useHandleWcRequest.tsx b/apps/web/src/components/WalletConnect/useHandleWcRequest.tsx index 47c5facca4..85604d0604 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 { @@ -6,8 +7,10 @@ import { useFindNetwork, useGetImplicitAccount, useGetOwnedAccountSafe, + walletKit, } from "@umami/state"; import { WalletConnectError } from "@umami/utils"; +import { formatJsonRpcResult } from "@walletconnect/jsonrpc-utils"; import { type SessionTypes, type SignClientTypes, type Verify } from "@walletconnect/types"; import { SignPayloadRequestModal } from "../common/SignPayloadRequestModal"; @@ -18,7 +21,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 * @@ -31,6 +33,7 @@ export const useHandleWcRequest = () => { const getAccount = useGetOwnedAccountSafe(); const getImplicitAccount = useGetImplicitAccount(); const findNetwork = useFindNetwork(); + const toast = useToast(); return async ( event: { @@ -54,11 +57,38 @@ 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 = getAccount(accountPkh); + if (!signer) { + throw new WalletConnectError( + `Unknown account, no signer: ${accountPkh}`, + "UNAUTHORIZED_EVENT", + session + ); + } + const response = formatJsonRpcResult(id, [ + { + algo: "ed25519", + address: accountPkh, + pubkey: accountPkh, + }, + ]); + await walletKit.respondSessionRequest({ topic, response }); + + toast({ + description: "Successfully signed the payload", + status: "success", + }); + return; } case "tezos_sign": {