mirror of
https://github.com/sonr-io/sonr.git
synced 2026-08-02 17:31:39 +00:00
feature/refactor did state (#10)
* feat(did): remove account types * feat: Refactor Property to Proof in zkprop.go * feat: add ZKP proof mechanism for verifications * fix: return bool and error from pinInitialVault * feat: implement KeyshareSet for managing user and validator keyshares * feat: Update Credential type in protobuf * feat: update credential schema with sign count * feat: migrate and modules to middleware * refactor: rename vault module to ORM * chore(dwn): add service worker registration to index template * feat: integrate service worker for offline functionality * refactor(did): use DIDNamespace enum for verification method in proto reflection * refactor: update protobuf definitions to support Keyshare * feat: expose did keeper in app keepers * Add Motr Web App * refactor: rename motr/handlers/discovery.go to motr/handlers/openid.go * refactor: move session related code to middleware * feat: add database operations for managing assets, chains, and credentials * feat: add htmx support for UI updates * refactor: extract common helper scripts * chore: remove unused storage GUI components * refactor: Move frontend rendering to dedicated handlers * refactor: rename to * refactor: move alert implementation to templ * feat: add alert component with icon, title, and message * feat: add new RequestHeaders struct to store request headers * Feature/create home view (#9) * refactor: move view logic to new htmx handler * refactor: remove unnecessary dependencies * refactor: remove unused dependencies * feat(devbox): integrate air for local development * feat: implement openid connect discovery document * refactor: rename to * refactor(did): update service handling to support DNS discovery * feat: add support for user and validator keyshares * refactor: move keyshare signing logic to signer
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
package keeper
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/onsonr/sonr/x/did/builder"
|
||||
"github.com/onsonr/sonr/x/did/types"
|
||||
"google.golang.org/grpc/peer"
|
||||
)
|
||||
|
||||
type Context struct {
|
||||
SDKCtx sdk.Context
|
||||
Keeper Keeper
|
||||
Peer *peer.Peer
|
||||
}
|
||||
|
||||
func (k Keeper) CurrentCtx(goCtx context.Context) Context {
|
||||
ctx := sdk.UnwrapSDKContext(goCtx)
|
||||
peer, _ := peer.FromContext(goCtx)
|
||||
return Context{SDKCtx: ctx, Peer: peer, Keeper: k}
|
||||
}
|
||||
|
||||
func (c Context) Params() *types.Params {
|
||||
return c.Keeper.GetParams(c.SDK())
|
||||
}
|
||||
|
||||
func (c Context) SDK() sdk.Context {
|
||||
return c.SDKCtx
|
||||
}
|
||||
|
||||
func (c Context) IsAnonymous() bool {
|
||||
if c.Peer == nil {
|
||||
return true
|
||||
}
|
||||
return c.Peer.Addr == nil
|
||||
}
|
||||
|
||||
func (c Context) PeerID() string {
|
||||
if c.Peer == nil {
|
||||
return ""
|
||||
}
|
||||
return c.Peer.Addr.String()
|
||||
}
|
||||
|
||||
func (c Context) GetService(origin string) (*types.Service, error) {
|
||||
rec, err := c.Keeper.OrmDB.ServiceRecordTable().GetByOrigin(c.SDK(), origin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return builder.ModuleFormatAPIServiceRecord(rec), nil
|
||||
}
|
||||
|
||||
func (c Context) GetServiceInfo(origin string) *types.ServiceInfo {
|
||||
rec, _ := c.GetService(origin)
|
||||
if rec == nil {
|
||||
return &types.ServiceInfo{Exists: false, Origin: origin, Fingerprint: types.ComputeOriginTXTRecord(origin)}
|
||||
}
|
||||
return &types.ServiceInfo{Exists: true, Origin: origin, Fingerprint: types.ComputeOriginTXTRecord(origin), Service: rec}
|
||||
}
|
||||
@@ -66,7 +66,8 @@ func (k Keeper) GetParams(ctx sdk.Context) *types.Params {
|
||||
if err != nil {
|
||||
p = types.DefaultParams()
|
||||
}
|
||||
return &p
|
||||
params := p.ActiveParams(k.HasIPFSConnection())
|
||||
return ¶ms
|
||||
}
|
||||
|
||||
// GetExpirationBlockHeight returns the block height at which the given duration will have passed
|
||||
|
||||
@@ -23,11 +23,11 @@ func (k Keeper) assembleInitialVault(ctx sdk.Context) (string, int64, error) {
|
||||
}
|
||||
|
||||
// pinInitialVault pins the initial vault to the local IPFS node
|
||||
func (k Keeper) pinInitialVault(_ sdk.Context, cid string, address string) error {
|
||||
func (k Keeper) pinInitialVault(_ sdk.Context, cid string, address string) (bool, error) {
|
||||
// Resolve the path
|
||||
path, err := path.NewPath(cid)
|
||||
if err != nil {
|
||||
return err
|
||||
return false, err
|
||||
}
|
||||
|
||||
// 1. Initialize vault.db sqlite database in local IPFS with Mount
|
||||
@@ -37,13 +37,13 @@ func (k Keeper) pinInitialVault(_ sdk.Context, cid string, address string) error
|
||||
// 3. Publish the path to the IPNS
|
||||
_, err = k.ipfsClient.Name().Publish(context.Background(), path, options.Name.Key(address))
|
||||
if err != nil {
|
||||
return err
|
||||
return false, err
|
||||
}
|
||||
|
||||
// 4. Insert the accounts into x/auth
|
||||
|
||||
// 5. Insert the controller into state
|
||||
return nil
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// GetFromIPFS gets a file from the local IPFS node
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
"github.com/ipfs/kubo/client/rpc"
|
||||
|
||||
apiv1 "github.com/onsonr/sonr/api/did/v1"
|
||||
middleware "github.com/onsonr/sonr/x/did/middleware"
|
||||
middleware "github.com/onsonr/sonr/x/did/context"
|
||||
"github.com/onsonr/sonr/x/did/types"
|
||||
)
|
||||
|
||||
@@ -90,7 +90,7 @@ func NewKeeper(
|
||||
|
||||
// IsClaimedServiceOrigin checks if a service origin is unclaimed
|
||||
func (k Keeper) IsUnclaimedServiceOrigin(ctx sdk.Context, origin string) bool {
|
||||
rec, _ := k.OrmDB.ServiceRecordTable().GetByOriginUri(ctx, origin)
|
||||
rec, _ := k.OrmDB.ServiceRecordTable().GetByOrigin(ctx, origin)
|
||||
return rec == nil
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ func (k Keeper) IsValidServiceOrigin(ctx sdk.Context, origin string, clientInfo
|
||||
if origin != clientInfo.Hostname {
|
||||
return false
|
||||
}
|
||||
rec, err := k.OrmDB.ServiceRecordTable().GetByOriginUri(ctx, origin)
|
||||
rec, err := k.OrmDB.ServiceRecordTable().GetByOrigin(ctx, origin)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
package keeper
|
||||
|
||||
import (
|
||||
didv1 "github.com/onsonr/sonr/api/did/v1"
|
||||
"github.com/onsonr/sonr/x/did/types"
|
||||
)
|
||||
|
||||
func convertServiceRecord(rec *didv1.ServiceRecord) *types.Service {
|
||||
return &types.Service{
|
||||
Origin: rec.OriginUri,
|
||||
}
|
||||
}
|
||||
+10
-40
@@ -2,11 +2,6 @@ package keeper
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"google.golang.org/genproto/googleapis/api/httpbody"
|
||||
"google.golang.org/grpc/peer"
|
||||
|
||||
"github.com/onsonr/sonr/x/did/types"
|
||||
)
|
||||
@@ -23,52 +18,27 @@ func NewQuerier(keeper Keeper) Querier {
|
||||
|
||||
// Params returns the total set of did parameters.
|
||||
func (k Querier) Params(
|
||||
c context.Context,
|
||||
goCtx context.Context,
|
||||
req *types.QueryRequest,
|
||||
) (*types.QueryParamsResponse, error) {
|
||||
ctx := sdk.UnwrapSDKContext(c)
|
||||
|
||||
p, err := k.Keeper.Params.Get(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
params := p.ActiveParams(k.HasIPFSConnection())
|
||||
return &types.QueryParamsResponse{Params: ¶ms}, nil
|
||||
) (*types.QueryResponse, error) {
|
||||
ctx := k.CurrentCtx(goCtx)
|
||||
return &types.QueryResponse{Params: k.GetParams(ctx.SDK())}, nil
|
||||
}
|
||||
|
||||
// Resolve implements types.QueryServer.
|
||||
func (k Querier) Resolve(
|
||||
goCtx context.Context,
|
||||
req *types.QueryRequest,
|
||||
) (*types.QueryResolveResponse, error) {
|
||||
// ctx := sdk.UnwrapSDKContext(goCtx)
|
||||
return &types.QueryResolveResponse{}, nil
|
||||
) (*types.QueryResponse, error) {
|
||||
ctx := k.CurrentCtx(goCtx)
|
||||
return &types.QueryResponse{Params: k.GetParams(ctx.SDK())}, nil
|
||||
}
|
||||
|
||||
// Service implements types.QueryServer.
|
||||
func (k Querier) Service(
|
||||
goCtx context.Context,
|
||||
req *types.QueryRequest,
|
||||
) (*types.QueryServiceResponse, error) {
|
||||
ctx := sdk.UnwrapSDKContext(goCtx)
|
||||
|
||||
_, ok := peer.FromContext(goCtx)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("failed to get peer from context")
|
||||
}
|
||||
|
||||
rec, err := k.OrmDB.ServiceRecordTable().GetByOriginUri(ctx, req.Origin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &types.QueryServiceResponse{Service: convertServiceRecord(rec)}, nil
|
||||
}
|
||||
|
||||
// HTMX implements types.QueryServer.
|
||||
func (k Querier) HTMX(goCtx context.Context, req *types.QueryRequest) (*httpbody.HttpBody, error) {
|
||||
// ctx := sdk.UnwrapSDKContext(goCtx)
|
||||
return &httpbody.HttpBody{
|
||||
ContentType: "text/html",
|
||||
Data: []byte("<html><body>HTMX</body></html>"),
|
||||
}, nil
|
||||
) (*types.QueryResponse, error) {
|
||||
ctx := k.CurrentCtx(goCtx)
|
||||
return &types.QueryResponse{Service: ctx.GetServiceInfo(req.GetOrigin()), Params: ctx.Params()}, nil
|
||||
}
|
||||
|
||||
+30
-38
@@ -9,7 +9,7 @@ import (
|
||||
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
|
||||
|
||||
"github.com/onsonr/sonr/x/did/builder"
|
||||
"github.com/onsonr/sonr/x/did/middleware"
|
||||
snrctx "github.com/onsonr/sonr/x/did/context"
|
||||
"github.com/onsonr/sonr/x/did/types"
|
||||
)
|
||||
|
||||
@@ -24,38 +24,17 @@ func NewMsgServerImpl(keeper Keeper) types.MsgServer {
|
||||
return &msgServer{k: keeper}
|
||||
}
|
||||
|
||||
// UpdateParams updates the x/did module parameters.
|
||||
func (ms msgServer) UpdateParams(
|
||||
ctx context.Context,
|
||||
msg *types.MsgUpdateParams,
|
||||
) (*types.MsgUpdateParamsResponse, error) {
|
||||
if ms.k.authority != msg.Authority {
|
||||
// AuthorizeService implements types.MsgServer.
|
||||
func (ms msgServer) AuthorizeService(goCtx context.Context, msg *types.MsgAuthorizeService) (*types.MsgAuthorizeServiceResponse, error) {
|
||||
if ms.k.authority != msg.Controller {
|
||||
return nil, errors.Wrapf(
|
||||
govtypes.ErrInvalidSigner,
|
||||
"invalid authority; expected %s, got %s",
|
||||
ms.k.authority,
|
||||
msg.Authority,
|
||||
msg.Controller,
|
||||
)
|
||||
}
|
||||
|
||||
return nil, ms.k.Params.Set(ctx, msg.Params)
|
||||
}
|
||||
|
||||
// Authorize implements types.MsgServer.
|
||||
func (ms msgServer) Authorize(
|
||||
ctx context.Context,
|
||||
msg *types.MsgAuthorize,
|
||||
) (*types.MsgAuthorizeResponse, error) {
|
||||
if ms.k.authority != msg.Authority {
|
||||
return nil, errors.Wrapf(
|
||||
govtypes.ErrInvalidSigner,
|
||||
"invalid authority; expected %s, got %s",
|
||||
ms.k.authority,
|
||||
msg.Authority,
|
||||
)
|
||||
}
|
||||
// ctx := sdk.UnwrapSDKContext(goCtx)
|
||||
return &types.MsgAuthorizeResponse{}, nil
|
||||
return &types.MsgAuthorizeServiceResponse{}, nil
|
||||
}
|
||||
|
||||
// AllocateVault implements types.MsgServer.
|
||||
@@ -64,7 +43,7 @@ func (ms msgServer) AllocateVault(
|
||||
msg *types.MsgAllocateVault,
|
||||
) (*types.MsgAllocateVaultResponse, error) {
|
||||
ctx := sdk.UnwrapSDKContext(goCtx)
|
||||
clientInfo, err := middleware.ExtractClientInfo(goCtx)
|
||||
clientInfo, err := snrctx.ExtractClientInfo(goCtx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -79,7 +58,7 @@ func (ms msgServer) AllocateVault(
|
||||
return nil, err
|
||||
}
|
||||
|
||||
regOpts, err := builder.NewRegistrationOptions(msg.Origin, msg.Subject, cid, ms.k.GetParams(ctx))
|
||||
regOpts, err := builder.GetPublicKeyCredentialCreationOptions(msg.Origin, msg.Subject, cid, ms.k.GetParams(ctx))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -113,23 +92,36 @@ func (ms msgServer) RegisterService(
|
||||
) (*types.MsgRegisterServiceResponse, error) {
|
||||
ctx := sdk.UnwrapSDKContext(goCtx)
|
||||
|
||||
clientInfo, err := middleware.ExtractClientInfo(goCtx)
|
||||
clientInfo, err := snrctx.ExtractClientInfo(goCtx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 1.Check if the service origin is valid
|
||||
if !ms.k.IsValidServiceOrigin(ctx, msg.OriginUri, clientInfo) {
|
||||
if !ms.k.IsValidServiceOrigin(ctx, msg.Service.Origin, clientInfo) {
|
||||
return nil, types.ErrInvalidServiceOrigin
|
||||
}
|
||||
return ms.k.insertService(ctx, msg)
|
||||
return ms.k.insertService(ctx, msg.Service)
|
||||
}
|
||||
|
||||
// SyncVault implements types.MsgServer.
|
||||
func (ms msgServer) SyncVault(
|
||||
ctx context.Context,
|
||||
msg *types.MsgSyncVault,
|
||||
) (*types.MsgSyncVaultResponse, error) {
|
||||
// SyncController implements types.MsgServer.
|
||||
func (ms msgServer) SyncController(ctx context.Context, msg *types.MsgSyncController) (*types.MsgSyncControllerResponse, error) {
|
||||
// ctx := sdk.UnwrapSDKContext(goCtx)
|
||||
return &types.MsgSyncVaultResponse{}, nil
|
||||
return &types.MsgSyncControllerResponse{}, nil
|
||||
}
|
||||
|
||||
// UpdateParams updates the x/did module parameters.
|
||||
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,
|
||||
"invalid authority; expected %s, got %s",
|
||||
ms.k.authority,
|
||||
msg.Authority,
|
||||
)
|
||||
}
|
||||
return nil, ms.k.Params.Set(ctx, msg.Params)
|
||||
}
|
||||
|
||||
@@ -3,19 +3,17 @@ package keeper
|
||||
import (
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
|
||||
didv1 "github.com/onsonr/sonr/api/did/v1"
|
||||
"github.com/onsonr/sonr/x/did/builder"
|
||||
"github.com/onsonr/sonr/x/did/types"
|
||||
)
|
||||
|
||||
// insertService inserts a service record into the database
|
||||
func (k Keeper) insertService(
|
||||
ctx sdk.Context,
|
||||
svc *types.MsgRegisterService,
|
||||
svc *types.Service,
|
||||
) (*types.MsgRegisterServiceResponse, error) {
|
||||
record := didv1.ServiceRecord{
|
||||
Id: svc.OriginUri,
|
||||
}
|
||||
err := k.OrmDB.ServiceRecordTable().Insert(ctx, &record)
|
||||
record := builder.APIFormatServiceRecord(svc)
|
||||
err := k.OrmDB.ServiceRecordTable().Insert(ctx, record)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user