From 7d038cd21660aac54c9fc17bc38482386ac114a7 Mon Sep 17 00:00:00 2001 From: willclarktech Date: Tue, 30 Mar 2021 15:38:25 +0200 Subject: [PATCH] stargate: Rearrange auth queries --- packages/stargate/src/queries/auth.spec.ts | 18 +++++------ packages/stargate/src/queries/auth.ts | 18 +++++------ .../stargate/src/signingstargateclient.ts | 2 +- packages/stargate/src/stargateclient.ts | 32 ++++++++++--------- 4 files changed, 36 insertions(+), 34 deletions(-) diff --git a/packages/stargate/src/queries/auth.spec.ts b/packages/stargate/src/queries/auth.spec.ts index c95d3fca..c09584db 100644 --- a/packages/stargate/src/queries/auth.spec.ts +++ b/packages/stargate/src/queries/auth.spec.ts @@ -53,23 +53,24 @@ describe("AuthExtension", () => { tmClient.disconnect(); }); - it("returns null for non-existent address", async () => { + it("rejects for non-existent address", async () => { pendingWithoutSimapp(); const [client, tmClient] = await makeClientWithAuth(simapp.tendermintUrl); - const account = await client.auth.account(nonExistentAddress); - expect(account).toBeNull(); + await expectAsync(client.auth.account(nonExistentAddress)).toBeRejectedWithError( + /account cosmos1p79apjaufyphcmsn4g07cynqf0wyjuezqu84hd not found/i, + ); tmClient.disconnect(); }); }); - describe("unverified", () => { + describe("verified", () => { describe("account", () => { it("works for unused account", async () => { pendingWithoutSimapp(); const [client, tmClient] = await makeClientWithAuth(simapp.tendermintUrl); - const account = await client.auth.unverified.account(unused.address); + const account = await client.auth.verified.account(unused.address); assert(account); expect(account.typeUrl).toEqual("/cosmos.auth.v1beta1.BaseAccount"); @@ -86,7 +87,7 @@ describe("AuthExtension", () => { it("works for account with pubkey and non-zero sequence", async () => { pendingWithoutSimapp(); const [client, tmClient] = await makeClientWithAuth(simapp.tendermintUrl); - const account = await client.auth.unverified.account(validator.delegatorAddress); + const account = await client.auth.verified.account(validator.delegatorAddress); assert(account); expect(account.typeUrl).toEqual("/cosmos.auth.v1beta1.BaseAccount"); @@ -103,10 +104,9 @@ describe("AuthExtension", () => { it("returns null for non-existent address", async () => { pendingWithoutSimapp(); const [client, tmClient] = await makeClientWithAuth(simapp.tendermintUrl); + const account = await client.auth.verified.account(nonExistentAddress); - await expectAsync(client.auth.unverified.account(nonExistentAddress)).toBeRejectedWithError( - /account cosmos1p79apjaufyphcmsn4g07cynqf0wyjuezqu84hd not found/i, - ); + expect(account).toBeNull(); tmClient.disconnect(); }); diff --git a/packages/stargate/src/queries/auth.ts b/packages/stargate/src/queries/auth.ts index 07a65906..fe7259f3 100644 --- a/packages/stargate/src/queries/auth.ts +++ b/packages/stargate/src/queries/auth.ts @@ -13,7 +13,7 @@ export interface AuthExtension { * `typeUrl` and decode the `value` using its own type decoder. */ readonly account: (address: string) => Promise; - readonly unverified: { + readonly verified: { /** * Returns an account if it exists and `null` otherwise. * @@ -35,16 +35,16 @@ export function setupAuthExtension(base: QueryClient): AuthExtension { return { auth: { account: async (address: string) => { - // https://github.com/cosmos/cosmos-sdk/blob/8cab43c8120fec5200c3459cbf4a92017bb6f287/x/auth/types/keys.go#L29-L32 - const key = Uint8Array.from([0x01, ...toAccAddress(address)]); - const responseData = await base.queryVerified("acc", key); - if (responseData.length === 0) return null; - return Any.decode(responseData); + const { account } = await queryService.Account({ address: address }); + return account ?? null; }, - unverified: { + verified: { account: async (address: string) => { - const { account } = await queryService.Account({ address: address }); - return account ?? null; + // https://github.com/cosmos/cosmos-sdk/blob/8cab43c8120fec5200c3459cbf4a92017bb6f287/x/auth/types/keys.go#L29-L32 + const key = Uint8Array.from([0x01, ...toAccAddress(address)]); + const responseData = await base.queryVerified("acc", key); + if (responseData.length === 0) return null; + return Any.decode(responseData); }, }, }, diff --git a/packages/stargate/src/signingstargateclient.ts b/packages/stargate/src/signingstargateclient.ts index 0732a8b6..39608135 100644 --- a/packages/stargate/src/signingstargateclient.ts +++ b/packages/stargate/src/signingstargateclient.ts @@ -276,7 +276,7 @@ export class SigningStargateClient extends StargateClient { if (explicitSignerData) { signerData = explicitSignerData; } else { - const accountFromChain = await this.getAccountUnverified(signerAddress); + const accountFromChain = await this.getAccount(signerAddress); if (!accountFromChain) { throw new Error("Account not found"); } diff --git a/packages/stargate/src/stargateclient.ts b/packages/stargate/src/stargateclient.ts index a97c7e46..bbfb7ff3 100644 --- a/packages/stargate/src/stargateclient.ts +++ b/packages/stargate/src/stargateclient.ts @@ -160,29 +160,31 @@ export class StargateClient { return status.syncInfo.latestBlockHeight; } - // this is nice to display data to the user, but is slower public async getAccount(searchAddress: string): Promise { - const account = await this.forceGetQueryClient().auth.account(searchAddress); - return account ? accountFromAny(account) : null; + try { + const account = await this.forceGetQueryClient().auth.account(searchAddress); + return account ? accountFromAny(account) : null; + } catch (error) { + if (/rpc error: code = NotFound/i.test(error)) { + return null; + } + throw error; + } } - // if we just need to get the sequence for signing a transaction, let's make this faster - // (no need to wait a block before submitting) - public async getAccountUnverified(searchAddress: string): Promise { - const account = await this.forceGetQueryClient().auth.unverified.account(searchAddress); + public async getAccountVerified(searchAddress: string): Promise { + const account = await this.forceGetQueryClient().auth.verified.account(searchAddress); return account ? accountFromAny(account) : null; } public async getSequence(address: string): Promise { const account = await this.getAccount(address); - if (account) { - return { - accountNumber: account.accountNumber, - sequence: account.sequence, - }; - } else { - return null; - } + return account + ? { + accountNumber: account.accountNumber, + sequence: account.sequence, + } + : null; } public async getBlock(height?: number): Promise {