feat: NWC Subscription System
This commit is contained in:
@@ -33,9 +33,24 @@ err = client.Request(ctx, "make_invoice", params, &invoice)
|
||||
- `lookup_invoice` - Check invoice status
|
||||
- `pay_invoice` - Pay invoice
|
||||
|
||||
## Payment Notifications
|
||||
|
||||
```go
|
||||
// Subscribe to payment notifications
|
||||
err = client.SubscribeNotifications(ctx, func(notificationType string, notification map[string]any) error {
|
||||
if notificationType == "payment_received" {
|
||||
amount := notification["amount"].(float64)
|
||||
description := notification["description"].(string)
|
||||
// Process payment...
|
||||
}
|
||||
return nil
|
||||
})
|
||||
```
|
||||
|
||||
## Features
|
||||
|
||||
- NIP-44 encryption
|
||||
- Event signing
|
||||
- Relay communication
|
||||
- Payment notifications
|
||||
- Error handling
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
"orly.dev/pkg/protocol/ws"
|
||||
"orly.dev/pkg/utils/chk"
|
||||
"orly.dev/pkg/utils/context"
|
||||
"orly.dev/pkg/utils/log"
|
||||
"orly.dev/pkg/utils/values"
|
||||
)
|
||||
|
||||
@@ -138,4 +139,110 @@ func (cl *Client) Request(c context.T, method string, params, result any) (err e
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// NotificationHandler is a callback for handling NWC notifications
|
||||
type NotificationHandler func(notificationType string, notification map[string]any) error
|
||||
|
||||
// SubscribeNotifications subscribes to NWC notification events (kinds 23197/23196)
|
||||
// and handles them with the provided callback. It maintains a persistent connection
|
||||
// with auto-reconnection on disconnect.
|
||||
func (cl *Client) SubscribeNotifications(c context.T, handler NotificationHandler) (err error) {
|
||||
delay := time.Second
|
||||
for {
|
||||
if err = cl.subscribeNotificationsOnce(c, handler); err != nil {
|
||||
if err == context.Canceled {
|
||||
return err
|
||||
}
|
||||
select {
|
||||
case <-time.After(delay):
|
||||
if delay < 30*time.Second {
|
||||
delay *= 2
|
||||
}
|
||||
case <-c.Done():
|
||||
return context.Canceled
|
||||
}
|
||||
continue
|
||||
}
|
||||
delay = time.Second
|
||||
}
|
||||
}
|
||||
|
||||
// subscribeNotificationsOnce performs a single subscription attempt
|
||||
func (cl *Client) subscribeNotificationsOnce(c context.T, handler NotificationHandler) (err error) {
|
||||
// Connect to relay
|
||||
var rc *ws.Client
|
||||
if rc, err = ws.RelayConnect(c, cl.relay); chk.E(err) {
|
||||
return fmt.Errorf("relay connection failed: %w", err)
|
||||
}
|
||||
defer rc.Close()
|
||||
|
||||
// Subscribe to notification events filtered by "p" tag
|
||||
// Support both NIP-44 (kind 23197) and legacy NIP-04 (kind 23196)
|
||||
var sub *ws.Subscription
|
||||
if sub, err = rc.Subscribe(
|
||||
c, filters.New(
|
||||
&filter.F{
|
||||
Kinds: kinds.New(kind.New(23197), kind.New(23196)),
|
||||
Tags: tags.New(
|
||||
tag.New("p", hex.Enc(cl.clientSecretKey.Pub())),
|
||||
),
|
||||
Since: ×tamp.T{V: time.Now().Unix()},
|
||||
},
|
||||
),
|
||||
); chk.E(err) {
|
||||
return fmt.Errorf("subscription failed: %w", err)
|
||||
}
|
||||
defer sub.Unsub()
|
||||
|
||||
log.I.F("subscribed to NWC notifications from wallet %s", hex.Enc(cl.walletPublicKey))
|
||||
|
||||
// Process notification events
|
||||
for {
|
||||
select {
|
||||
case <-c.Done():
|
||||
return context.Canceled
|
||||
case ev := <-sub.Events:
|
||||
if ev == nil {
|
||||
// Channel closed, subscription ended
|
||||
return fmt.Errorf("subscription closed")
|
||||
}
|
||||
|
||||
// Process the notification event
|
||||
if err := cl.processNotificationEvent(ev, handler); err != nil {
|
||||
log.E.F("error processing notification: %v", err)
|
||||
// Continue processing other notifications even if one fails
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// processNotificationEvent decrypts and processes a single notification event
|
||||
func (cl *Client) processNotificationEvent(ev *event.E, handler NotificationHandler) (err error) {
|
||||
// Decrypt the notification content
|
||||
var decrypted []byte
|
||||
if decrypted, err = encryption.Decrypt(ev.Content, cl.conversationKey); err != nil {
|
||||
return fmt.Errorf("failed to decrypt notification: %w", err)
|
||||
}
|
||||
|
||||
// Parse the notification JSON
|
||||
var notification map[string]any
|
||||
if err = json.Unmarshal(decrypted, ¬ification); err != nil {
|
||||
return fmt.Errorf("failed to parse notification JSON: %w", err)
|
||||
}
|
||||
|
||||
// Extract notification type
|
||||
notificationType, ok := notification["notification_type"].(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("missing or invalid notification_type")
|
||||
}
|
||||
|
||||
// Extract notification data
|
||||
notificationData, ok := notification["notification"].(map[string]any)
|
||||
if !ok {
|
||||
return fmt.Errorf("missing or invalid notification data")
|
||||
}
|
||||
|
||||
// Route to type-specific handler
|
||||
return handler(notificationType, notificationData)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package nwc_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"orly.dev/pkg/crypto/encryption"
|
||||
"orly.dev/pkg/crypto/p256k"
|
||||
"orly.dev/pkg/encoders/event"
|
||||
@@ -12,84 +11,85 @@ import (
|
||||
"orly.dev/pkg/encoders/tags"
|
||||
"orly.dev/pkg/encoders/timestamp"
|
||||
"orly.dev/pkg/protocol/nwc"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNWCConversationKey(t *testing.T) {
|
||||
secret := "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
|
||||
walletPubkey := "816fd7f1d000ae81a3da251c91866fc47f4bcd6ce36921e6d46773c32f1d548b"
|
||||
|
||||
|
||||
uri := "nostr+walletconnect://" + walletPubkey + "?relay=wss://relay.getalby.com/v1&secret=" + secret
|
||||
|
||||
|
||||
parts, err := nwc.ParseConnectionURI(uri)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
|
||||
// Validate conversation key was generated
|
||||
convKey := parts.GetConversationKey()
|
||||
if len(convKey) == 0 {
|
||||
t.Fatal("conversation key should not be empty")
|
||||
}
|
||||
|
||||
|
||||
// Validate wallet public key
|
||||
walletKey := parts.GetWalletPublicKey()
|
||||
if len(walletKey) == 0 {
|
||||
t.Fatal("wallet public key should not be empty")
|
||||
}
|
||||
|
||||
|
||||
expected, err := hex.Dec(walletPubkey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
|
||||
if len(walletKey) != len(expected) {
|
||||
t.Fatal("wallet public key length mismatch")
|
||||
}
|
||||
|
||||
|
||||
for i := range walletKey {
|
||||
if walletKey[i] != expected[i] {
|
||||
t.Fatal("wallet public key mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
t.Log("✅ Conversation key and wallet pubkey validation passed")
|
||||
|
||||
// Test passed
|
||||
}
|
||||
|
||||
func TestNWCEncryptionDecryption(t *testing.T) {
|
||||
secret := "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
|
||||
walletPubkey := "816fd7f1d000ae81a3da251c91866fc47f4bcd6ce36921e6d46773c32f1d548b"
|
||||
|
||||
|
||||
uri := "nostr+walletconnect://" + walletPubkey + "?relay=wss://relay.getalby.com/v1&secret=" + secret
|
||||
|
||||
|
||||
parts, err := nwc.ParseConnectionURI(uri)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
|
||||
convKey := parts.GetConversationKey()
|
||||
testMessage := `{"method":"get_info","params":null}`
|
||||
|
||||
|
||||
// Test encryption
|
||||
encrypted, err := encryption.Encrypt([]byte(testMessage), convKey)
|
||||
if err != nil {
|
||||
t.Fatalf("encryption failed: %v", err)
|
||||
}
|
||||
|
||||
|
||||
if len(encrypted) == 0 {
|
||||
t.Fatal("encrypted message should not be empty")
|
||||
}
|
||||
|
||||
|
||||
// Test decryption
|
||||
decrypted, err := encryption.Decrypt(encrypted, convKey)
|
||||
if err != nil {
|
||||
t.Fatalf("decryption failed: %v", err)
|
||||
}
|
||||
|
||||
|
||||
if string(decrypted) != testMessage {
|
||||
t.Fatalf("decrypted message mismatch: got %s, want %s", string(decrypted), testMessage)
|
||||
}
|
||||
|
||||
t.Log("✅ NWC encryption/decryption cycle validated")
|
||||
|
||||
// Test passed
|
||||
}
|
||||
|
||||
func TestNWCEventCreation(t *testing.T) {
|
||||
@@ -97,33 +97,33 @@ func TestNWCEventCreation(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
|
||||
clientKey := &p256k.Signer{}
|
||||
if err := clientKey.InitSec(secretBytes); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
|
||||
walletPubkey, err := hex.Dec("816fd7f1d000ae81a3da251c91866fc47f4bcd6ce36921e6d46773c32f1d548b")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
|
||||
convKey, err := encryption.GenerateConversationKeyWithSigner(clientKey, walletPubkey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
|
||||
request := map[string]any{"method": "get_info"}
|
||||
reqBytes, err := json.Marshal(request)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
|
||||
encrypted, err := encryption.Encrypt(reqBytes, convKey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
|
||||
// Create NWC event
|
||||
ev := &event.E{
|
||||
Content: encrypted,
|
||||
@@ -134,24 +134,24 @@ func TestNWCEventCreation(t *testing.T) {
|
||||
tag.New("p", hex.Enc(walletPubkey)),
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
if err := ev.Sign(clientKey); err != nil {
|
||||
t.Fatalf("event signing failed: %v", err)
|
||||
}
|
||||
|
||||
|
||||
// Validate event structure
|
||||
if len(ev.Content) == 0 {
|
||||
t.Fatal("event content should not be empty")
|
||||
}
|
||||
|
||||
|
||||
if len(ev.ID) == 0 {
|
||||
t.Fatal("event should have ID after signing")
|
||||
}
|
||||
|
||||
|
||||
if len(ev.Sig) == 0 {
|
||||
t.Fatal("event should have signature after signing")
|
||||
}
|
||||
|
||||
|
||||
// Validate tags
|
||||
hasEncryption := false
|
||||
hasP := false
|
||||
@@ -166,14 +166,14 @@ func TestNWCEventCreation(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if !hasEncryption {
|
||||
t.Fatal("event missing encryption tag")
|
||||
}
|
||||
|
||||
|
||||
if !hasP {
|
||||
t.Fatal("event missing p tag")
|
||||
}
|
||||
|
||||
t.Log("✅ NWC event creation and signing validated")
|
||||
}
|
||||
|
||||
// Test passed
|
||||
}
|
||||
|
||||
470
pkg/protocol/nwc/mock_wallet_service.go
Normal file
470
pkg/protocol/nwc/mock_wallet_service.go
Normal file
@@ -0,0 +1,470 @@
|
||||
package nwc
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"orly.dev/pkg/crypto/encryption"
|
||||
"orly.dev/pkg/crypto/p256k"
|
||||
"orly.dev/pkg/encoders/event"
|
||||
"orly.dev/pkg/encoders/filter"
|
||||
"orly.dev/pkg/encoders/filters"
|
||||
"orly.dev/pkg/encoders/hex"
|
||||
"orly.dev/pkg/encoders/kind"
|
||||
"orly.dev/pkg/encoders/kinds"
|
||||
"orly.dev/pkg/encoders/tag"
|
||||
"orly.dev/pkg/encoders/tags"
|
||||
"orly.dev/pkg/encoders/timestamp"
|
||||
"orly.dev/pkg/interfaces/signer"
|
||||
"orly.dev/pkg/protocol/ws"
|
||||
"orly.dev/pkg/utils/chk"
|
||||
"orly.dev/pkg/utils/context"
|
||||
)
|
||||
|
||||
// MockWalletService implements a mock NIP-47 wallet service for testing
|
||||
type MockWalletService struct {
|
||||
relay string
|
||||
walletSecretKey signer.I
|
||||
walletPublicKey []byte
|
||||
client *ws.Client
|
||||
ctx context.T
|
||||
cancel context.F
|
||||
balance int64 // in satoshis
|
||||
balanceMutex sync.RWMutex
|
||||
connectedClients map[string][]byte // pubkey -> conversation key
|
||||
clientsMutex sync.RWMutex
|
||||
}
|
||||
|
||||
// NewMockWalletService creates a new mock wallet service
|
||||
func NewMockWalletService(relay string, initialBalance int64) (service *MockWalletService, err error) {
|
||||
// Generate wallet keypair
|
||||
walletKey := &p256k.Signer{}
|
||||
if err = walletKey.Generate(); chk.E(err) {
|
||||
return
|
||||
}
|
||||
|
||||
ctx, cancel := context.Cancel(context.Bg())
|
||||
|
||||
service = &MockWalletService{
|
||||
relay: relay,
|
||||
walletSecretKey: walletKey,
|
||||
walletPublicKey: walletKey.Pub(),
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
balance: initialBalance,
|
||||
connectedClients: make(map[string][]byte),
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Start begins the mock wallet service
|
||||
func (m *MockWalletService) Start() (err error) {
|
||||
// Connect to relay
|
||||
if m.client, err = ws.RelayConnect(m.ctx, m.relay); chk.E(err) {
|
||||
return fmt.Errorf("failed to connect to relay: %w", err)
|
||||
}
|
||||
|
||||
// Publish wallet info event
|
||||
if err = m.publishWalletInfo(); chk.E(err) {
|
||||
return fmt.Errorf("failed to publish wallet info: %w", err)
|
||||
}
|
||||
|
||||
// Subscribe to request events
|
||||
if err = m.subscribeToRequests(); chk.E(err) {
|
||||
return fmt.Errorf("failed to subscribe to requests: %w", err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Stop stops the mock wallet service
|
||||
func (m *MockWalletService) Stop() {
|
||||
if m.cancel != nil {
|
||||
m.cancel()
|
||||
}
|
||||
if m.client != nil {
|
||||
m.client.Close()
|
||||
}
|
||||
}
|
||||
|
||||
// GetWalletPublicKey returns the wallet's public key
|
||||
func (m *MockWalletService) GetWalletPublicKey() []byte {
|
||||
return m.walletPublicKey
|
||||
}
|
||||
|
||||
// publishWalletInfo publishes the NIP-47 info event (kind 13194)
|
||||
func (m *MockWalletService) publishWalletInfo() (err error) {
|
||||
capabilities := []string{
|
||||
"get_info",
|
||||
"get_balance",
|
||||
"make_invoice",
|
||||
"pay_invoice",
|
||||
}
|
||||
|
||||
info := map[string]any{
|
||||
"capabilities": capabilities,
|
||||
"notifications": []string{"payment_received", "payment_sent"},
|
||||
}
|
||||
|
||||
var content []byte
|
||||
if content, err = json.Marshal(info); chk.E(err) {
|
||||
return
|
||||
}
|
||||
|
||||
ev := &event.E{
|
||||
Content: content,
|
||||
CreatedAt: timestamp.Now(),
|
||||
Kind: kind.New(13194),
|
||||
Tags: tags.New(),
|
||||
}
|
||||
|
||||
if err = ev.Sign(m.walletSecretKey); chk.E(err) {
|
||||
return
|
||||
}
|
||||
|
||||
return m.client.Publish(m.ctx, ev)
|
||||
}
|
||||
|
||||
// subscribeToRequests subscribes to NWC request events (kind 23194)
|
||||
func (m *MockWalletService) subscribeToRequests() (err error) {
|
||||
var sub *ws.Subscription
|
||||
if sub, err = m.client.Subscribe(
|
||||
m.ctx, filters.New(
|
||||
&filter.F{
|
||||
Kinds: kinds.New(kind.New(23194)),
|
||||
Tags: tags.New(
|
||||
tag.New("p", hex.Enc(m.walletPublicKey)),
|
||||
),
|
||||
Since: ×tamp.T{V: time.Now().Unix()},
|
||||
},
|
||||
),
|
||||
); chk.E(err) {
|
||||
return
|
||||
}
|
||||
|
||||
// Handle incoming request events
|
||||
go m.handleRequestEvents(sub)
|
||||
return
|
||||
}
|
||||
|
||||
// handleRequestEvents processes incoming NWC request events
|
||||
func (m *MockWalletService) handleRequestEvents(sub *ws.Subscription) {
|
||||
for {
|
||||
select {
|
||||
case <-m.ctx.Done():
|
||||
return
|
||||
case ev := <-sub.Events:
|
||||
if ev == nil {
|
||||
continue
|
||||
}
|
||||
if err := m.processRequestEvent(ev); chk.E(err) {
|
||||
fmt.Printf("Error processing request event: %v\n", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// processRequestEvent processes a single NWC request event
|
||||
func (m *MockWalletService) processRequestEvent(ev *event.E) (err error) {
|
||||
// Get client pubkey from event
|
||||
clientPubkey := ev.Pubkey
|
||||
clientPubkeyHex := hex.Enc(clientPubkey)
|
||||
|
||||
// Generate or get conversation key
|
||||
var conversationKey []byte
|
||||
m.clientsMutex.Lock()
|
||||
if existingKey, exists := m.connectedClients[clientPubkeyHex]; exists {
|
||||
conversationKey = existingKey
|
||||
} else {
|
||||
if conversationKey, err = encryption.GenerateConversationKeyWithSigner(
|
||||
m.walletSecretKey, clientPubkey,
|
||||
); chk.E(err) {
|
||||
m.clientsMutex.Unlock()
|
||||
return
|
||||
}
|
||||
m.connectedClients[clientPubkeyHex] = conversationKey
|
||||
}
|
||||
m.clientsMutex.Unlock()
|
||||
|
||||
// Decrypt request content
|
||||
var decrypted []byte
|
||||
if decrypted, err = encryption.Decrypt(ev.Content, conversationKey); chk.E(err) {
|
||||
return
|
||||
}
|
||||
|
||||
var request map[string]any
|
||||
if err = json.Unmarshal(decrypted, &request); chk.E(err) {
|
||||
return
|
||||
}
|
||||
|
||||
method, ok := request["method"].(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("invalid method")
|
||||
}
|
||||
|
||||
params := request["params"]
|
||||
|
||||
// Process the method
|
||||
var result any
|
||||
if result, err = m.processMethod(method, params); chk.E(err) {
|
||||
// Send error response
|
||||
return m.sendErrorResponse(clientPubkey, conversationKey, "INTERNAL", err.Error())
|
||||
}
|
||||
|
||||
// Send success response
|
||||
return m.sendSuccessResponse(clientPubkey, conversationKey, result)
|
||||
}
|
||||
|
||||
// processMethod handles the actual NWC method execution
|
||||
func (m *MockWalletService) processMethod(method string, params any) (result any, err error) {
|
||||
switch method {
|
||||
case "get_info":
|
||||
return m.getInfo()
|
||||
case "get_balance":
|
||||
return m.getBalance()
|
||||
case "make_invoice":
|
||||
return m.makeInvoice(params)
|
||||
case "pay_invoice":
|
||||
return m.payInvoice(params)
|
||||
default:
|
||||
err = fmt.Errorf("unsupported method: %s", method)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// getInfo returns wallet information
|
||||
func (m *MockWalletService) getInfo() (result map[string]any, err error) {
|
||||
result = map[string]any{
|
||||
"alias": "Mock Wallet",
|
||||
"color": "#3399FF",
|
||||
"pubkey": hex.Enc(m.walletPublicKey),
|
||||
"network": "mainnet",
|
||||
"block_height": 850000,
|
||||
"block_hash": "0000000000000000000123456789abcdef",
|
||||
"methods": []string{"get_info", "get_balance", "make_invoice", "pay_invoice"},
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// getBalance returns the current wallet balance
|
||||
func (m *MockWalletService) getBalance() (result map[string]any, err error) {
|
||||
m.balanceMutex.RLock()
|
||||
balance := m.balance
|
||||
m.balanceMutex.RUnlock()
|
||||
|
||||
result = map[string]any{
|
||||
"balance": balance * 1000, // convert to msats
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// makeInvoice creates a Lightning invoice
|
||||
func (m *MockWalletService) makeInvoice(params any) (result map[string]any, err error) {
|
||||
paramsMap, ok := params.(map[string]any)
|
||||
if !ok {
|
||||
err = fmt.Errorf("invalid params")
|
||||
return
|
||||
}
|
||||
|
||||
amount, ok := paramsMap["amount"].(float64)
|
||||
if !ok {
|
||||
err = fmt.Errorf("missing or invalid amount")
|
||||
return
|
||||
}
|
||||
|
||||
description := ""
|
||||
if desc, ok := paramsMap["description"].(string); ok {
|
||||
description = desc
|
||||
}
|
||||
|
||||
paymentHash := make([]byte, 32)
|
||||
rand.Read(paymentHash)
|
||||
|
||||
// Generate a fake bolt11 invoice
|
||||
bolt11 := fmt.Sprintf("lnbc%dm1pwxxxxxxx", int64(amount/1000))
|
||||
|
||||
result = map[string]any{
|
||||
"type": "incoming",
|
||||
"invoice": bolt11,
|
||||
"description": description,
|
||||
"payment_hash": hex.Enc(paymentHash),
|
||||
"amount": int64(amount),
|
||||
"created_at": time.Now().Unix(),
|
||||
"expires_at": time.Now().Add(24 * time.Hour).Unix(),
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// payInvoice pays a Lightning invoice
|
||||
func (m *MockWalletService) payInvoice(params any) (result map[string]any, err error) {
|
||||
paramsMap, ok := params.(map[string]any)
|
||||
if !ok {
|
||||
err = fmt.Errorf("invalid params")
|
||||
return
|
||||
}
|
||||
|
||||
invoice, ok := paramsMap["invoice"].(string)
|
||||
if !ok {
|
||||
err = fmt.Errorf("missing or invalid invoice")
|
||||
return
|
||||
}
|
||||
|
||||
// Mock payment amount (would parse from invoice in real implementation)
|
||||
amount := int64(1000) // 1000 msats
|
||||
|
||||
// Check balance
|
||||
m.balanceMutex.Lock()
|
||||
if m.balance*1000 < amount {
|
||||
m.balanceMutex.Unlock()
|
||||
err = fmt.Errorf("insufficient balance")
|
||||
return
|
||||
}
|
||||
m.balance -= amount / 1000
|
||||
m.balanceMutex.Unlock()
|
||||
|
||||
preimage := make([]byte, 32)
|
||||
rand.Read(preimage)
|
||||
|
||||
result = map[string]any{
|
||||
"type": "outgoing",
|
||||
"invoice": invoice,
|
||||
"amount": amount,
|
||||
"preimage": hex.Enc(preimage),
|
||||
"created_at": time.Now().Unix(),
|
||||
}
|
||||
|
||||
// Emit payment_sent notification
|
||||
go m.emitPaymentNotification("payment_sent", result)
|
||||
return
|
||||
}
|
||||
|
||||
// sendSuccessResponse sends a successful NWC response
|
||||
func (m *MockWalletService) sendSuccessResponse(clientPubkey []byte, conversationKey []byte, result any) (err error) {
|
||||
response := map[string]any{
|
||||
"result": result,
|
||||
}
|
||||
|
||||
var responseBytes []byte
|
||||
if responseBytes, err = json.Marshal(response); chk.E(err) {
|
||||
return
|
||||
}
|
||||
|
||||
return m.sendEncryptedResponse(clientPubkey, conversationKey, responseBytes)
|
||||
}
|
||||
|
||||
// sendErrorResponse sends an error NWC response
|
||||
func (m *MockWalletService) sendErrorResponse(clientPubkey []byte, conversationKey []byte, code, message string) (err error) {
|
||||
response := map[string]any{
|
||||
"error": map[string]any{
|
||||
"code": code,
|
||||
"message": message,
|
||||
},
|
||||
}
|
||||
|
||||
var responseBytes []byte
|
||||
if responseBytes, err = json.Marshal(response); chk.E(err) {
|
||||
return
|
||||
}
|
||||
|
||||
return m.sendEncryptedResponse(clientPubkey, conversationKey, responseBytes)
|
||||
}
|
||||
|
||||
// sendEncryptedResponse sends an encrypted response event (kind 23195)
|
||||
func (m *MockWalletService) sendEncryptedResponse(clientPubkey []byte, conversationKey []byte, content []byte) (err error) {
|
||||
var encrypted []byte
|
||||
if encrypted, err = encryption.Encrypt(content, conversationKey); chk.E(err) {
|
||||
return
|
||||
}
|
||||
|
||||
ev := &event.E{
|
||||
Content: encrypted,
|
||||
CreatedAt: timestamp.Now(),
|
||||
Kind: kind.New(23195),
|
||||
Tags: tags.New(
|
||||
tag.New("encryption", "nip44_v2"),
|
||||
tag.New("p", hex.Enc(clientPubkey)),
|
||||
),
|
||||
}
|
||||
|
||||
if err = ev.Sign(m.walletSecretKey); chk.E(err) {
|
||||
return
|
||||
}
|
||||
|
||||
return m.client.Publish(m.ctx, ev)
|
||||
}
|
||||
|
||||
// emitPaymentNotification emits a payment notification (kind 23197)
|
||||
func (m *MockWalletService) emitPaymentNotification(notificationType string, paymentData map[string]any) (err error) {
|
||||
notification := map[string]any{
|
||||
"notification_type": notificationType,
|
||||
"notification": paymentData,
|
||||
}
|
||||
|
||||
var content []byte
|
||||
if content, err = json.Marshal(notification); chk.E(err) {
|
||||
return
|
||||
}
|
||||
|
||||
// Send notification to all connected clients
|
||||
m.clientsMutex.RLock()
|
||||
defer m.clientsMutex.RUnlock()
|
||||
|
||||
for clientPubkeyHex, conversationKey := range m.connectedClients {
|
||||
var clientPubkey []byte
|
||||
if clientPubkey, err = hex.Dec(clientPubkeyHex); chk.E(err) {
|
||||
continue
|
||||
}
|
||||
|
||||
var encrypted []byte
|
||||
if encrypted, err = encryption.Encrypt(content, conversationKey); chk.E(err) {
|
||||
continue
|
||||
}
|
||||
|
||||
ev := &event.E{
|
||||
Content: encrypted,
|
||||
CreatedAt: timestamp.Now(),
|
||||
Kind: kind.New(23197),
|
||||
Tags: tags.New(
|
||||
tag.New("encryption", "nip44_v2"),
|
||||
tag.New("p", hex.Enc(clientPubkey)),
|
||||
),
|
||||
}
|
||||
|
||||
if err = ev.Sign(m.walletSecretKey); chk.E(err) {
|
||||
continue
|
||||
}
|
||||
|
||||
m.client.Publish(m.ctx, ev)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// SimulateIncomingPayment simulates an incoming payment for testing
|
||||
func (m *MockWalletService) SimulateIncomingPayment(pubkey []byte, amount int64, description string) (err error) {
|
||||
// Add to balance
|
||||
m.balanceMutex.Lock()
|
||||
m.balance += amount / 1000 // convert msats to sats
|
||||
m.balanceMutex.Unlock()
|
||||
|
||||
paymentHash := make([]byte, 32)
|
||||
rand.Read(paymentHash)
|
||||
|
||||
preimage := make([]byte, 32)
|
||||
rand.Read(preimage)
|
||||
|
||||
paymentData := map[string]any{
|
||||
"type": "incoming",
|
||||
"invoice": fmt.Sprintf("lnbc%dm1pwxxxxxxx", amount/1000),
|
||||
"description": description,
|
||||
"amount": amount,
|
||||
"payment_hash": hex.Enc(paymentHash),
|
||||
"preimage": hex.Enc(preimage),
|
||||
"created_at": time.Now().Unix(),
|
||||
}
|
||||
|
||||
// Emit payment_received notification
|
||||
return m.emitPaymentNotification("payment_received", paymentData)
|
||||
}
|
||||
@@ -1,21 +1,21 @@
|
||||
package nwc_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
"orly.dev/pkg/protocol/nwc"
|
||||
"orly.dev/pkg/protocol/ws"
|
||||
"orly.dev/pkg/utils/context"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestNWCClientCreation(t *testing.T) {
|
||||
uri := "nostr+walletconnect://816fd7f1d000ae81a3da251c91866fc47f4bcd6ce36921e6d46773c32f1d548b?relay=wss://relay.getalby.com/v1&secret=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
|
||||
|
||||
|
||||
c, err := nwc.NewClient(uri)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
|
||||
if c == nil {
|
||||
t.Fatal("client should not be nil")
|
||||
}
|
||||
@@ -29,7 +29,7 @@ func TestNWCInvalidURI(t *testing.T) {
|
||||
"nostr+walletconnect://816fd7f1d000ae81a3da251c91866fc47f4bcd6ce36921e6d46773c32f1d548b",
|
||||
"nostr+walletconnect://816fd7f1d000ae81a3da251c91866fc47f4bcd6ce36921e6d46773c32f1d548b?relay=invalid",
|
||||
}
|
||||
|
||||
|
||||
for _, uri := range invalidURIs {
|
||||
_, err := nwc.NewClient(uri)
|
||||
if err == nil {
|
||||
@@ -41,42 +41,42 @@ func TestNWCInvalidURI(t *testing.T) {
|
||||
func TestNWCRelayConnection(t *testing.T) {
|
||||
ctx, cancel := context.Timeout(context.TODO(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
|
||||
rc, err := ws.RelayConnect(ctx, "wss://relay.getalby.com/v1")
|
||||
if err != nil {
|
||||
t.Fatalf("relay connection failed: %v", err)
|
||||
}
|
||||
defer rc.Close()
|
||||
|
||||
|
||||
t.Log("relay connection successful")
|
||||
}
|
||||
|
||||
func TestNWCRequestTimeout(t *testing.T) {
|
||||
uri := "nostr+walletconnect://816fd7f1d000ae81a3da251c91866fc47f4bcd6ce36921e6d46773c32f1d548b?relay=wss://relay.getalby.com/v1&secret=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
|
||||
|
||||
|
||||
c, err := nwc.NewClient(uri)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
|
||||
ctx, cancel := context.Timeout(context.TODO(), 2*time.Second)
|
||||
defer cancel()
|
||||
|
||||
|
||||
var r map[string]any
|
||||
err = c.Request(ctx, "get_info", nil, &r)
|
||||
|
||||
|
||||
if err == nil {
|
||||
t.Log("unexpected success - wallet may be active")
|
||||
t.Log("wallet responded")
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
expectedErrors := []string{
|
||||
"no response from wallet",
|
||||
"subscription closed",
|
||||
"timeout waiting for response",
|
||||
"context deadline exceeded",
|
||||
}
|
||||
|
||||
|
||||
errorFound := false
|
||||
for _, expected := range expectedErrors {
|
||||
if contains(err.Error(), expected) {
|
||||
@@ -84,18 +84,18 @@ func TestNWCRequestTimeout(t *testing.T) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if !errorFound {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
|
||||
t.Logf("proper timeout handling: %v", err)
|
||||
}
|
||||
|
||||
func contains(s, substr string) bool {
|
||||
return len(s) >= len(substr) && (s == substr || (len(s) > len(substr) &&
|
||||
(s[:len(substr)] == substr || s[len(s)-len(substr):] == substr ||
|
||||
findInString(s, substr))))
|
||||
return len(s) >= len(substr) && (s == substr || (len(s) > len(substr) &&
|
||||
(s[:len(substr)] == substr || s[len(s)-len(substr):] == substr ||
|
||||
findInString(s, substr))))
|
||||
}
|
||||
|
||||
func findInString(s, substr string) bool {
|
||||
@@ -109,48 +109,48 @@ func findInString(s, substr string) bool {
|
||||
|
||||
func TestNWCEncryption(t *testing.T) {
|
||||
uri := "nostr+walletconnect://816fd7f1d000ae81a3da251c91866fc47f4bcd6ce36921e6d46773c32f1d548b?relay=wss://relay.getalby.com/v1&secret=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
|
||||
|
||||
|
||||
c, err := nwc.NewClient(uri)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
|
||||
// We can't directly access private fields, but we can test the client creation
|
||||
// validates that the conversation key is properly generated
|
||||
// check conversation key generation
|
||||
if c == nil {
|
||||
t.Fatal("client creation should succeed with valid URI")
|
||||
}
|
||||
|
||||
t.Log("✅ NWC client encryption setup validated")
|
||||
|
||||
// Test passed
|
||||
}
|
||||
|
||||
func TestNWCEventFormat(t *testing.T) {
|
||||
uri := "nostr+walletconnect://816fd7f1d000ae81a3da251c91866fc47f4bcd6ce36921e6d46773c32f1d548b?relay=wss://relay.getalby.com/v1&secret=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
|
||||
|
||||
|
||||
c, err := nwc.NewClient(uri)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Test that the client can be created and is properly initialized
|
||||
|
||||
// Test client creation
|
||||
// The Request method will create proper NWC events with:
|
||||
// - Kind 23194 for requests
|
||||
// - Proper encryption tag
|
||||
// - Signed with client key
|
||||
|
||||
|
||||
ctx, cancel := context.Timeout(context.TODO(), 1*time.Second)
|
||||
defer cancel()
|
||||
|
||||
|
||||
var r map[string]any
|
||||
err = c.Request(ctx, "get_info", nil, &r)
|
||||
|
||||
|
||||
// We expect this to fail due to inactive connection, but it should fail
|
||||
// AFTER creating and sending a properly formatted NWC event
|
||||
// after creating and sending NWC event
|
||||
if err == nil {
|
||||
t.Log("✅ Unexpected success - wallet may be active")
|
||||
t.Log("wallet responded")
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
// Verify it failed for the right reason (connection/response issue, not formatting)
|
||||
validFailures := []string{
|
||||
"subscription closed",
|
||||
@@ -158,7 +158,7 @@ func TestNWCEventFormat(t *testing.T) {
|
||||
"context deadline exceeded",
|
||||
"timeout waiting for response",
|
||||
}
|
||||
|
||||
|
||||
validFailure := false
|
||||
for _, failure := range validFailures {
|
||||
if contains(err.Error(), failure) {
|
||||
@@ -166,10 +166,10 @@ func TestNWCEventFormat(t *testing.T) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if !validFailure {
|
||||
t.Fatalf("unexpected error type (suggests formatting issue): %v", err)
|
||||
}
|
||||
|
||||
t.Log("✅ NWC event format validation passed")
|
||||
}
|
||||
|
||||
// Test passed
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user