Files
sonr/x/did/keeper/context.go
T
Prad NukalaandGitHub 96e6486c43 feature/migrate models (#16)
* feat: add new supported attestation formats to genesis

* feat: refactor keyType to keytype enum

* refactor: remove unused imports and code

* refactor: update main.go to use src package

* refactor: move web-related structs from  to

* refactor: move client middleware package to root

* refactor: remove unused IndexedDB dependency

* feat: update worker implementation to use

* feat: add Caddyfile and Caddy configuration for vault service

* refactor(config): move keyshare and address to Motr config

* fix: validate service origin in AllocateVault

* chore: remove IndexedDB configuration

* feat: add support for IPNS-based vault access
2024-09-19 02:04:22 -04:00

95 lines
2.1 KiB
Go

package keeper
import (
"time"
"github.com/ipfs/kubo/client/rpc"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/onsonr/sonr/x/did/types"
"google.golang.org/grpc/peer"
)
type Context struct {
SDKCtx sdk.Context
Keeper Keeper
Peer *peer.Peer
}
// AverageBlockTime returns the average block time in seconds
func (c Context) AverageBlockTime() float64 {
return float64(c.SDK().BlockTime().Sub(c.SDK().BlockTime()).Seconds())
}
// GetExpirationBlockHeight returns the block height at which the given duration will have passed
func (c Context) CalculateExpiration(duration time.Duration) int64 {
return c.SDKCtx.BlockHeight() + int64(duration.Seconds()/c.AverageBlockTime())
}
// IPFSConnected returns true if the IPFS client is initialized
func (c Context) IPFSConnected() bool {
if c.Keeper.ipfsClient == nil {
ipfsClient, err := rpc.NewLocalApi()
if err != nil {
return false
}
c.Keeper.ipfsClient = ipfsClient
}
return c.Keeper.ipfsClient != nil
}
func (c Context) IsAnonymous() bool {
if c.Peer == nil {
return true
}
return c.Peer.Addr == nil
}
func (c Context) Params() *types.Params {
p, err := c.Keeper.Params.Get(c.SDK())
if err != nil {
p = types.DefaultParams()
}
params := p.ActiveParams(c.IPFSConnected())
return &params
}
func (c Context) PeerID() string {
if c.Peer == nil {
return ""
}
return c.Peer.Addr.String()
}
func (c Context) SDK() sdk.Context {
return c.SDKCtx
}
// ValidateOrigin checks if a service origin is valid
func (c Context) ValidateOrigin(origin string) error {
if origin == "localhost" {
return nil
}
return types.ErrInvalidServiceOrigin
}
// VerifyMinimumStake checks if a validator has a minimum stake
func (c Context) VerifyMinimumStake(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 := c.Keeper.StakingKeeper.GetDelegation(c.SDK(), address, addval)
if err != nil {
return false
}
if del.Shares.IsZero() {
return false
}
return del.Shares.IsPositive()
}