From fa9d64a7801d4c254b2081cd9c76043504b1b6d4 Mon Sep 17 00:00:00 2001 From: Nur Fikri Date: Wed, 27 Nov 2024 05:55:20 +0700 Subject: [PATCH] fix: walletconnect getAccount and getKey --- example/starter/src/ui/layout/index.tsx | 2 +- packages/graz/package.json | 2 +- packages/graz/src/actions/account.ts | 2 + .../actions/wallet/wallet-connect/index.ts | 50 ++++++++----------- 4 files changed, 25 insertions(+), 31 deletions(-) diff --git a/example/starter/src/ui/layout/index.tsx b/example/starter/src/ui/layout/index.tsx index 6f9ff8e7..b098e025 100644 --- a/example/starter/src/ui/layout/index.tsx +++ b/example/starter/src/ui/layout/index.tsx @@ -52,7 +52,7 @@ export const Layout = ({ children }: { children: ReactNode }) => { 0 0 40px #b7fcc2, 0 0 50px #b7fcc2`, animation: `${glowAnimation} 3s infinite linear alternate`, }} - w="container.md" + w={["full", "full", "container.md"]} > {isConnected ? ( diff --git a/packages/graz/package.json b/packages/graz/package.json index dc09c247..859d5ef6 100644 --- a/packages/graz/package.json +++ b/packages/graz/package.json @@ -1,7 +1,7 @@ { "name": "graz", "description": "React hooks for Cosmos", - "version": "0.1.28", + "version": "0.1.29", "author": "Griko Nibras ", "repository": "https://github.com/graz-sh/graz.git", "homepage": "https://github.com/graz-sh/graz", diff --git a/packages/graz/src/actions/account.ts b/packages/graz/src/actions/account.ts index c22f5616..6319132b 100644 --- a/packages/graz/src/actions/account.ts +++ b/packages/graz/src/actions/account.ts @@ -188,6 +188,8 @@ export const reconnect = async (args?: ReconnectArgs) => { try { const isWalletReady = checkWallet(_reconnectConnector || undefined); if (recentChains && isWalletReady && _reconnectConnector) { + const isWC = isWalletConnect(_reconnectConnector); + if (isWC) return; const key = await connect({ chainId: recentChains, walletType: _reconnectConnector, diff --git a/packages/graz/src/actions/wallet/wallet-connect/index.ts b/packages/graz/src/actions/wallet/wallet-connect/index.ts index f07aed40..31dd0527 100644 --- a/packages/graz/src/actions/wallet/wallet-connect/index.ts +++ b/packages/graz/src/actions/wallet/wallet-connect/index.ts @@ -305,24 +305,12 @@ export const getWalletConnect = (params?: GetWalletConnectParams): Wallet => { }; const getAccount = async (chainId: string): Promise => { - const { wcSignClients } = useGrazSessionStore.getState(); - const wcSignClient = wcSignClients.get(walletType); - if (!wcSignClient) throw new Error("walletConnect.signClient is not defined"); - const topic = getSession([chainId])?.topic; - if (!topic) throw new Error("No wallet connect session"); - const result: { address: string; algo: string; pubkey: string }[] = await wcSignClient.request({ - topic, - chainId: `cosmos:${chainId}`, - request: { - method: "cosmos_getAccounts", - params: {}, - }, - }); - if (!result[0]) throw new Error("No wallet connect account"); + const key = await getKey(chainId); + return { - address: result[0].address, - algo: result[0].algo as Algo, - pubkey: new Uint8Array(Buffer.from(result[0].pubkey, encoding)), + address: key.bech32Address, + algo: key.algo as Algo, + pubkey: key.pubKey, }; }; @@ -339,7 +327,10 @@ export const getWalletConnect = (params?: GetWalletConnectParams): Wallet => { const key = keys.find((x) => x.chainId === chainId); if (!key) throw new Error(`No wallet connect key for chainId ${chainId}`); - return key; + return { + ...key, + pubKey: Buffer.from(String(key.pubKey), encoding) as unknown as Uint8Array, + }; }; const wcSignDirect = async (...args: SignDirectParams) => { @@ -354,7 +345,8 @@ export const getWalletConnect = (params?: GetWalletConnectParams): Wallet => { if (!signDoc.bodyBytes) throw new Error("No bodyBytes"); if (!signDoc.authInfoBytes) throw new Error("No authInfoBytes"); - const req = { + redirectToApp(); + const result: WalletConnectSignDirectResponse = await wcSignClient.request({ topic, chainId: `cosmos:${chainId}`, request: { @@ -362,16 +354,14 @@ export const getWalletConnect = (params?: GetWalletConnectParams): Wallet => { params: { signerAddress: signer, signDoc: { - ...signDoc, - bodyBytes: Buffer.from(signDoc.bodyBytes).toString(encoding), - authInfoBytes: Buffer.from(signDoc.authInfoBytes).toString(encoding), + chainId: signDoc.chainId, accountNumber: signDoc.accountNumber?.toString(), + bodyBytes: signDoc.bodyBytes ? Buffer.from(signDoc.bodyBytes).toString(encoding) : null, + authInfoBytes: signDoc.authInfoBytes ? Buffer.from(signDoc.authInfoBytes).toString(encoding) : null, }, }, }, - }; - redirectToApp(); - const result: WalletConnectSignDirectResponse = await wcSignClient.request(req); + }); return result; }; @@ -380,10 +370,12 @@ export const getWalletConnect = (params?: GetWalletConnectParams): Wallet => { const { signature, signed } = await wcSignDirect(chainId, signer, signDoc); return { signed: { - chainId: signed.chainId, - accountNumber: Long.fromString(signed.accountNumber, false), - authInfoBytes: new Uint8Array(Buffer.from(signed.authInfoBytes, encoding)), - bodyBytes: new Uint8Array(Buffer.from(signed.bodyBytes, encoding)), + chainId: signed.chainId ?? "", + accountNumber: signed.accountNumber ? Long.fromString(signed.accountNumber) : new Long(0), + authInfoBytes: signed.authInfoBytes + ? new Uint8Array(Buffer.from(signed.authInfoBytes, encoding)) + : new Uint8Array([]), + bodyBytes: signed.bodyBytes ? new Uint8Array(Buffer.from(signed.bodyBytes, encoding)) : new Uint8Array([]), }, signature, };