diff --git a/packages/wallet-ts/src/strategies/wallet-strategy/strategies/Ledger/Base.ts b/packages/wallet-ts/src/strategies/wallet-strategy/strategies/Ledger/Base.ts index 163e10da0..31dd69837 100644 --- a/packages/wallet-ts/src/strategies/wallet-strategy/strategies/Ledger/Base.ts +++ b/packages/wallet-ts/src/strategies/wallet-strategy/strategies/Ledger/Base.ts @@ -1,40 +1,40 @@ /* eslint-disable class-methods-use-this */ -import { AccountAddress, EthereumChainId } from '@injectivelabs/ts-types' -import { bufferToHex, addHexPrefix } from 'ethereumjs-util' -import { Common, Chain, Hardfork } from '@ethereumjs/common' -import { FeeMarketEIP1559Transaction } from '@ethereumjs/tx' import { ErrorType, - GeneralException, LedgerException, - TransactionException, - UnspecifiedErrorCode, WalletException, + GeneralException, + UnspecifiedErrorCode, + TransactionException, } from '@injectivelabs/exceptions' import { DirectSignResponse } from '@cosmjs/proto-signing' +import { bufferToHex, addHexPrefix } from 'ethereumjs-util' +import { Common, Chain, Hardfork } from '@ethereumjs/common' +import { FeeMarketEIP1559Transaction } from '@ethereumjs/tx' +import { Alchemy, Network as AlchemyNetwork } from 'alchemy-sdk' +import { AccountAddress, EthereumChainId } from '@injectivelabs/ts-types' import { TxGrpcApi, TxRaw, TxResponse, toUtf8 } from '@injectivelabs/sdk-ts' -import { TIP_IN_GWEI } from '../../../../utils/constants.js' +import { + LedgerWalletInfo, + SendTransactionOptions, + LedgerDerivationPathType, +} from '../../types.js' import { ConcreteWalletStrategy, EthereumWalletStrategyArgs, WalletStrategyEthereumOptions, } from '../../../types/index.js' -import { - LedgerDerivationPathType, - LedgerWalletInfo, - SendTransactionOptions, -} from '../../types.js' -import BaseConcreteStrategy from '../Base.js' import { DEFAULT_BASE_DERIVATION_PATH, DEFAULT_ADDRESS_SEARCH_LIMIT, DEFAULT_NUM_ADDRESSES_TO_FETCH, } from '../../constants.js' import LedgerHW from './hw/index.js' +import BaseConcreteStrategy from '../Base.js' import { domainHash, messageHash } from './utils.js' -import { WalletAction, WalletDeviceType } from '../../../../types/enums.js' +import { TIP_IN_GWEI } from '../../../../utils/constants.js' import { getKeyFromRpcUrl } from '../../../../utils/alchemy.js' -import { Alchemy, Network as AlchemyNetwork } from 'alchemy-sdk' +import { WalletAction, WalletDeviceType } from '../../../../types/enums.js' const getNetworkFromChainId = (chainId: EthereumChainId): Chain => { if (chainId === EthereumChainId.Goerli) { diff --git a/packages/wallets/wallet-base/src/utils/index.ts b/packages/wallets/wallet-base/src/utils/index.ts index ead0c7095..2782fbce9 100644 --- a/packages/wallets/wallet-base/src/utils/index.ts +++ b/packages/wallets/wallet-base/src/utils/index.ts @@ -4,3 +4,6 @@ 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 3daff13cb..fceba4d4c 100644 --- a/packages/wallets/wallet-base/src/utils/wallet.ts +++ b/packages/wallets/wallet-base/src/utils/wallet.ts @@ -19,6 +19,15 @@ export const isEthWallet = (wallet: Wallet): boolean => export const isCosmosWallet = (wallet: Wallet): boolean => !isEthWallet(wallet) +export const isCosmosBrowserWallet = (wallet: Wallet): boolean => + [ + Wallet.Leap, + Wallet.Ninji, + Wallet.Keplr, + Wallet.OWallet, + Wallet.Cosmostation, + ].includes(wallet) + export const isEip712V2OnlyWallet = (wallet: Wallet): boolean => [ Wallet.Magic, diff --git a/packages/wallets/wallet-core/package.json b/packages/wallets/wallet-core/package.json index ec49f106b..74b9da43b 100644 --- a/packages/wallets/wallet-core/package.json +++ b/packages/wallets/wallet-core/package.json @@ -60,7 +60,7 @@ "@injectivelabs/sdk-ts": "^1.14.33-beta.2", "@injectivelabs/ts-types": "^1.14.32", "@injectivelabs/utils": "^1.14.32", - "@injectivelabs/wallet-base": "^0.0.6", + "@injectivelabs/wallet-base": "^1.14.33-beta.2", "eip1193-provider": "^1.0.1" }, "devDependencies": { diff --git a/packages/wallets/wallet-core/src/strategy/BaseWalletStrategy.ts b/packages/wallets/wallet-core/src/strategy/BaseWalletStrategy.ts index e984ebe53..2c646d27c 100644 --- a/packages/wallets/wallet-core/src/strategy/BaseWalletStrategy.ts +++ b/packages/wallets/wallet-core/src/strategy/BaseWalletStrategy.ts @@ -207,7 +207,7 @@ export default class BaseWalletStrategy implements WalletStrategyInterface { } public getCosmosWallet(chainId: ChainId): CosmosWalletAbstraction { - if ([Wallet.Keplr, Wallet.Leap].includes(this.getWallet())) { + if (![Wallet.Keplr, Wallet.Leap].includes(this.getWallet())) { throw new WalletException( new Error(`You can't use this method outside of Keplr/Leap wallet`), ) diff --git a/packages/wallets/wallet-cosmos/src/cosmos.d.ts b/packages/wallets/wallet-cosmos/src/cosmos.d.ts index 7b1c30ed2..c50b57cc3 100644 --- a/packages/wallets/wallet-cosmos/src/cosmos.d.ts +++ b/packages/wallets/wallet-cosmos/src/cosmos.d.ts @@ -6,5 +6,6 @@ declare global { leap: KeplrWindow['keplr'] keplr: KeplrWindow['keplr'] ninji: KeplrWindow['ninji'] + owallet?: KeplrWindow['owallet'] } } diff --git a/packages/wallets/wallet-cosmos/src/data/index.ts b/packages/wallets/wallet-cosmos/src/data/index.ts new file mode 100644 index 000000000..dd6b3e260 --- /dev/null +++ b/packages/wallets/wallet-cosmos/src/data/index.ts @@ -0,0 +1,8 @@ +import { Wallet } from '@injectivelabs/wallet-base' + +export const cosmosWallets = [ + Wallet.Leap, + Wallet.Ninji, + Wallet.Keplr, + Wallet.OWallet, +] diff --git a/packages/wallets/wallet-cosmos/src/index.ts b/packages/wallets/wallet-cosmos/src/index.ts index dbcbb3896..2aba985bd 100644 --- a/packages/wallets/wallet-cosmos/src/index.ts +++ b/packages/wallets/wallet-cosmos/src/index.ts @@ -1,2 +1,3 @@ export { CosmosWallet } from './wallet.js' export { CosmosWalletStrategy } from './strategy/strategy.js' +export * from './utils/index.js' diff --git a/packages/wallets/wallet-cosmos/src/strategy/strategy.ts b/packages/wallets/wallet-cosmos/src/strategy/strategy.ts index 88fb13c22..7b7bf354e 100644 --- a/packages/wallets/wallet-cosmos/src/strategy/strategy.ts +++ b/packages/wallets/wallet-cosmos/src/strategy/strategy.ts @@ -22,6 +22,7 @@ import { } from '@injectivelabs/exceptions' import { Wallet, + capitalize, StdSignDoc, WalletAction, WalletDeviceType, @@ -52,13 +53,18 @@ export class CosmosWalletStrategy if (!cosmosWallets.includes(args.wallet)) { throw new CosmosWalletException( - new Error(`Cosmos Wallet for ${args.wallet} is not supported.`), + new Error( + `Cosmos Wallet for ${capitalize(args.wallet)} is not supported.`, + ), ) } this.wallet = args.wallet this.chainId = args.chainId || CosmosChainId.Injective - this.cosmosWallet = new CosmosWallet(args.chainId, args.wallet) + this.cosmosWallet = new CosmosWallet({ + wallet: args.wallet, + chainId: args.chainId, + }) } async getWalletDeviceType(): Promise { @@ -137,7 +143,9 @@ export class CosmosWalletStrategy throw new CosmosWalletException( new Error( - `sendEthereumTransaction is not supported. ${wallet} only supports sending cosmos transactions`, + `sendEthereumTransaction is not supported. ${capitalize( + wallet, + )} only supports sending cosmos transactions`, ), { code: UnspecifiedErrorCode, @@ -252,7 +260,7 @@ export class CosmosWalletStrategy const { wallet } = this throw new CosmosWalletException( - new Error(`getEthereumChainId is not supported on ${wallet}`), + new Error(`getEthereumChainId is not supported on ${capitalize(wallet)}`), { code: UnspecifiedErrorCode, context: WalletAction.GetChainId, @@ -264,7 +272,11 @@ export class CosmosWalletStrategy const { wallet } = this throw new CosmosWalletException( - new Error(`getEthereumTransactionReceipt is not supported on ${wallet}`), + new Error( + `getEthereumTransactionReceipt is not supported on ${capitalize( + wallet, + )}`, + ), { code: UnspecifiedErrorCode, context: WalletAction.GetEthereumTransactionReceipt, @@ -310,7 +322,7 @@ export class CosmosWalletStrategy public getCosmosWallet(chainId: ChainId): CosmosWallet { const { wallet, cosmosWallet } = this - return !cosmosWallet ? new CosmosWallet(chainId, wallet) : cosmosWallet + return !cosmosWallet ? new CosmosWallet({ chainId, wallet }) : cosmosWallet } private getCurrentCosmosWallet(): CosmosWallet { @@ -318,7 +330,7 @@ export class CosmosWalletStrategy if (!cosmosWallet) { throw new CosmosWalletException( - new Error(`Please install the ${wallet} wallet extension`), + new Error(`Please install the ${capitalize(wallet)} wallet extension`), { code: UnspecifiedErrorCode, type: ErrorType.WalletNotInstalledError, diff --git a/packages/wallets/wallet-cosmos/src/utils/index.ts b/packages/wallets/wallet-cosmos/src/utils/index.ts new file mode 100644 index 000000000..1cdb7ed87 --- /dev/null +++ b/packages/wallets/wallet-cosmos/src/utils/index.ts @@ -0,0 +1,60 @@ +import type { Keplr } from '@keplr-wallet/types' +import { ChainId } from '@injectivelabs/ts-types' +import { PublicKey } from '@injectivelabs/sdk-ts' +import { Wallet, capitalize } from '@injectivelabs/wallet-base' +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 + } + + switch (wallet) { + case Wallet.Keplr: + return $window.keplr !== undefined + case Wallet.Ninji: + return $window.ninji !== undefined + case Wallet.Leap: + return $window.leap !== undefined + default: + return false + } +} + +export const confirmCosmosAddress = async ({ + wallet, + chainId, + injectiveAddress, +}: { + wallet: Wallet + chainId: ChainId + injectiveAddress: string +}) => { + if (!cosmosWallets.includes(wallet)) { + throw new CosmosWalletException( + new Error(`Cosmos Wallet for ${capitalize(wallet)} is not supported.`), + ) + } + + const keplr = new CosmosWallet({ chainId, wallet }) + const key = await keplr.getKey() + const publicKey = PublicKey.fromBase64( + Buffer.from(key.pubKey).toString('base64'), + ) + + const { address: derivedAddress } = publicKey.toAddress() + + if (derivedAddress !== injectiveAddress) { + throw new CosmosWalletException( + new Error( + `Connected ${capitalize( + wallet, + )} address is wrong. Please update Injective on ${capitalize(wallet)}.`, + ), + ) + } +} diff --git a/packages/wallets/wallet-cosmos/src/wallet.ts b/packages/wallets/wallet-cosmos/src/wallet.ts index 1929fec4e..ceabf977f 100644 --- a/packages/wallets/wallet-cosmos/src/wallet.ts +++ b/packages/wallets/wallet-cosmos/src/wallet.ts @@ -19,8 +19,8 @@ import { WalletErrorActionModule, } from '@injectivelabs/exceptions' import { BroadcastMode } from '@cosmjs/launchpad' -import { Wallet } from '@injectivelabs/wallet-base' import { CosmosTxV1Beta1Tx } from '@injectivelabs/sdk-ts' +import { Wallet, capitalize } from '@injectivelabs/wallet-base' import { SigningStargateClient, StdFee } from '@cosmjs/stargate' import type { EncodeObject, OfflineDirectSigner } from '@cosmjs/proto-signing' @@ -35,10 +35,13 @@ export class CosmosWallet { public wallet: Wallet private chainId: CosmosChainId | TestnetCosmosChainId | ChainId - constructor( - chainId: CosmosChainId | TestnetCosmosChainId | ChainId, - wallet: Wallet, - ) { + constructor({ + wallet, + chainId, + }: { + wallet: Wallet + chainId: CosmosChainId | TestnetCosmosChainId | ChainId + }) { this.wallet = wallet this.chainId = chainId } @@ -46,7 +49,7 @@ export class CosmosWallet { public async isChainIdSupported(chainId: CosmosChainId): Promise { const { wallet } = this - return new CosmosWallet(chainId, wallet).checkChainIdSupport() + return new CosmosWallet({ chainId, wallet }).checkChainIdSupport() } public async getCosmosWallet() { @@ -75,7 +78,7 @@ export class CosmosWallet { throw new CosmosWalletException( new Error( - `${wallet} may not support ${ + `${capitalize(wallet)} may not support ${ chainName[0] || chainId } network. Please check if the chain can be added.`, ), @@ -136,7 +139,9 @@ export class CosmosWallet { if ([Wallet.Keplr, Wallet.OWallet].includes(wallet)) { throw new CosmosWalletException( - new Error(`getOfflineAminoSigner is not support on ${wallet}`), + new Error( + `getOfflineAminoSigner is not support on ${capitalize(wallet)}`, + ), ) } @@ -243,7 +248,9 @@ export class CosmosWallet { if (![Wallet.Keplr, Wallet.OWallet].includes(wallet)) { throw new CosmosWalletException( new Error( - `signAndBroadcastAminoUsingCosmjs is not support on ${wallet}`, + `signAndBroadcastAminoUsingCosmjs is not support on ${capitalize( + wallet, + )}`, ), ) } @@ -280,7 +287,7 @@ export class CosmosWallet { if (wallet !== Wallet.Keplr) { throw new CosmosWalletException( - new Error(`signArbitrary is not supported on ${wallet}`), + new Error(`signArbitrary is not supported on ${capitalize(wallet)}`), ) } @@ -332,7 +339,7 @@ export class CosmosWallet { } catch (e) { throw new CosmosWalletException( new Error( - `${wallet} doesn't support ${ + `${capitalize(wallet)} doesn't support ${ chainName[0] || chainId } network. Please use another Cosmos wallet`, ), @@ -345,7 +352,7 @@ export class CosmosWallet { if (!$window) { throw new CosmosWalletException( - new Error(`Please install ${wallet} extension`), + new Error(`Please install ${capitalize(wallet)} extension`), { code: UnspecifiedErrorCode, type: ErrorType.WalletNotInstalledError, @@ -374,7 +381,7 @@ export class CosmosWallet { if (!cosmos) { throw new CosmosWalletException( - new Error(`Please install ${wallet} extension`), + new Error(`Please install ${capitalize(wallet)} extension`), { code: UnspecifiedErrorCode, type: ErrorType.WalletNotInstalledError, @@ -390,9 +397,9 @@ export class CosmosWallet { const { wallet } = this const cosmosWallet = await this.getCosmosWallet() - if ([Wallet.Keplr, Wallet.OWallet].includes(wallet)) { + if (![Wallet.Keplr, Wallet.OWallet].includes(wallet)) { throw new CosmosWalletException( - new Error(`disableGasCheck is not support on ${wallet}`), + new Error(`disableGasCheck is not support on ${capitalize(wallet)}`), ) } @@ -412,7 +419,7 @@ export class CosmosWallet { if ([Wallet.Keplr, Wallet.OWallet].includes(wallet)) { throw new CosmosWalletException( - new Error(`enableGasCheck is not support on ${wallet}`), + new Error(`enableGasCheck is not support on ${capitalize(wallet)}`), ) } diff --git a/packages/wallets/wallet-cosmostation/src/index.ts b/packages/wallets/wallet-cosmostation/src/index.ts index 5f47151f4..013525b61 100644 --- a/packages/wallets/wallet-cosmostation/src/index.ts +++ b/packages/wallets/wallet-cosmostation/src/index.ts @@ -1,2 +1,3 @@ export { CosmostationWallet } from './wallet.js' export { Cosmostation as CosmostationWalletStrategy } from './strategy/strategy.js' +export * from './utils/index.js' diff --git a/packages/wallets/wallet-cosmostation/src/utils/index.ts b/packages/wallets/wallet-cosmostation/src/utils/index.ts new file mode 100644 index 000000000..e5c72a5e6 --- /dev/null +++ b/packages/wallets/wallet-cosmostation/src/utils/index.ts @@ -0,0 +1,9 @@ +import { Cosmos } from '@cosmostation/extension-client' + +export const isCosmosStationWalletInstalled = () => { + const $window = (typeof window !== 'undefined' ? window : {}) as Window & { + cosmostation?: Cosmos + } + + return $window.cosmostation !== undefined +} diff --git a/packages/wallets/wallet-evm/package.json b/packages/wallets/wallet-evm/package.json index 78d0a0387..a22d99148 100644 --- a/packages/wallets/wallet-evm/package.json +++ b/packages/wallets/wallet-evm/package.json @@ -60,7 +60,7 @@ "@injectivelabs/sdk-ts": "^1.14.33-beta.2", "@injectivelabs/ts-types": "^1.14.32", "@injectivelabs/utils": "^1.14.32", - "@injectivelabs/wallet-base": "^0.0.6", + "@injectivelabs/wallet-base": "^1.14.33-beta.2", "ethers": "^6.5.1", "long": "^5.2.1", "shx": "^0.3.3" diff --git a/packages/wallets/wallet-evm/src/data/index.ts b/packages/wallets/wallet-evm/src/data/index.ts new file mode 100644 index 000000000..668766cbe --- /dev/null +++ b/packages/wallets/wallet-evm/src/data/index.ts @@ -0,0 +1,9 @@ +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/strategy/strategy.ts b/packages/wallets/wallet-evm/src/strategy/strategy.ts index 3af529d7d..484951a62 100644 --- a/packages/wallets/wallet-evm/src/strategy/strategy.ts +++ b/packages/wallets/wallet-evm/src/strategy/strategy.ts @@ -1,4 +1,12 @@ /* eslint-disable class-methods-use-this */ +import { + TxRaw, + toUtf8, + TxGrpcApi, + TxResponse, + DirectSignResponse, + AminoSignResponse, +} from '@injectivelabs/sdk-ts' import { ErrorType, ErrorContext, @@ -13,6 +21,7 @@ import { } from '@injectivelabs/exceptions' import { Wallet, + capitalize, StdSignDoc, WalletAction, WalletDeviceType, @@ -26,14 +35,6 @@ import { } from '@injectivelabs/wallet-base' import { sleep } from '@injectivelabs/utils' import { AccountAddress, EthereumChainId } from '@injectivelabs/ts-types' -import { - TxRaw, - toUtf8, - TxGrpcApi, - TxResponse, - DirectSignResponse, - AminoSignResponse, -} from '@injectivelabs/sdk-ts' import { getBitGetProvider, getPhantomProvider, @@ -41,14 +42,7 @@ import { getOkxWalletProvider, getTrustWalletProvider, } from './utils/index.js' - -const evmWallets = [ - Wallet.BitGet, - Wallet.Phantom, - Wallet.Metamask, - Wallet.OkxWallet, - Wallet.TrustWallet, -] +import { evmWallets } from '../data/index.js' export class EvmWallet extends BaseConcreteStrategy @@ -65,7 +59,9 @@ export class EvmWallet if (!evmWallets.includes(args.wallet)) { throw new WalletException( - new Error(`Evm Wallet for ${args.wallet} is not supported.`), + 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 01fe03894..97b88bd1b 100644 --- a/packages/wallets/wallet-evm/src/utils/index.ts +++ b/packages/wallets/wallet-evm/src/utils/index.ts @@ -1,31 +1,82 @@ -import { ethers } from 'ethers' -import { WalletException } from '@injectivelabs/exceptions' +import { + Wallet, + capitalize, + BrowserEip1993Provider, +} from '@injectivelabs/wallet-base' 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 { BrowserEip1993Provider } from '@injectivelabs/wallet-base' +import { getTrustWalletProvider } from '../strategy/utils/trustWallet.js' +import { evmWallets } from '../data/index.js' + +export const getEvmProvider = async ( + wallet: Wallet, +): Promise => { + if (!evmWallets.includes(wallet)) { + throw new WalletException( + new Error(`Evm Wallet for ${capitalize(wallet)} is not supported.`), + ) + } -export const getEthersProviderFromMetamask = async () => { try { - const provider = (await getMetamaskProvider()) as BrowserEip1993Provider + let provider + + if (wallet === Wallet.Metamask) { + provider = (await getMetamaskProvider()) as BrowserEip1993Provider + } + + if (wallet === Wallet.BitGet) { + provider = (await getBitGetProvider()) as BrowserEip1993Provider + } + + if (wallet === Wallet.Phantom) { + provider = (await getPhantomProvider()) as BrowserEip1993Provider + } + + if (wallet === Wallet.TrustWallet) { + provider = (await getTrustWalletProvider()) as BrowserEip1993Provider + } + + if (wallet === Wallet.OkxWallet) { + provider = (await getOkxWalletProvider()) as BrowserEip1993Provider + } if (!provider) { - throw new WalletException(new Error('Please install Metamask Extension')) + throw new WalletException( + new Error(`Please install ${capitalize(wallet)} Extension`), + ) } - return new ethers.BrowserProvider(provider, 'any') + return provider } catch (e) { - throw new WalletException(new Error('Please install Metamask Extension')) + throw new WalletException( + new Error(`Please install ${capitalize(wallet)} Extension`), + ) } } -export const updateMetamaskNetwork = async (chainId: EthereumChainId) => { - try { - const provider = (await getMetamaskProvider()) as BrowserEip1993Provider +export const updateEvmNetwork = async ( + wallet: Wallet, + chainId: EthereumChainId, +) => { + if (!evmWallets.includes(wallet)) { + throw new WalletException( + new Error(`Evm Wallet for ${capitalize(wallet)} is not supported.`), + ) + } - if (!provider) { - throw new WalletException(new Error('Please install Metamask Extension')) - } + const provider = (await getEvmProvider(wallet)) as BrowserEip1993Provider + if (!provider) { + throw new WalletException( + new Error(`Please install ${capitalize(wallet)} Extension`), + ) + } + + try { const chainIdToHex = chainId.toString(16) return await Promise.race([ @@ -42,6 +93,8 @@ export const updateMetamaskNetwork = async (chainId: EthereumChainId) => { ), ]) } catch (e) { - throw new WalletException(new Error('Please update your Metamask network')) + throw new WalletException( + new Error(`Please update your ${capitalize(wallet)} network`), + ) } } diff --git a/packages/wallets/wallet-strategy/src/strategy/WalletStrategy.ts b/packages/wallets/wallet-strategy/src/strategy/WalletStrategy.ts deleted file mode 100644 index 013e6bbbb..000000000 --- a/packages/wallets/wallet-strategy/src/strategy/WalletStrategy.ts +++ /dev/null @@ -1,180 +0,0 @@ -import { - Wallet, - isEthWallet, - MagicMetadata, - ConcreteStrategiesArg, - ConcreteWalletStrategy, - WalletStrategyArguments, - ConcreteWalletStrategyOptions, - WalletStrategyEthereumOptions, - ConcreteEthereumWalletStrategyArgs, -} from '@injectivelabs/wallet-base' -import { - LedgerLiveStrategy, - LedgerLegacyStrategy, -} from '@injectivelabs/wallet-ledger' -import { MagicStrategy } from '@injectivelabs/wallet-magic' -import { EvmWalletStrategy } from '@injectivelabs/wallet-evm' -import { BaseWalletStrategy } from '@injectivelabs/wallet-core' -import { CosmosWalletStrategy } from '@injectivelabs/wallet-cosmos' -import { TrezorWalletStrategy } from '@injectivelabs/wallet-trezor' -import { WalletConnectStrategy } from '@injectivelabs/wallet-wallet-connect' -import { PrivateKeyWalletStrategy } from '@injectivelabs/wallet-private-key' -import { CosmostationWalletStrategy } from '@injectivelabs/wallet-cosmostation' - -const ethereumWalletsDisabled = (args: WalletStrategyArguments) => { - const { ethereumOptions } = args - - if (!ethereumOptions) { - return true - } - - const { ethereumChainId } = ethereumOptions - - if (!ethereumChainId) { - return true - } - - return false -} - -const createStrategy = ({ - args, - wallet, -}: { - args: WalletStrategyArguments - wallet: Wallet -}): ConcreteWalletStrategy | undefined => { - /** - * If we only want to use Cosmos Native Wallets - * We are not creating strategies for Ethereum Native Wallets - */ - if (isEthWallet(wallet) && ethereumWalletsDisabled(args)) { - return undefined - } - - const ethWalletArgs = { - chainId: args.chainId, - ethereumOptions: args.ethereumOptions as WalletStrategyEthereumOptions, - } as ConcreteEthereumWalletStrategyArgs - - switch (wallet) { - case Wallet.Metamask: - return new EvmWalletStrategy({ - ...ethWalletArgs, - wallet: Wallet.Metamask, - }) - case Wallet.Ledger: - return new LedgerLiveStrategy(ethWalletArgs) - case Wallet.LedgerLegacy: - return new LedgerLegacyStrategy(ethWalletArgs) - case Wallet.TrustWallet: - return new EvmWalletStrategy({ - ...ethWalletArgs, - wallet: Wallet.TrustWallet, - }) - case Wallet.Trezor: - return new TrezorWalletStrategy(ethWalletArgs) - case Wallet.Phantom: - return new EvmWalletStrategy({ - ...ethWalletArgs, - wallet: Wallet.Phantom, - }) - case Wallet.OkxWallet: - return new EvmWalletStrategy({ - ...ethWalletArgs, - wallet: Wallet.OkxWallet, - }) - case Wallet.BitGet: - return new EvmWalletStrategy({ - ...ethWalletArgs, - wallet: Wallet.BitGet, - }) - case Wallet.WalletConnect: - return new WalletConnectStrategy({ - ...ethWalletArgs, - metadata: args.options?.metadata, - }) - case Wallet.PrivateKey: - return new PrivateKeyWalletStrategy({ - ...ethWalletArgs, - privateKey: args.options?.privateKey, - }) - case Wallet.Keplr: - return new CosmosWalletStrategy({ ...args, wallet: Wallet.Keplr }) - case Wallet.Cosmostation: - return new CosmostationWalletStrategy({ ...args }) - case Wallet.Leap: - return new CosmosWalletStrategy({ ...args, wallet: Wallet.Leap }) - case Wallet.Ninji: - return new CosmosWalletStrategy({ ...args, wallet: Wallet.Ninji }) - case Wallet.OWallet: - return new CosmosWalletStrategy({ ...args, wallet: Wallet.OWallet }) - case Wallet.Magic: - if ( - !args.options?.metadata?.magic || - !(args.options?.metadata.magic as MagicMetadata)?.apiKey || - !(args.options?.metadata.magic as MagicMetadata)?.rpcEndpoint - ) { - return undefined - } - - return new MagicStrategy({ - ...args, - metadata: args.options.metadata.magic as MagicMetadata, - }) - default: - return undefined - } -} - -const createAllStrategies = ( - args: WalletStrategyArguments, -): ConcreteStrategiesArg => { - return Object.values(Wallet).reduce( - (strategies, wallet) => ({ - ...strategies, - [wallet]: createStrategy({ args, wallet: wallet as Wallet }), - }), - {} as ConcreteStrategiesArg, - ) -} - -export class WalletStrategy extends BaseWalletStrategy { - constructor(args: WalletStrategyArguments) { - const strategies = createAllStrategies(args) - - super({ - ...args, - strategies, - }) - } - - /** - * Case 1: Private Key is set dynamically - * If we have a dynamically set private key, - * we are creating a new PrivateKey strategy - * with the specified private key - * - * Case 2: Wallet Connect Metadata set dynamically - */ - public setOptions(options?: ConcreteWalletStrategyOptions) { - if (options?.privateKey) { - this.strategies[Wallet.PrivateKey] = createStrategy({ - args: { ...this.args, options: { privateKey: options.privateKey } }, - wallet: Wallet.PrivateKey, - }) - } - - if (options?.metadata) { - this.strategies[Wallet.WalletConnect] = createStrategy({ - args: { ...this.args, options: { metadata: options.metadata } }, - wallet: Wallet.WalletConnect, - }) - } - } -} - -export const createStrategyFactory = (args: WalletStrategyArguments) => { - return new WalletStrategy(args) -} diff --git a/packages/wallets/wallet-strategy/src/strategy/index.ts b/packages/wallets/wallet-strategy/src/strategy/index.ts index aa1136ab5..013e6bbbb 100644 --- a/packages/wallets/wallet-strategy/src/strategy/index.ts +++ b/packages/wallets/wallet-strategy/src/strategy/index.ts @@ -1 +1,180 @@ -export * from './WalletStrategy.js' +import { + Wallet, + isEthWallet, + MagicMetadata, + ConcreteStrategiesArg, + ConcreteWalletStrategy, + WalletStrategyArguments, + ConcreteWalletStrategyOptions, + WalletStrategyEthereumOptions, + ConcreteEthereumWalletStrategyArgs, +} from '@injectivelabs/wallet-base' +import { + LedgerLiveStrategy, + LedgerLegacyStrategy, +} from '@injectivelabs/wallet-ledger' +import { MagicStrategy } from '@injectivelabs/wallet-magic' +import { EvmWalletStrategy } from '@injectivelabs/wallet-evm' +import { BaseWalletStrategy } from '@injectivelabs/wallet-core' +import { CosmosWalletStrategy } from '@injectivelabs/wallet-cosmos' +import { TrezorWalletStrategy } from '@injectivelabs/wallet-trezor' +import { WalletConnectStrategy } from '@injectivelabs/wallet-wallet-connect' +import { PrivateKeyWalletStrategy } from '@injectivelabs/wallet-private-key' +import { CosmostationWalletStrategy } from '@injectivelabs/wallet-cosmostation' + +const ethereumWalletsDisabled = (args: WalletStrategyArguments) => { + const { ethereumOptions } = args + + if (!ethereumOptions) { + return true + } + + const { ethereumChainId } = ethereumOptions + + if (!ethereumChainId) { + return true + } + + return false +} + +const createStrategy = ({ + args, + wallet, +}: { + args: WalletStrategyArguments + wallet: Wallet +}): ConcreteWalletStrategy | undefined => { + /** + * If we only want to use Cosmos Native Wallets + * We are not creating strategies for Ethereum Native Wallets + */ + if (isEthWallet(wallet) && ethereumWalletsDisabled(args)) { + return undefined + } + + const ethWalletArgs = { + chainId: args.chainId, + ethereumOptions: args.ethereumOptions as WalletStrategyEthereumOptions, + } as ConcreteEthereumWalletStrategyArgs + + switch (wallet) { + case Wallet.Metamask: + return new EvmWalletStrategy({ + ...ethWalletArgs, + wallet: Wallet.Metamask, + }) + case Wallet.Ledger: + return new LedgerLiveStrategy(ethWalletArgs) + case Wallet.LedgerLegacy: + return new LedgerLegacyStrategy(ethWalletArgs) + case Wallet.TrustWallet: + return new EvmWalletStrategy({ + ...ethWalletArgs, + wallet: Wallet.TrustWallet, + }) + case Wallet.Trezor: + return new TrezorWalletStrategy(ethWalletArgs) + case Wallet.Phantom: + return new EvmWalletStrategy({ + ...ethWalletArgs, + wallet: Wallet.Phantom, + }) + case Wallet.OkxWallet: + return new EvmWalletStrategy({ + ...ethWalletArgs, + wallet: Wallet.OkxWallet, + }) + case Wallet.BitGet: + return new EvmWalletStrategy({ + ...ethWalletArgs, + wallet: Wallet.BitGet, + }) + case Wallet.WalletConnect: + return new WalletConnectStrategy({ + ...ethWalletArgs, + metadata: args.options?.metadata, + }) + case Wallet.PrivateKey: + return new PrivateKeyWalletStrategy({ + ...ethWalletArgs, + privateKey: args.options?.privateKey, + }) + case Wallet.Keplr: + return new CosmosWalletStrategy({ ...args, wallet: Wallet.Keplr }) + case Wallet.Cosmostation: + return new CosmostationWalletStrategy({ ...args }) + case Wallet.Leap: + return new CosmosWalletStrategy({ ...args, wallet: Wallet.Leap }) + case Wallet.Ninji: + return new CosmosWalletStrategy({ ...args, wallet: Wallet.Ninji }) + case Wallet.OWallet: + return new CosmosWalletStrategy({ ...args, wallet: Wallet.OWallet }) + case Wallet.Magic: + if ( + !args.options?.metadata?.magic || + !(args.options?.metadata.magic as MagicMetadata)?.apiKey || + !(args.options?.metadata.magic as MagicMetadata)?.rpcEndpoint + ) { + return undefined + } + + return new MagicStrategy({ + ...args, + metadata: args.options.metadata.magic as MagicMetadata, + }) + default: + return undefined + } +} + +const createAllStrategies = ( + args: WalletStrategyArguments, +): ConcreteStrategiesArg => { + return Object.values(Wallet).reduce( + (strategies, wallet) => ({ + ...strategies, + [wallet]: createStrategy({ args, wallet: wallet as Wallet }), + }), + {} as ConcreteStrategiesArg, + ) +} + +export class WalletStrategy extends BaseWalletStrategy { + constructor(args: WalletStrategyArguments) { + const strategies = createAllStrategies(args) + + super({ + ...args, + strategies, + }) + } + + /** + * Case 1: Private Key is set dynamically + * If we have a dynamically set private key, + * we are creating a new PrivateKey strategy + * with the specified private key + * + * Case 2: Wallet Connect Metadata set dynamically + */ + public setOptions(options?: ConcreteWalletStrategyOptions) { + if (options?.privateKey) { + this.strategies[Wallet.PrivateKey] = createStrategy({ + args: { ...this.args, options: { privateKey: options.privateKey } }, + wallet: Wallet.PrivateKey, + }) + } + + if (options?.metadata) { + this.strategies[Wallet.WalletConnect] = createStrategy({ + args: { ...this.args, options: { metadata: options.metadata } }, + wallet: Wallet.WalletConnect, + }) + } + } +} + +export const createStrategyFactory = (args: WalletStrategyArguments) => { + return new WalletStrategy(args) +} diff --git a/yarn.lock b/yarn.lock index 1acd239a8..605413172 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2210,16 +2210,6 @@ dependencies: browser-headers "^0.4.1" -"@injectivelabs/indexer-proto-ts@1.13.1": - version "1.13.1" - resolved "https://registry.npmjs.org/@injectivelabs/indexer-proto-ts/-/indexer-proto-ts-1.13.1.tgz#94de25619e1725d393c2e447298374466540e763" - integrity sha512-w5C2qREyNZyc7bvGUvxzl5JetUkChFDTzZqW55qox+Iy6jiCkNILPTiArlHMlv/i04ymJ3B70Ftsr1j7V4lZrA== - dependencies: - "@injectivelabs/grpc-web" "^0.0.1" - google-protobuf "^3.14.0" - protobufjs "^7.0.0" - rxjs "^7.4.0" - "@injectivelabs/indexer-proto-ts@1.13.3": version "1.13.3" resolved "https://registry.yarnpkg.com/@injectivelabs/indexer-proto-ts/-/indexer-proto-ts-1.13.3.tgz#9f1963a8bc87bd4d6f88262fb4a4552b2e6fa79e" @@ -2250,67 +2240,6 @@ protobufjs "^7.0.0" rxjs "^7.4.0" -"@injectivelabs/sdk-ts@^1.14.19": - version "1.14.32" - resolved "https://registry.yarnpkg.com/@injectivelabs/sdk-ts/-/sdk-ts-1.14.32.tgz#560bc07a717d14bac81ee4f5ef17f9ac42abbd67" - integrity sha512-l4V5TK4QnTS8kgv4Qnmmi4rZo+2yF9y4S6lhH6eoyRuPb/xXODUzkyQDIHwbd1Qf92G31qSdL76Koe13v/SBIQ== - dependencies: - "@apollo/client" "^3.11.10" - "@cosmjs/amino" "^0.32.3" - "@cosmjs/proto-signing" "^0.32.3" - "@cosmjs/stargate" "^0.32.3" - "@ethersproject/bytes" "^5.7.0" - "@injectivelabs/core-proto-ts" "1.13.4" - "@injectivelabs/exceptions" "^1.14.32" - "@injectivelabs/grpc-web" "^0.0.1" - "@injectivelabs/grpc-web-node-http-transport" "^0.0.2" - "@injectivelabs/grpc-web-react-native-transport" "^0.0.2" - "@injectivelabs/indexer-proto-ts" "1.13.1" - "@injectivelabs/mito-proto-ts" "1.13.0" - "@injectivelabs/networks" "^1.14.32" - "@injectivelabs/olp-proto-ts" "1.13.1" - "@injectivelabs/test-utils" "^1.14.32" - "@injectivelabs/ts-types" "^1.14.32" - "@injectivelabs/utils" "^1.14.32" - "@metamask/eth-sig-util" "^4.0.0" - "@noble/curves" "^1.4.0" - axios "^1.6.4" - bech32 "^2.0.0" - bip39 "^3.0.4" - cosmjs-types "^0.9.0" - ethereumjs-util "^7.1.4" - ethers "^6.5.1" - google-protobuf "^3.21.0" - graphql "^16.3.0" - http-status-codes "^2.2.0" - js-sha3 "^0.8.0" - jscrypto "^1.0.3" - keccak256 "^1.0.6" - secp256k1 "^4.0.3" - shx "^0.3.2" - snakecase-keys "^5.4.1" - -"@injectivelabs/wallet-base@^0.0.6": - version "0.0.6" - resolved "https://registry.yarnpkg.com/@injectivelabs/wallet-base/-/wallet-base-0.0.6.tgz#073ee305b901c939b1dd5807b0506105c8d3f84d" - integrity sha512-QfFCW7KGbTC6MSPZxTAwy4vqH855EHWtvEXi3l3cS2OWb8llt1XhOc2W0YiCywcgEWh/Sv7EQb00VfMQAGM58g== - dependencies: - "@ethereumjs/common" "^3.1.1" - "@ethereumjs/tx" "^4.1.1" - "@injectivelabs/exceptions" "^1.14.19" - "@injectivelabs/networks" "^1.14.19" - "@injectivelabs/sdk-ts" "^1.14.19" - "@injectivelabs/ts-types" "^1.14.19" - "@injectivelabs/utils" "^1.14.19" - alchemy-sdk "^3.4.7" - eip1193-provider "^1.0.1" - eth-sig-util "^3.0.1" - ethereumjs-util "^7.1.0" - ethers "^6.5.1" - hdkey "^2.0.1" - long "^5.2.1" - shx "^0.3.3" - "@ioredis/commands@^1.1.1": version "1.2.0" resolved "https://registry.npmjs.org/@ioredis/commands/-/commands-1.2.0.tgz#6d61b3097470af1fdbbe622795b8921d42018e11" @@ -12163,12 +12092,7 @@ libsodium-wrappers-sumo@^0.7.11: dependencies: libsodium-sumo "^0.7.13" -libsodium-wrappers@^0.7.6, "libsodium-wrappers@npm:@bangjelkoski/noop": - version "0.0.1" - resolved "https://registry.npmjs.org/@bangjelkoski/noop/-/noop-0.0.1.tgz#172235ce00dd4269258c420f01464f07eb9801ed" - integrity sha512-lQgYJJbok2Unu2vKh0TAtWxXo16kLXNrjqmfo0hQTAuE/tVvz5zVqMgGsj+gMkkWfsInEbUndG0nlyCwaFv8hQ== - -"libsodium@npm:@bangjelkoski/noop": +libsodium-wrappers@^0.7.6, "libsodium-wrappers@npm:@bangjelkoski/noop", "libsodium@npm:@bangjelkoski/noop": version "0.0.1" resolved "https://registry.npmjs.org/@bangjelkoski/noop/-/noop-0.0.1.tgz#172235ce00dd4269258c420f01464f07eb9801ed" integrity sha512-lQgYJJbok2Unu2vKh0TAtWxXo16kLXNrjqmfo0hQTAuE/tVvz5zVqMgGsj+gMkkWfsInEbUndG0nlyCwaFv8hQ==