refactor: remove unnecessary proxy config

This commit is contained in:
Prad Nukala
2024-09-26 13:52:03 -04:00
parent 6aacee387f
commit a190e1eac6
37 changed files with 298 additions and 353 deletions
-59
View File
@@ -1,59 +0,0 @@
package keeper
import (
"context"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/onsonr/sonr/config/dwn"
vault "github.com/onsonr/sonr/x/vault/internal"
"github.com/onsonr/sonr/x/vault/types"
)
// AssembleVault assembles the initial vault
func (k Keeper) AssembleVault(ctx sdk.Context, subject string, origin string) (string, int64, error) {
_, con, err := k.DIDKeeper.NewController(ctx)
if err != nil {
return "", 0, err
}
usrKs, err := con.ExportUserKs()
if err != nil {
return "", 0, err
}
sch, err := k.CurrentSchema(ctx)
if err != nil {
return "", 0, err
}
cnfg := vault.NewConfig(usrKs, con.SonrAddress(), con.ChainID(), sch)
v, err := types.NewVault(cnfg, "sonr-testnet")
if err != nil {
return "", 0, err
}
cid, err := k.ipfsClient.Unixfs().Add(context.Background(), v.FS)
if err != nil {
return "", 0, err
}
return cid.String(), k.CalculateExpiration(ctx, time.Second*15), nil
}
// CurrentSchema returns the current schema
func (k Keeper) CurrentSchema(ctx sdk.Context) (*dwn.Schema, error) {
p, err := k.Params.Get(ctx)
if err != nil {
return nil, err
}
schema := p.Schema
return &dwn.Schema{
Version: int(schema.Version),
Account: schema.Account,
Asset: schema.Asset,
Chain: schema.Chain,
Credential: schema.Credential,
Jwk: schema.Jwk,
Grant: schema.Grant,
Keyshare: schema.Keyshare,
PublicKey: schema.PublicKey,
Profile: schema.Profile,
}, nil
}
+81
View File
@@ -0,0 +1,81 @@
package keeper
import (
"context"
"time"
"cosmossdk.io/log"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ipfs/boxo/path"
"github.com/ipfs/kubo/client/rpc"
"github.com/onsonr/sonr/x/vault/types"
)
func (k Keeper) Logger() log.Logger {
return k.logger
}
// InitGenesis initializes the module's state from a genesis state.
func (k *Keeper) InitGenesis(ctx context.Context, data *types.GenesisState) error {
// this line is used by starport scaffolding # genesis/module/init
if err := data.Params.Validate(); err != nil {
return err
}
return k.Params.Set(ctx, data.Params)
}
// ExportGenesis exports the module's state to a genesis state.
func (k *Keeper) ExportGenesis(ctx context.Context) *types.GenesisState {
params, err := k.Params.Get(ctx)
if err != nil {
panic(err)
}
// this line is used by starport scaffolding # genesis/module/export
return &types.GenesisState{
Params: params,
}
}
// IPFSConnected returns true if the IPFS client is initialized
func (c Keeper) IPFSConnected() bool {
if c.ipfsClient == nil {
ipfsClient, err := rpc.NewLocalApi()
if err != nil {
return false
}
c.ipfsClient = ipfsClient
}
return c.ipfsClient != nil
}
// CalculateExpiration calculates the expiration time for a vault
func (k Keeper) CalculateExpiration(c sdk.Context, duration time.Duration) int64 {
blockTime := c.BlockTime()
avgBlockTime := float64(blockTime.Sub(blockTime).Seconds())
return int64(duration.Seconds() / avgBlockTime)
}
// HasPathInIPFS checks if a file is in the local IPFS node
func (k Keeper) HasPathInIPFS(ctx sdk.Context, cid string) (bool, error) {
path, err := path.NewPath(cid)
if err != nil {
return false, err
}
v, err := k.ipfsClient.Unixfs().Get(ctx, path)
if err != nil {
return false, err
}
if v == nil {
return false, nil
}
return true, nil
}
// validateSubjectOrigin checks if the subject and origin are valid
func (k Keeper) validateSubjectOrigin(ctx sdk.Context, subject string, origin string) error {
return nil
}
+36 -54
View File
@@ -15,9 +15,9 @@ import (
"cosmossdk.io/log"
"cosmossdk.io/orm/model/ormdb"
"github.com/ipfs/boxo/path"
"github.com/ipfs/kubo/client/rpc"
apiv1 "github.com/onsonr/sonr/api/vault/v1"
"github.com/onsonr/sonr/config/dwn"
"github.com/onsonr/sonr/x/vault/types"
@@ -89,66 +89,48 @@ func NewKeeper(
return k
}
func (k Keeper) Logger() log.Logger {
return k.logger
}
// InitGenesis initializes the module's state from a genesis state.
func (k *Keeper) InitGenesis(ctx context.Context, data *types.GenesisState) error {
// this line is used by starport scaffolding # genesis/module/init
if err := data.Params.Validate(); err != nil {
return err
}
return k.Params.Set(ctx, data.Params)
}
// ExportGenesis exports the module's state to a genesis state.
func (k *Keeper) ExportGenesis(ctx context.Context) *types.GenesisState {
params, err := k.Params.Get(ctx)
// currentSchema returns the current schema
func (k Keeper) CurrentSchema(ctx sdk.Context) (*dwn.Schema, error) {
p, err := k.Params.Get(ctx)
if err != nil {
panic(err)
}
// this line is used by starport scaffolding # genesis/module/export
return &types.GenesisState{
Params: params,
return nil, err
}
schema := p.Schema
return &dwn.Schema{
Version: int(schema.Version),
Account: schema.Account,
Asset: schema.Asset,
Chain: schema.Chain,
Credential: schema.Credential,
Jwk: schema.Jwk,
Grant: schema.Grant,
Keyshare: schema.Keyshare,
PublicKey: schema.PublicKey,
Profile: schema.Profile,
}, nil
}
// IPFSConnected returns true if the IPFS client is initialized
func (c Keeper) IPFSConnected() bool {
if c.ipfsClient == nil {
ipfsClient, err := rpc.NewLocalApi()
if err != nil {
return false
}
c.ipfsClient = ipfsClient
}
return c.ipfsClient != nil
}
// CalculateExpiration calculates the expiration time for a vault
func (k Keeper) CalculateExpiration(c sdk.Context, duration time.Duration) int64 {
blockTime := c.BlockTime()
avgBlockTime := float64(blockTime.Sub(blockTime).Seconds())
return int64(duration.Seconds() / avgBlockTime)
}
// HasPathInIPFS checks if a file is in the local IPFS node
func (k Keeper) HasPathInIPFS(ctx sdk.Context, cid string) (bool, error) {
path, err := path.NewPath(cid)
// assembleVault assembles the initial vault
func (k Keeper) AssembleVault(ctx sdk.Context) (string, int64, error) {
_, con, err := k.DIDKeeper.NewController(ctx)
if err != nil {
return false, err
return "", 0, err
}
v, err := k.ipfsClient.Unixfs().Get(ctx, path)
usrKs, err := con.ExportUserKs()
if err != nil {
return false, err
return "", 0, err
}
if v == nil {
return false, nil
sch, err := k.CurrentSchema(ctx)
if err != nil {
return "", 0, err
}
return true, nil
v, err := types.NewVault(usrKs, con.SonrAddress(), con.ChainID(), sch)
if err != nil {
return "", 0, err
}
cid, err := k.ipfsClient.Unixfs().Add(context.Background(), v.FS)
if err != nil {
return "", 0, err
}
return cid.String(), k.CalculateExpiration(ctx, time.Second*15), nil
}
@@ -34,8 +34,8 @@ func (ms msgServer) AllocateVault(goCtx context.Context, msg *types.MsgAllocateV
// 1.Check if the service origin is valid
ctx := sdk.UnwrapSDKContext(goCtx)
// 2.Allocate the vault
cid, expiryBlock, err := ms.k.AssembleVault(ctx, msg.GetSubject(), msg.GetOrigin())
// 2.Allocate the vault msg.GetSubject(), msg.GetOrigin()
cid, expiryBlock, err := ms.k.AssembleVault(ctx)
if err != nil {
return nil, err
}