Skip to content

Commit

Permalink
feat: add x/authz message parser (#97)
Browse files Browse the repository at this point in the history
<!-- < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < ☺
v                               ✰  Thanks for creating a PR! ✰
v    Before smashing the submit button please review the checkboxes.
v If a checkbox is n/a - please still include it but + a little note why
☺ > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >  -->

<!-- Small description -->
Closes
[BDU-1030](https://forbole.atlassian.net/jira/software/c/projects/BDU/issues/BDU-1030)

- [x] Targeted PR against correct branch.
- [x] Linked to Github issue with discussion and accepted design OR link
to spec that describes this work.
- [ ] Wrote unit tests.
- [x] Re-reviewed `Files changed` in the Github PR explorer.

[BDU-1030]:
https://forbole.atlassian.net/browse/BDU-1030?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
  • Loading branch information
MonikaCat committed Dec 22, 2023
1 parent 6b4c9f3 commit fee382f
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
uses: actions/checkout@v3

- name: Compute diff 📜
uses: technote-space/[email protected].0
uses: technote-space/[email protected].2
with:
SUFFIX_FILTER: |
.go
Expand All @@ -27,7 +27,7 @@ jobs:
- name: Setup Go 🧰
if: "env.GIT_DIFF != ''"
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: 1.19

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
uses: actions/checkout@v3

- name: Setup Go 🧰
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: 1.19

Expand Down
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
## Unreleased
### Changes
- ([\#97](https://github.com/forbole/juno/pull/97)) Add `x/authz` message parser

## v5.1.0
### Changes
- Bumped Go version to `1.20`
- Fixed a bug that caused messages type urls to be empty in the database
- Implemented a better default `EncodingConfigBuilder`
- Added the ability to set `DatabaseConfig` field values using setter methods
- ([\#96](https://github.com/forbole/juno/pull/96)) Export `Codec` and `LegacyAmino` from the `Database` implementation

## v5.0.0
### Changes
- Updated Cosmos SDK to `v0.47.2`

### Database
- ([\#96](https://github.com/forbole/juno/pull/96)) Exported Codec and LegacyAmino codec inside Database wrapper

## v4.2.0
### Changes
- ([\#93](https://github.com/forbole/juno/pull/93)) Decode IBC transfer data to JSON for `/ibc.core.channel.v1.MsgRecvPacket` message
Expand Down
89 changes: 89 additions & 0 deletions modules/messages/account_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package messages

import (
"fmt"
"strings"

"github.com/gogo/protobuf/proto"

Expand All @@ -14,6 +15,7 @@ import (

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
authztypes "github.com/cosmos/cosmos-sdk/x/authz"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
Expand Down Expand Up @@ -55,6 +57,7 @@ var CosmosMessageAddressesParser = JoinMessageParsers(
IBCTransferMessagesParser,
SlashingMessagesParser,
StakingMessagesParser,
AuthzMessageParser,
DefaultMessagesParser,
)

Expand Down Expand Up @@ -230,3 +233,89 @@ func StakingMessagesParser(_ codec.Codec, cosmosMsg sdk.Msg) ([]string, error) {

return nil, MessageNotSupported(cosmosMsg)
}

// AuthzMessageParser returns the list of all the accounts involved in the given
// message if it's related to the x/authz module
func AuthzMessageParser(c codec.Codec, cosmosMsg sdk.Msg) ([]string, error) {
switch msg := cosmosMsg.(type) {
case *authztypes.MsgGrant:
return []string{msg.Grantee, msg.Granter}, nil
case *authztypes.MsgRevoke:
return []string{msg.Grantee, msg.Granter}, nil
case *authztypes.MsgExec:
for _, index := range msg.Msgs {
var executedMsg sdk.Msg
if err := c.UnpackAny(index, &executedMsg); err != nil {
return nil, fmt.Errorf("error while unpacking message from authz: %s", err)
}

switch {
case strings.Contains(index.TypeUrl, "bank"):
addresses, err := BankMessagesParser(c, executedMsg)
if err != nil {
return nil, MessageNotSupported(executedMsg)
}
addresses = append(addresses, msg.Grantee)
return addresses, nil
case strings.Contains(index.TypeUrl, "crisis"):
addresses, err := CrisisMessagesParser(c, executedMsg)
if err != nil {
return nil, MessageNotSupported(executedMsg)
}
addresses = append(addresses, msg.Grantee)
return addresses, nil
case strings.Contains(index.TypeUrl, "distribution"):
addresses, err := DistributionMessagesParser(c, executedMsg)
if err != nil {
return nil, MessageNotSupported(executedMsg)
}
addresses = append(addresses, msg.Grantee)
return addresses, nil
case strings.Contains(index.TypeUrl, "evidence"):
addresses, err := EvidenceMessagesParser(c, executedMsg)
if err != nil {
return nil, MessageNotSupported(executedMsg)
}
addresses = append(addresses, msg.Grantee)
return addresses, nil
case strings.Contains(index.TypeUrl, "gov.v1beta1"):
addresses, err := GovMessagesParser(c, executedMsg)
if err != nil {
return nil, MessageNotSupported(executedMsg)
}
addresses = append(addresses, msg.Grantee)
return addresses, nil
case strings.Contains(index.TypeUrl, "ibc"):
addresses, err := IBCTransferMessagesParser(c, executedMsg)
if err != nil {
return nil, MessageNotSupported(executedMsg)
}
addresses = append(addresses, msg.Grantee)
return addresses, nil
case strings.Contains(index.TypeUrl, "slashing"):
addresses, err := SlashingMessagesParser(c, executedMsg)
if err != nil {
return nil, MessageNotSupported(executedMsg)
}
addresses = append(addresses, msg.Grantee)
return addresses, nil
case strings.Contains(index.TypeUrl, "staking"):
addresses, err := StakingMessagesParser(c, executedMsg)
if err != nil {
return nil, MessageNotSupported(executedMsg)
}
addresses = append(addresses, msg.Grantee)
return addresses, nil
default:
addresses, err := DefaultMessagesParser(c, executedMsg)
if err != nil {
return nil, MessageNotSupported(executedMsg)
}
addresses = append(addresses, msg.Grantee)
return addresses, nil
}
}
}

return nil, MessageNotSupported(cosmosMsg)
}

0 comments on commit fee382f

Please sign in to comment.