@@ -32,17 +32,39 @@ import { makeSignDoc } from '@cosmjs/proto-signing'
3232import { SEND_TRANSACTION_MODE } from '@cosmostation/extension-client/cosmos.js'
3333import { CosmostationWallet } from './../wallet.js'
3434
35- const INJECTIVE_CHAIN_NAME = 'injective'
35+ const getChainNameFromChainId = ( chainId : CosmosChainId | ChainId ) => {
36+ const [ chainName ] = chainId . split ( '-' )
37+
38+ if ( chainName . includes ( 'cosmoshub' ) ) {
39+ return 'cosmos'
40+ }
41+
42+ if ( chainName . includes ( 'core' ) ) {
43+ return 'persistence'
44+ }
45+
46+ if ( chainName . includes ( 'evmos' ) ) {
47+ return 'evmos'
48+ }
49+
50+ if ( chainId . includes ( 'ssc' ) || chainId . includes ( 'fetch' ) ) {
51+ return chainId
52+ }
53+
54+ return chainName
55+ }
3656
3757export class Cosmostation
3858 extends BaseConcreteStrategy
3959 implements ConcreteWalletStrategy
4060{
4161 private cosmostationWallet ?: Cosmos
62+ public chainName : string
4263
43- constructor ( args : { chainId : ChainId } ) {
64+ constructor ( args : { chainId : ChainId | CosmosChainId } ) {
4465 super ( args )
4566 this . chainId = args . chainId || CosmosChainId . Injective
67+ this . chainName = getChainNameFromChainId ( this . chainId )
4668 }
4769
4870 async getWalletDeviceType ( ) : Promise < WalletDeviceType > {
@@ -57,20 +79,17 @@ export class Cosmostation
5779 const cosmostationWallet = await this . getCosmostationWallet ( )
5880
5981 try {
60- const accounts = await cosmostationWallet . requestAccount (
61- INJECTIVE_CHAIN_NAME ,
62- )
82+ const accounts = await cosmostationWallet . requestAccount ( this . chainName )
6383
6484 return [ accounts . address ]
6585 } catch ( e : unknown ) {
6686 if ( ( e as any ) . code === 4001 ) {
67- throw new CosmosWalletException (
68- new Error ( 'The user rejected the request' ) ,
69- {
70- code : UnspecifiedErrorCode ,
71- context : WalletAction . GetAccounts ,
72- } ,
73- )
87+ throw new CosmosWalletException ( new Error ( 'The user rejected the request' ) ,
88+ {
89+ code : UnspecifiedErrorCode ,
90+ context : WalletAction . GetAccounts ,
91+ } ,
92+ )
7493 }
7594
7695 throw new CosmosWalletException ( new Error ( ( e as any ) . message ) , {
@@ -94,9 +113,7 @@ export class Cosmostation
94113 _options : { address : AccountAddress ; ethereumChainId : EthereumChainId } ,
95114 ) : Promise < string > {
96115 throw new CosmosWalletException (
97- new Error (
98- 'sendEthereumTransaction is not supported. Cosmostation only supports sending cosmos transactions' ,
99- ) ,
116+ new Error ( 'sendEthereumTransaction is not supported. Cosmostation only supports sending cosmos transactions' ) ,
100117 {
101118 code : UnspecifiedErrorCode ,
102119 context : WalletAction . SendEthereumTransaction ,
@@ -113,7 +130,7 @@ export class Cosmostation
113130
114131 try {
115132 const response = await cosmostationWallet . sendTransaction (
116- INJECTIVE_CHAIN_NAME ,
133+ this . chainName ,
117134 CosmosTxV1Beta1Tx . TxRaw . encode ( txRaw ) . finish ( ) ,
118135 SEND_TRANSACTION_MODE . SYNC ,
119136 )
@@ -167,7 +184,7 @@ export class Cosmostation
167184 try {
168185 /* Sign the transaction */
169186 const signDirectResponse = await cosmostationWallet . signDirect (
170- INJECTIVE_CHAIN_NAME ,
187+ this . chainName ,
171188 {
172189 chain_id : chainId ,
173190 body_bytes : signDoc . bodyBytes ,
@@ -200,20 +217,15 @@ export class Cosmostation
200217 const cosmostationWallet = await this . getCosmostationWallet ( )
201218
202219 try {
203- const account = await cosmostationWallet . requestAccount (
204- INJECTIVE_CHAIN_NAME ,
205- )
220+ const account = await cosmostationWallet . requestAccount ( this . chainName )
206221
207222 return Buffer . from ( account . publicKey ) . toString ( 'base64' )
208223 } catch ( e : unknown ) {
209224 if ( ( e as any ) . code === 4001 ) {
210- throw new CosmosWalletException (
211- new Error ( 'The user rejected the request' ) ,
212- {
213- code : UnspecifiedErrorCode ,
214- context : WalletAction . GetAccounts ,
215- } ,
216- )
225+ throw new CosmosWalletException ( new Error ( 'The user rejected the request' ) , {
226+ code : UnspecifiedErrorCode ,
227+ context : WalletAction . GetAccounts ,
228+ } )
217229 }
218230
219231 throw new CosmosWalletException ( new Error ( ( e as any ) . message ) , {
@@ -244,7 +256,7 @@ export class Cosmostation
244256 const cosmostationWallet = await this . getCosmostationWallet ( )
245257
246258 const signature = await cosmostationWallet . signMessage (
247- INJECTIVE_CHAIN_NAME ,
259+ this . chainName ,
248260 signer ,
249261 toUtf8 ( data ) ,
250262 )
@@ -270,9 +282,7 @@ export class Cosmostation
270282
271283 async getEthereumTransactionReceipt ( _txHash : string ) : Promise < string > {
272284 throw new CosmosWalletException (
273- new Error (
274- 'getEthereumTransactionReceipt is not supported on Cosmostation' ,
275- ) ,
285+ new Error ( 'getEthereumTransactionReceipt is not supported on Cosmostation' ) ,
276286 {
277287 code : UnspecifiedErrorCode ,
278288 type : ErrorType . WalletError ,
0 commit comments