Set NWC URI in data-nwc

This commit is contained in:
ekzyis 2024-10-28 00:12:17 +01:00
parent 22691f8ebd
commit 0bee12cb19
5 changed files with 52 additions and 17 deletions

View File

@ -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}
}

View File

@ -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() }}
<html>
@components.Head()
<body
@ -17,6 +19,7 @@ templ Index() {
data-commit={ templ.JSONString(ctx.Value("commit")) }
data-user={ templ.JSONString(u) }
data-wallet={ templ.JSONString(w) }
data-nwc={ templ.JSONString(nwc) }
>
<div id="content" class="grid mt-3">
<div class="grid gap-10 self-center justify-self-center">

View File

@ -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`)
console.log(nwc)

View File

@ -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
}

View File

@ -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
}