67 lines
1.3 KiB
Go
67 lines
1.3 KiB
Go
package nwc
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"fmt"
|
|
"log"
|
|
"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",
|
|
hex.EncodeToString(nwc.WalletPubkey.Serialize()),
|
|
strings.Join(relays, "&"),
|
|
hex.EncodeToString(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)
|
|
}
|
|
|
|
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 {
|
|
log.Fatalf("failed to parse URL: %v", err)
|
|
}
|
|
return []*url.URL{u}
|
|
}
|