Set NWC URI in data-nwc
This commit is contained in:
parent
22691f8ebd
commit
0bee12cb19
|
@ -1,7 +1,9 @@
|
||||||
package nwc
|
package nwc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -21,10 +23,10 @@ func (nwc *NwcConnection) Serialize() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(
|
||||||
"nostr+walletconnect//%s?%s&secret=%s",
|
"nostr+walletconnect://%s?%s&secret=%s",
|
||||||
nwc.WalletPubkey.Serialize(),
|
hex.EncodeToString(nwc.WalletPubkey.Serialize()),
|
||||||
strings.Join(relays, "&"),
|
strings.Join(relays, "&"),
|
||||||
nwc.Secret.Serialize(),
|
hex.EncodeToString(nwc.Secret.Serialize()),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,16 +49,18 @@ func NewConnection() (*NwcConnection, error) {
|
||||||
return nil, error(err)
|
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
|
// relays on which we will listen for requests
|
||||||
u, err := url.Parse("wss://relay.primal.net")
|
u, err := url.Parse("wss://relay.primal.net")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, error(err)
|
log.Fatalf("failed to parse URL: %v", err)
|
||||||
}
|
}
|
||||||
relays := []*url.URL{u}
|
return []*url.URL{u}
|
||||||
|
|
||||||
return &NwcConnection{
|
|
||||||
walletPubkey,
|
|
||||||
relays,
|
|
||||||
secret,
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,11 +5,13 @@ import (
|
||||||
|
|
||||||
"github.com/ekzyis/magicwallet/pages/components"
|
"github.com/ekzyis/magicwallet/pages/components"
|
||||||
"github.com/ekzyis/magicwallet/db/models"
|
"github.com/ekzyis/magicwallet/db/models"
|
||||||
|
"github.com/ekzyis/magicwallet/nostr/nwc"
|
||||||
)
|
)
|
||||||
|
|
||||||
templ Index() {
|
templ Index() {
|
||||||
{{ u := ctx.Value("user").(models.User)}}
|
{{ u := ctx.Value("user").(models.User)}}
|
||||||
{{ w := ctx.Value("wallet").(models.Wallet) }}
|
{{ w := ctx.Value("wallet").(models.Wallet) }}
|
||||||
|
{{ nwc := ctx.Value("nwc").(*nwc.NwcConnection).Serialize() }}
|
||||||
<html>
|
<html>
|
||||||
@components.Head()
|
@components.Head()
|
||||||
<body
|
<body
|
||||||
|
@ -17,6 +19,7 @@ templ Index() {
|
||||||
data-commit={ templ.JSONString(ctx.Value("commit")) }
|
data-commit={ templ.JSONString(ctx.Value("commit")) }
|
||||||
data-user={ templ.JSONString(u) }
|
data-user={ templ.JSONString(u) }
|
||||||
data-wallet={ templ.JSONString(w) }
|
data-wallet={ templ.JSONString(w) }
|
||||||
|
data-nwc={ templ.JSONString(nwc) }
|
||||||
>
|
>
|
||||||
<div id="content" class="grid mt-3">
|
<div id="content" class="grid mt-3">
|
||||||
<div class="grid gap-10 self-center justify-self-center">
|
<div class="grid gap-10 self-center justify-self-center">
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
const user = $$("user")
|
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(`logged in as ${user.Name} with id ${user.Id}`)
|
||||||
console.log(`you have ${wallet.Msats} msats`)
|
console.log(`you have ${wallet.Msats} msats`)
|
||||||
|
console.log(nwc)
|
|
@ -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, "path", ec.Request().URL.Path)
|
||||||
ctx = context.WithValue(ctx, "user", ec.Get("user"))
|
ctx = context.WithValue(ctx, "user", ec.Get("user"))
|
||||||
ctx = context.WithValue(ctx, "wallet", ec.Get("wallet"))
|
ctx = context.WithValue(ctx, "wallet", ec.Get("wallet"))
|
||||||
|
ctx = context.WithValue(ctx, "nwc", ec.Get("nwc"))
|
||||||
return ctx
|
return ctx
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/decred/dcrd/dcrec/secp256k1"
|
||||||
"github.com/ekzyis/magicwallet/db/models"
|
"github.com/ekzyis/magicwallet/db/models"
|
||||||
"github.com/ekzyis/magicwallet/nostr/nwc"
|
"github.com/ekzyis/magicwallet/nostr/nwc"
|
||||||
"github.com/ekzyis/magicwallet/server/router/context"
|
"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)
|
return fmt.Errorf("failed to insert new session: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
nwc, err := nwc.NewConnection()
|
nwcUri, err := nwc.NewConnection()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create nwc connection uri: %v", err)
|
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) "+
|
"INSERT INTO wallets(wallet_pubkey, secret, user_id) "+
|
||||||
"VALUES($1, $2, $3) "+
|
"VALUES($1, $2, $3) "+
|
||||||
"RETURNING id, wallet_pubkey, secret, msats, user_id",
|
"RETURNING id, wallet_pubkey, secret, msats, user_id",
|
||||||
hex.EncodeToString(nwc.WalletPubkey.Serialize()),
|
hex.EncodeToString(nwcUri.WalletPubkey.Serialize()),
|
||||||
hex.EncodeToString(nwc.Secret.Serialize()),
|
hex.EncodeToString(nwcUri.Secret.Serialize()),
|
||||||
u.Id).
|
u.Id).
|
||||||
Scan(&w.Id, &w.WalletPubkey, &w.Secret, &w.Msats, &w.UserId)
|
Scan(&w.Id, &w.WalletPubkey, &w.Secret, &w.Msats, &w.UserId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -88,6 +89,7 @@ func newSession(hc context.Context, ec echo.Context) error {
|
||||||
})
|
})
|
||||||
ec.Set("user", u)
|
ec.Set("user", u)
|
||||||
ec.Set("wallet", w)
|
ec.Set("wallet", w)
|
||||||
|
ec.Set("nwc", nwcUri)
|
||||||
|
|
||||||
return nil
|
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 "+
|
"JOIN users u ON u.id = w.user_id "+
|
||||||
"WHERE u.id = $1 "+
|
"WHERE u.id = $1 "+
|
||||||
"LIMIT 1", u.Id).Scan(&w.Id, &w.WalletPubkey, &w.Secret, &w.Msats, &w.UserId)
|
"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("user", u)
|
||||||
ec.Set("wallet", w)
|
ec.Set("wallet", w)
|
||||||
|
ec.Set("nwc", &nwc.NwcConnection{
|
||||||
|
WalletPubkey: pk,
|
||||||
|
Relays: nwc.Relays(),
|
||||||
|
Secret: sk,
|
||||||
|
})
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue