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
-27
View File
@@ -1,27 +0,0 @@
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
}
+46
View File
@@ -1,18 +1,24 @@
package keeper
import (
"crypto/sha256"
"fmt"
"cosmossdk.io/collections"
storetypes "cosmossdk.io/core/store"
"cosmossdk.io/log"
"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"
"gopkg.in/macaroon.v2"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
stakkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/onsonr/crypto/mpc"
apiv1 "github.com/onsonr/sonr/api/did/v1"
"github.com/onsonr/sonr/x/did/types"
)
@@ -86,3 +92,43 @@ func NewKeeper(
k.Schema = schema
return k
}
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
}
// 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
}
-29
View File
@@ -1,29 +0,0 @@
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
}
@@ -15,20 +15,35 @@ import (
"github.com/labstack/echo/v4"
promise "github.com/nlepage/go-js-promise"
"github.com/onsonr/sonr/nebula/pages"
"github.com/onsonr/sonr/x/vault/client/htmx/middleware"
"github.com/onsonr/sonr/x/vault/client/htmx/state"
"github.com/onsonr/sonr/x/vault/client/dwn/middleware"
"github.com/onsonr/sonr/x/vault/client/dwn/state"
)
func main() {
e := echo.New()
e.Use(middleware.UseSession)
registerViews(e)
registerState(e)
Serve(e)
}
func registerState(e *echo.Echo) {
g := e.Group("state")
g.POST("/login/:identifier", state.HandleCredentialAssertion)
// g.GET("/discovery", state.GetDiscovery)
g.GET("/jwks", state.GetJWKS)
g.GET("/token", state.GetToken)
g.POST("/:origin/grant/:subject", state.GrantAuthorization)
g.POST("/register/:subject", state.HandleCredentialCreation)
g.POST("/register/:subject/check", state.CheckSubjectIsValid)
}
func registerViews(e *echo.Echo) {
e.GET("/home", pages.Home)
e.GET("/login", pages.Login)
e.GET("/register", pages.Register)
e.GET("/profile", pages.Profile)
e.GET("/authorize", pages.Authorize)
state.RegisterHandlers(e)
Serve(e)
}
// Serve serves HTTP requests using handler or http.DefaultServeMux if handler is nil.
@@ -8,16 +8,16 @@ import (
"github.com/labstack/echo/v4"
)
func checkSubjectIsValid(e echo.Context) error {
func CheckSubjectIsValid(e echo.Context) error {
credentialID := e.FormValue("credentialID")
return e.JSON(200, credentialID)
}
func handleCredentialAssertion(e echo.Context) error {
func HandleCredentialAssertion(e echo.Context) error {
return e.JSON(200, "HandleCredentialAssertion")
}
func handleCredentialCreation(e echo.Context) error {
func HandleCredentialCreation(e echo.Context) error {
// Get the serialized credential data from the form
credentialDataJSON := e.FormValue("credentialData")
@@ -4,19 +4,19 @@ import (
"github.com/labstack/echo/v4"
)
func grantAuthorization(e echo.Context) error {
func GrantAuthorization(e echo.Context) error {
// Implement authorization endpoint using passkey authentication
// Store session data in cache
return nil
}
func getJWKS(e echo.Context) error {
func GetJWKS(e echo.Context) error {
// Implement token endpoint
// Use cached session data for validation
return nil
}
func getToken(e echo.Context) error {
func GetToken(e echo.Context) error {
// Implement token endpoint
// Use cached session data for validation
return nil
-16
View File
@@ -1,16 +0,0 @@
package state
import (
"github.com/labstack/echo/v4"
)
func RegisterHandlers(e *echo.Echo) {
g := e.Group("state")
g.POST("/login/:identifier", handleCredentialAssertion)
// g.GET("/discovery", state.GetDiscovery)
g.GET("/jwks", getJWKS)
g.GET("/token", getToken)
g.POST("/:origin/grant/:subject", grantAuthorization)
g.POST("/register/:subject", handleCredentialCreation)
g.POST("/register/:subject/check", checkSubjectIsValid)
}
-1
View File
@@ -1 +0,0 @@
package state
-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
}
+6 -6
View File
@@ -74,7 +74,7 @@ func (m *GenesisState) GetParams() Params {
type Params struct {
IpfsActive bool `protobuf:"varint,1,opt,name=ipfs_active,json=ipfsActive,proto3" json:"ipfs_active,omitempty"`
LocalRegistrationEnabled bool `protobuf:"varint,2,opt,name=local_registration_enabled,json=localRegistrationEnabled,proto3" json:"local_registration_enabled,omitempty"`
Schema *Schema `protobuf:"bytes,3,opt,name=schema,proto3" json:"schema,omitempty"`
Schema *Schema `protobuf:"bytes,4,opt,name=schema,proto3" json:"schema,omitempty"`
}
func (m *Params) Reset() { *m = Params{} }
@@ -270,11 +270,11 @@ var fileDescriptor_4c971b352fb6cc17 = []byte{
0xfa, 0xd2, 0x8d, 0x85, 0xf4, 0xaa, 0x41, 0xfa, 0xbe, 0x5f, 0x40, 0x8b, 0x5e, 0x12, 0x61, 0x2f,
0x60, 0x58, 0x19, 0x29, 0xaa, 0xb1, 0xc5, 0x52, 0x39, 0x6f, 0x85, 0x57, 0x46, 0x8f, 0x51, 0x8b,
0x49, 0x85, 0x17, 0x7c, 0x8b, 0xf2, 0x9c, 0x12, 0xc5, 0x46, 0xe0, 0x55, 0xf0, 0xd9, 0x31, 0x24,
0x4e, 0x4e, 0x71, 0x26, 0xf8, 0xf6, 0xdd, 0x66, 0xe7, 0xc4, 0x8b, 0xce, 0x7f, 0xfe, 0xe0, 0xeb,
0x4e, 0x4e, 0x71, 0x26, 0xf8, 0xce, 0xdd, 0x66, 0xe7, 0xc4, 0x8b, 0xce, 0x7f, 0xfe, 0xe0, 0xeb,
0xf5, 0x28, 0xfa, 0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 0x89, 0x02, 0x00, 0x00,
0x57, 0x11, 0x84, 0xf3, 0x89, 0x02, 0x00, 0x00,
}
func (this *Params) Equal(that interface{}) bool {
@@ -379,7 +379,7 @@ func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) {
i = encodeVarintGenesis(dAtA, i, uint64(size))
}
i--
dAtA[i] = 0x1a
dAtA[i] = 0x22
}
if m.LocalRegistrationEnabled {
i--
@@ -742,7 +742,7 @@ func (m *Params) Unmarshal(dAtA []byte) error {
}
}
m.LocalRegistrationEnabled = bool(v != 0)
case 3:
case 4:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field Schema", wireType)
}
@@ -26,17 +26,6 @@ var (
swJSFile = files.NewBytesFile(swJSData)
)
// NewConfig uses the config template to generate the dwn config file
func NewConfig(keyshareJSON string, adddress string, chainID string, schema *dwn.Schema) *dwn.Config {
dwnCfg := &dwn.Config{
Motr: createMotrConfig(keyshareJSON, adddress, "sonr.id"),
Ipfs: defaultIPFSConfig(),
Sonr: defaultSonrConfig(chainID),
Schema: schema,
}
return dwnCfg
}
// NewVaultDirectory creates a new directory with the default files
func NewVaultDirectory(cnfg *dwn.Config) (files.Node, error) {
dwnJSON, err := json.Marshal(cnfg)
@@ -86,28 +75,3 @@ func MarshalConfigFile(c *dwn.Config) (files.Node, error) {
}
return files.NewBytesFile(dwnConfigData), nil
}
func createMotrConfig(keyshareJSON string, adddress string, origin string) *dwn.Motr {
return &dwn.Motr{
Keyshare: keyshareJSON,
Address: adddress,
Origin: origin,
}
}
func defaultIPFSConfig() *dwn.IPFS {
return &dwn.IPFS{
ApiUrl: "https://api.sonr-ipfs.land",
GatewayUrl: "https://ipfs.sonr.land",
}
}
func defaultSonrConfig(chainID string) *dwn.Sonr {
return &dwn.Sonr{
ApiUrl: "https://api.sonr.land",
GrpcUrl: "https://grpc.sonr.land",
RpcUrl: "https://rpc.sonr.land",
WebSocketUrl: "wss://rpc.sonr.land/ws",
ChainId: chainID,
}
}
+34 -3
View File
@@ -4,15 +4,21 @@ import (
"github.com/ipfs/boxo/files"
"github.com/onsonr/sonr/config/dwn"
vault "github.com/onsonr/sonr/x/vault/internal"
vault "github.com/onsonr/sonr/x/vault/types/internal"
)
type Vault struct {
FS files.Node
}
func NewVault(cnfg *dwn.Config, chainID string) (*Vault, error) {
fileMap, err := vault.NewVaultDirectory(cnfg)
func NewVault(keyshareJSON string, adddress string, chainID string, schema *dwn.Schema) (*Vault, error) {
dwnCfg := &dwn.Config{
Motr: createMotrConfig(keyshareJSON, adddress, "sonr.id"),
Ipfs: defaultIPFSConfig(),
Sonr: defaultSonrConfig(chainID),
Schema: schema,
}
fileMap, err := vault.NewVaultDirectory(dwnCfg)
if err != nil {
return nil, err
}
@@ -20,3 +26,28 @@ func NewVault(cnfg *dwn.Config, chainID string) (*Vault, error) {
FS: fileMap,
}, nil
}
func createMotrConfig(keyshareJSON string, adddress string, origin string) *dwn.Motr {
return &dwn.Motr{
Keyshare: keyshareJSON,
Address: adddress,
Origin: origin,
}
}
func defaultIPFSConfig() *dwn.IPFS {
return &dwn.IPFS{
ApiUrl: "https://api.sonr-ipfs.land",
GatewayUrl: "https://ipfs.sonr.land",
}
}
func defaultSonrConfig(chainID string) *dwn.Sonr {
return &dwn.Sonr{
ApiUrl: "https://api.sonr.land",
GrpcUrl: "https://grpc.sonr.land",
RpcUrl: "https://rpc.sonr.land",
WebSocketUrl: "wss://rpc.sonr.land/ws",
ChainId: chainID,
}
}