chirli 0.1.0 → 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/ARCHITECTURE.md +18 -20
- package/CONTRIBUTING.md +7 -3
- package/README.md +63 -55
- package/SECURITY.md +11 -11
- package/dist/allowlist.d.ts +3 -0
- package/dist/allowlist.js +9 -0
- package/dist/announcements.d.ts +4 -4
- package/dist/announcements.js +66 -34
- package/dist/attachments.d.ts +43 -0
- package/dist/attachments.js +340 -0
- package/dist/blossom.d.ts +2 -2
- package/dist/blossom.js +120 -66
- package/dist/bot-handlers.d.ts +9 -18
- package/dist/bot-handlers.js +125 -66
- package/dist/bot.d.ts +17 -20
- package/dist/bot.js +150 -55
- package/dist/client.d.ts +43 -39
- package/dist/client.js +476 -290
- package/dist/command-discovery.d.ts +2 -2
- package/dist/command-discovery.js +16 -7
- package/dist/command-schema.d.ts +2 -2
- package/dist/command-schema.js +62 -31
- package/dist/commands.d.ts +7 -6
- package/dist/commands.js +116 -51
- package/dist/defaults.js +13 -6
- package/dist/errors.d.ts +12 -12
- package/dist/errors.js +55 -18
- package/dist/inbox.d.ts +9 -7
- package/dist/inbox.js +107 -62
- package/dist/index.d.ts +25 -24
- package/dist/index.js +11 -11
- package/dist/key-sync.d.ts +4 -4
- package/dist/key-sync.js +88 -50
- package/dist/keys.d.ts +7 -6
- package/dist/keys.js +187 -93
- package/dist/message-storage-sqlite.d.ts +1 -1
- package/dist/message-storage-sqlite.js +32 -27
- package/dist/message-storage.d.ts +2 -2
- package/dist/messaging-metadata.d.ts +4 -4
- package/dist/messaging-metadata.js +73 -46
- package/dist/metadata.d.ts +3 -3
- package/dist/metadata.js +29 -15
- package/dist/modes.d.ts +7 -7
- package/dist/modes.js +53 -24
- package/dist/nip4e-compat.d.ts +2 -2
- package/dist/nip4e-compat.js +2 -2
- package/dist/nipcd.d.ts +14 -14
- package/dist/nipcd.js +225 -126
- package/dist/protocol.d.ts +7 -6
- package/dist/protocol.js +101 -54
- package/dist/public-inbox.d.ts +17 -15
- package/dist/public-inbox.js +129 -138
- package/dist/public-thread.d.ts +7 -4
- package/dist/public-thread.js +205 -88
- package/dist/relay-pool.d.ts +5 -5
- package/dist/relay-pool.js +172 -67
- package/dist/relay-roles.js +22 -13
- package/dist/relay.d.ts +11 -11
- package/dist/relay.js +194 -100
- package/dist/request-timing.d.ts +2 -2
- package/dist/request-timing.js +19 -7
- package/dist/session.js +7 -5
- package/dist/signer.d.ts +1 -1
- package/dist/signers/bunker-relays.d.ts +2 -2
- package/dist/signers/bunker-relays.js +16 -13
- package/dist/signers/bunker.d.ts +2 -2
- package/dist/signers/bunker.js +57 -37
- package/dist/signers/nsec.d.ts +2 -2
- package/dist/signers/nsec.js +8 -8
- package/dist/signers.d.ts +8 -8
- package/dist/signers.js +25 -18
- package/dist/storage-sqlite.d.ts +3 -1
- package/dist/storage-sqlite.js +110 -58
- package/dist/storage.d.ts +10 -6
- package/dist/store.d.ts +12 -8
- package/dist/store.js +156 -78
- package/docs/ACCESS.md +14 -25
- package/docs/API.md +125 -79
- package/docs/KEYS.md +3 -3
- package/docs/NIP-CD.md +29 -19
- package/docs/PUBLIC.md +33 -23
- package/docs/RECOVERY.md +7 -5
- package/docs/RELAYS.md +6 -6
- package/docs/SESSIONS.md +25 -21
- package/docs/SIGNERS.md +5 -3
- package/docs/STORAGE.md +15 -15
- package/docs/TUTORIAL.md +42 -39
- package/package.json +1 -37
- package/dist/inbound-policy.d.ts +0 -4
- package/dist/inbound-policy.js +0 -9
package/dist/key-sync.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { generateSecretKey, getPublicKey, nip44 } from
|
|
2
|
-
import { hexKey, signChecked, validTimestamp } from
|
|
3
|
-
import { websocketURL } from
|
|
4
|
-
import { relayUnion } from
|
|
5
|
-
import { verifyEvent } from
|
|
6
|
-
import { InboxSubscription } from
|
|
1
|
+
import { generateSecretKey, getPublicKey, nip44, } from "nostr-tools";
|
|
2
|
+
import { hexKey, signChecked, validTimestamp, } from "./protocol.js";
|
|
3
|
+
import { websocketURL } from "./metadata.js";
|
|
4
|
+
import { relayUnion } from "./relay-roles.js";
|
|
5
|
+
import { verifyEvent } from "nostr-tools";
|
|
6
|
+
import { InboxSubscription } from "./inbox.js";
|
|
7
7
|
/** Same-identity NIP-4E transfer only; identity signing secrets are never exportable. */
|
|
8
8
|
export class KeySync {
|
|
9
9
|
identity;
|
|
@@ -27,21 +27,27 @@ export class KeySync {
|
|
|
27
27
|
this.exportKey = exportKey;
|
|
28
28
|
this.current = current;
|
|
29
29
|
this.timeout = options.timeoutMs ?? 30000;
|
|
30
|
-
if (!Number.isSafeInteger(this.timeout) ||
|
|
31
|
-
|
|
30
|
+
if (!Number.isSafeInteger(this.timeout) ||
|
|
31
|
+
this.timeout < 100 ||
|
|
32
|
+
this.timeout > 120000) {
|
|
33
|
+
throw new TypeError("keySync.timeoutMs must be in 100..120000");
|
|
32
34
|
}
|
|
33
|
-
this.responses =
|
|
35
|
+
this.responses =
|
|
36
|
+
store.metadata("key-sync-responses") ?? {};
|
|
34
37
|
}
|
|
35
38
|
verified(event, kind) {
|
|
36
39
|
try {
|
|
37
|
-
return event.kind === kind &&
|
|
40
|
+
return (event.kind === kind &&
|
|
41
|
+
event.pubkey === this.identity.pubkey &&
|
|
42
|
+
validTimestamp(event.created_at) &&
|
|
43
|
+
verifyEvent(JSON.parse(JSON.stringify(event))));
|
|
38
44
|
}
|
|
39
45
|
catch {
|
|
40
46
|
return false;
|
|
41
47
|
}
|
|
42
48
|
}
|
|
43
49
|
single(event, name) {
|
|
44
|
-
const tags = event.tags.filter(tag => tag[0] === name);
|
|
50
|
+
const tags = event.tags.filter((tag) => tag[0] === name);
|
|
45
51
|
return tags.length === 1 ? tags[0][1] : undefined;
|
|
46
52
|
}
|
|
47
53
|
handle(event) {
|
|
@@ -49,14 +55,23 @@ export class KeySync {
|
|
|
49
55
|
return Promise.resolve();
|
|
50
56
|
}
|
|
51
57
|
const now = Math.floor(Date.now() / 1000);
|
|
52
|
-
if (!this.verified(event, 4454) ||
|
|
58
|
+
if (!this.verified(event, 4454) ||
|
|
59
|
+
event.content !== "" ||
|
|
60
|
+
event.created_at < now - 300 ||
|
|
61
|
+
event.created_at > now + 60) {
|
|
53
62
|
return Promise.resolve();
|
|
54
63
|
}
|
|
55
|
-
const p = this.single(event,
|
|
56
|
-
|
|
64
|
+
const p = this.single(event, "P"), alias = this.single(event, "pubkey"), target = this.single(event, "n");
|
|
65
|
+
const requester = p ?? alias;
|
|
66
|
+
if (!hexKey(requester) ||
|
|
67
|
+
(p && alias && p !== alias) ||
|
|
68
|
+
event.tags
|
|
69
|
+
.filter((t) => ["P", "pubkey", "n"].includes(t[0]))
|
|
70
|
+
.some((t) => t.length !== 2) ||
|
|
71
|
+
["P", "pubkey", "n"].some((name) => event.tags.filter((t) => t[0] === name).length > 1) ||
|
|
72
|
+
(target !== undefined && !hexKey(target))) {
|
|
57
73
|
return Promise.resolve();
|
|
58
74
|
}
|
|
59
|
-
const requester = (p ?? alias);
|
|
60
75
|
if (this.responses[event.id]?.done) {
|
|
61
76
|
return Promise.resolve();
|
|
62
77
|
}
|
|
@@ -80,7 +95,7 @@ export class KeySync {
|
|
|
80
95
|
let content, sender;
|
|
81
96
|
try {
|
|
82
97
|
sender = getPublicKey(ephemeral);
|
|
83
|
-
content = nip44.v2.encrypt(Buffer.from(key).toString(
|
|
98
|
+
content = nip44.v2.encrypt(Buffer.from(key).toString("hex"), nip44.v2.utils.getConversationKey(ephemeral, requester));
|
|
84
99
|
}
|
|
85
100
|
finally {
|
|
86
101
|
ephemeral.fill(0);
|
|
@@ -88,17 +103,24 @@ export class KeySync {
|
|
|
88
103
|
const response = await signChecked(this.identity, {
|
|
89
104
|
kind: 4455,
|
|
90
105
|
created_at: now,
|
|
91
|
-
tags: [
|
|
92
|
-
|
|
106
|
+
tags: [
|
|
107
|
+
["P", sender],
|
|
108
|
+
["p", this.identity.pubkey],
|
|
109
|
+
["p", requester],
|
|
110
|
+
],
|
|
111
|
+
content,
|
|
93
112
|
});
|
|
94
113
|
if (this.stopped || this.generation !== generation) {
|
|
95
114
|
return;
|
|
96
115
|
}
|
|
97
|
-
const hints = event.tags
|
|
116
|
+
const hints = event.tags
|
|
117
|
+
.filter((t) => t[0] === "relay" && websocketURL(t[1]))
|
|
118
|
+
.slice(0, 8)
|
|
119
|
+
.map((t) => t[1]);
|
|
98
120
|
saved = {
|
|
99
121
|
event: response,
|
|
100
122
|
relays: relayUnion(this.relays, hints),
|
|
101
|
-
done: false
|
|
123
|
+
done: false,
|
|
102
124
|
};
|
|
103
125
|
this.responses[event.id] = saved;
|
|
104
126
|
this.persist();
|
|
@@ -113,7 +135,8 @@ export class KeySync {
|
|
|
113
135
|
this.persist();
|
|
114
136
|
await this.transport.publish(saved.relays, saved.event);
|
|
115
137
|
this.responses[event.id] = {
|
|
116
|
-
...saved,
|
|
138
|
+
...saved,
|
|
139
|
+
done: true,
|
|
117
140
|
};
|
|
118
141
|
this.persist();
|
|
119
142
|
})().finally(() => this.jobs.delete(event.id));
|
|
@@ -123,11 +146,11 @@ export class KeySync {
|
|
|
123
146
|
persist() {
|
|
124
147
|
const entries = Object.entries(this.responses).sort((a, b) => b[1].event.created_at - a[1].event.created_at);
|
|
125
148
|
this.responses = Object.fromEntries(entries.slice(0, 512));
|
|
126
|
-
this.store.saveMetadata(
|
|
149
|
+
this.store.saveMetadata("key-sync-responses", this.responses);
|
|
127
150
|
}
|
|
128
151
|
request(expected) {
|
|
129
152
|
if (this.stopped) {
|
|
130
|
-
return Promise.reject(new Error(
|
|
153
|
+
return Promise.reject(new Error("Key synchronization stopped"));
|
|
131
154
|
}
|
|
132
155
|
const existing = this.requests.get(expected);
|
|
133
156
|
if (existing) {
|
|
@@ -139,7 +162,7 @@ export class KeySync {
|
|
|
139
162
|
}
|
|
140
163
|
async runRequest(expected) {
|
|
141
164
|
if (!hexKey(expected) || expected === this.identity.pubkey) {
|
|
142
|
-
throw new Error(
|
|
165
|
+
throw new Error("Only separated encryption keys can be synchronized");
|
|
143
166
|
}
|
|
144
167
|
const secret = generateSecretKey(), client = getPublicKey(secret);
|
|
145
168
|
let sub, timer, stop;
|
|
@@ -148,13 +171,11 @@ export class KeySync {
|
|
|
148
171
|
resolve = yes;
|
|
149
172
|
reject = no;
|
|
150
173
|
});
|
|
151
|
-
void response.catch(() => {
|
|
152
|
-
});
|
|
174
|
+
void response.catch(() => { });
|
|
153
175
|
const canceled = new Promise((_resolve, reject) => {
|
|
154
176
|
abort = reject;
|
|
155
177
|
});
|
|
156
|
-
void canceled.catch(() => {
|
|
157
|
-
});
|
|
178
|
+
void canceled.catch(() => { });
|
|
158
179
|
const cancel = (error) => {
|
|
159
180
|
if (aborted) {
|
|
160
181
|
return;
|
|
@@ -171,7 +192,7 @@ export class KeySync {
|
|
|
171
192
|
throw aborted;
|
|
172
193
|
}
|
|
173
194
|
};
|
|
174
|
-
stop = () => cancel(new Error(
|
|
195
|
+
stop = () => cancel(new Error("Key synchronization stopped"));
|
|
175
196
|
this.cancel.add(stop);
|
|
176
197
|
try {
|
|
177
198
|
sub = new InboxSubscription({
|
|
@@ -181,19 +202,21 @@ export class KeySync {
|
|
|
181
202
|
filter: {
|
|
182
203
|
kinds: [4455],
|
|
183
204
|
authors: [this.identity.pubkey],
|
|
184
|
-
|
|
205
|
+
"#p": [client],
|
|
185
206
|
},
|
|
186
207
|
reconnect: false,
|
|
187
208
|
recovery: false,
|
|
188
|
-
error: () => {
|
|
189
|
-
},
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
209
|
+
error: () => { },
|
|
210
|
+
reconnected: async () => { },
|
|
211
|
+
receive: (event) => {
|
|
212
|
+
if (finished ||
|
|
213
|
+
!this.verified(event, 4455) ||
|
|
214
|
+
!event.tags.some((t) => t[0] === "p" && t[1] === client) ||
|
|
215
|
+
event.created_at < Math.floor(Date.now() / 1000) - 300 ||
|
|
216
|
+
event.created_at > Math.floor(Date.now() / 1000) + 60) {
|
|
194
217
|
return;
|
|
195
218
|
}
|
|
196
|
-
const sender = this.single(event,
|
|
219
|
+
const sender = this.single(event, "P");
|
|
197
220
|
if (!hexKey(sender)) {
|
|
198
221
|
return;
|
|
199
222
|
}
|
|
@@ -202,33 +225,45 @@ export class KeySync {
|
|
|
202
225
|
if (!hexKey(raw)) {
|
|
203
226
|
return;
|
|
204
227
|
}
|
|
205
|
-
const value = new Uint8Array(Buffer.from(raw,
|
|
206
|
-
if (getPublicKey(value) !== expected ||
|
|
228
|
+
const value = new Uint8Array(Buffer.from(raw, "hex"));
|
|
229
|
+
if (getPublicKey(value) !== expected ||
|
|
230
|
+
this.current() !== expected) {
|
|
207
231
|
value.fill(0);
|
|
208
232
|
return;
|
|
209
233
|
}
|
|
210
234
|
finished = true;
|
|
211
235
|
resolve(value);
|
|
212
236
|
}
|
|
213
|
-
catch {
|
|
237
|
+
catch {
|
|
238
|
+
/* Ignore unrelated, forged or undecryptable responses. */
|
|
214
239
|
}
|
|
215
|
-
}
|
|
240
|
+
},
|
|
216
241
|
});
|
|
217
|
-
timer = setTimeout(() => cancel(new Error(
|
|
218
|
-
await Promise.race([sub.ready, response.then(() => {
|
|
219
|
-
}), canceled]);
|
|
242
|
+
timer = setTimeout(() => cancel(new Error("Key synchronization timed out")), this.timeout);
|
|
243
|
+
await Promise.race([sub.ready, response.then(() => { }), canceled]);
|
|
220
244
|
check();
|
|
221
245
|
if (finished) {
|
|
222
246
|
return await response;
|
|
223
247
|
}
|
|
224
|
-
const request = await Promise.race([
|
|
248
|
+
const request = await Promise.race([
|
|
249
|
+
signChecked(this.identity, {
|
|
225
250
|
kind: 4454,
|
|
226
251
|
created_at: Math.floor(Date.now() / 1000),
|
|
227
|
-
tags: [
|
|
228
|
-
|
|
229
|
-
|
|
252
|
+
tags: [
|
|
253
|
+
["P", client],
|
|
254
|
+
["pubkey", client],
|
|
255
|
+
...this.relays.map((relay) => ["relay", relay]),
|
|
256
|
+
["n", expected],
|
|
257
|
+
],
|
|
258
|
+
content: "",
|
|
259
|
+
}),
|
|
260
|
+
canceled,
|
|
261
|
+
]);
|
|
230
262
|
check();
|
|
231
|
-
await Promise.race([
|
|
263
|
+
await Promise.race([
|
|
264
|
+
this.transport.publish(this.relays, request),
|
|
265
|
+
canceled,
|
|
266
|
+
]);
|
|
232
267
|
check();
|
|
233
268
|
return await response;
|
|
234
269
|
}
|
|
@@ -250,6 +285,9 @@ export class KeySync {
|
|
|
250
285
|
}
|
|
251
286
|
}
|
|
252
287
|
async idle() {
|
|
253
|
-
await Promise.allSettled([
|
|
288
|
+
await Promise.allSettled([
|
|
289
|
+
...this.jobs.values(),
|
|
290
|
+
...this.requests.values(),
|
|
291
|
+
]);
|
|
254
292
|
}
|
|
255
293
|
}
|
package/dist/keys.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { type Identity } from
|
|
2
|
-
import type { MessagingMetadata } from
|
|
3
|
-
import type { Store } from
|
|
4
|
-
import type { Transport } from
|
|
5
|
-
import type { ProtocolMode } from
|
|
1
|
+
import { type Identity } from "./protocol.js";
|
|
2
|
+
import type { MessagingMetadata } from "./messaging-metadata.js";
|
|
3
|
+
import type { Store } from "./store.js";
|
|
4
|
+
import type { Transport } from "./relay.js";
|
|
5
|
+
import type { ProtocolMode } from "./modes.js";
|
|
6
6
|
export interface KeyRotationOptions {
|
|
7
7
|
/** Automatic rotation while started; minimum 1000 ms. Configuration authorizes declarations. */
|
|
8
8
|
intervalMs: number;
|
|
@@ -38,10 +38,11 @@ export declare class EncryptionKeys {
|
|
|
38
38
|
private metadata?;
|
|
39
39
|
private state;
|
|
40
40
|
private operation?;
|
|
41
|
-
readonly rotation?: Required<Pick<KeyRotationOptions,
|
|
41
|
+
readonly rotation?: Required<Pick<KeyRotationOptions, "intervalMs" | "retainLegacyKeys">> & Pick<KeyRotationOptions, "legacyMaxAgeMs">;
|
|
42
42
|
constructor(identity: Identity, mode: ProtocolMode, store: Store, transport: Transport, relays: string[], options?: false | KeyRotationOptions, publishRelays?: string[][], metadata?: MessagingMetadata | undefined);
|
|
43
43
|
private save;
|
|
44
44
|
private apply;
|
|
45
|
+
private activeKey;
|
|
45
46
|
current(): string;
|
|
46
47
|
status(): EncryptionKeyStatus;
|
|
47
48
|
private knows;
|