Skip to content

Commit

Permalink
fix authz to SDK v50
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed Dec 1, 2023
1 parent 5342317 commit 069779d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 25 deletions.
57 changes: 42 additions & 15 deletions chain/cosmos/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"strings"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
Expand All @@ -17,14 +16,42 @@ import (
// grant Grant authorization to an address
// revoke revoke authorization

const ()

// AuthzGrant grants a message as a permission to an account.
func AuthzGrant(ctx context.Context, chain *CosmosChain, granter ibc.Wallet, grantee string, msgType string) (*sdk.TxResponse, error) {
if !strings.HasPrefix(msgType, "/") {
msgType = "/" + msgType
func AuthzGrant(ctx context.Context, chain *CosmosChain, granter ibc.Wallet, grantee, authType string, extraFlags ...string) (*sdk.TxResponse, error) {

allowed := "send|generic|delegate|unbond|redelegate"
if !strings.Contains(allowed, authType) {
return nil, fmt.Errorf("invalid auth type: %s allowed: %s", authType, allowed)
}

cmd := []string{"authz", "grant", grantee, authType}

// when using the generic type, you must specify a --msg-type flag
if authType == "generic" {
msgTypeIndex := -1
for i, flag := range extraFlags {
if flag == "--msg-type" {
msgTypeIndex = i
break
}
}

if msgTypeIndex == -1 {
return nil, fmt.Errorf("missing --msg-type flag when granting generic authz")
}

msgType := extraFlags[msgTypeIndex+1]
if !strings.HasPrefix(msgType, "/") {
extraFlags[msgTypeIndex+1] = "/" + msgType
}
}

cmd = append(cmd, extraFlags...)

txHash, err := chain.GetNode().ExecTx(ctx, granter.KeyName(),
"authz", "grant", grantee, "generic", "--msg-type", msgType, "--output=json",
append(cmd, "--output", "json")...,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -71,14 +98,14 @@ func AuthzRevoke(ctx context.Context, chain *CosmosChain, granter ibc.Wallet, gr
type QueryAuthzGrantsResponse struct {
Grants []struct {
Authorization struct {
Type string `json:"@type"`
Msg string `json:"msg"`
Type string `json:"type"`
Value struct {
Msg string `json:"msg"`
} `json:"value"`
} `json:"authorization"`
Expiration any `json:"expiration"`
} `json:"grants"`
Pagination struct {
NextKey any `json:"next_key"`
Total string `json:"total"`
Total string `json:"total"`
} `json:"pagination"`
}

Expand All @@ -88,14 +115,14 @@ type QueryAuthzGrantsByResponse struct {
Granter string `json:"granter"`
Grantee string `json:"grantee"`
Authorization struct {
Type string `json:"@type"`
Msg string `json:"msg"`
Type string `json:"type"`
Value struct {
Msg string `json:"msg"`
} `json:"value"`
} `json:"authorization"`
Expiration time.Time `json:"expiration"`
} `json:"grants"`
Pagination struct {
NextKey any `json:"next_key"`
Total string `json:"total"`
Total string `json:"total"`
} `json:"pagination"`
}

Expand Down
5 changes: 2 additions & 3 deletions chain/cosmos/chain_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,10 +515,9 @@ func (tn *ChainNode) TxHashToResponse(ctx context.Context, txHash string) (*sdk.
}

i := &sdk.TxResponse{}
if err := json.Unmarshal(stdout, &i); err != nil {
return i, err
}

// ignore the error since some types do not unmarshal (ex: height of int64 vs string)
json.Unmarshal(stdout, &i)

Check failure on line 520 in chain/cosmos/chain_node.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `json.Unmarshal` is not checked (errcheck)
return i, nil
}

Expand Down
2 changes: 0 additions & 2 deletions chain/cosmos/cosmos_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,8 +580,6 @@ func (c *CosmosChain) ExportState(ctx context.Context, height int64) (string, er
return c.getFullNode().ExportState(ctx, height)
}



func (c *CosmosChain) GetTransaction(txhash string) (*types.TxResponse, error) {
fn := c.getFullNode()
return fn.GetTransaction(fn.CliContext(), txhash)
Expand Down
13 changes: 8 additions & 5 deletions examples/cosmos/chain_core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
)

func TestCoreSDKCommands(t *testing.T) {
// TODO: simplify this test to the basics and convert to SDK v50
if testing.Short() {
t.Skip("skipping in short mode")
}
Expand Down Expand Up @@ -80,13 +79,15 @@ func testAuthz(ctx context.Context, t *testing.T, chain *cosmos.CosmosChain, use
grantee := users[1].FormattedAddress()

// Grant BankSend Authz
txRes, _ := cosmos.AuthzGrant(ctx, chain, users[0], grantee, "/cosmos.bank.v1beta1.MsgSend")
// TODO: test other types as well (send is giving a NPE)
txRes, _ := cosmos.AuthzGrant(ctx, chain, users[0], grantee, "generic", "--msg-type", "/cosmos.bank.v1beta1.MsgSend")
require.EqualValues(t, 0, txRes.Code)

grants, err := cosmos.AuthzQueryGrants(ctx, chain, granter, grantee, "")
require.NoError(t, err)
require.Len(t, grants.Grants, 1)
require.EqualValues(t, grants.Grants[0].Authorization.Msg, "/cosmos.bank.v1beta1.MsgSend")
require.EqualValues(t, grants.Grants[0].Authorization.Type, "cosmos-sdk/GenericAuthorization")
require.EqualValues(t, grants.Grants[0].Authorization.Value.Msg, "/cosmos.bank.v1beta1.MsgSend")

byGrantee, err := cosmos.AuthzQueryGrantsByGrantee(ctx, chain, grantee, "")
require.NoError(t, err)
Expand All @@ -100,6 +101,8 @@ func testAuthz(ctx context.Context, t *testing.T, chain *cosmos.CosmosChain, use
require.EqualValues(t, byGranter.Grants[0].Granter, granter)
require.EqualValues(t, byGranter.Grants[0].Grantee, grantee)

fmt.Printf("grants: %+v %+v %+v\n", grants, byGrantee, byGranter)

// Perform BankSend tx via authz

// before balance
Expand All @@ -110,6 +113,7 @@ func testAuthz(ctx context.Context, t *testing.T, chain *cosmos.CosmosChain, use
sendAmt := 1234

nestedCmd := []string{
chain.Config().Bin,
"tx", "bank", "send", granter, grantee, fmt.Sprintf("%d%s", sendAmt, chain.Config().Denom),
"--from", granter, "--generate-only",
"--chain-id", chain.GetNode().Chain.Config().ChainID,
Expand All @@ -122,14 +126,13 @@ func testAuthz(ctx context.Context, t *testing.T, chain *cosmos.CosmosChain, use

resp, err := cosmos.AuthzExec(ctx, chain, users[1], nestedCmd)
require.NoError(t, err)
fmt.Printf("resp: %+v\n", resp)
require.EqualValues(t, 0, resp.Code)

// after balance
balanceAfter, err := chain.GetBalance(ctx, granter, chain.Config().Denom)
require.NoError(t, err)

fmt.Printf("balanceAfter: %+v\n", balanceAfter)

require.EqualValues(t, balanceBefore.SubRaw(int64(sendAmt)), balanceAfter)
}

Expand Down

0 comments on commit 069779d

Please sign in to comment.