nostr-wot-sdk 0.3.2 → 0.4.1
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/CHANGELOG.md +45 -0
- package/README.md +32 -87
- package/dist/index.cjs +32 -129
- package/dist/index.d.cts +94 -159
- package/dist/index.d.ts +94 -159
- package/dist/index.js +33 -125
- package/dist/react/index.cjs +42 -126
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.d.cts +65 -57
- package/dist/react/index.d.ts +65 -57
- package/dist/react/index.js +42 -126
- package/dist/react/index.js.map +1 -1
- package/package.json +1 -11
- package/dist/local/index.cjs +0 -796
- package/dist/local/index.cjs.map +0 -1
- package/dist/local/index.d.cts +0 -376
- package/dist/local/index.d.ts +0 -376
- package/dist/local/index.js +0 -784
- package/dist/local/index.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,49 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [0.4.1] - 2025-02-05
|
|
9
|
+
|
|
10
|
+
### Changed
|
|
11
|
+
|
|
12
|
+
- **`getDetails` now returns `score`** in addition to `hops` and `paths`
|
|
13
|
+
- Extension: `{ hops: 2, paths: 5, score: 0.65 }`
|
|
14
|
+
- Oracle fallback: `{ hops: 2, paths: 5, score: 0 }` (score is 0 when using oracle)
|
|
15
|
+
|
|
16
|
+
- **`getDistanceBatch` now accepts options object** instead of boolean
|
|
17
|
+
- `{ includePaths: true }` - Include path counts
|
|
18
|
+
- `{ includeScores: true }` - Include trust scores
|
|
19
|
+
- `{ includePaths: true, includeScores: true }` - Include both
|
|
20
|
+
- Legacy boolean `true` still works (backwards compatible, same as `{ includePaths: true }`)
|
|
21
|
+
|
|
22
|
+
### Added
|
|
23
|
+
|
|
24
|
+
- New `DistanceBatchOptions` type exported for TypeScript users
|
|
25
|
+
|
|
26
|
+
## [0.4.0] - 2025-02-05
|
|
27
|
+
|
|
28
|
+
### Breaking Changes
|
|
29
|
+
|
|
30
|
+
- **Removed `useExtension` option** - The SDK now always uses the extension when available
|
|
31
|
+
- Extension-first is now the only mode; no need to opt-in
|
|
32
|
+
- `WoTOptions.useExtension` has been removed from the type
|
|
33
|
+
- Simply create `new WoT()` or `new WoT({ fallback: { myPubkey: '...' } })`
|
|
34
|
+
|
|
35
|
+
- **Removed local sync functionality** - Use the browser extension for local graph storage
|
|
36
|
+
- Removed `src/local/` directory entirely
|
|
37
|
+
- Removed `nostr-wot-sdk/local` export
|
|
38
|
+
- Extension handles all syncing and local storage
|
|
39
|
+
|
|
40
|
+
- **Trust scores now come from extension only**
|
|
41
|
+
- Removed SDK-side trust score calculation
|
|
42
|
+
- `getTrustScore(target)` no longer accepts options parameter
|
|
43
|
+
- Returns 0 when extension is not available
|
|
44
|
+
|
|
45
|
+
### Changed
|
|
46
|
+
|
|
47
|
+
- `WoT` constructor now has all parameters optional
|
|
48
|
+
- `WoTProvider` no longer has `useExtension` option
|
|
49
|
+
- Simplified codebase with extension-first architecture
|
|
50
|
+
|
|
8
51
|
## [0.3.2] - 2025-02-05
|
|
9
52
|
|
|
10
53
|
### Changed
|
|
@@ -128,6 +171,8 @@ function Profile({ pubkey }) {
|
|
|
128
171
|
- TypeScript support with full type definitions
|
|
129
172
|
- Error classes: `WoTError`, `NetworkError`, `NotFoundError`, `TimeoutError`, `ValidationError`
|
|
130
173
|
|
|
174
|
+
[0.4.1]: https://github.com/nostr-wot/nostr-wot-sdk/compare/v0.4.0...v0.4.1
|
|
175
|
+
[0.4.0]: https://github.com/nostr-wot/nostr-wot-sdk/compare/v0.3.2...v0.4.0
|
|
131
176
|
[0.3.2]: https://github.com/nostr-wot/nostr-wot-sdk/compare/v0.3.1...v0.3.2
|
|
132
177
|
[0.3.1]: https://github.com/nostr-wot/nostr-wot-sdk/compare/v0.3.0...v0.3.1
|
|
133
178
|
[0.3.0]: https://github.com/nostr-wot/nostr-wot-sdk/compare/v0.2.0...v0.3.0
|
package/README.md
CHANGED
|
@@ -16,9 +16,8 @@ Install the [Nostr WoT Extension](https://github.com/nostr-wot/nostr-wot-extensi
|
|
|
16
16
|
```javascript
|
|
17
17
|
import { WoT } from 'nostr-wot-sdk';
|
|
18
18
|
|
|
19
|
-
//
|
|
19
|
+
// The SDK automatically uses the extension when available
|
|
20
20
|
const wot = new WoT({
|
|
21
|
-
useExtension: true,
|
|
22
21
|
fallback: {
|
|
23
22
|
oracle: 'https://nostr-wot.com',
|
|
24
23
|
myPubkey: 'abc123...' // Used only if extension unavailable
|
|
@@ -33,21 +32,21 @@ console.log(hops); // 2
|
|
|
33
32
|
const trusted = await wot.isInMyWoT('def456...', { maxHops: 3 });
|
|
34
33
|
console.log(trusted); // true
|
|
35
34
|
|
|
36
|
-
// Trust score
|
|
35
|
+
// Trust score (from extension)
|
|
37
36
|
const score = await wot.getTrustScore('def456...');
|
|
38
37
|
console.log(score); // 0.72
|
|
39
38
|
```
|
|
40
39
|
|
|
41
40
|
When the extension is installed, **it always takes priority** — the SDK uses the extension's pubkey and locally-cached follow graph automatically.
|
|
42
41
|
|
|
43
|
-
### Without Extension (Oracle
|
|
42
|
+
### Without Extension (Oracle Fallback)
|
|
44
43
|
|
|
45
44
|
```javascript
|
|
46
45
|
import { WoT } from 'nostr-wot-sdk';
|
|
47
46
|
|
|
48
47
|
const wot = new WoT({
|
|
49
48
|
oracle: 'https://nostr-wot.com',
|
|
50
|
-
myPubkey: 'abc123...' // Required
|
|
49
|
+
myPubkey: 'abc123...' // Required for oracle fallback
|
|
51
50
|
});
|
|
52
51
|
|
|
53
52
|
const hops = await wot.getDistance('def456...');
|
|
@@ -59,7 +58,6 @@ const hops = await wot.getDistance('def456...');
|
|
|
59
58
|
- **Simple API** — Three methods cover most use cases
|
|
60
59
|
- **Cross-Site Trust** — Extension provides same WoT data on all websites
|
|
61
60
|
- **Offline Support** — Extension caches data locally for offline queries
|
|
62
|
-
- **Custom Scoring** — Define your own trust weights
|
|
63
61
|
- **Batch Queries** — Check multiple pubkeys efficiently
|
|
64
62
|
- **TypeScript** — Full type definitions included
|
|
65
63
|
|
|
@@ -72,17 +70,15 @@ const wot = new WoT(options);
|
|
|
72
70
|
|
|
73
71
|
| Option | Type | Default | Description |
|
|
74
72
|
|--------|------|---------|-------------|
|
|
75
|
-
| `
|
|
76
|
-
| `
|
|
77
|
-
| `myPubkey` | string | — | Your pubkey (optional with extension, required otherwise) |
|
|
73
|
+
| `oracle` | string | `'https://nostr-wot.com'` | Oracle API URL (fallback when extension unavailable) |
|
|
74
|
+
| `myPubkey` | string | — | Your pubkey (optional - fetched from extension when available) |
|
|
78
75
|
| `maxHops` | number | `3` | Default max search depth |
|
|
79
76
|
| `timeout` | number | `5000` | Request timeout (ms) |
|
|
80
|
-
| `scoring` | object | See below | Trust score weights |
|
|
81
77
|
| `fallback` | object | — | Fallback config when extension unavailable |
|
|
82
78
|
|
|
83
|
-
|
|
79
|
+
Trust scores are calculated by the extension and not configurable via the SDK.
|
|
84
80
|
|
|
85
|
-
**Note:** When
|
|
81
|
+
**Note:** When the extension is installed, it always takes priority over `myPubkey` or `oracle` settings.
|
|
86
82
|
|
|
87
83
|
### Methods
|
|
88
84
|
|
|
@@ -102,12 +98,12 @@ const trusted = await wot.isInMyWoT('def456...', { maxHops: 2 });
|
|
|
102
98
|
// Returns: boolean
|
|
103
99
|
```
|
|
104
100
|
|
|
105
|
-
#### `getTrustScore(target
|
|
101
|
+
#### `getTrustScore(target)`
|
|
106
102
|
|
|
107
|
-
Get computed trust score
|
|
103
|
+
Get computed trust score from the extension.
|
|
108
104
|
```javascript
|
|
109
105
|
const score = await wot.getTrustScore('def456...');
|
|
110
|
-
// Returns: number (0-1)
|
|
106
|
+
// Returns: number (0-1), or 0 if extension unavailable
|
|
111
107
|
```
|
|
112
108
|
|
|
113
109
|
#### `getDistanceBetween(from, to, options?)`
|
|
@@ -128,11 +124,11 @@ const results = await wot.batchCheck(['pk1...', 'pk2...', 'pk3...']);
|
|
|
128
124
|
|
|
129
125
|
#### `getDetails(target, options?)`
|
|
130
126
|
|
|
131
|
-
Get distance
|
|
127
|
+
Get distance, path count, and score details.
|
|
132
128
|
```javascript
|
|
133
129
|
const details = await wot.getDetails('def456...');
|
|
134
|
-
// Returns: { hops: 2, paths: 5 }
|
|
135
|
-
// Oracle may also return: bridges, mutual
|
|
130
|
+
// Returns: { hops: 2, paths: 5, score: 0.65 }
|
|
131
|
+
// Oracle may also return: bridges, mutual (but score will be 0)
|
|
136
132
|
```
|
|
137
133
|
|
|
138
134
|
#### `getMyPubkey()`
|
|
@@ -153,7 +149,7 @@ const usingExt = await wot.isUsingExtension();
|
|
|
153
149
|
|
|
154
150
|
#### `getExtensionConfig()`
|
|
155
151
|
|
|
156
|
-
Get extension's configuration
|
|
152
|
+
Get extension's configuration.
|
|
157
153
|
```javascript
|
|
158
154
|
const config = await wot.getExtensionConfig();
|
|
159
155
|
// Returns: { maxHops: 3, timeout: 5000, scoring: {...} } or null
|
|
@@ -161,22 +157,33 @@ const config = await wot.getExtensionConfig();
|
|
|
161
157
|
|
|
162
158
|
### Batch Operations
|
|
163
159
|
|
|
164
|
-
#### `getDistanceBatch(targets,
|
|
160
|
+
#### `getDistanceBatch(targets, options?)`
|
|
165
161
|
|
|
166
162
|
Get distances for multiple pubkeys in a single call.
|
|
167
163
|
```javascript
|
|
168
|
-
//
|
|
164
|
+
// Default (just hops)
|
|
169
165
|
const distances = await wot.getDistanceBatch(['pk1...', 'pk2...']);
|
|
170
166
|
// Returns: { 'pk1...': 2, 'pk2...': null }
|
|
171
167
|
|
|
172
|
-
// With paths
|
|
173
|
-
const
|
|
168
|
+
// With paths
|
|
169
|
+
const withPaths = await wot.getDistanceBatch(['pk1...', 'pk2...'], { includePaths: true });
|
|
174
170
|
// Returns: { 'pk1...': { hops: 2, paths: 5 }, 'pk2...': null }
|
|
171
|
+
|
|
172
|
+
// With scores
|
|
173
|
+
const withScores = await wot.getDistanceBatch(['pk1...', 'pk2...'], { includeScores: true });
|
|
174
|
+
// Returns: { 'pk1...': { hops: 2, score: 0.65 }, 'pk2...': null }
|
|
175
|
+
|
|
176
|
+
// With both
|
|
177
|
+
const full = await wot.getDistanceBatch(['pk1...', 'pk2...'], { includePaths: true, includeScores: true });
|
|
178
|
+
// Returns: { 'pk1...': { hops: 2, paths: 5, score: 0.65 }, 'pk2...': null }
|
|
179
|
+
|
|
180
|
+
// Legacy boolean still works (backwards compatible)
|
|
181
|
+
const legacy = await wot.getDistanceBatch(['pk1...'], true); // same as { includePaths: true }
|
|
175
182
|
```
|
|
176
183
|
|
|
177
184
|
#### `getTrustScoreBatch(targets)`
|
|
178
185
|
|
|
179
|
-
Get trust scores for multiple pubkeys in a single call.
|
|
186
|
+
Get trust scores for multiple pubkeys in a single call.
|
|
180
187
|
```javascript
|
|
181
188
|
const scores = await wot.getTrustScoreBatch(['pk1...', 'pk2...']);
|
|
182
189
|
// Returns: { 'pk1...': 0.72, 'pk2...': null }
|
|
@@ -248,7 +255,6 @@ The SDK automatically detects and connects to the extension using an event-based
|
|
|
248
255
|
|
|
249
256
|
```javascript
|
|
250
257
|
const wot = new WoT({
|
|
251
|
-
useExtension: true,
|
|
252
258
|
fallback: {
|
|
253
259
|
oracle: 'https://nostr-wot.com',
|
|
254
260
|
myPubkey: 'abc123...'
|
|
@@ -307,60 +313,6 @@ The SDK uses a standard event-based protocol to communicate with the extension:
|
|
|
307
313
|
| `nostr-wot-ready` | Extension → Page | API is ready at `window.nostr.wot` |
|
|
308
314
|
| `nostr-wot-error` | Extension → Page | Injection failed with error |
|
|
309
315
|
|
|
310
|
-
## Custom Scoring
|
|
311
|
-
|
|
312
|
-
Define how trust scores are calculated:
|
|
313
|
-
```javascript
|
|
314
|
-
const wot = new WoT({
|
|
315
|
-
useExtension: true,
|
|
316
|
-
scoring: {
|
|
317
|
-
// Distance weights (score multiplier per hop)
|
|
318
|
-
distanceWeights: {
|
|
319
|
-
1: 1.0, // Direct follows
|
|
320
|
-
2: 0.5, // 2 hops
|
|
321
|
-
3: 0.25, // 3 hops
|
|
322
|
-
4: 0.1, // 4+ hops
|
|
323
|
-
},
|
|
324
|
-
// Bonus values (additive)
|
|
325
|
-
mutualBonus: 0.5, // +0.5 for mutual follows
|
|
326
|
-
pathBonus: 0.1, // +0.1 per additional path
|
|
327
|
-
maxPathBonus: 0.5, // Cap path bonus at +0.5
|
|
328
|
-
}
|
|
329
|
-
});
|
|
330
|
-
```
|
|
331
|
-
|
|
332
|
-
### Scoring Formula
|
|
333
|
-
```
|
|
334
|
-
score = (baseScore × distanceWeight) + bonuses
|
|
335
|
-
|
|
336
|
-
where:
|
|
337
|
-
baseScore = 1 / (hops + 1)
|
|
338
|
-
bonuses = mutualBonus (if mutual) + min(pathBonus × (paths - 1), maxPathBonus)
|
|
339
|
-
|
|
340
|
-
Example: 2 hops + 30% path bonus = 0.5 + 0.3 = 0.80
|
|
341
|
-
```
|
|
342
|
-
|
|
343
|
-
## Server-Side Local Mode
|
|
344
|
-
|
|
345
|
-
For Node.js/server environments where the browser extension isn't available:
|
|
346
|
-
|
|
347
|
-
```javascript
|
|
348
|
-
import { LocalWoT } from 'nostr-wot-sdk/local';
|
|
349
|
-
|
|
350
|
-
const wot = new LocalWoT({
|
|
351
|
-
myPubkey: 'abc123...',
|
|
352
|
-
relays: ['wss://relay.damus.io', 'wss://nos.lol']
|
|
353
|
-
});
|
|
354
|
-
|
|
355
|
-
// Sync follow graph (2 hops from your pubkey)
|
|
356
|
-
await wot.sync({ depth: 2 });
|
|
357
|
-
|
|
358
|
-
// Now queries run locally
|
|
359
|
-
const hops = await wot.getDistance('def456...');
|
|
360
|
-
```
|
|
361
|
-
|
|
362
|
-
Storage options: `'memory'` (default), `'indexeddb'` (browser), or custom adapter.
|
|
363
|
-
|
|
364
316
|
## Framework Integration
|
|
365
317
|
|
|
366
318
|
### React
|
|
@@ -416,12 +368,6 @@ function Profile({ pubkey }) {
|
|
|
416
368
|
fallback: { myPubkey: 'abc123...' }
|
|
417
369
|
}}>
|
|
418
370
|
|
|
419
|
-
// Oracle-only mode (no extension)
|
|
420
|
-
<WoTProvider options={{
|
|
421
|
-
useExtension: false,
|
|
422
|
-
myPubkey: 'abc123...'
|
|
423
|
-
}}>
|
|
424
|
-
|
|
425
371
|
// Custom extension connection timeouts
|
|
426
372
|
<WoTProvider extensionOptions={{
|
|
427
373
|
checkTimeout: 100, // Extension detection timeout (ms)
|
|
@@ -462,8 +408,7 @@ Full type definitions included:
|
|
|
462
408
|
```typescript
|
|
463
409
|
import { WoT, DistanceResult, WoTOptions } from 'nostr-wot-sdk';
|
|
464
410
|
|
|
465
|
-
const
|
|
466
|
-
const wot = new WoT(options);
|
|
411
|
+
const wot = new WoT();
|
|
467
412
|
const result: DistanceResult | null = await wot.getDetails(pubkey);
|
|
468
413
|
const score: number = await wot.getTrustScore(pubkey);
|
|
469
414
|
```
|
package/dist/index.cjs
CHANGED
|
@@ -44,35 +44,8 @@ var ValidationError = class extends WoTError {
|
|
|
44
44
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
45
45
|
}
|
|
46
46
|
};
|
|
47
|
-
var StorageError = class extends WoTError {
|
|
48
|
-
constructor(operation, message) {
|
|
49
|
-
super(message || `Storage operation failed: ${operation}`);
|
|
50
|
-
this.name = "StorageError";
|
|
51
|
-
this.operation = operation;
|
|
52
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
var RelayError = class extends WoTError {
|
|
56
|
-
constructor(relay, message) {
|
|
57
|
-
super(message || `Relay connection failed: ${relay}`);
|
|
58
|
-
this.name = "RelayError";
|
|
59
|
-
this.relay = relay;
|
|
60
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
47
|
|
|
64
48
|
// src/utils.ts
|
|
65
|
-
var DEFAULT_SCORING = {
|
|
66
|
-
distanceWeights: {
|
|
67
|
-
1: 1,
|
|
68
|
-
2: 0.5,
|
|
69
|
-
3: 0.25,
|
|
70
|
-
4: 0.1
|
|
71
|
-
},
|
|
72
|
-
mutualBonus: 0.5,
|
|
73
|
-
pathBonus: 0.1,
|
|
74
|
-
maxPathBonus: 0.5
|
|
75
|
-
};
|
|
76
49
|
var DEFAULT_ORACLE = "https://nostr-wot.com";
|
|
77
50
|
var DEFAULT_MAX_HOPS = 3;
|
|
78
51
|
var DEFAULT_TIMEOUT = 5e3;
|
|
@@ -87,49 +60,10 @@ function isValidOracleUrl(url) {
|
|
|
87
60
|
return false;
|
|
88
61
|
}
|
|
89
62
|
}
|
|
90
|
-
function isValidRelayUrl(url) {
|
|
91
|
-
try {
|
|
92
|
-
const parsed = new URL(url);
|
|
93
|
-
return parsed.protocol === "wss:" || parsed.protocol === "ws:";
|
|
94
|
-
} catch (e) {
|
|
95
|
-
return false;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
63
|
var MAX_BATCH_SIZE = 1e4;
|
|
99
64
|
function normalizePubkey(pubkey) {
|
|
100
65
|
return pubkey.toLowerCase();
|
|
101
66
|
}
|
|
102
|
-
function mergeScoringConfig(partial) {
|
|
103
|
-
var _a, _b, _c;
|
|
104
|
-
if (!partial) return { ...DEFAULT_SCORING };
|
|
105
|
-
return {
|
|
106
|
-
distanceWeights: {
|
|
107
|
-
...DEFAULT_SCORING.distanceWeights,
|
|
108
|
-
...partial.distanceWeights
|
|
109
|
-
},
|
|
110
|
-
mutualBonus: (_a = partial.mutualBonus) != null ? _a : DEFAULT_SCORING.mutualBonus,
|
|
111
|
-
pathBonus: (_b = partial.pathBonus) != null ? _b : DEFAULT_SCORING.pathBonus,
|
|
112
|
-
maxPathBonus: (_c = partial.maxPathBonus) != null ? _c : DEFAULT_SCORING.maxPathBonus
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
function calculateTrustScore(result, scoring) {
|
|
116
|
-
var _a, _b;
|
|
117
|
-
const { hops, paths, mutual } = result;
|
|
118
|
-
const { distanceWeights, mutualBonus, pathBonus, maxPathBonus } = scoring;
|
|
119
|
-
const baseScore = 1 / (hops + 1);
|
|
120
|
-
const maxDefinedHop = Math.max(...Object.keys(distanceWeights).map(Number));
|
|
121
|
-
const distanceWeight = (_b = (_a = distanceWeights[hops]) != null ? _a : distanceWeights[maxDefinedHop]) != null ? _b : 0.1;
|
|
122
|
-
let bonuses = 0;
|
|
123
|
-
if (mutual === true) {
|
|
124
|
-
bonuses += mutualBonus;
|
|
125
|
-
}
|
|
126
|
-
if (paths > 1) {
|
|
127
|
-
const pathCountBonus = Math.min(pathBonus * (paths - 1), maxPathBonus);
|
|
128
|
-
bonuses += pathCountBonus;
|
|
129
|
-
}
|
|
130
|
-
const score = baseScore * distanceWeight + bonuses;
|
|
131
|
-
return Math.min(1, Math.max(0, score));
|
|
132
|
-
}
|
|
133
67
|
async function fetchWithTimeout(url, options = {}) {
|
|
134
68
|
const { timeout = DEFAULT_TIMEOUT, ...fetchOptions } = options;
|
|
135
69
|
const controller = new AbortController();
|
|
@@ -378,41 +312,26 @@ function resetDefaultConnector() {
|
|
|
378
312
|
|
|
379
313
|
// src/wot.ts
|
|
380
314
|
var WoT = class {
|
|
381
|
-
constructor(options) {
|
|
315
|
+
constructor(options = {}) {
|
|
382
316
|
this.extension = null;
|
|
383
317
|
this.extensionChecked = false;
|
|
384
318
|
this.extensionPubkey = null;
|
|
385
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k
|
|
386
|
-
this.
|
|
387
|
-
|
|
388
|
-
if (!this.useExtension) {
|
|
389
|
-
if (!options.myPubkey) {
|
|
390
|
-
throw new ValidationError("myPubkey is required when not using extension", "myPubkey");
|
|
391
|
-
}
|
|
392
|
-
if (!isValidPubkey(options.myPubkey)) {
|
|
393
|
-
throw new ValidationError(
|
|
394
|
-
"myPubkey must be a valid 64-character hex string",
|
|
395
|
-
"myPubkey"
|
|
396
|
-
);
|
|
397
|
-
}
|
|
319
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
|
320
|
+
this.fallbackOptions = (_a = options.fallback) != null ? _a : null;
|
|
321
|
+
if (options.myPubkey && isValidPubkey(options.myPubkey)) {
|
|
398
322
|
this.fallbackPubkey = normalizePubkey(options.myPubkey);
|
|
323
|
+
} else if ((_b = this.fallbackOptions) == null ? void 0 : _b.myPubkey) {
|
|
324
|
+
this.fallbackPubkey = normalizePubkey(this.fallbackOptions.myPubkey);
|
|
399
325
|
} else {
|
|
400
|
-
|
|
401
|
-
this.fallbackPubkey = normalizePubkey(options.myPubkey);
|
|
402
|
-
} else if ((_c = this.fallbackOptions) == null ? void 0 : _c.myPubkey) {
|
|
403
|
-
this.fallbackPubkey = normalizePubkey(this.fallbackOptions.myPubkey);
|
|
404
|
-
} else {
|
|
405
|
-
this.fallbackPubkey = null;
|
|
406
|
-
}
|
|
326
|
+
this.fallbackPubkey = null;
|
|
407
327
|
}
|
|
408
|
-
const oracleUrl = (
|
|
328
|
+
const oracleUrl = (_e = (_d = options.oracle) != null ? _d : (_c = this.fallbackOptions) == null ? void 0 : _c.oracle) != null ? _e : DEFAULT_ORACLE;
|
|
409
329
|
if (!isValidOracleUrl(oracleUrl)) {
|
|
410
330
|
throw new ValidationError("oracle must be a valid HTTPS URL", "oracle");
|
|
411
331
|
}
|
|
412
332
|
this.oracle = oracleUrl;
|
|
413
|
-
this.maxHops = (
|
|
414
|
-
this.timeout = (
|
|
415
|
-
this.scoring = mergeScoringConfig((_n = options.scoring) != null ? _n : (_m = this.fallbackOptions) == null ? void 0 : _m.scoring);
|
|
333
|
+
this.maxHops = (_h = (_g = options.maxHops) != null ? _g : (_f = this.fallbackOptions) == null ? void 0 : _f.maxHops) != null ? _h : DEFAULT_MAX_HOPS;
|
|
334
|
+
this.timeout = (_k = (_j = options.timeout) != null ? _j : (_i = this.fallbackOptions) == null ? void 0 : _i.timeout) != null ? _k : DEFAULT_TIMEOUT;
|
|
416
335
|
}
|
|
417
336
|
/**
|
|
418
337
|
* Checks if browser extension is available and returns it
|
|
@@ -420,7 +339,6 @@ var WoT = class {
|
|
|
420
339
|
*/
|
|
421
340
|
async getExtension() {
|
|
422
341
|
var _a, _b;
|
|
423
|
-
if (!this.useExtension) return null;
|
|
424
342
|
if (this.extensionChecked) return this.extension;
|
|
425
343
|
this.extensionChecked = true;
|
|
426
344
|
if (typeof window === "undefined") return null;
|
|
@@ -565,23 +483,18 @@ var WoT = class {
|
|
|
565
483
|
return distance !== null && distance <= maxHops;
|
|
566
484
|
}
|
|
567
485
|
/**
|
|
568
|
-
* Get computed trust score
|
|
486
|
+
* Get computed trust score from extension
|
|
569
487
|
* @param target - Target pubkey (hex)
|
|
570
|
-
* @
|
|
571
|
-
* @returns Trust score between 0 and 1, or 0 if not connected
|
|
488
|
+
* @returns Trust score between 0 and 1, or 0 if not connected or extension unavailable
|
|
572
489
|
*/
|
|
573
|
-
async getTrustScore(target
|
|
490
|
+
async getTrustScore(target) {
|
|
574
491
|
const normalizedTarget = this.validatePubkey(target, "target");
|
|
575
492
|
const ext = await this.getExtension();
|
|
576
493
|
if (ext) {
|
|
577
494
|
const score = await ext.getTrustScore(normalizedTarget);
|
|
578
495
|
return score != null ? score : 0;
|
|
579
496
|
}
|
|
580
|
-
|
|
581
|
-
if (!details) {
|
|
582
|
-
return 0;
|
|
583
|
-
}
|
|
584
|
-
return calculateTrustScore(details, this.scoring);
|
|
497
|
+
return 0;
|
|
585
498
|
}
|
|
586
499
|
/**
|
|
587
500
|
* Get distance between any two pubkeys
|
|
@@ -619,7 +532,7 @@ var WoT = class {
|
|
|
619
532
|
* @returns Map of pubkey to result
|
|
620
533
|
*/
|
|
621
534
|
async batchCheck(targets, options) {
|
|
622
|
-
var _a, _b, _c
|
|
535
|
+
var _a, _b, _c;
|
|
623
536
|
if (!Array.isArray(targets) || targets.length === 0) {
|
|
624
537
|
throw new ValidationError("targets must be a non-empty array", "targets");
|
|
625
538
|
}
|
|
@@ -663,17 +576,11 @@ var WoT = class {
|
|
|
663
576
|
);
|
|
664
577
|
for (const item of response.results) {
|
|
665
578
|
const inWoT = item.distance !== null && item.distance <= maxHops;
|
|
666
|
-
const score = item.distance !== null ? calculateTrustScore(
|
|
667
|
-
{
|
|
668
|
-
hops: item.distance,
|
|
669
|
-
paths: (_d = item.paths) != null ? _d : 1
|
|
670
|
-
},
|
|
671
|
-
this.scoring
|
|
672
|
-
) : 0;
|
|
673
579
|
results.set(item.pubkey, {
|
|
674
580
|
pubkey: item.pubkey,
|
|
675
581
|
distance: item.distance,
|
|
676
|
-
score,
|
|
582
|
+
score: 0,
|
|
583
|
+
// Trust scores only available via extension
|
|
677
584
|
inWoT
|
|
678
585
|
});
|
|
679
586
|
}
|
|
@@ -714,10 +621,11 @@ var WoT = class {
|
|
|
714
621
|
const myPubkey = await this.getEffectivePubkey();
|
|
715
622
|
const maxHops = (_a = options == null ? void 0 : options.maxHops) != null ? _a : this.maxHops;
|
|
716
623
|
try {
|
|
717
|
-
|
|
624
|
+
const response = await this.apiRequest(
|
|
718
625
|
`/details/${myPubkey}/${normalizedTarget}?maxHops=${maxHops}`,
|
|
719
626
|
options
|
|
720
627
|
);
|
|
628
|
+
return { ...response, score: 0 };
|
|
721
629
|
} catch (error) {
|
|
722
630
|
if (error instanceof NotFoundError) {
|
|
723
631
|
return null;
|
|
@@ -738,12 +646,6 @@ var WoT = class {
|
|
|
738
646
|
getOracle() {
|
|
739
647
|
return this.oracle;
|
|
740
648
|
}
|
|
741
|
-
/**
|
|
742
|
-
* Get the current scoring configuration
|
|
743
|
-
*/
|
|
744
|
-
getScoringConfig() {
|
|
745
|
-
return { ...this.scoring };
|
|
746
|
-
}
|
|
747
649
|
/**
|
|
748
650
|
* Check if extension is available and being used
|
|
749
651
|
*/
|
|
@@ -844,26 +746,32 @@ var WoT = class {
|
|
|
844
746
|
const normalizedTarget = this.validatePubkey(target, "target");
|
|
845
747
|
return ext.getPath(normalizedTarget);
|
|
846
748
|
}
|
|
847
|
-
async getDistanceBatch(targets,
|
|
749
|
+
async getDistanceBatch(targets, options = false) {
|
|
848
750
|
if (!Array.isArray(targets) || targets.length === 0) {
|
|
849
751
|
return {};
|
|
850
752
|
}
|
|
851
753
|
const normalizedTargets = targets.map(
|
|
852
754
|
(t, i) => this.validatePubkey(t, `targets[${i}]`)
|
|
853
755
|
);
|
|
756
|
+
const opts = typeof options === "boolean" ? { includePaths: options } : options || {};
|
|
757
|
+
const { includePaths, includeScores } = opts;
|
|
854
758
|
const ext = await this.getExtension();
|
|
855
759
|
if (ext) {
|
|
856
|
-
|
|
857
|
-
return ext.getDistanceBatch(normalizedTargets, true);
|
|
858
|
-
}
|
|
859
|
-
return ext.getDistanceBatch(normalizedTargets, false);
|
|
760
|
+
return ext.getDistanceBatch(normalizedTargets, opts);
|
|
860
761
|
}
|
|
861
|
-
if (includePaths) {
|
|
762
|
+
if (includePaths || includeScores) {
|
|
862
763
|
const results2 = {};
|
|
863
764
|
await Promise.all(
|
|
864
765
|
normalizedTargets.map(async (pubkey) => {
|
|
865
766
|
const details = await this.getDetails(pubkey);
|
|
866
|
-
|
|
767
|
+
if (!details) {
|
|
768
|
+
results2[pubkey] = null;
|
|
769
|
+
return;
|
|
770
|
+
}
|
|
771
|
+
const result = { hops: details.hops };
|
|
772
|
+
if (includePaths) result.paths = details.paths;
|
|
773
|
+
if (includeScores) result.score = details.score;
|
|
774
|
+
results2[pubkey] = result;
|
|
867
775
|
})
|
|
868
776
|
);
|
|
869
777
|
return results2;
|
|
@@ -905,25 +813,20 @@ var WoT = class {
|
|
|
905
813
|
|
|
906
814
|
exports.DEFAULT_MAX_HOPS = DEFAULT_MAX_HOPS;
|
|
907
815
|
exports.DEFAULT_ORACLE = DEFAULT_ORACLE;
|
|
908
|
-
exports.DEFAULT_SCORING = DEFAULT_SCORING;
|
|
909
816
|
exports.DEFAULT_TIMEOUT = DEFAULT_TIMEOUT;
|
|
910
817
|
exports.ExtensionConnector = ExtensionConnector;
|
|
911
818
|
exports.MAX_BATCH_SIZE = MAX_BATCH_SIZE;
|
|
912
819
|
exports.NetworkError = NetworkError;
|
|
913
820
|
exports.NotFoundError = NotFoundError;
|
|
914
|
-
exports.RelayError = RelayError;
|
|
915
|
-
exports.StorageError = StorageError;
|
|
916
821
|
exports.TimeoutError = TimeoutError;
|
|
917
822
|
exports.ValidationError = ValidationError;
|
|
918
823
|
exports.WoT = WoT;
|
|
919
824
|
exports.WoTError = WoTError;
|
|
920
|
-
exports.calculateTrustScore = calculateTrustScore;
|
|
921
825
|
exports.checkAndConnect = checkAndConnect;
|
|
922
826
|
exports.checkExtension = checkExtension;
|
|
923
827
|
exports.connectExtension = connectExtension;
|
|
924
828
|
exports.getDefaultConnector = getDefaultConnector;
|
|
925
829
|
exports.isValidOracleUrl = isValidOracleUrl;
|
|
926
830
|
exports.isValidPubkey = isValidPubkey;
|
|
927
|
-
exports.isValidRelayUrl = isValidRelayUrl;
|
|
928
831
|
exports.normalizePubkey = normalizePubkey;
|
|
929
832
|
exports.resetDefaultConnector = resetDefaultConnector;
|