Skip to content

Commit

Permalink
Merge pull request #162 from hypersign-protocol/161-schema-rpc-author…
Browse files Browse the repository at this point in the history
…-schema-id-check

Schema ID Verification Check
  • Loading branch information
arnabghose997 committed May 31, 2022
2 parents 432d264 + 742a280 commit 99a9243
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/ssi/schema-ops.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 11 additions & 8 deletions x/ssi/keeper/msg_server_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 6 additions & 1 deletion x/ssi/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 + "` ")
Expand All @@ -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.
Expand Down

0 comments on commit 99a9243

Please sign in to comment.