Skip to content

Commit

Permalink
chore: cosmostation wallet support deposits
Browse files Browse the repository at this point in the history
  • Loading branch information
shane-moore committed Dec 26, 2024
1 parent 5527b3d commit 0f1bb9d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 35 deletions.
7 changes: 4 additions & 3 deletions packages/wallets/wallet-base/src/base.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { ChainId, EthereumChainId } from '@injectivelabs/ts-types'
import { ChainId, CosmosChainId, EthereumChainId } from '@injectivelabs/ts-types'
import {
WalletEventListener,
ConcreteWalletStrategyArgs,
ConcreteCosmosWalletStrategyArgs,
ConcreteEthereumWalletStrategyArgs,
} from './types/index.js'

export default abstract class BaseConcreteStrategy {
protected chainId: ChainId
protected chainId: ChainId | CosmosChainId

protected ethereumChainId?: EthereumChainId

protected listeners: Partial<Record<WalletEventListener, any>> = {}

protected constructor(args: ConcreteWalletStrategyArgs | ConcreteEthereumWalletStrategyArgs) {
protected constructor(args: ConcreteWalletStrategyArgs | ConcreteEthereumWalletStrategyArgs | ConcreteCosmosWalletStrategyArgs) {
this.ethereumChainId = 'ethereumOptions' in args && args.ethereumOptions
? args.ethereumOptions.ethereumChainId
: undefined
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 @@ -45,7 +45,7 @@ export class CosmosWalletStrategy

constructor(
args: {
chainId: ChainId
chainId: ChainId | CosmosChainId
endpoints?: { rest: string; rpc: string }
} & { wallet: Wallet },
) {
Expand Down
72 changes: 41 additions & 31 deletions packages/wallets/wallet-cosmostation/src/strategy/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,39 @@ import { makeSignDoc } from '@cosmjs/proto-signing'
import { SEND_TRANSACTION_MODE } from '@cosmostation/extension-client/cosmos.js'
import { CosmostationWallet } from './../wallet.js'

const INJECTIVE_CHAIN_NAME = 'injective'
const getChainNameFromChainId = (chainId: CosmosChainId | ChainId) => {
const [chainName] = chainId.split('-')

if (chainName.includes('cosmoshub')) {
return 'cosmos'
}

if (chainName.includes('core')) {
return 'persistence'
}

if (chainName.includes('evmos')) {
return 'evmos'
}

if (chainId.includes('ssc') || chainId.includes('fetch')) {
return chainId
}

return chainName
}

export class Cosmostation
extends BaseConcreteStrategy
implements ConcreteWalletStrategy
{
private cosmostationWallet?: Cosmos
public chainName: string

constructor(args: { chainId: ChainId }) {
constructor(args: { chainId: ChainId | CosmosChainId }) {
super(args)
this.chainId = args.chainId || CosmosChainId.Injective
this.chainName = getChainNameFromChainId(this.chainId)
}

async getWalletDeviceType(): Promise<WalletDeviceType> {
Expand All @@ -57,20 +79,17 @@ export class Cosmostation
const cosmostationWallet = await this.getCosmostationWallet()

try {
const accounts = await cosmostationWallet.requestAccount(
INJECTIVE_CHAIN_NAME,
)
const accounts = await cosmostationWallet.requestAccount(this.chainName)

return [accounts.address]
} catch (e: unknown) {
if ((e as any).code === 4001) {
throw new CosmosWalletException(
new Error('The user rejected the request'),
{
code: UnspecifiedErrorCode,
context: WalletAction.GetAccounts,
},
)
throw new CosmosWalletException(new Error('The user rejected the request'),
{
code: UnspecifiedErrorCode,
context: WalletAction.GetAccounts,
},
)
}

throw new CosmosWalletException(new Error((e as any).message), {
Expand All @@ -94,9 +113,7 @@ export class Cosmostation
_options: { address: AccountAddress; ethereumChainId: EthereumChainId },
): Promise<string> {
throw new CosmosWalletException(
new Error(
'sendEthereumTransaction is not supported. Cosmostation only supports sending cosmos transactions',
),
new Error('sendEthereumTransaction is not supported. Cosmostation only supports sending cosmos transactions'),
{
code: UnspecifiedErrorCode,
context: WalletAction.SendEthereumTransaction,
Expand All @@ -113,7 +130,7 @@ export class Cosmostation

try {
const response = await cosmostationWallet.sendTransaction(
INJECTIVE_CHAIN_NAME,
this.chainName,
CosmosTxV1Beta1Tx.TxRaw.encode(txRaw).finish(),
SEND_TRANSACTION_MODE.SYNC,
)
Expand Down Expand Up @@ -167,7 +184,7 @@ export class Cosmostation
try {
/* Sign the transaction */
const signDirectResponse = await cosmostationWallet.signDirect(
INJECTIVE_CHAIN_NAME,
this.chainName,
{
chain_id: chainId,
body_bytes: signDoc.bodyBytes,
Expand Down Expand Up @@ -200,20 +217,15 @@ export class Cosmostation
const cosmostationWallet = await this.getCosmostationWallet()

try {
const account = await cosmostationWallet.requestAccount(
INJECTIVE_CHAIN_NAME,
)
const account = await cosmostationWallet.requestAccount(this.chainName)

return Buffer.from(account.publicKey).toString('base64')
} catch (e: unknown) {
if ((e as any).code === 4001) {
throw new CosmosWalletException(
new Error('The user rejected the request'),
{
code: UnspecifiedErrorCode,
context: WalletAction.GetAccounts,
},
)
throw new CosmosWalletException(new Error('The user rejected the request'), {
code: UnspecifiedErrorCode,
context: WalletAction.GetAccounts,
})
}

throw new CosmosWalletException(new Error((e as any).message), {
Expand Down Expand Up @@ -244,7 +256,7 @@ export class Cosmostation
const cosmostationWallet = await this.getCosmostationWallet()

const signature = await cosmostationWallet.signMessage(
INJECTIVE_CHAIN_NAME,
this.chainName,
signer,
toUtf8(data),
)
Expand All @@ -270,9 +282,7 @@ export class Cosmostation

async getEthereumTransactionReceipt(_txHash: string): Promise<string> {
throw new CosmosWalletException(
new Error(
'getEthereumTransactionReceipt is not supported on Cosmostation',
),
new Error('getEthereumTransactionReceipt is not supported on Cosmostation'),
{
code: UnspecifiedErrorCode,
type: ErrorType.WalletError,
Expand Down

0 comments on commit 0f1bb9d

Please sign in to comment.