-
Notifications
You must be signed in to change notification settings - Fork 606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Authz #2206
Authz #2206
Changes from 6 commits
6672f36
de7ef7a
b694cf8
415cb39
f73dd97
00ea208
89a04f9
791b5b5
a00b4c5
0f33897
6506326
f433f3b
2760edf
f086c52
2a47d22
eec4579
ccada40
a221d02
dd2d1f6
17391d6
11344a6
1ad2e65
f9f5fbd
f3cebe9
f047f32
187b95f
73de3b6
5e13222
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,12 +1,17 @@ | ||||||
package types | ||||||
|
||||||
import ( | ||||||
"fmt" | ||||||
"testing" | ||||||
"time" | ||||||
|
||||||
"github.com/stretchr/testify/require" | ||||||
|
||||||
cdctypes "github.com/cosmos/cosmos-sdk/codec/types" | ||||||
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" | ||||||
sdk "github.com/cosmos/cosmos-sdk/types" | ||||||
"github.com/cosmos/cosmos-sdk/x/auth/legacy/legacytx" | ||||||
"github.com/cosmos/cosmos-sdk/x/authz" | ||||||
|
||||||
appParams "github.com/osmosis-labs/osmosis/v10/app/params" | ||||||
) | ||||||
|
@@ -867,3 +872,148 @@ func TestMsgExitSwapShareAmountIn(t *testing.T) { | |||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
// Test authz | ||||||
func TestAuthzMsg(t *testing.T) { | ||||||
appParams.SetAddressPrefixes() | ||||||
vuong177 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
pk1 := ed25519.GenPrivKey().PubKey() | ||||||
addr1 := sdk.AccAddress(pk1.Address()).String() | ||||||
coin := sdk.NewCoin("stake", sdk.NewInt(1)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
someDate := time.Date(1, 1, 1, 1, 1, 1, 1, time.UTC) | ||||||
|
||||||
testCases := []struct { | ||||||
p0mvn marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
expectedGrantSignByteMsg string | ||||||
expectedRevokeSignByteMsg string | ||||||
expectedExecStrSignByteMsg string | ||||||
gammMsg sdk.Msg | ||||||
}{ | ||||||
{ | ||||||
expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.gamm.v1beta1.MsgExitSwapShareAmountIn"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way to have this represented in a struct format and the unmarshal to json string if needed? It's hard to follow this in a string format. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think it's a bit complex. Why we don't use pretty json in expectedGrantSignByteMsg. Like that
instead of use one line string There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good, thanks for suggesting the alternative |
||||||
expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.gamm.v1beta1.MsgExitSwapShareAmountIn"}}],"sequence":"1","timeout_height":"1"}`, | ||||||
expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/gamm/exit-swap-share-amount-in","value":{"pool_id":"1","sender":"%s","share_in_amount":"100","token_out_denom":"test","token_out_min_amount":"100"}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), | ||||||
gammMsg: &MsgExitSwapShareAmountIn{ | ||||||
Sender: addr1, | ||||||
PoolId: 1, | ||||||
TokenOutDenom: "test", | ||||||
ShareInAmount: sdk.NewInt(100), | ||||||
TokenOutMinAmount: sdk.NewInt(100), | ||||||
}, | ||||||
}, | ||||||
{ | ||||||
expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.gamm.v1beta1.MsgExitSwapExternAmountOut"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, | ||||||
expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.gamm.v1beta1.MsgExitSwapExternAmountOut"}}],"sequence":"1","timeout_height":"1"}`, | ||||||
expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/gamm/exit-swap-extern-amount-out","value":{"pool_id":"1","sender":"%s","share_in_max_amount":"1","token_out":{"amount":"1","denom":"stake"}}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), | ||||||
gammMsg: &MsgExitSwapExternAmountOut{ | ||||||
Sender: addr1, | ||||||
PoolId: 1, | ||||||
TokenOut: coin, | ||||||
ShareInMaxAmount: sdk.NewInt(1), | ||||||
}, | ||||||
}, | ||||||
{ | ||||||
expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.gamm.v1beta1.MsgExitPool"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, | ||||||
expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.gamm.v1beta1.MsgExitPool"}}],"sequence":"1","timeout_height":"1"}`, | ||||||
expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/gamm/exit-pool","value":{"pool_id":"1","sender":"%s","share_in_amount":"100","token_out_mins":[{"amount":"1","denom":"stake"}]}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), | ||||||
gammMsg: &MsgExitPool{ | ||||||
Sender: addr1, | ||||||
PoolId: 1, | ||||||
ShareInAmount: sdk.NewInt(100), | ||||||
TokenOutMins: sdk.NewCoins(coin), | ||||||
}, | ||||||
}, | ||||||
{ | ||||||
expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.gamm.v1beta1.MsgJoinPool"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, | ||||||
expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.gamm.v1beta1.MsgJoinPool"}}],"sequence":"1","timeout_height":"1"}`, | ||||||
expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/gamm/join-pool","value":{"pool_id":"1","sender":"%s","share_out_amount":"1","token_in_maxs":[{"amount":"1","denom":"stake"}]}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), | ||||||
gammMsg: &MsgJoinPool{ | ||||||
Sender: addr1, | ||||||
PoolId: 1, | ||||||
ShareOutAmount: sdk.NewInt(1), | ||||||
TokenInMaxs: sdk.NewCoins(coin), | ||||||
}, | ||||||
}, | ||||||
{ | ||||||
expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.gamm.v1beta1.MsgJoinSwapExternAmountIn"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, | ||||||
expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.gamm.v1beta1.MsgJoinSwapExternAmountIn"}}],"sequence":"1","timeout_height":"1"}`, | ||||||
expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/gamm/join-swap-extern-amount-in","value":{"pool_id":"1","sender":"%s","share_out_min_amount":"1","token_in":{"amount":"1","denom":"stake"}}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), | ||||||
gammMsg: &MsgJoinSwapExternAmountIn{ | ||||||
Sender: addr1, | ||||||
PoolId: 1, | ||||||
TokenIn: coin, | ||||||
ShareOutMinAmount: sdk.NewInt(1), | ||||||
}, | ||||||
}, | ||||||
{ | ||||||
expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.gamm.v1beta1.MsgJoinSwapShareAmountOut"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, | ||||||
expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.gamm.v1beta1.MsgJoinSwapShareAmountOut"}}],"sequence":"1","timeout_height":"1"}`, | ||||||
expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/gamm/join-swap-share-amount-out","value":{"pool_id":"1","sender":"%s","share_out_amount":"1","token_in_denom":"denom","token_in_max_amount":"1"}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), | ||||||
gammMsg: &MsgJoinSwapShareAmountOut{ | ||||||
Sender: addr1, | ||||||
PoolId: 1, | ||||||
TokenInDenom: "denom", | ||||||
ShareOutAmount: sdk.NewInt(1), | ||||||
TokenInMaxAmount: sdk.NewInt(1), | ||||||
}, | ||||||
}, | ||||||
{ | ||||||
expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.gamm.v1beta1.MsgSwapExactAmountIn"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, | ||||||
expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.gamm.v1beta1.MsgSwapExactAmountIn"}}],"sequence":"1","timeout_height":"1"}`, | ||||||
expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/gamm/swap-exact-amount-in","value":{"routes":[{"token_out_denom":"test"},{"pool_id":"1","token_out_denom":"test2"}],"sender":"%s","token_in":{"amount":"1","denom":"stake"},"token_out_min_amount":"1"}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), | ||||||
gammMsg: &MsgSwapExactAmountIn{ | ||||||
Sender: addr1, | ||||||
Routes: []SwapAmountInRoute{{ | ||||||
PoolId: 0, | ||||||
TokenOutDenom: "test", | ||||||
}, { | ||||||
PoolId: 1, | ||||||
TokenOutDenom: "test2", | ||||||
}}, | ||||||
TokenIn: coin, | ||||||
TokenOutMinAmount: sdk.NewInt(1), | ||||||
}, | ||||||
}, | ||||||
{ | ||||||
expectedGrantSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/osmosis.gamm.v1beta1.MsgSwapExactAmountOut"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, | ||||||
expectedRevokeSignByteMsg: `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/osmosis.gamm.v1beta1.MsgSwapExactAmountOut"}}],"sequence":"1","timeout_height":"1"}`, | ||||||
expectedExecStrSignByteMsg: fmt.Sprintf(`{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"osmosis/gamm/swap-exact-amount-out","value":{"routes":[{"token_in_denom":"test"},{"pool_id":"1","token_in_denom":"test2"}],"sender":"%s","token_in_max_amount":"1","token_out":{"amount":"1","denom":"stake"}}}]}}],"sequence":"1","timeout_height":"1"}`, addr1), | ||||||
gammMsg: &MsgSwapExactAmountOut{ | ||||||
Sender: addr1, | ||||||
Routes: []SwapAmountOutRoute{{ | ||||||
PoolId: 0, | ||||||
TokenInDenom: "test", | ||||||
}, { | ||||||
PoolId: 1, | ||||||
TokenInDenom: "test2", | ||||||
}}, | ||||||
TokenOut: coin, | ||||||
TokenInMaxAmount: sdk.NewInt(1), | ||||||
}, | ||||||
}, | ||||||
} | ||||||
for _, tc := range testCases { | ||||||
// Authz: Grant Msg | ||||||
typeURL := sdk.MsgTypeURL(tc.gammMsg) | ||||||
grant, err := authz.NewGrant(someDate, authz.NewGenericAuthorization(typeURL), someDate.Add(time.Hour)) | ||||||
require.NoError(t, err) | ||||||
msgGrant := &authz.MsgGrant{Granter: "cosmos1abc", Grantee: "cosmos1def", Grant: grant} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we extract |
||||||
require.Equal(t, | ||||||
tc.expectedGrantSignByteMsg, | ||||||
string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgGrant}, "memo")), | ||||||
) | ||||||
|
||||||
// Authz: Revoke Msg | ||||||
msgRevoke := &authz.MsgRevoke{Granter: "cosmos1abc", Grantee: "cosmos1def", MsgTypeUrl: typeURL} | ||||||
require.Equal(t, | ||||||
tc.expectedRevokeSignByteMsg, | ||||||
string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgRevoke}, "memo")), | ||||||
) | ||||||
|
||||||
// Authz: Exec Msg | ||||||
msgAny, _ := cdctypes.NewAnyWithValue(tc.gammMsg) | ||||||
msgExec := &authz.MsgExec{Grantee: "cosmos1def", Msgs: []*cdctypes.Any{msgAny}} | ||||||
require.Equal(t, | ||||||
tc.expectedExecStrSignByteMsg, | ||||||
string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msgExec}, "memo")), | ||||||
) | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please elaborate elaborate here that this test is meant more so to test the authz than the gamm message