From 98b81f8fa8ba18a629b3bcea1ecb1b45b3a55c67 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 13 Aug 2020 15:31:19 +0200 Subject: [PATCH] Use AuthExtension in StargateClient --- packages/proto-signing/src/any.ts | 13 ---------- packages/proto-signing/src/index.ts | 1 - packages/proto-signing/types/any.d.ts | 10 -------- packages/proto-signing/types/index.d.ts | 1 - packages/stargate/src/stargateclient.ts | 34 +++++++++---------------- 5 files changed, 12 insertions(+), 47 deletions(-) delete mode 100644 packages/proto-signing/src/any.ts delete mode 100644 packages/proto-signing/types/any.d.ts diff --git a/packages/proto-signing/src/any.ts b/packages/proto-signing/src/any.ts deleted file mode 100644 index 27a49698..00000000 --- a/packages/proto-signing/src/any.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { google } from "./generated/codecimpl"; - -/** - * Decodes a serialized [google.protobuf.Any](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/any.proto) - * and returns the components. - */ -export function decodeAny(serialized: Uint8Array): { readonly typeUrl: string; readonly value: Uint8Array } { - const envelope = google.protobuf.Any.decode(serialized); - return { - typeUrl: envelope.type_url, - value: envelope.value, - }; -} diff --git a/packages/proto-signing/src/index.ts b/packages/proto-signing/src/index.ts index ce95215d..114a1bd7 100644 --- a/packages/proto-signing/src/index.ts +++ b/packages/proto-signing/src/index.ts @@ -1,5 +1,4 @@ export { omitDefaults } from "./adr27"; -export { decodeAny } from "./any"; export { Coin } from "./msgs"; export { cosmosField } from "./decorator"; export { Registry } from "./registry"; diff --git a/packages/proto-signing/types/any.d.ts b/packages/proto-signing/types/any.d.ts deleted file mode 100644 index c76a6b15..00000000 --- a/packages/proto-signing/types/any.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -/** - * Decodes a serialized [google.protobuf.Any](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/any.proto) - * and returns the components. - */ -export declare function decodeAny( - serialized: Uint8Array, -): { - readonly typeUrl: string; - readonly value: Uint8Array; -}; diff --git a/packages/proto-signing/types/index.d.ts b/packages/proto-signing/types/index.d.ts index ce95215d..114a1bd7 100644 --- a/packages/proto-signing/types/index.d.ts +++ b/packages/proto-signing/types/index.d.ts @@ -1,5 +1,4 @@ export { omitDefaults } from "./adr27"; -export { decodeAny } from "./any"; export { Coin } from "./msgs"; export { cosmosField } from "./decorator"; export { Registry } from "./registry"; diff --git a/packages/stargate/src/stargateclient.ts b/packages/stargate/src/stargateclient.ts index 93970323..03a89048 100644 --- a/packages/stargate/src/stargateclient.ts +++ b/packages/stargate/src/stargateclient.ts @@ -11,13 +11,12 @@ import { SearchTxQuery, } from "@cosmjs/launchpad"; import { Uint53, Uint64 } from "@cosmjs/math"; -import { decodeAny } from "@cosmjs/proto-signing"; import { broadcastTxCommitSuccess, Client as TendermintClient, QueryString } from "@cosmjs/tendermint-rpc"; import { assert, assertDefined } from "@cosmjs/utils"; import Long from "long"; import { cosmos } from "./generated/codecimpl"; -import { BankExtension, QueryClient, setupBankExtension } from "./queries"; +import { AuthExtension, BankExtension, QueryClient, setupAuthExtension, setupBankExtension } from "./queries"; /** A transaction that is indexed as part of the transaction history */ export interface IndexedTx { @@ -81,14 +80,16 @@ export function assertIsBroadcastTxSuccess( } } -function uint64FromProto(input: number | Long): Uint64 { +function uint64FromProto(input: number | Long | null | undefined): Uint64 { + if (!input) return Uint64.fromNumber(0); return Uint64.fromString(input.toString()); } -function decodeBaseAccount(data: Uint8Array, prefix: string): Account { - const { address, pubKey, accountNumber, sequence } = cosmos.auth.BaseAccount.decode(data); +function accountFromProto(input: cosmos.auth.IBaseAccount, prefix: string): Account { + const { address, pubKey, accountNumber, sequence } = input; // Pubkey is still Amino-encoded in BaseAccount (https://github.com/cosmos/cosmos-sdk/issues/6886) - const pubkey = pubKey.length ? decodeAminoPubkey(pubKey) : null; + const pubkey = pubKey && pubKey.length ? decodeAminoPubkey(pubKey) : null; + assert(address); return { address: Bech32.encode(prefix, address), pubkey: pubkey, @@ -115,7 +116,7 @@ export interface PrivateStargateClient { export class StargateClient { private readonly tmClient: TendermintClient; - private readonly queryClient: QueryClient & BankExtension; + private readonly queryClient: QueryClient & AuthExtension & BankExtension; private chainId: string | undefined; public static async connect(endpoint: string): Promise { @@ -125,7 +126,7 @@ export class StargateClient { private constructor(tmClient: TendermintClient) { this.tmClient = tmClient; - this.queryClient = QueryClient.withExtensions(tmClient, setupBankExtension); + this.queryClient = QueryClient.withExtensions(tmClient, setupAuthExtension, setupBankExtension); } public async getChainId(): Promise { @@ -145,21 +146,10 @@ export class StargateClient { } public async getAccount(searchAddress: string): Promise { - const { prefix, data: binAddress } = Bech32.decode(searchAddress); - // https://github.com/cosmos/cosmos-sdk/blob/8cab43c8120fec5200c3459cbf4a92017bb6f287/x/auth/types/keys.go#L29-L32 - const accountKey = Uint8Array.from([0x01, ...binAddress]); - const responseData = await this.queryClient.queryVerified("acc", accountKey); + const { prefix } = Bech32.decode(searchAddress); - if (responseData.length === 0) return null; - - const { typeUrl, value } = decodeAny(responseData); - switch (typeUrl) { - case "/cosmos.auth.BaseAccount": { - return decodeBaseAccount(value, prefix); - } - default: - throw new Error(`Unsupported type: '${typeUrl}'`); - } + const account = await this.queryClient.auth.account(searchAddress); + return account ? accountFromProto(account, prefix) : null; } public async getSequence(address: string): Promise {