From a1d24a064cf87603c0398e8c98ed4f82cdd7ecb2 Mon Sep 17 00:00:00 2001 From: Emil Georgiev Date: Tue, 2 Apr 2024 22:20:35 +0300 Subject: [PATCH] add comments --- x/authz/keeper/keys.go | 9 ++++++--- x/authz/keeper/keys_test.go | 3 +++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/x/authz/keeper/keys.go b/x/authz/keeper/keys.go index f222c03dc0f9..0c89af07c96d 100644 --- a/x/authz/keeper/keys.go +++ b/x/authz/keeper/keys.go @@ -106,23 +106,26 @@ func firstAddressFromGrantStoreKey(key []byte) sdk.AccAddress { return sdk.AccAddress(key[1 : 1+addrLen]) } +// grantKeyToString converts a byte slice representing a grant key into a human-readable UTF-8 encoded string. +// The expected format of the byte slice is as follows: +// 0x01 func grantKeyToString(skey []byte) string { // get grant key prefix prefix := skey[bytePositionOfGrantKeyPrefix] - // get granter address len and granter address + // get granter address len and granter address, found at a specified position in the byte slice granterAddressLen := int(skey[bytePositionOfGranterAddressLen]) startByteIndex := omitTwoBytes endByteIndex := startByteIndex + granterAddressLen granterAddressBytes := skey[startByteIndex:endByteIndex] - // get grantee address len and grantee address + // get grantee address len and grantee address, found at a specified position in the byte slice granteeAddressLen := int(skey[endByteIndex]) startByteIndex = endByteIndex + 1 endByteIndex = startByteIndex + granteeAddressLen granteeAddressBytes := skey[startByteIndex:endByteIndex] - // get message type + // get message type, start from the specific byte to the end startByteIndex = endByteIndex msgTypeBytes := skey[startByteIndex:] diff --git a/x/authz/keeper/keys_test.go b/x/authz/keeper/keys_test.go index 3b1bb3d829ee..51f8d7d06a20 100644 --- a/x/authz/keeper/keys_test.go +++ b/x/authz/keeper/keys_test.go @@ -3,6 +3,7 @@ package keeper import ( "testing" "time" + "unicode/utf8" "github.com/stretchr/testify/require" @@ -47,6 +48,7 @@ func TestParseGrantStoreKey(t *testing.T) { granterAddr, _ := sdk.AccAddressFromBech32("cosmos15ky9du8a2wlstz6fpx3p4mqpjyrm5cgqjwl8sq") msg := "/cosmos.bank.v1beta1.MsgSend" skey := grantStoreKey(granteeAddr, granterAddr, msg) + require.False(t, utf8.Valid(skey)) // Action actual := grantKeyToString(skey) @@ -54,4 +56,5 @@ func TestParseGrantStoreKey(t *testing.T) { // Assert expected := "1|20|cosmos15ky9du8a2wlstz6fpx3p4mqpjyrm5cgqjwl8sq|20|cosmos15ky9du8a2wlstz6fpx3p4mqpjyrm5cgp0ctjdj|/cosmos.bank.v1beta1.MsgSend" require.Equal(t, expected, actual) + require.True(t, utf8.ValidString(actual)) }