feat: Implement service worker for IPFS vault

This commit is contained in:
Prad Nukala
2024-10-07 20:13:38 -04:00
parent fa7b11198e
commit c608a067ed
34 changed files with 740 additions and 758 deletions
+4 -12
View File
@@ -6,8 +6,8 @@ import (
_ "embed"
"encoding/json"
"github.com/a-h/templ"
"github.com/ipfs/boxo/files"
"github.com/onsonr/sonr/pkg/nebula/global"
)
//go:embed app.wasm
@@ -28,12 +28,8 @@ func NewVaultDirectory(cnfg *Config) (files.Node, error) {
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)
err = global.IndexFile().Render(context.Background(), w)
if err != nil {
return nil, err
}
@@ -47,13 +43,9 @@ func NewVaultDirectory(cnfg *Config) (files.Node, error) {
}
// Use IndexHTML template to generate the index file
func IndexHTMLFile(c *Config) (files.Node, error) {
str, err := templ.JSONString(c)
if err != nil {
return nil, err
}
func IndexHTMLFile() (files.Node, error) {
w := bytes.NewBuffer(nil)
err = indexFile(str).Render(context.Background(), w)
err := global.IndexFile().Render(context.Background(), w)
if err != nil {
return nil, err
}
-104
View File
@@ -1,104 +0,0 @@
package dwn
var motrHandle = templ.NewOnceHandle()
templ importScripts() {
<script src="https://cdn.sonr.io/js/htmx.min.js"></script>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://unpkg.com/alpinejs" defer></script>
}
templ indexFile(cfg string) {
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link href="https://cdn.sonr.io/stylesheet.css" rel="stylesheet"/>
@importScripts()
<style>
[x-cloak] {
display: none;
}
</style>
<title>Sonr DWN</title>
<script>
if ("serviceWorker" in navigator) {
window.addEventListener("load", function() {
navigator.serviceWorker
.register("/sw.js")
.then(function (registration) {
console.log("Service Worker registered with scope:", registration.scope);
})
.catch(function (error) {
console.log("Service Worker registration failed:", error);
});
});
}
</script>
</head>
<body class="flex items-center justify-center h-full bg-neutral-50 lg:p-24 md:16 p-4">
<main class="flex-row items-center justify-center mx-auto w-fit max-w-screen-sm gap-y-3">
<div id="output">Loading...</div>
</main>
@motrHandle.Once() {
<script src="/assets/main.js" type="module"></script>
@initializeMotr(cfg)
}
</body>
</html>
}
script initializeMotr(config string) {
const motr = new Motr(JSON.parse(config));
async function demo() {
try {
// Insert a new account
const accountId = await motr.insertAccount({
name: 'John Doe',
address: '0x1234567890123456789012345678901234567890',
publicKey: 'sample_public_key',
chainCode: 'SONR',
index: 0,
controller: 'sample_controller',
createdAt: new Date()
});
console.log('Inserted account with ID:', accountId);
// Retrieve the account
const account = await motr.getAccount(accountId);
console.log('Retrieved account:', account);
// Insert a new credential
const credentialId = await motr.insertCredential({
subject: 'john@example.com',
label: 'John\'s Device',
controller: 'sample_controller',
attestationType: 'platform',
origin: 'https://app.sonr.io'
});
console.log('Inserted credential with ID:', credentialId);
// Retrieve the credential
const credential = await motr.getCredential(credentialId);
console.log('Retrieved credential:', credential);
document.getElementById('output').innerHTML = `
<h2>Demo Results:</h2>
<p>Inserted account ID: ${accountId}</p>
<p>Retrieved account name: ${account.name}</p>
<p>Inserted credential ID: ${credentialId}</p>
<p>Retrieved credential subject: ${credential.subject}</p>
`;
} catch (error) {
console.error('Error in demo:', error);
document.getElementById('output').innerHTML = `<p>Error: ${error.message}</p>`;
}
}
// Run the demo when the page loads
window.onload = demo;
}
-168
View File
@@ -1,168 +0,0 @@
// Code generated by templ - DO NOT EDIT.
// templ: version: v0.2.778
package dwn
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import templruntime "github.com/a-h/templ/runtime"
var motrHandle = templ.NewOnceHandle()
func importScripts() templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<script src=\"https://cdn.sonr.io/js/htmx.min.js\"></script><script src=\"https://cdn.tailwindcss.com\"></script><script src=\"https://unpkg.com/alpinejs\" defer></script>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return templ_7745c5c3_Err
})
}
func indexFile(cfg string) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var2 := templ.GetChildren(ctx)
if templ_7745c5c3_Var2 == nil {
templ_7745c5c3_Var2 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<!doctype html><html lang=\"en\"><head><meta charset=\"UTF-8\"><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><link href=\"https://cdn.sonr.io/stylesheet.css\" rel=\"stylesheet\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = importScripts().Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<style>\n [x-cloak] {\n display: none;\n }\n </style><title>Sonr DWN</title><script>\n if (\"serviceWorker\" in navigator) {\n window.addEventListener(\"load\", function() {\n navigator.serviceWorker\n .register(\"/sw.js\")\n .then(function (registration) {\n console.log(\"Service Worker registered with scope:\", registration.scope);\n })\n .catch(function (error) {\n console.log(\"Service Worker registration failed:\", error);\n });\n });\n }\n </script></head><body class=\"flex items-center justify-center h-full bg-neutral-50 lg:p-24 md:16 p-4\"><main class=\"flex-row items-center justify-center mx-auto w-fit max-w-screen-sm gap-y-3\"><div id=\"output\">Loading...</div></main>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Var3 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<script src=\"/assets/main.js\" type=\"module\"></script> ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = initializeMotr(cfg).Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return templ_7745c5c3_Err
})
templ_7745c5c3_Err = motrHandle.Once().Render(templ.WithChildren(ctx, templ_7745c5c3_Var3), templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</body></html>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return templ_7745c5c3_Err
})
}
func initializeMotr(config string) templ.ComponentScript {
return templ.ComponentScript{
Name: `__templ_initializeMotr_d70f`,
Function: `function __templ_initializeMotr_d70f(config){const motr = new Motr(JSON.parse(config));
async function demo() {
try {
// Insert a new account
const accountId = await motr.insertAccount({
name: 'John Doe',
address: '0x1234567890123456789012345678901234567890',
publicKey: 'sample_public_key',
chainCode: 'SONR',
index: 0,
controller: 'sample_controller',
createdAt: new Date()
});
console.log('Inserted account with ID:', accountId);
// Retrieve the account
const account = await motr.getAccount(accountId);
console.log('Retrieved account:', account);
// Insert a new credential
const credentialId = await motr.insertCredential({
subject: 'john@example.com',
label: 'John\'s Device',
controller: 'sample_controller',
attestationType: 'platform',
origin: 'https://app.sonr.io'
});
console.log('Inserted credential with ID:', credentialId);
// Retrieve the credential
const credential = await motr.getCredential(credentialId);
console.log('Retrieved credential:', credential);
document.getElementById('output').innerHTML = ` + "`" + `
<h2>Demo Results:</h2>
<p>Inserted account ID: ${accountId}</p>
<p>Retrieved account name: ${account.name}</p>
<p>Inserted credential ID: ${credentialId}</p>
<p>Retrieved credential subject: ${credential.subject}</p>
` + "`" + `;
} catch (error) {
console.error('Error in demo:', error);
document.getElementById('output').innerHTML = ` + "`" + `<p>Error: ${error.message}</p>` + "`" + `;
}
}
// Run the demo when the page loads
window.onload = demo;
}`,
Call: templ.SafeScript(`__templ_initializeMotr_d70f`, config),
CallInline: templ.SafeScriptInline(`__templ_initializeMotr_d70f`, config),
}
}
var _ = templruntime.GeneratedTemplate