Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilGeorgiev committed Apr 2, 2024
1 parent 5b5e64b commit a1d24a0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
9 changes: 6 additions & 3 deletions x/authz/keeper/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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<prefix(1 Byte)><granterAddressLen(1 Byte)><granterAddress_Bytes><granteeAddressLen(1 Byte)><granteeAddress_Bytes><msgType_Bytes>
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:]

Expand Down
3 changes: 3 additions & 0 deletions x/authz/keeper/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"testing"
"time"
"unicode/utf8"

"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -47,11 +48,13 @@ 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)

// Assert
expected := "1|20|cosmos15ky9du8a2wlstz6fpx3p4mqpjyrm5cgqjwl8sq|20|cosmos15ky9du8a2wlstz6fpx3p4mqpjyrm5cgp0ctjdj|/cosmos.bank.v1beta1.MsgSend"
require.Equal(t, expected, actual)
require.True(t, utf8.ValidString(actual))
}

0 comments on commit a1d24a0

Please sign in to comment.