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/keys.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { generateSecretKey, getPublicKey, hexKey, identityMessagingKey, signChecked } from
|
|
2
|
-
import { publicationGroups } from
|
|
3
|
-
import { messagingMetadata } from
|
|
4
|
-
import { Nip4eCompatibilityError } from
|
|
5
|
-
const secret = (value) => new Uint8Array(Buffer.from(value,
|
|
1
|
+
import { generateSecretKey, getPublicKey, hexKey, identityMessagingKey, signChecked, } from "./protocol.js";
|
|
2
|
+
import { publicationGroups } from "./relay-roles.js";
|
|
3
|
+
import { messagingMetadata } from "./metadata.js";
|
|
4
|
+
import { Nip4eCompatibilityError } from "./errors.js";
|
|
5
|
+
const secret = (value) => new Uint8Array(Buffer.from(value, "hex"));
|
|
6
6
|
const pub = (value) => getPublicKey(secret(value));
|
|
7
|
-
const encoded = (value) => Buffer.from(value).toString(
|
|
7
|
+
const encoded = (value) => Buffer.from(value).toString("hex");
|
|
8
8
|
/** One account-local codec-protected keyring, independent of message-mode database scope. */
|
|
9
9
|
export class EncryptionKeys {
|
|
10
10
|
identity;
|
|
@@ -27,49 +27,64 @@ export class EncryptionKeys {
|
|
|
27
27
|
this.metadata = metadata;
|
|
28
28
|
identityMessagingKey(identity);
|
|
29
29
|
if (options) {
|
|
30
|
-
if (mode !==
|
|
31
|
-
throw new TypeError(
|
|
30
|
+
if (mode !== "nip4e") {
|
|
31
|
+
throw new TypeError("keyRotation requires explicit mode: nip4e");
|
|
32
32
|
}
|
|
33
|
-
if (!Number.isSafeInteger(options.intervalMs) ||
|
|
34
|
-
|
|
33
|
+
if (!Number.isSafeInteger(options.intervalMs) ||
|
|
34
|
+
options.intervalMs < 1000 ||
|
|
35
|
+
options.intervalMs > Number.MAX_SAFE_INTEGER / 2) {
|
|
36
|
+
throw new TypeError("intervalMs must be a safe integer >= 1000");
|
|
35
37
|
}
|
|
36
38
|
const retain = options.retainLegacyKeys ?? 5;
|
|
37
39
|
if (!Number.isSafeInteger(retain) || retain < 1 || retain > 100) {
|
|
38
|
-
throw new TypeError(
|
|
40
|
+
throw new TypeError("retainLegacyKeys must be in 1..100");
|
|
39
41
|
}
|
|
40
|
-
if (options.legacyMaxAgeMs !== undefined &&
|
|
41
|
-
|
|
42
|
+
if (options.legacyMaxAgeMs !== undefined &&
|
|
43
|
+
(!Number.isSafeInteger(options.legacyMaxAgeMs) ||
|
|
44
|
+
options.legacyMaxAgeMs < 1000)) {
|
|
45
|
+
throw new TypeError("legacyMaxAgeMs must be >= 1000");
|
|
42
46
|
}
|
|
43
47
|
this.rotation = {
|
|
44
|
-
...options,
|
|
48
|
+
...options,
|
|
49
|
+
retainLegacyKeys: retain,
|
|
45
50
|
};
|
|
46
51
|
}
|
|
47
52
|
this.state = store.keyring(identity.pubkey) ?? {
|
|
48
|
-
version: 1,
|
|
53
|
+
version: 1,
|
|
54
|
+
legacy: [],
|
|
49
55
|
};
|
|
50
56
|
if (this.state.version !== 1 || !Array.isArray(this.state.legacy)) {
|
|
51
|
-
throw new Error(
|
|
57
|
+
throw new Error("Unsupported encryption keyring");
|
|
52
58
|
}
|
|
53
59
|
// A stale application-supplied initial key may be a retained key after restart.
|
|
54
|
-
const supplied = identity.encryptionKey &&
|
|
55
|
-
|
|
60
|
+
const supplied = identity.encryptionKey &&
|
|
61
|
+
getPublicKey(identity.encryptionKey) !== identity.pubkey
|
|
62
|
+
? encoded(identity.encryptionKey)
|
|
63
|
+
: undefined;
|
|
64
|
+
if (mode === "nip4e") {
|
|
56
65
|
if (!this.state.active) {
|
|
57
66
|
this.state.active = {
|
|
58
|
-
secret: supplied ?? encoded(generateSecretKey()),
|
|
67
|
+
secret: supplied ?? encoded(generateSecretKey()),
|
|
68
|
+
since: Date.now(),
|
|
59
69
|
};
|
|
60
70
|
this.state.bootstrapPublicKey = pub(this.state.active.secret);
|
|
61
71
|
}
|
|
62
|
-
else if (supplied &&
|
|
63
|
-
|
|
72
|
+
else if (supplied &&
|
|
73
|
+
pub(supplied) !== this.state.bootstrapPublicKey &&
|
|
74
|
+
supplied !== this.state.active.secret &&
|
|
75
|
+
!this.state.legacy.some((k) => k.secret === supplied)) {
|
|
76
|
+
throw new Error("Supplied messaging key conflicts with persisted keyring; use the saved account keyring");
|
|
64
77
|
}
|
|
65
78
|
}
|
|
66
79
|
for (const key of identity.oldEncryptionKeys ?? []) {
|
|
67
80
|
const value = encoded(key);
|
|
68
|
-
if (getPublicKey(key) !== identity.pubkey &&
|
|
81
|
+
if (getPublicKey(key) !== identity.pubkey &&
|
|
82
|
+
value !== this.state.active?.secret &&
|
|
83
|
+
!this.state.legacy.some((k) => k.secret === value)) {
|
|
69
84
|
this.state.legacy.push({
|
|
70
85
|
secret: value,
|
|
71
86
|
since: Date.now(),
|
|
72
|
-
retiredAt: Date.now()
|
|
87
|
+
retiredAt: Date.now(),
|
|
73
88
|
});
|
|
74
89
|
}
|
|
75
90
|
}
|
|
@@ -80,34 +95,67 @@ export class EncryptionKeys {
|
|
|
80
95
|
this.store.saveKeyring(this.identity.pubkey, this.state);
|
|
81
96
|
}
|
|
82
97
|
apply() {
|
|
83
|
-
const keys = [
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
98
|
+
const keys = [
|
|
99
|
+
...(this.state.active ? [this.state.active] : []),
|
|
100
|
+
...this.state.legacy,
|
|
101
|
+
...(this.state.pending?.secret
|
|
102
|
+
? [
|
|
103
|
+
{
|
|
104
|
+
secret: this.state.pending.secret,
|
|
105
|
+
since: 0,
|
|
106
|
+
},
|
|
107
|
+
]
|
|
108
|
+
: []),
|
|
109
|
+
];
|
|
110
|
+
this.identity.oldEncryptionKeys = [...new Set(keys.map((k) => k.secret))]
|
|
111
|
+
.filter((k) => this.mode !== "nip4e" || k !== this.state.active?.secret)
|
|
112
|
+
.map(secret);
|
|
113
|
+
if (this.mode === "nip4e") {
|
|
114
|
+
this.identity.encryptionKey = secret(this.activeKey().secret);
|
|
89
115
|
}
|
|
90
116
|
}
|
|
117
|
+
activeKey() {
|
|
118
|
+
const active = this.state.active;
|
|
119
|
+
if (!active)
|
|
120
|
+
throw new Error("Missing active encryption key");
|
|
121
|
+
return active;
|
|
122
|
+
}
|
|
91
123
|
current() {
|
|
92
|
-
return this.mode ===
|
|
124
|
+
return this.mode === "nip4e"
|
|
125
|
+
? pub(this.activeKey().secret)
|
|
126
|
+
: this.identity.pubkey;
|
|
93
127
|
}
|
|
94
128
|
status() {
|
|
95
129
|
return {
|
|
96
130
|
currentPublicKey: this.current(),
|
|
97
|
-
decryptionPublicKeys: [
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
131
|
+
decryptionPublicKeys: [
|
|
132
|
+
...new Set([
|
|
133
|
+
this.identity.pubkey,
|
|
134
|
+
...(this.state.active ? [pub(this.state.active.secret)] : []),
|
|
135
|
+
...this.state.legacy.map((k) => pub(k.secret)),
|
|
136
|
+
...(this.state.pending?.secret
|
|
137
|
+
? [pub(this.state.pending.secret)]
|
|
138
|
+
: []),
|
|
139
|
+
]),
|
|
140
|
+
],
|
|
141
|
+
activatedAt: this.mode === "nip4e" ? this.state.active?.since : undefined,
|
|
142
|
+
legacy: this.state.legacy.map((k) => ({
|
|
143
|
+
publicKey: pub(k.secret),
|
|
144
|
+
retiredAt: k.retiredAt,
|
|
101
145
|
})),
|
|
102
|
-
...(this.state.pending
|
|
103
|
-
|
|
104
|
-
|
|
146
|
+
...(this.state.pending
|
|
147
|
+
? {
|
|
148
|
+
pending: {
|
|
149
|
+
publicKey: this.state.pending.event.tags[0][1],
|
|
150
|
+
eventId: this.state.pending.event.id,
|
|
151
|
+
},
|
|
105
152
|
}
|
|
106
|
-
|
|
153
|
+
: {}),
|
|
107
154
|
};
|
|
108
155
|
}
|
|
109
156
|
knows(key) {
|
|
110
|
-
return key === this.identity.pubkey ||
|
|
157
|
+
return (key === this.identity.pubkey ||
|
|
158
|
+
[this.state.active, ...this.state.legacy].some((k) => k && pub(k.secret) === key));
|
|
111
159
|
}
|
|
112
160
|
onImported;
|
|
113
161
|
synchronize;
|
|
@@ -118,37 +166,58 @@ export class EncryptionKeys {
|
|
|
118
166
|
if (key === this.identity.pubkey) {
|
|
119
167
|
return;
|
|
120
168
|
}
|
|
121
|
-
const found = [
|
|
122
|
-
|
|
123
|
-
|
|
169
|
+
const found = [
|
|
170
|
+
this.state.active,
|
|
171
|
+
...this.state.legacy,
|
|
172
|
+
...(this.state.pending?.secret
|
|
173
|
+
? [
|
|
174
|
+
{
|
|
175
|
+
secret: this.state.pending.secret,
|
|
176
|
+
since: 0,
|
|
177
|
+
},
|
|
178
|
+
]
|
|
179
|
+
: []),
|
|
180
|
+
].find((value) => value && pub(value.secret) === key);
|
|
124
181
|
return found ? secret(found.secret) : undefined;
|
|
125
182
|
}
|
|
126
183
|
async accept(event, allowed) {
|
|
127
|
-
const key = event?.tags.find(tag => tag[0] ===
|
|
128
|
-
if (this.mode ===
|
|
129
|
-
|
|
184
|
+
const key = event?.tags.find((tag) => tag[0] === "n")?.[1];
|
|
185
|
+
if (this.mode === "nip4e" &&
|
|
186
|
+
this.synchronize &&
|
|
187
|
+
hexKey(key) &&
|
|
188
|
+
key !== this.identity.pubkey &&
|
|
189
|
+
!allowed.includes(key) &&
|
|
190
|
+
!this.state.pending) {
|
|
191
|
+
const value = this.exportEncryptionKey(key) ?? (await this.synchronize(key));
|
|
130
192
|
try {
|
|
131
|
-
if (getPublicKey(value) !== key ||
|
|
132
|
-
|
|
193
|
+
if (getPublicKey(value) !== key ||
|
|
194
|
+
this.store
|
|
195
|
+
.declaration(this.identity.pubkey)
|
|
196
|
+
?.tags.find((tag) => tag[0] === "n")?.[1] !== key) {
|
|
197
|
+
throw new Error("Key synchronization no longer matches the current declaration");
|
|
133
198
|
}
|
|
134
199
|
const now = Date.now(), next = structuredClone(this.state);
|
|
135
200
|
if (next.active) {
|
|
136
201
|
next.legacy.unshift({
|
|
137
|
-
...next.active,
|
|
202
|
+
...next.active,
|
|
203
|
+
retiredAt: now,
|
|
138
204
|
});
|
|
139
205
|
}
|
|
140
206
|
next.active = {
|
|
141
|
-
secret: encoded(value),
|
|
207
|
+
secret: encoded(value),
|
|
208
|
+
since: now,
|
|
142
209
|
};
|
|
143
210
|
const seen = new Set([key]);
|
|
144
|
-
next.legacy = next.legacy
|
|
211
|
+
next.legacy = next.legacy
|
|
212
|
+
.filter((item) => {
|
|
145
213
|
const publicKey = pub(item.secret);
|
|
146
214
|
if (seen.has(publicKey)) {
|
|
147
215
|
return false;
|
|
148
216
|
}
|
|
149
217
|
seen.add(publicKey);
|
|
150
218
|
return true;
|
|
151
|
-
})
|
|
219
|
+
})
|
|
220
|
+
.slice(0, this.rotation?.retainLegacyKeys ?? 5);
|
|
152
221
|
this.store.saveKeyring(this.identity.pubkey, next);
|
|
153
222
|
this.state = next;
|
|
154
223
|
this.apply();
|
|
@@ -161,7 +230,9 @@ export class EncryptionKeys {
|
|
|
161
230
|
this.check(event, [...allowed, this.current()]);
|
|
162
231
|
}
|
|
163
232
|
async declaration() {
|
|
164
|
-
for (const event of await (this.metadata
|
|
233
|
+
for (const event of await (this.metadata
|
|
234
|
+
? this.metadata.read(this.identity.pubkey, [10044])
|
|
235
|
+
: messagingMetadata(this.identity.pubkey, [10044], this.transport, this.relays))) {
|
|
165
236
|
this.store.rememberDeclaration(event);
|
|
166
237
|
}
|
|
167
238
|
return this.store.declaration(this.identity.pubkey);
|
|
@@ -170,24 +241,26 @@ export class EncryptionKeys {
|
|
|
170
241
|
if (!event) {
|
|
171
242
|
return;
|
|
172
243
|
}
|
|
173
|
-
const key = event.tags.find(t => t[0] ===
|
|
244
|
+
const key = event.tags.find((t) => t[0] === "n")?.[1];
|
|
174
245
|
if (!hexKey(key)) {
|
|
175
|
-
throw new Nip4eCompatibilityError(
|
|
246
|
+
throw new Nip4eCompatibilityError("NIP4E_DECLARATION_INVALID");
|
|
176
247
|
}
|
|
177
248
|
if (!allowed.includes(key)) {
|
|
178
|
-
throw new Nip4eCompatibilityError(
|
|
249
|
+
throw new Nip4eCompatibilityError("NIP4E_KEY_CONFLICT");
|
|
179
250
|
}
|
|
180
251
|
}
|
|
181
252
|
serialize(run) {
|
|
182
|
-
const task = (this.operation ?? Promise.resolve())
|
|
183
|
-
|
|
253
|
+
const task = (this.operation ?? Promise.resolve())
|
|
254
|
+
.catch(() => { })
|
|
255
|
+
.then(run);
|
|
184
256
|
this.operation = task;
|
|
185
|
-
void task
|
|
257
|
+
void task
|
|
258
|
+
.finally(() => {
|
|
186
259
|
if (this.operation === task) {
|
|
187
260
|
this.operation = undefined;
|
|
188
261
|
}
|
|
189
|
-
})
|
|
190
|
-
|
|
262
|
+
})
|
|
263
|
+
.catch(() => { });
|
|
191
264
|
return task;
|
|
192
265
|
}
|
|
193
266
|
async resume() {
|
|
@@ -197,10 +270,14 @@ export class EncryptionKeys {
|
|
|
197
270
|
}
|
|
198
271
|
// A mode change must finish the previously authorized operation in its original mode.
|
|
199
272
|
const target = pending.event.tags[0][1];
|
|
200
|
-
if ((pending.secret && this.mode !==
|
|
201
|
-
|
|
273
|
+
if ((pending.secret && this.mode !== "nip4e") ||
|
|
274
|
+
(!pending.secret && target !== this.current())) {
|
|
275
|
+
throw new Error("Pending key declaration belongs to the previous mode; resume it before switching modes");
|
|
202
276
|
}
|
|
203
|
-
this.check(await this.declaration(), [
|
|
277
|
+
this.check(await this.declaration(), [
|
|
278
|
+
target,
|
|
279
|
+
...(pending.previous ? [pending.previous] : []),
|
|
280
|
+
]);
|
|
204
281
|
this.save();
|
|
205
282
|
this.apply(); // Reassert durable custody after any prior local persistence failure.
|
|
206
283
|
for (const group of publicationGroups(this.publishRelays)) {
|
|
@@ -214,14 +291,18 @@ export class EncryptionKeys {
|
|
|
214
291
|
const now = Date.now();
|
|
215
292
|
if (next.active) {
|
|
216
293
|
next.legacy.unshift({
|
|
217
|
-
...next.active,
|
|
294
|
+
...next.active,
|
|
295
|
+
retiredAt: now,
|
|
218
296
|
});
|
|
219
297
|
}
|
|
220
298
|
next.active = {
|
|
221
|
-
secret: pending.secret,
|
|
299
|
+
secret: pending.secret,
|
|
300
|
+
since: now,
|
|
222
301
|
};
|
|
223
302
|
const retain = this.rotation?.retainLegacyKeys ?? 5, age = this.rotation?.legacyMaxAgeMs;
|
|
224
|
-
next.legacy = next.legacy
|
|
303
|
+
next.legacy = next.legacy
|
|
304
|
+
.filter((k) => age === undefined || now - k.retiredAt <= age)
|
|
305
|
+
.slice(0, retain);
|
|
225
306
|
}
|
|
226
307
|
delete next.pending;
|
|
227
308
|
this.store.saveKeyring(this.identity.pubkey, next);
|
|
@@ -237,17 +318,25 @@ export class EncryptionKeys {
|
|
|
237
318
|
async announce(options = {}) {
|
|
238
319
|
await this.serialize(async () => {
|
|
239
320
|
await this.resume();
|
|
240
|
-
const current = await this.declaration(), old = current?.tags.find(t => t[0] ===
|
|
241
|
-
if (options.replaceEncryptionKey !== undefined &&
|
|
242
|
-
|
|
321
|
+
const current = await this.declaration(), old = current?.tags.find((t) => t[0] === "n")?.[1];
|
|
322
|
+
if (options.replaceEncryptionKey !== undefined &&
|
|
323
|
+
(!hexKey(options.replaceEncryptionKey) ||
|
|
324
|
+
old !== options.replaceEncryptionKey ||
|
|
325
|
+
!this.knows(options.replaceEncryptionKey))) {
|
|
326
|
+
throw new Error("replaceEncryptionKey must match the latest declaration and a locally available decryption key");
|
|
243
327
|
}
|
|
244
|
-
await this.accept(current, [
|
|
245
|
-
|
|
328
|
+
await this.accept(current, [
|
|
329
|
+
this.current(),
|
|
330
|
+
...(options.replaceEncryptionKey ? [options.replaceEncryptionKey] : []),
|
|
331
|
+
]);
|
|
332
|
+
if (current && old === this.current()) {
|
|
246
333
|
// Reuse the signed declaration but rebroadcast to current write/DM sets.
|
|
247
334
|
const next = {
|
|
248
|
-
...this.state,
|
|
249
|
-
|
|
250
|
-
|
|
335
|
+
...this.state,
|
|
336
|
+
pending: {
|
|
337
|
+
event: current,
|
|
338
|
+
previous: old,
|
|
339
|
+
},
|
|
251
340
|
};
|
|
252
341
|
this.store.saveKeyring(this.identity.pubkey, next);
|
|
253
342
|
this.state = next;
|
|
@@ -263,19 +352,21 @@ export class EncryptionKeys {
|
|
|
263
352
|
const event = await signChecked(this.identity, {
|
|
264
353
|
kind: 10044,
|
|
265
354
|
created_at,
|
|
266
|
-
tags: [[
|
|
267
|
-
content:
|
|
355
|
+
tags: [["n", key]],
|
|
356
|
+
content: "",
|
|
268
357
|
});
|
|
358
|
+
const previousKey = previous?.tags.find((tag) => tag[0] === "n")?.[1];
|
|
269
359
|
const next = {
|
|
270
|
-
...this.state,
|
|
360
|
+
...this.state,
|
|
361
|
+
pending: {
|
|
271
362
|
event,
|
|
272
|
-
...(value
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
} : {})
|
|
278
|
-
}
|
|
363
|
+
...(value
|
|
364
|
+
? {
|
|
365
|
+
secret: value,
|
|
366
|
+
}
|
|
367
|
+
: {}),
|
|
368
|
+
...(previousKey === undefined ? {} : { previous: previousKey }),
|
|
369
|
+
},
|
|
279
370
|
};
|
|
280
371
|
this.store.saveKeyring(this.identity.pubkey, next);
|
|
281
372
|
this.state = next;
|
|
@@ -284,8 +375,8 @@ export class EncryptionKeys {
|
|
|
284
375
|
}
|
|
285
376
|
rotating;
|
|
286
377
|
rotate() {
|
|
287
|
-
if (this.mode !==
|
|
288
|
-
return Promise.reject(new Error(
|
|
378
|
+
if (this.mode !== "nip4e") {
|
|
379
|
+
return Promise.reject(new Error("Encryption-key rotation requires mode: nip4e"));
|
|
289
380
|
}
|
|
290
381
|
if (this.rotating) {
|
|
291
382
|
return this.rotating;
|
|
@@ -301,24 +392,27 @@ export class EncryptionKeys {
|
|
|
301
392
|
await this.stage(pub(value), current, value);
|
|
302
393
|
});
|
|
303
394
|
this.rotating = task;
|
|
304
|
-
void task
|
|
395
|
+
void task
|
|
396
|
+
.finally(() => {
|
|
305
397
|
if (this.rotating === task) {
|
|
306
398
|
this.rotating = undefined;
|
|
307
399
|
}
|
|
308
|
-
})
|
|
309
|
-
|
|
400
|
+
})
|
|
401
|
+
.catch(() => { });
|
|
310
402
|
return task;
|
|
311
403
|
}
|
|
312
404
|
async rotateIfDue() {
|
|
313
|
-
if (this.rotation &&
|
|
405
|
+
if (this.rotation &&
|
|
406
|
+
Date.now() - this.activeKey().since >= this.rotation.intervalMs) {
|
|
314
407
|
await this.rotate();
|
|
315
408
|
}
|
|
316
409
|
}
|
|
317
410
|
nextDelay() {
|
|
318
|
-
return this.rotation
|
|
411
|
+
return this.rotation
|
|
412
|
+
? Math.min(2147483647, Math.max(1000, this.rotation.intervalMs - (Date.now() - this.activeKey().since)))
|
|
413
|
+
: undefined;
|
|
319
414
|
}
|
|
320
415
|
async idle() {
|
|
321
|
-
await this.operation?.catch(() => {
|
|
322
|
-
});
|
|
416
|
+
await this.operation?.catch(() => { });
|
|
323
417
|
}
|
|
324
418
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { DatabaseSync } from
|
|
2
|
-
import { closeSync, openSync } from
|
|
1
|
+
import { DatabaseSync } from "node:sqlite";
|
|
2
|
+
import { closeSync, openSync } from "node:fs";
|
|
3
3
|
/** Indexed message persistence. The application owns instances passed into Client. */
|
|
4
|
-
export function sqliteMessageStorage(path =
|
|
5
|
-
if (path !==
|
|
6
|
-
closeSync(openSync(path,
|
|
4
|
+
export function sqliteMessageStorage(path = "chirli-messages.sqlite") {
|
|
5
|
+
if (path !== ":memory:")
|
|
6
|
+
closeSync(openSync(path, "a", 0o600));
|
|
7
7
|
const db = new DatabaseSync(path);
|
|
8
8
|
try {
|
|
9
9
|
db.exec(`PRAGMA journal_mode=WAL; PRAGMA synchronous=FULL; PRAGMA busy_timeout=5000;
|
|
@@ -21,33 +21,34 @@ export function sqliteMessageStorage(path = 'chirli-messages.sqlite') {
|
|
|
21
21
|
throw error;
|
|
22
22
|
}
|
|
23
23
|
const validateState = (state) => {
|
|
24
|
-
if (![
|
|
25
|
-
throw new TypeError(
|
|
24
|
+
if (!["pending", "processing", "failed", "done"].includes(state))
|
|
25
|
+
throw new TypeError("Invalid message state");
|
|
26
26
|
};
|
|
27
27
|
return {
|
|
28
28
|
insert(account, message) {
|
|
29
29
|
validateState(message.state);
|
|
30
|
-
return db
|
|
31
|
-
.
|
|
30
|
+
return (db
|
|
31
|
+
.prepare("INSERT OR IGNORE INTO message_records VALUES(?,?,?,?,?,?)")
|
|
32
|
+
.run(account, message.id, message.conversation, message.timestamp, message.payload, message.state).changes === 1);
|
|
32
33
|
},
|
|
33
34
|
updateState(account, id, state) {
|
|
34
35
|
validateState(state);
|
|
35
|
-
db.prepare(
|
|
36
|
+
db.prepare("UPDATE message_records SET state=? WHERE account=? AND id=?").run(state, account, id);
|
|
36
37
|
},
|
|
37
38
|
query(account, options = {}) {
|
|
38
|
-
const order = options.order ??
|
|
39
|
-
if (![
|
|
40
|
-
throw new TypeError(
|
|
41
|
-
const timeOrder = order ===
|
|
42
|
-
const idOrder = order ===
|
|
43
|
-
const where = [
|
|
39
|
+
const order = options.order ?? "oldest";
|
|
40
|
+
if (!["oldest", "newest", "recovery"].includes(order))
|
|
41
|
+
throw new TypeError("Invalid message query order");
|
|
42
|
+
const timeOrder = order === "newest" ? "DESC" : "ASC";
|
|
43
|
+
const idOrder = order === "recovery" ? "DESC" : "ASC";
|
|
44
|
+
const where = ["account=?"];
|
|
44
45
|
const parameters = [account];
|
|
45
46
|
if (options.id !== undefined) {
|
|
46
|
-
where.push(
|
|
47
|
+
where.push("id=?");
|
|
47
48
|
parameters.push(options.id);
|
|
48
49
|
}
|
|
49
50
|
if (options.conversation !== undefined) {
|
|
50
|
-
where.push(
|
|
51
|
+
where.push("conversation=?");
|
|
51
52
|
parameters.push(options.conversation);
|
|
52
53
|
}
|
|
53
54
|
if (options.states) {
|
|
@@ -56,30 +57,34 @@ export function sqliteMessageStorage(path = 'chirli-messages.sqlite') {
|
|
|
56
57
|
const states = [...new Set(options.states)];
|
|
57
58
|
for (const state of states)
|
|
58
59
|
validateState(state);
|
|
59
|
-
if (states.length === 3 && !states.includes(
|
|
60
|
+
if (states.length === 3 && !states.includes("done")) {
|
|
60
61
|
where.push("state IN ('pending','processing','failed')");
|
|
61
62
|
}
|
|
62
63
|
else {
|
|
63
|
-
where.push(`state IN (${states.map(() =>
|
|
64
|
+
where.push(`state IN (${states.map(() => "?").join(",")})`);
|
|
64
65
|
parameters.push(...states);
|
|
65
66
|
}
|
|
66
67
|
}
|
|
67
68
|
if (options.cursor) {
|
|
68
|
-
const timeComparison = order ===
|
|
69
|
-
const idComparison = order ===
|
|
69
|
+
const timeComparison = order === "newest" ? "<" : ">";
|
|
70
|
+
const idComparison = order === "recovery" ? "<" : ">";
|
|
70
71
|
// This range lets SQLite seek into the ordering index before applying tie-breaking.
|
|
71
72
|
where.push(`timestamp ${timeComparison}= ? AND (timestamp ${timeComparison} ? OR (timestamp=? AND id ${idComparison} ?))`);
|
|
72
73
|
parameters.push(options.cursor.timestamp, options.cursor.timestamp, options.cursor.timestamp, options.cursor.id);
|
|
73
74
|
}
|
|
74
|
-
let sql = `SELECT id,conversation,timestamp,payload,state FROM message_records WHERE ${where.join(
|
|
75
|
+
let sql = `SELECT id,conversation,timestamp,payload,state FROM message_records WHERE ${where.join(" AND ")} ORDER BY timestamp ${timeOrder},id ${idOrder}`;
|
|
75
76
|
if (options.limit !== undefined) {
|
|
76
77
|
if (!Number.isSafeInteger(options.limit) || options.limit < 0)
|
|
77
|
-
throw new TypeError(
|
|
78
|
-
sql +=
|
|
78
|
+
throw new TypeError("Invalid message query limit");
|
|
79
|
+
sql += " LIMIT ?";
|
|
79
80
|
parameters.push(options.limit);
|
|
80
81
|
}
|
|
81
|
-
return db
|
|
82
|
+
return db
|
|
83
|
+
.prepare(sql)
|
|
84
|
+
.all(...parameters);
|
|
85
|
+
},
|
|
86
|
+
close() {
|
|
87
|
+
db.close();
|
|
82
88
|
},
|
|
83
|
-
close() { db.close(); },
|
|
84
89
|
};
|
|
85
90
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { MessageState } from
|
|
1
|
+
import type { MessageState } from "./storage.js";
|
|
2
2
|
/** Message payload and its processing state form one durable record. */
|
|
3
3
|
export interface MessageStorageEntry {
|
|
4
4
|
id: string;
|
|
@@ -13,7 +13,7 @@ export interface MessageStorageQuery {
|
|
|
13
13
|
conversation?: string;
|
|
14
14
|
states?: readonly MessageState[];
|
|
15
15
|
/** oldest: time ASC, ID ASC; newest: time DESC, ID ASC; recovery: time ASC, ID DESC. */
|
|
16
|
-
order?:
|
|
16
|
+
order?: "oldest" | "newest" | "recovery";
|
|
17
17
|
/** Exclusive position in the requested order. */
|
|
18
18
|
cursor?: {
|
|
19
19
|
timestamp: number;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { Event } from
|
|
2
|
-
import { type ReconnectOptions } from
|
|
3
|
-
import type { Store } from
|
|
4
|
-
import type { Transport } from
|
|
1
|
+
import type { Event } from "nostr-tools";
|
|
2
|
+
import { type ReconnectOptions } from "./inbox.js";
|
|
3
|
+
import type { Store } from "./store.js";
|
|
4
|
+
import type { Transport } from "./relay.js";
|
|
5
5
|
export interface MetadataCacheOptions {
|
|
6
6
|
ttlMs?: number;
|
|
7
7
|
negativeTtlMs?: number;
|