Skip to content

Commit

Permalink
Dynamic validaton implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
askolesov committed Mar 1, 2022
1 parent e90eeb5 commit 661b60d
Show file tree
Hide file tree
Showing 12 changed files with 277 additions and 216 deletions.
25 changes: 14 additions & 11 deletions x/cheqd/keeper/keeper_did.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,29 @@ func (k Keeper) SetDidCount(ctx sdk.Context, count uint64) {
}

// AppendDid appends a did in the store with a new id and updates the count
func (k Keeper) AppendDid(ctx sdk.Context, did types.Did, metadata *types.Metadata) (*string, error) {
// TODO check that the did doesn't exist
func (k Keeper) AppendDid(ctx sdk.Context, did *types.Did, metadata *types.Metadata) (string, error) {
// Check that did doesn't exist
if k.HasDid(ctx, did.Id) {
return "", types.ErrDidDocNotFound.Wrapf(did.Id)
}

// Create the did
count := k.GetDidCount(ctx)
err := k.SetDid(ctx, did, metadata)
if err != nil {
return nil, err
return "", err
}

// Update did count
k.SetDidCount(ctx, count+1)
return &did.Id, nil
return did.Id, nil
}

// SetDid set a specific did in the store
func (k Keeper) SetDid(ctx sdk.Context, did types.Did, metadata *types.Metadata) error {
stateValue, err := types.NewStateValue(&did, metadata)
func (k Keeper) SetDid(ctx sdk.Context, did *types.Did, metadata *types.Metadata) error {
stateValue, err := types.NewStateValue(did, metadata)
if err != nil {
return types.ErrSetToState.Wrap(err.Error())
return err
}

store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.DidKey))
Expand All @@ -67,20 +70,20 @@ func (k Keeper) SetDid(ctx sdk.Context, did types.Did, metadata *types.Metadata)
}

// GetDid returns a did from its id
func (k Keeper) GetDid(ctx *sdk.Context, id string) (*types.StateValue, error) {
func (k Keeper) GetDid(ctx *sdk.Context, id string) (types.StateValue, error) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.DidKey))

if !k.HasDid(*ctx, id) {
return nil, sdkerrors.ErrNotFound
return types.StateValue{}, sdkerrors.ErrNotFound.Wrap(id)
}

var value types.StateValue
var bytes = store.Get(GetDidIDBytes(id))
if err := k.cdc.Unmarshal(bytes, &value); err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidType, err.Error())
return types.StateValue{}, sdkerrors.Wrap(sdkerrors.ErrInvalidType, err.Error())
}

return &value, nil
return value, nil
}

// HasDid checks if the did exists in the store
Expand Down
97 changes: 71 additions & 26 deletions x/cheqd/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,83 @@ func NewMsgServer(keeper Keeper) types.MsgServer {

var _ types.MsgServer = msgServer{}


func AppendSignerIfNeed(signers []types.Signer, controller string, msg *types.MsgUpdateDidPayload) []types.Signer {
for _, signer := range signers {
if signer.Signer == controller {
return signers
}
func ResolveDid(k *Keeper, ctx *sdk.Context, did string, inMemoryDIDs map[string]types.StateValue) (types.StateValue, error) {
value, found := inMemoryDIDs[did]
if found {
return value, nil
}

signer := types.Signer{
Signer: controller,
stateValue, err := k.GetDid(ctx, did)
if err != nil {
return types.StateValue{}, err
}

if controller == msg.Id {
signer.VerificationMethod = msg.VerificationMethod
signer.Authentication = msg.Authentication
}
return stateValue, nil
}

func ValidateDIDHasSupportedAuthKey() {

return append(signers, signer)
}

func (k msgServer) ValidateDidControllers(ctx *sdk.Context, id string, controllers []string, verMethods []*types.VerificationMethod) error {

for _, verificationMethod := range verMethods {
if err := k.ValidateController(ctx, id, verificationMethod.Controller); err != nil {
return err
}
}
//func ValidateVerificationMethod(namespace string, vm *VerificationMethod) error {
// switch utils.GetVerificationMethodType(vm.Type) {
// case utils.PublicKeyJwk:
// if len(vm.PublicKeyJwk) == 0 {
// return ErrBadRequest.Wrapf("%s: should contain `PublicKeyJwk` verification material property", vm.Type)
// }
// case utils.PublicKeyMultibase:
// if len(vm.PublicKeyMultibase) == 0 {
// return ErrBadRequest.Wrapf("%s: should contain `PublicKeyMultibase` verification material property", vm.Type)
// }
// default:
// return ErrBadRequest.Wrapf("%s: unsupported verification method type", vm.Type)
// }
//
// if len(vm.PublicKeyMultibase) == 0 && vm.PublicKeyJwk == nil {
// return ErrBadRequest.Wrap("The verification method must contain either a PublicKeyMultibase or a PublicKeyJwk")
// }
//
// if len(vm.Controller) == 0 {
// return ErrBadRequestIsRequired.Wrap("Controller")
// }
//
// return nil
//}

for _, didController := range controllers {
if err := k.ValidateController(ctx, id, didController); err != nil {
return err
}
}
return nil
}


//func AppendSignerIfNeed(signers []types.Signer, controller string, msg *types.MsgUpdateDidPayload) []types.Signer {
// for _, signer := range signers {
// if signer.Signer == controller {
// return signers
// }
// }
//
// signer := types.Signer{
// Signer: controller,
// }
//
// if controller == msg.Id {
// signer.VerificationMethod = msg.VerificationMethod
// signer.Authentication = msg.Authentication
// }
//
// return append(signers, signer)
//}
//
//func (k msgServer) ValidateDidControllers(ctx *sdk.Context, id string, controllers []string, verMethods []*types.VerificationMethod) error {
//
// for _, verificationMethod := range verMethods {
// if err := k.ValidateController(ctx, id, verificationMethod.Controller); err != nil {
// return err
// }
// }
//
// for _, didController := range controllers {
// if err := k.ValidateController(ctx, id, didController); err != nil {
// return err
// }
// }
// return nil
//}
8 changes: 6 additions & 2 deletions x/cheqd/keeper/msg_server_create_did.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ func (k msgServer) CreateDid(goCtx context.Context, msg *types.MsgCreateDid) (*t
}

// Verify that all controllers have at least one authentication key of supported type
controllers :=
controllers := msg.Payload.AggregateControllerDids()
for _, v := range controllers {
// ValidateDIDHasAtLeaseOneSupportedAuthKey
}

//if err := k.ValidateDidControllers(&ctx, payload.Id, payload.Controller, payload.VerificationMethod); err != nil {
// return nil, err
//}
Expand All @@ -54,7 +58,7 @@ func (k msgServer) CreateDid(goCtx context.Context, msg *types.MsgCreateDid) (*t
}

// Build DID and metadata
did := msg.Payload.ToDID()
did := msg.Payload.ToDid()
metadata := types.NewMetadataFromContext(ctx)

// Write to state
Expand Down
22 changes: 0 additions & 22 deletions x/cheqd/keeper/validate/did_doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,3 @@ package validate

import "github.com/cheqd/cheqd-node/x/cheqd/utils/strings"

const (
PublicKeyJwk = "PublicKeyJwk"
PublicKeyMultibase = "PublicKeyMultibase"
)

var VerificationMethodType = map[string]string{
"JsonWebKey2020": PublicKeyJwk,
"Ed25519VerificationKey2020": PublicKeyMultibase,
}

var ServiceType = []string{
"LinkedDomains",
"DIDCommMessaging",
}

func GetVerificationMethodType(vmType string) string {
return VerificationMethodType[vmType]
}

func IsValidDidServiceType(sType string) bool {
return strings.Contains(ServiceType, sType)
}
126 changes: 126 additions & 0 deletions x/cheqd/keeper/validate/signatures.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package validate

import (
"github.com/cheqd/cheqd-node/x/cheqd/types"
"github.com/cheqd/cheqd-node/x/cheqd/utils"
)

var SupportedVerificationMethodTypes = []string{"Ed25519VerificationKey2020"}

func IsVerificationMethodTypeSupported(type_ string) bool {
return utils.Contains(SupportedVerificationMethodTypes, type_)
}

func IsVerificationMethodSupported(vm *types.VerificationMethod) bool {
return IsVerificationMethodTypeSupported(vm.Type)
}

func ValidateSignature() {
// TODO: Implement
}

// TODO: Think about different key types
//func (v VerificationMethod) GetPublicKey() ([]byte, error) {
// if len(v.PublicKeyMultibase) > 0 {
// _, key, err := multibase.Decode(v.PublicKeyMultibase)
// if err != nil {
// return nil, ErrInvalidPublicKey.Wrapf("Cannot decode verification method '%s' public key", v.Id)
// }
// return key, nil
// }
//
// if len(v.PublicKeyJwk) > 0 {
// return nil, ErrInvalidPublicKey.Wrap("JWK format not supported")
// }
//
// return nil, ErrInvalidPublicKey.Wrapf("verification method '%s' public key not found", v.Id)
//}


//func (k *Keeper) VerifySignature(ctx *sdk.Context, msg types.IdentityMsg, signers []types.Signer, signatures []*types.SignInfo) error {
// if len(signers) == 0 {
// return types.ErrInvalidSignature.Wrap("At least one signer should be present")
// }
//
// if len(signatures) == 0 {
// return types.ErrInvalidSignature.Wrap("At least one signature should be present")
// }
//
// signingInput := msg.GetSignBytes()
//
// for _, signer := range signers {
// if signer.VerificationMethod == nil {
// state, err := k.GetDid(ctx, signer.Signer)
// if err != nil {
// return types.ErrDidDocNotFound.Wrap(signer.Signer)
// }
//
// didDoc, err := state.UnpackDataAsDid()
// if err != nil {
// return types.ErrDidDocNotFound.Wrap(signer.Signer)
// }
//
// signer.Authentication = didDoc.Authentication
// signer.VerificationMethod = didDoc.VerificationMethod
// }
//
// valid, err := VerifyIdentitySignature(signer, signatures, signingInput)
// if err != nil {
// return sdkerrors.Wrap(types.ErrInvalidSignature, err.Error())
// }
//
// if !valid {
// return sdkerrors.Wrap(types.ErrInvalidSignature, signer.Signer)
// }
// }
//
// return nil
//}
//
//func (k *Keeper) ValidateController(ctx *sdk.Context, id string, controller string) error {
// if id == controller {
// return nil
// }
// state, err := k.GetDid(ctx, controller)
// if err != nil {
// return types.ErrDidDocNotFound.Wrap(controller)
// }
// didDoc, err := state.UnpackDataAsDid()
// if err != nil {
// return types.ErrDidDocNotFound.Wrap(controller)
// }
// if len(didDoc.Authentication) == 0 {
// return types.ErrBadRequestInvalidVerMethod.Wrap(
// fmt.Sprintf("Verificatition method controller %s doesn't have an authentication keys", controller))
// }
// return nil
//}
//
//func VerifyIdentitySignature(signer types.Signer, signatures []*types.SignInfo, signingInput []byte) (bool, error) {
// result := true
// foundOne := false
//
// for _, info := range signatures {
// did, _ := utils.SplitDidUrlIntoDidAndFragment(info.VerificationMethodId)
// if did == signer.Signer {
// pubKey, err := FindPublicKey(signer, info.VerificationMethodId)
// if err != nil {
// return false, err
// }
//
// signature, err := base64.StdEncoding.DecodeString(info.Signature)
// if err != nil {
// return false, err
// }
//
// result = result && ed25519.Verify(pubKey, signingInput, signature)
// foundOne = true
// }
// }
//
// if !foundOne {
// return false, fmt.Errorf("signature %s not found", signer.Signer)
// }
//
// return result, nil
//}
10 changes: 0 additions & 10 deletions x/cheqd/keeper/validate/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,3 @@ func FindPublicKey(signer types.Signer, id string) (ed25519.PublicKey, error) {

return nil, types.ErrVerificationMethodNotFound.Wrap(id)
}

func FindVerificationMethod(vms []*types.VerificationMethod, id string) *types.VerificationMethod {
for _, vm := range vms {
if vm.Id == id {
return vm
}
}

return nil
}
Loading

0 comments on commit 661b60d

Please sign in to comment.