diff --git a/packages/stargate/src/queries/bank.ts b/packages/stargate/src/queries/bank.ts index 2207f8e6..4266f288 100644 --- a/packages/stargate/src/queries/bank.ts +++ b/packages/stargate/src/queries/bank.ts @@ -1,16 +1,8 @@ -import { Bech32, toAscii } from "@cosmjs/encoding"; +import { toAscii } from "@cosmjs/encoding"; import { cosmos } from "../generated/codecimpl"; import { QueryClient } from "../queryclient"; - -/** - * Use this to convert a protobuf.js class to the interface (e.g. Coin to ICoin) - * in a ways that makes Jasmine's toEqual happy. - */ -// eslint-disable-next-line @typescript-eslint/ban-types -function toObject(thing: I): Omit { - return { ...thing }; -} +import { toAccAddress, toObject } from "./utils"; export interface BankExtension { readonly bank: { @@ -42,14 +34,13 @@ export function setupBankExtension(base: QueryClient): BankExtension { // it seem like prefix stores just do a dumb concat with the keys (no tricks to avoid overlap) // https://github.com/cosmos/cosmos-sdk/blob/2879c0702c87dc9dd828a8c42b9224dc054e28ad/store/prefix/store.go#L61-L64 // https://github.com/cosmos/cosmos-sdk/blob/2879c0702c87dc9dd828a8c42b9224dc054e28ad/store/prefix/store.go#L37-L43 - const binAddress = Bech32.decode(address).data; - const bankKey = Uint8Array.from([...toAscii("balances"), ...binAddress, ...toAscii(denom)]); - const responseData = await base.queryVerified("bank", bankKey); + const key = Uint8Array.from([...toAscii("balances"), ...toAccAddress(address), ...toAscii(denom)]); + const responseData = await base.queryVerified("bank", key); return responseData.length ? toObject(cosmos.Coin.decode(responseData)) : null; }, unverified: { allBalances: async (address: string) => { - const { balances } = await queryService.allBalances({ address: Bech32.decode(address).data }); + const { balances } = await queryService.allBalances({ address: toAccAddress(address) }); return balances.map(toObject); }, }, diff --git a/packages/stargate/src/queries/index.ts b/packages/stargate/src/queries/index.ts new file mode 100644 index 00000000..07dc15ed --- /dev/null +++ b/packages/stargate/src/queries/index.ts @@ -0,0 +1 @@ +export { BankExtension, setupBankExtension } from "./bank"; diff --git a/packages/stargate/src/queries/utils.ts b/packages/stargate/src/queries/utils.ts new file mode 100644 index 00000000..0466765e --- /dev/null +++ b/packages/stargate/src/queries/utils.ts @@ -0,0 +1,19 @@ +import { Bech32 } from "@cosmjs/encoding"; + +/** + * Takes a bech32 encoded address and returns the data part. The prefix is ignored and discarded. + * This is called AccAddress in Cosmos SDK, which is basically an alias for raw binary data. + * The result is typically 20 bytes long but not restricted to that. + */ +export function toAccAddress(address: string): Uint8Array { + return Bech32.decode(address).data; +} + +/** + * Use this to convert a protobuf.js class to the interface (e.g. Coin to ICoin) + * in a ways that makes Jasmine's toEqual happy. + */ +// eslint-disable-next-line @typescript-eslint/ban-types +export function toObject(thing: I): Omit { + return { ...thing }; +} diff --git a/packages/stargate/src/stargateclient.ts b/packages/stargate/src/stargateclient.ts index 64e34451..8d8d147b 100644 --- a/packages/stargate/src/stargateclient.ts +++ b/packages/stargate/src/stargateclient.ts @@ -17,7 +17,7 @@ import { assert, assertDefined } from "@cosmjs/utils"; import Long from "long"; import { cosmos } from "./generated/codecimpl"; -import { BankExtension, setupBankExtension } from "./queries/bank"; +import { BankExtension, setupBankExtension } from "./queries"; import { QueryClient } from "./queryclient"; /** A transaction that is indexed as part of the transaction history */ diff --git a/packages/stargate/types/queries/index.d.ts b/packages/stargate/types/queries/index.d.ts new file mode 100644 index 00000000..07dc15ed --- /dev/null +++ b/packages/stargate/types/queries/index.d.ts @@ -0,0 +1 @@ +export { BankExtension, setupBankExtension } from "./bank"; diff --git a/packages/stargate/types/queries/utils.d.ts b/packages/stargate/types/queries/utils.d.ts new file mode 100644 index 00000000..3d99f5c2 --- /dev/null +++ b/packages/stargate/types/queries/utils.d.ts @@ -0,0 +1,11 @@ +/** + * Takes a bech32 encoded address and returns the data part. The prefix is ignored and discarded. + * This is called AccAddress in Cosmos SDK, which is basically an alias for raw binary data. + * The result is typically 20 bytes long but not restricted to that. + */ +export declare function toAccAddress(address: string): Uint8Array; +/** + * Use this to convert a protobuf.js class to the interface (e.g. Coin to ICoin) + * in a ways that makes Jasmine's toEqual happy. + */ +export declare function toObject(thing: I): Omit;