Skip to content

Commit

Permalink
chore: wallet strategy enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasRalee committed Nov 28, 2024
1 parent 3268f77 commit 5ac0bc3
Show file tree
Hide file tree
Showing 20 changed files with 424 additions and 332 deletions.
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions packages/wallets/wallet-base/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
9 changes: 9 additions & 0 deletions packages/wallets/wallet-base/src/utils/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/wallets/wallet-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`),
)
Expand Down
1 change: 1 addition & 0 deletions packages/wallets/wallet-cosmos/src/cosmos.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ declare global {
leap: KeplrWindow['keplr']
keplr: KeplrWindow['keplr']
ninji: KeplrWindow['ninji']
owallet?: KeplrWindow['owallet']
}
}
8 changes: 8 additions & 0 deletions packages/wallets/wallet-cosmos/src/data/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Wallet } from '@injectivelabs/wallet-base'

export const cosmosWallets = [
Wallet.Leap,
Wallet.Ninji,
Wallet.Keplr,
Wallet.OWallet,
]
1 change: 1 addition & 0 deletions packages/wallets/wallet-cosmos/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { CosmosWallet } from './wallet.js'
export { CosmosWalletStrategy } from './strategy/strategy.js'
export * from './utils/index.js'
26 changes: 19 additions & 7 deletions packages/wallets/wallet-cosmos/src/strategy/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from '@injectivelabs/exceptions'
import {
Wallet,
capitalize,
StdSignDoc,
WalletAction,
WalletDeviceType,
Expand Down Expand Up @@ -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<WalletDeviceType> {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -310,15 +322,15 @@ 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 {
const { wallet, cosmosWallet } = this

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,
Expand Down
60 changes: 60 additions & 0 deletions packages/wallets/wallet-cosmos/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -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)}.`,
),
)
}
}
39 changes: 23 additions & 16 deletions packages/wallets/wallet-cosmos/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -35,18 +35,21 @@ 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
}

public async isChainIdSupported(chainId: CosmosChainId): Promise<boolean> {
const { wallet } = this

return new CosmosWallet(chainId, wallet).checkChainIdSupport()
return new CosmosWallet({ chainId, wallet }).checkChainIdSupport()
}

public async getCosmosWallet() {
Expand Down Expand Up @@ -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.`,
),
Expand Down Expand Up @@ -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)}`,
),
)
}

Expand Down Expand Up @@ -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,
)}`,
),
)
}
Expand Down Expand Up @@ -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)}`),
)
}

Expand Down Expand Up @@ -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`,
),
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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)}`),
)
}

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

Expand Down
1 change: 1 addition & 0 deletions packages/wallets/wallet-cosmostation/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { CosmostationWallet } from './wallet.js'
export { Cosmostation as CosmostationWalletStrategy } from './strategy/strategy.js'
export * from './utils/index.js'
9 changes: 9 additions & 0 deletions packages/wallets/wallet-cosmostation/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -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
}
Loading

0 comments on commit 5ac0bc3

Please sign in to comment.