Skip to content

Commit

Permalink
refactor: imports, added cosmos wallet strategy, removed deprecated m…
Browse files Browse the repository at this point in the history
…ethods
  • Loading branch information
bangjelkoski committed Oct 29, 2024
1 parent 8dc1f82 commit 9127634
Show file tree
Hide file tree
Showing 32 changed files with 8,110 additions and 284 deletions.
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@
"test:sdk-ts:core:modules": "jest ./packages/sdk-ts/src/core/modules",
"test:sdk-ts:core": "jest ./packages/sdk-ts/src/core",
"test:sdk-ts:client": "jest ./packages/sdk-ts/src/client",
"test:bridge-ts": "jest ./packages/bridge-ts/src",
"test:sdk-ui-ts": "jest ./packages/sdk-ui-ts/src",
"test:token-metadata": "jest ./packages/token-metadata/test",
"test:ci": "jest --coverage --ci --reporters='jest-junit'",
"coverage": "jest --coverage",
"coverage:unit": "yarn test:unit --coverage",
Expand Down
4 changes: 3 additions & 1 deletion packages/sdk-ts/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DirectSignResponse } from '@cosmjs/proto-signing'
import { AminoSignResponse } from '@cosmjs/amino'
import {
CosmosTxV1Beta1Tx,
CosmosBaseV1Beta1Coin,
Expand All @@ -23,5 +24,6 @@ export enum StreamOperation {

export type GrpcCoin = CosmosBaseV1Beta1Coin.Coin
export type TxRaw = CosmosTxV1Beta1Tx.TxRaw
export type SignDoc = CosmosTxV1Beta1Tx.SignDoc

export { DirectSignResponse }
export { DirectSignResponse, AminoSignResponse }
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import {
TxRaw,
TxResponse,
waitTxBroadcasted,
AminoSignResponse,
DirectSignResponse,
createTxRawFromSigResponse,
createSignDocFromTransaction,
} from '@injectivelabs/sdk-ts'
import type { DirectSignResponse } from '@cosmjs/proto-signing'
import { StdSignDoc } from '@keplr-wallet/types'
import { AminoSignResponse } from '@cosmjs/launchpad'
import { LeapWallet } from '../../../utils/wallets/leap'
import { WalletAction, WalletDeviceType } from '../../../types/enums'
import { ConcreteCosmosWalletStrategy } from '../../types/strategy'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import {
TxRaw,
TxResponse,
waitTxBroadcasted,
AminoSignResponse,
DirectSignResponse,
createTxRawFromSigResponse,
createSignDocFromTransaction,
} from '@injectivelabs/sdk-ts'
import type { DirectSignResponse } from '@cosmjs/proto-signing'
import { StdSignDoc } from '@keplr-wallet/types'
import { AminoSignResponse } from '@cosmjs/launchpad'
import { NinjiWallet } from '../../../utils/wallets/ninji'
import { WalletAction, WalletDeviceType } from '../../../types/enums'
import { ConcreteCosmosWalletStrategy } from '../../types/strategy'
Expand Down
39 changes: 14 additions & 25 deletions packages/wallets/wallet-base/src/types/strategy.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import type { DirectSignResponse } from '@cosmjs/proto-signing'
import {
ChainId,
CosmosChainId,
AccountAddress,
EthereumChainId,
} from '@injectivelabs/ts-types'
import type { TxRaw, TxResponse } from '@injectivelabs/sdk-ts'
import { AminoSignResponse, StdSignDoc } from '@keplr-wallet/types'
import type {
TxRaw,
TxResponse,
AminoSignResponse,
DirectSignResponse,
} from '@injectivelabs/sdk-ts'
import { StdSignDoc } from '@keplr-wallet/types'
import { WalletDeviceType, Wallet } from './enums'

export type onAccountChangeCallback = (account: string) => void
Expand Down Expand Up @@ -102,7 +106,7 @@ export interface ConcreteCosmosWalletStrategy {
options: SendTransactionOptions,
): Promise<TxResponse>

signTransaction(transaction: {
signCosmosTransaction(transaction: {
txRaw: TxRaw
chainId: string
accountNumber: number
Expand Down Expand Up @@ -132,7 +136,6 @@ export interface ConcreteWalletStrategy
extends Omit<
ConcreteCosmosWalletStrategy,
| 'sendTransaction'
| 'signTransaction'
| 'isChainIdSupported'
| 'signAminoTransaction'
> {
Expand Down Expand Up @@ -171,14 +174,6 @@ export interface ConcreteWalletStrategy
options: { address: string; ethereumChainId: EthereumChainId },
): Promise<string>

/** @deprecated * */
signTransaction(
data:
| string /* EIP712 Typed Data in JSON */
| { txRaw: TxRaw; accountNumber: number; chainId: string },
address: string,
): Promise<string | DirectSignResponse>

/**
* Sign a cosmos transaction using the wallet provider
*
Expand All @@ -199,11 +194,9 @@ export interface ConcreteWalletStrategy
* @param address - injective address
*/
signAminoCosmosTransaction(transaction: {
signDoc: any
accountNumber: number
chainId: string
signDoc: StdSignDoc
address: string
}): Promise<string>
}): Promise<AminoSignResponse>

/**
* Sign EIP712 TypedData using the wallet provider
Expand Down Expand Up @@ -252,20 +245,14 @@ export interface WalletStrategy {
tx: any,
options: { address: AccountAddress; ethereumChainId: EthereumChainId },
): Promise<string>
signTransaction(
data: string | { txRaw: TxRaw; accountNumber: number; chainId: string },
address: AccountAddress,
): Promise<string | DirectSignResponse>
signEip712TypedData(
eip712TypedData: string,
address: AccountAddress,
): Promise<string>
signAminoCosmosTransaction(transaction: {
signDoc: any
accountNumber: number
chainId: string
signDoc: StdSignDoc
address: string
}): Promise<string>
}): Promise<AminoSignResponse>
signCosmosTransaction(transaction: {
txRaw: TxRaw
accountNumber: number
Expand All @@ -281,3 +268,5 @@ export interface WalletStrategy {
disconnect(): Promise<void>
getCosmosWallet?(chainId: ChainId): CosmosWalletAbstraction
}

export { StdSignDoc}
8 changes: 8 additions & 0 deletions packages/wallets/wallet-base/src/utils/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,11 @@ 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,
]
67 changes: 11 additions & 56 deletions packages/wallets/wallet-core/src/broadcaster/MsgBroadcaster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,8 @@ export class MsgBroadcaster {
const { walletStrategy } = this
const txWithAddresses = {
...tx,
ethereumAddress: getEthereumSignerAddress(
tx.injectiveAddress || tx.address,
),
injectiveAddress: getInjectiveSignerAddress(
tx.injectiveAddress || tx.address,
),
ethereumAddress: getEthereumSignerAddress(tx.injectiveAddress),
injectiveAddress: getInjectiveSignerAddress(tx.injectiveAddress),
} as MsgBroadcasterTxOptionsWithAddresses

if (ofacWallets.includes(txWithAddresses.ethereumAddress)) {
Expand Down Expand Up @@ -171,12 +167,8 @@ export class MsgBroadcaster {
const { walletStrategy } = this
const txWithAddresses = {
...tx,
ethereumAddress: getEthereumSignerAddress(
tx.injectiveAddress || tx.address,
),
injectiveAddress: getInjectiveSignerAddress(
tx.injectiveAddress || tx.address,
),
ethereumAddress: getEthereumSignerAddress(tx.injectiveAddress),
injectiveAddress: getInjectiveSignerAddress(tx.injectiveAddress),
} as MsgBroadcasterTxOptionsWithAddresses

if (ofacWallets.includes(txWithAddresses.ethereumAddress)) {
Expand All @@ -190,39 +182,6 @@ export class MsgBroadcaster {
: this.broadcastWeb3V2(txWithAddresses)
}

/**
* Broadcasting the transaction using the client
* side approach for cosmos native wallets
* and feeDelegation support approach for ethereum native
* wallets (default one)
*
* @param tx
* @returns {string} transaction hash
* @deprecated
*/
async broadcastOld(tx: MsgBroadcasterTxOptions) {
const { walletStrategy } = this
const txWithAddresses = {
...tx,
ethereumAddress: getEthereumSignerAddress(
tx.injectiveAddress || tx.address,
),
injectiveAddress: getInjectiveSignerAddress(
tx.injectiveAddress || tx.address,
),
} as MsgBroadcasterTxOptionsWithAddresses

if (ofacWallets.includes(txWithAddresses.ethereumAddress)) {
throw new GeneralException(
new Error('You cannot execute this transaction'),
)
}

return isCosmosWallet(walletStrategy.wallet)
? this.broadcastCosmos(txWithAddresses)
: this.broadcastWeb3WithFeeDelegation(txWithAddresses)
}

/**
* Broadcasting the transaction using the feeDelegation
* support approach for both cosmos and ethereum native wallets
Expand All @@ -234,12 +193,8 @@ export class MsgBroadcaster {
const { walletStrategy } = this
const txWithAddresses = {
...tx,
ethereumAddress: getEthereumSignerAddress(
tx.injectiveAddress || tx.address,
),
injectiveAddress: getInjectiveSignerAddress(
tx.injectiveAddress || tx.address,
),
ethereumAddress: getEthereumSignerAddress(tx.injectiveAddress),
injectiveAddress: getInjectiveSignerAddress(tx.injectiveAddress),
} as MsgBroadcasterTxOptionsWithAddresses

if (ofacWallets.includes(txWithAddresses.ethereumAddress)) {
Expand Down Expand Up @@ -643,14 +598,14 @@ export class MsgBroadcaster {
timeoutHeight: timeoutHeight.toFixed(),
})

const signature = (await walletStrategy.signAminoCosmosTransaction({
const signResponse = await walletStrategy.signAminoCosmosTransaction({
signDoc: aminoSignDoc,
chainId,
address: tx.injectiveAddress,
accountNumber: baseAccount.accountNumber,
})) as string
})

txRaw.signatures = [Buffer.from(signature, 'base64')]
txRaw.signatures = [
Buffer.from(signResponse.signature.signature, 'base64'),
]

return walletStrategy.sendTransaction(txRaw, {
chainId,
Expand Down
13 changes: 0 additions & 13 deletions packages/wallets/wallet-core/src/broadcaster/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import BaseWalletStrategy from '../strategy/BaseWalletStrategy'

export interface MsgBroadcasterTxOptions {
memo?: string
address?: string /* @deprecated */
ethereumAddress?: string
injectiveAddress?: string
msgs: Msgs | Msgs[]
Expand All @@ -26,18 +25,6 @@ export interface MsgBroadcasterTxOptionsWithAddresses
export interface MsgBroadcasterOptions {
network: Network
endpoints?: NetworkEndpoints

/**
* Only used if we want to override the default
* endpoints taken from the network param
*
* @deprecated - taken from the network parameter or use the endpoints
* */
networkEndpoints?: {
indexerApi: string
sentryGrpcApi: string
sentryHttpApi: string
}
chainId?: ChainId
ethereumChainId?: EthereumChainId
feePayerPubKey?: string
Expand Down
25 changes: 9 additions & 16 deletions packages/wallets/wallet-core/src/strategy/BaseWalletStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { TxRaw, TxResponse } from '@injectivelabs/sdk-ts'
import { DirectSignResponse } from '@cosmjs/proto-signing'
import {
TxRaw,
TxResponse,
AminoSignResponse,
DirectSignResponse,
} from '@injectivelabs/sdk-ts'
import {
ChainId,
AccountAddress,
Expand All @@ -21,6 +25,7 @@ import {
ConcreteWalletStrategyOptions,
WalletStrategy as WalletStrategyInterface,
} from '@injectivelabs/wallet-base'
import { StdSignDoc } from '@keplr-wallet/types'

const getInitialWallet = (args: WalletStrategyArguments): Wallet => {
if (args.wallet) {
Expand Down Expand Up @@ -124,16 +129,6 @@ export default class BaseWalletStrategy implements WalletStrategyInterface {
return this.getStrategy().sendEthereumTransaction(tx, options)
}

/** @deprecated * */
public async signTransaction(
data:
| string /* When using EIP712 typed data */
| { txRaw: TxRaw; accountNumber: number; chainId: string },
address: AccountAddress,
): Promise<string | DirectSignResponse> {
return this.getStrategy().signTransaction(data, address)
}

public async signEip712TypedData(
eip712TypedData: string,
address: AccountAddress,
Expand All @@ -153,11 +148,9 @@ export default class BaseWalletStrategy implements WalletStrategyInterface {
}

public async signAminoCosmosTransaction(transaction: {
signDoc: any
accountNumber: number
chainId: string
signDoc: StdSignDoc
address: string
}): Promise<string> {
}): Promise<AminoSignResponse> {
if (isEthWallet(this.wallet)) {
throw new WalletException(
new Error(`You can't sign Cosmos Transaction using ${this.wallet}`),
Expand Down
Loading

0 comments on commit 9127634

Please sign in to comment.