Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/lib/restClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,52 @@ describe('RESTClient', () => {
expect(res.channel.state).toBe(State.STATE_UNINITIALIZED_UNSPECIFIED)
expect(res.channel.ordering).toBe(Order.ORDER_NONE_UNSPECIFIED)
})

describe('auth.accountInfo', () => {
it('parses a plain BaseAccount', async () => {
nock(mockRestUris[0])
.get('/cosmos/auth/v1beta1/accounts/init1qqqq')
.reply(200, {
account: {
'@type': '/cosmos.auth.v1beta1.BaseAccount',
address: 'init1qqqq',
pub_key: null,
account_number: '7',
sequence: '42',
},
})

const client = new RESTClient(mockRestUris)
const account = await client.auth.accountInfo('init1qqqq')

expect(account.getAccountNumber()).toBe(7)
expect(account.getSequenceNumber()).toBe(42)
})

it('unwraps an ethermint-style EthAccount (Injective)', async () => {
nock(mockRestUris[0])
.get('/cosmos/auth/v1beta1/accounts/inj1qqqq')
.reply(200, {
account: {
'@type': '/injective.types.v1beta1.EthAccount',
base_account: {
address: 'inj1qqqq',
pub_key: {
'@type': '/injective.crypto.v1beta1.ethsecp256k1.PubKey',
key: 'AsV5oddeB+hkByIJo/4lZiVUgXTzNfBPKC73cZ4K1YD2',
},
account_number: '13',
sequence: '5',
},
code_hash: '',
},
})

const client = new RESTClient(mockRestUris)
const account = await client.auth.accountInfo('inj1qqqq')

expect(account.getAccountNumber()).toBe(13)
expect(account.getSequenceNumber()).toBe(5)
})
})
})
42 changes: 42 additions & 0 deletions src/lib/restClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import {
AccAddress,
Account,
APIRequester,
AuthAPI as AuthAPI_,
BaseAccount,
IbcAPI as IbcAPI_,
RESTClientConfig,
RESTClient as RESTClient_,
Expand Down Expand Up @@ -66,6 +70,7 @@ export class RESTClient extends RESTClient_ {
super(requestState.restUris[0], config, apiRequester)

this.ibc = new IbcAPI(this.apiRequester)
this.auth = new AuthAPI(this.apiRequester)
}

private static createApiRequester(state: RESTRequestState): APIRequester {
Expand Down Expand Up @@ -184,6 +189,43 @@ export class RESTClient extends RESTClient_ {
}
}

// ethermint-style chains (e.g. Injective) wrap the base account in an
// EthAccount type that initia.js cannot parse
const ETH_ACCOUNT_TYPE_URLS: string[] = [
'/injective.types.v1beta1.EthAccount',
]

interface EthAccountData {
'@type': string
base_account?: Omit<BaseAccount.Data, '@type'>
}

class AuthAPI extends AuthAPI_ {
public async accountInfo(
address: AccAddress,
params: RESTParams = {},
headers: Record<string, string> = {}
): Promise<Account> {
const { account } = await this.c.get<{
account: Account.Data | EthAccountData
}>(`/cosmos/auth/v1beta1/accounts/${address}`, params, headers)

if (
ETH_ACCOUNT_TYPE_URLS.includes(account['@type']) &&
'base_account' in account &&
account.base_account
) {
return BaseAccount.fromData({
...account.base_account,
'@type': '/cosmos.auth.v1beta1.BaseAccount',
pub_key: undefined,
})
}

return Account.fromData(account as Account.Data)
}
}

class IbcAPI extends IbcAPI_ {
constructor(c: APIRequester) {
super(c)
Expand Down
1 change: 1 addition & 0 deletions src/workers/walletSequence.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ function makeWorker(options: {
wallet,
0n,
undefined,
undefined,
{ autoStart: false }
) as unknown as TestWalletWorker

Expand Down
Loading