@nostr-wot/graph 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 nostr-wot.com
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,104 @@
1
+ # @nostr-wot/graph
2
+
3
+ Build and query a **local** Web-of-Trust follow graph in the browser. Crawl kind:3 contact lists over relays, persist them to IndexedDB (pubkey interning + delta-encoded follow arrays), and compute social distance / trust score with an in-memory BFS — no extension and no remote Oracle required.
4
+
5
+ Ported from the Nostr WoT browser extension's proven engine and re-based onto [`@nostr-wot/relay`](../relay)'s pool.
6
+
7
+ > **Cross-origin note:** IndexedDB is origin-scoped, so a graph built on `site-a.com` cannot be read by `site-b.com`. This package is reusable on _any_ site, but each origin crawls and caches its own graph.
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ npm i @nostr-wot/graph @nostr-wot/relay nostr-tools
13
+ ```
14
+
15
+ ## Quick start
16
+
17
+ ```ts
18
+ import { WotGraph } from "@nostr-wot/graph";
19
+
20
+ const wg = new WotGraph({
21
+ namespace: "myapp", // IndexedDB partition key
22
+ relays: ["wss://relay.damus.io", "wss://nos.lol"],
23
+ });
24
+
25
+ await wg.load(); // rehydrate cached graph (instant if present)
26
+
27
+ if (wg.isStale(24 * 60 * 60 * 1000)) { // older than a day?
28
+ await wg.crawl(myPubkey, {
29
+ maxDepth: 2,
30
+ onProgress: (p) => console.log(p.depth, p.fetched, p.queued),
31
+ });
32
+ }
33
+
34
+ wg.getDistance(target); // { hops, paths } | null
35
+ wg.getScore(target); // 0..1
36
+ wg.isInWoT(target, 2); // boolean
37
+ wg.filterByWoT(pubkeys); // trusted subset, sorted by score desc
38
+ ```
39
+
40
+ ## API
41
+
42
+ | Method | Description |
43
+ |---|---|
44
+ | `load()` | Hydrate the cached graph from IndexedDB. |
45
+ | `crawl(root, opts)` | BFS-fetch kind:3 to build/refresh the graph. Concurrent calls share one in-flight promise. |
46
+ | `getDistance(pubkey)` | `{ hops, paths }` from the crawled root, or `null`. |
47
+ | `getScore(pubkey)` | Trust score `0..1` (`calculateScore`). |
48
+ | `isInWoT(pubkey, maxHops=2)` | Within `maxHops` of the root. |
49
+ | `filterByWoT(pubkeys, opts?)` | Trusted subset, sorted by score descending. |
50
+ | `getFollows(pubkey)` | Follow list (hex). |
51
+ | `stats()` | `{ nodes, edges, root, lastCrawl, maxDepth }`. |
52
+ | `isStale(ttlMs)` | Last crawl older than `ttlMs`. |
53
+ | `clear()` | Wipe this namespace. |
54
+ | `stop()` | Abort an in-flight crawl (partial data stays usable). |
55
+ | `asWoTSource()` | Adapter for `@nostr-wot/wot`. |
56
+
57
+ ### `crawl` options
58
+
59
+ ```ts
60
+ crawl(rootPubkey, {
61
+ maxDepth?: number; // default 2
62
+ onProgress?: (p) => void; // { depth, fetched, queued }
63
+ signal?: AbortSignal; // cancel
64
+ }): Promise<CrawlResult>; // { fetched, nodes, edges, depth, durationMs, stoppedEarly }
65
+ ```
66
+
67
+ Crawls tolerate unreachable relays and only throw `CrawlError` if **zero** relays are configured. In Node without an IndexedDB polyfill the store runs memory-only (crawl/query work, nothing persists).
68
+
69
+ ## React (`/react`)
70
+
71
+ ```tsx
72
+ import { WotGraphProvider, useWotGraph, useDistance, useCrawl } from "@nostr-wot/graph/react";
73
+
74
+ <WotGraphProvider namespace="myapp" relays={["wss://relay.damus.io"]}>
75
+ <App />
76
+ </WotGraphProvider>;
77
+
78
+ function Trust({ pubkey }: { pubkey: string }) {
79
+ const dist = useDistance(pubkey);
80
+ return <span>{dist ? `${dist.hops} hops` : "unknown"}</span>;
81
+ }
82
+
83
+ function CrawlButton({ me }: { me: string }) {
84
+ const { crawl, crawling, progress } = useCrawl();
85
+ return (
86
+ <button disabled={crawling} onClick={() => crawl(me, { maxDepth: 2 })}>
87
+ {crawling ? `depth ${progress?.depth ?? 0}…` : "Build graph"}
88
+ </button>
89
+ );
90
+ }
91
+ ```
92
+
93
+ ## Use as a `@nostr-wot/wot` source
94
+
95
+ ```ts
96
+ import { WoT } from "@nostr-wot/wot";
97
+
98
+ const wot = new WoT({ source: wg.asWoTSource() }); // resolve locally instead of the Oracle
99
+ await wot.getDistance(target);
100
+ ```
101
+
102
+ ## License
103
+
104
+ MIT