diff --git a/CHANGELOG.md b/CHANGELOG.md index e037bb59..7bdb0182 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,12 @@ and this project adheres to [#1291]: https://github.com/cosmos/cosmjs/issues/1291 [#1329]: https://github.com/cosmos/cosmjs/pull/1329 +### Added +- @cosmjs/stargate: Add `granteeGrants` and `granterGrants` queries to + `AuthzExtension` ([#1308]). + +[#1308]: https://github.com/cosmos/cosmjs/pull/1308 + ## [0.29.5] - 2022-12-07 ### Fixed diff --git a/packages/stargate/src/modules/authz/queries.spec.ts b/packages/stargate/src/modules/authz/queries.spec.ts index db516ef1..22c73ac3 100644 --- a/packages/stargate/src/modules/authz/queries.spec.ts +++ b/packages/stargate/src/modules/authz/queries.spec.ts @@ -12,6 +12,7 @@ import { faucet, makeRandomAddress, pendingWithoutSimapp44Or46, + pendingWithoutSimapp46, simapp, simapp44Enabled, simapp46Enabled, @@ -95,7 +96,63 @@ describe("AuthzExtension", () => { // Decode the message const msgDecoded = GenericAuthorization.decode(grant.authorization.value).msg; - // Check if its the same one then we granted + // Check if it's the same one then we granted + expect(msgDecoded).toEqual(grantedMsg); + + tmClient.disconnect(); + }); + }); + + describe("granter grants", () => { + it("works", async () => { + pendingWithoutSimapp46(); + const [client, tmClient] = await makeClientWithAuthz(simapp.tendermintUrl); + const response = await client.authz.granterGrants(granter1Address); + expect(response.grants.length).toEqual(1); + const grant = response.grants[0]; + + // Needs to respond with a grant + assertDefined(grant.authorization); + + // Needs to have the correct granter and grantee + expect(grant.granter).toEqual(granter1Address); + expect(grant.grantee).toEqual(grantee1Address); + + // Needs to be GenericAuthorization to decode it below + expect(grant.authorization.typeUrl).toEqual("/cosmos.authz.v1beta1.GenericAuthorization"); + + // Decode the message + const msgDecoded = GenericAuthorization.decode(grant.authorization.value).msg; + + // Check if it's the same one then we granted + expect(msgDecoded).toEqual(grantedMsg); + + tmClient.disconnect(); + }); + }); + + describe("grantee grants", () => { + it("works", async () => { + pendingWithoutSimapp46(); + const [client, tmClient] = await makeClientWithAuthz(simapp.tendermintUrl); + const response = await client.authz.granteeGrants(grantee1Address); + expect(response.grants.length).toEqual(1); + const grant = response.grants[0]; + + // Needs to respond with a grant + assertDefined(grant.authorization); + + // Needs to have the correct granter and grantee + expect(grant.granter).toEqual(granter1Address); + expect(grant.grantee).toEqual(grantee1Address); + + // Needs to be GenericAuthorization to decode it below + expect(grant.authorization.typeUrl).toEqual("/cosmos.authz.v1beta1.GenericAuthorization"); + + // Decode the message + const msgDecoded = GenericAuthorization.decode(grant.authorization.value).msg; + + // Check if it's the same one then we granted expect(msgDecoded).toEqual(grantedMsg); tmClient.disconnect(); diff --git a/packages/stargate/src/modules/authz/queries.ts b/packages/stargate/src/modules/authz/queries.ts index 9c548e6f..0dd2b53b 100644 --- a/packages/stargate/src/modules/authz/queries.ts +++ b/packages/stargate/src/modules/authz/queries.ts @@ -1,4 +1,9 @@ -import { QueryClientImpl, QueryGrantsResponse } from "cosmjs-types/cosmos/authz/v1beta1/query"; +import { + QueryClientImpl, + QueryGranteeGrantsResponse, + QueryGranterGrantsResponse, + QueryGrantsResponse, +} from "cosmjs-types/cosmos/authz/v1beta1/query"; import { createPagination, createProtobufRpcClient, QueryClient } from "../../queryclient"; @@ -10,6 +15,14 @@ export interface AuthzExtension { msgTypeUrl: string, paginationKey?: Uint8Array, ) => Promise; + readonly granteeGrants: ( + grantee: string, + paginationKey?: Uint8Array, + ) => Promise; + readonly granterGrants: ( + granter: string, + paginationKey?: Uint8Array, + ) => Promise; }; } @@ -22,14 +35,24 @@ export function setupAuthzExtension(base: QueryClient): AuthzExtension { return { authz: { grants: async (granter: string, grantee: string, msgTypeUrl: string, paginationKey?: Uint8Array) => { - const response = await queryService.Grants({ + return await queryService.Grants({ granter: granter, grantee: grantee, msgTypeUrl: msgTypeUrl, pagination: createPagination(paginationKey), }); - - return response; + }, + granteeGrants: async (grantee: string, paginationKey?: Uint8Array) => { + return await queryService.GranteeGrants({ + grantee: grantee, + pagination: createPagination(paginationKey), + }); + }, + granterGrants: async (granter: string, paginationKey?: Uint8Array) => { + return await queryService.GranterGrants({ + granter: granter, + pagination: createPagination(paginationKey), + }); }, }, };