From 5f4eec8c1032d9de3503cb4e28d982328f7dbd0a Mon Sep 17 00:00:00 2001 From: Bojan Angjelkoski Date: Fri, 29 Nov 2024 14:04:05 +0100 Subject: [PATCH] refactor: minor --- packages/utils/src/formatters.ts | 3 +++ .../wallets/wallet-base/src/utils/index.ts | 3 --- .../wallets/wallet-base/src/utils/wallet.ts | 21 ++++++++++--------- .../src/strategy/BaseWalletStrategy.ts | 6 +++--- .../wallet-cosmos/src/strategy/strategy.ts | 2 +- .../wallets/wallet-cosmos/src/utils/index.ts | 18 +++++++++------- packages/wallets/wallet-cosmos/src/wallet.ts | 3 ++- packages/wallets/wallet-evm/src/data/index.ts | 9 -------- packages/wallets/wallet-evm/src/index.ts | 1 + .../wallet-evm/src/strategy/strategy.ts | 7 +++---- .../wallets/wallet-evm/src/utils/index.ts | 8 +++---- .../wallet-strategy/src/strategy/index.ts | 4 ++-- 12 files changed, 41 insertions(+), 44 deletions(-) delete mode 100644 packages/wallets/wallet-evm/src/data/index.ts diff --git a/packages/utils/src/formatters.ts b/packages/utils/src/formatters.ts index 0514ed5fe..ee9f43948 100644 --- a/packages/utils/src/formatters.ts +++ b/packages/utils/src/formatters.ts @@ -37,3 +37,6 @@ export const snakeToPascal = (str: string): string => { ) .join('/') } + +export const capitalize = (str: string): string => + str[0].toUpperCase() + str.slice(1) diff --git a/packages/wallets/wallet-base/src/utils/index.ts b/packages/wallets/wallet-base/src/utils/index.ts index 2782fbce9..ead0c7095 100644 --- a/packages/wallets/wallet-base/src/utils/index.ts +++ b/packages/wallets/wallet-base/src/utils/index.ts @@ -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) diff --git a/packages/wallets/wallet-base/src/utils/wallet.ts b/packages/wallets/wallet-base/src/utils/wallet.ts index fceba4d4c..a10eae65f 100644 --- a/packages/wallets/wallet-base/src/utils/wallet.ts +++ b/packages/wallets/wallet-base/src/utils/wallet.ts @@ -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, @@ -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 => [ @@ -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, -] diff --git a/packages/wallets/wallet-core/src/strategy/BaseWalletStrategy.ts b/packages/wallets/wallet-core/src/strategy/BaseWalletStrategy.ts index 2c646d27c..a09c11e4b 100644 --- a/packages/wallets/wallet-core/src/strategy/BaseWalletStrategy.ts +++ b/packages/wallets/wallet-core/src/strategy/BaseWalletStrategy.ts @@ -12,7 +12,7 @@ import { import { GeneralException, WalletException } from '@injectivelabs/exceptions' import { Wallet, - isEthWallet, + isEvmWallet, isCosmosWallet, WalletDeviceType, ConcreteStrategiesArg, @@ -151,7 +151,7 @@ export default class BaseWalletStrategy implements WalletStrategyInterface { signDoc: StdSignDoc address: string }): Promise { - if (isEthWallet(this.wallet)) { + if (isEvmWallet(this.wallet)) { throw new WalletException( new Error(`You can't sign Cosmos Transaction using ${this.wallet}`), ) @@ -166,7 +166,7 @@ export default class BaseWalletStrategy implements WalletStrategyInterface { chainId: string address: string }): Promise { - if (isEthWallet(this.wallet)) { + if (isEvmWallet(this.wallet)) { throw new WalletException( new Error(`You can't sign Cosmos Transaction using ${this.wallet}`), ) diff --git a/packages/wallets/wallet-cosmos/src/strategy/strategy.ts b/packages/wallets/wallet-cosmos/src/strategy/strategy.ts index 7b7bf354e..84b968597 100644 --- a/packages/wallets/wallet-cosmos/src/strategy/strategy.ts +++ b/packages/wallets/wallet-cosmos/src/strategy/strategy.ts @@ -22,7 +22,6 @@ import { } from '@injectivelabs/exceptions' import { Wallet, - capitalize, StdSignDoc, WalletAction, WalletDeviceType, @@ -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] diff --git a/packages/wallets/wallet-cosmos/src/utils/index.ts b/packages/wallets/wallet-cosmos/src/utils/index.ts index 1cdb7ed87..9b06c3718 100644 --- a/packages/wallets/wallet-cosmos/src/utils/index.ts +++ b/packages/wallets/wallet-cosmos/src/utils/index.ts @@ -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) { @@ -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 } @@ -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'), ) diff --git a/packages/wallets/wallet-cosmos/src/wallet.ts b/packages/wallets/wallet-cosmos/src/wallet.ts index ceabf977f..58e26dff3 100644 --- a/packages/wallets/wallet-cosmos/src/wallet.ts +++ b/packages/wallets/wallet-cosmos/src/wallet.ts @@ -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' diff --git a/packages/wallets/wallet-evm/src/data/index.ts b/packages/wallets/wallet-evm/src/data/index.ts deleted file mode 100644 index 668766cbe..000000000 --- a/packages/wallets/wallet-evm/src/data/index.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { Wallet } from '@injectivelabs/wallet-base' - -export const evmWallets = [ - Wallet.BitGet, - Wallet.Phantom, - Wallet.Metamask, - Wallet.OkxWallet, - Wallet.TrustWallet, -] diff --git a/packages/wallets/wallet-evm/src/index.ts b/packages/wallets/wallet-evm/src/index.ts index 7be5a8d63..d5907c594 100644 --- a/packages/wallets/wallet-evm/src/index.ts +++ b/packages/wallets/wallet-evm/src/index.ts @@ -1,3 +1,4 @@ export { EvmWallet as EvmWalletStrategy } from './strategy/strategy.js' + export * from './strategy/utils/index.js' export * from './utils/index.js' diff --git a/packages/wallets/wallet-evm/src/strategy/strategy.ts b/packages/wallets/wallet-evm/src/strategy/strategy.ts index 484951a62..bfb786f1a 100644 --- a/packages/wallets/wallet-evm/src/strategy/strategy.ts +++ b/packages/wallets/wallet-evm/src/strategy/strategy.ts @@ -1,4 +1,5 @@ /* eslint-disable class-methods-use-this */ +import { isEvmBrowserWallet } from '@injectivelabs/wallet-base' import { TxRaw, toUtf8, @@ -21,7 +22,6 @@ import { } from '@injectivelabs/exceptions' import { Wallet, - capitalize, StdSignDoc, WalletAction, WalletDeviceType, @@ -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, @@ -42,7 +42,6 @@ import { getOkxWalletProvider, getTrustWalletProvider, } from './utils/index.js' -import { evmWallets } from '../data/index.js' export class EvmWallet extends BaseConcreteStrategy @@ -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.`, diff --git a/packages/wallets/wallet-evm/src/utils/index.ts b/packages/wallets/wallet-evm/src/utils/index.ts index 97b88bd1b..b8bd831a8 100644 --- a/packages/wallets/wallet-evm/src/utils/index.ts +++ b/packages/wallets/wallet-evm/src/utils/index.ts @@ -1,8 +1,9 @@ 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' @@ -10,12 +11,11 @@ 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 => { - if (!evmWallets.includes(wallet)) { + if (!isEvmBrowserWallet(wallet)) { throw new WalletException( new Error(`Evm Wallet for ${capitalize(wallet)} is not supported.`), ) @@ -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.`), ) diff --git a/packages/wallets/wallet-strategy/src/strategy/index.ts b/packages/wallets/wallet-strategy/src/strategy/index.ts index 013e6bbbb..6b44fa8ab 100644 --- a/packages/wallets/wallet-strategy/src/strategy/index.ts +++ b/packages/wallets/wallet-strategy/src/strategy/index.ts @@ -1,6 +1,6 @@ import { Wallet, - isEthWallet, + isEvmWallet, MagicMetadata, ConcreteStrategiesArg, ConcreteWalletStrategy, @@ -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 }