Skip to content

Commit

Permalink
Merge pull request #294 from cheqd/dev-1080-metadata-update
Browse files Browse the repository at this point in the history
DEV-1080, DEV-710: Update `VersionId` and `Update` DID metadata fields
  • Loading branch information
askolesov authored Mar 18, 2022
2 parents 1ad0c88 + 989e995 commit 9e07d8e
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 19 deletions.
7 changes: 1 addition & 6 deletions tests/e2e-bash/tests/test_rotate_key.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}'",
Expand Down
6 changes: 3 additions & 3 deletions x/cheqd/keeper/msg_server_update_did.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
17 changes: 11 additions & 6 deletions x/cheqd/types/stateValue.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) {
Expand Down
52 changes: 50 additions & 2 deletions x/cheqd/types/stateValue_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
4 changes: 2 additions & 2 deletions x/cheqd/utils/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package utils

import (
"fmt"
multibase "github.com/multiformats/go-multibase"
"github.com/multiformats/go-multibase"
)

func ValidateMultibase(data string) error {
Expand All @@ -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)
}
11 changes: 11 additions & 0 deletions x/cheqd/utils/tx.go
Original file line number Diff line number Diff line change
@@ -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())
}

0 comments on commit 9e07d8e

Please sign in to comment.