THAMER
Sovereign Bitcoin Wallet

WALLET AI BITCOIN

Your Bitcoin Wallet. Your Keys. Your Engine.

Self-hosted Bitcoin software built for developers, applications, and AI agents.

CREATE WALLET

Linking NIP-90 Buyer Requests to Provider Results with Go

permalink THAMER
#ai-agents#bitcoin#nip-90#nostr#wab

The second scanner extends the first experiment from request discovery to linked provider activity. It queries nos.lol and relay.primal.net over a seven-day window ending 2026-11-28 06:19:24 UTC. Each initial query stops at 60 distinct event IDs before EOSE. The combined 120 records yield 115 distinct requests after deduplication.

The implementation uses gorilla/websocket and groups request IDs into batches of at most 25. Five batches across two relays produce ten follow-up queries. The filters request kinds 6000–6999, 7000 and 9735 with matching #e references, using the same time window and a per-query limit of 200. All ten follow-up queries report EOSE. The scanner deduplicates response events by ID and counts each matching request once per response event.

Go · 223 lines
1package main
2
3import (
4	"encoding/json"
5
6	"fmt"
7	"os"
8
9	"time"
10
11	"github.com/gorilla/websocket"
12)
13
14type event struct {
15	ID   string
16	Kind int
17	Tags [][]string
18}
19type relayResult struct {
20	URL      string `json:"url"`
21	Requests int    `json:"requests"`
22	EOSE     bool   `json:"eose"`
23	Error    string `json:"error,omitempty"`
24}
25type kindResult struct {
26	Requests     int `json:"requests"`
27	PositiveBids int `json:"positive_bids"`
28}
29type bid struct {
30	ID        string `json:"event_id"`
31	Kind      int    `json:"kind"`
32	Millisats int64  `json:"max_bid_msat"`
33}
34
35func readRelay(url string, since, until int64, limit int, ids []string) (relayResult, []event) {
36	report := relayResult{URL: url}
37	dialer := websocket.Dialer{HandshakeTimeout: 6 * time.Second}
38	conn, _, err := dialer.Dial(url, nil)
39	if err != nil {
40		report.Error = err.Error()
41		return report, nil
42	}
43	defer conn.Close()
44	conn.SetReadLimit(2 << 20)
45	conn.SetReadDeadline(time.Now().Add(8 * time.Second))
46	kinds := make([]int, 1000)
47	for i := range kinds {
48		kinds[i] = 5000 + i
49	}
50	if len(ids) > 0 {
51		kinds = make([]int, 0, 1002)
52		for i := 6000; i < 7000; i++ {
53			kinds = append(kinds, i)
54		}
55		kinds = append(kinds, 7000, 9735)
56	}
57	filter := map[string]any{"kinds": kinds, "since": since, "until": until, "limit": limit}
58	if len(ids) > 0 {
59		filter["#e"] = ids
60	}
61	if err = conn.WriteJSON([]any{"REQ", "wab-discovery", filter}); err != nil {
62		report.Error = err.Error()
63		return report, nil
64	}
65	requests := map[string]event{}
66	for {
67		var msg []json.RawMessage
68		if err = conn.ReadJSON(&msg); err != nil {
69			report.Error = err.Error()
70			break
71		}
72		if len(msg) < 2 {
73			continue
74		}
75		var op, sub string
76		_ = json.Unmarshal(msg[0], &op)
77		_ = json.Unmarshal(msg[1], &sub)
78		if sub != "wab-discovery" {
79			continue
80		}
81		if op == "EOSE" {
82			report.EOSE = true
83			break
84		}
85		if op == "CLOSED" {
86			report.Error = "subscription closed by relay"
87			if len(msg) > 2 {
88				_ = json.Unmarshal(msg[2], &report.Error)
89			}
90			break
91		}
92		if op != "EVENT" || len(msg) < 3 {
93			continue
94		}
95		var item event
96		var timestamp struct {
97			CreatedAt int64 `json:"created_at"`
98		}
99		if json.Unmarshal(msg[2], &item) != nil || json.Unmarshal(msg[2], &timestamp) != nil {
100			continue
101		}
102		if len(item.ID) != 64 || (len(ids) == 0 && (item.Kind < 5000 || item.Kind > 5999)) || timestamp.CreatedAt < since || timestamp.CreatedAt > until {
103			continue
104		}
105		requests[item.ID] = item
106		if len(requests) >= limit {
107			break
108		}
109	}
110	report.Requests = len(requests)
111	if !report.EOSE && report.Error == "" {
112		report.Error = "client request cap reached before EOSE"
113	}
114	items := make([]event, 0, len(requests))
115	for _, item := range requests {
116		items = append(items, item)
117	}
118	return report, items
119}
120
121type outcome struct {
122	Feedback    int `json:"feedback"`
123	Quotes      int `json:"positive_amount_events"`
124	Invoices    int `json:"invoice_events"`
125	Results     int `json:"results"`
126	ZapReceipts int `json:"direct_zap_receipt_candidates"`
127}
128
129func main() {
130	end := time.Now().UTC().Truncate(time.Second)
131	start := end.Add(-7 * 24 * time.Hour)
132	urls := []string{"wss://nos.lol", "wss://relay.primal.net"}
133	requests := map[string]event{}
134	reports := map[string]relayResult{}
135	for _, url := range urls {
136		report, items := readRelay(url, start.Unix(), end.Unix(), 60, nil)
137		reports[url] = report
138		for _, item := range items {
139			requests[item.ID] = item
140		}
141	}
142	ids := make([]string, 0, len(requests))
143	for id := range requests {
144		ids = append(ids, id)
145	}
146	linked := map[string]event{}
147	var responseQueries []relayResult
148	for i := 0; i < len(ids); i += 25 {
149		batch := ids[i:min(i+25, len(ids))]
150		for _, url := range urls {
151			report, items := readRelay(url, start.Unix(), end.Unix(), 200, batch)
152			responseQueries = append(responseQueries, report)
153			for _, item := range items {
154				linked[item.ID] = item
155			}
156		}
157	}
158	jobs := map[string]outcome{}
159	for _, item := range linked {
160		matched := map[string]bool{}
161		for _, tag := range item.Tags {
162			if len(tag) > 1 && tag[0] == "e" {
163				if _, ok := requests[tag[1]]; ok {
164					matched[tag[1]] = true
165				}
166			}
167		}
168		for id := range matched {
169			row := jobs[id]
170			switch {
171			case item.Kind == 7000:
172				row.Feedback++
173			case item.Kind >= 6000 && item.Kind < 7000:
174				row.Results++
175			case item.Kind == 9735:
176				row.ZapReceipts++
177			default:
178				continue
179			}
180			if item.Kind != 9735 {
181				for _, tag := range item.Tags {
182					if len(tag) < 2 || tag[0] != "amount" {
183						continue
184					}
185					var amount int64
186					if _, err := fmt.Sscan(tag[1], &amount); err == nil && amount > 0 {
187						row.Quotes++
188					}
189					if len(tag) > 2 && tag[2] != "" {
190						row.Invoices++
191					}
192					break
193				}
194			}
195			jobs[id] = row
196		}
197	}
198	totals := map[string]int{}
199	for _, row := range jobs {
200		if row.Feedback > 0 {
201			totals["with_feedback"]++
202		}
203		if row.Quotes > 0 {
204			totals["with_positive_amount"]++
205		}
206		if row.Invoices > 0 {
207			totals["with_invoice_field"]++
208		}
209		if row.Results > 0 {
210			totals["with_result"]++
211		}
212		if row.ZapReceipts > 0 {
213			totals["with_direct_zap_receipt_candidate"]++
214		}
215	}
216	result := map[string]any{"since": start, "until": end, "unique_requests": len(requests), "counts": totals, "buyer_queries": reports, "response_queries": responseQueries, "observed_jobs": jobs, "verified_payments": nil, "event_signatures_verified": false, "receipt_scope": "direct e-tag links to buyer requests only; receipts linked only to provider events were not queried"}
217	enc := json.NewEncoder(os.Stdout)
218	enc.SetIndent("", "  ")
219	if err := enc.Encode(result); err != nil {
220		fmt.Fprintln(os.Stderr, err)
221		os.Exit(1)
222	}
223}
Source: /repo/wab-nip90-v2/main.go · Raw file · Read-only excerpt

The sample contains 78 requests linked to at least one result (67.83% of 115) and one linked to feedback. The remaining 37 have no linked result observed. These are request-level counts, not a count of unique providers or accepted jobs. Feedback and result categories need not be mutually exclusive.

EOSE describes the stored-event response to a particular subscription, not complete network history. Initial request caps, relay selection, time boundaries and missing events limit coverage. The code checks ID length and timestamps but does not recompute IDs or verify signatures. It also does not check that a result kind equals the corresponding request kind plus 1000 or validate provider identity and result semantics. Treat links as observed references rather than authenticated fulfillment.

A future measurement could use fixed cohorts, overlapping time windows, deterministic batch ordering and explicit signature and relationship validation. These are proposed improvements, not features tested in this run.

Related research
https://thamer.org/ask/wab-nip90-v2-linked-activity
https://thamer.org/ask/wab-nip90-go-relay-sample
https://thamer.org/docs/wab-nip90-payment-evidence

Primary reference (as cited by the original experiment)
https://github.com/nostr-protocol/nips/blob/master/90.md

Questions & Answers

Explore all questions →

Latest from the Lab

THAMER illustrated developer portrait and signature

ABOUT THE DEVELOPER

Independent Software Developer specializing in Bitcoin Script, Golang, and AI Agents.

Software engineering · Independent research · Open-source projects