Skip to content

Commit

Permalink
refactor: every error messages thrown from RPC will carry a status code
Browse files Browse the repository at this point in the history
  • Loading branch information
arnabghose997 committed Mar 2, 2023
1 parent 146cfe0 commit be2da81
Show file tree
Hide file tree
Showing 16 changed files with 129 additions and 179 deletions.
10 changes: 3 additions & 7 deletions x/ssi/keeper/client_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"strings"

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

Expand Down Expand Up @@ -64,12 +63,9 @@ func getClientSpecDocBytes(clientSpecOpts types.ClientSpecOpts) ([]byte, error)
case "":
return clientSpecOpts.SSIDoc.GetSignBytes(), nil
default:
return nil, sdkerrors.Wrap(
types.ErrInvalidClientSpecType,
fmt.Sprintf(
"supported client specs are : [%s]",
strings.Join(types.SupportedClientSpecs, ", "),
),
return nil, fmt.Errorf(
"supported client specs are : [%s]",
strings.Join(types.SupportedClientSpecs, ", "),
)
}
}
20 changes: 12 additions & 8 deletions x/ssi/keeper/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,37 @@ package keeper

import (
"encoding/binary"
"fmt"
"time"

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

func (k Keeper) RegisterCred(ctx sdk.Context, cred *types.Credential) uint64 {
count := k.GetCredentialCount(ctx)
func (k Keeper) RegisterCredentialStatusInState(ctx sdk.Context, cred *types.Credential) uint64 {
count := k.GetCredentialStatusCount(ctx)
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.CredKey))

id := cred.GetClaim().GetId()
credBytes := k.cdc.MustMarshal(cred)

store.Set([]byte(id), credBytes)
k.SetCredentialCount(ctx, count+1)
k.SetCredentialStatusCount(ctx, count+1)
return count
}

func (k Keeper) GetCredential(ctx *sdk.Context, id string) (*types.Credential, error) {
func (k Keeper) GetCredentialStatusFromState(ctx *sdk.Context, id string) (*types.Credential, error) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.CredKey))

var cred types.Credential
var bytes = store.Get([]byte(id))
if len(bytes) == 0 {
return nil, fmt.Errorf("credential status document %s not found", id)
}

if err := k.cdc.Unmarshal(bytes, &cred); err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidType, err.Error())
return nil, fmt.Errorf("internal: unable to unmarshal credentialStatus id %s from state", id)
}

return &cred, nil
Expand All @@ -40,7 +44,7 @@ func (k Keeper) HasCredential(ctx sdk.Context, id string) bool {
return store.Has([]byte(id))
}

func (k Keeper) GetCredentialCount(ctx sdk.Context) uint64 {
func (k Keeper) GetCredentialStatusCount(ctx sdk.Context) uint64 {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.CredCountKey))
byteKey := []byte(types.CredCountKey)
bz := store.Get(byteKey)
Expand All @@ -50,7 +54,7 @@ func (k Keeper) GetCredentialCount(ctx sdk.Context) uint64 {
return binary.BigEndian.Uint64(bz)
}

func (k Keeper) SetCredentialCount(ctx sdk.Context, count uint64) {
func (k Keeper) SetCredentialStatusCount(ctx sdk.Context, count uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.CredCountKey))
byteKey := []byte(types.CredCountKey)
bz := make([]byte, 8)
Expand Down
6 changes: 3 additions & 3 deletions x/ssi/keeper/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package keeper

import (
"encoding/binary"
"fmt"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/hypersign-protocol/hid-node/utils"
"github.com/hypersign-protocol/hid-node/x/ssi/types"
)
Expand Down Expand Up @@ -50,11 +50,11 @@ func (k Keeper) GetDidDocumentState(ctx *sdk.Context, id string) (*types.DidDocu
var bytes = store.Get([]byte(id))

if len(bytes) == 0 {
return nil, sdkerrors.Wrap(types.ErrDidDocNotFound, id)
return nil, fmt.Errorf("DID Document %s not found", id)
}

if err := k.cdc.Unmarshal(bytes, &didDocState); err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidType, err.Error())
return nil, fmt.Errorf("internal: unable to unmarshal didDocBytes of id %s", id)
}

return &didDocState, nil
Expand Down
4 changes: 2 additions & 2 deletions x/ssi/keeper/grpc_query_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (k Keeper) QueryCredential(goCtx context.Context, req *types.QueryCredentia
}
ctx := sdk.UnwrapSDKContext(goCtx)

cred, err := k.GetCredential(&ctx, req.CredId)
cred, err := k.GetCredentialStatusFromState(&ctx, req.CredId)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -51,5 +51,5 @@ func (k Keeper) QueryCredentials(goCtx context.Context, req *types.QueryCredenti
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryCredentialsResponse{Credentials: credentials, TotalCount: k.GetCredentialCount(ctx)}, nil
return &types.QueryCredentialsResponse{Credentials: credentials, TotalCount: k.GetCredentialStatusCount(ctx)}, nil
}
3 changes: 1 addition & 2 deletions x/ssi/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/hypersign-protocol/hid-node/x/ssi/types"
"github.com/hypersign-protocol/hid-node/x/ssi/verification"
)
Expand Down Expand Up @@ -39,7 +38,7 @@ func (k msgServer) checkControllerPresenceInState(

// Check if they are deactivated
if didDoc.DidDocumentMetadata.Deactivated {
return sdkerrors.Wrapf(types.ErrDidDocDeactivated, "%s", didDoc.DidDocument.Id)
return fmt.Errorf("DID document %s is deactivated", didDoc.DidDocument.Id)
}
}
return nil
Expand Down
18 changes: 9 additions & 9 deletions x/ssi/keeper/msg_server_create_did.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ func (k msgServer) CreateDID(goCtx context.Context, msg *types.MsgCreateDID) (*t

// Validate DID Document
if err := msgDidDocument.ValidateDidDocument(); err != nil {
return nil, err
return nil, sdkerrors.Wrap(types.ErrInvalidDidDoc, err.Error())
}

// Validate namespace in DID Document
if err := didNamespaceValidation(k, ctx, msgDidDocument); err != nil {
return nil, err
if err := didDocNamespaceValidation(k, ctx, msgDidDocument); err != nil {
return nil, sdkerrors.Wrap(types.ErrInvalidDidDoc, err.Error())
}

// Checks if the Did Document is already registered
Expand All @@ -38,21 +38,21 @@ func (k msgServer) CreateDID(goCtx context.Context, msg *types.MsgCreateDID) (*t
controllerList := getControllersForCreateDID(msgDidDocument)

if err := k.checkControllerPresenceInState(ctx, controllerList, msgDidDocument.Id); err != nil {
return nil, err
return nil, sdkerrors.Wrap(types.ErrInvalidDidDoc, err.Error())
}

// Collect necessary Verification Methods which are needed to be valid
requiredVMs, err := getVerificationMethodsForCreateDID(msgDidDocument)
if err != nil {
return nil, err
return nil, sdkerrors.Wrap(types.ErrVerificationMethodNotFound, err.Error())
}

// Associate Signatures
signMap := makeSignatureMap(msgSignatures)

requiredVmMap, err := k.formMustControllerVmListMap(ctx, controllerList, requiredVMs, signMap)
if err != nil {
return nil, err
return nil, sdkerrors.Wrap(types.ErrInvalidDidDoc, err.Error())
}

// ClientSpec check
Expand All @@ -64,13 +64,13 @@ func (k msgServer) CreateDID(goCtx context.Context, msg *types.MsgCreateDID) (*t
var didDocBytes []byte
didDocBytes, err = getClientSpecDocBytes(clientSpecOpts)
if err != nil {
return nil, err
return nil, sdkerrors.Wrapf(types.ErrInvalidClientSpecType, err.Error())
}

// Verify Signatures
err = verification.VerifySignatureOfEveryController(didDocBytes, requiredVmMap)
if err != nil {
return nil, err
return nil, sdkerrors.Wrapf(types.ErrInvalidSignature, err.Error())
}

// Formt DID Document Metadata
Expand Down Expand Up @@ -122,7 +122,7 @@ func getVerificationMethodsForCreateDID(didDocument *types.Did) ([]*types.Verifi

if !foundAtleastOneSubjectVM && types.FindInSlice(didDocument.Controller, didDocument.Id) {
return nil, fmt.Errorf(
"there should be atleast one verification method from subject DID controller %v", didDocument.Id)
"there should be atleast one verification method of DID Subject %v", didDocument.Id)
}

return mustHaveVerificaitonMethods, nil
Expand Down
30 changes: 15 additions & 15 deletions x/ssi/keeper/msg_server_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (k msgServer) RegisterCredentialStatus(goCtx context.Context, msg *types.Ms
// Check the format of Credential ID
err := verification.IsValidID(credId, chainNamespace, "credDocument")
if err != nil {
return nil, err
return nil, sdkerrors.Wrap(types.ErrInvalidSchemaID, err.Error())
}

// Check if the credential already exist in the store
Expand All @@ -46,7 +46,7 @@ func (k msgServer) RegisterCredentialStatus(goCtx context.Context, msg *types.Ms
// Check if issuer's DID is deactivated
issuerDidDocument, err := k.GetDidDocumentState(&ctx, issuerId)
if err != nil {
return nil, err
return nil, sdkerrors.Wrap(types.ErrInvalidDidDoc, err.Error())
}
if issuerDidDocument.DidDocumentMetadata.Deactivated {
return nil, sdkerrors.Wrap(types.ErrDidDocDeactivated, fmt.Sprintf("%s is deactivated and cannot used be used to register credential status", issuerDidDocument.DidDocument.Id))
Expand All @@ -66,12 +66,12 @@ func (k msgServer) RegisterCredentialStatus(goCtx context.Context, msg *types.Ms
}

if err := verification.VerifyCredentialStatusDates(issuanceDateParsed, expirationDateParsed); err != nil {
return nil, err
return nil, sdkerrors.Wrapf(types.ErrInvalidCredentialField, err.Error())
}

// Check if updated date is similar to created date
if err := verification.VerifyCredentialProofDates(msgCredProof, true); err != nil {
return nil, err
return nil, sdkerrors.Wrapf(types.ErrInvalidCredentialField, err.Error())
}

// Check if the created date lies between issuance and expiration
Expand All @@ -95,13 +95,13 @@ func (k msgServer) RegisterCredentialStatus(goCtx context.Context, msg *types.Ms

credDocBytes, err := getClientSpecDocBytes(clientSpecOpts)
if err != nil {
return nil, err
return nil, sdkerrors.Wrapf(types.ErrInvalidClientSpecType, err.Error())
}

// Verify Signature
err = k.VerifyDocumentProof(ctx, credDocBytes, msgCredProof)
if err != nil {
return nil, err
return nil, sdkerrors.Wrapf(types.ErrInvalidSignature, err.Error())
}

cred := &types.Credential{
Expand All @@ -113,14 +113,14 @@ func (k msgServer) RegisterCredentialStatus(goCtx context.Context, msg *types.Ms
Proof: msgCredProof,
}

id = k.RegisterCred(ctx, cred)
id = k.RegisterCredentialStatusInState(ctx, cred)

} else {
cred, err := k.updateCredentialStatus(ctx, msg)
if err != nil {
return nil, err
}
id = k.RegisterCred(ctx, cred)
id = k.RegisterCredentialStatusInState(ctx, cred)
}

return &types.MsgRegisterCredentialStatusResponse{Id: id}, nil
Expand All @@ -133,7 +133,7 @@ func (k msgServer) updateCredentialStatus(ctx sdk.Context, msg *types.MsgRegiste
credId := msgNewCredStatus.GetClaim().GetId()

// Get Credential from store
oldCredStatus, err := k.GetCredential(&ctx, credId)
oldCredStatus, err := k.GetCredentialStatusFromState(&ctx, credId)
if err != nil {
return nil, err
}
Expand All @@ -147,7 +147,7 @@ func (k msgServer) updateCredentialStatus(ctx sdk.Context, msg *types.MsgRegiste
// Check if issuer's DID is deactivated
issuerDidDocument, err := k.GetDidDocumentState(&ctx, issuerId)
if err != nil {
return nil, err
return nil, sdkerrors.Wrap(types.ErrDidDocNotFound, err.Error())
}
if issuerDidDocument.DidDocumentMetadata.Deactivated {
return nil, sdkerrors.Wrap(types.ErrDidDocDeactivated, fmt.Sprintf("%s is deactivated and cannot used be used to register credential status", issuerDidDocument.DidDocument.Id))
Expand All @@ -156,7 +156,7 @@ func (k msgServer) updateCredentialStatus(ctx sdk.Context, msg *types.MsgRegiste
// Check if the provided isser Id is the one who issued the VC
if issuerId != oldCredStatus.GetIssuer() {
return nil, sdkerrors.Wrapf(types.ErrInvalidCredentialField,
fmt.Sprintf("Issuer ID %s is not issuer of verifiable credential id %s", issuerId, credId))
fmt.Sprintf("issuer id %s is not issuer of verifiable credential id %s", issuerId, credId))
}

// Check if the new expiration date and issuance date are same as old one.
Expand Down Expand Up @@ -192,12 +192,12 @@ func (k msgServer) updateCredentialStatus(ctx sdk.Context, msg *types.MsgRegiste

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

// Check if updated date iss imilar to created date
if err := verification.VerifyCredentialProofDates(msgNewCredProof, false); err != nil {
return nil, err
return nil, sdkerrors.Wrap(types.ErrInvalidCredentialField, err.Error())
}

// Check if the created date lies between issuance and expiration
Expand Down Expand Up @@ -286,13 +286,13 @@ func (k msgServer) updateCredentialStatus(ctx sdk.Context, msg *types.MsgRegiste

credDocBytes, err := getClientSpecDocBytes(clientSpecOpts)
if err != nil {
return nil, err
return nil, sdkerrors.Wrap(types.ErrInvalidClientSpecType, err.Error())
}

// Verify Signature
err = k.VerifyDocumentProof(ctx, credDocBytes, msgNewCredProof)
if err != nil {
return nil, err
return nil, sdkerrors.Wrap(types.ErrInvalidSignature, err.Error())
}

cred := types.Credential{
Expand Down
10 changes: 5 additions & 5 deletions x/ssi/keeper/msg_server_deactivate_did.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (k msgServer) DeactivateDID(goCtx context.Context, msg *types.MsgDeactivate
// Get the DID Document from state
didDocumentState, err := k.GetDidDocumentState(&ctx, msgDidId)
if err != nil {
return nil, err
return nil, sdkerrors.Wrap(types.ErrDidDocNotFound, err.Error())
}
didDocument := didDocumentState.DidDocument
didDocumentMetadata := didDocumentState.DidDocumentMetadata
Expand All @@ -51,7 +51,7 @@ func (k msgServer) DeactivateDID(goCtx context.Context, msg *types.MsgDeactivate
// Gather controllers
controllers := getControllersForDeactivateDID(didDocument)
if err := k.checkControllerPresenceInState(ctx, controllers, didDocument.Id); err != nil {
return nil, err
return nil, sdkerrors.Wrap(types.ErrInvalidDidDoc, err.Error())
}

signMap := makeSignatureMap(msgSignatures)
Expand All @@ -60,7 +60,7 @@ func (k msgServer) DeactivateDID(goCtx context.Context, msg *types.MsgDeactivate
controllerMap, err := k.formAnyControllerVmListMap(ctx, controllers,
didDocument.VerificationMethod, signMap)
if err != nil {
return nil, err
return nil, sdkerrors.Wrap(types.ErrInvalidDidDoc, err.Error())
}

// Get Client Spec
Expand All @@ -72,13 +72,13 @@ func (k msgServer) DeactivateDID(goCtx context.Context, msg *types.MsgDeactivate
var didDocBytes []byte
didDocBytes, err = getClientSpecDocBytes(clientSpecOpts)
if err != nil {
return nil, err
return nil, sdkerrors.Wrapf(types.ErrInvalidClientSpecType, err.Error())
}

// Signature Verification
err = verification.VerifySignatureOfAnyController(didDocBytes, controllerMap)
if err != nil {
return nil, err
return nil, sdkerrors.Wrapf(types.ErrInvalidSignature, err.Error())
}

// Create updated metadata
Expand Down
4 changes: 2 additions & 2 deletions x/ssi/keeper/msg_server_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ func (k msgServer) CreateSchema(goCtx context.Context, msg *types.MsgCreateSchem

schemaDocBytes, err := getClientSpecDocBytes(clientSpecOpts)
if err != nil {
return nil, err
return nil, sdkerrors.Wrap(types.ErrInvalidClientSpecType, err.Error())
}

// Signature check
if err := k.VerifyDocumentProof(ctx, schemaDocBytes, schemaProof); err != nil {
return nil, err
return nil, sdkerrors.Wrap(types.ErrInvalidClientSpecType, err.Error())
}

var schema = types.Schema{
Expand Down
Loading

0 comments on commit be2da81

Please sign in to comment.