From 4ba6302c0daaf04de8209bc421d934b08f955f40 Mon Sep 17 00:00:00 2001 From: Nur Fikri Date: Sat, 30 Sep 2023 12:53:03 +0700 Subject: [PATCH 1/2] fix: wallet connect multi prompt --- packages/graz/src/actions/account.ts | 8 +++++--- packages/graz/src/actions/wallet/index.ts | 9 +++++++++ .../src/actions/wallet/wallet-connect/index.ts | 17 ++++++++++++++--- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/packages/graz/src/actions/account.ts b/packages/graz/src/actions/account.ts index b55ea34b..6f7fa6e6 100644 --- a/packages/graz/src/actions/account.ts +++ b/packages/graz/src/actions/account.ts @@ -6,7 +6,7 @@ import { RECONNECT_SESSION_KEY } from "../constant"; import { grazSessionDefaultValues, useGrazInternalStore, useGrazSessionStore } from "../store"; import type { Maybe } from "../types/core"; import type { WalletType } from "../types/wallet"; -import { checkWallet, getWallet } from "./wallet"; +import { checkWallet, getWallet, isWalletConnect } from "./wallet"; export type ConnectArgs = Maybe<{ chain?: GrazChain; @@ -53,8 +53,10 @@ export const connect = async (args?: ConnectArgs): Promise => { await wallet.init?.(); if (!_account || activeChain?.chainId !== chain.chainId) { await wallet.enable(chain.chainId); - const account = await wallet.getKey(chain.chainId); - useGrazSessionStore.setState({ account }); + if (!isWalletConnect(currentWalletType)) { + const account = await wallet.getKey(chain.chainId); + useGrazSessionStore.setState({ account }); + } } useGrazInternalStore.setState({ diff --git a/packages/graz/src/actions/wallet/index.ts b/packages/graz/src/actions/wallet/index.ts index f2ab36ff..ca92cc36 100644 --- a/packages/graz/src/actions/wallet/index.ts +++ b/packages/graz/src/actions/wallet/index.ts @@ -85,3 +85,12 @@ export const getWallet = (type: WalletType = useGrazInternalStore.getState().wal export const getAvailableWallets = (): Record => { return Object.fromEntries(WALLET_TYPES.map((type) => [type, checkWallet(type)])) as Record; }; + +export const isWalletConnect = (type: WalletType): boolean => { + return ( + type === WalletType.WALLETCONNECT || + type === WalletType.WC_KEPLR_MOBILE || + type === WalletType.WC_LEAP_MOBILE || + type === WalletType.WC_COSMOSTATION_MOBILE + ); +}; diff --git a/packages/graz/src/actions/wallet/wallet-connect/index.ts b/packages/graz/src/actions/wallet/wallet-connect/index.ts index 30c40af8..030b25e3 100644 --- a/packages/graz/src/actions/wallet/wallet-connect/index.ts +++ b/packages/graz/src/actions/wallet/wallet-connect/index.ts @@ -227,6 +227,7 @@ export const getWalletConnect = (params?: GetWalletConnectParams): Wallet => { const { account, activeChain } = useGrazSessionStore.getState(); const lastSession = checkSession(chainId); + if ((activeChain?.chainId !== chainId && !lastSession) || !account) { const { uri, approval } = await signClient.connect({ requiredNamespaces: { @@ -246,9 +247,20 @@ export const getWalletConnect = (params?: GetWalletConnectParams): Wallet => { const approving = async (signal: AbortSignal) => { if (signal.aborted) return Promise.reject(new Error("User closed wallet connect")); - return new Promise((resolve, reject) => { - approval().then(resolve).catch(reject); + approval() + .then((d) => { + const sessionProperties = d.sessionProperties; + if (!sessionProperties) return reject(new Error("No session properties")); + const _acc: (Key & { chainId?: string })[] = JSON.parse(String(sessionProperties.keys)); + const resAcc = _acc.find((i) => i.chainId === chainId); + if (!resAcc) return reject(new Error("No account")); + useGrazSessionStore.setState({ + account: resAcc, + }); + return resolve(d); + }) + .catch(reject); signal.addEventListener("abort", () => { reject(new Error("User closed wallet connect")); }); @@ -304,7 +316,6 @@ export const getWalletConnect = (params?: GetWalletConnectParams): Wallet => { params: {}, }, }); - if (!result[0]) throw new Error("No wallet connect account"); return { address: result[0].address, From ecdea5f8abe870329e0a22372979fae6af3e8b2a Mon Sep 17 00:00:00 2001 From: Nur Fikri Date: Sat, 30 Sep 2023 13:34:35 +0700 Subject: [PATCH 2/2] feat: v0.0.51 --- packages/graz/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/graz/package.json b/packages/graz/package.json index ecd62791..89c62e20 100644 --- a/packages/graz/package.json +++ b/packages/graz/package.json @@ -1,7 +1,7 @@ { "name": "graz", "description": "React hooks for Cosmos", - "version": "0.0.50", + "version": "0.0.51", "author": "Griko Nibras ", "repository": "https://github.com/graz-sh/graz.git", "homepage": "https://github.com/graz-sh/graz",