Skip to content

Commit

Permalink
refactor: remove experimental chains and strategy improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
bangjelkoski committed Nov 14, 2023
1 parent 44b9fcd commit 2d396eb
Show file tree
Hide file tree
Showing 15 changed files with 162 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export declare namespace MsgTransferCosmjs {

/**
* @category Messages
*
* @deprected use MsgTransfer with SIGN_DIRECT and a Cosmos wallet
*/
export default class MsgTransferCosmjs {
params: MsgTransferCosmjs.Params
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import {
createSignDocFromTransaction,
} from '@injectivelabs/sdk-ts'
import { DirectSignResponse, makeSignDoc } from '@cosmjs/proto-signing'
import { cosmos, InstallError, Cosmos } from '@cosmostation/extension-client'
import { InstallError, Cosmos } from '@cosmostation/extension-client'
import { SEND_TRANSACTION_MODE } from '@cosmostation/extension-client/cosmos'
import { AminoSignResponse, StdSignDoc } from '@keplr-wallet/types'
import { ConcreteCosmosWalletStrategy } from '../../types/strategy'
import { CosmostationWallet } from './../../../utils/wallets/cosmostation'
import { WalletAction, WalletDeviceType } from '../../../types/enums'
import { CosmosTxV1Beta1Tx } from '@injectivelabs/sdk-ts'

Expand All @@ -40,7 +41,7 @@ const getChainNameFromChainId = (chainId: CosmosChainId) => {
export default class Cosmostation implements ConcreteCosmosWalletStrategy {
public chainName: string

public provider?: Cosmos
public cosmostationWallet?: Cosmos

public chainId: CosmosChainId

Expand All @@ -53,23 +54,12 @@ export default class Cosmostation implements ConcreteCosmosWalletStrategy {
return Promise.resolve(WalletDeviceType.Browser)
}

async isChainIdSupported(chainId?: CosmosChainId): Promise<boolean> {
const actualChainId = chainId || this.chainId
const provider = await this.getProvider()

const supportedChainIds = await provider.getSupportedChainIds()

return !!supportedChainIds.official.find(
(chainId) => chainId === actualChainId,
)
}

async getAddresses(): Promise<string[]> {
const { chainName } = this
const provider = await this.getProvider()
const cosmostationWallet = await this.getCosmostationWallet()

try {
const accounts = await provider.requestAccount(chainName)
const accounts = await cosmostationWallet.requestAccount(chainName)

return [accounts.address]
} catch (e: unknown) {
Expand All @@ -94,11 +84,11 @@ export default class Cosmostation implements ConcreteCosmosWalletStrategy {
transaction: DirectSignResponse | CosmosTxV1Beta1Tx.TxRaw,
): Promise<TxResponse> {
const { chainName } = this
const provider = await this.getProvider()
const cosmostationWallet = await this.getCosmostationWallet()
const txRaw = createTxRawFromSigResponse(transaction)

try {
const response = await provider.sendTransaction(
const response = await cosmostationWallet.sendTransaction(
chainName,
CosmosTxV1Beta1Tx.TxRaw.encode(txRaw).finish(),
SEND_TRANSACTION_MODE.ASYNC,
Expand Down Expand Up @@ -133,12 +123,12 @@ export default class Cosmostation implements ConcreteCosmosWalletStrategy {
chainId: string
}) {
const { chainName, chainId } = this
const provider = await this.getProvider()
const cosmostationWallet = await this.getCosmostationWallet()
const signDoc = createSignDocFromTransaction(transaction)

try {
/* Sign the transaction */
const signDirectResponse = await provider.signDirect(
const signDirectResponse = await cosmostationWallet.signDirect(
chainName,
{
chain_id: chainId,
Expand Down Expand Up @@ -179,10 +169,10 @@ export default class Cosmostation implements ConcreteCosmosWalletStrategy {

async getPubKey(): Promise<string> {
const { chainName } = this
const provider = await this.getProvider()
const cosmostationWallet = await this.getCosmostationWallet()

try {
const account = await provider.requestAccount(chainName)
const account = await cosmostationWallet.requestAccount(chainName)

return Buffer.from(account.publicKey).toString('base64')
} catch (e: unknown) {
Expand All @@ -203,15 +193,17 @@ export default class Cosmostation implements ConcreteCosmosWalletStrategy {
}
}

private async getProvider(): Promise<Cosmos> {
if (this.provider) {
return this.provider
private async getCosmostationWallet(): Promise<Cosmos> {
if (this.cosmostationWallet) {
return this.cosmostationWallet
}

const cosmostationWallet = new CosmostationWallet(this.chainId)

try {
const provider = await cosmos()
const provider = await cosmostationWallet.getCosmostationWallet()

this.provider = provider
this.cosmostationWallet = provider

return provider
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,10 @@ export default class Keplr implements ConcreteCosmosWalletStrategy {
: Promise.resolve(WalletDeviceType.Browser)
}

async isChainIdSupported(chainId?: CosmosChainId): Promise<boolean> {
const keplrWallet = chainId
? new KeplrWallet(chainId)
: this.getKeplrWallet()

return keplrWallet.checkChainIdSupport()
}

async getAddresses(): Promise<string[]> {
const keplrWallet = this.getKeplrWallet()

try {
if (!(await keplrWallet.checkChainIdSupport())) {
await keplrWallet.experimentalSuggestChain()
}

const accounts = await keplrWallet.getAccounts()

return accounts.map((account) => account.address)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ export default class Leap implements ConcreteCosmosWalletStrategy {
return Promise.resolve(WalletDeviceType.Browser)
}

async isChainIdSupported(chainId?: CosmosChainId): Promise<boolean> {
const leapWallet = chainId ? new LeapWallet(chainId) : this.getLeapWallet()

return leapWallet.checkChainIdSupport()
}

async getAddresses(): Promise<string[]> {
const { chainId } = this
const leapWallet = this.getLeapWallet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,6 @@ export default class Ninji implements ConcreteCosmosWalletStrategy {
return Promise.resolve(WalletDeviceType.Browser)
}

async isChainIdSupported(chainId?: CosmosChainId): Promise<boolean> {
const ninjiWallet = chainId
? new NinjiWallet(chainId)
: this.getNinjiWallet()

return ninjiWallet.checkChainIdSupport()
}

async getAddresses(): Promise<string[]> {
const { chainId } = this
const ninjiWallet = this.getNinjiWallet()
Expand Down
2 changes: 0 additions & 2 deletions packages/wallet-ts/src/strategies/types/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ export interface ConcreteCosmosWalletStrategy {
*/
sendTransaction(transaction: DirectSignResponse | TxRaw): Promise<TxResponse>

isChainIdSupported(chainId?: CosmosChainId): Promise<boolean>

signTransaction(transaction: {
txRaw: TxRaw
chainId: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,21 @@ import {
toUtf8,
} from '@injectivelabs/sdk-ts'
import { DirectSignResponse, makeSignDoc } from '@cosmjs/proto-signing'
import { cosmos, InstallError, Cosmos } from '@cosmostation/extension-client'
import { InstallError, Cosmos } from '@cosmostation/extension-client'
import { SEND_TRANSACTION_MODE } from '@cosmostation/extension-client/cosmos'
import { ConcreteWalletStrategy } from '../../types'
import BaseConcreteStrategy from './Base'
import { WalletAction, WalletDeviceType } from '../../../types/enums'
import { CosmosTxV1Beta1Tx } from '@injectivelabs/sdk-ts'
import { CosmostationWallet } from './../../../utils/wallets/cosmostation'

const INJECTIVE_CHAIN_NAME = 'injective'

export default class Cosmostation
extends BaseConcreteStrategy
implements ConcreteWalletStrategy
{
private provider?: Cosmos
private cosmostationWallet?: Cosmos

constructor(args: { chainId: ChainId }) {
super(args)
Expand All @@ -43,10 +44,12 @@ export default class Cosmostation
}

async getAddresses(): Promise<string[]> {
const provider = await this.getProvider()
const cosmostationWallet = await this.getCosmostationWallet()

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

return [accounts.address]
} catch (e: unknown) {
Expand Down Expand Up @@ -95,11 +98,11 @@ export default class Cosmostation
transaction: DirectSignResponse | CosmosTxV1Beta1Tx.TxRaw,
_options: { address: AccountAddress; chainId: ChainId },
): Promise<TxResponse> {
const provider = await this.getProvider()
const cosmostationWallet = await this.getCosmostationWallet()
const txRaw = createTxRawFromSigResponse(transaction)

try {
const response = await provider.sendTransaction(
const response = await cosmostationWallet.sendTransaction(
INJECTIVE_CHAIN_NAME,
CosmosTxV1Beta1Tx.TxRaw.encode(txRaw).finish(),
SEND_TRANSACTION_MODE.SYNC,
Expand Down Expand Up @@ -147,12 +150,12 @@ export default class Cosmostation
accountNumber: number
}) {
const { chainId } = this
const provider = await this.getProvider()
const cosmostationWallet = await this.getCosmostationWallet()
const signDoc = createSignDocFromTransaction(transaction)

try {
/* Sign the transaction */
const signDirectResponse = await provider.signDirect(
const signDirectResponse = await cosmostationWallet.signDirect(
INJECTIVE_CHAIN_NAME,
{
chain_id: chainId,
Expand Down Expand Up @@ -183,10 +186,12 @@ export default class Cosmostation
}

async getPubKey(): Promise<string> {
const provider = await this.getProvider()
const cosmostationWallet = await this.getCosmostationWallet()

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

return Buffer.from(account.publicKey).toString('base64')
} catch (e: unknown) {
Expand Down Expand Up @@ -225,9 +230,9 @@ export default class Cosmostation
data: string | Uint8Array,
): Promise<string> {
try {
const provider = await this.getProvider()
const cosmostationWallet = await this.getCosmostationWallet()

const signature = await provider.signMessage(
const signature = await cosmostationWallet.signMessage(
INJECTIVE_CHAIN_NAME,
signer,
toUtf8(data),
Expand Down Expand Up @@ -265,15 +270,17 @@ export default class Cosmostation
)
}

private async getProvider(): Promise<Cosmos> {
if (this.provider) {
return this.provider
private async getCosmostationWallet(): Promise<Cosmos> {
if (this.cosmostationWallet) {
return this.cosmostationWallet
}

const cosmostationWallet = new CosmostationWallet(this.chainId)

try {
const provider = await cosmos()
const provider = await cosmostationWallet.getCosmostationWallet()

this.provider = provider
this.cosmostationWallet = provider

return provider
} catch (e) {
Expand All @@ -289,7 +296,6 @@ export default class Cosmostation

throw new CosmosWalletException(new Error((e as any).message), {
code: UnspecifiedErrorCode,
type: ErrorType.WalletError,
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default class Keplr

try {
if (!(await keplrWallet.checkChainIdSupport())) {
await keplrWallet.experimentalSuggestChain()
await keplrWallet.chainNotSupported()
}

const accounts = await keplrWallet.getAccounts()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { cosmos, InstallError } from '@cosmostation/extension-client'
import {
ErrorType,
UnspecifiedErrorCode,
CosmosWalletException,
} from '@injectivelabs/exceptions'
import {
ChainId,
CosmosChainId,
TestnetCosmosChainId,
} from '@injectivelabs/ts-types'

export class CosmostationWallet {
private chainId: CosmosChainId | TestnetCosmosChainId | ChainId

constructor(chainId: CosmosChainId | TestnetCosmosChainId | ChainId) {
this.chainId = chainId
}

static async isChainIdSupported(chainId: CosmosChainId): Promise<boolean> {
return new CosmostationWallet(chainId).checkChainIdSupport()
}

public async checkChainIdSupport() {
const { chainId: actualChainId } = this
const provider = await this.getCosmostationWallet()
const chainName = actualChainId.split('-')

try {
const supportedChainIds = await provider.getSupportedChainIds()

return !!supportedChainIds.official.find((chainId) => chainId === chainId)
} catch (e) {
throw new CosmosWalletException(
new Error(
`Cosmostation doesn't support ${
chainName[0] || actualChainId
} network. Please use another Cosmos wallet`,
),
)
}
}

async getCosmostationWallet() {
try {
const provider = await cosmos()

return provider
} catch (e) {
if (e instanceof InstallError) {
throw new CosmosWalletException(
new Error('Please install the Cosmostation extension'),
{
code: UnspecifiedErrorCode,
type: ErrorType.WalletNotInstalledError,
},
)
}

throw new CosmosWalletException(new Error((e as any).message), {
code: UnspecifiedErrorCode,
})
}
}
}
1 change: 1 addition & 0 deletions packages/wallet-ts/src/utils/wallets/cosmostation/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './CosmostationWallet'
2 changes: 2 additions & 0 deletions packages/wallet-ts/src/utils/wallets/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export * from './keplr'
export * from './cosmostation'
export * from './leap'
export * from './phantom'
export * from './metamask'
export * from './ninji'
export * from './trust-wallet'
Loading

0 comments on commit 2d396eb

Please sign in to comment.