feat: add initial DID implementation

This commit is contained in:
Prad Nukala
2024-09-05 18:37:38 -04:00
parent 3fd84dbdda
commit b3b176a25d
35 changed files with 1073 additions and 14 deletions
+7 -9
View File
@@ -3,19 +3,17 @@ package module
import (
"os"
"github.com/cosmos/cosmos-sdk/codec"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/store"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
"github.com/cosmos/cosmos-sdk/codec"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
modulev1 "github.com/onsonr/sonr/api/did/module/v1"
"github.com/onsonr/sonr/x/did/keeper"
@@ -58,7 +56,7 @@ type ModuleOutputs struct {
func ProvideModule(in ModuleInputs) ModuleOutputs {
govAddr := authtypes.NewModuleAddress(govtypes.ModuleName).String()
k := keeper.NewKeeper(in.Cdc, in.StoreService, in.AccountKeeper, log.NewLogger(os.Stderr), govAddr)
k := keeper.NewKeeper(in.Cdc, in.StoreService, in.AccountKeeper, &in.StakingKeeper, log.NewLogger(os.Stderr), govAddr)
m := NewAppModule(in.Cdc, k)
return ModuleOutputs{Module: m, Keeper: k, Out: depinject.Out{}}
+1
View File
@@ -0,0 +1 @@
package keeper
+4 -1
View File
@@ -9,6 +9,7 @@ import (
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
stakkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
apiv1 "github.com/onsonr/sonr/api/did/v1"
"github.com/onsonr/sonr/x/did/types"
@@ -26,12 +27,13 @@ type Keeper struct {
Schema collections.Schema
AccountKeeper authkeeper.AccountKeeper
StakingKeeper *stakkeeper.Keeper
authority string
}
// NewKeeper creates a new poa Keeper instance
func NewKeeper(cdc codec.BinaryCodec, storeService storetypes.KVStoreService, accKeeper authkeeper.AccountKeeper, logger log.Logger, authority string) Keeper {
func NewKeeper(cdc codec.BinaryCodec, storeService storetypes.KVStoreService, accKeeper authkeeper.AccountKeeper, stkKeeper *stakkeeper.Keeper, logger log.Logger, authority string) Keeper {
logger = logger.With(log.ModuleKey, "x/"+types.ModuleName)
sb := collections.NewSchemaBuilder(storeService)
if authority == "" {
@@ -52,6 +54,7 @@ func NewKeeper(cdc codec.BinaryCodec, storeService storetypes.KVStoreService, ac
authority: authority,
OrmDB: store,
AccountKeeper: accKeeper,
StakingKeeper: stkKeeper,
}
schema, err := sb.Build()
if err != nil {
+57 -1
View File
@@ -1,7 +1,38 @@
package keeper
import sdk "github.com/cosmos/cosmos-sdk/types"
import (
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// CheckValidatorExists checks if a validator exists
func (k Keeper) CheckValidatorExists(ctx sdk.Context, addr string) bool {
address, err := sdk.ValAddressFromBech32(addr)
if err != nil {
return false
}
ok, err := k.StakingKeeper.Validator(ctx, address)
if err != nil {
return false
}
if ok != nil {
return true
}
return false
}
// GetAverageBlockTime returns the average block time in seconds
func (k Keeper) GetAverageBlockTime(ctx sdk.Context) float64 {
return float64(ctx.BlockTime().Sub(ctx.BlockTime()).Seconds())
}
// GetExpirationBlockHeight returns the block height at which the given duration will have passed
func (k Keeper) GetExpirationBlockHeight(ctx sdk.Context, duration time.Duration) int64 {
return ctx.BlockHeight() + int64(duration.Seconds()/k.GetAverageBlockTime(ctx))
}
// ValidServiceOrigin checks if a service origin is valid
func (k Keeper) ValidServiceOrigin(ctx sdk.Context, origin string) bool {
rec, err := k.OrmDB.ServiceRecordTable().GetByOriginUri(ctx, origin)
if err != nil {
@@ -12,3 +43,28 @@ func (k Keeper) ValidServiceOrigin(ctx sdk.Context, origin string) bool {
}
return true
}
// VerifyMinimumStake checks if a validator has a minimum stake
func (k Keeper) VerifyMinimumStake(ctx sdk.Context, addr string) bool {
address, err := sdk.AccAddressFromBech32(addr)
if err != nil {
return false
}
addval, err := sdk.ValAddressFromBech32(addr)
if err != nil {
return false
}
del, err := k.StakingKeeper.GetDelegation(ctx, address, addval)
if err != nil {
return false
}
if del.Shares.IsZero() {
return false
}
return del.Shares.IsPositive()
}
// VerifyServicePermissions checks if a service has permission
func (k Keeper) VerifyServicePermissions(ctx sdk.Context, addr string, service string, permissions string) bool {
return false
}