nostr-wot-sdk 0.1.0 → 0.3.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 +1 -1
- package/README.md +191 -12
- package/dist/index.cjs +450 -58
- package/dist/index.d.cts +275 -17
- package/dist/index.d.ts +275 -17
- package/dist/index.js +442 -59
- package/dist/local/index.cjs +23 -0
- package/dist/local/index.cjs.map +1 -1
- package/dist/local/index.js +23 -0
- package/dist/local/index.js.map +1 -1
- package/dist/react/index.cjs +506 -74
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.d.cts +217 -9
- package/dist/react/index.d.ts +217 -9
- package/dist/react/index.js +507 -76
- package/dist/react/index.js.map +1 -1
- package/package.json +12 -4
- package/dist/index.cjs.map +0 -1
- package/dist/index.js.map +0 -1
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@ npm install nostr-wot-sdk
|
|
|
11
11
|
|
|
12
12
|
### With Browser Extension (Recommended)
|
|
13
13
|
|
|
14
|
-
Install the [Nostr WoT Extension](https://github.com/
|
|
14
|
+
Install the [Nostr WoT Extension](https://github.com/nostr-wot/nostr-wot-extension) for the best experience. The extension downloads your follow graph locally and works across all websites.
|
|
15
15
|
|
|
16
16
|
```javascript
|
|
17
17
|
import { WoT } from 'nostr-wot-sdk';
|
|
@@ -72,7 +72,7 @@ const wot = new WoT(options);
|
|
|
72
72
|
|
|
73
73
|
| Option | Type | Default | Description |
|
|
74
74
|
|--------|------|---------|-------------|
|
|
75
|
-
| `useExtension` | boolean | `false
|
|
75
|
+
| `useExtension` | boolean | `false`* | Use browser extension if available (recommended) |
|
|
76
76
|
| `oracle` | string | `'https://nostr-wot.com'` | Oracle API URL (fallback) |
|
|
77
77
|
| `myPubkey` | string | — | Your pubkey (optional with extension, required otherwise) |
|
|
78
78
|
| `maxHops` | number | `3` | Default max search depth |
|
|
@@ -80,6 +80,8 @@ const wot = new WoT(options);
|
|
|
80
80
|
| `scoring` | object | See below | Trust score weights |
|
|
81
81
|
| `fallback` | object | — | Fallback config when extension unavailable |
|
|
82
82
|
|
|
83
|
+
*Note: When using the React `WoTProvider`, `useExtension` defaults to `true`.
|
|
84
|
+
|
|
83
85
|
**Note:** When `useExtension: true` and the extension is installed, the extension's pubkey and data are always used, regardless of `myPubkey` or `oracle` settings.
|
|
84
86
|
|
|
85
87
|
### Methods
|
|
@@ -157,9 +159,79 @@ const config = await wot.getExtensionConfig();
|
|
|
157
159
|
// Returns: { maxHops: 3, timeout: 5000, scoring: {...} } or null
|
|
158
160
|
```
|
|
159
161
|
|
|
162
|
+
### Batch Operations
|
|
163
|
+
|
|
164
|
+
#### `getDistanceBatch(targets)`
|
|
165
|
+
|
|
166
|
+
Get distances for multiple pubkeys in a single call.
|
|
167
|
+
```javascript
|
|
168
|
+
const distances = await wot.getDistanceBatch(['pk1...', 'pk2...']);
|
|
169
|
+
// Returns: { 'pk1...': 2, 'pk2...': null }
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
#### `getTrustScoreBatch(targets)`
|
|
173
|
+
|
|
174
|
+
Get trust scores for multiple pubkeys in a single call.
|
|
175
|
+
```javascript
|
|
176
|
+
const scores = await wot.getTrustScoreBatch(['pk1...', 'pk2...']);
|
|
177
|
+
// Returns: { 'pk1...': 0.72, 'pk2...': null }
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
#### `filterByWoT(pubkeys, options?)`
|
|
181
|
+
|
|
182
|
+
Filter a list of pubkeys to only those within the Web of Trust.
|
|
183
|
+
```javascript
|
|
184
|
+
const trusted = await wot.filterByWoT(['pk1...', 'pk2...', 'pk3...']);
|
|
185
|
+
// Returns: ['pk1...', 'pk3...'] (only those in WoT)
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### Graph Queries (Extension-only)
|
|
189
|
+
|
|
190
|
+
These methods require the browser extension and return `null`/empty when unavailable.
|
|
191
|
+
|
|
192
|
+
#### `getFollows(pubkey?)`
|
|
193
|
+
|
|
194
|
+
Get the follow list for a pubkey (defaults to your pubkey).
|
|
195
|
+
```javascript
|
|
196
|
+
const follows = await wot.getFollows();
|
|
197
|
+
// Returns: ['pk1...', 'pk2...', ...]
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
#### `getCommonFollows(pubkey)`
|
|
201
|
+
|
|
202
|
+
Get mutual follows between you and a target.
|
|
203
|
+
```javascript
|
|
204
|
+
const common = await wot.getCommonFollows('def456...');
|
|
205
|
+
// Returns: ['pk1...', 'pk2...'] (people you both follow)
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
#### `getPath(target)`
|
|
209
|
+
|
|
210
|
+
Get the actual path from you to a target.
|
|
211
|
+
```javascript
|
|
212
|
+
const path = await wot.getPath('def456...');
|
|
213
|
+
// Returns: ['myPubkey', 'friend', 'friendOfFriend', 'def456...']
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
#### `getStats()`
|
|
217
|
+
|
|
218
|
+
Get graph statistics.
|
|
219
|
+
```javascript
|
|
220
|
+
const stats = await wot.getStats();
|
|
221
|
+
// Returns: { nodes: 50000, edges: 150000, lastSync: 1699999999, size: '12 MB' }
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
#### `isConfigured()`
|
|
225
|
+
|
|
226
|
+
Check if the extension is configured and ready.
|
|
227
|
+
```javascript
|
|
228
|
+
const status = await wot.isConfigured();
|
|
229
|
+
// Returns: { configured: true, mode: 'local', hasLocalGraph: true }
|
|
230
|
+
```
|
|
231
|
+
|
|
160
232
|
## Browser Extension
|
|
161
233
|
|
|
162
|
-
Install the [Nostr WoT Extension](https://github.com/
|
|
234
|
+
Install the [Nostr WoT Extension](https://github.com/nostr-wot/nostr-wot-extension) for:
|
|
163
235
|
|
|
164
236
|
- **Local Data** — Downloads and caches your follow graph locally
|
|
165
237
|
- **Fast Queries** — No network requests needed after sync
|
|
@@ -167,7 +239,7 @@ Install the [Nostr WoT Extension](https://github.com/mappingbitcoin/nostr-wot-ex
|
|
|
167
239
|
- **Privacy** — Queries never leave your browser
|
|
168
240
|
- **Offline** — Works without internet once synced
|
|
169
241
|
|
|
170
|
-
The SDK automatically detects
|
|
242
|
+
The SDK automatically detects and connects to the extension using an event-based handshake. When the extension is present, it **always takes priority** over oracle settings.
|
|
171
243
|
|
|
172
244
|
```javascript
|
|
173
245
|
const wot = new WoT({
|
|
@@ -186,6 +258,50 @@ if (await wot.isUsingExtension()) {
|
|
|
186
258
|
}
|
|
187
259
|
```
|
|
188
260
|
|
|
261
|
+
### Extension Connection Utilities
|
|
262
|
+
|
|
263
|
+
For advanced use cases, the SDK exports low-level extension connection functions:
|
|
264
|
+
|
|
265
|
+
```javascript
|
|
266
|
+
import {
|
|
267
|
+
checkExtension, // Check if extension is installed
|
|
268
|
+
connectExtension, // Connect to the extension
|
|
269
|
+
checkAndConnect, // Check and connect in one call
|
|
270
|
+
ExtensionConnector // Stateful connector class
|
|
271
|
+
} from 'nostr-wot-sdk';
|
|
272
|
+
|
|
273
|
+
// Check if extension is installed (100ms timeout)
|
|
274
|
+
const isInstalled = await checkExtension();
|
|
275
|
+
|
|
276
|
+
// Connect to extension (5s timeout)
|
|
277
|
+
const extension = await connectExtension();
|
|
278
|
+
|
|
279
|
+
// Or do both in one call
|
|
280
|
+
const result = await checkAndConnect();
|
|
281
|
+
if (result.state === 'connected') {
|
|
282
|
+
const distance = await result.extension.getDistance('target...');
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// For stateful connection management
|
|
286
|
+
const connector = new ExtensionConnector();
|
|
287
|
+
connector.subscribe((result) => {
|
|
288
|
+
console.log('State changed:', result.state);
|
|
289
|
+
});
|
|
290
|
+
await connector.connect();
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
#### Extension Events
|
|
294
|
+
|
|
295
|
+
The SDK uses a standard event-based protocol to communicate with the extension:
|
|
296
|
+
|
|
297
|
+
| Event | Direction | Purpose |
|
|
298
|
+
|-------|-----------|---------|
|
|
299
|
+
| `nostr-wot-check` | Page → Extension | Check if extension installed |
|
|
300
|
+
| `nostr-wot-present` | Extension → Page | Response confirming presence |
|
|
301
|
+
| `nostr-wot-connect` | Page → Extension | Request API injection |
|
|
302
|
+
| `nostr-wot-ready` | Extension → Page | API is ready at `window.nostr.wot` |
|
|
303
|
+
| `nostr-wot-error` | Extension → Page | Injection failed with error |
|
|
304
|
+
|
|
189
305
|
## Custom Scoring
|
|
190
306
|
|
|
191
307
|
Define how trust scores are calculated:
|
|
@@ -241,19 +357,33 @@ Storage options: `'memory'` (default), `'indexeddb'` (browser), or custom adapte
|
|
|
241
357
|
## Framework Integration
|
|
242
358
|
|
|
243
359
|
### React
|
|
360
|
+
|
|
361
|
+
The SDK provides first-class React support with automatic extension detection and connection. Just wrap your app with `WoTProvider` and you're ready to go — no additional configuration needed.
|
|
362
|
+
|
|
244
363
|
```javascript
|
|
245
|
-
import { useWoT,
|
|
364
|
+
import { WoTProvider, useWoT, useExtension } from 'nostr-wot-sdk/react';
|
|
246
365
|
|
|
247
|
-
// Wrap your app
|
|
366
|
+
// Wrap your app - automatically connects to extension
|
|
248
367
|
function App() {
|
|
249
368
|
return (
|
|
250
|
-
<WoTProvider
|
|
369
|
+
<WoTProvider>
|
|
251
370
|
<YourApp />
|
|
252
371
|
</WoTProvider>
|
|
253
372
|
);
|
|
254
373
|
}
|
|
255
374
|
|
|
256
|
-
//
|
|
375
|
+
// Check extension status anywhere
|
|
376
|
+
function ExtensionStatus() {
|
|
377
|
+
const { isConnected, isConnecting, isInstalled, error } = useExtension();
|
|
378
|
+
|
|
379
|
+
if (isConnecting) return <span>Connecting to extension...</span>;
|
|
380
|
+
if (!isInstalled) return <span>Install the WoT extension for best experience</span>;
|
|
381
|
+
if (error) return <span>Error: {error}</span>;
|
|
382
|
+
if (isConnected) return <span>Connected to extension!</span>;
|
|
383
|
+
return null;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// Use WoT data in components
|
|
257
387
|
function Profile({ pubkey }) {
|
|
258
388
|
const { distance, score, loading } = useWoT(pubkey);
|
|
259
389
|
|
|
@@ -271,14 +401,63 @@ function Profile({ pubkey }) {
|
|
|
271
401
|
}
|
|
272
402
|
```
|
|
273
403
|
|
|
404
|
+
#### Provider Options
|
|
405
|
+
|
|
406
|
+
```javascript
|
|
407
|
+
// With fallback for when extension is not installed
|
|
408
|
+
<WoTProvider options={{
|
|
409
|
+
fallback: { myPubkey: 'abc123...' }
|
|
410
|
+
}}>
|
|
411
|
+
|
|
412
|
+
// Oracle-only mode (no extension)
|
|
413
|
+
<WoTProvider options={{
|
|
414
|
+
useExtension: false,
|
|
415
|
+
myPubkey: 'abc123...'
|
|
416
|
+
}}>
|
|
417
|
+
|
|
418
|
+
// Custom extension connection timeouts
|
|
419
|
+
<WoTProvider extensionOptions={{
|
|
420
|
+
checkTimeout: 100, // Extension detection timeout (ms)
|
|
421
|
+
connectTimeout: 5000 // Connection timeout (ms)
|
|
422
|
+
}}>
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
#### Available Hooks
|
|
426
|
+
|
|
427
|
+
| Hook | Description |
|
|
428
|
+
|------|-------------|
|
|
429
|
+
| `useWoT(pubkey)` | Get distance, score, and details for a pubkey |
|
|
430
|
+
| `useIsInWoT(pubkey)` | Check if pubkey is in your WoT (boolean) |
|
|
431
|
+
| `useTrustScore(pubkey)` | Get trust score only |
|
|
432
|
+
| `useBatchWoT(pubkeys[])` | Check multiple pubkeys efficiently |
|
|
433
|
+
| `useExtension()` | Get extension connection state |
|
|
434
|
+
| `useWoTInstance()` | Get raw WoT instance for advanced usage |
|
|
435
|
+
|
|
436
|
+
#### Extension State
|
|
437
|
+
|
|
438
|
+
The `useExtension()` hook provides detailed extension status:
|
|
439
|
+
|
|
440
|
+
```javascript
|
|
441
|
+
const {
|
|
442
|
+
state, // 'idle' | 'checking' | 'connecting' | 'connected' | 'not-installed' | 'error'
|
|
443
|
+
isConnected, // Extension is connected and ready
|
|
444
|
+
isConnecting, // Currently checking/connecting
|
|
445
|
+
isInstalled, // Extension is installed (may still be connecting)
|
|
446
|
+
isChecked, // Initial check complete
|
|
447
|
+
error, // Error message if connection failed
|
|
448
|
+
connect, // Function to manually retry connection
|
|
449
|
+
} = useExtension();
|
|
450
|
+
```
|
|
451
|
+
|
|
274
452
|
## TypeScript
|
|
275
453
|
|
|
276
454
|
Full type definitions included:
|
|
277
455
|
```typescript
|
|
278
456
|
import { WoT, DistanceResult, WoTOptions } from 'nostr-wot-sdk';
|
|
279
457
|
|
|
280
|
-
const
|
|
281
|
-
const
|
|
458
|
+
const options: WoTOptions = { useExtension: true };
|
|
459
|
+
const wot = new WoT(options);
|
|
460
|
+
const result: DistanceResult | null = await wot.getDetails(pubkey);
|
|
282
461
|
const score: number = await wot.getTrustScore(pubkey);
|
|
283
462
|
```
|
|
284
463
|
|
|
@@ -299,8 +478,8 @@ try {
|
|
|
299
478
|
|
|
300
479
|
## Related
|
|
301
480
|
|
|
302
|
-
- [Nostr WoT Extension](https://github.com/
|
|
303
|
-
- [WoT Oracle](https://github.com/
|
|
481
|
+
- [Nostr WoT Extension](https://github.com/nostr-wot/nostr-wot-extension) — Browser extension (recommended)
|
|
482
|
+
- [WoT Oracle](https://github.com/nostr-wot/nostr-wot-oracle) — Backend service
|
|
304
483
|
- [nostr-wot.com](https://nostr-wot.com) — Public oracle & docs
|
|
305
484
|
|
|
306
485
|
## License
|