package main import ( "context" "os" "strings" "testing" "github.com/nbd-wtf/go-nostr" "time" ) // TestLookupSettledInvoiceAgainstWallet exercises the real settle-detection path // against a live wallet: it looks up an invoice the wallet already settled and // asserts this code recognises it as settled, that both lookup key shapes work, // and that the resulting receipt verifies under the advertised key. // // It is skipped unless NWC_INTEGRATION=1, so the default `go test` stays // hermetic. To run it, grab any settled incoming transaction from the wallet: // // sqlite3 /opt/alby-hub/data/nwc.db \ // "select payment_hash, payment_request from transactions // where type='incoming' and state='SETTLED' order by id desc limit 1;" // // NWC_INTEGRATION=1 NWC_URI='nostr+walletconnect://...' \ // SETTLED_HASH= SETTLED_INVOICE= go test -run LookupSettled -v func TestLookupSettledInvoiceAgainstWallet(t *testing.T) { if os.Getenv("NWC_INTEGRATION") != "1" { t.Skip("set NWC_INTEGRATION=1 to run against a live wallet") } uri := os.Getenv("NWC_URI") if uri == "" { t.Fatal("NWC_URI required") } hash := os.Getenv("SETTLED_HASH") invoice := os.Getenv("SETTLED_INVOICE") if hash == "" || invoice == "" { t.Fatal("SETTLED_HASH and SETTLED_INVOICE required") } spk, secret, relays, err := parseNWCURI(uri) if err != nil { t.Fatal(err) } cpk, err := nostr.GetPublicKey(secret) if err != nil { t.Fatal(err) } client := &nwcClient{ servicePubkey: spk, clientSecret: secret, clientPubkey: cpk, relays: relays, } ctx, cancel := context.WithTimeout(context.Background(), 40*time.Second) defer cancel() // 1. Lookup by payment hash. res, err := client.lookupInvoice(ctx, hash, "") if err != nil { t.Fatalf("lookup by hash: %v", err) } t.Logf("lookup by hash -> state=%q preimage=%q settled_at=%d", res.State, res.Preimage, res.SettledAt) if !isSettled(res) { t.Fatalf("settled invoice not recognised as settled: state=%q preimage=%q", res.State, res.Preimage) } if res.Preimage == "" { t.Fatal("settled invoice has no preimage; receipt would lack proof of payment") } // 2. Lookup by invoice only (the fallback key shape). res2, err := client.lookupInvoice(ctx, "", invoice) if err != nil { t.Fatalf("lookup by invoice: %v", err) } t.Logf("lookup by invoice -> state=%q hash=%s", res2.State, res2.PaymentHash) if !isSettled(res2) { t.Fatalf("lookup by invoice not settled: state=%q", res2.State) } if res2.PaymentHash != hash { t.Fatalf("lookup by invoice returned hash %s, want %s", res2.PaymentHash, hash) } // 3. The receipt built from this lookup must verify and match the invoice. sk, err := loadOrCreateZapKey(secret) if err != nil { t.Fatal(err) } z := &ZapServer{secret: sk} pk, _ := nostr.GetPublicKey(sk) z.pubkey = pk zr := &ZapRequest{ ID: strings.Repeat("ab", 32), PubKey: strings.Repeat("cd", 32), CreatedAt: time.Now().Unix(), Kind: 9734, Tags: [][]string{{"p", pk}, {"relays", "wss://relay.mleku.dev"}}, } raw := canonicalZapRequest(zr) ev, err := z.buildReceipt(zr, raw, invoice, res.Preimage, time.Unix(res.SettledAt, 0)) if err != nil { t.Fatal(err) } if ok, err := ev.CheckSignature(); err != nil || !ok { t.Fatalf("receipt signature invalid: %v %v", ok, err) } t.Logf("receipt built and verified: id=%s pubkey=%s", ev.ID[:16], ev.PubKey[:16]) }