mirror of
https://github.com/sonr-io/sonr.git
synced 2026-08-03 01:41:44 +00:00
develop
This commit is contained in:
+32
-15
@@ -20,6 +20,12 @@ 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{
|
||||
@@ -31,6 +37,32 @@ func NewConfig(keyshareJSON string, adddress string, chainID string, schema *dwn
|
||||
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)
|
||||
@@ -55,21 +87,6 @@ func MarshalConfigFile(c *dwn.Config) (files.Node, error) {
|
||||
return files.NewBytesFile(dwnConfigData), nil
|
||||
}
|
||||
|
||||
// Use DWNWasm template to generate the dwn wasm file
|
||||
func DWNWasmFile() files.Node {
|
||||
return files.NewBytesFile(dwnWasmData)
|
||||
}
|
||||
|
||||
// Use MotrMJS template to generate the dwn wasm file
|
||||
func MotrMJSFile() files.Node {
|
||||
return files.NewBytesFile(motrMJSData)
|
||||
}
|
||||
|
||||
// Use ServiceWorkerJS template to generate the service worker file
|
||||
func SWJSFile() files.Node {
|
||||
return files.NewBytesFile(swJSData)
|
||||
}
|
||||
|
||||
func createMotrConfig(keyshareJSON string, adddress string, origin string) *dwn.Motr {
|
||||
return &dwn.Motr{
|
||||
Keyshare: keyshareJSON,
|
||||
|
||||
@@ -51,6 +51,11 @@ templ indexFile(cfg string) {
|
||||
</html>
|
||||
}
|
||||
|
||||
templ serviceWorker() {
|
||||
<script>
|
||||
</script>
|
||||
}
|
||||
|
||||
script initializeMotr(config string) {
|
||||
const motr = new Motr(JSON.parse(config));
|
||||
|
||||
@@ -100,7 +105,6 @@ script initializeMotr(config string) {
|
||||
document.getElementById('output').innerHTML = `<p>Error: ${error.message}</p>`;
|
||||
}
|
||||
}
|
||||
|
||||
// Run the demo when the page loads
|
||||
window.onload = demo;
|
||||
}
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@ importScripts(
|
||||
"https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@v1.1.0/sw.js",
|
||||
);
|
||||
|
||||
registerWasmHTTPListener("app.wasm");
|
||||
registerWasmHTTPListener("/app.wasm");
|
||||
|
||||
// Skip installed stage and jump to activating stage
|
||||
self.addEventListener("install", (event) => {
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package vault
|
||||
|
||||
import (
|
||||
"github.com/cosmos/cosmos-sdk/types/bech32"
|
||||
"github.com/ipfs/boxo/files"
|
||||
"github.com/onsonr/crypto/mpc"
|
||||
"github.com/onsonr/sonr/config/dwn"
|
||||
)
|
||||
|
||||
type SchemaVersion = int
|
||||
|
||||
var CurrentSchemaVersion SchemaVersion = 1
|
||||
|
||||
type Vault struct {
|
||||
FS files.Node
|
||||
ValKs mpc.Share
|
||||
}
|
||||
|
||||
func New(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 := NewConfig(usrKsJSON, sonrAddr, chainID, DefaultSchema())
|
||||
|
||||
fileMap, err := NewVaultDirectory(cnfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Vault{
|
||||
FS: fileMap,
|
||||
ValKs: valKs,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func DefaultSchema() *dwn.Schema {
|
||||
return &dwn.Schema{
|
||||
Version: CurrentSchemaVersion,
|
||||
Account: "++, id, name, address, publicKey, chainCode, index, controller, createdAt",
|
||||
Asset: "++, id, name, symbol, decimals, chainCode, createdAt",
|
||||
Chain: "++, id, name, networkId, chainCode, createdAt",
|
||||
Credential: "++, id, subject, controller, attestationType, origin, label, deviceId, credentialId, publicKey, transport, signCount, userPresent, userVerified, backupEligible, backupState, cloneWarning, createdAt, updatedAt",
|
||||
Jwk: "++, kty, crv, x, y, n, e",
|
||||
Grant: "++, subject, controller, origin, token, scopes, createdAt, updatedAt",
|
||||
Keyshare: "++, id, data, role, createdAt, lastRefreshed",
|
||||
PublicKey: "++, role, algorithm, encoding, curve, key_type, raw, jwk",
|
||||
Profile: "++, id, subject, controller, originUri, publicMetadata, privateMetadata, createdAt, updatedAt",
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user