* if a request has no session cookie, a new user, session and wallet is created and session cookie is set * if a request has a session cookie and session exists in db, we will fetch user and wallet from db * this means that we have a user and wallet during each render without any login required
63 lines
1.2 KiB
Go
63 lines
1.2 KiB
Go
package nwc
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"github.com/ekzyis/magicwallet/nostr"
|
|
)
|
|
|
|
type NwcConnection struct {
|
|
WalletPubkey nostr.PublicKey
|
|
Relays []*url.URL
|
|
Secret nostr.PrivateKey
|
|
}
|
|
|
|
func (nwc *NwcConnection) Serialize() string {
|
|
var relays []string
|
|
for _, r := range nwc.Relays {
|
|
relays = append(relays, fmt.Sprintf("relay=%s", r.String()))
|
|
}
|
|
|
|
return fmt.Sprintf(
|
|
"nostr+walletconnect//%s?%s&secret=%s",
|
|
nwc.WalletPubkey.Serialize(),
|
|
strings.Join(relays, "&"),
|
|
nwc.Secret.Serialize(),
|
|
)
|
|
}
|
|
|
|
func NewConnection() (*NwcConnection, error) {
|
|
// https://github.com/nostr-protocol/nips/blob/master/47.md
|
|
|
|
error := func(err error) error {
|
|
return fmt.Errorf("failed to create nwc connection: %v\n", err)
|
|
}
|
|
|
|
// wallet keypair
|
|
_, walletPubkey, err := nostr.GenerateKeyPair()
|
|
if err != nil {
|
|
return nil, error(err)
|
|
}
|
|
|
|
// connection secret
|
|
secret, err := nostr.GeneratePrivateKey()
|
|
if err != nil {
|
|
return nil, error(err)
|
|
}
|
|
|
|
// relays on which we will listen for requests
|
|
u, err := url.Parse("wss://relay.primal.net")
|
|
if err != nil {
|
|
return nil, error(err)
|
|
}
|
|
relays := []*url.URL{u}
|
|
|
|
return &NwcConnection{
|
|
walletPubkey,
|
|
relays,
|
|
secret,
|
|
}, nil
|
|
}
|