Skip to content

Commit

Permalink
Merge pull request #1308 from mvid/authz-queries
Browse files Browse the repository at this point in the history
authz queries to meet the expanded types provided in the sdk
  • Loading branch information
webmaster128 authored Jan 3, 2023
2 parents 22ef61f + a4a6bf0 commit 33271bc
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 58 additions & 1 deletion packages/stargate/src/modules/authz/queries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
faucet,
makeRandomAddress,
pendingWithoutSimapp44Or46,
pendingWithoutSimapp46,
simapp,
simapp44Enabled,
simapp46Enabled,
Expand Down Expand Up @@ -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();
Expand Down
31 changes: 27 additions & 4 deletions packages/stargate/src/modules/authz/queries.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -10,6 +15,14 @@ export interface AuthzExtension {
msgTypeUrl: string,
paginationKey?: Uint8Array,
) => Promise<QueryGrantsResponse>;
readonly granteeGrants: (
grantee: string,
paginationKey?: Uint8Array,
) => Promise<QueryGranteeGrantsResponse>;
readonly granterGrants: (
granter: string,
paginationKey?: Uint8Array,
) => Promise<QueryGranterGrantsResponse>;
};
}

Expand All @@ -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),
});
},
},
};
Expand Down

0 comments on commit 33271bc

Please sign in to comment.