ncc-06-js 0.4.0 → 0.4.2
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/keys.js +3 -1
- package/src/resolver.js +46 -11
- package/src/tls.js +10 -3
package/package.json
CHANGED
package/src/keys.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { generateSecretKey, getPublicKey } from 'nostr-tools/pure';
|
|
1
|
+
import { generateSecretKey, getPublicKey as getPk } from 'nostr-tools/pure';
|
|
2
2
|
import { nip19 } from 'nostr-tools';
|
|
3
3
|
|
|
4
|
+
export const getPublicKey = getPk;
|
|
5
|
+
|
|
4
6
|
/**
|
|
5
7
|
* Generate a deterministic keypair, returning all common formats.
|
|
6
8
|
*/
|
package/src/resolver.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import WebSocket from 'ws';
|
|
2
|
+
import { SimplePool } from 'nostr-tools';
|
|
2
3
|
import { NCC05Resolver } from 'ncc-05-js';
|
|
3
|
-
import {
|
|
4
|
+
import { validateNcc02, parseNcc02Tags } from './ncc02.js';
|
|
4
5
|
import { validateLocatorFreshness, normalizeLocatorEndpoints } from './ncc05.js';
|
|
5
6
|
import { choosePreferredEndpoint } from './selector.js';
|
|
6
7
|
|
|
@@ -39,22 +40,54 @@ export async function resolveServiceEndpoint(options = {}) {
|
|
|
39
40
|
throw new Error('At least one bootstrap relay is required');
|
|
40
41
|
}
|
|
41
42
|
|
|
43
|
+
// 1. Resolve NCC-02 Service Record
|
|
42
44
|
const timestamp = now ?? Math.floor(Date.now() / 1000);
|
|
43
45
|
const locatorResolver = resolveLocator ?? defaultResolveLocator;
|
|
44
46
|
|
|
45
|
-
// 1. Resolve NCC-02 Service Record using the library
|
|
46
47
|
let serviceRecord;
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
48
|
+
if (ncc02Resolver) {
|
|
49
|
+
serviceRecord = await ncc02Resolver.resolve(servicePubkey, serviceId, {});
|
|
50
|
+
} else {
|
|
51
|
+
const poolToUse = pool || new SimplePool();
|
|
52
|
+
try {
|
|
53
|
+
const filters = [{
|
|
54
|
+
kinds: [30059],
|
|
55
|
+
authors: [servicePubkey],
|
|
56
|
+
'#d': [serviceId]
|
|
57
|
+
}];
|
|
58
|
+
const events = await new Promise((resolve) => {
|
|
59
|
+
const results = [];
|
|
60
|
+
const sub = poolToUse.subscribeMany(bootstrapRelays, filters, {
|
|
61
|
+
onevent(e) { results.push(e); },
|
|
62
|
+
oneose() { sub.close(); resolve(results); }
|
|
63
|
+
});
|
|
64
|
+
setTimeout(() => { sub.close(); resolve(results); }, publicationRelayTimeoutMs);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// Sort and validate
|
|
68
|
+
const validEvents = events
|
|
69
|
+
.filter(e => validateNcc02(e, { expectedAuthor: servicePubkey, expectedD: serviceId, now: timestamp }))
|
|
70
|
+
.sort((a, b) => b.created_at - a.created_at);
|
|
71
|
+
|
|
72
|
+
if (validEvents[0]) {
|
|
73
|
+
const tags = parseNcc02Tags(validEvents[0]);
|
|
74
|
+
serviceRecord = {
|
|
75
|
+
endpoint: tags.u,
|
|
76
|
+
fingerprint: tags.k,
|
|
77
|
+
expiry: Number(tags.exp),
|
|
78
|
+
eventId: validEvents[0].id,
|
|
79
|
+
pubkey: validEvents[0].pubkey
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
} finally {
|
|
83
|
+
if (!pool) poolToUse.close(bootstrapRelays);
|
|
55
84
|
}
|
|
56
85
|
}
|
|
57
86
|
|
|
87
|
+
if (!serviceRecord) {
|
|
88
|
+
throw new Error(`No valid NCC-02 record found for ${serviceId}`);
|
|
89
|
+
}
|
|
90
|
+
|
|
58
91
|
// 2. Resolve NCC-05 Locator
|
|
59
92
|
const locatorPayload = await locatorResolver({
|
|
60
93
|
bootstrapRelays,
|
|
@@ -156,7 +189,9 @@ async function defaultResolveLocator({
|
|
|
156
189
|
gossip: false
|
|
157
190
|
});
|
|
158
191
|
} finally {
|
|
159
|
-
resolver.close
|
|
192
|
+
if (resolver && typeof resolver.close === 'function') {
|
|
193
|
+
resolver.close();
|
|
194
|
+
}
|
|
160
195
|
}
|
|
161
196
|
}
|
|
162
197
|
|
package/src/tls.js
CHANGED
|
@@ -32,15 +32,22 @@ export async function ensureSelfSignedCert({
|
|
|
32
32
|
return { type: 2, value: name };
|
|
33
33
|
});
|
|
34
34
|
|
|
35
|
-
const generated = selfsigned.generate(attrs, {
|
|
35
|
+
const generated = await selfsigned.generate(attrs, {
|
|
36
36
|
algorithm: 'rsa',
|
|
37
37
|
keySize: 2048,
|
|
38
38
|
days: 365,
|
|
39
39
|
extensions: [{ name: 'subjectAltName', altNames: altNameObjects }]
|
|
40
40
|
});
|
|
41
41
|
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
const privateKey = generated.private || generated.privateKey;
|
|
43
|
+
const certificate = generated.cert || generated.certificate;
|
|
44
|
+
|
|
45
|
+
if (!privateKey || !certificate) {
|
|
46
|
+
throw new Error(`Self-signed cert generation failed. Keys returned: ${Object.keys(generated).join(', ')}`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
fs.writeFileSync(keyPath, privateKey, 'utf-8');
|
|
50
|
+
fs.writeFileSync(certPath, certificate, 'utf-8');
|
|
44
51
|
|
|
45
52
|
return { keyPath, certPath };
|
|
46
53
|
}
|