mirror of
https://github.com/onsonr/sonr.git
synced 2025-03-10 21:09:11 +00:00
* 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
114 lines
2.6 KiB
Go
114 lines
2.6 KiB
Go
package vault
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
_ "embed"
|
|
"encoding/json"
|
|
|
|
"github.com/a-h/templ"
|
|
"github.com/ipfs/boxo/files"
|
|
"github.com/onsonr/sonr/config/dwn"
|
|
)
|
|
|
|
//go:embed app.wasm
|
|
var dwnWasmData []byte
|
|
|
|
//go:embed motr.mjs
|
|
var motrMJSData []byte
|
|
|
|
//go:embed sw.js
|
|
var swJSData []byte
|
|
|
|
var (
|
|
dwnWasmFile = files.NewBytesFile(dwnWasmData)
|
|
motrMJSFile = files.NewBytesFile(motrMJSData)
|
|
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)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
dwnStr, err := templ.JSONString(cnfg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
w := bytes.NewBuffer(nil)
|
|
err = indexFile(dwnStr).Render(context.Background(), w)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
fileMap := map[string]files.Node{
|
|
"config.json": files.NewBytesFile(dwnJSON),
|
|
"motr.mjs": motrMJSFile,
|
|
"sw.js": swJSFile,
|
|
"app.wasm": dwnWasmFile,
|
|
"index.html": files.NewBytesFile(w.Bytes()),
|
|
}
|
|
return files.NewMapDirectory(fileMap), nil
|
|
}
|
|
|
|
// Use IndexHTML template to generate the index file
|
|
func IndexHTMLFile(c *dwn.Config) (files.Node, error) {
|
|
str, err := templ.JSONString(c)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
w := bytes.NewBuffer(nil)
|
|
err = indexFile(str).Render(context.Background(), w)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
indexData := w.Bytes()
|
|
return files.NewBytesFile(indexData), nil
|
|
}
|
|
|
|
// MarshalConfigFile uses the config template to generate the dwn config file
|
|
func MarshalConfigFile(c *dwn.Config) (files.Node, error) {
|
|
dwnConfigData, err := json.Marshal(c)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
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,
|
|
}
|
|
}
|