Skip to content

Commit

Permalink
refactor: minor
Browse files Browse the repository at this point in the history
  • Loading branch information
bangjelkoski committed Nov 29, 2024
1 parent 5ac0bc3 commit 5f4eec8
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 44 deletions.
3 changes: 3 additions & 0 deletions packages/utils/src/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ export const snakeToPascal = (str: string): string => {
)
.join('/')
}

export const capitalize = (str: string): string =>
str[0].toUpperCase() + str.slice(1)
3 changes: 0 additions & 3 deletions packages/wallets/wallet-base/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,3 @@ export * from './wallet.js'
export * from './constants.js'
export * from './alchemy.js'
export * from './cosmos.js'

export const capitalize = (str: string): string =>
str[0].toUpperCase() + str.slice(1)
21 changes: 11 additions & 10 deletions packages/wallets/wallet-base/src/utils/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Wallet } from './../types/enums.js'

export const isEthWallet = (wallet: Wallet): boolean =>
export const isEvmWallet = (wallet: Wallet): boolean =>
[
Wallet.Magic,
Wallet.Torus,
Expand All @@ -17,7 +17,16 @@ export const isEthWallet = (wallet: Wallet): boolean =>
Wallet.CosmostationEth,
].includes(wallet)

export const isCosmosWallet = (wallet: Wallet): boolean => !isEthWallet(wallet)
export const isCosmosWallet = (wallet: Wallet): boolean => !isEvmWallet(wallet)

export const isEvmBrowserWallet = (wallet: Wallet) => [
Wallet.BitGet,
Wallet.Phantom,
Wallet.Metamask,
Wallet.OkxWallet,
Wallet.TrustWallet,
].includes(wallet)


export const isCosmosBrowserWallet = (wallet: Wallet): boolean =>
[
Expand All @@ -38,11 +47,3 @@ export const isEip712V2OnlyWallet = (wallet: Wallet): boolean =>

export const isCosmosAminoOnlyWallet = (wallet: Wallet): boolean =>
[Wallet.LedgerCosmos].includes(wallet)

export const COSMOS_WALLETS = [
Wallet.Keplr,
Wallet.Leap,
Wallet.Ninji,
Wallet.Cosmostation,
Wallet.OWallet,
]
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { GeneralException, WalletException } from '@injectivelabs/exceptions'
import {
Wallet,
isEthWallet,
isEvmWallet,
isCosmosWallet,
WalletDeviceType,
ConcreteStrategiesArg,
Expand Down Expand Up @@ -151,7 +151,7 @@ export default class BaseWalletStrategy implements WalletStrategyInterface {
signDoc: StdSignDoc
address: string
}): Promise<AminoSignResponse> {
if (isEthWallet(this.wallet)) {
if (isEvmWallet(this.wallet)) {
throw new WalletException(
new Error(`You can't sign Cosmos Transaction using ${this.wallet}`),
)
Expand All @@ -166,7 +166,7 @@ export default class BaseWalletStrategy implements WalletStrategyInterface {
chainId: string
address: string
}): Promise<DirectSignResponse> {
if (isEthWallet(this.wallet)) {
if (isEvmWallet(this.wallet)) {
throw new WalletException(
new Error(`You can't sign Cosmos Transaction using ${this.wallet}`),
)
Expand Down
2 changes: 1 addition & 1 deletion packages/wallets/wallet-cosmos/src/strategy/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
} from '@injectivelabs/exceptions'
import {
Wallet,
capitalize,
StdSignDoc,
WalletAction,
WalletDeviceType,
Expand All @@ -32,6 +31,7 @@ import {
SendTransactionOptions,
createCosmosSignDocFromSignDoc,
} from '@injectivelabs/wallet-base'
import { capitalize } from '@injectivelabs/utils'
import { CosmosWallet } from './../wallet.js'

const cosmosWallets = [Wallet.Leap, Wallet.Ninji, Wallet.Keplr, Wallet.OWallet]
Expand Down
18 changes: 11 additions & 7 deletions packages/wallets/wallet-cosmos/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import type { Keplr } from '@keplr-wallet/types'
import type { Keplr as CosmosBrowserWallet } from '@keplr-wallet/types'
import { ChainId } from '@injectivelabs/ts-types'
import { PublicKey } from '@injectivelabs/sdk-ts'
import { Wallet, capitalize } from '@injectivelabs/wallet-base'
import { Wallet } from '@injectivelabs/wallet-base'
import { capitalize } from '@injectivelabs/utils'
import { CosmosWalletException } from '@injectivelabs/exceptions'
import { CosmosWallet } from './../wallet.js'
import { cosmosWallets } from './../data/index.js'

export const isCosmosWalletInstalled = (wallet: Wallet) => {
const $window = (typeof window !== 'undefined' ? window : {}) as Window & {
leap?: Keplr
keplr?: Keplr
ninji?: Keplr
leap?: CosmosBrowserWallet
keplr?: CosmosBrowserWallet
ninji?: CosmosBrowserWallet
oWallet?: CosmosBrowserWallet
}

switch (wallet) {
Expand All @@ -20,6 +22,8 @@ export const isCosmosWalletInstalled = (wallet: Wallet) => {
return $window.ninji !== undefined
case Wallet.Leap:
return $window.leap !== undefined
case Wallet.OWallet:
return $window.oWallet !== undefined
default:
return false
}
Expand All @@ -40,8 +44,8 @@ export const confirmCosmosAddress = async ({
)
}

const keplr = new CosmosWallet({ chainId, wallet })
const key = await keplr.getKey()
const cosmosWallet = new CosmosWallet({ chainId, wallet })
const key = await cosmosWallet.getKey()
const publicKey = PublicKey.fromBase64(
Buffer.from(key.pubKey).toString('base64'),
)
Expand Down
3 changes: 2 additions & 1 deletion packages/wallets/wallet-cosmos/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import {
CosmosWalletException,
WalletErrorActionModule,
} from '@injectivelabs/exceptions'
import { capitalize } from '@injectivelabs/utils'
import { BroadcastMode } from '@cosmjs/launchpad'
import { CosmosTxV1Beta1Tx } from '@injectivelabs/sdk-ts'
import { Wallet, capitalize } from '@injectivelabs/wallet-base'
import { Wallet } from '@injectivelabs/wallet-base'
import { SigningStargateClient, StdFee } from '@cosmjs/stargate'
import type { EncodeObject, OfflineDirectSigner } from '@cosmjs/proto-signing'

Expand Down
9 changes: 0 additions & 9 deletions packages/wallets/wallet-evm/src/data/index.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/wallets/wallet-evm/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { EvmWallet as EvmWalletStrategy } from './strategy/strategy.js'

export * from './strategy/utils/index.js'
export * from './utils/index.js'
7 changes: 3 additions & 4 deletions packages/wallets/wallet-evm/src/strategy/strategy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable class-methods-use-this */
import { isEvmBrowserWallet } from '@injectivelabs/wallet-base'
import {
TxRaw,
toUtf8,
Expand All @@ -21,7 +22,6 @@ import {
} from '@injectivelabs/exceptions'
import {
Wallet,
capitalize,
StdSignDoc,
WalletAction,
WalletDeviceType,
Expand All @@ -33,7 +33,7 @@ import {
ConcreteWalletStrategyArgs,
ConcreteEthereumWalletStrategyArgs,
} from '@injectivelabs/wallet-base'
import { sleep } from '@injectivelabs/utils'
import { sleep, capitalize } from '@injectivelabs/utils'
import { AccountAddress, EthereumChainId } from '@injectivelabs/ts-types'
import {
getBitGetProvider,
Expand All @@ -42,7 +42,6 @@ import {
getOkxWalletProvider,
getTrustWalletProvider,
} from './utils/index.js'
import { evmWallets } from '../data/index.js'

export class EvmWallet
extends BaseConcreteStrategy
Expand All @@ -57,7 +56,7 @@ export class EvmWallet
) {
super(args)

if (!evmWallets.includes(args.wallet)) {
if (!isEvmBrowserWallet(args.wallet)) {
throw new WalletException(
new Error(
`Evm Wallet for ${capitalize(args.wallet)} is not supported.`,
Expand Down
8 changes: 4 additions & 4 deletions packages/wallets/wallet-evm/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import {
Wallet,
capitalize,
isEvmBrowserWallet,
BrowserEip1993Provider,
} from '@injectivelabs/wallet-base'
import { capitalize } from '@injectivelabs/utils'
import { EthereumChainId } from '@injectivelabs/ts-types'
import { WalletException } from '@injectivelabs/exceptions'
import { getOkxWalletProvider } from '../strategy/utils/Okx.js'
import { getBitGetProvider } from '../strategy/utils/bitget.js'
import { getPhantomProvider } from '../strategy/utils/phantom.js'
import { getMetamaskProvider } from '../strategy/utils/metamask.js'
import { getTrustWalletProvider } from '../strategy/utils/trustWallet.js'
import { evmWallets } from '../data/index.js'

export const getEvmProvider = async (
wallet: Wallet,
): Promise<BrowserEip1993Provider> => {
if (!evmWallets.includes(wallet)) {
if (!isEvmBrowserWallet(wallet)) {
throw new WalletException(
new Error(`Evm Wallet for ${capitalize(wallet)} is not supported.`),
)
Expand Down Expand Up @@ -62,7 +62,7 @@ export const updateEvmNetwork = async (
wallet: Wallet,
chainId: EthereumChainId,
) => {
if (!evmWallets.includes(wallet)) {
if (!isEvmBrowserWallet(wallet)) {
throw new WalletException(
new Error(`Evm Wallet for ${capitalize(wallet)} is not supported.`),
)
Expand Down
4 changes: 2 additions & 2 deletions packages/wallets/wallet-strategy/src/strategy/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
Wallet,
isEthWallet,
isEvmWallet,
MagicMetadata,
ConcreteStrategiesArg,
ConcreteWalletStrategy,
Expand Down Expand Up @@ -49,7 +49,7 @@ const createStrategy = ({
* If we only want to use Cosmos Native Wallets
* We are not creating strategies for Ethereum Native Wallets
*/
if (isEthWallet(wallet) && ethereumWalletsDisabled(args)) {
if (isEvmWallet(wallet) && ethereumWalletsDisabled(args)) {
return undefined
}

Expand Down

0 comments on commit 5f4eec8

Please sign in to comment.