ncc-06-js 0.3.7 → 0.3.8
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/package.json +1 -1
- package/src/ncc02.js +18 -35
- package/src/resolver.js +6 -9
package/package.json
CHANGED
package/src/ncc02.js
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
import { finalizeEvent, getPublicKey, validateEvent, verifyEvent } from 'nostr-tools/pure';
|
|
2
|
-
import { NCC02Builder } from 'ncc-02-js';
|
|
3
2
|
|
|
4
3
|
const DEFAULT_KIND = 30059;
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
6
|
* Build an NCC-02 service record event.
|
|
8
|
-
* The event includes `d`, `u`, `k`, and `exp` tags and is signed with the provided secret key.
|
|
9
|
-
* @param {object} options
|
|
10
7
|
*/
|
|
11
8
|
export function buildNcc02ServiceRecord({
|
|
12
9
|
secretKey,
|
|
@@ -18,38 +15,27 @@ export function buildNcc02ServiceRecord({
|
|
|
18
15
|
kind = DEFAULT_KIND
|
|
19
16
|
}) {
|
|
20
17
|
if (!secretKey) {
|
|
21
|
-
throw new Error('secretKey is required
|
|
18
|
+
throw new Error('secretKey is required');
|
|
22
19
|
}
|
|
20
|
+
const timestamp = createdAt ?? Math.floor(Date.now() / 1000);
|
|
21
|
+
const expiresAt = timestamp + Number(expirySeconds);
|
|
22
|
+
const tags = [
|
|
23
|
+
['d', serviceId],
|
|
24
|
+
['exp', expiresAt.toString()]
|
|
25
|
+
];
|
|
26
|
+
if (endpoint) tags.push(['u', endpoint]);
|
|
27
|
+
if (fingerprint) tags.push(['k', fingerprint]);
|
|
23
28
|
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
expiryDays
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
// If createdAt or kind override is needed, we must re-sign.
|
|
36
|
-
if (createdAt || kind !== DEFAULT_KIND) {
|
|
37
|
-
const template = {
|
|
38
|
-
...event,
|
|
39
|
-
created_at: createdAt ?? event.created_at,
|
|
40
|
-
kind: kind ?? event.kind,
|
|
41
|
-
id: undefined,
|
|
42
|
-
sig: undefined
|
|
43
|
-
};
|
|
44
|
-
return finalizeEvent(template, secretKey);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return event;
|
|
29
|
+
const event = {
|
|
30
|
+
kind,
|
|
31
|
+
pubkey: getPublicKey(secretKey),
|
|
32
|
+
created_at: timestamp,
|
|
33
|
+
tags,
|
|
34
|
+
content: ''
|
|
35
|
+
};
|
|
36
|
+
return finalizeEvent(event, secretKey);
|
|
48
37
|
}
|
|
49
38
|
|
|
50
|
-
/**
|
|
51
|
-
* Extract the NCC-02 relevant tags from an event.
|
|
52
|
-
*/
|
|
53
39
|
export function parseNcc02Tags(event) {
|
|
54
40
|
if (!event || !Array.isArray(event.tags)) {
|
|
55
41
|
return {};
|
|
@@ -57,9 +43,6 @@ export function parseNcc02Tags(event) {
|
|
|
57
43
|
return Object.fromEntries(event.tags);
|
|
58
44
|
}
|
|
59
45
|
|
|
60
|
-
/**
|
|
61
|
-
* Verify an NCC-02 service record, ensuring signature, author, `d` tag, and expiration.
|
|
62
|
-
*/
|
|
63
46
|
export function validateNcc02(event, { expectedAuthor, expectedD, now, allowExpired = false } = {}) {
|
|
64
47
|
if (!event || event.kind !== DEFAULT_KIND) {
|
|
65
48
|
return false;
|
|
@@ -80,4 +63,4 @@ export function validateNcc02(event, { expectedAuthor, expectedD, now, allowExpi
|
|
|
80
63
|
return false;
|
|
81
64
|
}
|
|
82
65
|
return true;
|
|
83
|
-
}
|
|
66
|
+
}
|
package/src/resolver.js
CHANGED
|
@@ -44,18 +44,15 @@ export async function resolveServiceEndpoint(options = {}) {
|
|
|
44
44
|
|
|
45
45
|
// 1. Resolve NCC-02 Service Record using the library
|
|
46
46
|
let serviceRecord;
|
|
47
|
+
const resolver = ncc02Resolver || new NCC02Resolver(bootstrapRelays, { pool });
|
|
47
48
|
try {
|
|
48
|
-
|
|
49
|
-
serviceRecord = await resolver.resolve(servicePubkey, serviceId, {
|
|
50
|
-
// We can pass options if needed, e.g. minLevel
|
|
51
|
-
});
|
|
49
|
+
serviceRecord = await resolver.resolve(servicePubkey, serviceId, {});
|
|
52
50
|
} catch (err) {
|
|
53
|
-
// Map library errors or rethrow?
|
|
54
|
-
// The library throws generic Error or NCC02Error.
|
|
55
|
-
// We can just let it propagate or wrap.
|
|
56
|
-
// Existing code threw "No valid NCC-02 service record available".
|
|
57
|
-
// We'll let the library error bubble up as it provides more detail.
|
|
58
51
|
throw err;
|
|
52
|
+
} finally {
|
|
53
|
+
if (!ncc02Resolver) {
|
|
54
|
+
resolver.close();
|
|
55
|
+
}
|
|
59
56
|
}
|
|
60
57
|
|
|
61
58
|
// 2. Resolve NCC-05 Locator
|