refactor: update gRPC client to use new request types

This commit is contained in:
Prad Nukala
2024-10-18 15:10:09 -04:00
parent a0a5f0c7e0
commit 3ad6036eb8
13 changed files with 793 additions and 1055 deletions
-57
View File
@@ -1,57 +0,0 @@
package keeper
import (
"context"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
dwngen "github.com/onsonr/sonr/internal/dwn/gen"
"github.com/onsonr/sonr/x/vault/types"
)
// assembleVault assembles the initial vault
func (k Keeper) assembleVault(cotx sdk.Context, schema *dwngen.Schema) (string, int64, error) {
_, con, err := k.DIDKeeper.NewController(cotx)
if err != nil {
return "", 0, err
}
usrKs, err := con.ExportUserKs()
if err != nil {
return "", 0, err
}
v, err := types.NewVault(usrKs, con.SonrAddress(), con.ChainID(), schema)
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(), calculateBlockExpiry(cotx, time.Second*30), nil
}
// currentSchema returns the current schema
func (k Keeper) currentSchema(ctx sdk.Context) (*dwngen.Schema, error) {
p, err := k.Params.Get(ctx)
if err != nil {
return nil, err
}
schema := p.Schema
return &dwngen.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,
Profile: schema.Profile,
}, nil
}
func calculateBlockExpiry(sdkctx sdk.Context, duration time.Duration) int64 {
blockTime := sdkctx.BlockTime()
avgBlockTime := float64(blockTime.Sub(blockTime).Seconds())
return int64(duration.Seconds() / avgBlockTime)
}
+37 -3
View File
@@ -1,17 +1,21 @@
package keeper
import (
"time"
"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"
"github.com/ipfs/kubo/client/rpc"
apiv1 "github.com/onsonr/sonr/api/vault/v1"
dwngen "github.com/onsonr/sonr/internal/dwn/gen"
didkeeper "github.com/onsonr/sonr/x/did/keeper"
macaroonkeeper "github.com/onsonr/sonr/x/macaroon/keeper"
"github.com/onsonr/sonr/x/vault/types"
@@ -43,7 +47,7 @@ func NewKeeper(
logger log.Logger,
authority string,
authKeeper authkeeper.AccountKeeper,
didk didkeeper.Keeper,
didKeeper didkeeper.Keeper,
macaroonKeeper macaroonkeeper.Keeper,
) Keeper {
logger = logger.With(log.ModuleKey, "x/"+types.ModuleName)
@@ -64,11 +68,15 @@ func NewKeeper(
panic(err)
}
ipfsClient, _ := rpc.NewLocalApi()
ipfsClient, err := rpc.NewLocalApi()
if err != nil {
panic(err)
}
k := Keeper{
cdc: cdc,
logger: logger,
DIDKeeper: didk,
DIDKeeper: didKeeper,
MacaroonKeeper: macaroonKeeper,
AccountKeeper: authKeeper,
Params: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)),
@@ -87,3 +95,29 @@ func NewKeeper(
return k
}
// currentSchema returns the current schema
func (k Keeper) currentSchema(ctx sdk.Context) (*dwngen.Schema, error) {
p, err := k.Params.Get(ctx)
if err != nil {
return nil, err
}
schema := p.Schema
return &dwngen.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,
Profile: schema.Profile,
}, nil
}
func calculateBlockExpiry(sdkctx sdk.Context, duration time.Duration) int64 {
blockTime := sdkctx.BlockTime()
avgBlockTime := float64(blockTime.Sub(blockTime).Seconds())
return int64(duration.Seconds() / avgBlockTime)
}
+34 -10
View File
@@ -2,8 +2,11 @@ package keeper
import (
"context"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/onsonr/crypto/mpc"
didtypes "github.com/onsonr/sonr/x/did/types"
"github.com/onsonr/sonr/x/vault/types"
)
@@ -50,25 +53,46 @@ func (k Querier) Schema(goCtx context.Context, req *types.QuerySchemaRequest) (*
// ╰───────────────────────────────────────────────────────────╯
// Allocate implements types.QueryServer.
func (k Querier) Allocate(goCtx context.Context, req *types.AllocateRequest) (*types.AllocateResponse, error) {
func (k Querier) Allocate(goCtx context.Context, req *types.QueryAllocateRequest) (*types.QueryAllocateResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// 1. Get current schema
sch, err := k.currentSchema(ctx)
if err != nil {
ctx.Logger().Error(err.Error())
return nil, types.ErrInvalidSchema.Wrap(err.Error())
}
// 2.Allocate the vault msg.GetSubject(), msg.GetOrigin()
cid, expiryBlock, err := k.assembleVault(ctx, sch)
shares, err := mpc.GenerateKeyshares()
if err != nil {
ctx.Logger().Error(err.Error())
return nil, err
}
con, err := didtypes.NewController(ctx, shares)
if err != nil {
ctx.Logger().Error(err.Error())
return nil, err
}
usrKs, err := con.ExportUserKs()
if err != nil {
ctx.Logger().Error(err.Error())
return nil, types.ErrInvalidSchema.Wrap(err.Error())
}
v, err := types.NewVault(usrKs, con.SonrAddress(), con.ChainID(), sch)
if err != nil {
ctx.Logger().Error(err.Error())
return nil, types.ErrInvalidSchema.Wrap(err.Error())
}
cid, err := k.ipfsClient.Unixfs().Add(context.Background(), v.FS)
if err != nil {
ctx.Logger().Error(err.Error())
return nil, types.ErrVaultAssembly.Wrap(err.Error())
}
return &types.AllocateResponse{
return &types.QueryAllocateResponse{
Success: true,
Cid: cid,
ExpiryBlock: expiryBlock,
Cid: cid.String(),
ExpiryBlock: calculateBlockExpiry(ctx, time.Second*30),
}, nil
}
@@ -77,7 +101,7 @@ func (k Querier) Allocate(goCtx context.Context, req *types.AllocateRequest) (*t
// ╰───────────────────────────────────────────────────────────╯
// Sync implements types.QueryServer.
func (k Querier) Sync(goCtx context.Context, req *types.SyncRequest) (*types.SyncResponse, error) {
func (k Querier) Sync(goCtx context.Context, req *types.QuerySyncRequest) (*types.QuerySyncResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
p, err := k.Keeper.Params.Get(ctx)
if err != nil {
@@ -85,13 +109,13 @@ func (k Querier) Sync(goCtx context.Context, req *types.SyncRequest) (*types.Syn
}
c, _ := k.DIDKeeper.ResolveController(ctx, req.Did)
if c == nil {
return &types.SyncResponse{
return &types.QuerySyncResponse{
Success: false,
Schema: p.Schema,
ChainID: ctx.ChainID(),
}, nil
}
return &types.SyncResponse{
return &types.QuerySyncResponse{
Success: true,
Schema: p.Schema,
ChainID: ctx.ChainID(),