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

Feat(core)/bump dep #178

Open
wants to merge 12 commits into
base: dev
Choose a base branch
from
10 changes: 8 additions & 2 deletions packages/graz/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
"prepublishOnly": "pnpm build"
},
"peerDependencies": {
"@cosmjs/amino": "0.32.4",
"@cosmjs/cosmwasm-stargate": "0.32.4",
"@cosmjs/launchpad": "*",
"@cosmjs/proto-signing": "0.32.4",
"@cosmjs/stargate": "0.32.4",
"@cosmjs/tendermint-rpc": "0.32.4",
"@leapwallet/cosmos-social-login-capsule-provider": "^0.0.41",
"@tanstack/react-query": ">=5.62.0",
"react": ">=17"
Expand All @@ -58,14 +64,14 @@
"@dao-dao/cosmiframe": "0.1.0",
"@keplr-wallet/cosmos": "0.12.156",
"@metamask/providers": "12.0.0",
"@terra-money/station-connector": "1.1.0",
"@tanstack/react-query": "4.35.0",
"@terra-money/station-connector": "1.1.4",
"@vectis/extension-client": "^0.7.2",
"@walletconnect/sign-client": "2.17.2",
"@walletconnect/types": "2.17.2",
"@walletconnect/utils": "2.17.2",
"@walletconnect/modal": "2.7.0",
"cosmos-directory-client": "0.0.6",
"long": "4",
"zustand": "4.5.2"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/graz/src/actions/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export const reconnect = async (args?: ReconnectArgs) => {
};

export interface OfflineSigners {
offlineSigner: OfflineAminoSigner & OfflineDirectSigner;
offlineSigner: OfflineAminoSigner | OfflineDirectSigner;
offlineSignerAmino: OfflineAminoSigner;
offlineSignerAuto: OfflineAminoSigner | OfflineDirectSigner;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CosmosSnap, installSnap, isSnapInstalled } from "@cosmsnap/snapper";
import type { MetaMaskInpageProvider } from "@metamask/providers";

import { useGrazInternalStore } from "../../../store";
import type { KnownKeys, Wallet } from "../../../types/wallet";
Expand All @@ -7,7 +8,7 @@ import type { ChainId } from "../../../utils/multi-chain";
const metamaskSnapCosmosKeysMap: KnownKeys = {};

export const getMetamaskSnapCosmos = (): Wallet => {
const ethereum = window.ethereum;
const ethereum = window.ethereum as MetaMaskInpageProvider | undefined;
let cosmos = window.cosmos;
if (ethereum) {
const init = async () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/graz/src/actions/wallet/cosmostation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ export const getCosmostation = (): Wallet => {
const setDefaultOptions = (options: KeplrIntereactionOptions) => {
cosmostation.defaultOptions = options;
};
// TODO: CHECK IF THIS IS THE CORRECT WAY TO CAST
const res = Object.assign(cosmostation, {
subscription,
setDefaultOptions,
});
}) as unknown as Wallet;
return res;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/graz/src/actions/wallet/initia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { clearSession } from ".";
export interface InitiaWallet {
getVersion: () => Promise<string>;
getAddress: (chainId?: string) => Promise<string>;
getOfflineSigner: (chainId: string) => OfflineDirectSigner;
getOfflineSigner: (chainId: string) => OfflineDirectSigner & OfflineAminoSigner;
getOfflineSignerOnlyAmino: (chainId: string) => OfflineAminoSigner;
requestAddInitiaLayer: (chain: Partial<Chain>) => Promise<void>;
signAndBroadcastSync: (chainId: string, txBody: Uint8Array, gas: number) => Promise<string>;
Expand Down Expand Up @@ -85,7 +85,7 @@ export const getInitia = (): Wallet => {
};
};

const getOfflineSigner = (chainId: string) => {
const getOfflineSigner = (chainId: string): OfflineDirectSigner & OfflineAminoSigner => {
const directSigner = initia.getOfflineSigner(chainId);
const aminoSigner = initia.getOfflineSignerOnlyAmino(chainId);

Expand Down
4 changes: 3 additions & 1 deletion packages/graz/src/actions/wallet/keplr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ export const getKeplr = (): Wallet => {
const setDefaultOptions = (options: KeplrIntereactionOptions) => {
keplr.defaultOptions = options;
};

// TODO: CHECK IF THIS IS THE CORRECT WAY TO CAST
const res = Object.assign(keplr, {
subscription,
setDefaultOptions,
});
}) as unknown as Wallet;

return res;
}
Expand Down
5 changes: 4 additions & 1 deletion packages/graz/src/actions/wallet/leap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ export const getLeap = (): Wallet => {
const setDefaultOptions = (options: KeplrIntereactionOptions) => {
leap.defaultOptions = options;
};

// TODO: CHECK IF THIS IS THE CORRECT WAY TO CAST
const res = Object.assign(leap, {
subscription,
setDefaultOptions,
});
}) as unknown as Wallet;

return res;
}

Expand Down
4 changes: 3 additions & 1 deletion packages/graz/src/actions/wallet/okx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ export const getOkx = (): Wallet => {
const setDefaultOptions = (options: KeplrIntereactionOptions) => {
okxWallet.defaultOptions = options;
};

// TODO: CHECK IF THIS IS THE CORRECT WAY TO CAST
const res = Object.assign(okxWallet, {
subscription,
setDefaultOptions,
});
}) as unknown as Wallet;
return res;
}

Expand Down
24 changes: 18 additions & 6 deletions packages/graz/src/actions/wallet/vectis.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { AminoSignResponse } from "@cosmjs/amino";
import type { AminoSignResponse, OfflineAminoSigner } from "@cosmjs/amino";
import { fromBech32 } from "@cosmjs/encoding";
import type { DirectSignResponse } from "@cosmjs/proto-signing";
import type { DirectSignResponse, OfflineDirectSigner } from "@cosmjs/proto-signing";
import type { ChainInfo } from "@vectis/extension-client";
import Long from "long";

import { useGrazInternalStore } from "../../store";
import type { Key, SignAminoParams, SignDirectParams, Wallet } from "../../types/wallet";
import { clearSession } from ".";
import { ChainInfo } from "@vectis/extension-client";

/**
* Function to return {@link Wallet} object and throws and error if it does not exist on `window`.
Expand Down Expand Up @@ -48,7 +48,7 @@ export const getVectis = (): Wallet => {
rpcUrl: chainInfo.rpc,
restUrl: chainInfo.rest,
prettyName: chainInfo.chainName.replace(" ", ""),
bech32Prefix: chainInfo.bech32Config?.bech32PrefixAccAddr,
bech32Prefix: chainInfo.bech32Config.bech32PrefixAccAddr,
currencies: chainInfo.currencies,
feeCurrencies: chainInfo.feeCurrencies,
chainId: chainInfo.chainId,
Expand All @@ -75,23 +75,35 @@ export const getVectis = (): Wallet => {

const signDirect = async (...args: SignDirectParams): Promise<DirectSignResponse> => {
const { 1: signer, 2: signDoc } = args;
return vectis.signDirect(signer, {
const response = await vectis.signDirect(signer, {
bodyBytes: signDoc.bodyBytes || Uint8Array.from([]),
authInfoBytes: signDoc.authInfoBytes || Uint8Array.from([]),
accountNumber: Long.fromString(signDoc.accountNumber?.toString() || "", false),
chainId: signDoc.chainId || "",
});

return {
...response,
signed: {
...response.signed,
accountNumber: BigInt(signDoc.accountNumber ?? 0),
},
};
};

const signAmino = async (...args: SignAminoParams): Promise<AminoSignResponse> => {
const { 1: signer, 2: signDoc } = args;
return vectis.signAmino(signer, signDoc);
};

const getOfflineSignerAuto = (chainId: string): Promise<OfflineAminoSigner | OfflineDirectSigner> => {
return vectis.getOfflineSignerAuto(chainId) as unknown as Promise<OfflineAminoSigner | OfflineDirectSigner>;
};

return {
enable: (chainId: string | string[]) => vectis.enable(chainId),
getOfflineSigner: (chainId: string) => vectis.getOfflineSigner(chainId),
getOfflineSignerAuto: (chainId: string) => vectis.getOfflineSignerAuto(chainId),
getOfflineSignerAuto,
getKey,
subscription,
getOfflineSignerOnlyAmino,
Expand Down
30 changes: 15 additions & 15 deletions packages/graz/src/actions/wallet/wallet-connect/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import type { AminoSignResponse } from "@cosmjs/amino";
import type { AccountData, Algo, DirectSignResponse } from "@cosmjs/proto-signing";
import type { Keplr } from "@keplr-wallet/types";
import { WalletConnectModal } from "@walletconnect/modal";
import { SignClient } from "@walletconnect/sign-client";
import type { ISignClient, SignClientTypes } from "@walletconnect/types";
import { getSdkError } from "@walletconnect/utils";
import Long from "long";
import type { SignDoc } from "cosmjs-types/cosmos/tx/v1beta1/tx";

import { useGrazInternalStore, useGrazSessionStore } from "../../../store";
import { Key, type SignAminoParams, type SignDirectParams, type Wallet, WalletType } from "../../../types/wallet";
import type { Key } from "../../../types/wallet";
import { type SignAminoParams, type SignDirectParams, type Wallet, WalletType } from "../../../types/wallet";
import { isAndroid, isIos, isMobile } from "../../../utils/os";
import { promiseWithTimeout } from "../../../utils/timeout";
import type { GetWalletConnectParams, WalletConnectSignDirectResponse } from "./types";
import { WalletConnectModal } from "@walletconnect/modal";

export const getWalletConnect = (params?: GetWalletConnectParams): Wallet => {
if (!useGrazInternalStore.getState().walletConnect?.options?.projectId?.trim()) {
Expand Down Expand Up @@ -197,6 +198,7 @@ export const getWalletConnect = (params?: GetWalletConnectParams): Wallet => {
// "f896cbca30cd6dc414712d3d6fcc2f8f7d35d5bd30e3b1fc5d60cf6c8926f98f",
// ],
});

const lastSession = checkSession(chainId);
if (!lastSession) {
const { uri, approval } = await promiseWithTimeout(
Expand Down Expand Up @@ -268,7 +270,8 @@ export const getWalletConnect = (params?: GetWalletConnectParams): Wallet => {
await approving(signal);
} catch (error) {
walletConnectModal.closeModal();
if (!(error as Error).message.toLowerCase().includes("no matching key")) return Promise.reject(error);
if (!(error as Error).message.toLowerCase().includes("no matching key"))
return Promise.reject(new Error(error as string));
}
if (!params) {
walletConnectModal.closeModal();
Expand All @@ -280,7 +283,7 @@ export const getWalletConnect = (params?: GetWalletConnectParams): Wallet => {
(async () => {
const resultAcccounts = Object.fromEntries(
await Promise.all(
(activeChainIds || chainId)?.map(async (c): Promise<[string, Key]> => [c, await getKey(c)]),
(activeChainIds || chainId).map(async (c): Promise<[string, Key]> => [c, await getKey(c)]),
),
);
useGrazSessionStore.setState({
Expand Down Expand Up @@ -348,8 +351,8 @@ export const getWalletConnect = (params?: GetWalletConnectParams): Wallet => {
signDoc: {
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,
bodyBytes: Buffer.from(signDoc.bodyBytes).toString(encoding) || null,
authInfoBytes: Buffer.from(signDoc.authInfoBytes).toString(encoding) || null,
},
},
},
Expand All @@ -362,12 +365,10 @@ export const getWalletConnect = (params?: GetWalletConnectParams): Wallet => {
const { signature, signed } = await wcSignDirect(chainId, signer, signDoc);
return {
signed: {
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([]),
chainId: signed.chainId,
accountNumber: BigInt(signed.accountNumber),
authInfoBytes: new Uint8Array(Buffer.from(signed.authInfoBytes, encoding)),
bodyBytes: new Uint8Array(Buffer.from(signed.bodyBytes, encoding)),
},
signature,
};
Expand Down Expand Up @@ -408,8 +409,7 @@ export const getWalletConnect = (params?: GetWalletConnectParams): Wallet => {
const getOfflineSignerDirect = (chainId: string) => {
return {
getAccounts: async () => [await getAccount(chainId)],
signDirect: (signerAddress: string, signDoc: SignDirectParams["2"]) =>
signDirect(chainId, signerAddress, signDoc),
signDirect: (signerAddress: string, signDoc: SignDoc) => signDirect(chainId, signerAddress, signDoc),
};
};

Expand Down
3 changes: 2 additions & 1 deletion packages/graz/src/actions/wallet/xdefi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ export const getXDefi = (): Wallet => {
window.removeEventListener("keplr_keystorechange", listener);
};
};
// TODO: CHECK IF THIS IS THE CORRECT WAY TO CAST
const res = Object.assign(xdefi, {
subscription,
});
}) as unknown as Wallet;

return res;
}
Expand Down
4 changes: 3 additions & 1 deletion packages/graz/src/hooks/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ export const useBalances = <TMulti extends MultiChainHookArgs>(
if (!stargateClient) {
throw new Error(`Client is not ready ${_chain.chainId}`);
}
if (!_chain.bech32Config?.bech32PrefixAccAddr) throw new Error(`Bech32Config is missing ${_chain.chainId}`);
if (!_chain.bech32Config) {
throw new Error(`bech32Config is not defined for ${_chain.chainId}`);
}
const balances = await stargateClient.getAllBalances(
toBech32(_chain.bech32Config.bech32PrefixAccAddr, fromBech32(_address).data),
);
Expand Down
5 changes: 3 additions & 2 deletions packages/graz/src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import type { ChainInfo, Keplr } from "@keplr-wallet/types";
import type { CapsuleProvider } from "@leapwallet/cosmos-social-login-capsule-provider";
import type { ISignClient, SignClientTypes } from "@walletconnect/types";
import type { WalletConnectModalConfig } from "@walletconnect/modal";
import type { ISignClient, SignClientTypes } from "@walletconnect/types";
import { create } from "zustand";
import type { PersistOptions } from "zustand/middleware";
import { createJSONStorage } from "zustand/middleware";
import { persist, subscribeWithSelector } from "zustand/middleware";

import type { Dictionary } from "../types/core";
import { Key, WalletType } from "../types/wallet";
import type { Key } from "../types/wallet";
import { WalletType } from "../types/wallet";

export interface ChainConfig {
path?: string;
Expand Down
2 changes: 2 additions & 0 deletions packages/graz/src/types/tendermint.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { QueryClient } from "@cosmjs/stargate";
import type { Tendermint34Client, Tendermint37Client } from "@cosmjs/tendermint-rpc";

export type ExtensionSetup<P extends object = object> = (queryClient: QueryClient) => P;
export type TendermintClient = Tendermint37Client | Tendermint34Client;
28 changes: 23 additions & 5 deletions packages/graz/src/types/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import type { ChainInfo, Keplr, KeplrIntereactionOptions, Key as KeplrKey } from "@keplr-wallet/types";
import type { OfflineAminoSigner } from "@cosmjs/amino";
import type { DirectSignResponse, OfflineDirectSigner } from "@cosmjs/proto-signing";
import type {
ChainInfo,
Keplr,
KeplrIntereactionOptions,
KeplrSignOptions,
Key as KeplrKey,
} from "@keplr-wallet/types";

export enum WalletType {
KEPLR = "keplr",
Expand Down Expand Up @@ -48,10 +56,7 @@ export const WALLET_TYPES = [
WalletType.OKX,
];

export type Wallet = Pick<
Keplr,
"enable" | "getOfflineSigner" | "getOfflineSignerAuto" | "getOfflineSignerOnlyAmino" | "signDirect" | "signAmino"
> & {
export type Wallet = Pick<Keplr, "enable" | "getOfflineSignerOnlyAmino" | "signAmino"> & {
experimentalSuggestChain: (chainInfo: Omit<ChainInfo, "nodeProvider">) => Promise<void>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's not override getOfflineSigner, getOfflineSignerAuto, signDirect

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use the same structure as Keplr type, but using the OfflineAminoSigner & OfflineDirectSigner directly from @cosmjs, because Keplr type still use Long instead of BigInt

signArbitrary?: Keplr["signArbitrary"];
subscription?: (reconnect: () => void) => () => void;
Expand All @@ -60,6 +65,19 @@ export type Wallet = Pick<
setDefaultOptions?: (options: KeplrIntereactionOptions) => void;
onAfterLoginSuccessful?: () => Promise<void>;
getKey: (chainId: string) => Promise<Key>;
getOfflineSigner: (chainId: string, signOptions?: KeplrSignOptions) => OfflineAminoSigner | OfflineDirectSigner;
getOfflineSignerAuto: (chainId: string) => Promise<OfflineDirectSigner | OfflineAminoSigner>;
signDirect: (
chainId: string,
signer: string,
signDoc: {
bodyBytes?: Uint8Array | null;
authInfoBytes?: Uint8Array | null;
chainId?: string | null;
accountNumber?: bigint | null;
},
signOptions?: KeplrSignOptions,
) => Promise<DirectSignResponse>;
signEthereum?: Keplr["signEthereum"];
experimentalSignEIP712CosmosTx_v0?: Keplr["experimentalSignEIP712CosmosTx_v0"];
};
Expand Down
Loading
Loading