feature/dwn database state (#18)

* refactor: move database, navigator scripts to state package

* feat: add Schema config for dwn

* test: add unit tests for InitializeDatabase

* feat: use templated index.html for the DWN frontend

* feat: introduce templ generation for templ

* chore(deps): update devbox.json to use latest packages

* chore: update devbox to use bun

* feat: introduce dwn config generation

* feat: add motr.mjs for vault management

* refactor: move front end from  to  (alert)

* feat: implement devbox integration and devbox-based process management

* feat: embed motr.mjs script for offline demo

* refactor: embed motr.mjs data in embed.go

* chore: update workflows to use actions/checkout@v4

* refactor: move process-compose.yaml to deploy directory

* refactor: remove unnecessary JSON conversion
This commit is contained in:
Prad Nukala
2024-09-21 21:42:51 -04:00
committed by GitHub
parent a115b79db7
commit a4dbb41202
89 changed files with 1257 additions and 968 deletions
+76 -5
View File
@@ -1,19 +1,46 @@
package keeper
import (
"context"
"crypto/sha256"
"fmt"
"time"
"github.com/ipfs/kubo/client/rpc"
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/builder"
"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
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 := builder.NewVault(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
@@ -45,6 +72,26 @@ func (c Context) IsAnonymous() bool {
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 {
@@ -61,6 +108,30 @@ func (c Context) PeerID() string {
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
}
-7
View File
@@ -6,7 +6,6 @@ import (
"cosmossdk.io/log"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ipfs/boxo/path"
"google.golang.org/grpc/peer"
"github.com/onsonr/sonr/x/did/types"
)
@@ -71,9 +70,3 @@ func (k Keeper) HasPathInIPFS(ctx sdk.Context, cid string) (bool, error) {
}
return true, nil
}
func (k Keeper) UnwrapCtx(goCtx context.Context) Context {
ctx := sdk.UnwrapSDKContext(goCtx)
peer, _ := peer.FromContext(goCtx)
return Context{SDKCtx: ctx, Peer: peer, Keeper: k}
}
-86
View File
@@ -1,86 +0,0 @@
package keeper
import (
"context"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32"
"github.com/ipfs/boxo/files"
"github.com/ipfs/boxo/path"
"github.com/ipfs/kubo/core/coreiface/options"
"github.com/onsonr/crypto/mpc"
"github.com/onsonr/sonr/internal/vfs"
)
type Vault struct {
FS files.Node
ValKs mpc.Share
}
func NewVault(subject string, origin string, chainID string) (*Vault, error) {
shares, err := mpc.GenerateKeyshares()
var (
valKs = shares[0]
usrKs = shares[1]
)
usrKsJSON, err := usrKs.Marshal()
if err != nil {
return nil, err
}
sonrAddr, err := bech32.ConvertAndEncode("idx", valKs.GetPublicKey())
if err != nil {
return nil, err
}
cnfg, err := vfs.NewDWNConfigFile(usrKsJSON, sonrAddr, chainID)
if err != nil {
return nil, err
}
fileMap := map[string]files.Node{
"config.json": cnfg,
"sw.js": vfs.SWJSFile(),
"app.wasm": vfs.DWNWasmFile(),
"index.html": vfs.IndexFile(),
}
return &Vault{
FS: files.NewMapDirectory(fileMap),
ValKs: valKs,
}, nil
}
// AssembleVault assembles the initial vault
func (k Keeper) AssembleVault(ctx Context, subject string, origin string) (string, int64, error) {
v, err := NewVault(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
}
// 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
}
+3
View File
@@ -30,6 +30,9 @@ func init() {
// RegisterLegacyAminoCodec registers concrete types on the LegacyAmino codec
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&MsgUpdateParams{}, ModuleName+"/MsgUpdateParams", nil)
cdc.RegisterConcrete(&MsgRegisterController{}, ModuleName+"/MsgRegisterController", nil)
cdc.RegisterConcrete(&MsgRegisterService{}, ModuleName+"/MsgRegisterService", nil)
cdc.RegisterConcrete(&MsgAllocateVault{}, ModuleName+"/MsgAllocateVault", nil)
}
func RegisterInterfaces(registry types.InterfaceRegistry) {