From 742a2807248838b4874a2a52717f7d484cfd4c78 Mon Sep 17 00:00:00 2001 From: arnabghose997 Date: Tue, 31 May 2022 10:48:42 +0530 Subject: [PATCH] implemented check to verify the first compnent of schema id matches with the author --- docs/ssi/schema-ops.md | 2 +- x/ssi/keeper/msg_server_schema.go | 19 +++++++++++-------- x/ssi/utils/utils.go | 7 ++++++- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/docs/ssi/schema-ops.md b/docs/ssi/schema-ops.md index df2e630..33f6433 100644 --- a/docs/ssi/schema-ops.md +++ b/docs/ssi/schema-ops.md @@ -29,7 +29,7 @@ Flags: Command: ```sh -hid-noded tx ssi create-schema '{"type":"https://w3c-ccg.github.io/vc-json-schemas/schema/1.0/schema.json","modelVersion":"v1.0","id":"did:hs:abcdefghi;id=17de181feb67447da4e78259d92d0240;version=1.0","name":"HS credential template","author":"did:hs:0f49341a-20ef-43d1-bc93-de30993e6c51","authored":"Tue Apr 06 2021 00:09:56 GMT+0530 (India Standard Time)","schema":{"schema":"https://json-schema.org/draft-07/schema#","description":"test","type":"object","properties":"{myString:{type:string},myNumner:{type:number},myBool:{type:boolean}}","required":["myString","myNumner","myBool"],"additionalProperties":false}}' did:hs:0f49341a-20ef-43d1-bc93-de30993e6c51#zEYJrMxWigf9boyeJMTRN4Ern8DJMoCXaLK77pzQmxVjf --ver-key oVtY1xceDZQjkfwlbCEC2vgeADcxpgd27vtYasBhcM/JLR6PnPoD9jvjSJrMsMJwS7faPy5OlFCdj/kgLVZMEg== --from node1 --keyring-backend test --chain-id hidnode +hid-noded tx ssi create-schema '{"type":"https://w3c-ccg.github.io/vc-json-schemas/schema/1.0/schema.json","modelVersion":"v1.0","id":"did:hs:0f49341a-20ef-43d1-bc93-de30993e6c51;id=17de181feb67447da4e78259d92d0240;version=1.0","name":"HS credential template","author":"did:hs:0f49341a-20ef-43d1-bc93-de30993e6c51","authored":"Tue Apr 06 2021 00:09:56 GMT+0530 (India Standard Time)","schema":{"schema":"https://json-schema.org/draft-07/schema#","description":"test","type":"object","properties":"{myString:{type:string},myNumner:{type:number},myBool:{type:boolean}}","required":["myString","myNumner","myBool"],"additionalProperties":false}}' did:hs:0f49341a-20ef-43d1-bc93-de30993e6c51#zEYJrMxWigf9boyeJMTRN4Ern8DJMoCXaLK77pzQmxVjf --ver-key oVtY1xceDZQjkfwlbCEC2vgeADcxpgd27vtYasBhcM/JLR6PnPoD9jvjSJrMsMJwS7faPy5OlFCdj/kgLVZMEg== --from node1 --keyring-backend test --chain-id hidnode ``` The above command will fail if the User's (`did:hs:0f49341a-20ef-43d1-bc93-de30993e6c51`) DID is not registered on chain \ No newline at end of file diff --git a/x/ssi/keeper/msg_server_schema.go b/x/ssi/keeper/msg_server_schema.go index 3fca8ef..def6245 100644 --- a/x/ssi/keeper/msg_server_schema.go +++ b/x/ssi/keeper/msg_server_schema.go @@ -16,22 +16,25 @@ func (k msgServer) CreateSchema(goCtx context.Context, msg *types.MsgCreateSchem schemaMsg := msg.GetSchema() schemaID := schemaMsg.GetId() - if err := utils.IsValidSchemaID(schemaID); err != nil { + // Get the Did Document of Schema's Author + authorDidDocument, err := k.GetDid(&ctx, schemaMsg.GetAuthor()) + if err != nil { + return nil, sdkerrors.Wrap(err, fmt.Sprintf("The DID %s is not available", schemaMsg.GetAuthor())) + } + + // Check if Schema ID is valid + authorDid := authorDidDocument.GetDid().GetId() + if err := utils.IsValidSchemaID(schemaID, authorDid); err != nil { return nil, sdkerrors.Wrap(types.ErrInvalidSchemaID, err.Error()) } + // Check if Schema already exists if k.HasSchema(ctx, schemaID) { return nil, sdkerrors.Wrap(types.ErrSchemaExists, fmt.Sprintf("Schema ID: %s", schemaID)) } - //Get the DID of SChema's Author - authorDID, err := k.GetDid(&ctx, schemaMsg.GetAuthor()) - if err != nil { - return nil, sdkerrors.Wrap(err, fmt.Sprintf("The DID %s is not available", schemaMsg.GetAuthor())) - } - // Signature check - didSigners := authorDID.GetDid().GetSigners() + didSigners := authorDidDocument.GetDid().GetSigners() if err := k.VerifySignatureOnCreateSchema(&ctx, schemaMsg, didSigners, msg.GetSignatures()); err != nil { return nil, err } diff --git a/x/ssi/utils/utils.go b/x/ssi/utils/utils.go index 65065f8..2f2b73b 100644 --- a/x/ssi/utils/utils.go +++ b/x/ssi/utils/utils.go @@ -169,7 +169,7 @@ func FindVerificationMethod(vms []*types.VerificationMethod, id string) *types.V return nil } -func IsValidSchemaID(schemaId string) error { +func IsValidSchemaID(schemaId string, authorDid string) error { IdComponents := strings.Split(schemaId, ";") if len(IdComponents) < 2 { return errors.New("Expected 3 components in schema ID after being seperated by `;`, got " + fmt.Sprint(len(IdComponents)) + " components. The Schema ID is `" + schemaId + "` ") @@ -180,6 +180,11 @@ func IsValidSchemaID(schemaId string) error { return errors.New("Expected did:hs as prefix in schema ID, The Schema ID is " + schemaId) } + // Check if the first component matches with author Did + if authorDid != IdComponents[0] { + return errors.New("author`s did doesn`t match with the first component of schema id") + } + //Checking the type of version versionNumber := strings.Split(IdComponents[2], "=")[1] // TODO: The regex pattern should be configurable to match the version format.