Files
sonr/x/vault/keeper/querier.go
T
Prad NukalaandGitHub 311b957550 feature/1118 formatter interface creation (#1147)
- **refactor: improve query service code structure**
- **chore(deps): update protoc-gen-go-grpc to v1.5.1**
- **refactor: replace  package with**
- **chore(deps): update dependencies**
- **fix(deps): update webauthn to v0.11.2**
- **refactor: remove onsonr.sonr from package names**
- **refactor: improve code readability in vault querier**
- **refactor: simplify controller initialization**
- **fix: remove unnecessary function for counter data**
- **refactor: update button component file paths**
- **refactor(authentication): simplify register page**
- **fix: update error filenames in marketing section templates**
2024-10-21 11:30:52 -04:00

121 lines
4.2 KiB
Go

package keeper
import (
"context"
"fmt"
"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"
)
var _ types.QueryServer = Querier{}
type Querier struct{ Keeper }
func NewQuerier(keeper Keeper) Querier {
return Querier{Keeper: keeper}
}
// ╭───────────────────────────────────────────────────────────╮
// │ Fixed Query Methods │
// ╰───────────────────────────────────────────────────────────╯
// Params implements types.QueryServer.
func (k Querier) Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
p, err := k.Keeper.Params.Get(ctx)
if err != nil {
return nil, err
}
return &types.QueryParamsResponse{Params: &p}, nil
}
// Schema implements types.QueryServer.
func (k Querier) Schema(goCtx context.Context, req *types.QuerySchemaRequest) (*types.QuerySchemaResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
p, err := k.Keeper.Params.Get(ctx)
if err != nil {
return nil, err
}
return &types.QuerySchemaResponse{
Schema: p.Schema,
}, nil
}
// ╭───────────────────────────────────────────────────────────╮
// │ Pre-Authenticated Queries │
// ╰───────────────────────────────────────────────────────────╯
// Allocate implements types.QueryServer.
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(fmt.Sprintf("Error getting current schema: %s", err.Error()))
return nil, types.ErrInvalidSchema.Wrap(err.Error())
}
shares, err := mpc.GenerateKeyshares()
if err != nil {
ctx.Logger().Error(fmt.Sprintf("Error generating keyshares: %s", err.Error()))
return nil, types.ErrInvalidSchema.Wrap(err.Error())
}
con, err := didtypes.NewController(shares)
if err != nil {
ctx.Logger().Error(fmt.Sprintf("Error creating controller: %s", err.Error()))
return nil, err
}
v, err := types.NewVault("", con.SonrAddress(), con.ChainID(), sch)
if err != nil {
ctx.Logger().Error(fmt.Sprintf("Error creating vault: %s", 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(fmt.Sprintf("Error adding to IPFS: %s", err.Error()))
return nil, types.ErrVaultAssembly.Wrap(err.Error())
}
return &types.QueryAllocateResponse{
Success: true,
Cid: cid.String(),
ExpiryBlock: calculateBlockExpiry(ctx, time.Second*30),
}, nil
}
// ╭───────────────────────────────────────────────────────────╮
// │ Authenticated Endpoints │
// ╰───────────────────────────────────────────────────────────╯
// Sync implements types.QueryServer.
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 {
return nil, err
}
c, _ := k.DIDKeeper.ResolveController(ctx, req.Did)
if c == nil {
return &types.QuerySyncResponse{
Success: false,
Schema: p.Schema,
ChainID: ctx.ChainID(),
}, nil
}
return &types.QuerySyncResponse{
Success: true,
Schema: p.Schema,
ChainID: ctx.ChainID(),
Address: c.SonrAddress(),
}, nil
}