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

main.go

TEXT FILE

4.1 KB · Public source code for developers, search crawlers and AI agents

Download / RawBack to folder
Go · 163 lines
1package main
2
3import (
4	"encoding/json"
5	"flag"
6	"fmt"
7	"os"
8	"strconv"
9	"strings"
10	"time"
11
12	"github.com/gorilla/websocket"
13)
14
15type event struct {
16	ID   string
17	Kind int
18	Tags [][]string
19}
20type relayResult struct {
21	URL      string `json:"url"`
22	Requests int    `json:"requests"`
23	EOSE     bool   `json:"eose"`
24	Error    string `json:"error,omitempty"`
25}
26type kindResult struct {
27	Requests     int `json:"requests"`
28	PositiveBids int `json:"positive_bids"`
29}
30type bid struct {
31	ID        string `json:"event_id"`
32	Kind      int    `json:"kind"`
33	Millisats int64  `json:"max_bid_msat"`
34}
35
36func readRelay(url string, since, until int64, limit int) (relayResult, []event) {
37	report := relayResult{URL: url}
38	dialer := websocket.Dialer{HandshakeTimeout: 6 * time.Second}
39	conn, _, err := dialer.Dial(url, nil)
40	if err != nil {
41		report.Error = err.Error()
42		return report, nil
43	}
44	defer conn.Close()
45	conn.SetReadLimit(2 << 20)
46	conn.SetReadDeadline(time.Now().Add(8 * time.Second))
47	kinds := make([]int, 1000)
48	for i := range kinds {
49		kinds[i] = 5000 + i
50	}
51	filter := map[string]any{"kinds": kinds, "since": since, "until": until, "limit": limit}
52	if err = conn.WriteJSON([]any{"REQ", "wab-discovery", filter}); err != nil {
53		report.Error = err.Error()
54		return report, nil
55	}
56	requests := map[string]event{}
57	for {
58		var msg []json.RawMessage
59		if err = conn.ReadJSON(&msg); err != nil {
60			report.Error = err.Error()
61			break
62		}
63		if len(msg) < 2 {
64			continue
65		}
66		var op, sub string
67		_ = json.Unmarshal(msg[0], &op)
68		_ = json.Unmarshal(msg[1], &sub)
69		if sub != "wab-discovery" {
70			continue
71		}
72		if op == "EOSE" {
73			report.EOSE = true
74			break
75		}
76		if op == "CLOSED" {
77			report.Error = "subscription closed by relay"
78			if len(msg) > 2 {
79				_ = json.Unmarshal(msg[2], &report.Error)
80			}
81			break
82		}
83		if op != "EVENT" || len(msg) < 3 {
84			continue
85		}
86		var item event
87		var timestamp struct {
88			CreatedAt int64 `json:"created_at"`
89		}
90		if json.Unmarshal(msg[2], &item) != nil || json.Unmarshal(msg[2], &timestamp) != nil {
91			continue
92		}
93		if len(item.ID) != 64 || item.Kind < 5000 || item.Kind > 5999 || timestamp.CreatedAt < since || timestamp.CreatedAt > until {
94			continue
95		}
96		requests[item.ID] = item
97		if len(requests) >= limit {
98			break
99		}
100	}
101	report.Requests = len(requests)
102	if !report.EOSE && report.Error == "" {
103		report.Error = "client request cap reached before EOSE"
104	}
105	items := make([]event, 0, len(requests))
106	for _, item := range requests {
107		items = append(items, item)
108	}
109	return report, items
110}
111func main() {
112	relays := flag.String("relays", "wss://relay.damus.io,wss://nos.lol,wss://relay.primal.net", "comma-separated relay endpoints")
113	flag.Parse()
114	until := time.Now().UTC().Truncate(time.Second)
115	since := until.Add(-7 * 24 * time.Hour)
116	const limit = 200
117	type sample struct {
118		report relayResult
119		items  []event
120	}
121	results := make(chan sample)
122	endpoints := strings.Split(*relays, ",")
123	for _, endpoint := range endpoints {
124		go func(url string) {
125			report, items := readRelay(url, since.Unix(), until.Unix(), limit)
126			results <- sample{report, items}
127		}(endpoint)
128	}
129	unique := map[string]event{}
130	reports := map[string]relayResult{}
131	for range endpoints {
132		result := <-results
133		reports[result.report.URL] = result.report
134		for _, item := range result.items {
135			unique[item.ID] = item
136		}
137	}
138	counts := map[int]kindResult{}
139	bids := []bid{}
140	for _, item := range unique {
141		row := counts[item.Kind]
142		row.Requests++
143		for _, tag := range item.Tags {
144			if len(tag) < 2 || tag[0] != "bid" {
145				continue
146			}
147			price, err := strconv.ParseInt(tag[1], 10, 64)
148			if err == nil && price > 0 {
149				row.PositiveBids++
150				bids = append(bids, bid{item.ID, item.Kind, price})
151				break
152			}
153		}
154		counts[item.Kind] = row
155	}
156	report := map[string]any{"since": since, "until": until, "limit_per_relay": limit, "relays": reports, "unique_requests": len(unique), "by_kind": counts, "positive_bids": bids, "provider_quotes_checked": false, "payment_verification_performed": false, "event_signatures_verified": false}
157	encoder := json.NewEncoder(os.Stdout)
158	encoder.SetIndent("", "  ")
159	if err := encoder.Encode(report); err != nil {
160		fmt.Fprintln(os.Stderr, err)
161		os.Exit(1)
162	}
163}
Read-only source · Select a line number to link directly · Raw download above

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