diff --git a/tests/e2e-bash/tests/test_rotate_key.sh b/tests/e2e-bash/tests/test_rotate_key.sh index 69b157e1f..31d5fe3c7 100644 --- a/tests/e2e-bash/tests/test_rotate_key.sh +++ b/tests/e2e-bash/tests/test_rotate_key.sh @@ -35,17 +35,12 @@ RESULT=$(cheqd-noded tx cheqd create-did "${MSG_CREATE_DID}" "${KEY_ID}" "${OLD_ assert_tx_successful "$RESULT" -# Query DID to find out version id -# shellcheck disable=SC2086 -RESULT=$(cheqd-noded query cheqd did "${DID}" ${QUERY_PARAMS}) -VERSION_ID=$(echo "${RESULT}" | jq -r ".metadata.version_id") - - # Updating DID NEW_VER_KEY="$(cheqd-noded debug ed25519 random)" NEW_VER_PUB_BASE_64=$(echo "${NEW_VER_KEY}" | jq -r ".pub_key_base_64") NEW_VER_PRIV_BASE_64=$(echo "${NEW_VER_KEY}" | jq -r ".priv_key_base_64") NEW_VER_PUB_MULTIBASE_58=$(cheqd-noded debug encoding base64-multibase58 "${NEW_VER_PUB_BASE_64}") +VERSION_ID=$(echo "${RESULT}" | jq -r ".txhash") MSG_UPDATE_DID='{ "id": "'${DID}'", diff --git a/x/cheqd/keeper/msg_server_update_did.go b/x/cheqd/keeper/msg_server_update_did.go index bcf29c677..76e56c15c 100644 --- a/x/cheqd/keeper/msg_server_update_did.go +++ b/x/cheqd/keeper/msg_server_update_did.go @@ -2,6 +2,7 @@ package keeper import ( "context" + "github.com/cheqd/cheqd-node/x/cheqd/types" "github.com/cheqd/cheqd-node/x/cheqd/utils" sdk "github.com/cosmos/cosmos-sdk/types" @@ -48,9 +49,8 @@ func (k msgServer) UpdateDid(goCtx context.Context, msg *types.MsgUpdateDid) (*t updatedDid := msg.Payload.ToDid() updatedDid.ReplaceIds(updatedDid.Id, updatedDid.Id+UpdatedPostfix) - updatedMetadata := types.NewMetadataFromContext(ctx) - updatedMetadata.Created = existingStateValue.Metadata.Created - updatedMetadata.Updated = ctx.BlockTime().String() + updatedMetadata := *existingStateValue.Metadata + updatedMetadata.Update(ctx) updatedStateValue, err := types.NewStateValue(&updatedDid, &updatedMetadata) if err != nil { diff --git a/x/cheqd/types/stateValue.go b/x/cheqd/types/stateValue.go index 7fab95501..48cbea855 100644 --- a/x/cheqd/types/stateValue.go +++ b/x/cheqd/types/stateValue.go @@ -1,12 +1,13 @@ package types import ( - "encoding/base64" + "reflect" + "time" + + "github.com/cheqd/cheqd-node/x/cheqd/utils" "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/gogo/protobuf/proto" - "github.com/tendermint/tendermint/crypto/tmhash" - "reflect" ) // StateValueData is interface uniting possible types to be used for stateValue.data field @@ -31,10 +32,14 @@ func NewStateValue(data StateValueData, metadata *Metadata) (StateValue, error) } func NewMetadataFromContext(ctx sdk.Context) Metadata { - created := ctx.BlockTime().String() - txHash := base64.StdEncoding.EncodeToString(tmhash.Sum(ctx.TxBytes())) + created := ctx.BlockTime().Format(time.RFC3339) + txHash := utils.GetTxHash(ctx.TxBytes()) + + return Metadata{Created: created, Deactivated: false, VersionId: txHash} +} - return Metadata{Created: created, Updated: created, Deactivated: false, VersionId: txHash} +func (m *Metadata) Update(ctx sdk.Context) { + m.Updated = ctx.BlockTime().Format(time.RFC3339) } func (m StateValue) UnpackData() (StateValueData, error) { diff --git a/x/cheqd/types/stateValue_test.go b/x/cheqd/types/stateValue_test.go index f37b55e1f..b79648f32 100644 --- a/x/cheqd/types/stateValue_test.go +++ b/x/cheqd/types/stateValue_test.go @@ -1,17 +1,21 @@ package types import ( + "testing" + "time" + "github.com/cheqd/cheqd-node/x/cheqd/utils" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec/types" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "testing" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" ) func Test_PackUnpackAny(t *testing.T) { original := &Did{ - Id: "test", + Id: "test", } // Construct codec @@ -36,3 +40,47 @@ func Test_PackUnpackAny(t *testing.T) { require.IsType(t, &Did{}, decoded) require.Equal(t, original, decoded) } + +func Test_NewMetadataFromContext(t *testing.T) { + createdTime := time.Now() + ctx := sdk.NewContext(nil, tmproto.Header{ChainID: "test_chain_id", Time: createdTime}, true, nil) + ctx.WithTxBytes([]byte("test_tx")) + expectedMetadata := Metadata{ + Created: createdTime.UTC().Format(time.RFC3339), + Updated: "", + Deactivated: false, + VersionId: utils.GetTxHash(ctx.TxBytes()), + } + + metadata := NewMetadataFromContext(ctx) + + require.Equal(t, expectedMetadata, metadata) + +} + +func Test_UpdateMetadata(t *testing.T) { + createdTime := time.Now() + updatedTime := createdTime.Add(time.Hour) + + ctx1 := NewContext(createdTime, []byte("test1_tx")) + ctx2 := NewContext(updatedTime, []byte("test1_tx")) + + expectedMetadata := Metadata{ + Created: createdTime.UTC().Format(time.RFC3339), + Updated: updatedTime.UTC().Format(time.RFC3339), + Deactivated: false, + VersionId: utils.GetTxHash(ctx2.TxBytes()), + } + + metadata := NewMetadataFromContext(ctx1) + metadata.Update(ctx2) + + require.Equal(t, expectedMetadata, metadata) + +} + +func NewContext(time time.Time, txBytes []byte) sdk.Context { + ctx := sdk.NewContext(nil, tmproto.Header{ChainID: "test_chain_id", Time: time}, true, nil) + ctx.WithTxBytes(txBytes) + return ctx +} diff --git a/x/cheqd/utils/encoding.go b/x/cheqd/utils/encoding.go index 10de42937..a75df898c 100644 --- a/x/cheqd/utils/encoding.go +++ b/x/cheqd/utils/encoding.go @@ -2,7 +2,7 @@ package utils import ( "fmt" - multibase "github.com/multiformats/go-multibase" + "github.com/multiformats/go-multibase" ) func ValidateMultibase(data string) error { @@ -25,5 +25,5 @@ func ValidateMultibaseEncoding(data string, expectedEncoding multibase.Encoding) } func ValidateBase58(data string) error { - return ValidateMultibaseEncoding(string(multibase.Base58BTC) + data, multibase.Base58BTC) + return ValidateMultibaseEncoding(string(multibase.Base58BTC)+data, multibase.Base58BTC) } diff --git a/x/cheqd/utils/tx.go b/x/cheqd/utils/tx.go new file mode 100644 index 000000000..89e9daf13 --- /dev/null +++ b/x/cheqd/utils/tx.go @@ -0,0 +1,11 @@ +package utils + +import ( + "fmt" + "github.com/tendermint/tendermint/types" +) + +func GetTxHash(txBytes []byte) string { + //return base64.StdEncoding.EncodeToString(tmhash.Sum(txBytes)) + return fmt.Sprintf("%X", types.Tx(txBytes).Hash()) +}