Skip to content

Commit

Permalink
Merge pull request #303 from hypersign-protocol/release/v0.1.x
Browse files Browse the repository at this point in the history
v0.1.3: Release
  • Loading branch information
arnabghose997 authored Nov 10, 2022
2 parents c9afc3a + 3918897 commit 253df94
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 139 deletions.
5 changes: 5 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,11 @@ func New(
return fromVM, nil
})

app.UpgradeKeeper.SetUpgradeHandler("v013", func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
ctx.Logger().Info("v0.1.3 upgrade")
return fromVM, nil
})

// register the staking hooks
// NOTE: stakingKeeper above is passed by reference, so that it will contain these hooks
app.StakingKeeper = *stakingKeeper.SetHooks(
Expand Down
45 changes: 45 additions & 0 deletions x/ssi/keeper/document_verification/credential_verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ package verification

import (
"regexp"
"time"
"fmt"

"github.com/hypersign-protocol/hid-node/x/ssi/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

func VerifyCredentialHash(credHash string) bool {
Expand All @@ -19,3 +24,43 @@ func VerifyCredentialHash(credHash string) bool {

return matchFound
}

func VerifyCredentialStatusDates(issuanceDate time.Time, expirationDate time.Time) error {
var dateDiff int64 = int64(expirationDate.Sub(issuanceDate)) / 1e9 // converting nanoseconds to seconds
if dateDiff < 0 {
return sdkerrors.Wrapf(types.ErrInvalidDate, fmt.Sprintf("expiration date %s cannot be less than issuance date %s", expirationDate, issuanceDate))
}

return nil
}

func VerifyCredentialProofDates(credProof *types.CredentialProof, credRegistration bool) error {
var dateDiff int64

proofCreatedDate := credProof.GetCreated()
proofCreatedDateParsed, err := time.Parse(time.RFC3339, proofCreatedDate)
if err != nil {
return sdkerrors.Wrapf(types.ErrInvalidDate, fmt.Sprintf("invalid created date format: %s", proofCreatedDate))
}

proofUpdatedDate := credProof.GetUpdated()
proofUpdatedDateParsed, err := time.Parse(time.RFC3339, proofUpdatedDate)
if err != nil {
return sdkerrors.Wrapf(types.ErrInvalidDate, fmt.Sprintf("invalid created date format: %s", proofUpdatedDate))
}

// If credRegistration is True, check for equity of updated and created dates will proceeed
// Else, check for updated date being greater than created date will proceeed
if credRegistration {
if !proofUpdatedDateParsed.Equal(proofCreatedDateParsed) {
return sdkerrors.Wrapf(types.ErrInvalidDate, fmt.Sprintf("updated date %s should be similar to created date %s", proofUpdatedDate, proofCreatedDate))
}
} else {
dateDiff = int64(proofUpdatedDateParsed.Sub(proofCreatedDateParsed)) / 1e9 // converting nanoseconds to seconds
if dateDiff <= 0 {
return sdkerrors.Wrapf(types.ErrInvalidDate, fmt.Sprintf("update date %s cannot be less than or equal to created date %s in case of credential status update", proofUpdatedDate, proofCreatedDate))
}
}

return nil
}
9 changes: 9 additions & 0 deletions x/ssi/keeper/document_verification/did_verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,12 @@ func ValidateDidDocument(didDoc *types.Did, genesisNamespace string) error {

return nil
}


// Check the Deactivate status of DID
func VerifyDidDeactivate(metadata *types.Metadata, id string) error {
if metadata.GetDeactivated() {
return sdkerrors.Wrap(types.ErrDidDocDeactivated, fmt.Sprintf("DidDoc ID: %s", id))
}
return nil
}
28 changes: 18 additions & 10 deletions x/ssi/keeper/msg_server_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ func (k msgServer) RegisterCredentialStatus(goCtx context.Context, msg *types.Ms
return nil, sdkerrors.Wrapf(types.ErrInvalidDate, fmt.Sprintf("invalid issuance date format: %s", issuanceDate))
}

if err := VerifyCredentialStatusDates(issuanceDateParsed, expirationDateParsed); err != nil {
if err := verify.VerifyCredentialStatusDates(issuanceDateParsed, expirationDateParsed); err != nil {
return nil, err
}

// Check if updated date iss imilar to created date
if err := VerifyCredentialProofDates(credProof, true); err != nil {
if err := verify.VerifyCredentialProofDates(credProof, true); err != nil {
return nil, err
}

Expand All @@ -90,10 +90,14 @@ func (k msgServer) RegisterCredentialStatus(goCtx context.Context, msg *types.Ms
}

did := didDocument.GetDidDocument()
signature := credProof.GetProofValue()
verificationMethod := credProof.GetVerificationMethod()

err = k.VerifyCredentialSignature(credMsg, did, signature, verificationMethod)
// Verify Signature
signature := &types.SignInfo{
VerificationMethodId: credProof.GetVerificationMethod(),
Signature: credProof.GetProofValue(),
}
signatures := []*types.SignInfo{signature}
err = k.VerifyDocumentSignature(&ctx, credMsg, did, signatures)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -190,12 +194,12 @@ func (k msgServer) updateCredentialStatus(ctx sdk.Context, newCredStatus *types.
}

// Check if new expiration date isn't less than new issuance date
if err := VerifyCredentialStatusDates(newIssuanceDateParsed, newExpirationDateParsed); err != nil {
if err := verify.VerifyCredentialStatusDates(newIssuanceDateParsed, newExpirationDateParsed); err != nil {
return nil, err
}

// Check if updated date iss imilar to created date
if err := VerifyCredentialProofDates(newCredProof, false); err != nil {
if err := verify.VerifyCredentialProofDates(newCredProof, false); err != nil {
return nil, err
}

Expand Down Expand Up @@ -257,10 +261,14 @@ func (k msgServer) updateCredentialStatus(ctx sdk.Context, newCredStatus *types.
}

did := didDocument.GetDidDocument()
signature := newCredProof.GetProofValue()
verificationMethod := newCredProof.GetVerificationMethod()

err = k.VerifyCredentialSignature(newCredStatus, did, signature, verificationMethod)
// Verify Signature
signature := &types.SignInfo{
VerificationMethodId: newCredProof.GetVerificationMethod(),
Signature: newCredProof.GetProofValue(),
}
signatures := []*types.SignInfo{signature}
err = k.VerifyDocumentSignature(&ctx, newCredStatus, did, signatures)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions x/ssi/keeper/msg_server_did.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (k msgServer) CreateDID(goCtx context.Context, msg *types.MsgCreateDID) (*t
}

// Verification of Did Document Signature
if err := k.VerifySignature(&ctx, didMsg, didSigners, signatures); err != nil {
if err := k.VerifyDidSignature(&ctx, didMsg, didSigners, signatures); err != nil {
return nil, err
}

Expand Down Expand Up @@ -88,7 +88,7 @@ func (k msgServer) UpdateDID(goCtx context.Context, msg *types.MsgUpdateDID) (*t
oldMetaData := oldDIDDoc.GetDidDocumentMetadata()

// Check if the status of DID Document is deactivated
if err := VerifyDidDeactivate(oldMetaData, didId); err != nil {
if err := verify.VerifyDidDeactivate(oldMetaData, didId); err != nil {
return nil, err
}

Expand Down Expand Up @@ -157,7 +157,7 @@ func (k msgServer) DeactivateDID(goCtx context.Context, msg *types.MsgDeactivate
oldVersionId := metadata.GetVersionId()

// Check if the DID is already deactivated
if err := VerifyDidDeactivate(metadata, didId); err != nil {
if err := verify.VerifyDidDeactivate(metadata, didId); err != nil {
return nil, err
}

Expand All @@ -177,7 +177,7 @@ func (k msgServer) DeactivateDID(goCtx context.Context, msg *types.MsgDeactivate
// Signature Verification
signers := didDoc.GetSigners()
signatures := msg.Signatures
if err := k.VerifySignature(&ctx, didDoc, signers, signatures); err != nil {
if err := k.VerifyDidSignature(&ctx, didDoc, signers, signatures); err != nil {
return nil, err
}

Expand Down
7 changes: 6 additions & 1 deletion x/ssi/keeper/msg_server_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ func (k msgServer) CreateSchema(goCtx context.Context, msg *types.MsgCreateSchem
}

// Signature check
if err := k.VerifySchemaSignature(schemaDoc, authorDidDocument.DidDocument, schemaProof.ProofValue, schemaProof.VerificationMethod); err != nil {
signature := &types.SignInfo{
VerificationMethodId: schemaProof.VerificationMethod,
Signature: schemaProof.ProofValue,
}
signatures := []*types.SignInfo{signature}
if err := k.VerifyDocumentSignature(&ctx, schemaDoc, authorDidDocument.DidDocument, signatures); err != nil {
return nil, err
}

Expand Down
Loading

0 comments on commit 253df94

Please sign in to comment.