Skip to content

Commit

Permalink
restructurize wallet, add keplr helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
lukachi committed Mar 21, 2024
1 parent 2048f26 commit e17f5db
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 27 deletions.
10 changes: 7 additions & 3 deletions packages/client/src/client.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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)
Expand All @@ -39,6 +42,7 @@ export const makeRarimoClient = (config: Config): RarimoClient => {
wallet,
disconnect,
connect,
getChainInfo,
query,
tx,
})
Expand Down
1 change: 1 addition & 0 deletions packages/client/src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './broadcast-maker'
export * from './chain-info'
export * from './keplr'
export * from './request'
export * from './stub'
35 changes: 35 additions & 0 deletions packages/client/src/helpers/keplr.ts
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions packages/client/src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
1 change: 1 addition & 0 deletions packages/client/src/types/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,5 @@ export type RarimoClient = {
tx: RarimoBroadcaster
connect: (injectedWallet?: Wallet) => Promise<void>
disconnect: () => void
getChainInfo: () => Promise<ChainInfo>
}
29 changes: 5 additions & 24 deletions packages/client/src/wallet.ts
Original file line number Diff line number Diff line change
@@ -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<OfflineSigner>(injectedSigner || initStub)
const signer = ref<OfflineSigner>(initStub)
const accounts = ref<readonly AccountData[]>(initStub)
const chainId = ref('')

Expand All @@ -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()
}

Expand Down

0 comments on commit e17f5db

Please sign in to comment.