mirror of
https://github.com/sonr-io/sonr.git
synced 2026-08-04 18:31:41 +00:00
feature/did accounts (#23)
* feat: add support for DID number as primary key for Controllers * refactor: rename pkg/proxy to app/proxy * feat: add vault module keeper tests * feat(vault): add DID keeper to vault module * refactor: move vault client code to its own package * refactor(vault): extract schema definition * refactor: use vaulttypes for MsgAllocateVault * refactor: update vault assembly logic to use new methods * feat: add dwn-proxy command * refactor: remove unused context.go file * refactor: remove unused web-related code * feat: add DWN proxy server * feat: add BuildTx RPC to vault module * fix: Implement BuildTx endpoint * feat: add devbox integration to project
This commit is contained in:
@@ -1,165 +0,0 @@
|
||||
package keeper
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
nftkeeper "cosmossdk.io/x/nft/keeper"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/ipfs/boxo/path"
|
||||
"github.com/ipfs/kubo/client/rpc"
|
||||
"github.com/ipfs/kubo/core/coreiface/options"
|
||||
"github.com/onsonr/sonr/pkg/vault"
|
||||
"github.com/onsonr/sonr/x/did/types"
|
||||
"google.golang.org/grpc/peer"
|
||||
"gopkg.in/macaroon.v2"
|
||||
)
|
||||
|
||||
func (k Keeper) UnwrapCtx(goCtx context.Context) Context {
|
||||
ctx := sdk.UnwrapSDKContext(goCtx)
|
||||
peer, _ := peer.FromContext(goCtx)
|
||||
return Context{SDKCtx: ctx, Peer: peer, Keeper: k}
|
||||
}
|
||||
|
||||
type Context struct {
|
||||
SDKCtx sdk.Context
|
||||
Keeper Keeper
|
||||
Peer *peer.Peer
|
||||
NFTKeeper nftkeeper.Keeper
|
||||
}
|
||||
|
||||
// AssembleVault assembles the initial vault
|
||||
func (k Keeper) AssembleVault(ctx Context, subject string, origin string) (string, int64, error) {
|
||||
v, err := vault.New(subject, origin, "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(), ctx.CalculateExpiration(time.Second * 15), nil
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// IssueMacaroon creates a macaroon with the specified parameters.
|
||||
func (c Context) IssueMacaroon(sharedMPCPubKey, location, id string, blockExpiry uint64) (*macaroon.Macaroon, error) {
|
||||
// Derive the root key by hashing the shared MPC public key
|
||||
rootKey := sha256.Sum256([]byte(sharedMPCPubKey))
|
||||
// Create the macaroon
|
||||
m, err := macaroon.New(rootKey[:], []byte(id), location, macaroon.LatestVersion)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Add the block expiry caveat
|
||||
caveat := fmt.Sprintf("block-expiry=%d", blockExpiry)
|
||||
err = m.AddFirstPartyCaveat([]byte(caveat))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return m, 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 ¶ms
|
||||
}
|
||||
|
||||
func (c Context) PeerID() string {
|
||||
if c.Peer == nil {
|
||||
return ""
|
||||
}
|
||||
return c.Peer.Addr.String()
|
||||
}
|
||||
|
||||
// PinVaultController pins the initial vault to the local IPFS node
|
||||
func (k Keeper) PinVaultController(_ sdk.Context, cid string, address string) (bool, error) {
|
||||
// Resolve the path
|
||||
path, err := path.NewPath(cid)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
// 1. Initialize vault.db sqlite database in local IPFS with Mount
|
||||
|
||||
// 2. Insert the InitialWalletAccounts
|
||||
|
||||
// 3. Publish the path to the IPNS
|
||||
_, err = k.ipfsClient.Name().Publish(context.Background(), path, options.Name.Key(address))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
// 4. Insert the accounts into x/auth
|
||||
|
||||
// 5. Insert the controller into state
|
||||
return true, nil
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package keeper
|
||||
|
||||
import (
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/onsonr/crypto/mpc"
|
||||
"github.com/onsonr/sonr/x/did/types"
|
||||
)
|
||||
|
||||
func (k Keeper) NewController(ctx sdk.Context) (uint64, types.ControllerI, error) {
|
||||
shares, err := mpc.GenerateKeyshares()
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
controller, err := types.NewController(shares)
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
entry, err := controller.GetTableEntry()
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
num, err := k.OrmDB.ControllerTable().InsertReturningNumber(ctx, entry)
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
return num, controller, nil
|
||||
}
|
||||
+6
-32
@@ -4,9 +4,8 @@ import (
|
||||
"context"
|
||||
|
||||
"cosmossdk.io/log"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/ipfs/boxo/path"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/onsonr/sonr/x/did/types"
|
||||
)
|
||||
|
||||
@@ -32,41 +31,16 @@ func (k *Keeper) ExportGenesis(ctx context.Context) *types.GenesisState {
|
||||
}
|
||||
|
||||
// this line is used by starport scaffolding # genesis/module/export
|
||||
|
||||
return &types.GenesisState{
|
||||
Params: params,
|
||||
}
|
||||
}
|
||||
|
||||
// CheckValidatorExists checks if a validator exists
|
||||
func (k Keeper) CheckValidatorExists(ctx sdk.Context, addr string) bool {
|
||||
address, err := sdk.ValAddressFromBech32(addr)
|
||||
// CurrentSchema returns the current schema
|
||||
func (k Keeper) CurrentParams(ctx sdk.Context) (*types.Params, error) {
|
||||
p, err := k.Params.Get(ctx)
|
||||
if err != nil {
|
||||
return false
|
||||
return nil, err
|
||||
}
|
||||
ok, err := k.StakingKeeper.Validator(ctx, address)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if ok != nil {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// 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
|
||||
return &p, nil
|
||||
}
|
||||
|
||||
+3
-18
@@ -7,13 +7,11 @@ import (
|
||||
"cosmossdk.io/orm/model/ormdb"
|
||||
nftkeeper "cosmossdk.io/x/nft/keeper"
|
||||
"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"
|
||||
"github.com/onsonr/sonr/x/did/types"
|
||||
@@ -34,8 +32,7 @@ type Keeper struct {
|
||||
NftKeeper nftkeeper.Keeper
|
||||
StakingKeeper *stakkeeper.Keeper
|
||||
|
||||
authority string
|
||||
ipfsClient *rpc.HttpApi
|
||||
authority string
|
||||
}
|
||||
|
||||
// NewKeeper creates a new poa Keeper instance
|
||||
@@ -66,11 +63,9 @@ func NewKeeper(
|
||||
}
|
||||
|
||||
// Initialize IPFS client
|
||||
ipfsClient, _ := rpc.NewLocalApi()
|
||||
k := Keeper{
|
||||
ipfsClient: ipfsClient,
|
||||
cdc: cdc,
|
||||
logger: logger,
|
||||
cdc: cdc,
|
||||
logger: logger,
|
||||
Params: collections.NewItem(
|
||||
sb,
|
||||
types.ParamsKey,
|
||||
@@ -91,13 +86,3 @@ func NewKeeper(
|
||||
k.Schema = schema
|
||||
return k
|
||||
}
|
||||
|
||||
// VerifyServicePermissions checks if a service has permission
|
||||
func (k Keeper) VerifyServicePermissions(
|
||||
ctx sdk.Context,
|
||||
addr string,
|
||||
service string,
|
||||
permissions string,
|
||||
) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package keeper
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"gopkg.in/macaroon.v2"
|
||||
)
|
||||
|
||||
// IssueMacaroon creates a macaroon with the specified parameters.
|
||||
func (k Keeper) IssueMacaroon(ctx sdk.Context, sharedMPCPubKey, location, id string, blockExpiry uint64) (*macaroon.Macaroon, error) {
|
||||
// Derive the root key by hashing the shared MPC public key
|
||||
rootKey := sha256.Sum256([]byte(sharedMPCPubKey))
|
||||
// Create the macaroon
|
||||
m, err := macaroon.New(rootKey[:], []byte(id), location, macaroon.LatestVersion)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Add the block expiry caveat
|
||||
caveat := fmt.Sprintf("block-expiry=%d", blockExpiry)
|
||||
err = m.AddFirstPartyCaveat([]byte(caveat))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
+9
-17
@@ -3,6 +3,7 @@ package keeper
|
||||
import (
|
||||
"context"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/onsonr/sonr/x/did/types"
|
||||
)
|
||||
|
||||
@@ -17,25 +18,16 @@ func NewQuerier(keeper Keeper) Querier {
|
||||
}
|
||||
|
||||
// Params returns the total set of did parameters.
|
||||
func (k Querier) Params(
|
||||
goCtx context.Context,
|
||||
req *types.QueryRequest,
|
||||
) (*types.QueryParamsResponse, error) {
|
||||
ctx := k.UnwrapCtx(goCtx)
|
||||
return &types.QueryParamsResponse{Params: ctx.Params()}, nil
|
||||
func (k Querier) Params(goCtx context.Context, req *types.QueryRequest) (*types.QueryParamsResponse, error) {
|
||||
ctx := sdk.UnwrapSDKContext(goCtx)
|
||||
p, err := k.Keeper.CurrentParams(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &types.QueryParamsResponse{Params: p}, nil
|
||||
}
|
||||
|
||||
// Resolve implements types.QueryServer.
|
||||
func (k Querier) Resolve(
|
||||
goCtx context.Context,
|
||||
req *types.QueryRequest,
|
||||
) (*types.QueryResolveResponse, error) {
|
||||
_ = k.UnwrapCtx(goCtx)
|
||||
func (k Querier) Resolve(goCtx context.Context, req *types.QueryRequest) (*types.QueryResolveResponse, error) {
|
||||
return &types.QueryResolveResponse{}, nil
|
||||
}
|
||||
|
||||
// Sync implements types.QueryServer.
|
||||
func (k Querier) Sync(goCtx context.Context, req *types.SyncRequest) (*types.SyncResponse, error) {
|
||||
// ctx := sdk.UnwrapSDKContext(goCtx)
|
||||
return &types.SyncResponse{}, nil
|
||||
}
|
||||
|
||||
+3
-47
@@ -21,50 +21,14 @@ func NewMsgServerImpl(keeper Keeper) types.MsgServer {
|
||||
return &msgServer{k: keeper}
|
||||
}
|
||||
|
||||
// # AllocateVault
|
||||
//
|
||||
// AllocateVault implements types.MsgServer.
|
||||
func (ms msgServer) AllocateVault(
|
||||
goCtx context.Context,
|
||||
msg *types.MsgAllocateVault,
|
||||
) (*types.MsgAllocateVaultResponse, error) {
|
||||
// 1.Check if the service origin is valid
|
||||
ctx := ms.k.UnwrapCtx(goCtx)
|
||||
if err := ctx.ValidateOrigin(msg.Origin); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 2.Allocate the vault
|
||||
cid, expiryBlock, err := ms.k.AssembleVault(ctx, msg.GetSubject(), msg.GetOrigin())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 3.Return the response
|
||||
return &types.MsgAllocateVaultResponse{
|
||||
ExpiryBlock: expiryBlock,
|
||||
Cid: cid,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// # RegisterController
|
||||
//
|
||||
// RegisterController implements types.MsgServer.
|
||||
func (ms msgServer) RegisterController(
|
||||
goCtx context.Context,
|
||||
msg *types.MsgRegisterController,
|
||||
) (*types.MsgRegisterControllerResponse, error) {
|
||||
func (ms msgServer) RegisterController(goCtx context.Context, msg *types.MsgRegisterController) (*types.MsgRegisterControllerResponse, error) {
|
||||
_ = sdk.UnwrapSDKContext(goCtx)
|
||||
return &types.MsgRegisterControllerResponse{}, nil
|
||||
}
|
||||
|
||||
// # RegisterService
|
||||
//
|
||||
// RegisterService implements types.MsgServer.
|
||||
func (ms msgServer) RegisterService(
|
||||
goCtx context.Context,
|
||||
msg *types.MsgRegisterService,
|
||||
) (*types.MsgRegisterServiceResponse, error) {
|
||||
func (ms msgServer) RegisterService(goCtx context.Context, msg *types.MsgRegisterService) (*types.MsgRegisterServiceResponse, error) {
|
||||
// ctx := sdk.UnwrapSDKContext(goCtx)
|
||||
|
||||
// 1.Check if the service origin is valid
|
||||
@@ -74,8 +38,6 @@ func (ms msgServer) RegisterService(
|
||||
return nil, errors.Wrapf(types.ErrInvalidServiceOrigin, "invalid service origin")
|
||||
}
|
||||
|
||||
// # AuthorizeService
|
||||
//
|
||||
// AuthorizeService implements types.MsgServer.
|
||||
func (ms msgServer) AuthorizeService(goCtx context.Context, msg *types.MsgAuthorizeService) (*types.MsgAuthorizeServiceResponse, error) {
|
||||
if ms.k.authority != msg.Controller {
|
||||
@@ -89,13 +51,8 @@ func (ms msgServer) AuthorizeService(goCtx context.Context, msg *types.MsgAuthor
|
||||
return &types.MsgAuthorizeServiceResponse{}, nil
|
||||
}
|
||||
|
||||
// # UpdateParams
|
||||
//
|
||||
// UpdateParams updates the x/did module parameters.
|
||||
func (ms msgServer) UpdateParams(
|
||||
ctx context.Context,
|
||||
msg *types.MsgUpdateParams,
|
||||
) (*types.MsgUpdateParamsResponse, error) {
|
||||
func (ms msgServer) UpdateParams(ctx context.Context, msg *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
|
||||
if ms.k.authority != msg.Authority {
|
||||
return nil, errors.Wrapf(
|
||||
govtypes.ErrInvalidSigner,
|
||||
@@ -110,6 +67,5 @@ func (ms msgServer) UpdateParams(
|
||||
// ExecuteTx implements types.MsgServer.
|
||||
func (ms msgServer) ExecuteTx(ctx context.Context, msg *types.MsgExecuteTx) (*types.MsgExecuteTxResponse, error) {
|
||||
// ctx := sdk.UnwrapSDKContext(goCtx)
|
||||
panic("ExecuteTx is unimplemented")
|
||||
return &types.MsgExecuteTxResponse{}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user