wallet_integration_test.go raw

   1  package main
   2  
   3  import (
   4  	"context"
   5  	"os"
   6  	"strings"
   7  	"testing"
   8  
   9  	"github.com/nbd-wtf/go-nostr"
  10  	"time"
  11  )
  12  
  13  // TestLookupSettledInvoiceAgainstWallet exercises the real settle-detection path
  14  // against a live wallet: it looks up an invoice the wallet already settled and
  15  // asserts this code recognises it as settled, that both lookup key shapes work,
  16  // and that the resulting receipt verifies under the advertised key.
  17  //
  18  // It is skipped unless NWC_INTEGRATION=1, so the default `go test` stays
  19  // hermetic. To run it, grab any settled incoming transaction from the wallet:
  20  //
  21  //	sqlite3 /opt/alby-hub/data/nwc.db \
  22  //	  "select payment_hash, payment_request from transactions
  23  //	   where type='incoming' and state='SETTLED' order by id desc limit 1;"
  24  //
  25  //	NWC_INTEGRATION=1 NWC_URI='nostr+walletconnect://...' \
  26  //	  SETTLED_HASH=<hash> SETTLED_INVOICE=<bolt11> go test -run LookupSettled -v
  27  func TestLookupSettledInvoiceAgainstWallet(t *testing.T) {
  28  	if os.Getenv("NWC_INTEGRATION") != "1" {
  29  		t.Skip("set NWC_INTEGRATION=1 to run against a live wallet")
  30  	}
  31  	uri := os.Getenv("NWC_URI")
  32  	if uri == "" {
  33  		t.Fatal("NWC_URI required")
  34  	}
  35  	hash := os.Getenv("SETTLED_HASH")
  36  	invoice := os.Getenv("SETTLED_INVOICE")
  37  	if hash == "" || invoice == "" {
  38  		t.Fatal("SETTLED_HASH and SETTLED_INVOICE required")
  39  	}
  40  
  41  	spk, secret, relays, err := parseNWCURI(uri)
  42  	if err != nil {
  43  		t.Fatal(err)
  44  	}
  45  	cpk, err := nostr.GetPublicKey(secret)
  46  	if err != nil {
  47  		t.Fatal(err)
  48  	}
  49  	client := &nwcClient{
  50  		servicePubkey: spk, clientSecret: secret, clientPubkey: cpk, relays: relays,
  51  	}
  52  
  53  	ctx, cancel := context.WithTimeout(context.Background(), 40*time.Second)
  54  	defer cancel()
  55  
  56  	// 1. Lookup by payment hash.
  57  	res, err := client.lookupInvoice(ctx, hash, "")
  58  	if err != nil {
  59  		t.Fatalf("lookup by hash: %v", err)
  60  	}
  61  	t.Logf("lookup by hash -> state=%q preimage=%q settled_at=%d", res.State, res.Preimage, res.SettledAt)
  62  	if !isSettled(res) {
  63  		t.Fatalf("settled invoice not recognised as settled: state=%q preimage=%q", res.State, res.Preimage)
  64  	}
  65  	if res.Preimage == "" {
  66  		t.Fatal("settled invoice has no preimage; receipt would lack proof of payment")
  67  	}
  68  
  69  	// 2. Lookup by invoice only (the fallback key shape).
  70  	res2, err := client.lookupInvoice(ctx, "", invoice)
  71  	if err != nil {
  72  		t.Fatalf("lookup by invoice: %v", err)
  73  	}
  74  	t.Logf("lookup by invoice -> state=%q hash=%s", res2.State, res2.PaymentHash)
  75  	if !isSettled(res2) {
  76  		t.Fatalf("lookup by invoice not settled: state=%q", res2.State)
  77  	}
  78  	if res2.PaymentHash != hash {
  79  		t.Fatalf("lookup by invoice returned hash %s, want %s", res2.PaymentHash, hash)
  80  	}
  81  
  82  	// 3. The receipt built from this lookup must verify and match the invoice.
  83  	sk, err := loadOrCreateZapKey(secret)
  84  	if err != nil {
  85  		t.Fatal(err)
  86  	}
  87  	z := &ZapServer{secret: sk}
  88  	pk, _ := nostr.GetPublicKey(sk)
  89  	z.pubkey = pk
  90  
  91  	zr := &ZapRequest{
  92  		ID: strings.Repeat("ab", 32), PubKey: strings.Repeat("cd", 32),
  93  		CreatedAt: time.Now().Unix(), Kind: 9734,
  94  		Tags: [][]string{{"p", pk}, {"relays", "wss://relay.mleku.dev"}},
  95  	}
  96  	raw := canonicalZapRequest(zr)
  97  	ev, err := z.buildReceipt(zr, raw, invoice, res.Preimage, time.Unix(res.SettledAt, 0))
  98  	if err != nil {
  99  		t.Fatal(err)
 100  	}
 101  	if ok, err := ev.CheckSignature(); err != nil || !ok {
 102  		t.Fatalf("receipt signature invalid: %v %v", ok, err)
 103  	}
 104  	t.Logf("receipt built and verified: id=%s pubkey=%s", ev.ID[:16], ev.PubKey[:16])
 105  }
 106