Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wallet connect multi prompt #119

Merged
merged 2 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/graz/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "graz",
"description": "React hooks for Cosmos",
"version": "0.0.50",
"version": "0.0.51",
"author": "Griko Nibras <[email protected]>",
"repository": "https://github.com/graz-sh/graz.git",
"homepage": "https://github.com/graz-sh/graz",
Expand Down
8 changes: 5 additions & 3 deletions packages/graz/src/actions/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -53,8 +53,10 @@ export const connect = async (args?: ConnectArgs): Promise<ConnectResult> => {
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({
Expand Down
9 changes: 9 additions & 0 deletions packages/graz/src/actions/wallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,12 @@ export const getWallet = (type: WalletType = useGrazInternalStore.getState().wal
export const getAvailableWallets = (): Record<WalletType, boolean> => {
return Object.fromEntries(WALLET_TYPES.map((type) => [type, checkWallet(type)])) as Record<WalletType, boolean>;
};

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
);
};
17 changes: 14 additions & 3 deletions packages/graz/src/actions/wallet/wallet-connect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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"));
});
Expand Down Expand Up @@ -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,
Expand Down