diff --git a/nostr/nwc/nwc.go b/nostr/nwc/nwc.go
index 61441b7..5fa3068 100644
--- a/nostr/nwc/nwc.go
+++ b/nostr/nwc/nwc.go
@@ -1,7 +1,9 @@
package nwc
import (
+ "encoding/hex"
"fmt"
+ "log"
"net/url"
"strings"
@@ -21,10 +23,10 @@ func (nwc *NwcConnection) Serialize() string {
}
return fmt.Sprintf(
- "nostr+walletconnect//%s?%s&secret=%s",
- nwc.WalletPubkey.Serialize(),
+ "nostr+walletconnect://%s?%s&secret=%s",
+ hex.EncodeToString(nwc.WalletPubkey.Serialize()),
strings.Join(relays, "&"),
- nwc.Secret.Serialize(),
+ hex.EncodeToString(nwc.Secret.Serialize()),
)
}
@@ -47,16 +49,18 @@ func NewConnection() (*NwcConnection, error) {
return nil, error(err)
}
+ return &NwcConnection{
+ WalletPubkey: walletPubkey,
+ Relays: Relays(),
+ Secret: secret,
+ }, nil
+}
+
+func Relays() []*url.URL {
// relays on which we will listen for requests
u, err := url.Parse("wss://relay.primal.net")
if err != nil {
- return nil, error(err)
+ log.Fatalf("failed to parse URL: %v", err)
}
- relays := []*url.URL{u}
-
- return &NwcConnection{
- walletPubkey,
- relays,
- secret,
- }, nil
+ return []*url.URL{u}
}
diff --git a/pages/index.templ b/pages/index.templ
index 9e2abe1..5a66ac9 100644
--- a/pages/index.templ
+++ b/pages/index.templ
@@ -5,11 +5,13 @@ import (
"github.com/ekzyis/magicwallet/pages/components"
"github.com/ekzyis/magicwallet/db/models"
+ "github.com/ekzyis/magicwallet/nostr/nwc"
)
templ Index() {
{{ u := ctx.Value("user").(models.User)}}
{{ w := ctx.Value("wallet").(models.Wallet) }}
+ {{ nwc := ctx.Value("nwc").(*nwc.NwcConnection).Serialize() }}
@components.Head()
diff --git a/public/js/index.js b/public/js/index.js
index 864cda4..8c3e66c 100644
--- a/public/js/index.js
+++ b/public/js/index.js
@@ -1,5 +1,6 @@
const user = $$("user")
-const wallet = $$("wallet"
-)
+const wallet = $$("wallet")
+const nwc = $$("nwc")
console.log(`logged in as ${user.Name} with id ${user.Id}`)
-console.log(`you have ${wallet.Msats} msats`)
\ No newline at end of file
+console.log(`you have ${wallet.Msats} msats`)
+console.log(nwc)
\ No newline at end of file
diff --git a/server/router/context/context.go b/server/router/context/context.go
index fdf6f4e..a86777d 100644
--- a/server/router/context/context.go
+++ b/server/router/context/context.go
@@ -23,5 +23,6 @@ func (hc *Context) WithContext(ec echo.Context) context.Context {
ctx = context.WithValue(ctx, "path", ec.Request().URL.Path)
ctx = context.WithValue(ctx, "user", ec.Get("user"))
ctx = context.WithValue(ctx, "wallet", ec.Get("wallet"))
+ ctx = context.WithValue(ctx, "nwc", ec.Get("nwc"))
return ctx
}
diff --git a/server/router/middleware/session.go b/server/router/middleware/session.go
index 57b844a..1bab4dc 100644
--- a/server/router/middleware/session.go
+++ b/server/router/middleware/session.go
@@ -6,6 +6,7 @@ import (
"net/http"
"time"
+ "github.com/decred/dcrd/dcrec/secp256k1"
"github.com/ekzyis/magicwallet/db/models"
"github.com/ekzyis/magicwallet/nostr/nwc"
"github.com/ekzyis/magicwallet/server/router/context"
@@ -59,7 +60,7 @@ func newSession(hc context.Context, ec echo.Context) error {
return fmt.Errorf("failed to insert new session: %v", err)
}
- nwc, err := nwc.NewConnection()
+ nwcUri, err := nwc.NewConnection()
if err != nil {
return fmt.Errorf("failed to create nwc connection uri: %v", err)
}
@@ -68,8 +69,8 @@ func newSession(hc context.Context, ec echo.Context) error {
"INSERT INTO wallets(wallet_pubkey, secret, user_id) "+
"VALUES($1, $2, $3) "+
"RETURNING id, wallet_pubkey, secret, msats, user_id",
- hex.EncodeToString(nwc.WalletPubkey.Serialize()),
- hex.EncodeToString(nwc.Secret.Serialize()),
+ hex.EncodeToString(nwcUri.WalletPubkey.Serialize()),
+ hex.EncodeToString(nwcUri.Secret.Serialize()),
u.Id).
Scan(&w.Id, &w.WalletPubkey, &w.Secret, &w.Msats, &w.UserId)
if err != nil {
@@ -88,6 +89,7 @@ func newSession(hc context.Context, ec echo.Context) error {
})
ec.Set("user", u)
ec.Set("wallet", w)
+ ec.Set("nwc", nwcUri)
return nil
}
@@ -117,8 +119,32 @@ func findSession(hc context.Context, ec echo.Context, sid string) error {
"JOIN users u ON u.id = w.user_id "+
"WHERE u.id = $1 "+
"LIMIT 1", u.Id).Scan(&w.Id, &w.WalletPubkey, &w.Secret, &w.Msats, &w.UserId)
+ if err != nil {
+ return fmt.Errorf("wallet not found: %v", err)
+ }
+
+ pkBytes, err := hex.DecodeString(w.WalletPubkey)
+ if err != nil {
+ return fmt.Errorf("failed to decode wallet pubkey: %v", err)
+ }
+ pk, err := secp256k1.ParsePubKey(pkBytes)
+ if err != nil {
+ return fmt.Errorf("failed to parse wallet pubkey: %v", err)
+ }
+
+ skBytes, err := hex.DecodeString(w.Secret)
+ if err != nil {
+ return fmt.Errorf("failed to decode wallet secret: %v", err)
+ }
+ sk, _ := secp256k1.PrivKeyFromBytes(skBytes)
ec.Set("user", u)
ec.Set("wallet", w)
+ ec.Set("nwc", &nwc.NwcConnection{
+ WalletPubkey: pk,
+ Relays: nwc.Relays(),
+ Secret: sk,
+ })
+
return nil
}