sonr/x/did/keeper/keeper.go
Prad Nukala 4f2d342649
feature/ipfs vault allocation (#8)
* refactor: move constants to genesis.proto

* feat: add ipfs_active flag to genesis state

* feat: add IPFS connection initialization to keeper

* feat: add testnet process-compose

* refactor: rename sonr-testnet docker image to sonr-runner

* refactor: update docker-vm-release workflow to use 'latest' tag

* feat: add permission to workflows

* feat: add new service chain execution

* feat: add abstract vault class to pkl

* feat: use jetpackio/devbox image for runner

* feat: introduce dwn for local service worker

* refactor: remove unnecessary dockerfile layers

* refactor(deploy): Update Dockerfile to copy go.mod and go.sum from the parent directory

* build: move Dockerfile to root directory

* build: Add Dockerfile for deployment

* feat: Update Dockerfile to work with Go project in parent directory

* build: Update docker-compose.yaml to use relative paths

* feat: Update docker-compose to work with new image and parent git directory

* refactor: remove unnecessary test script

* <no value>

* feat: add test_node script for running node tests

* feat: add IPFS cluster to testnet

* feat: add docker image for sonr-runner

* fix: typo in export path

* feat(did): Add Localhost Registration Enabled Genesis Option

* feat: add support for Sqlite DB in vault

* feat: improve vault model JSON serialization

* feat: support querying HTMX endpoint for DID

* feat: Add primary key, unique, default, not null, auto increment, and foreign key field types

* feat: Add PublicKey model in pkl/vault.pkl

* feat: add frontend server

* refactor: move dwn.wasm to vfs directory

* feat(frontend): remove frontend server implementation

* feat: Add a frontend server and web auth protocol

* feat: implement new key types for MPC and ZK proofs

* fix: Update enum types and DefaultKeyInfos

* fix: correct typo in KeyAlgorithm enum

* feat(did): add attestation format validation

* feat: Add x/did/builder/extractor.go

* feat: Update JWK parsing in x/did/builder/extractor.go

* feat: Use github.com/onsonr/sonr/x/did/types package

* feat: Extract and format public keys from WebAuthn credentials

* feat: Introduce a new `mapToJWK` function to convert a map to a `types.JWK` struct

* feat: add support for extracting JWK public keys

* feat: remove VerificationMethod struct

* refactor: extract public key extraction logic

* feat: add helper functions to map COSECurveID to JWK curve names

* feat: pin initial vault
2024-09-07 18:12:58 -04:00

141 lines
3.3 KiB
Go

package keeper
import (
"cosmossdk.io/collections"
storetypes "cosmossdk.io/core/store"
"cosmossdk.io/log"
"cosmossdk.io/orm/model/ormdb"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
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"
"github.com/ipfs/kubo/client/rpc"
apiv1 "github.com/onsonr/sonr/api/did/v1"
middleware "github.com/onsonr/sonr/x/did/middleware"
"github.com/onsonr/sonr/x/did/types"
)
// Keeper defines the middleware keeper.
type Keeper struct {
cdc codec.BinaryCodec
logger log.Logger
// state management
OrmDB apiv1.StateStore
Params collections.Item[types.Params]
Schema collections.Schema
AccountKeeper authkeeper.AccountKeeper
StakingKeeper *stakkeeper.Keeper
authority string
ipfsClient *rpc.HttpApi
}
// NewKeeper creates a new poa Keeper instance
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 == "" {
authority = authtypes.NewModuleAddress(govtypes.ModuleName).String()
}
db, err := ormdb.NewModuleDB(
&types.ORMModuleSchema,
ormdb.ModuleDBOptions{KVStoreService: storeService},
)
if err != nil {
panic(err)
}
store, err := apiv1.NewStateStore(db)
if err != nil {
panic(err)
}
// Initialize IPFS client
ipfsClient, _ := rpc.NewLocalApi()
k := Keeper{
ipfsClient: ipfsClient,
cdc: cdc,
logger: logger,
Params: collections.NewItem(
sb,
types.ParamsKey,
"params",
codec.CollValue[types.Params](cdc),
),
authority: authority,
OrmDB: store,
AccountKeeper: accKeeper,
StakingKeeper: stkKeeper,
}
schema, err := sb.Build()
if err != nil {
panic(err)
}
k.Schema = schema
return k
}
// IsClaimedServiceOrigin checks if a service origin is unclaimed
func (k Keeper) IsUnclaimedServiceOrigin(ctx sdk.Context, origin string) bool {
rec, _ := k.OrmDB.ServiceRecordTable().GetByOriginUri(ctx, origin)
return rec == nil
}
// IsValidServiceOrigin checks if a service origin is valid
func (k Keeper) IsValidServiceOrigin(ctx sdk.Context, origin string, clientInfo *middleware.ClientInfo) bool {
if origin != clientInfo.Hostname {
return false
}
rec, err := k.OrmDB.ServiceRecordTable().GetByOriginUri(ctx, origin)
if err != nil {
return false
}
if rec == nil {
return false
}
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
}