From e17f5db9074c9232a781a28b39fbd0d54af686b0 Mon Sep 17 00:00:00 2001 From: lukachi Date: Thu, 21 Mar 2024 13:15:42 +0200 Subject: [PATCH] restructurize wallet, add keplr helpers --- packages/client/src/client.ts | 10 +++++--- packages/client/src/helpers/index.ts | 1 + packages/client/src/helpers/keplr.ts | 35 ++++++++++++++++++++++++++++ packages/client/src/index.ts | 1 + packages/client/src/types/client.ts | 1 + packages/client/src/wallet.ts | 29 ++++------------------- 6 files changed, 50 insertions(+), 27 deletions(-) create mode 100644 packages/client/src/helpers/keplr.ts diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index 8d08361d6..0805cedf3 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -1,9 +1,8 @@ import { ref, toRaw } from '@distributedlab/reactivity' -import { stub } from '@/helpers' +import { getChainInfo as getChainInfoHelper, stub } from '@/helpers' import { makeRarimoBroadcaster } from './broadcaster' -import { getChainInfo } from './helpers' import { makeRarimoQuerier } from './querier' import type { Config, @@ -24,8 +23,12 @@ export const makeRarimoClient = (config: Config): RarimoClient => { wallet.value.disconnect() } + const getChainInfo = async () => { + return getChainInfoHelper(config, query.value) + } + const connect = async (injectedWallet?: Wallet) => { - const chainInfo = await getChainInfo(config, query.value) + const chainInfo = await getChainInfo() const _wallet = injectedWallet || makeWallet() await _wallet.connect(chainInfo) @@ -39,6 +42,7 @@ export const makeRarimoClient = (config: Config): RarimoClient => { wallet, disconnect, connect, + getChainInfo, query, tx, }) diff --git a/packages/client/src/helpers/index.ts b/packages/client/src/helpers/index.ts index 0b5bf1109..d410c6fcc 100644 --- a/packages/client/src/helpers/index.ts +++ b/packages/client/src/helpers/index.ts @@ -1,4 +1,5 @@ export * from './broadcast-maker' export * from './chain-info' +export * from './keplr' export * from './request' export * from './stub' diff --git a/packages/client/src/helpers/keplr.ts b/packages/client/src/helpers/keplr.ts new file mode 100644 index 000000000..ba03cc1e3 --- /dev/null +++ b/packages/client/src/helpers/keplr.ts @@ -0,0 +1,35 @@ +import type { ChainInfo, Window as KeplrWindow } from '@keplr-wallet/types' + +import { WalletExtensionNotInstalledError } from '@/errors' + +declare global { + // eslint-disable-next-line @typescript-eslint/no-empty-interface + interface Window extends KeplrWindow {} +} + +export const getKeplrDirectSigner = async (chainInfo: ChainInfo) => { + if (window.keplr === undefined) throw new WalletExtensionNotInstalledError() + + await window.keplr.experimentalSuggestChain(chainInfo) + await window.keplr.enable(chainInfo.chainId) + + return window.keplr.getOfflineSigner(chainInfo.chainId) +} + +export const getKeplrAminoSigner = async (chainInfo: ChainInfo) => { + if (window.keplr === undefined) throw new WalletExtensionNotInstalledError() + + await window.keplr.experimentalSuggestChain(chainInfo) + await window.keplr.enable(chainInfo.chainId) + + return window.keplr.getOfflineSignerOnlyAmino(chainInfo.chainId) +} + +export const getKeplrSigner = async (chainInfo: ChainInfo) => { + if (window.keplr === undefined) throw new WalletExtensionNotInstalledError() + + await window.keplr.experimentalSuggestChain(chainInfo) + await window.keplr.enable(chainInfo.chainId) + + return window.keplr.getOfflineSignerAuto(chainInfo.chainId) +} diff --git a/packages/client/src/index.ts b/packages/client/src/index.ts index d93c7d7cd..b1c0e5c08 100644 --- a/packages/client/src/index.ts +++ b/packages/client/src/index.ts @@ -1,6 +1,7 @@ export * from './client' export * from './enums' export * from './errors' +export * from './helpers' export * from './querier' export * from './types' export * from './utils' diff --git a/packages/client/src/types/client.ts b/packages/client/src/types/client.ts index 3c01502a1..ec3bbe72d 100644 --- a/packages/client/src/types/client.ts +++ b/packages/client/src/types/client.ts @@ -99,4 +99,5 @@ export type RarimoClient = { tx: RarimoBroadcaster connect: (injectedWallet?: Wallet) => Promise disconnect: () => void + getChainInfo: () => Promise } diff --git a/packages/client/src/wallet.ts b/packages/client/src/wallet.ts index b62861106..753352d68 100644 --- a/packages/client/src/wallet.ts +++ b/packages/client/src/wallet.ts @@ -1,24 +1,15 @@ import type { OfflineSigner } from '@cosmjs/proto-signing' import { computed, ref, toRaw } from '@distributedlab/reactivity' -import type { - AccountData, - ChainInfo, - Window as KeplrWindow, -} from '@keplr-wallet/types' +import type { AccountData, ChainInfo } from '@keplr-wallet/types' -import { WalletExtensionNotInstalledError, WalletIsEmptyError } from '@/errors' -import { stub } from '@/helpers' +import { WalletIsEmptyError } from '@/errors' +import { getKeplrDirectSigner, stub } from '@/helpers' import type { Wallet } from '@/types' -declare global { - // eslint-disable-next-line @typescript-eslint/no-empty-interface - interface Window extends KeplrWindow {} -} - const initStub = stub('Wallet not initialized!') export const makeWallet = (injectedSigner?: OfflineSigner): Wallet => { - const signer = ref(injectedSigner || initStub) + const signer = ref(initStub) const accounts = ref(initStub) const chainId = ref('') @@ -34,19 +25,9 @@ export const makeWallet = (injectedSigner?: OfflineSigner): Wallet => { }) const connect = async (chainInfo: ChainInfo) => { - if (injectedSigner) { - chainId.value = chainInfo.chainId - accounts.value = await signer.value.getAccounts() + signer.value = injectedSigner || (await getKeplrDirectSigner(chainInfo)) - return - } - - if (window.keplr === undefined) throw new WalletExtensionNotInstalledError() chainId.value = chainInfo.chainId - - await window.keplr.experimentalSuggestChain(chainInfo) - await window.keplr.enable(chainId.value) - signer.value = window.keplr.getOfflineSigner(chainId.value) accounts.value = await signer.value.getAccounts() }