Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic Update for Expired Credential Statuses #230

Merged
merged 1 commit into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
"github.com/cosmos/cosmos-sdk/x/authz"
authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper"
authzmodule "github.com/cosmos/cosmos-sdk/x/authz/module"
"github.com/cosmos/cosmos-sdk/x/bank"
Expand Down Expand Up @@ -415,7 +416,7 @@ func New(
app.mm.SetOrderBeginBlockers(
upgradetypes.ModuleName, capabilitytypes.ModuleName, minttypes.ModuleName, distrtypes.ModuleName, slashingtypes.ModuleName,
evidencetypes.ModuleName, stakingtypes.ModuleName, ibchost.ModuleName,
feegrant.ModuleName,
feegrant.ModuleName, authz.ModuleName, ssimoduletypes.ModuleName,
)

app.mm.SetOrderEndBlockers(crisistypes.ModuleName, govtypes.ModuleName, stakingtypes.ModuleName)
Expand Down
15 changes: 15 additions & 0 deletions x/ssi/abci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ssi

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/hypersign-protocol/hid-node/x/ssi/keeper"
)

// BeginBlocker is called at the beginning of every block
func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
// Set all the credential status that have passed their expiration date
// to Expired
if err := k.SetCredentialStatusToExpired(ctx); err != nil {
panic(err)
}
}
37 changes: 33 additions & 4 deletions x/ssi/keeper/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"encoding/binary"
"time"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -10,14 +11,12 @@ import (
)

func (k Keeper) RegisterCred(ctx sdk.Context, cred *types.Credential) uint64 {
// Get Cred count
count := k.GetCredentialCount(ctx)
// Get the store
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.CredKey))
// Get ID from DID Doc

id := cred.GetClaim().GetId()
// Marshal the DID into bytes
credBytes := k.cdc.MustMarshal(cred)

store.Set([]byte(id), credBytes)
k.SetCredentialCount(ctx, count+1)
return count
Expand Down Expand Up @@ -58,3 +57,33 @@ func (k Keeper) SetCredentialCount(ctx sdk.Context, count uint64) {
binary.BigEndian.PutUint64(bz, count)
store.Set(byteKey, bz)
}

func (k Keeper) SetCredentialStatusToExpired(ctx sdk.Context) error {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.CredKey))
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
var cred types.Credential
if err := k.cdc.Unmarshal(iterator.Value(), &cred); err != nil {
return err
}

currentBlockTime := ctx.BlockTime()
expirationDate, err := time.Parse(time.RFC3339, cred.GetExpirationDate())
if err != nil {
return err
}

// Set the Credential Status to Expired
if currentBlockTime.After(expirationDate) {
cred.Claim.CurrentStatus = "Expired"
cred.Claim.StatusReason = "Credential Expired"
cred.Proof.Updated = currentBlockTime.Format(time.RFC3339)

updatedCredBytes := k.cdc.MustMarshal(&cred)
store.Set([]byte(cred.Claim.Id), updatedCredBytes)
}
}
return nil
}
1 change: 0 additions & 1 deletion x/ssi/keeper/msg_server_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ func (k msgServer) updateCredentialStatus(ctx sdk.Context, newCredStatus *types.
fmt.Sprintf("credential is already %s", newClaimStatus))
}

// TODO: Status change to Expired should happend automatically within hid-node
case "Revoked", "Expired":
if newClaimStatus == oldClaimStatus {
return nil, sdkerrors.Wrapf(
Expand Down
4 changes: 3 additions & 1 deletion x/ssi/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
func (AppModule) ConsensusVersion() uint64 { return 2 }

// BeginBlock executes all ABCI BeginBlock logic respective to the capability module.
func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}
func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
BeginBlocker(ctx, am.keeper)
}

// EndBlock executes all ABCI EndBlock logic respective to the capability module. It
// returns no validator updates.
Expand Down