@ghostlytools/sdk 1.12.1 → 1.13.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/dist/bitcoinAddress-BbPG0DF7.js +3927 -0
- package/dist/bitcoinAddress-BbPG0DF7.js.map +1 -0
- package/dist/core.js +496 -2386
- package/dist/core.js.map +1 -1
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/{pairedTransports-CkYMAoYN.js → pairedTransports-C0I24YFz.js} +9 -291
- package/dist/pairedTransports-C0I24YFz.js.map +1 -0
- package/dist/testing.js +2 -2
- package/dist/types/browser/src/proofs/contract.d.ts +5 -0
- package/dist/types/browser/src/proofs/pkarrRelays.d.ts +4 -0
- package/dist/types/browser/src/proofs/verify.d.ts +3 -0
- package/dist/types/core/src/chatFiles.d.ts +16 -0
- package/dist/types/core/src/clipboardGuard.d.ts +5 -3
- package/dist/types/core/src/dhtDelivery.d.ts +20 -0
- package/dist/types/core/src/domainProofs.d.ts +2 -1
- package/dist/types/core/src/ghostlink.d.ts +30 -0
- package/dist/types/core/src/groupCalls.d.ts +56 -0
- package/dist/types/core/src/groupCommunity.d.ts +15 -1
- package/dist/types/core/src/groupSession.d.ts +15 -1
- package/dist/types/core/src/index.d.ts +1 -1
- package/dist/types/core/src/relay.d.ts +14 -1
- package/dist/types/core/src/statusCards.d.ts +6 -0
- package/dist/types/core/src/viewerGuard.d.ts +10 -3
- package/dist/{verify-CSo7-43-.js → verify-BoVhjvc_.js} +24 -5
- package/dist/verify-BoVhjvc_.js.map +1 -0
- package/package.json +1 -1
- package/dist/bitcoinAddress-C2MAmcDT.js +0 -1225
- package/dist/bitcoinAddress-C2MAmcDT.js.map +0 -1
- package/dist/pairedTransports-CkYMAoYN.js.map +0 -1
- package/dist/verify-CSo7-43-.js.map +0 -1
|
@@ -0,0 +1,3927 @@
|
|
|
1
|
+
import { a as concatBytes, c as fromZ32, f as toBase64Url, h as utf8Encode, l as isZ32Key, m as utf8Decode, p as toZ32, s as fromBase64Url, u as randomBytes } from "./bolt11-CRiQrma3.js";
|
|
2
|
+
import { ed25519 } from "@noble/curves/ed25519.js";
|
|
3
|
+
import { sha256 } from "@noble/hashes/sha2.js";
|
|
4
|
+
import { bech32, bech32m, createBase58check } from "@scure/base";
|
|
5
|
+
// ../core/src/text.ts
|
|
6
|
+
/**
|
|
7
|
+
* Display text a peer chooses: nicknames and service names. It is shown next
|
|
8
|
+
* to the peer's messages and in the chat header, so it must not be able to
|
|
9
|
+
* read as something it is not, and it must not be able to grow without bound.
|
|
10
|
+
*/
|
|
11
|
+
/** Longest nickname kept from a peer. The `hello` frame has always cut here. */
|
|
12
|
+
var MAX_NICK_LENGTH = 64;
|
|
13
|
+
/**
|
|
14
|
+
* Invisible and direction-changing characters. Unlike a file name, display
|
|
15
|
+
* text may legitimately hold emoji and Indic or Arabic joiners, so the two
|
|
16
|
+
* joiners stay and the rest of Cf goes: bidi overrides and isolates reorder
|
|
17
|
+
* what follows them, and the others hide text or let two different names
|
|
18
|
+
* render identically.
|
|
19
|
+
*/
|
|
20
|
+
var HIDDEN = /[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]/gu;
|
|
21
|
+
/** Zero-width non-joiner and zero-width joiner. */
|
|
22
|
+
var KEEP = /[\u200c\u200d]/;
|
|
23
|
+
/** `text` without its invisible and direction-changing characters, at any length: for display text with a cut of its own. */
|
|
24
|
+
function stripHidden(text) {
|
|
25
|
+
return text.replace(HIDDEN, (char) => KEEP.test(char) ? char : "");
|
|
26
|
+
}
|
|
27
|
+
/** The variation selectors other than VS15 and VS16: a symbol's, an ideograph's, a Mongolian letter's. */
|
|
28
|
+
var SELECTOR = /[︀-︍᠋-᠍᠏\u{e0100}-\u{e01ef}]/u;
|
|
29
|
+
/** Letters of the alphabets that neither join their letters nor make conjuncts: a joiner beside one changes nothing. */
|
|
30
|
+
var NO_JOINING = /[\p{sc=Latin}\p{sc=Greek}\p{sc=Cyrillic}\p{sc=Armenian}\p{sc=Georgian}\p{sc=Hebrew}\p{sc=Han}\p{sc=Hiragana}\p{sc=Katakana}\p{sc=Hangul}\p{sc=Thai}\p{sc=Lao}\p{sc=Ethiopic}]/u;
|
|
31
|
+
var EUROPEAN = /[\p{sc=Latin}\p{sc=Greek}\p{sc=Cyrillic}]/u;
|
|
32
|
+
var LETTER = /\p{L}/u;
|
|
33
|
+
var MARK = /\p{M}/u;
|
|
34
|
+
var SYMBOL = /\p{S}/u;
|
|
35
|
+
var PICTOGRAPH = /\p{Extended_Pictographic}/u;
|
|
36
|
+
/** What an emoji sequence's joiner follows: a pictograph, a skin tone, or VS16 after a pictograph. */
|
|
37
|
+
var BEFORE_EMOJI_JOINER = /[\p{Extended_Pictographic}\p{Emoji_Modifier}️]/u;
|
|
38
|
+
var EMOJI = /\p{Emoji}/u;
|
|
39
|
+
var ASCII = /^[\0-\x7f]$/;
|
|
40
|
+
var KEYCAP_BASE = /^[#*0-9]$/;
|
|
41
|
+
var joining = (char) => LETTER.test(char) && !NO_JOINING.test(char);
|
|
42
|
+
/**
|
|
43
|
+
* `text` without the joiners and variation selectors that join or select nothing, which draw nothing and could only
|
|
44
|
+
* make a name look like another one, or one nobody can type. A zero-width joiner stays between two emoji (a skin tone
|
|
45
|
+
* or VS16 before it) and either joiner between two letters of a script that joins its letters or makes conjuncts
|
|
46
|
+
* (Persian, the Indic scripts; the marks after the first letter, a virama, don't count); VS15 and VS16 stay after an
|
|
47
|
+
* emoji (an ASCII one only in a keycap), and the other selectors after a symbol or a letter that is not Latin, Greek or
|
|
48
|
+
* Cyrillic.
|
|
49
|
+
*/
|
|
50
|
+
function dropLoneJoiners(text) {
|
|
51
|
+
const chars = Array.from(text);
|
|
52
|
+
let out = "", last = "", base = "";
|
|
53
|
+
for (let i = 0; i < chars.length; i++) {
|
|
54
|
+
const char = chars[i], next = chars[i + 1] ?? "";
|
|
55
|
+
let keep = true;
|
|
56
|
+
if (char === "" || char === "") keep = char === "" && BEFORE_EMOJI_JOINER.test(last) && PICTOGRAPH.test(next) || joining(base) && joining(next);
|
|
57
|
+
else if (char === "︎" || char === "️") keep = EMOJI.test(last) && !ASCII.test(last) || KEYCAP_BASE.test(last) && next === "⃣";
|
|
58
|
+
else if (SELECTOR.test(char)) keep = SYMBOL.test(last) || LETTER.test(last) && !EUROPEAN.test(last);
|
|
59
|
+
if (!keep) continue;
|
|
60
|
+
out += char;
|
|
61
|
+
last = char;
|
|
62
|
+
if (!MARK.test(char)) base = char;
|
|
63
|
+
}
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* What draws nothing when it stands alone and `HIDDEN` leaves in, since text needs it beside something else: the
|
|
68
|
+
* joiners, the fillers, the variation selectors and the other default-ignorable code points, combining marks, white
|
|
69
|
+
* space and the blank Braille pattern.
|
|
70
|
+
*/
|
|
71
|
+
var UNSEEN = /^[\p{Default_Ignorable_Code_Point}\p{M}\p{White_Space}\u2800]*$/u;
|
|
72
|
+
/** Whether `text` holds a character that shows by itself: a name of joiners, fillers or marks alone has none. */
|
|
73
|
+
function hasVisible(text) {
|
|
74
|
+
return !UNSEEN.test(text);
|
|
75
|
+
}
|
|
76
|
+
/** Undefined when nothing visible is left, so callers can fall back. */
|
|
77
|
+
function sanitizeDisplayText(text, max) {
|
|
78
|
+
const clean = [...stripHidden(text)].slice(0, max).join("").trim();
|
|
79
|
+
return hasVisible(clean) ? clean : void 0;
|
|
80
|
+
}
|
|
81
|
+
/** A nickname as it arrived from a peer, ready to show. */
|
|
82
|
+
function sanitizeNick(nick) {
|
|
83
|
+
return typeof nick === "string" ? sanitizeDisplayText(nick, 64) : void 0;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* `text` without the characters of `chars` it ends with, in one pass from the end. A `/[…]+$/` replace would retry
|
|
87
|
+
* from every character of a run that does not reach the end.
|
|
88
|
+
*/
|
|
89
|
+
function trimEndOf(text, chars) {
|
|
90
|
+
let end = text.length;
|
|
91
|
+
while (end > 0 && chars.includes(text[end - 1])) end--;
|
|
92
|
+
return end === text.length ? text : text.slice(0, end);
|
|
93
|
+
}
|
|
94
|
+
/** What a token is copied with before it: quotes, guillemets, brackets, a backtick. */
|
|
95
|
+
var PASTE_OPENERS = "\"'“‘«‹<[({`";
|
|
96
|
+
/** What a token is copied with after it: those closed, and a sentence's marks. */
|
|
97
|
+
var PASTE_CLOSERS = "\"'”’»›>])}`.,!?;:…";
|
|
98
|
+
var PASTE_SPACES = " \n\r\xA0 ";
|
|
99
|
+
/**
|
|
100
|
+
* The token in what someone copied with the words around it: an invite, a group's link or a device code from the end
|
|
101
|
+
* of "Join us: <https://…#ghostly1…>!", "« ghostly1… »" or "code: ghostly1…". The last word, without the quotes,
|
|
102
|
+
* brackets and marks before and after it. Readers try it only once the paste itself did not read, so a bare token
|
|
103
|
+
* reads as it always did and a token with a character lost is still refused as one.
|
|
104
|
+
* A `/` a link ends with (typed, or added by a chat app's linkifier: `…/#/join/group2/…/`) is not the token's either,
|
|
105
|
+
* as `inviteRouteCode` drops it from an invite's route; a bare address keeps its own (`https://ghostly.tools/`).
|
|
106
|
+
*/
|
|
107
|
+
function unwrapPasted(text) {
|
|
108
|
+
const words = trimEndOf(text.trim(), PASTE_CLOSERS + PASTE_SPACES).split(/\s+/);
|
|
109
|
+
const last = words[words.length - 1] ?? "";
|
|
110
|
+
let start = 0;
|
|
111
|
+
while (start < last.length && PASTE_OPENERS.includes(last[start])) start++;
|
|
112
|
+
const token = trimEndOf(last.slice(start), PASTE_CLOSERS);
|
|
113
|
+
const bare = trimEndOf(token, PASTE_CLOSERS + "/");
|
|
114
|
+
return /:\/\/[^/]*$/.test(bare) ? token : bare;
|
|
115
|
+
}
|
|
116
|
+
// ../core/src/identity.ts
|
|
117
|
+
/** How many public keys `identityFromSeed` keeps however long ago they were used: a start with 50 chats and 20 groups derives about 400 distinct ones. */
|
|
118
|
+
var IDENTITY_MEMO_SIZE = 1024;
|
|
119
|
+
/**
|
|
120
|
+
* Past `IDENTITY_MEMO_SIZE`, a key is let go only once it has not been asked for this long: a start asks for all of a
|
|
121
|
+
* profile's keys within seconds (a 600-chat one, ~4,000 of them), then asks again for the running links' ones.
|
|
122
|
+
*/
|
|
123
|
+
var IDENTITY_MEMO_IDLE_MS = 6e5;
|
|
124
|
+
/** The most keys kept, used or not: ~410 bytes each in V8 (its id, its key and when it was used), ~6.7 MB in all. */
|
|
125
|
+
var IDENTITY_MEMO_MAX = 16384;
|
|
126
|
+
/**
|
|
127
|
+
* Public keys already derived, by the SHA-256 of their seed, the most recently used last. A chat's start derives the
|
|
128
|
+
* same few seeds' keys again and again (its link, its delivery, its capability record): ~0.25 ms each in V8, ~3.4x
|
|
129
|
+
* that in JSC. The seed itself is never kept, so a seed its owner zeroes leaves nothing behind here.
|
|
130
|
+
*/
|
|
131
|
+
var publicKeys = /* @__PURE__ */ new Map();
|
|
132
|
+
var publicKeyFastPath = null;
|
|
133
|
+
/** Gives (or, with `null`, takes back) the faster derivation of every public key `identityFromSeed` derives. */
|
|
134
|
+
function setPublicKeyFastPath(derive) {
|
|
135
|
+
publicKeyFastPath = derive;
|
|
136
|
+
}
|
|
137
|
+
function derivePublicKey(seed) {
|
|
138
|
+
if (publicKeyFastPath) try {
|
|
139
|
+
const key = publicKeyFastPath(seed);
|
|
140
|
+
if (key.length === 32) return Uint8Array.from(key);
|
|
141
|
+
} catch {}
|
|
142
|
+
return ed25519.getPublicKey(seed);
|
|
143
|
+
}
|
|
144
|
+
function publicKeyOf(seed) {
|
|
145
|
+
const id = toBase64Url(sha256(seed));
|
|
146
|
+
const now = Date.now();
|
|
147
|
+
let entry = publicKeys.get(id);
|
|
148
|
+
if (entry) {
|
|
149
|
+
publicKeys.delete(id);
|
|
150
|
+
entry.at = now;
|
|
151
|
+
} else {
|
|
152
|
+
entry = {
|
|
153
|
+
key: derivePublicKey(seed),
|
|
154
|
+
at: now
|
|
155
|
+
};
|
|
156
|
+
for (const [oldest, { at }] of publicKeys) {
|
|
157
|
+
if (publicKeys.size < 1024) break;
|
|
158
|
+
if (publicKeys.size < 16384 && now - at < 6e5) break;
|
|
159
|
+
publicKeys.delete(oldest);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
publicKeys.set(id, entry);
|
|
163
|
+
return entry.key.slice();
|
|
164
|
+
}
|
|
165
|
+
function identityFromSeed(seed) {
|
|
166
|
+
if (seed.length !== 32) throw new Error("Seed must be exactly 32 bytes");
|
|
167
|
+
const publicKey = publicKeyOf(seed);
|
|
168
|
+
return {
|
|
169
|
+
seed,
|
|
170
|
+
seedB64: toBase64Url(seed),
|
|
171
|
+
publicKey,
|
|
172
|
+
pubKeyZ32: toZ32(publicKey)
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
function identityFromSeedB64(seedB64) {
|
|
176
|
+
return identityFromSeed(fromBase64Url(seedB64));
|
|
177
|
+
}
|
|
178
|
+
function createIdentity() {
|
|
179
|
+
return identityFromSeed(randomBytes(32));
|
|
180
|
+
}
|
|
181
|
+
function publicKeyFromZ32(z32) {
|
|
182
|
+
if (z32.length !== 52) throw new Error("Invalid public key: expected 52 z-base-32 characters");
|
|
183
|
+
const bytes = fromZ32(z32);
|
|
184
|
+
if (toZ32(bytes) !== z32) throw new Error("Invalid public key: not canonical z-base-32");
|
|
185
|
+
return bytes;
|
|
186
|
+
}
|
|
187
|
+
function sign(message, seed) {
|
|
188
|
+
return ed25519.sign(message, seed);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Ed25519 verification as RFC 8032 writes it (`zip215: false`): canonical encodings only, and no key of small order,
|
|
192
|
+
* which no secret key goes with and which "verifies" a constant signature of any message.
|
|
193
|
+
*/
|
|
194
|
+
function verify(signature, message, publicKey) {
|
|
195
|
+
try {
|
|
196
|
+
return ed25519.verify(signature, message, publicKey, { zip215: false });
|
|
197
|
+
} catch {
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
// ../core/src/dns.ts
|
|
202
|
+
var TYPE_NS = 2;
|
|
203
|
+
var TYPE_TXT = 16;
|
|
204
|
+
var CLASS_IN = 1;
|
|
205
|
+
var FLAGS_REPLY = 32768;
|
|
206
|
+
var MAX_CHARACTER_STRING = 255;
|
|
207
|
+
var MAX_POINTER_OFFSET = 16383;
|
|
208
|
+
var Writer = class {
|
|
209
|
+
chunks = [];
|
|
210
|
+
length = 0;
|
|
211
|
+
push(bytes) {
|
|
212
|
+
this.chunks.push(bytes);
|
|
213
|
+
this.length += bytes.length;
|
|
214
|
+
}
|
|
215
|
+
u8(v) {
|
|
216
|
+
this.push(Uint8Array.of(v & 255));
|
|
217
|
+
}
|
|
218
|
+
u16(v) {
|
|
219
|
+
this.push(Uint8Array.of(v >>> 8 & 255, v & 255));
|
|
220
|
+
}
|
|
221
|
+
u32(v) {
|
|
222
|
+
this.push(Uint8Array.of(v >>> 24 & 255, v >>> 16 & 255, v >>> 8 & 255, v & 255));
|
|
223
|
+
}
|
|
224
|
+
bytes() {
|
|
225
|
+
return concatBytes(...this.chunks);
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
function writeName(writer, name, suffixOffsets) {
|
|
229
|
+
const labels = name.split(".").filter((l) => l.length > 0);
|
|
230
|
+
for (let i = 0; i < labels.length; i++) {
|
|
231
|
+
const suffix = labels.slice(i).join(".");
|
|
232
|
+
const pointer = suffixOffsets.get(suffix);
|
|
233
|
+
if (pointer !== void 0) {
|
|
234
|
+
writer.u16(49152 | pointer);
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
if (writer.length <= MAX_POINTER_OFFSET) suffixOffsets.set(suffix, writer.length);
|
|
238
|
+
const label = utf8Encode(labels[i]);
|
|
239
|
+
if (label.length > 63) throw new Error(`DNS label too long: ${labels[i]}`);
|
|
240
|
+
writer.u8(label.length);
|
|
241
|
+
writer.push(label);
|
|
242
|
+
}
|
|
243
|
+
writer.u8(0);
|
|
244
|
+
}
|
|
245
|
+
/** `authoritative` sets the AA flag, which a did:dht packet must carry. */
|
|
246
|
+
function encodeTxtPacket(records, options = {}) {
|
|
247
|
+
const writer = new Writer();
|
|
248
|
+
writer.u16(0);
|
|
249
|
+
writer.u16(options.authoritative ? 33792 : FLAGS_REPLY);
|
|
250
|
+
writer.u16(0);
|
|
251
|
+
writer.u16(records.length);
|
|
252
|
+
writer.u16(0);
|
|
253
|
+
writer.u16(0);
|
|
254
|
+
const suffixOffsets = /* @__PURE__ */ new Map();
|
|
255
|
+
for (const record of records) {
|
|
256
|
+
writeName(writer, record.name, suffixOffsets);
|
|
257
|
+
if ("host" in record) {
|
|
258
|
+
writer.u16(TYPE_NS);
|
|
259
|
+
writer.u16(CLASS_IN);
|
|
260
|
+
writer.u32(record.ttl);
|
|
261
|
+
const host = new Writer();
|
|
262
|
+
writeName(host, record.host, /* @__PURE__ */ new Map());
|
|
263
|
+
writer.u16(host.length);
|
|
264
|
+
writer.push(host.bytes());
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
writer.u16(TYPE_TXT);
|
|
268
|
+
writer.u16(CLASS_IN);
|
|
269
|
+
writer.u32(record.ttl);
|
|
270
|
+
const value = utf8Encode(record.value);
|
|
271
|
+
const strings = [];
|
|
272
|
+
for (let i = 0; i < value.length || i === 0; i += MAX_CHARACTER_STRING) {
|
|
273
|
+
const chunk = value.subarray(i, i + MAX_CHARACTER_STRING);
|
|
274
|
+
strings.push(Uint8Array.of(chunk.length), chunk);
|
|
275
|
+
}
|
|
276
|
+
const rdata = concatBytes(...strings);
|
|
277
|
+
writer.u16(rdata.length);
|
|
278
|
+
writer.push(rdata);
|
|
279
|
+
}
|
|
280
|
+
return writer.bytes();
|
|
281
|
+
}
|
|
282
|
+
/** A name at `start` (compressed or not), and where the bytes after it begin. Labels are joined with ".", no trailing dot. */
|
|
283
|
+
function readDnsName(data, start) {
|
|
284
|
+
const labels = [];
|
|
285
|
+
let pos = start;
|
|
286
|
+
let next = -1;
|
|
287
|
+
let jumps = 0;
|
|
288
|
+
for (;;) {
|
|
289
|
+
if (pos >= data.length) throw new Error("DNS name out of bounds");
|
|
290
|
+
const len = data[pos];
|
|
291
|
+
if ((len & 192) === 192) {
|
|
292
|
+
if (pos + 1 >= data.length) throw new Error("DNS pointer out of bounds");
|
|
293
|
+
if (next < 0) next = pos + 2;
|
|
294
|
+
pos = (len & 63) << 8 | data[pos + 1];
|
|
295
|
+
if (++jumps > 32) throw new Error("DNS name has too many compression pointers");
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
if (len & 192) throw new Error("Unsupported DNS label type");
|
|
299
|
+
pos += 1;
|
|
300
|
+
if (len === 0) break;
|
|
301
|
+
if (pos + len > data.length) throw new Error("DNS label out of bounds");
|
|
302
|
+
labels.push(utf8Decode(data.subarray(pos, pos + len)));
|
|
303
|
+
pos += len;
|
|
304
|
+
}
|
|
305
|
+
return {
|
|
306
|
+
name: labels.join("."),
|
|
307
|
+
next: next < 0 ? pos : next
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
function decodeTxtPacket(data) {
|
|
311
|
+
if (data.length < 12) throw new Error("DNS packet too short");
|
|
312
|
+
const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
313
|
+
const questions = view.getUint16(4);
|
|
314
|
+
const answers = view.getUint16(6);
|
|
315
|
+
let pos = 12;
|
|
316
|
+
for (let i = 0; i < questions; i++) pos = readDnsName(data, pos).next + 4;
|
|
317
|
+
const records = [];
|
|
318
|
+
for (let i = 0; i < answers; i++) {
|
|
319
|
+
const { name, next } = readDnsName(data, pos);
|
|
320
|
+
pos = next;
|
|
321
|
+
if (pos + 10 > data.length) throw new Error("DNS record out of bounds");
|
|
322
|
+
const type = view.getUint16(pos);
|
|
323
|
+
const ttl = view.getUint32(pos + 4);
|
|
324
|
+
const rdLength = view.getUint16(pos + 8);
|
|
325
|
+
pos += 10;
|
|
326
|
+
const end = pos + rdLength;
|
|
327
|
+
if (end > data.length) throw new Error("DNS rdata out of bounds");
|
|
328
|
+
if (type === TYPE_TXT) {
|
|
329
|
+
const parts = [];
|
|
330
|
+
let p = pos;
|
|
331
|
+
while (p < end) {
|
|
332
|
+
const len = data[p];
|
|
333
|
+
if (p + 1 + len > end) throw new Error("DNS character-string out of bounds");
|
|
334
|
+
parts.push(data.subarray(p + 1, p + 1 + len));
|
|
335
|
+
p += 1 + len;
|
|
336
|
+
}
|
|
337
|
+
try {
|
|
338
|
+
records.push({
|
|
339
|
+
name,
|
|
340
|
+
value: utf8Decode(concatBytes(...parts)),
|
|
341
|
+
ttl
|
|
342
|
+
});
|
|
343
|
+
} catch {}
|
|
344
|
+
}
|
|
345
|
+
pos = end;
|
|
346
|
+
}
|
|
347
|
+
return records;
|
|
348
|
+
}
|
|
349
|
+
/** Every answer record of a packet, of any type. Names are decompressed; the rdata is left to the caller. */
|
|
350
|
+
function decodeDnsAnswers(data) {
|
|
351
|
+
if (data.length < 12) throw new Error("DNS packet too short");
|
|
352
|
+
const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
353
|
+
const questions = view.getUint16(4);
|
|
354
|
+
const answers = view.getUint16(6);
|
|
355
|
+
let pos = 12;
|
|
356
|
+
for (let i = 0; i < questions; i++) pos = readDnsName(data, pos).next + 4;
|
|
357
|
+
const records = [];
|
|
358
|
+
for (let i = 0; i < answers; i++) {
|
|
359
|
+
const { name, next } = readDnsName(data, pos);
|
|
360
|
+
pos = next;
|
|
361
|
+
if (pos + 10 > data.length) throw new Error("DNS record out of bounds");
|
|
362
|
+
const type = view.getUint16(pos);
|
|
363
|
+
const ttl = view.getUint32(pos + 4);
|
|
364
|
+
const length = view.getUint16(pos + 8);
|
|
365
|
+
pos += 10;
|
|
366
|
+
if (pos + length > data.length) throw new Error("DNS rdata out of bounds");
|
|
367
|
+
records.push({
|
|
368
|
+
name,
|
|
369
|
+
type,
|
|
370
|
+
ttl,
|
|
371
|
+
offset: pos,
|
|
372
|
+
length
|
|
373
|
+
});
|
|
374
|
+
pos += length;
|
|
375
|
+
}
|
|
376
|
+
return records;
|
|
377
|
+
}
|
|
378
|
+
// ../core/src/pkarr.ts
|
|
379
|
+
/** Mainline DHT (BEP44) caps the value of a mutable item at 1000 bytes. */
|
|
380
|
+
var MAX_DNS_PACKET_BYTES = 1e3;
|
|
381
|
+
var SIGNATURE_LENGTH = 64;
|
|
382
|
+
var TIMESTAMP_LENGTH = 8;
|
|
383
|
+
/** The largest relay payload: a 64-byte signature, an 8-byte timestamp and the largest DNS packet (1072 bytes). */
|
|
384
|
+
var RELAY_PAYLOAD_MAX_BYTES = 1072;
|
|
385
|
+
var PacketTooLargeError = class extends Error {
|
|
386
|
+
size;
|
|
387
|
+
constructor(size) {
|
|
388
|
+
super(`DNS packet is too large, expected max ${MAX_DNS_PACKET_BYTES} bytes but got: ${size}`);
|
|
389
|
+
this.size = size;
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
var DEFAULT_TTL = 300;
|
|
393
|
+
function signable(timestampMicros, dnsPacket) {
|
|
394
|
+
return concatBytes(utf8Encode(`3:seqi${timestampMicros}e1:v${dnsPacket.length}:`), dnsPacket);
|
|
395
|
+
}
|
|
396
|
+
function toTxtRecords(pubKeyZ32, records) {
|
|
397
|
+
return records.map((r) => ({
|
|
398
|
+
name: `${r.label}.${pubKeyZ32}`,
|
|
399
|
+
value: r.value,
|
|
400
|
+
ttl: r.ttl ?? DEFAULT_TTL
|
|
401
|
+
}));
|
|
402
|
+
}
|
|
403
|
+
/** Size of the encoded DNS packet these records would produce. */
|
|
404
|
+
function measureRecords(pubKeyZ32, records) {
|
|
405
|
+
return encodeTxtPacket(toTxtRecords(pubKeyZ32, records)).length;
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* Builds the payload Pkarr relays accept on `PUT /<z32 public key>`:
|
|
409
|
+
* `<64 bytes signature><8 bytes big-endian timestamp (µs)><encoded DNS packet>`
|
|
410
|
+
*/
|
|
411
|
+
function createRelayPayload(identity, records, timestampMicros = BigInt(Date.now()) * 1000n) {
|
|
412
|
+
return signRelayPayload(identity, encodeTxtPacket(toTxtRecords(identity.pubKeyZ32, records)), timestampMicros);
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Signs a DNS packet exactly as given (its own names and flags) into a relay payload. `seq` is the
|
|
416
|
+
* BEP44 sequence number: Ghostly's records use microseconds, a did:dht document seconds.
|
|
417
|
+
*/
|
|
418
|
+
function signRelayPayload(identity, dnsPacket, seq) {
|
|
419
|
+
if (dnsPacket.length > 1e3) throw new PacketTooLargeError(dnsPacket.length);
|
|
420
|
+
if (seq < 0n || seq >= 1n << 64n) throw new RangeError(`Sequence number ${seq} does not fit in 64 bits`);
|
|
421
|
+
const signature = sign(signable(seq, dnsPacket), identity.seed);
|
|
422
|
+
const timestamp = new Uint8Array(TIMESTAMP_LENGTH);
|
|
423
|
+
new DataView(timestamp.buffer).setBigUint64(0, seq);
|
|
424
|
+
return concatBytes(signature, timestamp, dnsPacket);
|
|
425
|
+
}
|
|
426
|
+
/** A relay payload's parts, its size checked: the signature, the sequence number and the DNS packet (not decoded). */
|
|
427
|
+
function splitRelayPayload(payload) {
|
|
428
|
+
if (payload.length < 84) throw new Error("Relay payload too short");
|
|
429
|
+
if (payload.length > 1072) throw new PacketTooLargeError(payload.length);
|
|
430
|
+
return {
|
|
431
|
+
signature: payload.subarray(0, SIGNATURE_LENGTH),
|
|
432
|
+
seq: new DataView(payload.buffer, payload.byteOffset + SIGNATURE_LENGTH, TIMESTAMP_LENGTH).getBigUint64(0),
|
|
433
|
+
dnsPacket: payload.subarray(72)
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
/** Checks a relay payload's size and signature. Returns its sequence number and DNS packet, not decoded. */
|
|
437
|
+
function openRelayPayload(pubKeyZ32, payload) {
|
|
438
|
+
const { signature, seq, dnsPacket } = splitRelayPayload(payload);
|
|
439
|
+
const publicKey = publicKeyFromZ32(pubKeyZ32);
|
|
440
|
+
if (!verify(signature, signable(seq, dnsPacket), publicKey)) throw new Error("Invalid signature");
|
|
441
|
+
return {
|
|
442
|
+
seq,
|
|
443
|
+
dnsPacket
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
function decodeRelayPayload(pubKeyZ32, seq, dnsPacket) {
|
|
447
|
+
const suffix = `.${pubKeyZ32}`;
|
|
448
|
+
return {
|
|
449
|
+
pubKeyZ32,
|
|
450
|
+
timestampMicros: seq,
|
|
451
|
+
records: decodeTxtPacket(dnsPacket).map((r) => ({
|
|
452
|
+
label: r.name.endsWith(suffix) ? r.name.slice(0, -suffix.length) : r.name,
|
|
453
|
+
value: r.value,
|
|
454
|
+
ttl: r.ttl
|
|
455
|
+
}))
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
/** Verifies and decodes a relay payload. Throws if the signature does not match. */
|
|
459
|
+
function parseRelayPayload(pubKeyZ32, payload) {
|
|
460
|
+
const { seq, dnsPacket } = openRelayPayload(pubKeyZ32, payload);
|
|
461
|
+
return decodeRelayPayload(pubKeyZ32, seq, dnsPacket);
|
|
462
|
+
}
|
|
463
|
+
/**
|
|
464
|
+
* Decodes a payload whose signature this process made or already checked (one it is putting), without checking it
|
|
465
|
+
* again: an Ed25519 verify costs about 1 ms, and a CLI daemon at its relay budget checked its own packets ~500 times a
|
|
466
|
+
* minute. Never for bytes read from elsewhere: `parseRelayPayload` is.
|
|
467
|
+
*/
|
|
468
|
+
function readOwnRelayPayload(pubKeyZ32, payload) {
|
|
469
|
+
const { seq, dnsPacket } = splitRelayPayload(payload);
|
|
470
|
+
return decodeRelayPayload(pubKeyZ32, seq, dnsPacket);
|
|
471
|
+
}
|
|
472
|
+
/** How far ahead of this clock a packet's time may be and still count as now: clocks drift, not by this much. */
|
|
473
|
+
var PKARR_FUTURE_SKEW_MS = 6e5;
|
|
474
|
+
/**
|
|
475
|
+
* The newer of two signed packets of one key. A packet dated further ahead than `PKARR_FUTURE_SKEW_MS` never wins over
|
|
476
|
+
* one dated now or before: whoever could sign it once (a key several people hold, a clock far off) must not keep every
|
|
477
|
+
* later packet from being taken. Between two of the same kind, the later time wins; on a tie, `known` stays.
|
|
478
|
+
*/
|
|
479
|
+
function newerPacket(known, packet, now = Date.now()) {
|
|
480
|
+
if (!known) return packet ?? null;
|
|
481
|
+
if (!packet) return known;
|
|
482
|
+
const limit = BigInt(now + PKARR_FUTURE_SKEW_MS) * 1000n;
|
|
483
|
+
const knownAhead = known.timestampMicros > limit;
|
|
484
|
+
if (knownAhead !== packet.timestampMicros > limit) return knownAhead ? packet : known;
|
|
485
|
+
return packet.timestampMicros > known.timestampMicros ? packet : known;
|
|
486
|
+
}
|
|
487
|
+
// ../core/src/transport.ts
|
|
488
|
+
/**
|
|
489
|
+
* `transport` with `extra` added to every request's options: a group's edges say `group` so. The optional methods
|
|
490
|
+
* stay optional (a caller reads `configure` as "the DHT is reached directly").
|
|
491
|
+
*/
|
|
492
|
+
function withRequestOptions(transport, extra) {
|
|
493
|
+
const wrapped = {
|
|
494
|
+
publish: (identity, records, options) => transport.publish(identity, records, {
|
|
495
|
+
...options,
|
|
496
|
+
...extra
|
|
497
|
+
}),
|
|
498
|
+
resolve: (pubKeyZ32, options) => transport.resolve(pubKeyZ32, {
|
|
499
|
+
...options,
|
|
500
|
+
...extra
|
|
501
|
+
}),
|
|
502
|
+
describe: () => transport.describe()
|
|
503
|
+
};
|
|
504
|
+
if (transport.publishPayload) wrapped.publishPayload = (pubKeyZ32, payload, options) => transport.publishPayload(pubKeyZ32, payload, {
|
|
505
|
+
...options,
|
|
506
|
+
...extra
|
|
507
|
+
});
|
|
508
|
+
if (transport.discovery) wrapped.discovery = () => transport.discovery();
|
|
509
|
+
if (transport.subscribe) wrapped.subscribe = (listener) => transport.subscribe(listener);
|
|
510
|
+
if (transport.networkChanged) wrapped.networkChanged = () => transport.networkChanged();
|
|
511
|
+
if (transport.readAnsweredAt) wrapped.readAnsweredAt = (pubKeyZ32) => transport.readAnsweredAt(pubKeyZ32);
|
|
512
|
+
if (transport.readHeldBackFor) wrapped.readHeldBackFor = (pubKeyZ32) => transport.readHeldBackFor(pubKeyZ32);
|
|
513
|
+
if (transport.publishHeld) wrapped.publishHeld = (pubKeyZ32, options) => transport.publishHeld(pubKeyZ32, {
|
|
514
|
+
...options,
|
|
515
|
+
...extra
|
|
516
|
+
});
|
|
517
|
+
if (transport.onServerTime) wrapped.onServerTime = (listener) => transport.onServerTime(listener);
|
|
518
|
+
if (transport.configure) wrapped.configure = (options) => transport.configure(options);
|
|
519
|
+
if (transport.turnRead) wrapped.turnRead = (pubKeyZ32, options) => transport.turnRead(pubKeyZ32, options);
|
|
520
|
+
if (transport.turnPut) wrapped.turnPut = (pubKeyZ32, payload, conditions) => transport.turnPut(pubKeyZ32, payload, conditions);
|
|
521
|
+
if (transport.turnWarm) wrapped.turnWarm = () => transport.turnWarm();
|
|
522
|
+
if (transport.forget) wrapped.forget = (keys) => transport.forget(keys);
|
|
523
|
+
return wrapped;
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* A request the transport held back to stay within a request budget, its own or a relay's rate limit: nothing went
|
|
527
|
+
* out, and the same request goes through once the budget frees one, in about `retryInMs`. A wait, never a failure:
|
|
528
|
+
* whoever publishes tries again then, and says nothing is wrong meanwhile.
|
|
529
|
+
*/
|
|
530
|
+
var DiscoveryBudgetError = class extends Error {
|
|
531
|
+
constructor(retryInMs, message = "Discovery request budget reached; retry shortly") {
|
|
532
|
+
const errors = Error;
|
|
533
|
+
const limit = errors.stackTraceLimit;
|
|
534
|
+
if (typeof limit === "number") errors.stackTraceLimit = 0;
|
|
535
|
+
try {
|
|
536
|
+
super(message);
|
|
537
|
+
} finally {
|
|
538
|
+
if (typeof limit === "number") errors.stackTraceLimit = limit;
|
|
539
|
+
}
|
|
540
|
+
this.code = "discovery-budget";
|
|
541
|
+
this.retryInMs = retryInMs;
|
|
542
|
+
this.name = "DiscoveryBudgetError";
|
|
543
|
+
}
|
|
544
|
+
};
|
|
545
|
+
/** Whether `error` is a request the budget held back (`DiscoveryBudgetError`), also from another copy of this module. */
|
|
546
|
+
function isDiscoveryBudgetError(error) {
|
|
547
|
+
return error instanceof DiscoveryBudgetError || typeof error === "object" && error !== null && error.code === "discovery-budget" && typeof error.retryInMs === "number";
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* A publish no relay took (`RelayTransport`), saying whether one of them may hold the packet all the same. Not
|
|
551
|
+
* `mayBeStored` only when every relay was not asked now (its budget, its rate limit, its breaker, its network
|
|
552
|
+
* cooldown), answered that it did not take it (a 4xx), or could not be reached before the request went (a refused
|
|
553
|
+
* connection or a name that does not resolve, where the runtime says so: Node does, a browser does not), or when there
|
|
554
|
+
* is no relay at all. A relay that did not answer in time, dropped the connection, or answered with a server error
|
|
555
|
+
* may have stored it. The message is the publish's usual one.
|
|
556
|
+
*/
|
|
557
|
+
var PublishFailedError = class extends Error {
|
|
558
|
+
mayBeStored;
|
|
559
|
+
code = "publish-failed";
|
|
560
|
+
constructor(message, mayBeStored) {
|
|
561
|
+
super(message);
|
|
562
|
+
this.mayBeStored = mayBeStored;
|
|
563
|
+
}
|
|
564
|
+
};
|
|
565
|
+
/**
|
|
566
|
+
* Whether `error` is a publish that went nowhere (`PublishFailedError` with `mayBeStored` false), also from another
|
|
567
|
+
* copy of this module. Anything else (a timeout, the DHT's own failure beside the relays', a Desktop's error in
|
|
568
|
+
* words) may have left this device.
|
|
569
|
+
*/
|
|
570
|
+
function publishedNowhere(error) {
|
|
571
|
+
return typeof error === "object" && error !== null && error.code === "publish-failed" && error.mayBeStored === false;
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* A publish Ghostly Desktop's Rust relay client held back, as it says it in words (`pkarr_network.rs`
|
|
575
|
+
* `publish_failure`: "Publish held back on every relay…; retry in N ms: …"), as the `DiscoveryBudgetError` it is: every
|
|
576
|
+
* relay held the packet back for its rate limit, or held it back beside relays left alone for failing, and nothing failed
|
|
577
|
+
* now. Anything else is returned as it came.
|
|
578
|
+
*/
|
|
579
|
+
function heldBackError(error) {
|
|
580
|
+
const text = typeof error === "string" ? error : error instanceof Error ? error.message : "";
|
|
581
|
+
const wait = /^Publish held back [^;]*; retry in (\d+) ms/.exec(text);
|
|
582
|
+
return wait ? new DiscoveryBudgetError(Number(wait[1]), text) : error;
|
|
583
|
+
}
|
|
584
|
+
/** When to try again after the budget held a request back: when it frees one, but never sooner than `min` nor later than `max`. */
|
|
585
|
+
function budgetRetryMs(error, min, max) {
|
|
586
|
+
return Math.min(max, Math.max(min, Number.isFinite(error.retryInMs) ? error.retryInMs : max));
|
|
587
|
+
}
|
|
588
|
+
// ../core/src/relayBreaker.ts
|
|
589
|
+
/**
|
|
590
|
+
* A circuit breaker per Pkarr relay. A relay that failed `threshold` times in a row (no answer, a server error,
|
|
591
|
+
* its own rate limit, 429, or an answer too slow to use, `SLOW_MS` in relay.ts) is left alone for a while: a minute the first time, twice as long each time it
|
|
592
|
+
* trips again, five minutes at most. When the wait is over, one request goes to it (the probe); an answer
|
|
593
|
+
* closes the breaker, a failure opens it again for longer. The relays still closed take its turns meanwhile.
|
|
594
|
+
*
|
|
595
|
+
* Only what the relay did counts: a request this client held back for its own budget is not a failure, and an
|
|
596
|
+
* answer the protocol expects (404, 409, 412, 428) is a relay working as it should.
|
|
597
|
+
*/
|
|
598
|
+
/** Failures in a row that trip a relay's breaker. */
|
|
599
|
+
var BREAKER_THRESHOLD = 3;
|
|
600
|
+
/** How long a relay is left alone the first time its breaker trips; doubled on each trip in a row. */
|
|
601
|
+
var BREAKER_BASE_MS = 6e4;
|
|
602
|
+
/** The longest a relay is left alone. */
|
|
603
|
+
var BREAKER_MAX_MS = 3e5;
|
|
604
|
+
/**
|
|
605
|
+
* While every relay is left alone for failing, one of them is asked this often anyway: with nothing else to try, a
|
|
606
|
+
* long wait only delays noticing that they answer again (a link stayed down minutes after the relays came back).
|
|
607
|
+
*/
|
|
608
|
+
var BREAKER_ALL_DOWN_PROBE_MS = 15e3;
|
|
609
|
+
var RelayBreaker = class {
|
|
610
|
+
options;
|
|
611
|
+
circuits = /* @__PURE__ */ new Map();
|
|
612
|
+
threshold;
|
|
613
|
+
baseMs;
|
|
614
|
+
maxMs;
|
|
615
|
+
allDownProbeMs;
|
|
616
|
+
now;
|
|
617
|
+
/** When a relay was last asked because every relay was left alone. */
|
|
618
|
+
lastAllDownProbe = -Infinity;
|
|
619
|
+
constructor(options = {}) {
|
|
620
|
+
this.options = options;
|
|
621
|
+
this.threshold = options.threshold ?? 3;
|
|
622
|
+
this.baseMs = options.baseMs ?? 6e4;
|
|
623
|
+
this.maxMs = options.maxMs ?? 3e5;
|
|
624
|
+
this.allDownProbeMs = options.allDownProbeMs ?? 15e3;
|
|
625
|
+
this.now = options.now ?? Date.now;
|
|
626
|
+
}
|
|
627
|
+
circuit(relay) {
|
|
628
|
+
let circuit = this.circuits.get(relay);
|
|
629
|
+
if (!circuit) this.circuits.set(relay, circuit = {
|
|
630
|
+
failures: 0,
|
|
631
|
+
trips: 0,
|
|
632
|
+
openUntil: 0,
|
|
633
|
+
probing: false
|
|
634
|
+
});
|
|
635
|
+
return circuit;
|
|
636
|
+
}
|
|
637
|
+
/** How long the relay is still left alone, in ms; 0 when a request may go (closed, or its probe is due). */
|
|
638
|
+
blockedFor(relay) {
|
|
639
|
+
const circuit = this.circuits.get(relay);
|
|
640
|
+
if (!circuit || circuit.openUntil === 0) return 0;
|
|
641
|
+
const left = circuit.openUntil - this.now();
|
|
642
|
+
if (left > 0) return left;
|
|
643
|
+
return circuit.probing ? this.baseMs : 0;
|
|
644
|
+
}
|
|
645
|
+
/** Why the relay is left alone, while it is: its rate limit or failures. */
|
|
646
|
+
blockedKind(relay) {
|
|
647
|
+
return this.blockedFor(relay) > 0 ? this.circuits.get(relay)?.kind : void 0;
|
|
648
|
+
}
|
|
649
|
+
/** A request is going to the relay now: its probe, when the wait is over. Call only when `blockedFor` is 0. */
|
|
650
|
+
begin(relay) {
|
|
651
|
+
const circuit = this.circuits.get(relay);
|
|
652
|
+
if (circuit && circuit.openUntil !== 0 && circuit.openUntil <= this.now()) circuit.probing = true;
|
|
653
|
+
}
|
|
654
|
+
/**
|
|
655
|
+
* When every relay of `relays` is left alone for failing (none for its rate limit, none with its probe due or out),
|
|
656
|
+
* the one whose wait ends first may be asked now, once every `BREAKER_ALL_DOWN_PROBE_MS`. Returns it, or null. The
|
|
657
|
+
* caller marks the request with `beginAllDown` when it really goes (a request its budget holds back is no probe).
|
|
658
|
+
*/
|
|
659
|
+
allDownProbe(relays) {
|
|
660
|
+
const now = this.now();
|
|
661
|
+
if (!relays.length || now - this.lastAllDownProbe < this.allDownProbeMs) return null;
|
|
662
|
+
let pick = null, soonest = Infinity;
|
|
663
|
+
for (const relay of relays) {
|
|
664
|
+
const circuit = this.circuits.get(relay);
|
|
665
|
+
if (!circuit || circuit.openUntil <= now || circuit.probing || circuit.kind !== "error") return null;
|
|
666
|
+
if (circuit.openUntil < soonest) {
|
|
667
|
+
soonest = circuit.openUntil;
|
|
668
|
+
pick = relay;
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
return pick;
|
|
672
|
+
}
|
|
673
|
+
/** The probe `allDownProbe` named is going now: its answer closes the breaker, a failure trips it again. */
|
|
674
|
+
beginAllDown(relay) {
|
|
675
|
+
const circuit = this.circuits.get(relay);
|
|
676
|
+
if (circuit) circuit.probing = true;
|
|
677
|
+
this.lastAllDownProbe = this.now();
|
|
678
|
+
}
|
|
679
|
+
/**
|
|
680
|
+
* A request that went to the relay was dropped before it answered, by this client (another relay answered first), or
|
|
681
|
+
* its answer says nothing either way: a probe among them is no longer out, and the next request may probe again.
|
|
682
|
+
*/
|
|
683
|
+
cancel(relay) {
|
|
684
|
+
const circuit = this.circuits.get(relay);
|
|
685
|
+
if (circuit) circuit.probing = false;
|
|
686
|
+
}
|
|
687
|
+
/**
|
|
688
|
+
* The relay answered: whatever was wrong is over. When it was left alone and so was every other relay of `relays`
|
|
689
|
+
* (by default those that failed), for failing, the outage is over for them too: they are asked again now, not at the
|
|
690
|
+
* end of a wait of up to `BREAKER_MAX_MS` (two clients whose probes another relay answered each would share none
|
|
691
|
+
* for minutes). Their trips stay: one really down trips again at once, for longer.
|
|
692
|
+
*/
|
|
693
|
+
success(relay, relays = [...this.circuits.keys()]) {
|
|
694
|
+
const circuit = this.circuits.get(relay);
|
|
695
|
+
if (!circuit) return;
|
|
696
|
+
const recovered = circuit.openUntil !== 0;
|
|
697
|
+
this.circuits.delete(relay);
|
|
698
|
+
if (!recovered) return;
|
|
699
|
+
const others = relays.filter((r) => r !== relay).map((r) => this.circuits.get(r));
|
|
700
|
+
if (others.length && others.every((other) => !!other && other.openUntil !== 0 && other.kind === "error")) {
|
|
701
|
+
const now = this.now();
|
|
702
|
+
for (const other of others) other.openUntil = Math.min(other.openUntil, now);
|
|
703
|
+
}
|
|
704
|
+
this.options.onRecover?.(relay);
|
|
705
|
+
}
|
|
706
|
+
/**
|
|
707
|
+
* The relay failed a request; a probe that fails trips it again at once. While it is left alone and no probe is out,
|
|
708
|
+
* a failure is a request sent before it tripped (a start burst): already counted in, it trips nothing again.
|
|
709
|
+
*/
|
|
710
|
+
failure(relay, kind, reason) {
|
|
711
|
+
const circuit = this.circuit(relay);
|
|
712
|
+
if (!circuit.probing && circuit.openUntil > this.now()) return;
|
|
713
|
+
circuit.failures++;
|
|
714
|
+
circuit.kind = kind;
|
|
715
|
+
circuit.reason = reason;
|
|
716
|
+
if (circuit.probing || circuit.failures >= this.threshold) this.trip(relay, circuit);
|
|
717
|
+
}
|
|
718
|
+
trip(relay, circuit) {
|
|
719
|
+
this.lastAllDownProbe = Math.max(this.lastAllDownProbe, this.now());
|
|
720
|
+
const forMs = Math.min(this.baseMs * 2 ** circuit.trips, this.maxMs);
|
|
721
|
+
circuit.openUntil = this.now() + forMs;
|
|
722
|
+
circuit.trips++;
|
|
723
|
+
circuit.failures = 0;
|
|
724
|
+
circuit.probing = false;
|
|
725
|
+
this.options.onTrip?.(relay, circuit.reason ?? "", forMs);
|
|
726
|
+
}
|
|
727
|
+
/** How long until a relay left alone is asked again: its wait, or the next probe while every relay of `relays` is left alone. */
|
|
728
|
+
askedAgainIn(relay, relays) {
|
|
729
|
+
const wait = this.blockedFor(relay);
|
|
730
|
+
if (!relays.every((r) => this.blockedFor(r) > 0)) return wait;
|
|
731
|
+
return Math.min(wait, Math.max(0, this.lastAllDownProbe + this.allDownProbeMs - this.now()));
|
|
732
|
+
}
|
|
733
|
+
/** A new network: every relay starts fresh. */
|
|
734
|
+
reset() {
|
|
735
|
+
this.circuits.clear();
|
|
736
|
+
this.lastAllDownProbe = -Infinity;
|
|
737
|
+
}
|
|
738
|
+
/**
|
|
739
|
+
* Each relay's health, in the order given. While every one is left alone for failing, `until` is the next probe
|
|
740
|
+
* (`allDownProbe`, every 15 s), whose answer ends the wait of them all: said as the end of each one's wait, a daemon's
|
|
741
|
+
* `status` and the connection panel named a time up to 5 minutes after the relays were seen answering again.
|
|
742
|
+
*/
|
|
743
|
+
health(relays) {
|
|
744
|
+
const allDown = relays.length > 0 && relays.every((relay) => this.blockedFor(relay) > 0 && this.circuits.get(relay)?.kind === "error");
|
|
745
|
+
return relays.map((relay) => {
|
|
746
|
+
const circuit = this.circuits.get(relay);
|
|
747
|
+
if (!circuit || circuit.openUntil === 0 || this.blockedFor(relay) === 0) return {
|
|
748
|
+
relay,
|
|
749
|
+
state: "ok"
|
|
750
|
+
};
|
|
751
|
+
const until = allDown ? this.now() + this.askedAgainIn(relay, relays) : Math.max(circuit.openUntil, this.now());
|
|
752
|
+
return {
|
|
753
|
+
relay,
|
|
754
|
+
state: circuit.kind === "throttled" ? "throttled" : "failing",
|
|
755
|
+
until,
|
|
756
|
+
...circuit.reason && { reason: circuit.reason }
|
|
757
|
+
};
|
|
758
|
+
});
|
|
759
|
+
}
|
|
760
|
+
};
|
|
761
|
+
// ../core/src/linkTrace.ts
|
|
762
|
+
var sink = null;
|
|
763
|
+
/** Where the lines go besides the console; `null` removes it. */
|
|
764
|
+
function setLinkTraceSink(next) {
|
|
765
|
+
sink = next;
|
|
766
|
+
}
|
|
767
|
+
function traceLink(me, step, detail) {
|
|
768
|
+
const console_ = globalThis.__ghostlyLinkTrace;
|
|
769
|
+
if (!console_ && !sink) return;
|
|
770
|
+
const line = JSON.stringify({
|
|
771
|
+
t: Date.now(),
|
|
772
|
+
me: me.slice(0, 6),
|
|
773
|
+
step,
|
|
774
|
+
...detail
|
|
775
|
+
});
|
|
776
|
+
if (console_) console.debug(`[ghostly:link] ${line}`);
|
|
777
|
+
try {
|
|
778
|
+
sink?.(line);
|
|
779
|
+
} catch {}
|
|
780
|
+
}
|
|
781
|
+
// ../core/src/relay.ts
|
|
782
|
+
var asker = (options, write) => ({
|
|
783
|
+
background: !!options.background,
|
|
784
|
+
group: !!options.group,
|
|
785
|
+
urgent: !!options.urgent,
|
|
786
|
+
door: !!options.door,
|
|
787
|
+
write,
|
|
788
|
+
signal: !!options.signal,
|
|
789
|
+
watch: !!options.watch && !write,
|
|
790
|
+
watchWrite: !!options.watch && write,
|
|
791
|
+
departed: !!options.departed && !write,
|
|
792
|
+
awaited: !!options.awaited && !write,
|
|
793
|
+
resume: !!options.resume && !write,
|
|
794
|
+
back: !!options.back
|
|
795
|
+
});
|
|
796
|
+
/** A 1:1 chat's request: neither a group's nor a background one. */
|
|
797
|
+
var isChat = (who) => !who.background && !who.group;
|
|
798
|
+
/** A 1:1 chat's offer or answer, or its read for the answer to its offer: it may use the allowance (`SIGNALING_ALLOWANCE_SHARE`). */
|
|
799
|
+
var isChatSignal = (who) => who.signal && isChat(who);
|
|
800
|
+
/** A 1:1 chat's read of a contact that just went away: a few may use the chat's allowance (`DEPARTED_READS`). */
|
|
801
|
+
var isDepartedRead = (who) => who.departed && isChat(who);
|
|
802
|
+
/** A 1:1 chat's read of an invite this side made, its joiner due: a few may use the chat's allowance (`AWAITED_READS`). */
|
|
803
|
+
var isAwaitedRead = (who) => who.awaited && isChat(who);
|
|
804
|
+
/** A group's edge looking fast for a signal (`GROUP_BURST_MS`). */
|
|
805
|
+
var isGroupUrgentRead = (who) => who.group && who.urgent && !who.write && !who.background;
|
|
806
|
+
/**
|
|
807
|
+
* A group edge's read for a member that went away from a live session and has not shown itself back (`WATCH_SHARE`).
|
|
808
|
+
* A 1:1 chat's is not one: chats have their own reserve (`CHAT_RESERVE`), and a chat watching its contact must not wait
|
|
809
|
+
* behind the edges that watch the same contact.
|
|
810
|
+
*/
|
|
811
|
+
var isWatch = (who) => who.watch && who.group && !who.write;
|
|
812
|
+
/** A 1:1 chat's read of a contact that went away from a live session and has not shown itself back (`CHAT_WATCH_SHARE`). */
|
|
813
|
+
var isChatWatch = (who) => who.watch && isChat(who);
|
|
814
|
+
/**
|
|
815
|
+
* A group edge's request for a member that may be back: it may use the groups' allowance (`GROUP_SIGNALING_ALLOWANCE_SHARE`).
|
|
816
|
+
* So may its reads of a member that was live when this app last ran, until one is answered (`resume`): its offer waits on them.
|
|
817
|
+
*/
|
|
818
|
+
var isGroupSignal = (who) => who.group && !who.background && (who.signal || isWatch(who) || who.watchWrite || who.resume);
|
|
819
|
+
/** A group edge's read for the signal it waits for, the answer to its offer (`signal`): first on the floor (`ANSWER_READ_FIRST_MS`). */
|
|
820
|
+
var isGroupAnswerRead = (who) => isGroupSignal(who) && who.signal && !who.write;
|
|
821
|
+
/** Such a read, as the budget weighs it (`ANSWER_READ_FIRST_MS`). */
|
|
822
|
+
var ANSWER_READER = {
|
|
823
|
+
background: false,
|
|
824
|
+
group: true,
|
|
825
|
+
urgent: true,
|
|
826
|
+
door: false,
|
|
827
|
+
write: false,
|
|
828
|
+
signal: true,
|
|
829
|
+
watch: false,
|
|
830
|
+
watchWrite: false,
|
|
831
|
+
departed: false,
|
|
832
|
+
awaited: false,
|
|
833
|
+
resume: false,
|
|
834
|
+
back: false
|
|
835
|
+
};
|
|
836
|
+
/** A community member's way back to its group while it has no edge up: it may use its own allowance (`WAY_BACK_SHARE`). */
|
|
837
|
+
var isWayBack = (who) => who.back && who.group && !who.background;
|
|
838
|
+
/** Watch reads that the same other limits hold take turns together: urgent or not, background or not; the chats' on their own. */
|
|
839
|
+
var watchLane = (who) => isChat(who) ? "chat" : `${+who.urgent}${+who.background}`;
|
|
840
|
+
/** A group's read that is not a background one: an edge's, a private group's knock poll (`GROUP_RATION_WINDOW_MS`). */
|
|
841
|
+
var isGroupRead = (who) => who.group && !who.write && !who.background;
|
|
842
|
+
/**
|
|
843
|
+
* A read that takes its turn per key with the others (`READ_TURN_MS`): a background one, and a 1:1 chat's at any pace.
|
|
844
|
+
* Not a community door's bell.
|
|
845
|
+
*/
|
|
846
|
+
var isTurnRead = (who) => !who.write && !who.door && (who.background || !who.group);
|
|
847
|
+
/** A write that goes first once the budget frees a request (see `WRITE_FIRST_MS`): a chat's or a group's, never a background one. */
|
|
848
|
+
var firstWriter = (who) => !who.write || who.background ? null : who.group ? "group" : "chat";
|
|
849
|
+
/** A key's turn neither asked nor read in the last minute: it is dropped, and asking again starts a new one (`TURNS_PRUNE_MS`). */
|
|
850
|
+
var staleTurn = (turn, now) => now - Math.max(turn.askedAt, turn.servedAt) >= 6e4;
|
|
851
|
+
/**
|
|
852
|
+
* The times of `list` (oldest first) within the last `ms`. One dated after now, the device clock gone back since (an NTP
|
|
853
|
+
* step, a clock that ran ahead fixed by hand), counts as of now: as just spent, not for as long as the clock went back.
|
|
854
|
+
* Counted as they were, an hour's step back held every background read for 59 minutes, and a daemon with 50 contacts
|
|
855
|
+
* away read 4 of them in the next 15 (bug hunt, 2026-11-07).
|
|
856
|
+
* `list` itself when every time is already so (the usual case): `heldFor` reads eight lists at each request the budget
|
|
857
|
+
* holds, and copying each twice was a third of the CPU of a daemon whose 200 contacts were away.
|
|
858
|
+
*/
|
|
859
|
+
var within = (list, now, ms) => {
|
|
860
|
+
let kept = true;
|
|
861
|
+
for (const at of list) if (at > now || now - at >= ms) {
|
|
862
|
+
kept = false;
|
|
863
|
+
break;
|
|
864
|
+
}
|
|
865
|
+
return kept ? list : list.map((at) => Math.min(at, now)).filter((at) => now - at < ms);
|
|
866
|
+
};
|
|
867
|
+
/**
|
|
868
|
+
* Keys and when each was last set, those of the last minute: how many and the latest, without walking them all (the
|
|
869
|
+
* chats that need a relay, `chatNeeds`). Each is set at the time it is set, so the sets come oldest first; a clock
|
|
870
|
+
* gone back makes the later ones as of now.
|
|
871
|
+
*/
|
|
872
|
+
var RecentKeys = class {
|
|
873
|
+
at = /* @__PURE__ */ new Map();
|
|
874
|
+
/** Every set, oldest first, from `head`; one a later set or a delete replaced is skipped. */
|
|
875
|
+
order = [];
|
|
876
|
+
head = 0;
|
|
877
|
+
set(key, now) {
|
|
878
|
+
this.prune(now);
|
|
879
|
+
this.at.set(key, now);
|
|
880
|
+
this.order.push([key, now]);
|
|
881
|
+
}
|
|
882
|
+
delete(key) {
|
|
883
|
+
this.at.delete(key);
|
|
884
|
+
}
|
|
885
|
+
size(now) {
|
|
886
|
+
this.prune(now);
|
|
887
|
+
return this.at.size;
|
|
888
|
+
}
|
|
889
|
+
/** When the key set last was, -Infinity with none in the last minute. */
|
|
890
|
+
latest(now) {
|
|
891
|
+
this.prune(now);
|
|
892
|
+
while (this.order.length > this.head) {
|
|
893
|
+
const [key, at] = this.order[this.order.length - 1];
|
|
894
|
+
if (this.at.get(key) === at) return at;
|
|
895
|
+
this.order.pop();
|
|
896
|
+
}
|
|
897
|
+
return -Infinity;
|
|
898
|
+
}
|
|
899
|
+
/** The keys set in the last minute, oldest first. */
|
|
900
|
+
*entries(now) {
|
|
901
|
+
this.prune(now);
|
|
902
|
+
for (let i = this.head; i < this.order.length; i++) if (this.at.get(this.order[i][0]) === this.order[i][1]) yield this.order[i];
|
|
903
|
+
}
|
|
904
|
+
/** Drops the keys set a minute ago or more. */
|
|
905
|
+
prune(now) {
|
|
906
|
+
if (this.order.length > this.head && now < this.order[this.order.length - 1][1]) {
|
|
907
|
+
for (const [key, at] of this.at) this.at.set(key, Math.min(at, now));
|
|
908
|
+
this.order = [...this.at].sort((a, b) => a[1] - b[1]);
|
|
909
|
+
this.head = 0;
|
|
910
|
+
}
|
|
911
|
+
while (this.head < this.order.length) {
|
|
912
|
+
const [key, at] = this.order[this.head];
|
|
913
|
+
if (this.at.get(key) === at) {
|
|
914
|
+
if (now - at < 6e4) break;
|
|
915
|
+
this.at.delete(key);
|
|
916
|
+
}
|
|
917
|
+
this.head++;
|
|
918
|
+
}
|
|
919
|
+
if (this.head > 64 && this.head * 2 > this.order.length) {
|
|
920
|
+
this.order = this.order.slice(this.head);
|
|
921
|
+
this.head = 0;
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
};
|
|
925
|
+
/**
|
|
926
|
+
* Public Pkarr relays. They are generic Pkarr infrastructure (an HTTP bridge to
|
|
927
|
+
* the Mainline DHT), not a Ghostly backend: they only ever see signed,
|
|
928
|
+
* encrypted packets. Every client uses this list: the browser clients read and
|
|
929
|
+
* write through it, Ghostly Desktop writes to it (so browser contacts see its
|
|
930
|
+
* packets) and reads from it only when "Also use Pkarr relays" is on. Adding a
|
|
931
|
+
* relay is one line here; see docs/RELAYS.md for how one is chosen.
|
|
932
|
+
*/
|
|
933
|
+
var DEFAULT_RELAYS = [
|
|
934
|
+
"https://pkarr.pubky.org",
|
|
935
|
+
"https://pkarr.pubky.app",
|
|
936
|
+
"https://pkarr.miguelmedeiros.dev"
|
|
937
|
+
];
|
|
938
|
+
/**
|
|
939
|
+
* Relay lists that were the defaults once: a profile that still has one gets today's defaults. When a relay
|
|
940
|
+
* joins `DEFAULT_RELAYS`, the list as it was goes here, so profiles on the old defaults move with it.
|
|
941
|
+
*/
|
|
942
|
+
var PREVIOUS_DEFAULT_RELAYS = [["https://pkarr.pubky.org", "https://pkarr.pubky.app"]];
|
|
943
|
+
/** The defaults for `relays` when it is one of the old default lists, `relays` itself otherwise. */
|
|
944
|
+
function currentRelays(relays) {
|
|
945
|
+
const same = (a, b) => a.length === b.length && a.every((relay, i) => relay === b[i]);
|
|
946
|
+
return PREVIOUS_DEFAULT_RELAYS.some((old) => same(old, relays)) ? [...DEFAULT_RELAYS] : relays;
|
|
947
|
+
}
|
|
948
|
+
/**
|
|
949
|
+
* Requests this client allows itself per relay and minute. Relays limit by IP
|
|
950
|
+
* (120 a minute when this was written) and one address is often shared by
|
|
951
|
+
* several peers: two browser profiles, a tab and an extension, a household.
|
|
952
|
+
*/
|
|
953
|
+
var REQUESTS_PER_MINUTE = 30;
|
|
954
|
+
/**
|
|
955
|
+
* Of those, what background requests (`{ background: true }`: periodic looks at shared records, as a
|
|
956
|
+
* community group's hub makes) may spend, counted on their own: the rest is kept for links, whose
|
|
957
|
+
* signaling cannot wait, and a burst of signaling does not hold the background ones back afterwards.
|
|
958
|
+
*/
|
|
959
|
+
var BACKGROUND_REQUESTS_PER_MINUTE = 20;
|
|
960
|
+
/**
|
|
961
|
+
* Of the background share, what background writes may spend in a minute on a relay (10 of the 20): the rest is kept
|
|
962
|
+
* for background reads. Those writes are mostly the upkeep of chats whose contacts are away (the link's heartbeat, the
|
|
963
|
+
* DHT floor's keep-alive, the capability record), about one a minute for every two chats. A profile with 200 such chats
|
|
964
|
+
* spent the share on them, and on one relay 147 of its 200 contacts were not read once in 25 minutes: a contact back
|
|
965
|
+
* then went unnoticed (bug hunt, 2026-11-01). Upkeep held back goes when a request of this share frees.
|
|
966
|
+
*/
|
|
967
|
+
var BACKGROUND_WRITES_SHARE = 1 / 2;
|
|
968
|
+
/**
|
|
969
|
+
* A link's write the budget refused (its presence, its offer, its answer) goes first when the minute
|
|
970
|
+
* frees a request: reads on that relay wait this long after the refusal. A link that polls fast for a
|
|
971
|
+
* peer while its offer waits would otherwise take every request the minute frees, and the offer (the
|
|
972
|
+
* only thing that peer is waiting for) would go out only once the polling slowed down, half a minute later.
|
|
973
|
+
*/
|
|
974
|
+
var WRITE_FIRST_MS = 5e3;
|
|
975
|
+
/**
|
|
976
|
+
* Once a 1:1 chat needs the relays (the budget refused one of its requests there, or it read urgently anywhere: it polls
|
|
977
|
+
* fast for a signal due any moment), group requests and background ones leave it the last this many of each minute (more
|
|
978
|
+
* for several chats, `CHAT_RESERVE_PER_CHAT`), for a minute. Groups that spend the whole budget spend it over the minute, so the chat then takes the next request that frees, within
|
|
979
|
+
* seconds, and has room for what follows (the receipt). Waiting for a refusal was too late when a contact went away
|
|
980
|
+
* from a chat and from groups at once: the chat and the groups' edges all watched fast for it, the edges spent both
|
|
981
|
+
* relays' minute in 40 s, and the chat read the contact's offer half a minute late (2026-09-27). A chat with nothing
|
|
982
|
+
* to wait for leaves groups the whole budget: a community door alone costs about 22 requests a minute, and a reserve
|
|
983
|
+
* kept for any chat starved a new group's edges. The reserve is per chat: a chat that reads its contact at a slow pace
|
|
984
|
+
* again (it is live) gives its part back at once. Kept for the minute, a daemon back after a restart with its chats
|
|
985
|
+
* live in 18 s held a group's edge that had just missed its read for 48 s more (bug hunt r6a, 2026-09-29).
|
|
986
|
+
*/
|
|
987
|
+
var CHAT_RESERVE = 10;
|
|
988
|
+
/**
|
|
989
|
+
* …and more while several chats need the relays at once: this many requests each (a read, an offer or answer, the read
|
|
990
|
+
* for the answer to it), up to `CHAT_RESERVE_MOST_SHARE` of the minute. Background requests leave it too, not the
|
|
991
|
+
* groups' alone: the looks of chats whose contacts are not there, and their presence (upkeep, `LinkSession`). An app
|
|
992
|
+
* opened while 30 of its 50 chats' contacts were online spent both relays' minute in its first second, on the presence
|
|
993
|
+
* of the chats whose contacts were long gone, its groups' records and every chat's first read: 12 of the 30 offers and
|
|
994
|
+
* answers went out, no read for an answer could, and 0 of the 30 chats were live 60 s after the start (bug hunt,
|
|
995
|
+
* 2026-10-13).
|
|
996
|
+
*/
|
|
997
|
+
var CHAT_RESERVE_PER_CHAT = 3;
|
|
998
|
+
/** The most of a relay's minute the chats' reserve takes, however many chats need it: the rest stays for everything else. */
|
|
999
|
+
var CHAT_RESERVE_MOST_SHARE = 5 / 6;
|
|
1000
|
+
/**
|
|
1001
|
+
* A 1:1 chat's signaling (a new offer or answer, and its reads for the answer to its offer, `signal`) may go over a
|
|
1002
|
+
* relay's minute by this fraction of it: 6 on a relay of 30, 12 on one of 60, 1 on a relay of 5 (relay.pkarr.org and
|
|
1003
|
+
* pkarr.miguelmedeiros.dev allow 10), and never more than that in any minute; its reads leave the last of those to a write. Everything else still stops at
|
|
1004
|
+
* the limit. The minute before a chat leaves DHT only can be spent by then (the pairing, the texts over the DHT), and
|
|
1005
|
+
* its offer or answer, the one thing the contact waits for, then waited 40 s for requests to age out while the
|
|
1006
|
+
* contact's attempt gave up: the matrix's extension chat stayed "On DHT · retrying live" 46 times in 120 (2026-09-30).
|
|
1007
|
+
*/
|
|
1008
|
+
var SIGNALING_ALLOWANCE_SHARE = 1 / 5;
|
|
1009
|
+
/**
|
|
1010
|
+
* A 1:1 chat's reads of a contact that went away from a live session, in their first seconds (`departed`,
|
|
1011
|
+
* `DEPARTED_READ_MS` in link.ts), may go over a relay's minute within the chat's allowance (`SIGNALING_ALLOWANCE_SHARE`):
|
|
1012
|
+
* at most this many of each contact's in a minute, every relay together, leaving the last of the allowance to an offer or
|
|
1013
|
+
* answer. Their fast poll reads every 2 s, on the relays in turn: 6 cover 12 s from the goodbye, by when an app that
|
|
1014
|
+
* restarts has its offer out (2.5 to 10 s in the lab of #1441), and add at most 3 to a relay's minute. They wait for no
|
|
1015
|
+
* write the minute refused: that write cannot go before the minute frees anyway. A daemon restarted 20 s after its last
|
|
1016
|
+
* restart found its contact's minute spent on both relays: every read of it was held back, the offer it published as it
|
|
1017
|
+
* came up was never read, and the chat came back only through the race's DHT dial, 8 s on (2026-10-07).
|
|
1018
|
+
*/
|
|
1019
|
+
var DEPARTED_READS = 6;
|
|
1020
|
+
/**
|
|
1021
|
+
* A 1:1 chat's reads of an invite this side made, while its joiner is due and has not shown (`awaited`, `AWAITING_PEER_MS`
|
|
1022
|
+
* in link.ts), may go over a relay's minute within the chat's allowance (`SIGNALING_ALLOWANCE_SHARE`) the same way: at
|
|
1023
|
+
* most this many in a minute, every invite and every relay together. And while one asked in the last minute, the departed
|
|
1024
|
+
* reads leave them half of that on each relay: the last `AWAITED_READS / 2` of the allowance's reads (`DEPARTED_READS`
|
|
1025
|
+
* then take at most 3 on a relay of 30, against 5). Read every 2 to 4 s, they cover the seconds in which a joiner that
|
|
1026
|
+
* opened the link at once knocks. Within the chats' allowance, they add nothing to what a relay may see in a minute
|
|
1027
|
+
* (`WAY_BACK_SHARE`): 36 at most from the chats on a relay of 30, none past 5 on a relay of 5. An inviter pairing one joiner after
|
|
1028
|
+
* another on relays of 30 spent the minute on both 12 s in (two pairings, about 15 requests a relay each), the departed
|
|
1029
|
+
* reads of the contact that had just left took the allowance, and the third invite's key was read once, then not until
|
|
1030
|
+
* the minute freed: the joiner's knock 48 s late, and join to live in 60 s, against 2.6 and 4.6 s (CLI lab, 2026-11-07).
|
|
1031
|
+
* Not this many per invite: a web profile with 150 chats added from old sessions, each an invite awaited from that start,
|
|
1032
|
+
* had their reads take the whole allowance at every refill, and a live contact's reads after a reload went once a
|
|
1033
|
+
* minute: the chat came back in 2 to 7 min, against 41 to 69 s before (2026-11-07).
|
|
1034
|
+
*/
|
|
1035
|
+
var AWAITED_READS = 4;
|
|
1036
|
+
/**
|
|
1037
|
+
* A group edge's requests for a member that may be back may go over a relay's minute by this fraction of it (4 on a
|
|
1038
|
+
* relay of 30, 8 on one of 60, none on a relay of 5): its reads of a member that went away and its packet
|
|
1039
|
+
* written meanwhile, which that member reads first when it is back (`watch`), its offer or answer, and its reads for
|
|
1040
|
+
* the answer to its offer (`signal`). Its reads leave the last of those to a write and go
|
|
1041
|
+
* one at a time (`GROUP_OVER_PACE_MS`), so the allowance lasts until the member is back, and the read that finds its
|
|
1042
|
+
* offer still has the answer behind it. Less than a chat's (`SIGNALING_ALLOWANCE_SHARE`), which keeps room past the
|
|
1043
|
+
* groups'. A private group's admin that had just let members in and seen them restart had spent its minute in 30 s;
|
|
1044
|
+
* a member back then, with no other member online to carry its offer, had it read only when the minute freed: 18 to
|
|
1045
|
+
* 42 s later, one round in four of the member-back runs (2026-10-03).
|
|
1046
|
+
*
|
|
1047
|
+
* It is also theirs whatever the chats do: while the edges' signaling has had fewer than that many requests on a relay
|
|
1048
|
+
* in the last minute, neither the limit, nor the chats' own allowance past it, nor their reserve (`CHAT_RESERVE`) holds
|
|
1049
|
+
* it back. Its reads leave the last of it to a write there too. A daemon with 30 chats and 5 groups, back after a
|
|
1050
|
+
* restart, had its chats signal for 332 s: they kept their reserve, their allowance took what the limit left, and the
|
|
1051
|
+
* edges got nothing. By then their attempts had failed and backed off, and the group members were reachable again 13
|
|
1052
|
+
* to 25 min after the restart (CLI bug hunt, 2026-10-08).
|
|
1053
|
+
*
|
|
1054
|
+
* An edge back after a restart reads its member before it offers (`resume`), and those reads are on the floor too: as
|
|
1055
|
+
* plain reads, they waited behind the chats as before, and the edges never got to signal. The same daemon had its first
|
|
1056
|
+
* member back 71 to 367 s after the restart, and group texts sent in its first minutes were read 2 to 7 min late (CLI
|
|
1057
|
+
* bug hunt, 2026-10-14).
|
|
1058
|
+
*/
|
|
1059
|
+
var GROUP_SIGNALING_ALLOWANCE_SHARE = 2 / 15;
|
|
1060
|
+
/**
|
|
1061
|
+
* A community member's way back to its group while it has no edge up (its readings of the beacon, its request in a hub's
|
|
1062
|
+
* lobby: `back`) may go past everything that holds a group's request back by this fraction of a relay's minute (6 on a
|
|
1063
|
+
* relay of 30, 12 on one of 60, 1 on a relay of 5), its own: not the edges' (`GROUP_SIGNALING_ALLOWANCE_SHARE`),
|
|
1064
|
+
* and what it takes counts there alone, so it shortens no one else's minute or allowance. Its reads leave the last of it
|
|
1065
|
+
* to a write. A web app opened with 30 chats whose contacts were online and 15 groups whose edges all offered at once:
|
|
1066
|
+
* the edges' offers and reads for their members spent the groups' allowance in its first second, the chats' reserve
|
|
1067
|
+
* and their offers still waiting held the rest, and its 5 communities read their beacon (who the hubs are) a minute
|
|
1068
|
+
* later, or not in 3 minutes; 2 of them were not back in 6 (bug hunt, 2026-10-14). Sized for 5 communities' way back
|
|
1069
|
+
* in the first minute on the default relays: a reading of each beacon, and a read and a write in a hub's lobby (a write
|
|
1070
|
+
* goes to every relay). A member asks it only while it has no edge up in that community.
|
|
1071
|
+
*
|
|
1072
|
+
* At worst a relay then sees, in one minute, its limit, the chats' allowance past it, the edges', this one and the edges'
|
|
1073
|
+
* answer reads' (`ANSWER_READ_ALLOWANCE`): 49 on a relay of 30 (pkarr.pubky.org allows 50 an address,
|
|
1074
|
+
* `x-ratelimit-limit`), 95 on pkarr.pubky.app's 60 (it allows 1000) and 7 on a relay of 5 (relay.pkarr.org and
|
|
1075
|
+
* pkarr.miguelmedeiros.dev allow 10). A share raised past those limits gets 429s (docs/TRANSPORTS.md, "The default list").
|
|
1076
|
+
*/
|
|
1077
|
+
var WAY_BACK_SHARE = 1 / 5;
|
|
1078
|
+
/** Past the limit, a group's reads within its allowance go no more often than this on a relay. */
|
|
1079
|
+
var GROUP_OVER_PACE_MS = 5e3;
|
|
1080
|
+
/**
|
|
1081
|
+
* A group edge's read for the answer to its offer (`signal`) that the budget refused on a relay goes first there among
|
|
1082
|
+
* the groups' signaling (`GROUP_SIGNALING_ALLOWANCE_SHARE`): the edges' offers and answers, their reads back after a
|
|
1083
|
+
* restart (`resume`) and their watch reads wait this long after the refusal, while it is not read, for what it could
|
|
1084
|
+
* take then. It waits for no group write the budget refused (`WRITE_FIRST_MS`), nor for the turn of a group's key that
|
|
1085
|
+
* is not read for an answer (`GROUP_RATION_WINDOW_MS`), and the groups' other urgent reads do not spend its burst
|
|
1086
|
+
* (`GROUP_BURST_MS`): it has one of its own. Of everything on the floor it is the closest to a member back: the
|
|
1087
|
+
* offer is out, and the member has often answered already. A daemon back after a restart had its edges' offers and
|
|
1088
|
+
* reads back spend the floor in its first seconds, and read the answers to offers it had out at 0.4 s only once the
|
|
1089
|
+
* minute freed: 66 to 97 s on, and an answer read that late often found the member's attempt over (agents#971, CLI lab,
|
|
1090
|
+
* 2026-11-03).
|
|
1091
|
+
*/
|
|
1092
|
+
var ANSWER_READ_FIRST_MS = 5e3;
|
|
1093
|
+
/**
|
|
1094
|
+
* A group edge's read for the answer to its offer (`signal`) that everything else holds back (the limit, the groups'
|
|
1095
|
+
* allowance, the chats' reserve, a write the budget refused) goes within an allowance of its own on that relay: this
|
|
1096
|
+
* many in a minute, never more than the groups' allowance there (`GROUP_SIGNALING_ALLOWANCE_SHARE`: none on a relay of
|
|
1097
|
+
* 5), one at a time (`ANSWER_READ_SPACING_MS`). What it takes counts there alone, as a way back's (`WAY_BACK_SHARE`),
|
|
1098
|
+
* so it shortens no one else's minute. Going first on the floor (`ANSWER_READ_FIRST_MS`) was not enough: a daemon back
|
|
1099
|
+
* after a restart had its edges' reads back and offers spend its whole minute on each relay 6 s on, before any member
|
|
1100
|
+
* had answered (7 to 19 s on), and read the answers when the minute freed: from an answer going out to the hub
|
|
1101
|
+
* reading it, 60 to 66 s at the median in each of four runs. With this allowance, 2 to 60 s (14 s over the four runs,
|
|
1102
|
+
* against 63 s), and all 18 members were back 77 to 108 s on, against 102 to 153 s (agents#971, CLI lab, 2026-11-05).
|
|
1103
|
+
*
|
|
1104
|
+
* At worst it adds 3 to a relay's minute: 49 on a relay of 30 (pkarr.pubky.org allows 50), 95 on pkarr.pubky.app's 60,
|
|
1105
|
+
* and nothing on a relay of 5.
|
|
1106
|
+
*/
|
|
1107
|
+
var ANSWER_READ_ALLOWANCE = 3;
|
|
1108
|
+
/** Within that allowance, an edge's read for its answer goes no more often than this on a relay (`ANSWER_READ_ALLOWANCE`). */
|
|
1109
|
+
var ANSWER_READ_SPACING_MS = 2e4;
|
|
1110
|
+
/**
|
|
1111
|
+
* Groups' urgent reads (their edges looking fast for a signal) take at most a quarter of a relay's minute in any this
|
|
1112
|
+
* long, so that they never spend it in a burst. An app back after a restart has every edge offering and looking fast at
|
|
1113
|
+
* once; the members at the other end answer only once they notice its old sessions went (about 20 s with
|
|
1114
|
+
* node-datachannel). On the default relays the edges spent the groups' share of the minute in those seconds, and read
|
|
1115
|
+
* nothing for 40 s more: a private group's edges were live again 75 to 110 s after a restart (bug hunt r5a, 2026-09-29).
|
|
1116
|
+
*/
|
|
1117
|
+
var GROUP_BURST_MS = 15e3;
|
|
1118
|
+
/** …and never fewer than one link polling fast (every 2 s) reads in that time: one edge alone is never held back. */
|
|
1119
|
+
var GROUP_BURST_ONE_LINK = 8;
|
|
1120
|
+
/**
|
|
1121
|
+
* A group's edges' reads of members that went away from a live session and have not shown themselves back (`watch`: a
|
|
1122
|
+
* link looks for them closely for two minutes, `WATCH_PEER_MS`) take at most this share of a relay's requests, whatever
|
|
1123
|
+
* their number, spread over the minute (`GROUP_RATION_WINDOW_MS`): 2 in any 10 s on a relay of 30. Under it, the key
|
|
1124
|
+
* read longest ago goes first. A member alone in a private group whose three other members left at once watched each of
|
|
1125
|
+
* them every 2 s: those reads and the admin's knock poll spent both relays' minute, the member back first had its offer
|
|
1126
|
+
* read only when the minute freed, 25 to 44 s on, and its edge came up then (bug hunt r12, measured again 2026-10-03).
|
|
1127
|
+
*/
|
|
1128
|
+
var WATCH_SHARE = 1 / 3;
|
|
1129
|
+
/**
|
|
1130
|
+
* 1:1 chats' reads of contacts that went away from a live session and have not shown themselves back (`watch`) take at
|
|
1131
|
+
* most this share of a relay's requests the same way, on their own (a chat never waits behind the edges that watch the
|
|
1132
|
+
* same contact): 3 in any 10 s on a relay of 30, more than one contact's fast poll asks for. The key read longest ago
|
|
1133
|
+
* goes first. Not the few reads of a contact that just went away past the limit (`DEPARTED_READS`). Ten contacts that
|
|
1134
|
+
* left a CLI daemon at once were read every 2 s each: those reads spent both relays' minute in the 10 s after their
|
|
1135
|
+
* goodbyes, when none could be back yet, then the daemon read nobody, nor answered anyone, for 45 s, and the chats with
|
|
1136
|
+
* no HyperDHT listener at the daemon were live again 42 to 51 s after their contacts were back (CLI bug hunt, 2026-10-14).
|
|
1137
|
+
*/
|
|
1138
|
+
var CHAT_WATCH_SHARE = 1 / 2;
|
|
1139
|
+
/**
|
|
1140
|
+
* A key a group reads that asked within this long and was not read since keeps its turn: while one read longer ago
|
|
1141
|
+
* waits, the others wait too (`WATCH_SHARE`, `GROUP_RATION_WINDOW_MS`). One that stops asking loses it.
|
|
1142
|
+
*/
|
|
1143
|
+
var WATCH_TURN_MS = 5e3;
|
|
1144
|
+
/**
|
|
1145
|
+
* Background reads, and a 1:1 chat's at any pace, take turns per key the same way: while a key read longer ago asked
|
|
1146
|
+
* within this long and was not read since, a read of another key waits (a chat's, at a pace that is not the background
|
|
1147
|
+
* one, only for another chat's key). A saved chat that is not live reads its contact every 30 s in the
|
|
1148
|
+
* background, and one the budget refused read again only at its next poll. A request frees 60 s after the one that took
|
|
1149
|
+
* it, which is the poll after next of the chat that took it: the same chats took every request that freed. A daemon with
|
|
1150
|
+
* 30 chats and 5 groups, back after a restart while its groups signaled, read some contacts once in 10 min, and those
|
|
1151
|
+
* chats stayed down although both sides were online and offering (CLI bug hunt, 2026-10-12). A link's background read
|
|
1152
|
+
* the budget refused reads again when it frees a request (`LinkSession`), so the key whose turn it is is there to take it.
|
|
1153
|
+
* A chat not live after a restart looks fast while its offer is out, and those reads took no turn: the budget answered
|
|
1154
|
+
* them from the packet read before, every 2 to 8 s for 10 minutes, while the chats that were live already, the groups'
|
|
1155
|
+
* edges and the writes took every request that freed. A quarter of a daemon's 30 chats read their contact once in 10
|
|
1156
|
+
* minutes, their contacts online and offering, and showed them offline at 6 minutes (CLI bug hunt, 2026-10-13).
|
|
1157
|
+
*/
|
|
1158
|
+
var READ_TURN_MS = 5e3;
|
|
1159
|
+
/**
|
|
1160
|
+
* The keys' turns asked and read over a minute ago are dropped at most this often: walking every key on each ask cost in
|
|
1161
|
+
* proportion to the chats, 12-14% of an idle web app's script with 200 chats.
|
|
1162
|
+
*/
|
|
1163
|
+
var TURNS_PRUNE_MS = 1e3;
|
|
1164
|
+
/**
|
|
1165
|
+
* A 1:1 chat's read the budget held back this long without a break is starved: it goes before the writes that wait
|
|
1166
|
+
* (`WRITE_FIRST_MS`) and past the background share, within the minute, the key held back longest first. A daemon back
|
|
1167
|
+
* after a restart spends its whole minute on writes (each chat's and edge's offer, its packets after the attempt), which
|
|
1168
|
+
* go first, and requests free in bursts as the minute's first requests age out: every burst went to the writes that
|
|
1169
|
+
* waited, and the reads of the chats that were not in an attempt never had their turn (CLI restart lab, 2026-10-13).
|
|
1170
|
+
*/
|
|
1171
|
+
var READ_STARVED_MS = 6e4;
|
|
1172
|
+
/** How long a read that let an older one go first waits before it asks again. */
|
|
1173
|
+
var TURN_WAIT_MS = 1e3;
|
|
1174
|
+
/**
|
|
1175
|
+
* A background read the budget held back far down the turns asks again no later than this (`backgroundTurnIn`). The
|
|
1176
|
+
* requests past the background share go to a read held back long (`READ_STARVED_MS`) that is there when one frees: asked
|
|
1177
|
+
* only at its estimated turn, a web app with 200 chats away read each contact every 7 to 8 minutes, not every 6, and
|
|
1178
|
+
* one with 100 left half the writes' share unused at 20 s.
|
|
1179
|
+
*/
|
|
1180
|
+
var BACKGROUND_TURN_MAX_MS = 15e3;
|
|
1181
|
+
/**
|
|
1182
|
+
* Once a group's read found a relay's minute full, the groups' reads there are spread over the minute for a minute
|
|
1183
|
+
* (`GROUP_RATION_MS`): at most a sixth of it in any 10 s (5 on a relay of 30), the key read longest ago first. A sliding
|
|
1184
|
+
* minute that demand keeps full hands out its requests in bursts: the minute is spent in 25 to 35 s as requests age
|
|
1185
|
+
* out, then nothing frees for the rest of it. A private group's admin (its knock poll every 2 s while its link is new)
|
|
1186
|
+
* whose members left and came back read nothing for 18 to 42 s at a time, and the member back first waited that long
|
|
1187
|
+
* for its edge (bug hunt r12, measured again on 2026-10-03). Writes are not counted: they go first anyway.
|
|
1188
|
+
*/
|
|
1189
|
+
var GROUP_RATION_WINDOW_MS = 1e4;
|
|
1190
|
+
var GROUP_RATION_MS = 6e4;
|
|
1191
|
+
/**
|
|
1192
|
+
* While a link polls fast (an `urgent` read: its peer, or the peer's offer or answer, is due any moment) within this
|
|
1193
|
+
* long, background requests (a community's periodic looks) are held to `BACKGROUND_WHILE_SIGNALING` a minute on each
|
|
1194
|
+
* relay rather than their usual share. A fast poll reads every 2 s, one relay after the other.
|
|
1195
|
+
*/
|
|
1196
|
+
var SIGNALING_WINDOW_MS = 1e4;
|
|
1197
|
+
/**
|
|
1198
|
+
* Background requests a minute on each relay while a link signals: one look every 12 s. Next to a community's
|
|
1199
|
+
* lookups (20 a minute), a new group edge on one relay got 10 and read every 6 s, and its offer and answer missed each
|
|
1200
|
+
* other's fast windows (2026-09-27). Not none: fast windows can follow one another, and a hub still reads its knocks.
|
|
1201
|
+
*/
|
|
1202
|
+
var BACKGROUND_WHILE_SIGNALING = 5;
|
|
1203
|
+
/**
|
|
1204
|
+
* A read of a key the relays answered this recently is answered from that answer, with no request. Several parts of
|
|
1205
|
+
* a chat read the contact's keys (the link's poll, its capabilities, DHT delivery), and each tends to read again
|
|
1206
|
+
* within a few dozen milliseconds of its last read, when a pace changes or a signal goes out: nothing new can be there
|
|
1207
|
+
* yet, and a pairing spent a sixth of its requests on those reads (2026-09-27). A publish under the key forgets it.
|
|
1208
|
+
*/
|
|
1209
|
+
var FRESH_READ_MS = 500;
|
|
1210
|
+
/** A relay that missed a key's newest packet gets it (`catchUpLater`) no sooner than this, and tries again this often. */
|
|
1211
|
+
var CATCH_UP_MIN_MS = 1e3;
|
|
1212
|
+
var CATCH_UP_RETRY_MS = 5e3;
|
|
1213
|
+
/**
|
|
1214
|
+
* The longest another background write is told to wait behind the packets queued for a relay (`catchUpLater`): the
|
|
1215
|
+
* link and the DHT floor try again within a minute anyway, and longer kept a chat's upkeep off a relay with a long queue
|
|
1216
|
+
* for most of the presence window.
|
|
1217
|
+
*/
|
|
1218
|
+
var CATCH_UP_BEHIND_MAX_MS = 6e4;
|
|
1219
|
+
/** A catch-up put waits behind every link's request: a background write. A group edge's offer or answer goes as its own (`putEverywhere`). */
|
|
1220
|
+
var CATCH_UP_ASKER = {
|
|
1221
|
+
background: true,
|
|
1222
|
+
group: false,
|
|
1223
|
+
urgent: false,
|
|
1224
|
+
door: false,
|
|
1225
|
+
write: true,
|
|
1226
|
+
signal: false,
|
|
1227
|
+
watch: false,
|
|
1228
|
+
watchWrite: false,
|
|
1229
|
+
departed: false,
|
|
1230
|
+
awaited: false,
|
|
1231
|
+
resume: false,
|
|
1232
|
+
back: false
|
|
1233
|
+
};
|
|
1234
|
+
/** Keys whose last relay is remembered for their next read (`resolve`): a profile's links and lookups, with room. */
|
|
1235
|
+
var READ_TURNS_KEPT = 512;
|
|
1236
|
+
/** A relay that fails at the network level is left alone for this long. */
|
|
1237
|
+
var NETWORK_ERROR_COOLDOWN_MS = 2e4;
|
|
1238
|
+
/**
|
|
1239
|
+
* Relays whose limit per address is far from the others': this client's share of theirs, a minute, in place of
|
|
1240
|
+
* `REQUESTS_PER_MINUTE`. relay.pkarr.org (not a default, see docs/RELAYS.md) and pkarr.miguelmedeiros.dev (a default
|
|
1241
|
+
* since 2026-10-17) allow 10 (`x-ratelimit-limit`), which a browser cannot read without CORS exposing it: half of it
|
|
1242
|
+
* leaves room for a second app behind the same address. pkarr.pubky.app allows 1000 (pkarr.pubky.org, on the same address,
|
|
1243
|
+
* 50): 60 of ours leaves room for many peers behind one address, and a page that pairs three contacts in a minute,
|
|
1244
|
+
* about ten requests a relay each, still has requests to spare there (2026-09-28). Writes go to every relay and
|
|
1245
|
+
* count once one took them; reads go to a relay with requests left.
|
|
1246
|
+
*/
|
|
1247
|
+
var RELAY_REQUESTS_PER_MINUTE = {
|
|
1248
|
+
"https://relay.pkarr.org": 5,
|
|
1249
|
+
"https://pkarr.pubky.app": 60,
|
|
1250
|
+
"https://pkarr.miguelmedeiros.dev": 5
|
|
1251
|
+
};
|
|
1252
|
+
/**
|
|
1253
|
+
* A read asks the next relay too when the one it asked has not answered within this long, and takes the first good
|
|
1254
|
+
* answer. Measured on 2026-10-03 from a home connection: a relay hands over a packet it holds in 0.25 s (0.7 s on a new
|
|
1255
|
+
* connection), and one that has to fetch the packet from the DHT first takes 2.7 s. Reads used to wait for a relay's
|
|
1256
|
+
* answer before asking the next one, up to the 10 s timeout: a relay that answered slowly or not at all (pkarr.pubky.org
|
|
1257
|
+
* on 2026-10-03) made every read of a pairing that slow, and the pairing never finished.
|
|
1258
|
+
*/
|
|
1259
|
+
var HEDGE_MS = 1500;
|
|
1260
|
+
/**
|
|
1261
|
+
* The hedge delay for a key whose last read found nothing (404): a relay takes 3 to 5 s to say so (it asks the DHT
|
|
1262
|
+
* every time), and a hedge at `HEDGE_MS` would double every poll of a key nobody has published yet.
|
|
1263
|
+
*/
|
|
1264
|
+
var HEDGE_MISSING_MS = 6e3;
|
|
1265
|
+
/**
|
|
1266
|
+
* A packet that took longer than this to arrive, or a hedge lost to a relay that answered with the packet, is slow: it
|
|
1267
|
+
* counts against the relay's breaker like a failure (three in a row trip it, logged as "slow"), while another relay is
|
|
1268
|
+
* there to ask. A 404 says nothing either way: every relay is slow to give one.
|
|
1269
|
+
*/
|
|
1270
|
+
var SLOW_MS = 3e3;
|
|
1271
|
+
/** A relay found slow goes after the others in every read for this long, unless it answers fast meanwhile. */
|
|
1272
|
+
var SLOW_DEMOTE_MS = 6e4;
|
|
1273
|
+
/** How long each source has to answer a read or a put of the turn record (WISP 06 § Publishing and reading). */
|
|
1274
|
+
var TURN_SOURCE_TIMEOUT_MS = 8e3;
|
|
1275
|
+
/** A relay's refusals of a turn put: someone else wrote (409 an older or equal packet, 412 the condition, 428 a put in flight). */
|
|
1276
|
+
var TURN_REFUSALS = [
|
|
1277
|
+
409,
|
|
1278
|
+
412,
|
|
1279
|
+
428
|
|
1280
|
+
];
|
|
1281
|
+
/** How many times a payload signed here goes again past a relay's later copy (`putRedated`): three puts at most. */
|
|
1282
|
+
var PAYLOAD_REDATES = 2;
|
|
1283
|
+
/** On a relay's 404: the sequence of an item the DHT holds under the key that is no signed packet. */
|
|
1284
|
+
var INVALID_PACKET_SEQ_HEADER = "pkarr-invalid-signed-packet-seq";
|
|
1285
|
+
/** What `answer` says, or false when it has not said within `ms`. */
|
|
1286
|
+
function answeredWithin(answer, ms) {
|
|
1287
|
+
let timer;
|
|
1288
|
+
const late = new Promise((resolve) => {
|
|
1289
|
+
timer = setTimeout(() => resolve(false), ms);
|
|
1290
|
+
});
|
|
1291
|
+
return Promise.race([answer, late]).finally(() => clearTimeout(timer));
|
|
1292
|
+
}
|
|
1293
|
+
function normalizeRelayUrl(input) {
|
|
1294
|
+
try {
|
|
1295
|
+
const url = new URL(input.trim());
|
|
1296
|
+
if (url.protocol !== "https:" && url.protocol !== "http:") return null;
|
|
1297
|
+
return `${url.origin}${url.pathname.replace(/\/+$/, "")}`;
|
|
1298
|
+
} catch {
|
|
1299
|
+
return null;
|
|
1300
|
+
}
|
|
1301
|
+
}
|
|
1302
|
+
/**
|
|
1303
|
+
* Why a line of a Pkarr relay list is not a relay `normalizeRelayUrl` keeps, with the line as typed; null when it is
|
|
1304
|
+
* one. A host alone or another scheme (`relay.example.org`, `wss://…`) was meant as an https:// relay.
|
|
1305
|
+
*/
|
|
1306
|
+
function relayUrlProblem(input) {
|
|
1307
|
+
const line = input.trim();
|
|
1308
|
+
if (normalizeRelayUrl(line)) return null;
|
|
1309
|
+
return looksLikeHost(line) || looksLikeHost(line.replace(/^[a-z][a-z\d+.-]*:\/\//i, "")) ? `Use an https:// relay address: ${line}` : `Not a relay address: ${line}`;
|
|
1310
|
+
}
|
|
1311
|
+
/** No space, and a dot after the first character, before the last one and before any `/` or `:`; linear, for a long pasted line. */
|
|
1312
|
+
function looksLikeHost(text) {
|
|
1313
|
+
if (/\s/.test(text)) return false;
|
|
1314
|
+
const slash = text.search(/[/:]/);
|
|
1315
|
+
const dot = text.indexOf(".", 1);
|
|
1316
|
+
return dot > 0 && dot < text.length - 1 && (slash < 0 || dot < slash);
|
|
1317
|
+
}
|
|
1318
|
+
/** A relay the breaker leaves alone for failing (`RelayBreaker`): nothing was asked of it now. */
|
|
1319
|
+
var RelayLeftAloneError = class extends Error {};
|
|
1320
|
+
/**
|
|
1321
|
+
* A relay a put did not get an answer from. `sent` false: the request never left (its network cooling down after a
|
|
1322
|
+
* failure, or a connection that could not be made, `connectFailed`); true: it may have reached the relay.
|
|
1323
|
+
*/
|
|
1324
|
+
var RelayNoAnswerError = class extends Error {
|
|
1325
|
+
sent;
|
|
1326
|
+
constructor(message, sent) {
|
|
1327
|
+
super(message);
|
|
1328
|
+
this.sent = sent;
|
|
1329
|
+
}
|
|
1330
|
+
};
|
|
1331
|
+
/** What Node says of a connection it could not make: the request never left. A browser's TypeError says nothing either way. */
|
|
1332
|
+
var CONNECT_FAILURES = /* @__PURE__ */ new Set([
|
|
1333
|
+
"ECONNREFUSED",
|
|
1334
|
+
"ENOTFOUND",
|
|
1335
|
+
"EAI_AGAIN",
|
|
1336
|
+
"EHOSTUNREACH",
|
|
1337
|
+
"ENETUNREACH",
|
|
1338
|
+
"ENETDOWN",
|
|
1339
|
+
"EHOSTDOWN",
|
|
1340
|
+
"EADDRNOTAVAIL",
|
|
1341
|
+
"UND_ERR_CONNECT_TIMEOUT"
|
|
1342
|
+
]);
|
|
1343
|
+
/** A fetch that failed before its request left this device: Node's `cause.code` (each of an AggregateError's, for several addresses). */
|
|
1344
|
+
function connectFailed(error) {
|
|
1345
|
+
const cause = error?.cause;
|
|
1346
|
+
if (!cause || typeof cause !== "object") return false;
|
|
1347
|
+
if (Array.isArray(cause.errors) && cause.errors.length) return cause.errors.every((e) => CONNECT_FAILURES.has(e?.code));
|
|
1348
|
+
return CONNECT_FAILURES.has(cause.code);
|
|
1349
|
+
}
|
|
1350
|
+
/**
|
|
1351
|
+
* The error a publish ends with after a second round dated past a relay's later copy failed too: one that may have
|
|
1352
|
+
* stored the first round's packet still may, whatever the second round's relays said.
|
|
1353
|
+
*/
|
|
1354
|
+
function laterRound(first, later) {
|
|
1355
|
+
return publishedNowhere(later) && !publishedNowhere(first) ? new PublishFailedError(later.message, true) : later;
|
|
1356
|
+
}
|
|
1357
|
+
/** A packet date no further ahead of this clock than readers take (`newerPacket`): one this client may date past. */
|
|
1358
|
+
function withinSkew(timestamp) {
|
|
1359
|
+
return timestamp <= BigInt(Date.now() + PKARR_FUTURE_SKEW_MS) * 1000n;
|
|
1360
|
+
}
|
|
1361
|
+
/**
|
|
1362
|
+
* A read the relays failed (or left alone for failing before), when its caller has another path to ask
|
|
1363
|
+
* (`RelayTransport.resolve` with `keptOnFailure` false): `kept` is the newest packet the relays gave for the key before.
|
|
1364
|
+
*/
|
|
1365
|
+
var RelaysFailedError = class extends Error {
|
|
1366
|
+
kept;
|
|
1367
|
+
constructor(kept) {
|
|
1368
|
+
super("No Pkarr relay reachable");
|
|
1369
|
+
this.kept = kept;
|
|
1370
|
+
}
|
|
1371
|
+
};
|
|
1372
|
+
var RelayTransport = class {
|
|
1373
|
+
relays;
|
|
1374
|
+
timeoutMs;
|
|
1375
|
+
fetchFn;
|
|
1376
|
+
timeListeners = /* @__PURE__ */ new Set();
|
|
1377
|
+
lastTimestamp = /* @__PURE__ */ new Map();
|
|
1378
|
+
newest = /* @__PURE__ */ new Map();
|
|
1379
|
+
/**
|
|
1380
|
+
* The latest time of any packet read under a key, whoever dated it: the next packet this client puts there is dated
|
|
1381
|
+
* past it (`nextTimestamp`). A key is not always one writer's with one clock: a group's lobby, beacon and knock
|
|
1382
|
+
* records are written by every member, an inviter warms its contact's key with an empty packet, and this device's
|
|
1383
|
+
* own clock may have been set back since it last wrote. A relay and the DHT keep only the packet dated latest, so
|
|
1384
|
+
* one dated by this clock alone, when it is behind, was refused (409) or silently never read, until this clock
|
|
1385
|
+
* caught up with the other one: minutes, or an hour. Only up to `PKARR_FUTURE_SKEW_MS` ahead of this clock
|
|
1386
|
+
* (`withinSkew`): a packet one holder of a shared key dated further ahead must not date every later one of this
|
|
1387
|
+
* device past it (past 64 bits, unsignable; else ahead of every reader's clock, never taken over one dated now).
|
|
1388
|
+
*/
|
|
1389
|
+
seenTimestamp = /* @__PURE__ */ new Map();
|
|
1390
|
+
/**
|
|
1391
|
+
* The last packet this client put under a key that a relay took. A read the budget holds back answers the newer of
|
|
1392
|
+
* it and the last one read: a hub that wrote its own beacon entry since it last read the beacon must not see that
|
|
1393
|
+
* entry as old as the one it replaced (it would stop counting itself at its community's door until its next write).
|
|
1394
|
+
*/
|
|
1395
|
+
written = /* @__PURE__ */ new Map();
|
|
1396
|
+
coolingDown = /* @__PURE__ */ new Map();
|
|
1397
|
+
networkCooldown = /* @__PURE__ */ new Map();
|
|
1398
|
+
cursor = 0;
|
|
1399
|
+
/** Per key, the relay that answered its last read: the next read of the key starts at the one after it (`resolve`). */
|
|
1400
|
+
lastReadFrom = /* @__PURE__ */ new Map();
|
|
1401
|
+
spent = /* @__PURE__ */ new Map();
|
|
1402
|
+
spentBackground = /* @__PURE__ */ new Map();
|
|
1403
|
+
/** Background writes on each relay in the last minute (`BACKGROUND_WRITES_SHARE`). */
|
|
1404
|
+
spentBackgroundWrites = /* @__PURE__ */ new Map();
|
|
1405
|
+
/** Groups' urgent reads on each relay in the last `GROUP_BURST_MS`: edges' reads for an answer apart (`ANSWER_READ_FIRST_MS`). */
|
|
1406
|
+
spentGroupUrgent = /* @__PURE__ */ new Map();
|
|
1407
|
+
spentAnswerUrgent = /* @__PURE__ */ new Map();
|
|
1408
|
+
/** Watch reads on each relay in the last `GROUP_RATION_WINDOW_MS` (`WATCH_SHARE`). */
|
|
1409
|
+
spentWatch = /* @__PURE__ */ new Map();
|
|
1410
|
+
/** 1:1 chats' watch reads on each relay in the last `GROUP_RATION_WINDOW_MS` (`CHAT_WATCH_SHARE`). */
|
|
1411
|
+
spentChatWatch = /* @__PURE__ */ new Map();
|
|
1412
|
+
/** Groups' reads past the limit on each relay in the last `GROUP_OVER_PACE_MS` (`GROUP_SIGNALING_ALLOWANCE_SHARE`). */
|
|
1413
|
+
spentGroupOver = /* @__PURE__ */ new Map();
|
|
1414
|
+
/** Group edges' signaling requests on each relay in the last minute: their floor (`GROUP_SIGNALING_ALLOWANCE_SHARE`). */
|
|
1415
|
+
spentGroupSignal = /* @__PURE__ */ new Map();
|
|
1416
|
+
/** Community members' ways back on each relay in the last minute: their allowance (`WAY_BACK_SHARE`). */
|
|
1417
|
+
spentWayBack = /* @__PURE__ */ new Map();
|
|
1418
|
+
/** Edges' reads for an answer that only their own allowance let through, on each relay in the last minute (`ANSWER_READ_ALLOWANCE`). */
|
|
1419
|
+
spentAnswerAllowance = /* @__PURE__ */ new Map();
|
|
1420
|
+
/** Per contact, its departed reads past the limit in the last minute, every relay together (`DEPARTED_READS`). */
|
|
1421
|
+
departedOver = /* @__PURE__ */ new Map();
|
|
1422
|
+
/** The awaited reads past the limit in the last minute, every invite and every relay together (`AWAITED_READS`). */
|
|
1423
|
+
awaitedOver = [];
|
|
1424
|
+
/** When an awaited invite's read last asked: the departed reads leave it room for a minute (`AWAITED_READS`). */
|
|
1425
|
+
awaitedAt = -Infinity;
|
|
1426
|
+
/**
|
|
1427
|
+
* Per key a group reads (not in the background), when a read of it last went out and when it last asked: whose turn
|
|
1428
|
+
* it is under the watch share (`WATCH_SHARE`) and while the groups' reads are rationed (`GROUP_RATION_WINDOW_MS`). And
|
|
1429
|
+
* per key a chat watches, whose turn it is under theirs (`CHAT_WATCH_SHARE`).
|
|
1430
|
+
*/
|
|
1431
|
+
readTurns = /* @__PURE__ */ new Map();
|
|
1432
|
+
/** Per key read in turn (`isTurnRead`), when a read of it last went out and when it last asked (`READ_TURN_MS`). */
|
|
1433
|
+
keyTurns = /* @__PURE__ */ new Map();
|
|
1434
|
+
/** When `readTurns` and `keyTurns` last dropped their stale turns (`TURNS_PRUNE_MS`). */
|
|
1435
|
+
turnsPrunedAt = /* @__PURE__ */ new Map();
|
|
1436
|
+
/** Groups' reads (not background ones) on each relay in the last `GROUP_RATION_WINDOW_MS`. */
|
|
1437
|
+
spentGroupReads = /* @__PURE__ */ new Map();
|
|
1438
|
+
/** When a group's read last found each relay's minute full: its reads there are rationed for `GROUP_RATION_MS`. */
|
|
1439
|
+
groupsFullAt = /* @__PURE__ */ new Map();
|
|
1440
|
+
/** When a link's write was last refused on each relay (`chat <relay>`, `group <relay>`), while it waits for the budget. */
|
|
1441
|
+
writeWaiting = /* @__PURE__ */ new Map();
|
|
1442
|
+
/** Per relay, the keys whose edges' reads for an answer it last refused, and when, until one is read (`ANSWER_READ_FIRST_MS`). */
|
|
1443
|
+
answerReadWaiting = /* @__PURE__ */ new Map();
|
|
1444
|
+
/**
|
|
1445
|
+
* Per key a 1:1 chat asks for: when it last read urgently (for the next minute, groups leave it `CHAT_RESERVE` on
|
|
1446
|
+
* every relay) and when the budget last refused it on each relay (the same, on that relay). A read of the key at a
|
|
1447
|
+
* slow pace (the chat is live, or back in the background) drops it: `CHAT_RESERVE`. Per relay, the later of the two
|
|
1448
|
+
* for each key (`chatNeed`); `chatUrgentAt` keeps the urgent ones for a relay not asked yet.
|
|
1449
|
+
*/
|
|
1450
|
+
chatNeeds = /* @__PURE__ */ new Map();
|
|
1451
|
+
chatUrgentAt = new RecentKeys();
|
|
1452
|
+
/** When a link (a chat's or a group's) last asked urgently: background requests yield for `SIGNALING_WINDOW_MS`. */
|
|
1453
|
+
urgentAt = -Infinity;
|
|
1454
|
+
/** Timestamp of the last packet sent to each relay, per key: the compare-and-swap value for the next one. */
|
|
1455
|
+
lastPut = /* @__PURE__ */ new Map();
|
|
1456
|
+
/** Per relay and key, the newest packet that relay held back for its budget while another relay took it (`catchUpLater`). */
|
|
1457
|
+
catchUp = /* @__PURE__ */ new Map();
|
|
1458
|
+
/** Per relay, how many packets wait in `catchUp` for it: background reads leave them their share (`heldFor`). */
|
|
1459
|
+
catchUpWaiting = /* @__PURE__ */ new Map();
|
|
1460
|
+
catchUpTimer = null;
|
|
1461
|
+
/**
|
|
1462
|
+
* Per key whose reads the budget held back since its last read went out: when a request frees for it
|
|
1463
|
+
* (`readHeldBackFor`), how many times in a row, and since when (`READ_STARVED_MS`).
|
|
1464
|
+
*/
|
|
1465
|
+
heldBack = /* @__PURE__ */ new Map();
|
|
1466
|
+
/** When the relays last answered a read of each key, for `FRESH_READ_MS`. */
|
|
1467
|
+
readAt = /* @__PURE__ */ new Map();
|
|
1468
|
+
freshReadMs;
|
|
1469
|
+
perMinute;
|
|
1470
|
+
/** `requestsPerMinute` was given: it bounds every relay, one with its own share (`RELAY_REQUESTS_PER_MINUTE`) too. */
|
|
1471
|
+
perMinuteGiven;
|
|
1472
|
+
backgroundPerMinute;
|
|
1473
|
+
backgroundWhileSignaling;
|
|
1474
|
+
breaker;
|
|
1475
|
+
/** The relay the last read was answered by. */
|
|
1476
|
+
lastRelay = null;
|
|
1477
|
+
hedgeMs;
|
|
1478
|
+
/** Until when each relay found slow (`SLOW_MS`) goes after the others in a read (`readOrder`). */
|
|
1479
|
+
slowUntil = /* @__PURE__ */ new Map();
|
|
1480
|
+
/** Per relay, when each of this client's requests still waiting for its answer went out (`unanswering`). */
|
|
1481
|
+
waiting = /* @__PURE__ */ new Map();
|
|
1482
|
+
/** When each relay last answered a request of this client, whatever it said (`unanswering`). */
|
|
1483
|
+
answeredAt = /* @__PURE__ */ new Map();
|
|
1484
|
+
/** Keys whose last read was answered 404: their next read hedges later (`HEDGE_MISSING_MS`). */
|
|
1485
|
+
missing = /* @__PURE__ */ new Set();
|
|
1486
|
+
listeners = /* @__PURE__ */ new Set();
|
|
1487
|
+
constructor(options = {}) {
|
|
1488
|
+
this.relays = [];
|
|
1489
|
+
this.setRelays(options.relays ?? DEFAULT_RELAYS);
|
|
1490
|
+
this.timeoutMs = options.timeoutMs ?? 1e4;
|
|
1491
|
+
this.fetchFn = options.fetch ?? ((...args) => fetch(...args));
|
|
1492
|
+
this.perMinute = options.requestsPerMinute ?? 30;
|
|
1493
|
+
this.freshReadMs = options.freshReadMs ?? 500;
|
|
1494
|
+
this.hedgeMs = options.hedgeMs ?? 1500;
|
|
1495
|
+
this.perMinuteGiven = options.requestsPerMinute !== void 0;
|
|
1496
|
+
this.backgroundPerMinute = this.perMinute === 30 ? 20 : Math.ceil(this.perMinute * 2 / 3);
|
|
1497
|
+
this.backgroundWhileSignaling = this.perMinute === 30 ? 5 : Math.ceil(this.perMinute / 6);
|
|
1498
|
+
const log = options.log ?? ((line) => console.info(`ghostly: ${line}`));
|
|
1499
|
+
this.breaker = new RelayBreaker({
|
|
1500
|
+
...options.breaker,
|
|
1501
|
+
onTrip: (relay, reason, forMs) => {
|
|
1502
|
+
log(`relay ${new URL(relay).host} ${reason === "no answer" ? "did not answer" : reason === "slow" ? "answered too slowly" : `failed (${reason})`}; asked again in ${Math.round(forMs / 1e3)} s`);
|
|
1503
|
+
options.breaker?.onTrip?.(relay, reason, forMs);
|
|
1504
|
+
this.changed("tripped");
|
|
1505
|
+
},
|
|
1506
|
+
onRecover: (relay) => {
|
|
1507
|
+
log(`relay ${new URL(relay).host} answers again`);
|
|
1508
|
+
options.breaker?.onRecover?.(relay);
|
|
1509
|
+
this.changed("recovered");
|
|
1510
|
+
}
|
|
1511
|
+
});
|
|
1512
|
+
}
|
|
1513
|
+
/** How reads go and how each relay is doing, for the connection panel. */
|
|
1514
|
+
discovery() {
|
|
1515
|
+
const relays = this.breaker.health(this.relays).map((health) => {
|
|
1516
|
+
const limited = this.rateLimitedFor(health.relay);
|
|
1517
|
+
return health.state === "ok" && limited > 0 ? {
|
|
1518
|
+
...health,
|
|
1519
|
+
state: "throttled",
|
|
1520
|
+
until: Date.now() + limited,
|
|
1521
|
+
reason: "rate limited (429)"
|
|
1522
|
+
} : health;
|
|
1523
|
+
});
|
|
1524
|
+
return {
|
|
1525
|
+
path: this.lastRelay ? {
|
|
1526
|
+
via: "relay",
|
|
1527
|
+
relay: this.lastRelay
|
|
1528
|
+
} : null,
|
|
1529
|
+
relays
|
|
1530
|
+
};
|
|
1531
|
+
}
|
|
1532
|
+
/** Back online on some network: relays that failed (or rate limited this address) on the old one are asked again. */
|
|
1533
|
+
networkChanged() {
|
|
1534
|
+
this.breaker.reset();
|
|
1535
|
+
this.networkCooldown.clear();
|
|
1536
|
+
this.coolingDown.clear();
|
|
1537
|
+
this.slowUntil.clear();
|
|
1538
|
+
this.changed("recovered");
|
|
1539
|
+
}
|
|
1540
|
+
/**
|
|
1541
|
+
* `PkarrTransport.forget`: the packets and dates this client kept under these keys go, on every relay (`written`,
|
|
1542
|
+
* `newest`, `lastTimestamp`, `seenTimestamp`, `lastPut`). A spare invite replaced unused left them here for its two keys
|
|
1543
|
+
* for good: an idle CLI daemon gained 8 keys an hour (343 in `written` and 690 `lastPut` entries after a day). The maps
|
|
1544
|
+
* that bound themselves (`readAt`, `lastReadFrom`, `heldBack`, `missing`, the turns) and the catch-ups, which go once
|
|
1545
|
+
* put, are left as they are. A later put under one of these keys goes as a key's first.
|
|
1546
|
+
*/
|
|
1547
|
+
forget(keys) {
|
|
1548
|
+
const gone = new Set(keys);
|
|
1549
|
+
if (gone.size === 0) return;
|
|
1550
|
+
for (const key of gone) for (const perKey of [
|
|
1551
|
+
this.lastTimestamp,
|
|
1552
|
+
this.seenTimestamp,
|
|
1553
|
+
this.newest,
|
|
1554
|
+
this.written
|
|
1555
|
+
]) perKey.delete(key);
|
|
1556
|
+
for (const slot of this.lastPut.keys()) if (gone.has(slot.slice(slot.lastIndexOf(" ") + 1))) this.lastPut.delete(slot);
|
|
1557
|
+
}
|
|
1558
|
+
/** Called when a relay trips or recovers. */
|
|
1559
|
+
subscribe(listener) {
|
|
1560
|
+
this.listeners.add(listener);
|
|
1561
|
+
return () => this.listeners.delete(listener);
|
|
1562
|
+
}
|
|
1563
|
+
changed(change) {
|
|
1564
|
+
for (const listener of this.listeners) try {
|
|
1565
|
+
listener(change);
|
|
1566
|
+
} catch {}
|
|
1567
|
+
}
|
|
1568
|
+
/** What the relay did, for its breaker. */
|
|
1569
|
+
answered(relay, failure, method) {
|
|
1570
|
+
if (failure) {
|
|
1571
|
+
this.breaker.failure(relay, failure.kind, failure.reason);
|
|
1572
|
+
return;
|
|
1573
|
+
}
|
|
1574
|
+
if (method) this.networkCooldown.delete(`${method} ${relay}`);
|
|
1575
|
+
this.breaker.success(relay, this.relays);
|
|
1576
|
+
}
|
|
1577
|
+
setRelays(relays) {
|
|
1578
|
+
const normalized = relays.map(normalizeRelayUrl).filter((r) => r !== null);
|
|
1579
|
+
this.relays = [...new Set(normalized)];
|
|
1580
|
+
}
|
|
1581
|
+
describe() {
|
|
1582
|
+
return {
|
|
1583
|
+
protocol: "Pkarr relays (HTTP) → Mainline DHT (BEP44)",
|
|
1584
|
+
relays: [...this.relays]
|
|
1585
|
+
};
|
|
1586
|
+
}
|
|
1587
|
+
/**
|
|
1588
|
+
* Publishes to every relay. Peers may be configured with different relay
|
|
1589
|
+
* sets; writing everywhere keeps the overlap warm, the DHT covers the rest.
|
|
1590
|
+
* Returns once one relay took the packet: the others finish in the background.
|
|
1591
|
+
*/
|
|
1592
|
+
async publish(identity, records, options = {}) {
|
|
1593
|
+
if (this.relays.length === 0) throw new PublishFailedError("No Pkarr relays configured", false);
|
|
1594
|
+
await this.publishDated(identity, records, (key, payload, timestamp, conflict) => this.putEverywhere(key, payload, timestamp, options, conflict), options);
|
|
1595
|
+
}
|
|
1596
|
+
/**
|
|
1597
|
+
* The time the next packet under a key is dated: this clock's, and past every packet this client put or read there.
|
|
1598
|
+
* BEP44 sequence numbers must strictly increase, and the clock that dated the last one may not be this one's.
|
|
1599
|
+
*/
|
|
1600
|
+
nextTimestamp(pubKeyZ32) {
|
|
1601
|
+
const now = BigInt(Date.now()) * 1000n;
|
|
1602
|
+
const last = this.lastTimestamp.get(pubKeyZ32) ?? 0n, seen = this.seenTimestamp.get(pubKeyZ32) ?? 0n;
|
|
1603
|
+
const floor = last > seen ? last : seen;
|
|
1604
|
+
const timestamp = now > floor ? now : floor + 1n;
|
|
1605
|
+
this.lastTimestamp.set(pubKeyZ32, timestamp);
|
|
1606
|
+
return timestamp;
|
|
1607
|
+
}
|
|
1608
|
+
/**
|
|
1609
|
+
* Signs the records, dated by `nextTimestamp`, and hands the payload to `put` (the relays; the CLI adds the DHT).
|
|
1610
|
+
* When every relay's budget (or rate limit) holds this asker back, it throws that wait before signing anything
|
|
1611
|
+
* (`publishWait`): a daemon at its budget signed about 500 packets a minute that never went out. `elsewhere`: `put`
|
|
1612
|
+
* also takes the packet where no budget holds it back (the CLI's DHT), so it is signed whatever the relays say.
|
|
1613
|
+
* `put` calls `conflict` for a relay that answered 409: it holds a packet under this key dated at or after this one,
|
|
1614
|
+
* which this client has not read (an inviter's warm packet under the key it gave its contact, from a clock ahead of
|
|
1615
|
+
* this one). That packet is read from that relay, and the records go again dated just past it, once. Without this
|
|
1616
|
+
* the packet was refused until this device's clock passed the other one's, however long that took, and on a relay
|
|
1617
|
+
* that did take it, readers still preferred the later-dated empty packet of another relay. `read`: how to learn the
|
|
1618
|
+
* held packet's date where it is not a relay to ask (the CLI's DHT, whose nodes answer 302 for a later-dated packet).
|
|
1619
|
+
*/
|
|
1620
|
+
async publishDated(identity, records, put, options = {}, again = false, elsewhere = false) {
|
|
1621
|
+
const key = identity.pubKeyZ32;
|
|
1622
|
+
if (!again && !elsewhere) {
|
|
1623
|
+
const held = this.publishWait(key, asker(options, true));
|
|
1624
|
+
if (held) throw new DiscoveryBudgetError(held.wait, `Publish held back on every relay: ${held.reasons.join("; ")}`);
|
|
1625
|
+
const refused = this.refusedEverywhere(key, asker(options, true));
|
|
1626
|
+
if (refused) throw refused;
|
|
1627
|
+
}
|
|
1628
|
+
const timestamp = this.nextTimestamp(key);
|
|
1629
|
+
let redated;
|
|
1630
|
+
const conflict = (relay, read) => {
|
|
1631
|
+
if (again || redated) return;
|
|
1632
|
+
redated = (async () => {
|
|
1633
|
+
const held = read ? await read().catch(() => null) : await this.heldAt(relay, key, asker(options, true));
|
|
1634
|
+
if (held === null || held < timestamp || !withinSkew(held)) return false;
|
|
1635
|
+
traceLink(key, "publish-redated", {
|
|
1636
|
+
aheadMs: Number((held - timestamp) / 1000n),
|
|
1637
|
+
relay
|
|
1638
|
+
});
|
|
1639
|
+
if (this.lastTimestamp.get(key) !== timestamp) return true;
|
|
1640
|
+
await this.publishDated(identity, records, put, options, true, elsewhere);
|
|
1641
|
+
return true;
|
|
1642
|
+
})();
|
|
1643
|
+
redated.catch(() => {});
|
|
1644
|
+
};
|
|
1645
|
+
try {
|
|
1646
|
+
await put(key, createRelayPayload(identity, records, timestamp), timestamp, conflict);
|
|
1647
|
+
} catch (error) {
|
|
1648
|
+
const pending = redated;
|
|
1649
|
+
if (!pending || !await pending.catch((later) => {
|
|
1650
|
+
throw laterRound(error, later);
|
|
1651
|
+
})) throw error;
|
|
1652
|
+
}
|
|
1653
|
+
}
|
|
1654
|
+
/** `PkarrTransport.publishHeld`: `publishDated`'s check before signing, for a payload signed elsewhere in this process. */
|
|
1655
|
+
publishHeld(pubKeyZ32, options = {}) {
|
|
1656
|
+
if (this.relays.length === 0) return null;
|
|
1657
|
+
const held = this.publishWait(pubKeyZ32, asker(options, true));
|
|
1658
|
+
return held ? new DiscoveryBudgetError(held.wait, `Publish held back on every relay: ${held.reasons.join("; ")}`) : null;
|
|
1659
|
+
}
|
|
1660
|
+
/**
|
|
1661
|
+
* How long until some relay takes a publish of this asker, and why each holds it back; null when one would now. Walks the relays as `put` does,
|
|
1662
|
+
* without asking any: a relay the budget holds back gets its refused `take` (the asker's write then waits first in
|
|
1663
|
+
* line, as when `put` is refused), one rate limiting or throttling this address is a wait, one left alone for failing
|
|
1664
|
+
* is none. Null too when a relay fails another way (its network cooling down): the publish then goes, and fails, as
|
|
1665
|
+
* before, and when no relay is held back at all.
|
|
1666
|
+
*/
|
|
1667
|
+
publishWait(pubKeyZ32, who) {
|
|
1668
|
+
const probe = this.breaker.allDownProbe(this.relays);
|
|
1669
|
+
let wait = Infinity;
|
|
1670
|
+
const held = [], reasons = [];
|
|
1671
|
+
for (const relay of this.relays) {
|
|
1672
|
+
if (relay !== probe && this.networkCoolingDown(relay, "PUT")) return null;
|
|
1673
|
+
const limited = this.rateLimitedFor(relay);
|
|
1674
|
+
if (limited > 0) {
|
|
1675
|
+
wait = Math.min(wait, limited);
|
|
1676
|
+
reasons.push(`${relay}: Discovery relay is cooling down after a 429; retry shortly`);
|
|
1677
|
+
continue;
|
|
1678
|
+
}
|
|
1679
|
+
const blocked = relay === probe ? 0 : this.breaker.blockedFor(relay);
|
|
1680
|
+
if (blocked > 0) {
|
|
1681
|
+
if (this.breaker.blockedKind(relay) === "throttled") {
|
|
1682
|
+
wait = Math.min(wait, blocked);
|
|
1683
|
+
reasons.push(`${relay}: Discovery relay is throttling this address; retry shortly`);
|
|
1684
|
+
} else reasons.push(`${relay} is left alone after failing`);
|
|
1685
|
+
continue;
|
|
1686
|
+
}
|
|
1687
|
+
if (this.heldFor(relay, who, Date.now(), pubKeyZ32) <= 0) return null;
|
|
1688
|
+
held.push(relay);
|
|
1689
|
+
const left = this.heldFor(relay, who);
|
|
1690
|
+
wait = Math.min(wait, left);
|
|
1691
|
+
reasons.push(`${relay}: discovery budget reached; retry in ${Math.ceil(left / 1e3)} s`);
|
|
1692
|
+
}
|
|
1693
|
+
if (wait === Infinity) return null;
|
|
1694
|
+
for (const relay of held) this.take(relay, who, pubKeyZ32);
|
|
1695
|
+
return {
|
|
1696
|
+
wait,
|
|
1697
|
+
reasons
|
|
1698
|
+
};
|
|
1699
|
+
}
|
|
1700
|
+
/**
|
|
1701
|
+
* A publish that every relay would refuse now without being asked (its network cooling down, left alone after failing,
|
|
1702
|
+
* its rate limit or this asker's budget), and not as a wait alone (`publishWait` says those): the failure `putEverywhere`
|
|
1703
|
+
* would end in, in the same words, said before the packet is signed and dated. Null when some relay would be asked. With
|
|
1704
|
+
* every relay out of reach on a page that is online, each chat signed a packet every few seconds that went nowhere: ~58
|
|
1705
|
+
* a second at 300 chats (bug hunt, 2026-11-12).
|
|
1706
|
+
*/
|
|
1707
|
+
refusedEverywhere(pubKeyZ32, who) {
|
|
1708
|
+
const probe = this.breaker.allDownProbe(this.relays);
|
|
1709
|
+
const now = Date.now();
|
|
1710
|
+
const reasons = [], held = [];
|
|
1711
|
+
for (const relay of this.relays) {
|
|
1712
|
+
const refused = this.refusal(relay, relay === probe);
|
|
1713
|
+
if (refused) {
|
|
1714
|
+
reasons.push(String(refused));
|
|
1715
|
+
continue;
|
|
1716
|
+
}
|
|
1717
|
+
if (this.heldFor(relay, who, now, pubKeyZ32) <= 0) return null;
|
|
1718
|
+
held.push(relay);
|
|
1719
|
+
reasons.push(String(new DiscoveryBudgetError(this.heldFor(relay, who))));
|
|
1720
|
+
}
|
|
1721
|
+
for (const relay of held) this.take(relay, who, pubKeyZ32);
|
|
1722
|
+
return new PublishFailedError(`Publish failed on every relay: ${reasons.join("; ")}`, false);
|
|
1723
|
+
}
|
|
1724
|
+
/** What `put` refuses a request to this relay with before its budget is asked; null when it gets that far. */
|
|
1725
|
+
refusal(relay, probe) {
|
|
1726
|
+
if (!probe && this.networkCoolingDown(relay, "PUT")) return new RelayNoAnswerError(`${relay} did not answer a moment ago; retry shortly`, false);
|
|
1727
|
+
const limited = this.rateLimitedFor(relay);
|
|
1728
|
+
if (limited > 0) return new DiscoveryBudgetError(limited, "Discovery relay is cooling down after a 429; retry shortly");
|
|
1729
|
+
const blocked = probe ? 0 : this.breaker.blockedFor(relay);
|
|
1730
|
+
if (blocked > 0) {
|
|
1731
|
+
if (this.breaker.blockedKind(relay) === "throttled") return new DiscoveryBudgetError(blocked, "Discovery relay is throttling this address; retry shortly");
|
|
1732
|
+
return new RelayLeftAloneError(`${relay} is left alone after failing; asked again in ${Math.max(1, Math.ceil(this.breaker.askedAgainIn(relay, this.relays) / 1e3))} s`);
|
|
1733
|
+
}
|
|
1734
|
+
return null;
|
|
1735
|
+
}
|
|
1736
|
+
/** One relay's packet for a key, read now: its time, kept for `nextTimestamp`. Null when it has none or does not answer. */
|
|
1737
|
+
async heldAt(relay, pubKeyZ32, who) {
|
|
1738
|
+
if (!this.take(relay, who, pubKeyZ32)) return null;
|
|
1739
|
+
try {
|
|
1740
|
+
let payload;
|
|
1741
|
+
if (!(await this.request(`${relay}/${pubKeyZ32}`, { method: "GET" }, async (r, signal) => {
|
|
1742
|
+
if (r.ok && r.status !== 404) payload = await readRelayBody(r, signal);
|
|
1743
|
+
})).ok || !payload) return null;
|
|
1744
|
+
const packet = parseRelayPayload(pubKeyZ32, payload);
|
|
1745
|
+
this.sawTimestamp(pubKeyZ32, packet.timestampMicros);
|
|
1746
|
+
return packet.timestampMicros;
|
|
1747
|
+
} catch {
|
|
1748
|
+
return null;
|
|
1749
|
+
}
|
|
1750
|
+
}
|
|
1751
|
+
/** A packet under this key was read (on a relay, or by the CLI on the DHT): its time, for `nextTimestamp`. */
|
|
1752
|
+
sawTimestamp(pubKeyZ32, timestamp) {
|
|
1753
|
+
if (withinSkew(timestamp) && timestamp > (this.seenTimestamp.get(pubKeyZ32) ?? 0n)) this.seenTimestamp.set(pubKeyZ32, timestamp);
|
|
1754
|
+
}
|
|
1755
|
+
/**
|
|
1756
|
+
* Puts a payload `publishDated` just signed, dated `timestamp`, on every relay. Its signature is not checked: it was
|
|
1757
|
+
* made here (`publishPayload` checks one signed elsewhere, to read its sequence number). `conflict`: as `publishDated`
|
|
1758
|
+
* gives it.
|
|
1759
|
+
*/
|
|
1760
|
+
async putSigned(pubKeyZ32, payload, timestamp, options = {}, conflict) {
|
|
1761
|
+
if (this.relays.length === 0) throw new PublishFailedError("No Pkarr relays configured", false);
|
|
1762
|
+
await this.putEverywhere(pubKeyZ32, payload, timestamp, options, conflict);
|
|
1763
|
+
}
|
|
1764
|
+
/**
|
|
1765
|
+
* Puts a payload signed elsewhere (a did:dht document), byte for byte, on every relay; one signed here says its date
|
|
1766
|
+
* (`signedAt`) and is not checked again. `conflict`: as `publishDated` gives it.
|
|
1767
|
+
*/
|
|
1768
|
+
async publishPayload(pubKeyZ32, payload, options = {}, conflict) {
|
|
1769
|
+
if (this.relays.length === 0) throw new PublishFailedError("No Pkarr relays configured", false);
|
|
1770
|
+
const seq = options.signedAt ?? openRelayPayload(pubKeyZ32, payload).seq;
|
|
1771
|
+
if (options.signedAt === void 0 || !options.redate) return this.putEverywhere(pubKeyZ32, payload, seq, options, conflict);
|
|
1772
|
+
await this.putRedated(pubKeyZ32, payload, seq, options, PAYLOAD_REDATES);
|
|
1773
|
+
}
|
|
1774
|
+
/**
|
|
1775
|
+
* A payload every relay's budget held back while another path took it (the CLI's DHT): the publish is out, and each
|
|
1776
|
+
* relay gets it once its budget frees a request for the same asker (`catchUpLater`), unless a newer packet of the key
|
|
1777
|
+
* goes there first. Retried by the link instead, each try was signed and put on the DHT again (1-1.6 puts a minute per
|
|
1778
|
+
* chat at budget, not one per 4 min). `timestamp`: its date when signed here; read from the payload otherwise.
|
|
1779
|
+
*/
|
|
1780
|
+
putLater(pubKeyZ32, payload, timestamp, options = {}) {
|
|
1781
|
+
const at = timestamp ?? options.signedAt ?? openRelayPayload(pubKeyZ32, payload).seq;
|
|
1782
|
+
try {
|
|
1783
|
+
this.written.set(pubKeyZ32, readOwnRelayPayload(pubKeyZ32, payload));
|
|
1784
|
+
} catch {}
|
|
1785
|
+
for (const relay of this.relays) this.catchUpLater(relay, pubKeyZ32, payload, at, asker(options, true));
|
|
1786
|
+
}
|
|
1787
|
+
/**
|
|
1788
|
+
* A payload signed in this process (a group edge's offer or answer, `CarriedTransport`) put on every relay, past what
|
|
1789
|
+
* a relay that answers 409 holds: that one is read, and the records go again at once, signed by `options.redate` dated
|
|
1790
|
+
* just past it, `left` times at most. A packet put here is the key's latest too: a publish dated here next
|
|
1791
|
+
* (`nextTimestamp`, the goodbye after the edge stops) goes past it. Before, the 409 failed the publish (or, when another
|
|
1792
|
+
* relay took it, left the relay's copy in place), the link tried again 4 s on with its clock's date, refused as long as
|
|
1793
|
+
* the relay's copy was later, and an edge started again in the same millisecond lost its new offer to its last one.
|
|
1794
|
+
*/
|
|
1795
|
+
async putRedated(key, payload, timestamp, options, left) {
|
|
1796
|
+
if (timestamp > (this.lastTimestamp.get(key) ?? 0n)) this.lastTimestamp.set(key, timestamp);
|
|
1797
|
+
let redated;
|
|
1798
|
+
const conflict = (relay) => {
|
|
1799
|
+
if (left <= 0 || redated) return;
|
|
1800
|
+
redated = (async () => {
|
|
1801
|
+
const held = await this.heldAt(relay, key, asker(options, true));
|
|
1802
|
+
if (held === null || held < timestamp || !withinSkew(held)) return false;
|
|
1803
|
+
if (this.lastTimestamp.get(key) !== timestamp) return true;
|
|
1804
|
+
const again = options.redate(held);
|
|
1805
|
+
if (!again) return true;
|
|
1806
|
+
traceLink(key, "publish-redated", {
|
|
1807
|
+
aheadMs: Number((held - timestamp) / 1000n),
|
|
1808
|
+
relay
|
|
1809
|
+
});
|
|
1810
|
+
await this.putRedated(key, again.payload, again.signedAt, {
|
|
1811
|
+
...options,
|
|
1812
|
+
signedAt: again.signedAt
|
|
1813
|
+
}, left - 1);
|
|
1814
|
+
return true;
|
|
1815
|
+
})();
|
|
1816
|
+
redated.catch(() => {});
|
|
1817
|
+
};
|
|
1818
|
+
try {
|
|
1819
|
+
await this.putEverywhere(key, payload, timestamp, options, conflict);
|
|
1820
|
+
} catch (error) {
|
|
1821
|
+
const pending = redated;
|
|
1822
|
+
if (!pending || !await pending.catch((later) => {
|
|
1823
|
+
throw laterRound(error, later);
|
|
1824
|
+
})) throw error;
|
|
1825
|
+
}
|
|
1826
|
+
}
|
|
1827
|
+
/**
|
|
1828
|
+
* Puts the packet on every relay and settles as soon as one of them took it. A relay that is slow to
|
|
1829
|
+
* answer (one that stores the packet and answers the PUT only at the timeout) must not hold a link's
|
|
1830
|
+
* presence, offer or answer back: the rest finish in the background, their outcomes still counted.
|
|
1831
|
+
* When no relay takes it, this waits for all of them to say why.
|
|
1832
|
+
*/
|
|
1833
|
+
putEverywhere(pubKeyZ32, payload, timestamp, options, conflict) {
|
|
1834
|
+
this.readAt.delete(pubKeyZ32);
|
|
1835
|
+
this.missing.delete(pubKeyZ32);
|
|
1836
|
+
const waitingBefore = new Map(this.writeWaiting);
|
|
1837
|
+
const writer = firstWriter(asker(options, true));
|
|
1838
|
+
const noWriteWaits = () => {
|
|
1839
|
+
if (!writer) return;
|
|
1840
|
+
for (const relay of this.relays) {
|
|
1841
|
+
const key = `${writer} ${relay}`;
|
|
1842
|
+
if (this.writeWaiting.get(key) !== waitingBefore.get(key)) this.writeWaiting.delete(key);
|
|
1843
|
+
}
|
|
1844
|
+
};
|
|
1845
|
+
const probe = this.breaker.allDownProbe(this.relays);
|
|
1846
|
+
let first;
|
|
1847
|
+
const edgeSignal = isGroupSignal(asker(options, true));
|
|
1848
|
+
const failed = /* @__PURE__ */ new Set();
|
|
1849
|
+
const nowhere = this.relays.map(() => false);
|
|
1850
|
+
const puts = this.relays.map(async (relay, i) => {
|
|
1851
|
+
const slot = `${relay} ${pubKeyZ32}`;
|
|
1852
|
+
const previous = this.lastPut.get(slot);
|
|
1853
|
+
this.lastPut.set(slot, timestamp);
|
|
1854
|
+
if (first && isChat(asker(options, true)) && this.spareHeldFor(relay, Date.now()) > 0 && await answeredWithin(first, 3e3)) {
|
|
1855
|
+
nowhere[i] = true;
|
|
1856
|
+
throw new DiscoveryBudgetError(Math.max(CATCH_UP_MIN_MS, this.spareHeldFor(relay, Date.now())));
|
|
1857
|
+
}
|
|
1858
|
+
let response;
|
|
1859
|
+
try {
|
|
1860
|
+
response = await this.put(relay, pubKeyZ32, payload, previous, asker(options, true), relay === probe, (ok) => {
|
|
1861
|
+
first ??= ok;
|
|
1862
|
+
});
|
|
1863
|
+
if (response.status === 412) response = await this.put(relay, pubKeyZ32, payload, void 0, asker(options, true));
|
|
1864
|
+
} catch (error) {
|
|
1865
|
+
if (!isDiscoveryBudgetError(error) && !(error instanceof RelayLeftAloneError)) failed.add(relay);
|
|
1866
|
+
nowhere[i] = isDiscoveryBudgetError(error) || error instanceof RelayLeftAloneError || error instanceof RelayNoAnswerError && !error.sent;
|
|
1867
|
+
if (this.catchUp.has(slot) && isDiscoveryBudgetError(error)) this.catchUpLater(relay, pubKeyZ32, payload, timestamp, asker(options, true));
|
|
1868
|
+
throw error;
|
|
1869
|
+
}
|
|
1870
|
+
if (response.ok && (this.catchUp.get(slot)?.timestamp ?? 0n) <= timestamp) this.dropCatchUp(relay, pubKeyZ32);
|
|
1871
|
+
nowhere[i] = response.status >= 400 && response.status < 500;
|
|
1872
|
+
if (response.status >= 500 || response.status === 428) failed.add(relay);
|
|
1873
|
+
if (response.status === 409) conflict?.(relay);
|
|
1874
|
+
if (response.status === 429) throw new DiscoveryBudgetError(this.coolDown(relay, response), `${relay} responded 429; retry shortly`);
|
|
1875
|
+
if (!response.ok) throw new Error(`${relay} responded ${response.status}`);
|
|
1876
|
+
});
|
|
1877
|
+
const settled = Promise.allSettled(puts);
|
|
1878
|
+
return new Promise((resolve, reject) => {
|
|
1879
|
+
let accepted = false;
|
|
1880
|
+
for (const put of puts) put.then(() => {
|
|
1881
|
+
if (accepted) return;
|
|
1882
|
+
accepted = true;
|
|
1883
|
+
noWriteWaits();
|
|
1884
|
+
try {
|
|
1885
|
+
this.written.set(pubKeyZ32, readOwnRelayPayload(pubKeyZ32, payload));
|
|
1886
|
+
} catch {}
|
|
1887
|
+
resolve();
|
|
1888
|
+
}, () => {});
|
|
1889
|
+
settled.then((results) => {
|
|
1890
|
+
if (accepted) {
|
|
1891
|
+
noWriteWaits();
|
|
1892
|
+
results.forEach((r, i) => {
|
|
1893
|
+
if (r.status !== "rejected") return;
|
|
1894
|
+
if (isDiscoveryBudgetError(r.reason) || edgeSignal && failed.has(this.relays[i])) this.catchUpLater(this.relays[i], pubKeyZ32, payload, timestamp, edgeSignal ? asker(options, true) : CATCH_UP_ASKER);
|
|
1895
|
+
});
|
|
1896
|
+
return;
|
|
1897
|
+
}
|
|
1898
|
+
const held = results.map((r) => r.status === "rejected" && isDiscoveryBudgetError(r.reason) ? r.reason : null);
|
|
1899
|
+
const leftAlone = (r) => r.status === "rejected" && r.reason instanceof RelayLeftAloneError;
|
|
1900
|
+
const waits = held.filter((e) => e !== null);
|
|
1901
|
+
const reasons = results.map((r) => r.status === "rejected" ? String(r.reason) : "").join("; ");
|
|
1902
|
+
if (waits.length === results.length) reject(new DiscoveryBudgetError(Math.min(...waits.map((e) => e.retryInMs)), `Publish held back on every relay: ${reasons}`));
|
|
1903
|
+
else if (waits.length && results.every((r, i) => held[i] !== null || leftAlone(r))) reject(new DiscoveryBudgetError(Math.min(...waits.map((e) => e.retryInMs)), `Publish held back on every relay not left alone: ${reasons}`));
|
|
1904
|
+
else reject(new PublishFailedError(`Publish failed on every relay: ${reasons}`, !nowhere.every(Boolean)));
|
|
1905
|
+
});
|
|
1906
|
+
});
|
|
1907
|
+
}
|
|
1908
|
+
/**
|
|
1909
|
+
* Public relays rate limit by IP (120 requests a minute at the time of
|
|
1910
|
+
* writing) and several peers may share one address, so a poll costs one
|
|
1911
|
+
* request: one relay is asked, the next one when this one fails, or when it
|
|
1912
|
+
* has not answered within `HEDGE_MS` (a hedge: the first good answer wins and
|
|
1913
|
+
* the other request is dropped). The newest validly signed packet seen so far
|
|
1914
|
+
* wins, which also covers a relay that is still serving an older cached copy.
|
|
1915
|
+
*
|
|
1916
|
+
* In turn per key: a key's next read starts at the relay after the one that answered its last read. A relay that
|
|
1917
|
+
* does not have a packet yet (a publish that reached only the other relay, one rate limited there) answers a key's
|
|
1918
|
+
* reads at most once in a row. With one turn for all keys, a link that read its peer between two other reads landed
|
|
1919
|
+
* on the same relay every time: a restarted member's offer, only on `pkarr.pubky.app`, went unread for 12 to 60 s
|
|
1920
|
+
* while the member at the other end read `pkarr.pubky.org` (bug hunt r5a, 2026-09-29). A relay found slow lately
|
|
1921
|
+
* (`SLOW_DEMOTE_MS`) goes after the others, whatever the turn.
|
|
1922
|
+
*
|
|
1923
|
+
* A hedge spends a second request, so it goes only where the budget has requests to spare (`hedgeRoom`), never for a
|
|
1924
|
+
* background read (nobody waits on it), and later for a key whose last read found nothing (`HEDGE_MISSING_MS`).
|
|
1925
|
+
*
|
|
1926
|
+
* `keptOnFailure` false: a read the relays failed throws `RelaysFailedError` with the packet they gave last, in place
|
|
1927
|
+
* of handing that back as their answer (a CLI asks the DHT then, `RelaysAndDht`).
|
|
1928
|
+
*/
|
|
1929
|
+
async resolve(pubKeyZ32, options = {}, keptOnFailure = true) {
|
|
1930
|
+
if (this.relays.length === 0) throw new Error("No Pkarr relays configured");
|
|
1931
|
+
if (options.background && !options.urgent && !options.group) this.chatNeedLapsed(pubKeyZ32);
|
|
1932
|
+
const readAt = this.readAt.get(pubKeyZ32), age = readAt === void 0 ? Infinity : Date.now() - readAt;
|
|
1933
|
+
if (age >= 0 && age < this.freshReadMs) return this.newest.get(pubKeyZ32) ?? null;
|
|
1934
|
+
const order = this.readOrder(pubKeyZ32);
|
|
1935
|
+
const probe = this.breaker.allDownProbe(this.relays);
|
|
1936
|
+
let budgetWait = Infinity, down = false;
|
|
1937
|
+
const who = asker(options, false);
|
|
1938
|
+
const tried = /* @__PURE__ */ new Set();
|
|
1939
|
+
let hedgeAfter = options.background ? Infinity : this.missing.has(pubKeyZ32) ? Math.max(this.hedgeMs, HEDGE_MISSING_MS) : this.hedgeMs;
|
|
1940
|
+
/**
|
|
1941
|
+
* The next relay to ask, its request taken from its budget, or null. A hedge (`hedge`) goes only to a relay with
|
|
1942
|
+
* requests to spare; one passed over for that is still there if the relay asked fails.
|
|
1943
|
+
*/
|
|
1944
|
+
const next = (hedge) => {
|
|
1945
|
+
for (const relay of order) {
|
|
1946
|
+
if (tried.has(relay)) continue;
|
|
1947
|
+
if (relay !== probe && this.networkCoolingDown(relay, "GET")) {
|
|
1948
|
+
tried.add(relay);
|
|
1949
|
+
down = true;
|
|
1950
|
+
continue;
|
|
1951
|
+
}
|
|
1952
|
+
const limited = this.rateLimitedFor(relay);
|
|
1953
|
+
if (limited > 0) {
|
|
1954
|
+
tried.add(relay);
|
|
1955
|
+
budgetWait = Math.min(budgetWait, limited);
|
|
1956
|
+
continue;
|
|
1957
|
+
}
|
|
1958
|
+
const blocked = relay === probe ? 0 : this.breaker.blockedFor(relay);
|
|
1959
|
+
if (blocked > 0) {
|
|
1960
|
+
tried.add(relay);
|
|
1961
|
+
if (this.breaker.blockedKind(relay) === "throttled") budgetWait = Math.min(budgetWait, blocked);
|
|
1962
|
+
else down = true;
|
|
1963
|
+
continue;
|
|
1964
|
+
}
|
|
1965
|
+
if (hedge && (this.heldFor(relay, who, Date.now(), pubKeyZ32) > 0 || !this.hedgeRoom(relay))) continue;
|
|
1966
|
+
tried.add(relay);
|
|
1967
|
+
if (!this.take(relay, who, pubKeyZ32)) {
|
|
1968
|
+
budgetWait = Math.min(budgetWait, this.heldFor(relay, who, Date.now(), pubKeyZ32));
|
|
1969
|
+
continue;
|
|
1970
|
+
}
|
|
1971
|
+
if (relay === probe) this.breaker.beginAllDown(relay);
|
|
1972
|
+
else this.breaker.begin(relay);
|
|
1973
|
+
return relay;
|
|
1974
|
+
}
|
|
1975
|
+
return null;
|
|
1976
|
+
};
|
|
1977
|
+
const asked = /* @__PURE__ */ new Map();
|
|
1978
|
+
let lastAsked = 0;
|
|
1979
|
+
const ask = (hedge) => {
|
|
1980
|
+
const relay = next(hedge);
|
|
1981
|
+
if (!relay) return false;
|
|
1982
|
+
asked.set(relay, this.readFromRelay(relay, pubKeyZ32));
|
|
1983
|
+
lastAsked = Date.now();
|
|
1984
|
+
return true;
|
|
1985
|
+
};
|
|
1986
|
+
ask(false);
|
|
1987
|
+
let winner = null;
|
|
1988
|
+
while (asked.size > 0 && !winner) {
|
|
1989
|
+
const hedgeIn = hedgeAfter - (Date.now() - lastAsked);
|
|
1990
|
+
if (hedgeIn <= 0) {
|
|
1991
|
+
if (!ask(true)) hedgeAfter = Infinity;
|
|
1992
|
+
continue;
|
|
1993
|
+
}
|
|
1994
|
+
let timer;
|
|
1995
|
+
const hedgeDue = hedgeIn === Infinity ? null : new Promise((resolve) => {
|
|
1996
|
+
timer = setTimeout(() => resolve(null), hedgeIn);
|
|
1997
|
+
});
|
|
1998
|
+
const settled = await Promise.race([...[...asked.values()].map((attempt) => attempt.done.then((outcome) => ({
|
|
1999
|
+
attempt,
|
|
2000
|
+
outcome
|
|
2001
|
+
}))), ...hedgeDue ? [hedgeDue] : []]);
|
|
2002
|
+
clearTimeout(timer);
|
|
2003
|
+
if (!settled) continue;
|
|
2004
|
+
const { attempt, outcome } = settled;
|
|
2005
|
+
asked.delete(attempt.relay);
|
|
2006
|
+
const relay = attempt.relay;
|
|
2007
|
+
if (outcome.kind === "packet" || outcome.kind === "missing") {
|
|
2008
|
+
winner = {
|
|
2009
|
+
attempt,
|
|
2010
|
+
outcome
|
|
2011
|
+
};
|
|
2012
|
+
break;
|
|
2013
|
+
}
|
|
2014
|
+
if (outcome.kind === "throttled") {
|
|
2015
|
+
budgetWait = Math.min(budgetWait, this.coolDown(relay, outcome.response));
|
|
2016
|
+
this.answered(relay, {
|
|
2017
|
+
kind: "throttled",
|
|
2018
|
+
reason: "rate limited (429)"
|
|
2019
|
+
});
|
|
2020
|
+
} else if (outcome.kind === "failed") {
|
|
2021
|
+
this.networkCooldown.set(`GET ${relay}`, Date.now() + NETWORK_ERROR_COOLDOWN_MS);
|
|
2022
|
+
this.answered(relay, {
|
|
2023
|
+
kind: "error",
|
|
2024
|
+
reason: outcome.reason
|
|
2025
|
+
});
|
|
2026
|
+
down = true;
|
|
2027
|
+
}
|
|
2028
|
+
ask(false);
|
|
2029
|
+
}
|
|
2030
|
+
if (winner) {
|
|
2031
|
+
const { attempt, outcome } = winner, relay = attempt.relay;
|
|
2032
|
+
const took = Date.now() - attempt.started;
|
|
2033
|
+
if (outcome.kind === "packet") {
|
|
2034
|
+
this.sawTimestamp(pubKeyZ32, outcome.packet.timestampMicros);
|
|
2035
|
+
this.newest.set(pubKeyZ32, newerPacket(this.newest.get(pubKeyZ32), outcome.packet));
|
|
2036
|
+
this.missing.delete(pubKeyZ32);
|
|
2037
|
+
} else {
|
|
2038
|
+
this.missing.delete(pubKeyZ32);
|
|
2039
|
+
this.missing.add(pubKeyZ32);
|
|
2040
|
+
if (this.missing.size > READ_TURNS_KEPT) this.missing.delete(this.missing.values().next().value);
|
|
2041
|
+
}
|
|
2042
|
+
if (took <= 3e3) {
|
|
2043
|
+
this.answered(relay, void 0, "GET");
|
|
2044
|
+
this.slowUntil.delete(relay);
|
|
2045
|
+
} else {
|
|
2046
|
+
this.networkCooldown.delete(`GET ${relay}`);
|
|
2047
|
+
if (outcome.kind === "packet") this.slow(relay);
|
|
2048
|
+
else this.breaker.cancel(relay);
|
|
2049
|
+
}
|
|
2050
|
+
for (const loser of asked.values()) {
|
|
2051
|
+
loser.controller.abort();
|
|
2052
|
+
if (loser.started <= attempt.started && (outcome.kind === "packet" || Date.now() - loser.started >= 6e3)) this.slow(loser.relay);
|
|
2053
|
+
else this.breaker.cancel(loser.relay);
|
|
2054
|
+
}
|
|
2055
|
+
this.lastRelay = relay;
|
|
2056
|
+
this.answeredRead(pubKeyZ32);
|
|
2057
|
+
this.readFrom(pubKeyZ32, relay);
|
|
2058
|
+
return this.newest.get(pubKeyZ32) ?? null;
|
|
2059
|
+
}
|
|
2060
|
+
const resting = this.relays.every((r) => this.isCoolingDown(r, "GET") || this.breaker.blockedFor(r) > 0 || this.heldFor(r, who, Date.now(), pubKeyZ32) > 0);
|
|
2061
|
+
const known = down ? this.newest.get(pubKeyZ32) : newerPacket(this.newest.get(pubKeyZ32), this.written.get(pubKeyZ32));
|
|
2062
|
+
if (!down && budgetWait < Infinity) budgetWait = Math.max(budgetWait, this.backgroundTurnIn(pubKeyZ32, who, Date.now()));
|
|
2063
|
+
if (resting && known) {
|
|
2064
|
+
if (down && !keptOnFailure) throw new RelaysFailedError(known);
|
|
2065
|
+
if (!down && budgetWait < Infinity) this.heldBackUntil(pubKeyZ32, Date.now() + budgetWait);
|
|
2066
|
+
return known;
|
|
2067
|
+
}
|
|
2068
|
+
if (!down && budgetWait < Infinity) {
|
|
2069
|
+
this.heldBackUntil(pubKeyZ32, Date.now() + budgetWait);
|
|
2070
|
+
throw new DiscoveryBudgetError(budgetWait);
|
|
2071
|
+
}
|
|
2072
|
+
throw new Error("No Pkarr relay reachable");
|
|
2073
|
+
}
|
|
2074
|
+
/**
|
|
2075
|
+
* The order a key's read asks the relays in: in turn per key (see `resolve`), relays found slow lately or leaving a
|
|
2076
|
+
* request unanswered (`unanswering`) last. A relay
|
|
2077
|
+
* with a small share of its own (`RELAY_REQUESTS_PER_MINUTE` under `REQUESTS_PER_MINUTE`: pkarr.miguelmedeiros.dev's 5)
|
|
2078
|
+
* takes no turns: it comes after the others, for when they refuse, fail or are slow, unless every relay is one. In the
|
|
2079
|
+
* turn, it was spent in seconds and the relay after it took its turns too: pkarr.pubky.org got two reads in three and
|
|
2080
|
+
* pkarr.pubky.app, with twice its share, one. Its few requests are then left for writes, which go to every relay.
|
|
2081
|
+
*/
|
|
2082
|
+
readOrder(pubKeyZ32) {
|
|
2083
|
+
const full = this.relays.filter((relay) => !((RELAY_REQUESTS_PER_MINUTE[relay] ?? Infinity) < 30));
|
|
2084
|
+
const ring = full.length ? full : this.relays;
|
|
2085
|
+
const last = this.lastReadFrom.get(pubKeyZ32), lastAt = last === void 0 ? -1 : this.relays.indexOf(last);
|
|
2086
|
+
const after = lastAt < 0 ? -1 : ring.findIndex((relay) => this.relays.indexOf(relay) > lastAt);
|
|
2087
|
+
const start = lastAt < 0 ? this.cursor++ : after < 0 ? 0 : after;
|
|
2088
|
+
const turn = [...ring.map((_, i) => ring[(start + i) % ring.length]), ...this.relays.filter((relay) => !ring.includes(relay))];
|
|
2089
|
+
const now = Date.now(), behind = (relay) => (this.slowUntil.get(relay) ?? 0) > now || this.unanswering(relay, now);
|
|
2090
|
+
return [...turn.filter((relay) => !behind(relay)), ...turn.filter(behind)];
|
|
2091
|
+
}
|
|
2092
|
+
/**
|
|
2093
|
+
* A request of this client has waited on the relay for `HEDGE_MS` and it has answered none since: it goes after the
|
|
2094
|
+
* others in a read (`readOrder`), at no cost of a request. A relay that took connections and never answered held a
|
|
2095
|
+
* fresh device's first pairing at its turn: the key's last read had found nothing (`HEDGE_MISSING_MS`), so the read
|
|
2096
|
+
* waited 6 s on it before asking the relay that had held the packet all along (8 s join to live, not 2.5 s).
|
|
2097
|
+
*/
|
|
2098
|
+
unanswering(relay, now) {
|
|
2099
|
+
const since = this.answeredAt.get(relay) ?? -Infinity;
|
|
2100
|
+
for (const request of this.waiting.get(relay) ?? []) if (request.at > since && now - request.at >= this.hedgeMs) return true;
|
|
2101
|
+
return false;
|
|
2102
|
+
}
|
|
2103
|
+
/** One GET of a key at one relay, which `controller` drops. Never rejects: what happened is the outcome. */
|
|
2104
|
+
readFromRelay(relay, pubKeyZ32) {
|
|
2105
|
+
const controller = new AbortController();
|
|
2106
|
+
return {
|
|
2107
|
+
relay,
|
|
2108
|
+
controller,
|
|
2109
|
+
started: Date.now(),
|
|
2110
|
+
done: (async () => {
|
|
2111
|
+
let status = 0;
|
|
2112
|
+
try {
|
|
2113
|
+
let payload;
|
|
2114
|
+
const response = await this.request(`${relay}/${pubKeyZ32}`, { method: "GET" }, async (r, signal) => {
|
|
2115
|
+
status = r.status;
|
|
2116
|
+
if (r.ok && r.status !== 404) payload = await readRelayBody(r, signal);
|
|
2117
|
+
}, controller.signal);
|
|
2118
|
+
status = response.status;
|
|
2119
|
+
if (response.status === 429) return {
|
|
2120
|
+
kind: "throttled",
|
|
2121
|
+
response
|
|
2122
|
+
};
|
|
2123
|
+
if (response.status === 404) return { kind: "missing" };
|
|
2124
|
+
if (!response.ok) throw new Error(`${relay} responded ${response.status}`);
|
|
2125
|
+
return {
|
|
2126
|
+
kind: "packet",
|
|
2127
|
+
packet: parseRelayPayload(pubKeyZ32, payload)
|
|
2128
|
+
};
|
|
2129
|
+
} catch {
|
|
2130
|
+
if (controller.signal.aborted) return { kind: "cancelled" };
|
|
2131
|
+
return {
|
|
2132
|
+
kind: "failed",
|
|
2133
|
+
reason: status >= 200 && status < 300 ? "invalid packet" : status ? `HTTP ${status}` : "no answer"
|
|
2134
|
+
};
|
|
2135
|
+
}
|
|
2136
|
+
})()
|
|
2137
|
+
};
|
|
2138
|
+
}
|
|
2139
|
+
/**
|
|
2140
|
+
* The relay was slow (`SLOW_MS`): it goes after the others for a while, and counts it against its breaker while
|
|
2141
|
+
* another relay is there to ask. With none, a slow answer beats no answer: it counts as one.
|
|
2142
|
+
*/
|
|
2143
|
+
slow(relay) {
|
|
2144
|
+
this.slowUntil.set(relay, Date.now() + SLOW_DEMOTE_MS);
|
|
2145
|
+
if (this.relays.some((r) => r !== relay && this.breaker.blockedFor(r) === 0 && !this.isCoolingDown(r, "GET"))) this.breaker.failure(relay, "error", "slow");
|
|
2146
|
+
else this.breaker.success(relay, this.relays);
|
|
2147
|
+
}
|
|
2148
|
+
/**
|
|
2149
|
+
* A hedge may go to this relay: its minute has more requests left than what groups leave a chat (`reserveOf`), so
|
|
2150
|
+
* a second request for a read that already went out never takes one a pairing needs.
|
|
2151
|
+
*/
|
|
2152
|
+
hedgeRoom(relay) {
|
|
2153
|
+
const now = Date.now();
|
|
2154
|
+
return within(this.spent.get(relay) ?? [], now, 6e4).length < this.limitOf(relay) - this.reserveOf(relay);
|
|
2155
|
+
}
|
|
2156
|
+
readAnsweredAt(pubKeyZ32) {
|
|
2157
|
+
return this.readAt.get(pubKeyZ32);
|
|
2158
|
+
}
|
|
2159
|
+
/** The last read of this key was answered 404 (`HEDGE_MISSING_MS`): a relay takes seconds to say so. */
|
|
2160
|
+
lastReadMissing(pubKeyZ32) {
|
|
2161
|
+
return this.missing.has(pubKeyZ32);
|
|
2162
|
+
}
|
|
2163
|
+
readHeldBackFor(pubKeyZ32) {
|
|
2164
|
+
const held = this.heldBack.get(pubKeyZ32);
|
|
2165
|
+
const left = held === void 0 ? 0 : held.until - Date.now();
|
|
2166
|
+
return left > 0 ? left : void 0;
|
|
2167
|
+
}
|
|
2168
|
+
heldBackUntil(pubKeyZ32, until) {
|
|
2169
|
+
const held = this.heldBack.get(pubKeyZ32), times = (held?.times ?? 0) + 1;
|
|
2170
|
+
this.heldBack.delete(pubKeyZ32);
|
|
2171
|
+
this.heldBack.set(pubKeyZ32, {
|
|
2172
|
+
until,
|
|
2173
|
+
times,
|
|
2174
|
+
since: held?.since ?? Date.now()
|
|
2175
|
+
});
|
|
2176
|
+
if (this.heldBack.size > READ_TURNS_KEPT) this.heldBack.delete(this.heldBack.keys().next().value);
|
|
2177
|
+
}
|
|
2178
|
+
/** The relays answered a read of this key now; answers older than `FRESH_READ_MS` are dropped as the list grows. */
|
|
2179
|
+
answeredRead(pubKeyZ32) {
|
|
2180
|
+
const now = Date.now();
|
|
2181
|
+
this.heldBack.delete(pubKeyZ32);
|
|
2182
|
+
this.readAt.delete(pubKeyZ32);
|
|
2183
|
+
this.readAt.set(pubKeyZ32, now);
|
|
2184
|
+
if (this.readAt.size > 64) for (const [key, at] of this.readAt) {
|
|
2185
|
+
if (now - at < this.freshReadMs) break;
|
|
2186
|
+
this.readAt.delete(key);
|
|
2187
|
+
}
|
|
2188
|
+
}
|
|
2189
|
+
/** The relay that answered a key's read, for its next one; the keys read longest ago go past `READ_TURNS_KEPT`. */
|
|
2190
|
+
readFrom(pubKeyZ32, relay) {
|
|
2191
|
+
this.lastReadFrom.delete(pubKeyZ32);
|
|
2192
|
+
this.lastReadFrom.set(pubKeyZ32, relay);
|
|
2193
|
+
if (this.lastReadFrom.size > READ_TURNS_KEPT) this.lastReadFrom.delete(this.lastReadFrom.keys().next().value);
|
|
2194
|
+
}
|
|
2195
|
+
/**
|
|
2196
|
+
* A relay whose budget (this client's, or its own rate limit) held a packet back that another relay took: it gets
|
|
2197
|
+
* that packet when it frees a request, as a background write (`who`: a group edge's offer or answer as its own, also
|
|
2198
|
+
* after that relay failed it). Only the newest per key and relay, until that relay takes this packet or a newer one (one
|
|
2199
|
+
* the budget holds there takes its place). Without this,
|
|
2200
|
+
* that relay kept the key's older packet for good, and an app that restarted and read it first found a contact's offer
|
|
2201
|
+
* long answered, answered it again, and waited on it (bug hunt r7a).
|
|
2202
|
+
*/
|
|
2203
|
+
catchUpLater(relay, key, payload, timestamp, who = CATCH_UP_ASKER) {
|
|
2204
|
+
const slot = `${relay} ${key}`;
|
|
2205
|
+
if ((this.lastPut.get(slot) ?? 0n) > timestamp) return;
|
|
2206
|
+
const pending = this.catchUp.get(slot);
|
|
2207
|
+
if (pending && pending.timestamp >= timestamp) return;
|
|
2208
|
+
this.keepCatchUp(slot, {
|
|
2209
|
+
relay,
|
|
2210
|
+
key,
|
|
2211
|
+
payload,
|
|
2212
|
+
timestamp,
|
|
2213
|
+
who
|
|
2214
|
+
});
|
|
2215
|
+
this.scheduleCatchUp(CATCH_UP_MIN_MS);
|
|
2216
|
+
}
|
|
2217
|
+
keepCatchUp(slot, item) {
|
|
2218
|
+
if (!this.catchUp.has(slot)) this.catchUpWaiting.set(item.relay, (this.catchUpWaiting.get(item.relay) ?? 0) + 1);
|
|
2219
|
+
this.catchUp.set(slot, item);
|
|
2220
|
+
}
|
|
2221
|
+
dropCatchUp(relay, key) {
|
|
2222
|
+
if (!this.catchUp.delete(`${relay} ${key}`)) return;
|
|
2223
|
+
const left = (this.catchUpWaiting.get(relay) ?? 1) - 1;
|
|
2224
|
+
if (left > 0) this.catchUpWaiting.set(relay, left);
|
|
2225
|
+
else this.catchUpWaiting.delete(relay);
|
|
2226
|
+
}
|
|
2227
|
+
scheduleCatchUp(inMs) {
|
|
2228
|
+
if (this.catchUpTimer || this.catchUp.size === 0) return;
|
|
2229
|
+
this.catchUpTimer = setTimeout(() => {
|
|
2230
|
+
this.catchUpTimer = null;
|
|
2231
|
+
this.runCatchUp();
|
|
2232
|
+
}, inMs);
|
|
2233
|
+
}
|
|
2234
|
+
async runCatchUp() {
|
|
2235
|
+
let next = Infinity;
|
|
2236
|
+
for (const [slot, item] of [...this.catchUp]) {
|
|
2237
|
+
if (this.catchUp.get(slot) !== item) continue;
|
|
2238
|
+
const coolingDown = (this.networkCooldown.get(`PUT ${item.relay}`) ?? 0) - Date.now();
|
|
2239
|
+
const who = {
|
|
2240
|
+
...item.who,
|
|
2241
|
+
catchUp: true
|
|
2242
|
+
};
|
|
2243
|
+
const wait = Math.max(this.heldFor(item.relay, who), this.rateLimitedFor(item.relay), this.breaker.blockedFor(item.relay), coolingDown);
|
|
2244
|
+
if (wait > 0) {
|
|
2245
|
+
next = Math.min(next, wait);
|
|
2246
|
+
continue;
|
|
2247
|
+
}
|
|
2248
|
+
this.dropCatchUp(item.relay, item.key);
|
|
2249
|
+
try {
|
|
2250
|
+
const response = await this.put(item.relay, item.key, item.payload, void 0, who);
|
|
2251
|
+
if ((response.status === 428 || response.status === 429) && !this.catchUp.has(slot)) {
|
|
2252
|
+
this.keepCatchUp(slot, item);
|
|
2253
|
+
next = Math.min(next, CATCH_UP_RETRY_MS);
|
|
2254
|
+
}
|
|
2255
|
+
} catch (error) {
|
|
2256
|
+
if (isDiscoveryBudgetError(error) && !this.catchUp.has(slot)) {
|
|
2257
|
+
this.keepCatchUp(slot, item);
|
|
2258
|
+
next = Math.min(next, error.retryInMs);
|
|
2259
|
+
}
|
|
2260
|
+
}
|
|
2261
|
+
}
|
|
2262
|
+
if (this.catchUp.size) this.scheduleCatchUp(Math.max(CATCH_UP_MIN_MS, next === Infinity ? CATCH_UP_RETRY_MS : next));
|
|
2263
|
+
}
|
|
2264
|
+
/** `sent`: told, once the budget took the request, whether the relay takes the packet (a 2xx). */
|
|
2265
|
+
async put(relay, pubKeyZ32, payload, replaces, who, probe = false, sent) {
|
|
2266
|
+
const refused = this.refusal(relay, probe);
|
|
2267
|
+
if (refused) throw refused;
|
|
2268
|
+
if (!this.take(relay, who, pubKeyZ32)) throw new DiscoveryBudgetError(this.heldFor(relay, who));
|
|
2269
|
+
if (probe) this.breaker.beginAllDown(relay);
|
|
2270
|
+
else this.breaker.begin(relay);
|
|
2271
|
+
const request = this.request(`${relay}/${pubKeyZ32}`, {
|
|
2272
|
+
method: "PUT",
|
|
2273
|
+
body: payload,
|
|
2274
|
+
headers: replaces === void 0 ? void 0 : { "If-Match": replaces.toString() }
|
|
2275
|
+
});
|
|
2276
|
+
sent?.(request.then((r) => r.ok, () => false));
|
|
2277
|
+
let response;
|
|
2278
|
+
try {
|
|
2279
|
+
response = await request;
|
|
2280
|
+
} catch (error) {
|
|
2281
|
+
this.networkCooldown.set(`PUT ${relay}`, Date.now() + NETWORK_ERROR_COOLDOWN_MS);
|
|
2282
|
+
this.answered(relay, {
|
|
2283
|
+
kind: "error",
|
|
2284
|
+
reason: "no answer"
|
|
2285
|
+
});
|
|
2286
|
+
throw new RelayNoAnswerError(`${relay} did not answer`, !connectFailed(error));
|
|
2287
|
+
}
|
|
2288
|
+
if (response.status === 429) this.answered(relay, {
|
|
2289
|
+
kind: "throttled",
|
|
2290
|
+
reason: "rate limited (429)"
|
|
2291
|
+
});
|
|
2292
|
+
else if (response.status >= 500) this.answered(relay, {
|
|
2293
|
+
kind: "error",
|
|
2294
|
+
reason: `HTTP ${response.status}`
|
|
2295
|
+
});
|
|
2296
|
+
else this.answered(relay, void 0, "PUT");
|
|
2297
|
+
return response;
|
|
2298
|
+
}
|
|
2299
|
+
/**
|
|
2300
|
+
* Discovery reads and writes share a bounded per-relay request budget; background requests only part of it, and
|
|
2301
|
+
* groups leave a chat the budget refused its reserve. A link's write the budget refused goes before any read
|
|
2302
|
+
* once a request is free again.
|
|
2303
|
+
*/
|
|
2304
|
+
take(relay, who, key) {
|
|
2305
|
+
const now = Date.now();
|
|
2306
|
+
const writer = firstWriter(who);
|
|
2307
|
+
if (who.urgent && !who.background) {
|
|
2308
|
+
this.urgentAt = now;
|
|
2309
|
+
if (isChat(who)) this.chatNeed(key, now);
|
|
2310
|
+
}
|
|
2311
|
+
if (isGroupRead(who) || isChatWatch(who)) this.readAsked(key, who, now);
|
|
2312
|
+
if (isTurnRead(who)) this.turnAsked(key, who, now);
|
|
2313
|
+
if (isAwaitedRead(who)) this.awaitedAt = now;
|
|
2314
|
+
if (this.heldFor(relay, who, now, key) > 0) {
|
|
2315
|
+
if (isGroupRead(who) && (this.spent.get(relay)?.length ?? 0) >= this.limitOf(relay)) this.groupsFullAt.set(relay, now);
|
|
2316
|
+
if (isChat(who)) this.chatNeed(key, now, relay);
|
|
2317
|
+
if (writer) this.writeWaiting.set(`${writer} ${relay}`, now);
|
|
2318
|
+
if (isGroupAnswerRead(who)) {
|
|
2319
|
+
const waiting = this.answerReadWaiting.get(relay) ?? /* @__PURE__ */ new Map();
|
|
2320
|
+
this.answerReadWaiting.set(relay, waiting.set(key, now));
|
|
2321
|
+
}
|
|
2322
|
+
return false;
|
|
2323
|
+
}
|
|
2324
|
+
if (writer) this.writeWaiting.delete(`${writer} ${relay}`);
|
|
2325
|
+
if (isGroupAnswerRead(who)) this.answerReadWaiting.get(relay)?.delete(key);
|
|
2326
|
+
if (isWayBack(who) && this.heldFor(relay, {
|
|
2327
|
+
...who,
|
|
2328
|
+
back: false
|
|
2329
|
+
}, now, key, false) > 0) {
|
|
2330
|
+
this.spentWayBack.set(relay, [...this.spentWayBack.get(relay) ?? [], now]);
|
|
2331
|
+
if (isGroupRead(who)) this.readTurns.set(key, {
|
|
2332
|
+
servedAt: now,
|
|
2333
|
+
askedAt: now,
|
|
2334
|
+
waiting: false,
|
|
2335
|
+
watch: false,
|
|
2336
|
+
lane: watchLane(who)
|
|
2337
|
+
});
|
|
2338
|
+
return true;
|
|
2339
|
+
}
|
|
2340
|
+
if (isGroupAnswerRead(who) && this.heldFor(relay, who, now, key, false, false) > 0) {
|
|
2341
|
+
this.spentAnswerAllowance.set(relay, [...this.spentAnswerAllowance.get(relay) ?? [], now]);
|
|
2342
|
+
this.readTurns.set(key, {
|
|
2343
|
+
servedAt: now,
|
|
2344
|
+
askedAt: now,
|
|
2345
|
+
waiting: false,
|
|
2346
|
+
watch: false,
|
|
2347
|
+
lane: watchLane(who),
|
|
2348
|
+
answer: true
|
|
2349
|
+
});
|
|
2350
|
+
return true;
|
|
2351
|
+
}
|
|
2352
|
+
if (isGroupSignal(who) && !who.write && this.spent.get(relay).length >= this.limitOf(relay)) this.spentGroupOver.get(relay).push(now);
|
|
2353
|
+
if (isGroupSignal(who)) this.spentGroupSignal.get(relay).push(now);
|
|
2354
|
+
const departedOver = isDepartedRead(who) && this.spent.get(relay).length >= this.limitOf(relay);
|
|
2355
|
+
if (departedOver) this.overAdd(this.departedOver, key, now);
|
|
2356
|
+
if (isAwaitedRead(who) && this.spent.get(relay).length >= this.limitOf(relay)) this.awaitedOver = [...within(this.awaitedOver, now, 6e4), now];
|
|
2357
|
+
this.spent.get(relay).push(now);
|
|
2358
|
+
if (who.background) this.spentBackground.get(relay).push(now);
|
|
2359
|
+
if (who.background && who.write) this.spentBackgroundWrites.get(relay).push(now);
|
|
2360
|
+
if (isGroupUrgentRead(who) && isGroupAnswerRead(who)) this.spentAnswerUrgent.set(relay, [...this.spentAnswerUrgent.get(relay) ?? [], now]);
|
|
2361
|
+
else if (isGroupUrgentRead(who)) this.spentGroupUrgent.get(relay).push(now);
|
|
2362
|
+
if (isWatch(who)) this.spentWatch.get(relay).push(now);
|
|
2363
|
+
if (isChatWatch(who) && !departedOver) this.spentChatWatch.get(relay).push(now);
|
|
2364
|
+
if (isGroupRead(who)) this.spentGroupReads.get(relay).push(now);
|
|
2365
|
+
if (isGroupRead(who) || isChatWatch(who)) this.readTurns.set(key, {
|
|
2366
|
+
servedAt: now,
|
|
2367
|
+
askedAt: now,
|
|
2368
|
+
waiting: false,
|
|
2369
|
+
watch: isWatch(who) || isChatWatch(who),
|
|
2370
|
+
lane: watchLane(who)
|
|
2371
|
+
});
|
|
2372
|
+
if (isTurnRead(who)) this.keyTurns.set(key, {
|
|
2373
|
+
servedAt: now,
|
|
2374
|
+
askedAt: now,
|
|
2375
|
+
waiting: false,
|
|
2376
|
+
who
|
|
2377
|
+
});
|
|
2378
|
+
return true;
|
|
2379
|
+
}
|
|
2380
|
+
/**
|
|
2381
|
+
* How long until this relay's budget takes this request, 0 when it would now: the oldest request of the minute over
|
|
2382
|
+
* a limit ages out, a chat's reserve lapses, or a waiting write has had its turn. Keeps the lists to the minute, oldest first.
|
|
2383
|
+
*
|
|
2384
|
+
* Who goes first: a chat's refused write holds back everything but chat writes; a group's refused write holds back
|
|
2385
|
+
* group reads and background requests, never a chat's. Background requests yield to a link that signals. A chat's
|
|
2386
|
+
* signaling goes over the limit by its allowance (`SIGNALING_ALLOWANCE_SHARE`), and so do a few of its reads of a
|
|
2387
|
+
* contact that just went away (`DEPARTED_READS`) or of an invite awaited (`AWAITED_READS`). `ownAllowance` false: what holds an edge's read for its answer
|
|
2388
|
+
* without its own allowance (`ANSWER_READ_ALLOWANCE`).
|
|
2389
|
+
*/
|
|
2390
|
+
heldFor(relay, who, now = Date.now(), key, turns = true, ownAllowance = true) {
|
|
2391
|
+
const recent = within(this.spent.get(relay) ?? [], now, 6e4);
|
|
2392
|
+
const recentBackground = within(this.spentBackground.get(relay) ?? [], now, 6e4);
|
|
2393
|
+
this.spent.set(relay, recent);
|
|
2394
|
+
this.spentBackground.set(relay, recentBackground);
|
|
2395
|
+
const recentBackgroundWrites = within(this.spentBackgroundWrites.get(relay) ?? [], now, 6e4);
|
|
2396
|
+
this.spentBackgroundWrites.set(relay, recentBackgroundWrites);
|
|
2397
|
+
const over = (list, limit) => list.length >= limit ? list[list.length - limit] + 6e4 - now : 0;
|
|
2398
|
+
const limit = this.limitOf(relay);
|
|
2399
|
+
const departed = isDepartedRead(who) && key !== void 0 && recent.length >= limit && this.overReads(this.departedOver, key, now) < 6;
|
|
2400
|
+
if (isAwaitedRead(who)) this.awaitedOver = within(this.awaitedOver, now, 6e4);
|
|
2401
|
+
const awaited = isAwaitedRead(who) && key !== void 0 && recent.length >= limit && this.awaitedOver.length < 4;
|
|
2402
|
+
const share = isChatSignal(who) || departed || awaited ? SIGNALING_ALLOWANCE_SHARE : isGroupSignal(who) ? GROUP_SIGNALING_ALLOWANCE_SHARE : 0;
|
|
2403
|
+
let allowance = share ? Math.max(0, Math.floor(limit * share) - (who.write ? 0 : 1)) : 0;
|
|
2404
|
+
if (departed && now - this.awaitedAt < 6e4) allowance = Math.max(0, allowance - 2);
|
|
2405
|
+
const groupSignaled = within(this.spentGroupSignal.get(relay) ?? [], now, 6e4);
|
|
2406
|
+
this.spentGroupSignal.set(relay, groupSignaled);
|
|
2407
|
+
const groupFloor = isGroupSignal(who) && groupSignaled.length < allowance;
|
|
2408
|
+
let wait = groupFloor ? 0 : over(recent, limit + allowance);
|
|
2409
|
+
const starved = key !== void 0 && this.starved(key, who, now);
|
|
2410
|
+
const groupOver = within(this.spentGroupOver.get(relay) ?? [], now, GROUP_OVER_PACE_MS);
|
|
2411
|
+
this.spentGroupOver.set(relay, groupOver);
|
|
2412
|
+
if (isGroupSignal(who) && !who.write && recent.length >= limit && groupOver.length > 0) wait = Math.max(wait, groupOver[groupOver.length - 1] + GROUP_OVER_PACE_MS - now);
|
|
2413
|
+
const backgroundShare = Math.min(this.backgroundPerMinute, Math.ceil(limit * 2 / 3));
|
|
2414
|
+
const writesShare = Math.max(1, Math.floor(backgroundShare * BACKGROUND_WRITES_SHARE));
|
|
2415
|
+
const caughtUp = this.catchUpWaiting.get(relay) ?? 0;
|
|
2416
|
+
if (who.background && !who.write && caughtUp) {
|
|
2417
|
+
const writesLeft = writesShare - recentBackgroundWrites.length;
|
|
2418
|
+
if (writesLeft > 0) wait = Math.max(wait, over(recent, Math.max(1, limit - writesLeft)), over(recentBackground, Math.max(1, backgroundShare - writesLeft)));
|
|
2419
|
+
}
|
|
2420
|
+
if (who.background && who.write && !who.catchUp && caughtUp) wait = Math.max(wait, Math.min(CATCH_UP_BEHIND_MAX_MS, Math.ceil(caughtUp * 6e4 / writesShare)));
|
|
2421
|
+
if (who.background && !starved) {
|
|
2422
|
+
wait = Math.max(wait, over(recentBackground, backgroundShare));
|
|
2423
|
+
if (who.write) wait = Math.max(wait, over(recentBackgroundWrites, writesShare));
|
|
2424
|
+
const signaling = this.urgentAt + SIGNALING_WINDOW_MS - now;
|
|
2425
|
+
if (signaling > 0 && !who.door) wait = Math.max(wait, Math.min(signaling, over(recentBackground, Math.min(this.backgroundWhileSignaling, Math.ceil(limit / 6)))));
|
|
2426
|
+
}
|
|
2427
|
+
const burst = within(this.spentGroupUrgent.get(relay) ?? [], now, GROUP_BURST_MS);
|
|
2428
|
+
this.spentGroupUrgent.set(relay, burst);
|
|
2429
|
+
const answerBurst = within(this.spentAnswerUrgent.get(relay) ?? [], now, GROUP_BURST_MS);
|
|
2430
|
+
this.spentAnswerUrgent.set(relay, answerBurst);
|
|
2431
|
+
if (isGroupUrgentRead(who)) {
|
|
2432
|
+
const most = Math.max(8, Math.floor(limit * GROUP_BURST_MS / 6e4));
|
|
2433
|
+
const spent = isGroupAnswerRead(who) ? answerBurst : burst;
|
|
2434
|
+
if (spent.length >= most) wait = Math.max(wait, spent[spent.length - most] + GROUP_BURST_MS - now);
|
|
2435
|
+
}
|
|
2436
|
+
const watched = within(this.spentWatch.get(relay) ?? [], now, GROUP_RATION_WINDOW_MS);
|
|
2437
|
+
this.spentWatch.set(relay, watched);
|
|
2438
|
+
let turn = 0;
|
|
2439
|
+
if (isWatch(who)) {
|
|
2440
|
+
const most = Math.max(1, Math.ceil(limit * WATCH_SHARE * GROUP_RATION_WINDOW_MS / 6e4));
|
|
2441
|
+
if (watched.length >= most) wait = Math.max(wait, watched[watched.length - most] + GROUP_RATION_WINDOW_MS - now);
|
|
2442
|
+
if (this.readsAhead(key, now, watchLane(who)) > 0) turn = TURN_WAIT_MS;
|
|
2443
|
+
}
|
|
2444
|
+
const chatWatched = within(this.spentChatWatch.get(relay) ?? [], now, GROUP_RATION_WINDOW_MS);
|
|
2445
|
+
this.spentChatWatch.set(relay, chatWatched);
|
|
2446
|
+
if (isChatWatch(who) && !departed) {
|
|
2447
|
+
const most = Math.max(1, Math.ceil(limit * CHAT_WATCH_SHARE * GROUP_RATION_WINDOW_MS / 6e4));
|
|
2448
|
+
if (chatWatched.length >= most) wait = Math.max(wait, chatWatched[chatWatched.length - most] + GROUP_RATION_WINDOW_MS - now);
|
|
2449
|
+
if (this.readsAhead(key, now, watchLane(who)) > 0) turn = TURN_WAIT_MS;
|
|
2450
|
+
}
|
|
2451
|
+
const rationed = within(this.spentGroupReads.get(relay) ?? [], now, GROUP_RATION_WINDOW_MS);
|
|
2452
|
+
this.spentGroupReads.set(relay, rationed);
|
|
2453
|
+
const pastLimit = isGroupSignal(who) && recent.length >= limit;
|
|
2454
|
+
if (isGroupRead(who) && !pastLimit && now - (this.groupsFullAt.get(relay) ?? -Infinity) < GROUP_RATION_MS) {
|
|
2455
|
+
const most = Math.max(1, Math.ceil(limit * GROUP_RATION_WINDOW_MS / 6e4));
|
|
2456
|
+
if (rationed.length >= most) wait = Math.max(wait, rationed[rationed.length - most] + GROUP_RATION_WINDOW_MS - now);
|
|
2457
|
+
if (this.readsAhead(key, now, void 0, isGroupAnswerRead(who)) > 0) turn = TURN_WAIT_MS;
|
|
2458
|
+
}
|
|
2459
|
+
if (who.group && !groupFloor || who.background) {
|
|
2460
|
+
const reserved = this.chatReservedFor(relay, now);
|
|
2461
|
+
if (reserved > 0) wait = Math.max(wait, Math.min(reserved, over(recent, limit - this.chatReserveOf(relay, now))));
|
|
2462
|
+
}
|
|
2463
|
+
const writer = firstWriter(who);
|
|
2464
|
+
const waiting = (lane) => {
|
|
2465
|
+
const at = this.writeWaiting.get(`${lane} ${relay}`);
|
|
2466
|
+
return at !== void 0 && now - at < 5e3 ? at + WRITE_FIRST_MS - now : 0;
|
|
2467
|
+
};
|
|
2468
|
+
const signalOver = isChatSignal(who) && !who.write && recent.length >= limit;
|
|
2469
|
+
if (writer !== "chat" && !departed && !awaited && !signalOver && !starved) wait = Math.max(wait, waiting("chat"));
|
|
2470
|
+
if ((writer === null && who.group && !pastLimit && !isGroupAnswerRead(who) || who.background) && !starved) wait = Math.max(wait, waiting("group"));
|
|
2471
|
+
if (isGroupSignal(who) && !isGroupAnswerRead(who)) {
|
|
2472
|
+
const ahead = this.answerReadAhead(relay, now);
|
|
2473
|
+
if (ahead && this.heldFor(relay, ANSWER_READER, now, ahead.key, false, false) <= 0) wait = Math.max(wait, ahead.ms);
|
|
2474
|
+
}
|
|
2475
|
+
if (turns && wait <= 0 && isTurnRead(who) && this.turnAhead(relay, key, who, now)) turn = TURN_WAIT_MS;
|
|
2476
|
+
if (wait > 0 && isWayBack(who) && this.wayBackLeft(relay, who, now) > 0) return 0;
|
|
2477
|
+
if (wait > 0 && ownAllowance && isGroupAnswerRead(who) && this.answerAllowanceFree(relay, now)) return 0;
|
|
2478
|
+
return Math.max(wait, turn, 0);
|
|
2479
|
+
}
|
|
2480
|
+
/**
|
|
2481
|
+
* The edges' reads for their answers may take their own allowance on this relay now (`ANSWER_READ_ALLOWANCE`): fewer
|
|
2482
|
+
* than it in the last minute, and none within `ANSWER_READ_SPACING_MS`. None where the groups have no allowance.
|
|
2483
|
+
*/
|
|
2484
|
+
answerAllowanceFree(relay, now) {
|
|
2485
|
+
const spent = within(this.spentAnswerAllowance.get(relay) ?? [], now, 6e4);
|
|
2486
|
+
this.spentAnswerAllowance.set(relay, spent);
|
|
2487
|
+
const most = Math.min(3, Math.floor(this.limitOf(relay) * GROUP_SIGNALING_ALLOWANCE_SHARE));
|
|
2488
|
+
return spent.length < most && (spent.length === 0 || now - spent[spent.length - 1] >= 2e4);
|
|
2489
|
+
}
|
|
2490
|
+
/**
|
|
2491
|
+
* The edges' reads for their answers the budget refused on this relay within `ANSWER_READ_FIRST_MS`, still not read:
|
|
2492
|
+
* the key refused last, and how long the groups' other signaling waits for them there. Undefined when there is none.
|
|
2493
|
+
*/
|
|
2494
|
+
answerReadAhead(relay, now) {
|
|
2495
|
+
const waiting = this.answerReadWaiting.get(relay);
|
|
2496
|
+
if (!waiting) return void 0;
|
|
2497
|
+
let last;
|
|
2498
|
+
for (const [key, at] of waiting) if (now - at >= 5e3) waiting.delete(key);
|
|
2499
|
+
else if (!last || at > last.at) last = {
|
|
2500
|
+
key,
|
|
2501
|
+
at
|
|
2502
|
+
};
|
|
2503
|
+
return last && {
|
|
2504
|
+
key: last.key,
|
|
2505
|
+
ms: last.at + 5e3 - now
|
|
2506
|
+
};
|
|
2507
|
+
}
|
|
2508
|
+
/** What is left of the community members' way back allowance on this relay (`WAY_BACK_SHARE`); its reads leave a write the last of it. */
|
|
2509
|
+
wayBackLeft(relay, who, now) {
|
|
2510
|
+
const spent = within(this.spentWayBack.get(relay) ?? [], now, 6e4);
|
|
2511
|
+
this.spentWayBack.set(relay, spent);
|
|
2512
|
+
return Math.floor(this.limitOf(relay) * WAY_BACK_SHARE) - (who.write ? 0 : 1) - spent.length;
|
|
2513
|
+
}
|
|
2514
|
+
/** A group's read of `key`, or a chat's watch read, asks: it waits for its turn from now on, unless it is taken (`readsAhead`). */
|
|
2515
|
+
readAsked(key, who, now) {
|
|
2516
|
+
this.pruneTurns(this.readTurns, now);
|
|
2517
|
+
const turn = this.readTurns.get(key);
|
|
2518
|
+
this.readTurns.set(key, {
|
|
2519
|
+
servedAt: turn && !staleTurn(turn, now) ? turn.servedAt : -Infinity,
|
|
2520
|
+
askedAt: now,
|
|
2521
|
+
waiting: true,
|
|
2522
|
+
watch: isWatch(who) || isChatWatch(who),
|
|
2523
|
+
lane: watchLane(who),
|
|
2524
|
+
answer: isGroupAnswerRead(who)
|
|
2525
|
+
});
|
|
2526
|
+
}
|
|
2527
|
+
/**
|
|
2528
|
+
* Keys a group reads, still asking (within `WATCH_TURN_MS`), that were read longer ago than `key`: a share keeps a
|
|
2529
|
+
* request for each. With `watchLane`, only watched keys the same other limits hold: one the groups' burst holds back
|
|
2530
|
+
* would keep a turn it cannot take. The chats' watched keys only in their own lane (`CHAT_WATCH_SHARE`). `answer`: an
|
|
2531
|
+
* edge's read for its answer, which waits only for the others' (`ANSWER_READ_FIRST_MS`).
|
|
2532
|
+
*/
|
|
2533
|
+
readsAhead(key, now, watchLane, answer = false) {
|
|
2534
|
+
const mine = key === void 0 ? -Infinity : this.readTurns.get(key)?.servedAt ?? -Infinity;
|
|
2535
|
+
let ahead = 0;
|
|
2536
|
+
for (const [other, turn] of this.readTurns) {
|
|
2537
|
+
if (other === key || !turn.waiting || now - turn.askedAt >= WATCH_TURN_MS) continue;
|
|
2538
|
+
if (watchLane !== void 0 ? !turn.watch || turn.lane !== watchLane : turn.lane === "chat") continue;
|
|
2539
|
+
if (answer && !turn.answer) continue;
|
|
2540
|
+
if (turn.servedAt < mine) ahead++;
|
|
2541
|
+
}
|
|
2542
|
+
return ahead;
|
|
2543
|
+
}
|
|
2544
|
+
/** A read of `key` that takes turns asks: it waits for its turn from now on, unless it is taken (`turnAhead`). */
|
|
2545
|
+
turnAsked(key, who, now) {
|
|
2546
|
+
this.pruneTurns(this.keyTurns, now);
|
|
2547
|
+
const turn = this.keyTurns.get(key);
|
|
2548
|
+
this.keyTurns.set(key, {
|
|
2549
|
+
servedAt: turn && !staleTurn(turn, now) ? turn.servedAt : -Infinity,
|
|
2550
|
+
askedAt: now,
|
|
2551
|
+
waiting: true,
|
|
2552
|
+
who
|
|
2553
|
+
});
|
|
2554
|
+
}
|
|
2555
|
+
/** Drops the turns asked and read over a minute ago, at most once in `TURNS_PRUNE_MS` (or when the clock went back). */
|
|
2556
|
+
pruneTurns(turns, now) {
|
|
2557
|
+
const prunedAt = this.turnsPrunedAt.get(turns) ?? -Infinity;
|
|
2558
|
+
if (Math.abs(now - prunedAt) < TURNS_PRUNE_MS) return;
|
|
2559
|
+
this.turnsPrunedAt.set(turns, now);
|
|
2560
|
+
const back = now < prunedAt;
|
|
2561
|
+
for (const [key, turn] of turns) {
|
|
2562
|
+
if (back) {
|
|
2563
|
+
turn.servedAt = Math.min(turn.servedAt, now);
|
|
2564
|
+
turn.askedAt = Math.min(turn.askedAt, now);
|
|
2565
|
+
}
|
|
2566
|
+
if (staleTurn(turn, now)) turns.delete(key);
|
|
2567
|
+
}
|
|
2568
|
+
}
|
|
2569
|
+
/**
|
|
2570
|
+
* Whether a key read in turn longer ago than `key`, still asking, would take this relay's next request: one read in the
|
|
2571
|
+
* background for a background read, and a chat's that is starved (`READ_STARVED_MS`) for any. One asks again after
|
|
2572
|
+
* the budget held it back (a link's poll does, a one-off look does not), and keeps asking within `READ_TURN_MS` of its
|
|
2573
|
+
* last ask, or of when it was told the budget frees a request for it (`readHeldBackFor`).
|
|
2574
|
+
* One the budget holds back on its own account (a group's read, for the chats' reserve) keeps no turn: a group key
|
|
2575
|
+
* waiting for the chats held every chat's read back (CLI lab, 2026-10-13).
|
|
2576
|
+
*/
|
|
2577
|
+
turnAhead(relay, key, who, now) {
|
|
2578
|
+
const mine = key === void 0 ? -Infinity : this.keyTurns.get(key)?.servedAt ?? -Infinity;
|
|
2579
|
+
for (const [other, turn] of this.keyTurns) {
|
|
2580
|
+
const held = this.heldBack.get(other);
|
|
2581
|
+
if (other === key || !turn.waiting || turn.servedAt >= mine || !held || held.times < 2) continue;
|
|
2582
|
+
if (!(who.background && turn.who.background) && !this.starved(other, turn.who, now)) continue;
|
|
2583
|
+
if (now - Math.max(turn.askedAt, held.until) < READ_TURN_MS && this.heldFor(relay, turn.who, now, other, false) === 0) return true;
|
|
2584
|
+
}
|
|
2585
|
+
return false;
|
|
2586
|
+
}
|
|
2587
|
+
/**
|
|
2588
|
+
* How long until a background read of `key` the budget held back is likely to have its turn: the background reads of
|
|
2589
|
+
* keys ahead of it in turn (read longer ago, still asking; `turnAhead`), one after the other at the relays' background
|
|
2590
|
+
* share. Told only when the budget frees the next request, every held key asked again then, and lost it to the key
|
|
2591
|
+
* whose turn it was: a daemon whose 50 contacts were away asked about 600 times a minute for some 50 reads, and one
|
|
2592
|
+
* with 200 three times as often after four hours as after 15 minutes (bug hunts, 2026-11-06 and 2026-11-14).
|
|
2593
|
+
*/
|
|
2594
|
+
backgroundTurnIn(key, who, now) {
|
|
2595
|
+
if (!who.background || !isTurnRead(who)) return 0;
|
|
2596
|
+
const mine = this.keyTurns.get(key);
|
|
2597
|
+
if (!mine) return 0;
|
|
2598
|
+
const since = (k) => this.heldBack.get(k)?.since ?? now;
|
|
2599
|
+
const mineSince = since(key);
|
|
2600
|
+
let ahead = 0, read = 0;
|
|
2601
|
+
for (const [other, turn] of this.keyTurns) {
|
|
2602
|
+
if (other !== key && turn.who.background && now - turn.servedAt < 6e4) read++;
|
|
2603
|
+
if (other === key || !turn.waiting || !turn.who.background || staleTurn(turn, now)) continue;
|
|
2604
|
+
if (turn.servedAt < mine.servedAt || turn.servedAt === mine.servedAt && (since(other) < mineSince || since(other) === mineSince && other < key)) ahead++;
|
|
2605
|
+
}
|
|
2606
|
+
if (ahead === 0) return 0;
|
|
2607
|
+
const perMinute = this.relays.reduce((sum, relay) => sum + Math.min(this.backgroundPerMinute, Math.ceil(this.limitOf(relay) * 2 / 3)), 0);
|
|
2608
|
+
return Math.min(BACKGROUND_TURN_MAX_MS, Math.ceil(ahead * 6e4 / Math.max(1, perMinute, read)));
|
|
2609
|
+
}
|
|
2610
|
+
/** A 1:1 chat's read of `key` the budget has held back for `READ_STARVED_MS` without a break. */
|
|
2611
|
+
starved(key, who, now) {
|
|
2612
|
+
const since = this.heldBack.get(key)?.since;
|
|
2613
|
+
return !who.group && !who.write && !who.door && since !== void 0 && now - since >= READ_STARVED_MS;
|
|
2614
|
+
}
|
|
2615
|
+
/** This key's departed reads past the limit in the last minute (`DEPARTED_READS`). */
|
|
2616
|
+
overReads(over, key, now) {
|
|
2617
|
+
const reads = within(over.get(key) ?? [], now, 6e4);
|
|
2618
|
+
if (reads.length) over.set(key, reads);
|
|
2619
|
+
else over.delete(key);
|
|
2620
|
+
return reads.length;
|
|
2621
|
+
}
|
|
2622
|
+
/** One more, the keys with none in the last minute dropped. */
|
|
2623
|
+
overAdd(over, key, now) {
|
|
2624
|
+
for (const [other, reads] of over) if (now - reads[reads.length - 1] >= 6e4) over.delete(other);
|
|
2625
|
+
over.set(key, [...over.get(key) ?? [], now]);
|
|
2626
|
+
}
|
|
2627
|
+
/** A chat's request of `key` asked urgently (it needs every relay), or the budget refused it on `relay` (it needs that one). */
|
|
2628
|
+
chatNeed(key, now, relay) {
|
|
2629
|
+
if (relay !== void 0) {
|
|
2630
|
+
this.chatNeedsOn(relay, now).set(key, now);
|
|
2631
|
+
return;
|
|
2632
|
+
}
|
|
2633
|
+
this.chatUrgentAt.set(key, now);
|
|
2634
|
+
for (const needs of this.chatNeeds.values()) needs.set(key, now);
|
|
2635
|
+
}
|
|
2636
|
+
/** The chats that need this relay: those a request of was refused there, and the urgent ones. */
|
|
2637
|
+
chatNeedsOn(relay, now) {
|
|
2638
|
+
let needs = this.chatNeeds.get(relay);
|
|
2639
|
+
if (!needs) {
|
|
2640
|
+
this.chatNeeds.set(relay, needs = new RecentKeys());
|
|
2641
|
+
for (const [key, at] of this.chatUrgentAt.entries(now)) needs.set(key, at);
|
|
2642
|
+
}
|
|
2643
|
+
return needs;
|
|
2644
|
+
}
|
|
2645
|
+
/** The chat reading `key` has nothing due: it needs no relay any more. */
|
|
2646
|
+
chatNeedLapsed(key) {
|
|
2647
|
+
this.chatUrgentAt.delete(key);
|
|
2648
|
+
for (const needs of this.chatNeeds.values()) needs.delete(key);
|
|
2649
|
+
}
|
|
2650
|
+
/**
|
|
2651
|
+
* How long groups still leave a chat its reserve on this relay (`CHAT_RESERVE`); a chat's need a minute old goes.
|
|
2652
|
+
* Kept up to date as needs come and go (`RecentKeys`): walking them all for each request cost a daemon with 500
|
|
2653
|
+
* contacts away 2.7 s of CPU per 10 idle minutes (bug hunt, 2026-11-01).
|
|
2654
|
+
*/
|
|
2655
|
+
chatReservedFor(relay, now) {
|
|
2656
|
+
return this.chatNeedsOn(relay, now).latest(now) + 6e4 - now;
|
|
2657
|
+
}
|
|
2658
|
+
/**
|
|
2659
|
+
* How long a chat's packet another relay already took waits for this one: while chats need the relays, a copy takes
|
|
2660
|
+
* none of the requests their reserve keeps (`chatReserveOf`), as a group's request does not. 0 when it goes now.
|
|
2661
|
+
*/
|
|
2662
|
+
spareHeldFor(relay, now) {
|
|
2663
|
+
const reserved = this.chatReservedFor(relay, now);
|
|
2664
|
+
if (reserved <= 0) return 0;
|
|
2665
|
+
const recent = within(this.spent.get(relay) ?? [], now, 6e4);
|
|
2666
|
+
const keep = this.limitOf(relay) - this.chatReserveOf(relay, now);
|
|
2667
|
+
return recent.length >= keep ? Math.min(reserved, recent[recent.length - keep] + 6e4 - now) : 0;
|
|
2668
|
+
}
|
|
2669
|
+
/** Requests allowed to this relay a minute: this client's budget, or the relay's own share (`RELAY_REQUESTS_PER_MINUTE`). */
|
|
2670
|
+
limitOf(relay) {
|
|
2671
|
+
const share = RELAY_REQUESTS_PER_MINUTE[relay];
|
|
2672
|
+
if (share === void 0) return this.perMinute;
|
|
2673
|
+
return this.perMinuteGiven ? Math.min(this.perMinute, share) : share;
|
|
2674
|
+
}
|
|
2675
|
+
/** What the chats that need the relays keep on this relay (`CHAT_RESERVE_PER_CHAT`), never less than `reserveOf`. */
|
|
2676
|
+
chatReserveOf(relay, now) {
|
|
2677
|
+
const needing = this.chatNeedsOn(relay, now).size(now);
|
|
2678
|
+
return Math.max(this.reserveOf(relay), Math.min(needing * 3, Math.floor(this.limitOf(relay) * CHAT_RESERVE_MOST_SHARE)));
|
|
2679
|
+
}
|
|
2680
|
+
/** What groups leave a chat on this relay: `CHAT_RESERVE`, or a third of a smaller budget. */
|
|
2681
|
+
reserveOf(relay) {
|
|
2682
|
+
return Math.min(10, Math.floor(this.limitOf(relay) / 3));
|
|
2683
|
+
}
|
|
2684
|
+
isCoolingDown(relay, method) {
|
|
2685
|
+
return this.rateLimitedFor(relay) > 0 || this.networkCoolingDown(relay, method);
|
|
2686
|
+
}
|
|
2687
|
+
/** Left alone after failing at the network level. */
|
|
2688
|
+
networkCoolingDown(relay, method) {
|
|
2689
|
+
return (this.networkCooldown.get(`${method} ${relay}`) ?? 0) > Date.now();
|
|
2690
|
+
}
|
|
2691
|
+
/** How much longer the relay asked (429) to be left alone; 0 when it did not. */
|
|
2692
|
+
rateLimitedFor(relay) {
|
|
2693
|
+
return Math.max(0, (this.coolingDown.get(relay) ?? 0) - Date.now());
|
|
2694
|
+
}
|
|
2695
|
+
/** Leaves the relay alone for as long as its 429 asks; returns that, in ms. */
|
|
2696
|
+
coolDown(relay, response) {
|
|
2697
|
+
const retryAfter = Number.parseInt(response.headers.get("retry-after") ?? "", 10);
|
|
2698
|
+
const seconds = Number.isFinite(retryAfter) ? Math.min(Math.max(retryAfter, 1), 120) : 15;
|
|
2699
|
+
this.coolingDown.set(relay, Date.now() + seconds * 1e3);
|
|
2700
|
+
return seconds * 1e3;
|
|
2701
|
+
}
|
|
2702
|
+
/**
|
|
2703
|
+
* One request under one time limit. `read` runs before the timer is cleared, so a body read there is bounded in
|
|
2704
|
+
* time as well: a relay that sends its headers and then trickles, or never ends, is given up on like one that
|
|
2705
|
+
* never answered.
|
|
2706
|
+
*/
|
|
2707
|
+
/**
|
|
2708
|
+
* The relay's own time, when its answer says it and this runtime may read it: Node and the Desktop can, a browser
|
|
2709
|
+
* only when the relay exposes the header to other origins. Held against this device's clock (`ClockWatch`).
|
|
2710
|
+
*/
|
|
2711
|
+
serverTime(url, response, sent) {
|
|
2712
|
+
if (!this.timeListeners.size) return;
|
|
2713
|
+
let date = NaN;
|
|
2714
|
+
try {
|
|
2715
|
+
if (Number(response.headers.get("age") ?? 0) > 0) return;
|
|
2716
|
+
date = Date.parse(response.headers.get("date") ?? "");
|
|
2717
|
+
} catch {}
|
|
2718
|
+
if (!Number.isFinite(date)) return;
|
|
2719
|
+
const time = {
|
|
2720
|
+
source: new URL(url).origin,
|
|
2721
|
+
date,
|
|
2722
|
+
sent,
|
|
2723
|
+
received: Date.now()
|
|
2724
|
+
};
|
|
2725
|
+
for (const listener of this.timeListeners) try {
|
|
2726
|
+
listener(time);
|
|
2727
|
+
} catch {}
|
|
2728
|
+
}
|
|
2729
|
+
onServerTime(listener) {
|
|
2730
|
+
this.timeListeners.add(listener);
|
|
2731
|
+
return () => this.timeListeners.delete(listener);
|
|
2732
|
+
}
|
|
2733
|
+
/**
|
|
2734
|
+
* The turn record's read (WISP 06 § Publishing and reading): every relay, in parallel, `TURN_SOURCE_TIMEOUT_MS`
|
|
2735
|
+
* each. Unlike `resolve`, it does not stop at the first relay that answers, keeps nothing, and answers nothing from
|
|
2736
|
+
* memory. Each request counts in the relay's minute, so chats see it, but is never held back by it: a device may
|
|
2737
|
+
* not act before it has read its turn.
|
|
2738
|
+
*/
|
|
2739
|
+
turnRead(pubKeyZ32, options = {}) {
|
|
2740
|
+
const timeoutMs = Math.min(options.timeoutMs ?? 8e3, TURN_SOURCE_TIMEOUT_MS);
|
|
2741
|
+
return Promise.all(this.relays.map(async (relay) => {
|
|
2742
|
+
try {
|
|
2743
|
+
let payload;
|
|
2744
|
+
const read = async (r, signal) => {
|
|
2745
|
+
if (r.ok) payload = await readRelayBody(r, signal);
|
|
2746
|
+
};
|
|
2747
|
+
let response = await this.turnRequest(relay, `${pubKeyZ32}?policy=NetworkOnly`, { method: "GET" }, read, timeoutMs);
|
|
2748
|
+
const stale = response.status === 400;
|
|
2749
|
+
if (stale) response = await this.turnRequest(relay, pubKeyZ32, { method: "GET" }, read, timeoutMs);
|
|
2750
|
+
const age = stale ? { stale: true } : {};
|
|
2751
|
+
if (response.status === 404) {
|
|
2752
|
+
const invalid = response.headers.get(INVALID_PACKET_SEQ_HEADER);
|
|
2753
|
+
return {
|
|
2754
|
+
source: relay,
|
|
2755
|
+
answered: true,
|
|
2756
|
+
payloads: [],
|
|
2757
|
+
...age,
|
|
2758
|
+
...invalid && /^\d{1,19}$/.test(invalid) ? { sequences: [invalid] } : {}
|
|
2759
|
+
};
|
|
2760
|
+
}
|
|
2761
|
+
if (!response.ok || !payload) return {
|
|
2762
|
+
source: relay,
|
|
2763
|
+
answered: false,
|
|
2764
|
+
payloads: [],
|
|
2765
|
+
detail: `HTTP ${response.status}`
|
|
2766
|
+
};
|
|
2767
|
+
return {
|
|
2768
|
+
source: relay,
|
|
2769
|
+
answered: true,
|
|
2770
|
+
payloads: [payload],
|
|
2771
|
+
...age
|
|
2772
|
+
};
|
|
2773
|
+
} catch (error) {
|
|
2774
|
+
return {
|
|
2775
|
+
source: relay,
|
|
2776
|
+
answered: false,
|
|
2777
|
+
payloads: [],
|
|
2778
|
+
detail: error instanceof Error ? error.message : String(error)
|
|
2779
|
+
};
|
|
2780
|
+
}
|
|
2781
|
+
}));
|
|
2782
|
+
}
|
|
2783
|
+
/**
|
|
2784
|
+
* The turn record's put: the bytes as given, to each relay `conditions` names, with `If-Match` on the sequence that
|
|
2785
|
+
* relay is known to hold (none where it held no record). Unlike `putEverywhere`, a relay that refuses (409, 412,
|
|
2786
|
+
* 428) is not asked again without the condition: its refusal is the answer, and every relay's answer is reported.
|
|
2787
|
+
*/
|
|
2788
|
+
turnPut(pubKeyZ32, payload, conditions) {
|
|
2789
|
+
const relays = this.relays.filter((relay) => conditions[relay] !== void 0);
|
|
2790
|
+
return Promise.all(relays.map(async (relay) => {
|
|
2791
|
+
const replaces = conditions[relay];
|
|
2792
|
+
try {
|
|
2793
|
+
const response = await this.turnRequest(relay, pubKeyZ32, {
|
|
2794
|
+
method: "PUT",
|
|
2795
|
+
body: payload,
|
|
2796
|
+
headers: replaces === null ? void 0 : { "If-Match": replaces }
|
|
2797
|
+
});
|
|
2798
|
+
if (response.ok) return {
|
|
2799
|
+
source: relay,
|
|
2800
|
+
outcome: "stored",
|
|
2801
|
+
detail: `HTTP ${response.status}`
|
|
2802
|
+
};
|
|
2803
|
+
return {
|
|
2804
|
+
source: relay,
|
|
2805
|
+
outcome: TURN_REFUSALS.includes(response.status) ? "refused" : "failed",
|
|
2806
|
+
detail: `HTTP ${response.status}`
|
|
2807
|
+
};
|
|
2808
|
+
} catch (error) {
|
|
2809
|
+
return {
|
|
2810
|
+
source: relay,
|
|
2811
|
+
outcome: "failed",
|
|
2812
|
+
detail: error instanceof Error ? error.message : String(error)
|
|
2813
|
+
};
|
|
2814
|
+
}
|
|
2815
|
+
}));
|
|
2816
|
+
}
|
|
2817
|
+
async turnRequest(relay, path, init, read, timeoutMs = TURN_SOURCE_TIMEOUT_MS) {
|
|
2818
|
+
this.spent.get(relay)?.push(Date.now());
|
|
2819
|
+
const controller = new AbortController();
|
|
2820
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
2821
|
+
try {
|
|
2822
|
+
const response = await this.fetchFn(`${relay}/${path}`, {
|
|
2823
|
+
...init,
|
|
2824
|
+
cache: "no-store",
|
|
2825
|
+
signal: controller.signal
|
|
2826
|
+
});
|
|
2827
|
+
await read?.(response, controller.signal);
|
|
2828
|
+
return response;
|
|
2829
|
+
} finally {
|
|
2830
|
+
clearTimeout(timer);
|
|
2831
|
+
}
|
|
2832
|
+
}
|
|
2833
|
+
/** `cancel`: this client drops the request (a hedged read another relay answered first). */
|
|
2834
|
+
async request(url, init, read, cancel) {
|
|
2835
|
+
const controller = new AbortController();
|
|
2836
|
+
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
2837
|
+
const drop = () => controller.abort();
|
|
2838
|
+
cancel?.addEventListener("abort", drop, { once: true });
|
|
2839
|
+
const relay = url.slice(0, url.lastIndexOf("/")), request = { at: Date.now() };
|
|
2840
|
+
const waiting = this.waiting.get(relay) ?? /* @__PURE__ */ new Set();
|
|
2841
|
+
this.waiting.set(relay, waiting.add(request));
|
|
2842
|
+
try {
|
|
2843
|
+
const sent = Date.now();
|
|
2844
|
+
const response = await this.fetchFn(url, {
|
|
2845
|
+
...init,
|
|
2846
|
+
cache: "no-store",
|
|
2847
|
+
signal: controller.signal
|
|
2848
|
+
});
|
|
2849
|
+
this.answeredAt.set(relay, Date.now());
|
|
2850
|
+
this.serverTime(url, response, sent);
|
|
2851
|
+
await read?.(response, controller.signal);
|
|
2852
|
+
return response;
|
|
2853
|
+
} finally {
|
|
2854
|
+
waiting.delete(request);
|
|
2855
|
+
clearTimeout(timer);
|
|
2856
|
+
cancel?.removeEventListener("abort", drop);
|
|
2857
|
+
}
|
|
2858
|
+
}
|
|
2859
|
+
};
|
|
2860
|
+
/**
|
|
2861
|
+
* A relay's answer body, at most RELAY_PAYLOAD_MAX_BYTES: refused on a larger `content-length` before anything is
|
|
2862
|
+
* read, and otherwise streamed with a running count, so a relay (a default one or one the person added, working or
|
|
2863
|
+
* compromised) cannot fill memory with a huge or endless body. Stops when `signal` aborts.
|
|
2864
|
+
*/
|
|
2865
|
+
async function readRelayBody(response, signal, max = RELAY_PAYLOAD_MAX_BYTES) {
|
|
2866
|
+
const tooLarge = () => /* @__PURE__ */ new Error(`Relay answer is larger than ${max} bytes`);
|
|
2867
|
+
if (Number(response.headers.get("content-length")) > max) {
|
|
2868
|
+
response.body?.cancel().catch(() => {});
|
|
2869
|
+
throw tooLarge();
|
|
2870
|
+
}
|
|
2871
|
+
if (!response.body) return /* @__PURE__ */ new Uint8Array(0);
|
|
2872
|
+
const reader = response.body.getReader();
|
|
2873
|
+
const stop = () => {
|
|
2874
|
+
reader.cancel().catch(() => {});
|
|
2875
|
+
};
|
|
2876
|
+
signal.addEventListener("abort", stop, { once: true });
|
|
2877
|
+
try {
|
|
2878
|
+
const chunks = [];
|
|
2879
|
+
let length = 0;
|
|
2880
|
+
for (;;) {
|
|
2881
|
+
if (signal.aborted) throw new Error("Relay answer timed out");
|
|
2882
|
+
const { done, value } = await reader.read();
|
|
2883
|
+
if (signal.aborted) throw new Error("Relay answer timed out");
|
|
2884
|
+
if (done) break;
|
|
2885
|
+
length += value.byteLength;
|
|
2886
|
+
if (length > max) {
|
|
2887
|
+
stop();
|
|
2888
|
+
throw tooLarge();
|
|
2889
|
+
}
|
|
2890
|
+
chunks.push(value);
|
|
2891
|
+
}
|
|
2892
|
+
const body = new Uint8Array(length);
|
|
2893
|
+
let at = 0;
|
|
2894
|
+
for (const chunk of chunks) {
|
|
2895
|
+
body.set(chunk, at);
|
|
2896
|
+
at += chunk.byteLength;
|
|
2897
|
+
}
|
|
2898
|
+
return body;
|
|
2899
|
+
} finally {
|
|
2900
|
+
signal.removeEventListener("abort", stop);
|
|
2901
|
+
}
|
|
2902
|
+
}
|
|
2903
|
+
// ../core/src/identityTimeline.ts
|
|
2904
|
+
/** Entries a chat keeps, newest last. */
|
|
2905
|
+
var IDENTITY_TIMELINE_MAX = 100;
|
|
2906
|
+
var lastOf = (timeline, side, proofs) => {
|
|
2907
|
+
for (let i = timeline.length - 1; i >= 0; i--) if (timeline[i].side === side && proofs.includes(timeline[i].proof)) return i;
|
|
2908
|
+
return -1;
|
|
2909
|
+
};
|
|
2910
|
+
/** The contact's share of this kind still not verified (checking, or failed): the one its next attempt takes over. */
|
|
2911
|
+
var unverifiedOf = (timeline, provider) => {
|
|
2912
|
+
for (let i = timeline.length - 1; i >= 0; i--) {
|
|
2913
|
+
const e = timeline[i];
|
|
2914
|
+
if (e.side === "theirs" && e.kind === "shared" && e.state !== "verified" && e.provider === provider) return i;
|
|
2915
|
+
}
|
|
2916
|
+
return -1;
|
|
2917
|
+
};
|
|
2918
|
+
/**
|
|
2919
|
+
* The timeline after one step. A share already on it is updated in place, never repeated: a request sent again at
|
|
2920
|
+
* a reconnect, a second share of a proof the contact already verified. A new attempt after a failure or a stop is
|
|
2921
|
+
* a new entry, except the contact's own not verified: those keep one entry per kind, which its next attempt takes
|
|
2922
|
+
* over in place (its id and time stay, so no new sound and the chat does not move). The ids and the kind are the
|
|
2923
|
+
* contact's to choose, so otherwise its requests alone could push every other entry, this side's too, out of the
|
|
2924
|
+
* bound. A stop is written once; a withdrawal later found revoked becomes a revocation in place.
|
|
2925
|
+
*/
|
|
2926
|
+
function applyIdentityEvent(timeline, event, at) {
|
|
2927
|
+
const out = [...timeline];
|
|
2928
|
+
const i = lastOf(out, event.side, event.type === "result" ? [event.proof, event.requested] : [event.proof]);
|
|
2929
|
+
const last = i >= 0 ? out[i] : void 0;
|
|
2930
|
+
/** Puts the contact's attempt on the entry of its kind still not verified, if there is one. */
|
|
2931
|
+
const takeOver = (entry) => {
|
|
2932
|
+
const j = event.side === "theirs" ? unverifiedOf(out, entry.provider) : -1;
|
|
2933
|
+
if (j < 0) return false;
|
|
2934
|
+
out[j] = {
|
|
2935
|
+
id: out[j].id,
|
|
2936
|
+
proof: entry.proof,
|
|
2937
|
+
side: "theirs",
|
|
2938
|
+
kind: "shared",
|
|
2939
|
+
provider: entry.provider,
|
|
2940
|
+
...entry.subject ? { subject: entry.subject } : {},
|
|
2941
|
+
state: entry.state,
|
|
2942
|
+
...entry.error ? { error: entry.error } : {},
|
|
2943
|
+
at: out[j].at
|
|
2944
|
+
};
|
|
2945
|
+
return true;
|
|
2946
|
+
};
|
|
2947
|
+
const add = (entry) => {
|
|
2948
|
+
const prefix = `${entry.side}:${entry.proof}:`;
|
|
2949
|
+
let n = 0;
|
|
2950
|
+
for (const e of out) if (e.id.startsWith(prefix)) n = Math.max(n, Number(e.id.slice(prefix.length)) + 1 || 0);
|
|
2951
|
+
out.push({
|
|
2952
|
+
...entry,
|
|
2953
|
+
id: `${prefix}${n}`,
|
|
2954
|
+
at
|
|
2955
|
+
});
|
|
2956
|
+
};
|
|
2957
|
+
switch (event.type) {
|
|
2958
|
+
case "shared":
|
|
2959
|
+
if (last?.kind === "shared" && last.state !== "failed") {
|
|
2960
|
+
out[i] = {
|
|
2961
|
+
...last,
|
|
2962
|
+
provider: event.provider,
|
|
2963
|
+
subject: event.subject ?? last.subject
|
|
2964
|
+
};
|
|
2965
|
+
break;
|
|
2966
|
+
}
|
|
2967
|
+
if (takeOver({
|
|
2968
|
+
proof: event.proof,
|
|
2969
|
+
provider: event.provider,
|
|
2970
|
+
subject: event.subject,
|
|
2971
|
+
state: "verifying"
|
|
2972
|
+
})) break;
|
|
2973
|
+
add({
|
|
2974
|
+
proof: event.proof,
|
|
2975
|
+
side: event.side,
|
|
2976
|
+
kind: "shared",
|
|
2977
|
+
provider: event.provider,
|
|
2978
|
+
...event.subject ? { subject: event.subject } : {},
|
|
2979
|
+
state: "verifying"
|
|
2980
|
+
});
|
|
2981
|
+
break;
|
|
2982
|
+
case "result": {
|
|
2983
|
+
const settled = {
|
|
2984
|
+
state: event.ok ? "verified" : "failed",
|
|
2985
|
+
...event.ok || !event.error ? {} : { error: event.error }
|
|
2986
|
+
};
|
|
2987
|
+
if (last?.kind === "shared") {
|
|
2988
|
+
const { error: _, ...rest } = last;
|
|
2989
|
+
out[i] = {
|
|
2990
|
+
...rest,
|
|
2991
|
+
proof: event.proof,
|
|
2992
|
+
provider: event.provider ?? last.provider,
|
|
2993
|
+
subject: event.subject ?? last.subject,
|
|
2994
|
+
...settled
|
|
2995
|
+
};
|
|
2996
|
+
break;
|
|
2997
|
+
}
|
|
2998
|
+
if (!event.ok && event.provider && takeOver({
|
|
2999
|
+
proof: event.proof,
|
|
3000
|
+
provider: event.provider,
|
|
3001
|
+
subject: event.subject,
|
|
3002
|
+
...settled
|
|
3003
|
+
})) break;
|
|
3004
|
+
if (event.provider) add({
|
|
3005
|
+
proof: event.proof,
|
|
3006
|
+
side: event.side,
|
|
3007
|
+
kind: "shared",
|
|
3008
|
+
provider: event.provider,
|
|
3009
|
+
...event.subject ? { subject: event.subject } : {},
|
|
3010
|
+
...settled
|
|
3011
|
+
});
|
|
3012
|
+
break;
|
|
3013
|
+
}
|
|
3014
|
+
case "stopped":
|
|
3015
|
+
if (last?.kind === "stopped") {
|
|
3016
|
+
if (last.reason === "withdrawn" && event.reason === "revoked") out[i] = {
|
|
3017
|
+
...last,
|
|
3018
|
+
reason: "revoked"
|
|
3019
|
+
};
|
|
3020
|
+
break;
|
|
3021
|
+
}
|
|
3022
|
+
add({
|
|
3023
|
+
proof: event.proof,
|
|
3024
|
+
side: event.side,
|
|
3025
|
+
kind: "stopped",
|
|
3026
|
+
provider: event.provider,
|
|
3027
|
+
subject: event.subject ?? last?.subject,
|
|
3028
|
+
reason: event.reason
|
|
3029
|
+
});
|
|
3030
|
+
break;
|
|
3031
|
+
case "unsent":
|
|
3032
|
+
case "unanswered": if (last?.kind === "shared" && last.state === "verifying") out.splice(i, 1);
|
|
3033
|
+
}
|
|
3034
|
+
return out.length > 100 ? out.slice(-100) : out;
|
|
3035
|
+
}
|
|
3036
|
+
/** When the contact last shared an identity here (milliseconds), for the chat list: 0 when never. */
|
|
3037
|
+
function lastSharedWithMe(timeline) {
|
|
3038
|
+
let at = 0;
|
|
3039
|
+
for (const e of timeline ?? []) if (e.side === "theirs" && e.kind === "shared" && e.at > at) at = e.at;
|
|
3040
|
+
return at;
|
|
3041
|
+
}
|
|
3042
|
+
// ../core/src/identityProofs.ts
|
|
3043
|
+
/**
|
|
3044
|
+
* Identity proofs (WISP 300), provider-agnostic half.
|
|
3045
|
+
*
|
|
3046
|
+
* A proof is made once per profile: the external identity (a Nostr key, a domain, an SSH key, an
|
|
3047
|
+
* OpenID Connect account…) signs or attests the canonical **statement** below, which authorizes a
|
|
3048
|
+
* fresh Ed25519 **proof key** that only this profile holds. Sharing it with a contact is then a
|
|
3049
|
+
* challenge-response on that contact's authenticated paired channel: the contact sends a fresh nonce,
|
|
3050
|
+
* the proof key signs a **presentation** binding the proof to both participation keys, the
|
|
3051
|
+
* conversation, the session and that nonce. A copy replayed to anyone else fails: wrong audience,
|
|
3052
|
+
* unknown nonce, and nobody else has the proof key.
|
|
3053
|
+
*
|
|
3054
|
+
* Providers (browser side, `packages/browser/src/proofs/`) only know how to produce and check the
|
|
3055
|
+
* evidence over the statement bytes; everything here is shared, so every provider signs the same bytes.
|
|
3056
|
+
*/
|
|
3057
|
+
var IDENTITY_PROOF_CAPABILITY = "identity-proof/1";
|
|
3058
|
+
/** Domain separation: the first words of every statement. */
|
|
3059
|
+
var IDENTITY_STATEMENT_PREFIX = "Ghostly identity proof v1";
|
|
3060
|
+
/** No binding is valid for longer than this, whatever the provider allows. */
|
|
3061
|
+
var IDENTITY_MAX_VALIDITY = 3456e4;
|
|
3062
|
+
/** A binding issued this far in the future is refused (seconds of clock disagreement tolerated). */
|
|
3063
|
+
var IDENTITY_CLOCK_SKEW = 300;
|
|
3064
|
+
/** A contact's challenge must be answered within this many seconds. */
|
|
3065
|
+
var IDENTITY_CHALLENGE_WINDOW = 300;
|
|
3066
|
+
/** Evidence, serialized as JSON, is at most this many characters. */
|
|
3067
|
+
var IDENTITY_MAX_EVIDENCE = 16384;
|
|
3068
|
+
/** A whole `idp-*` frame, serialized. */
|
|
3069
|
+
var IDENTITY_MAX_FRAME = 32768;
|
|
3070
|
+
/** Unanswered challenges a verifier keeps per conversation. */
|
|
3071
|
+
var MAX_CHALLENGES = 8;
|
|
3072
|
+
/** A challenge still open at `now` (seconds): a presentation for it is still taken. */
|
|
3073
|
+
var liveAt = (c, now) => c.issuedAt > now - 300;
|
|
3074
|
+
/**
|
|
3075
|
+
* Proofs a verifier keeps from one contact. A newer proof of the same identity replaces the older one, and one no longer
|
|
3076
|
+
* standing (withdrawn, revoked, expired) gives its place to a new one; past that a presentation is refused before it is
|
|
3077
|
+
* checked, so a contact cannot make this side store, re-send to its pages and look up its proofs without end.
|
|
3078
|
+
*/
|
|
3079
|
+
var IDENTITY_MAX_RECEIVED = 16;
|
|
3080
|
+
/** `idp-*` frames a paired session reads from the contact per `IDENTITY_FRAME_WINDOW_MS`; the rest are dropped unread. */
|
|
3081
|
+
var IDENTITY_FRAME_LIMIT = 40;
|
|
3082
|
+
var IDENTITY_FRAME_WINDOW_MS = 6e4;
|
|
3083
|
+
/**
|
|
3084
|
+
* A proof this side holds verified, presented again with the same evidence between the same keys, is answered from that
|
|
3085
|
+
* check while it is younger than this (seconds): the contact chooses how often it presents, and each check costs this
|
|
3086
|
+
* device a provider lookup (a forge's per-IP quota, a DNS query, a contact's server) and a Pkarr read. An hour keeps an
|
|
3087
|
+
* answer as fresh as a re-share can expect; past it, and between, the daily re-check (or Check again) asks the provider.
|
|
3088
|
+
*/
|
|
3089
|
+
var IDENTITY_REUSE_CHECK_S = 3600;
|
|
3090
|
+
/**
|
|
3091
|
+
* Presentations a verifier checks with their provider per contact in an hour; past it the contact is told the check
|
|
3092
|
+
* could not run. Room for every proof it may keep (`IDENTITY_MAX_RECEIVED`) and a few retries: a contact making fresh
|
|
3093
|
+
* proofs cannot spend a forge's per-IP quota (GitHub: 60 an hour) for every chat and app on this network.
|
|
3094
|
+
*/
|
|
3095
|
+
var IDENTITY_MAX_CHECKS_PER_HOUR = 20;
|
|
3096
|
+
var IDENTITY_PROVIDER_ID = /^[a-z][a-z0-9-]{0,31}$/;
|
|
3097
|
+
/** Printable ASCII without spaces, so the statement can be built but never needs parsing. */
|
|
3098
|
+
var IDENTITY_SUBJECT = /^[\x21-\x7e]{1,512}$/;
|
|
3099
|
+
var BINDING_NONCE = /^[A-Za-z0-9_-]{22}$/;
|
|
3100
|
+
var CHALLENGE_NONCE = /^[A-Za-z0-9_-]{43}$/;
|
|
3101
|
+
var HEX64 = /^[a-f0-9]{64}$/;
|
|
3102
|
+
var SIG = /^[A-Za-z0-9_-]{86}$/;
|
|
3103
|
+
/**
|
|
3104
|
+
* Early revocation: the proof key publishes, under its own Pkarr key, a TXT record naming the proof it
|
|
3105
|
+
* revokes. Anyone holding a copy can look it up by the key in the binding; nothing else is published,
|
|
3106
|
+
* and the key is random, so the record says nothing about the identity or its contacts.
|
|
3107
|
+
*/
|
|
3108
|
+
var IDENTITY_REVOCATION_LABEL = "_ghostly-revoked";
|
|
3109
|
+
var identityRevocationValue = (id, at) => `v=1;id=${id};at=${at}`;
|
|
3110
|
+
/** True when these records (resolved and signature-checked under the proof key) revoke proof `id`. */
|
|
3111
|
+
function revokesIdentity(records, id) {
|
|
3112
|
+
return records.some((r) => r.label === "_ghostly-revoked" && new RegExp(`^v=1;id=${id};at=\\d{1,12}$`).test(r.value));
|
|
3113
|
+
}
|
|
3114
|
+
var nowSeconds = () => Math.floor(Date.now() / 1e3);
|
|
3115
|
+
var hex = (bytes) => Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
3116
|
+
var iso = (seconds) => (/* @__PURE__ */ new Date(seconds * 1e3)).toISOString().replace(/\.\d{3}Z$/, "Z");
|
|
3117
|
+
var isInt = (v) => Number.isSafeInteger(v) && v > 0;
|
|
3118
|
+
function isIdentityBinding(value) {
|
|
3119
|
+
const b = value;
|
|
3120
|
+
return !!b && typeof b === "object" && !Array.isArray(b) && Object.keys(b).sort().join(",") === "expiresAt,issuedAt,key,nonce,provider,subject,v" && b.v === 1 && typeof b.provider === "string" && IDENTITY_PROVIDER_ID.test(b.provider) && typeof b.subject === "string" && IDENTITY_SUBJECT.test(b.subject) && isZ32Key(b.key) && typeof b.nonce === "string" && BINDING_NONCE.test(b.nonce) && isInt(b.issuedAt) && isInt(b.expiresAt) && b.expiresAt > b.issuedAt && b.expiresAt - b.issuedAt <= 3456e4 && b.expiresAt < 253402300800;
|
|
3121
|
+
}
|
|
3122
|
+
/**
|
|
3123
|
+
* The canonical statement, one line, so it survives every tool and clipboard (no line-ending
|
|
3124
|
+
* ambiguity). Every field is space-free and the order is fixed, so the text is injective.
|
|
3125
|
+
*/
|
|
3126
|
+
function identityStatementText(b) {
|
|
3127
|
+
if (!isIdentityBinding(b)) throw new Error("Invalid identity binding");
|
|
3128
|
+
return `${IDENTITY_STATEMENT_PREFIX}: I control ${b.provider}:${b.subject} and authorize the Ghostly key ${b.key} to present it to contacts I choose, from ${iso(b.issuedAt)} until ${iso(b.expiresAt)}. Nonce: ${b.nonce}`;
|
|
3129
|
+
}
|
|
3130
|
+
function identityStatement(binding) {
|
|
3131
|
+
const text = identityStatementText(binding);
|
|
3132
|
+
const bytes = utf8Encode(text);
|
|
3133
|
+
return {
|
|
3134
|
+
binding: { ...binding },
|
|
3135
|
+
text,
|
|
3136
|
+
bytes,
|
|
3137
|
+
id: hex(sha256(bytes))
|
|
3138
|
+
};
|
|
3139
|
+
}
|
|
3140
|
+
/** A new binding and the seed of its proof key. The seed stays in this profile; never share it. */
|
|
3141
|
+
function newIdentityBinding(options) {
|
|
3142
|
+
const issuedAt = options.now ?? nowSeconds();
|
|
3143
|
+
const seed = randomBytes(32);
|
|
3144
|
+
const binding = {
|
|
3145
|
+
v: 1,
|
|
3146
|
+
provider: options.provider,
|
|
3147
|
+
subject: options.subject,
|
|
3148
|
+
key: identityFromSeed(seed).pubKeyZ32,
|
|
3149
|
+
issuedAt,
|
|
3150
|
+
expiresAt: issuedAt + options.validitySeconds,
|
|
3151
|
+
nonce: toBase64Url(randomBytes(16))
|
|
3152
|
+
};
|
|
3153
|
+
if (!isIdentityBinding(binding)) {
|
|
3154
|
+
seed.fill(0);
|
|
3155
|
+
throw new Error("Invalid identity provider, subject or validity");
|
|
3156
|
+
}
|
|
3157
|
+
return {
|
|
3158
|
+
binding,
|
|
3159
|
+
seed
|
|
3160
|
+
};
|
|
3161
|
+
}
|
|
3162
|
+
/** Bytes the proof key signs for one contact. `scope` is the presenter's view (subject = presenter). */
|
|
3163
|
+
function identityPresentationBytes(proofId, scope, nonce, issuedAt) {
|
|
3164
|
+
return utf8Encode(JSON.stringify([
|
|
3165
|
+
"ghostly-identity-presentation",
|
|
3166
|
+
1,
|
|
3167
|
+
proofId,
|
|
3168
|
+
scope.subject,
|
|
3169
|
+
scope.audience,
|
|
3170
|
+
scope.context,
|
|
3171
|
+
scope.session,
|
|
3172
|
+
nonce,
|
|
3173
|
+
issuedAt
|
|
3174
|
+
]));
|
|
3175
|
+
}
|
|
3176
|
+
function signIdentityPresentation(seed, proofId, scope, nonce, issuedAt) {
|
|
3177
|
+
return toBase64Url(sign(identityPresentationBytes(proofId, scope, nonce, issuedAt), seed));
|
|
3178
|
+
}
|
|
3179
|
+
function verifyIdentityPresentation(key, sig, proofId, scope, nonce, issuedAt) {
|
|
3180
|
+
if (typeof sig !== "string" || !SIG.test(sig) || !isZ32Key(key)) return false;
|
|
3181
|
+
return verify(fromBase64Url(sig), identityPresentationBytes(proofId, scope, nonce, issuedAt), publicKeyFromZ32(key));
|
|
3182
|
+
}
|
|
3183
|
+
var clean = (value, max) => {
|
|
3184
|
+
if (typeof value !== "string") return void 0;
|
|
3185
|
+
const text = stripHidden(value).trim();
|
|
3186
|
+
return text && hasVisible(text) ? text.slice(0, max) : void 0;
|
|
3187
|
+
};
|
|
3188
|
+
/**
|
|
3189
|
+
* Room for a line that names a domain (up to 253 characters) once or twice: a How line or a reason cut inside the name
|
|
3190
|
+
* names a record that does not exist, and the app no longer knows the reason to say it in the person's language.
|
|
3191
|
+
*/
|
|
3192
|
+
var SOURCE_MAX = 640;
|
|
3193
|
+
var ERROR_MAX = 512;
|
|
3194
|
+
/** Bounds a provider's result so a buggy provider cannot store or show arbitrary data. */
|
|
3195
|
+
function boundVerifiedIdentity(v) {
|
|
3196
|
+
if (!v || typeof v.subject !== "string" || !IDENTITY_SUBJECT.test(v.subject)) throw new Error("Provider returned an invalid subject");
|
|
3197
|
+
const source = clean(v.source, SOURCE_MAX);
|
|
3198
|
+
if (!source) throw new Error("Provider did not say how it verified");
|
|
3199
|
+
const out = {
|
|
3200
|
+
subject: v.subject,
|
|
3201
|
+
source
|
|
3202
|
+
};
|
|
3203
|
+
const attester = clean(v.attester, 253);
|
|
3204
|
+
if (attester) out.attester = attester;
|
|
3205
|
+
if (v.expiresAt !== void 0) {
|
|
3206
|
+
if (!isInt(v.expiresAt)) throw new Error("Provider returned an invalid expiry");
|
|
3207
|
+
out.expiresAt = v.expiresAt;
|
|
3208
|
+
}
|
|
3209
|
+
if (v.display) out.display = boundIdentityDisplay(v.display);
|
|
3210
|
+
return out;
|
|
3211
|
+
}
|
|
3212
|
+
/** Bounds a provider's public name/picture (from a proof or `lookupDisplay`): a name with nothing visible left is dropped. */
|
|
3213
|
+
function boundIdentityDisplay(d) {
|
|
3214
|
+
const display = {
|
|
3215
|
+
source: clean(d.source, 120) ?? "Self-described",
|
|
3216
|
+
fetchedAt: isInt(d.fetchedAt) ? d.fetchedAt : nowSeconds()
|
|
3217
|
+
};
|
|
3218
|
+
const name = clean(d.name, 64);
|
|
3219
|
+
if (name) display.name = name;
|
|
3220
|
+
if (typeof d.avatar === "string" && d.avatar.length <= 49152 && /^data:image\/(png|jpeg);base64,[A-Za-z0-9+/=]+$/.test(d.avatar)) display.avatar = d.avatar;
|
|
3221
|
+
if (typeof d.url === "string" && d.url.length <= 512) try {
|
|
3222
|
+
const u = new URL(d.url);
|
|
3223
|
+
if (u.protocol === "https:" && !u.username && !u.password) display.url = u.href;
|
|
3224
|
+
} catch {}
|
|
3225
|
+
return display;
|
|
3226
|
+
}
|
|
3227
|
+
var emptyIdentityLedger = () => ({
|
|
3228
|
+
challenges: [],
|
|
3229
|
+
shared: [],
|
|
3230
|
+
received: []
|
|
3231
|
+
});
|
|
3232
|
+
/** What the verifier should show for a received proof right now. */
|
|
3233
|
+
function receivedIdentityStatus(r, presenter, audience, now = nowSeconds()) {
|
|
3234
|
+
if (r.presenter !== presenter || r.audience !== audience) return "previous-key";
|
|
3235
|
+
if (r.status === "withdrawn") return "withdrawn";
|
|
3236
|
+
if (r.status === "revoked") return "revoked";
|
|
3237
|
+
if (Math.min(r.binding.expiresAt, r.verified.expiresAt ?? Infinity) <= now) return "expired";
|
|
3238
|
+
return r.status === "unconfirmed" ? "unconfirmed" : "verified";
|
|
3239
|
+
}
|
|
3240
|
+
var standing = (r, now) => r.status !== "withdrawn" && r.status !== "revoked" && Math.min(r.binding.expiresAt, r.verified.expiresAt ?? Infinity) > now;
|
|
3241
|
+
/**
|
|
3242
|
+
* `received` with room for one more proof: `replaced` (the older proof of the same identity) dropped, then the oldest ones
|
|
3243
|
+
* no longer standing; undefined when every place is still taken.
|
|
3244
|
+
*/
|
|
3245
|
+
function receivedWithRoom(received, id, replaced, now) {
|
|
3246
|
+
const kept = received.filter((r) => r.id !== id && !replaced(r));
|
|
3247
|
+
const gone = kept.filter((r) => !standing(r, now)).sort((a, b) => a.verifiedAt - b.verifiedAt);
|
|
3248
|
+
const drop = new Set(gone.slice(0, Math.max(0, kept.length - 16 + 1)));
|
|
3249
|
+
const out = kept.filter((r) => !drop.has(r));
|
|
3250
|
+
return out.length < 16 ? out : void 0;
|
|
3251
|
+
}
|
|
3252
|
+
var REVOKED = "Its owner revoked this proof";
|
|
3253
|
+
/** Revoked is final: a later lookup that fails or finds nothing (the relays forgot it) does not undo it. */
|
|
3254
|
+
var revokedHere = (received, id) => received.some((r) => r.id === id && r.status === "revoked");
|
|
3255
|
+
var shortError = (e) => clean(e instanceof Error ? e.message : String(e), ERROR_MAX) ?? "Invalid proof";
|
|
3256
|
+
/**
|
|
3257
|
+
* A check this side could not finish because of what its own network did: a host it refused to contact, a server
|
|
3258
|
+
* that did not answer or answered an error. The message is for this side only. The contact is told the proof could
|
|
3259
|
+
* not be checked, never which host, address or status was seen, so a proof cannot make a contact's app report on
|
|
3260
|
+
* the network it sits in.
|
|
3261
|
+
*/
|
|
3262
|
+
var IdentityCheckUnavailable = class extends Error {
|
|
3263
|
+
constructor(message) {
|
|
3264
|
+
super(message);
|
|
3265
|
+
this.name = "IdentityCheckUnavailable";
|
|
3266
|
+
}
|
|
3267
|
+
};
|
|
3268
|
+
/** What the contact is told when the check failed with an IdentityCheckUnavailable. */
|
|
3269
|
+
var IDENTITY_CHECK_UNAVAILABLE = "Your contact's app could not check it. Try again later.";
|
|
3270
|
+
var TOO_MANY_RECEIVED = "Your contact keeps too many identities from you already";
|
|
3271
|
+
var NEWER_HELD = "Your contact keeps a newer proof of this identity from you";
|
|
3272
|
+
var peerError = (e) => e instanceof IdentityCheckUnavailable ? IDENTITY_CHECK_UNAVAILABLE : shortError(e);
|
|
3273
|
+
var UNSUPPORTED_KIND = "Your contact's app cannot verify this kind of identity yet";
|
|
3274
|
+
var NOT_IN_PROFILE = "That identity is no longer in your profile";
|
|
3275
|
+
var CONTACT_REFUSED = "Your contact could not verify it";
|
|
3276
|
+
var EXPIRED = "The proof has expired";
|
|
3277
|
+
/** The refusals a contact's app writes itself (onRequest, onPresent): the only words of its `idp-result` kept as they came. */
|
|
3278
|
+
var CONTACT_REFUSALS = /* @__PURE__ */ new Set([
|
|
3279
|
+
UNSUPPORTED_KIND,
|
|
3280
|
+
IDENTITY_CHECK_UNAVAILABLE,
|
|
3281
|
+
TOO_MANY_RECEIVED,
|
|
3282
|
+
REVOKED,
|
|
3283
|
+
EXPIRED,
|
|
3284
|
+
NEWER_HELD
|
|
3285
|
+
]);
|
|
3286
|
+
/**
|
|
3287
|
+
* Why a share of mine was not accepted, as this app may say it under my identity: one of the refusals above or my own
|
|
3288
|
+
* app's line; else nothing past "not verified". Any other words came from the contact (a ledger kept before this check)
|
|
3289
|
+
* and would read as this app's own notice.
|
|
3290
|
+
*/
|
|
3291
|
+
var identityRefusal = (error) => typeof error === "string" && (CONTACT_REFUSALS.has(error) || error === NOT_IN_PROFILE) ? error : void 0;
|
|
3292
|
+
/**
|
|
3293
|
+
* The `idp-*` exchange of one conversation. Frames (all JSON, additive, over the paired channel,
|
|
3294
|
+
* only when both offers carry `identity-proof/1`):
|
|
3295
|
+
*
|
|
3296
|
+
* idp-hello {providers} each side, when the channel is ready
|
|
3297
|
+
* idp-request {id, provider} sharer → verifier: "give me a challenge for this proof"
|
|
3298
|
+
* idp-challenge {id, nonce, issuedAt} verifier, after saving the nonce
|
|
3299
|
+
* idp-present {nonce, issuedAt, binding, evidence, sig}
|
|
3300
|
+
* idp-result {id, ok, error?} verifier, after saving the outcome
|
|
3301
|
+
* idp-withdraw {id} / idp-withdrawn {id}
|
|
3302
|
+
*/
|
|
3303
|
+
var IdentityExchange = class {
|
|
3304
|
+
options;
|
|
3305
|
+
peerProviders;
|
|
3306
|
+
requested = /* @__PURE__ */ new Set();
|
|
3307
|
+
/** The nonces of the presentations in their provider's check now. */
|
|
3308
|
+
checking = /* @__PURE__ */ new Set();
|
|
3309
|
+
/** When the contact's presentations were checked with their provider (seconds), for `IDENTITY_MAX_CHECKS_PER_HOUR`. */
|
|
3310
|
+
checks = [];
|
|
3311
|
+
constructor(options) {
|
|
3312
|
+
this.options = options;
|
|
3313
|
+
}
|
|
3314
|
+
now() {
|
|
3315
|
+
return (this.options.now ?? nowSeconds)();
|
|
3316
|
+
}
|
|
3317
|
+
/** The ledger with one more step on the chat's timeline, in milliseconds (a test's clock, in seconds, when given). */
|
|
3318
|
+
log(l, event) {
|
|
3319
|
+
return {
|
|
3320
|
+
...l,
|
|
3321
|
+
timeline: applyIdentityEvent(l.timeline ?? [], event, this.options.now ? this.now() * 1e3 : Date.now())
|
|
3322
|
+
};
|
|
3323
|
+
}
|
|
3324
|
+
incomingScope() {
|
|
3325
|
+
const s = this.options.scope();
|
|
3326
|
+
return {
|
|
3327
|
+
...s,
|
|
3328
|
+
subject: s.audience,
|
|
3329
|
+
audience: s.subject
|
|
3330
|
+
};
|
|
3331
|
+
}
|
|
3332
|
+
send(frame) {
|
|
3333
|
+
if (JSON.stringify(frame).length > 32768) throw new Error("Identity proof too large");
|
|
3334
|
+
this.options.send(frame);
|
|
3335
|
+
}
|
|
3336
|
+
shared(id) {
|
|
3337
|
+
return this.options.storage.read().shared.find((s) => s.id === id);
|
|
3338
|
+
}
|
|
3339
|
+
async setShared(entry, event) {
|
|
3340
|
+
await this.options.storage.update((l) => {
|
|
3341
|
+
const next = {
|
|
3342
|
+
...l,
|
|
3343
|
+
shared: [...l.shared.filter((s) => s.id !== entry.id), entry]
|
|
3344
|
+
};
|
|
3345
|
+
return event ? this.log(next, event) : next;
|
|
3346
|
+
});
|
|
3347
|
+
}
|
|
3348
|
+
/** The providers the contact said it verifies; undefined until its hello. */
|
|
3349
|
+
get contactProviders() {
|
|
3350
|
+
return this.peerProviders;
|
|
3351
|
+
}
|
|
3352
|
+
/** The channel is ready: say what we verify, then send what the person queued while offline. */
|
|
3353
|
+
ready() {
|
|
3354
|
+
this.options.scope();
|
|
3355
|
+
this.send({
|
|
3356
|
+
t: "idp-hello",
|
|
3357
|
+
providers: [...this.options.providers()]
|
|
3358
|
+
});
|
|
3359
|
+
for (const s of this.options.storage.read().shared) if (s.status === "queued" || s.status === "pending") this.request(s);
|
|
3360
|
+
else if (s.status === "withdrawal-pending") this.send({
|
|
3361
|
+
t: "idp-withdraw",
|
|
3362
|
+
id: s.id
|
|
3363
|
+
});
|
|
3364
|
+
}
|
|
3365
|
+
stop() {
|
|
3366
|
+
this.requested.clear();
|
|
3367
|
+
this.peerProviders = void 0;
|
|
3368
|
+
}
|
|
3369
|
+
/**
|
|
3370
|
+
* The challenges whose window ended unanswered go, and with them their requests' lines still verifying: a contact
|
|
3371
|
+
* whose app closed, or whose proof left its profile, before it presented tells this side nothing, and its line read
|
|
3372
|
+
* "verifying" for good. No write when none ended.
|
|
3373
|
+
*/
|
|
3374
|
+
async expire() {
|
|
3375
|
+
const now = this.now();
|
|
3376
|
+
if (!this.options.storage.read().challenges.some((c) => !liveAt(c, now))) return;
|
|
3377
|
+
await this.options.storage.update((l) => this.withoutExpired(l, now));
|
|
3378
|
+
}
|
|
3379
|
+
withoutExpired(l, now) {
|
|
3380
|
+
const live = l.challenges.filter((c) => liveAt(c, now));
|
|
3381
|
+
if (live.length === l.challenges.length) return l;
|
|
3382
|
+
return [...new Set(l.challenges.filter((c) => c.id !== void 0 && !liveAt(c, now) && !live.some((x) => x.id === c.id)).map((c) => c.id))].reduce((out, proof) => this.log(out, {
|
|
3383
|
+
type: "unanswered",
|
|
3384
|
+
side: "theirs",
|
|
3385
|
+
proof
|
|
3386
|
+
}), {
|
|
3387
|
+
...l,
|
|
3388
|
+
challenges: live
|
|
3389
|
+
});
|
|
3390
|
+
}
|
|
3391
|
+
/** Share a proof of this profile with this contact: now if connected, else when it next is. */
|
|
3392
|
+
async share(id, connected) {
|
|
3393
|
+
const local = await this.options.localProof(id);
|
|
3394
|
+
if (!local) throw new Error("That identity is no longer in your profile");
|
|
3395
|
+
const { provider, subject } = local.statement.binding;
|
|
3396
|
+
const entry = {
|
|
3397
|
+
id,
|
|
3398
|
+
provider,
|
|
3399
|
+
subject,
|
|
3400
|
+
account: local.account ?? subject,
|
|
3401
|
+
status: "queued",
|
|
3402
|
+
at: this.now()
|
|
3403
|
+
};
|
|
3404
|
+
await this.setShared(entry, {
|
|
3405
|
+
type: "shared",
|
|
3406
|
+
side: "mine",
|
|
3407
|
+
proof: id,
|
|
3408
|
+
provider,
|
|
3409
|
+
subject
|
|
3410
|
+
});
|
|
3411
|
+
if (connected) this.request(entry);
|
|
3412
|
+
}
|
|
3413
|
+
request(s) {
|
|
3414
|
+
this.requested.add(s.id);
|
|
3415
|
+
this.send({
|
|
3416
|
+
t: "idp-request",
|
|
3417
|
+
id: s.id,
|
|
3418
|
+
provider: s.provider
|
|
3419
|
+
});
|
|
3420
|
+
}
|
|
3421
|
+
/** Stop sharing here. The contact is told (now or on reconnect); a copy it kept cannot be erased. */
|
|
3422
|
+
async withdraw(id, connected) {
|
|
3423
|
+
let s = this.shared(id);
|
|
3424
|
+
if (!s || s.status === "withdrawn") return;
|
|
3425
|
+
this.requested.delete(id);
|
|
3426
|
+
if (s.status === "queued") {
|
|
3427
|
+
let answered;
|
|
3428
|
+
await this.options.storage.update((l) => {
|
|
3429
|
+
answered = l.shared.find((x) => x.id === id && x.status === "pending");
|
|
3430
|
+
return answered ? l : this.log({
|
|
3431
|
+
...l,
|
|
3432
|
+
shared: l.shared.filter((x) => x.id !== id)
|
|
3433
|
+
}, {
|
|
3434
|
+
type: "unsent",
|
|
3435
|
+
side: "mine",
|
|
3436
|
+
proof: id
|
|
3437
|
+
});
|
|
3438
|
+
});
|
|
3439
|
+
if (!answered) return;
|
|
3440
|
+
s = answered;
|
|
3441
|
+
}
|
|
3442
|
+
await this.setShared({
|
|
3443
|
+
...s,
|
|
3444
|
+
status: "withdrawal-pending",
|
|
3445
|
+
at: this.now(),
|
|
3446
|
+
error: void 0
|
|
3447
|
+
}, s.status === "rejected" ? void 0 : {
|
|
3448
|
+
type: "stopped",
|
|
3449
|
+
side: "mine",
|
|
3450
|
+
proof: id,
|
|
3451
|
+
provider: s.provider,
|
|
3452
|
+
subject: s.subject,
|
|
3453
|
+
reason: "withdrawn"
|
|
3454
|
+
});
|
|
3455
|
+
if (connected) try {
|
|
3456
|
+
this.send({
|
|
3457
|
+
t: "idp-withdraw",
|
|
3458
|
+
id
|
|
3459
|
+
});
|
|
3460
|
+
} catch {}
|
|
3461
|
+
}
|
|
3462
|
+
/**
|
|
3463
|
+
* Run the checks again on a received proof: its revocation record, then (unless `revocationOnly`) the
|
|
3464
|
+
* provider's check (a domain record may be gone). `keepUnavailable`: a check that could not run (IdentityCheckUnavailable)
|
|
3465
|
+
* is thrown, and the proof is left as it was, for a check this side started on its own.
|
|
3466
|
+
*/
|
|
3467
|
+
async recheck(id, { revocationOnly = false, keepUnavailable = false } = {}) {
|
|
3468
|
+
const r = this.options.storage.read().received.find((x) => x.id === id);
|
|
3469
|
+
if (!r) throw new Error("Unknown identity");
|
|
3470
|
+
if (r.status === "revoked") return r;
|
|
3471
|
+
if (await this.options.revoked?.(identityStatement(r.binding)).catch(() => false)) return await this.settleRecheck(id, (x) => x.status === "revoked" ? x : {
|
|
3472
|
+
...x,
|
|
3473
|
+
status: "revoked",
|
|
3474
|
+
checkedAt: this.now(),
|
|
3475
|
+
error: void 0
|
|
3476
|
+
}, {
|
|
3477
|
+
type: "stopped",
|
|
3478
|
+
side: "theirs",
|
|
3479
|
+
proof: id,
|
|
3480
|
+
provider: r.binding.provider,
|
|
3481
|
+
subject: r.verified.subject,
|
|
3482
|
+
reason: "revoked"
|
|
3483
|
+
}) ?? r;
|
|
3484
|
+
if (revocationOnly) return r;
|
|
3485
|
+
let outcome;
|
|
3486
|
+
try {
|
|
3487
|
+
const verified = boundVerifiedIdentity(await this.options.verify(identityStatement(r.binding), r.evidence));
|
|
3488
|
+
if (verified.subject !== r.verified.subject) throw new Error("The identity changed");
|
|
3489
|
+
outcome = (x) => ({
|
|
3490
|
+
...x,
|
|
3491
|
+
verified: {
|
|
3492
|
+
...verified,
|
|
3493
|
+
display: verified.display ?? x.verified.display
|
|
3494
|
+
},
|
|
3495
|
+
checkedAt: this.now(),
|
|
3496
|
+
status: "verified",
|
|
3497
|
+
error: void 0
|
|
3498
|
+
});
|
|
3499
|
+
} catch (e) {
|
|
3500
|
+
if (keepUnavailable && e instanceof IdentityCheckUnavailable) throw e;
|
|
3501
|
+
const error = shortError(e);
|
|
3502
|
+
outcome = (x) => ({
|
|
3503
|
+
...x,
|
|
3504
|
+
checkedAt: this.now(),
|
|
3505
|
+
status: "unconfirmed",
|
|
3506
|
+
error
|
|
3507
|
+
});
|
|
3508
|
+
}
|
|
3509
|
+
return await this.settleRecheck(id, (x) => x.status === "revoked" ? x : x.status === "withdrawn" ? {
|
|
3510
|
+
...outcome(x),
|
|
3511
|
+
status: "withdrawn"
|
|
3512
|
+
} : outcome(x)) ?? r;
|
|
3513
|
+
}
|
|
3514
|
+
async settleRecheck(id, outcome, event) {
|
|
3515
|
+
let next;
|
|
3516
|
+
await this.options.storage.update((l) => {
|
|
3517
|
+
const x = l.received.find((y) => y.id === id);
|
|
3518
|
+
if (!x) return l;
|
|
3519
|
+
next = outcome(x);
|
|
3520
|
+
const out = {
|
|
3521
|
+
...l,
|
|
3522
|
+
received: l.received.map((y) => y.id === id ? next : y)
|
|
3523
|
+
};
|
|
3524
|
+
return event && x.status !== "revoked" ? this.log(out, event) : out;
|
|
3525
|
+
});
|
|
3526
|
+
return next;
|
|
3527
|
+
}
|
|
3528
|
+
async receive(frame) {
|
|
3529
|
+
try {
|
|
3530
|
+
if (JSON.stringify(frame).length > 32768) throw new Error("Identity proof too large");
|
|
3531
|
+
const id = typeof frame.id === "string" && HEX64.test(frame.id) ? frame.id : void 0;
|
|
3532
|
+
switch (frame.t) {
|
|
3533
|
+
case "idp-hello":
|
|
3534
|
+
if (Array.isArray(frame.providers) && frame.providers.length <= 64) this.peerProviders = frame.providers.filter((p) => typeof p === "string" && IDENTITY_PROVIDER_ID.test(p));
|
|
3535
|
+
return;
|
|
3536
|
+
case "idp-request": return await this.onRequest(id, frame.provider);
|
|
3537
|
+
case "idp-challenge": return await this.onChallenge(id, frame.nonce, frame.issuedAt);
|
|
3538
|
+
case "idp-present": {
|
|
3539
|
+
const nonce = typeof frame.nonce === "string" ? frame.nonce : "";
|
|
3540
|
+
if (this.checking.has(nonce)) throw new Error("Unknown or reused identity challenge");
|
|
3541
|
+
this.checking.add(nonce);
|
|
3542
|
+
try {
|
|
3543
|
+
return await this.onPresent(frame);
|
|
3544
|
+
} finally {
|
|
3545
|
+
this.checking.delete(nonce);
|
|
3546
|
+
}
|
|
3547
|
+
}
|
|
3548
|
+
case "idp-result": {
|
|
3549
|
+
const s = id ? this.shared(id) : void 0;
|
|
3550
|
+
if (!s || s.status !== "pending") return;
|
|
3551
|
+
const ok = frame.ok === true;
|
|
3552
|
+
const error = ok ? void 0 : typeof frame.error === "string" && CONTACT_REFUSALS.has(frame.error) ? frame.error : CONTACT_REFUSED;
|
|
3553
|
+
const entry = {
|
|
3554
|
+
...s,
|
|
3555
|
+
status: ok ? "accepted" : "rejected",
|
|
3556
|
+
at: this.now(),
|
|
3557
|
+
error
|
|
3558
|
+
};
|
|
3559
|
+
const replaced = (x) => ok && x.status === "accepted" && x.provider === s.provider && x.account !== void 0 && x.account === s.account;
|
|
3560
|
+
await this.options.storage.update((l) => l.shared.filter((x) => x.id !== s.id && replaced(x)).reduce((out, x) => this.log(out, {
|
|
3561
|
+
type: "stopped",
|
|
3562
|
+
side: "mine",
|
|
3563
|
+
proof: x.id,
|
|
3564
|
+
provider: x.provider,
|
|
3565
|
+
subject: x.subject,
|
|
3566
|
+
reason: "withdrawn"
|
|
3567
|
+
}), this.log({
|
|
3568
|
+
...l,
|
|
3569
|
+
shared: [...l.shared.filter((x) => x.id !== s.id).map((x) => replaced(x) ? {
|
|
3570
|
+
...x,
|
|
3571
|
+
status: "withdrawn",
|
|
3572
|
+
at: entry.at
|
|
3573
|
+
} : x), entry]
|
|
3574
|
+
}, {
|
|
3575
|
+
type: "result",
|
|
3576
|
+
side: "mine",
|
|
3577
|
+
proof: s.id,
|
|
3578
|
+
ok,
|
|
3579
|
+
error
|
|
3580
|
+
})));
|
|
3581
|
+
return;
|
|
3582
|
+
}
|
|
3583
|
+
case "idp-withdraw": {
|
|
3584
|
+
if (!id) return;
|
|
3585
|
+
const r = this.options.storage.read().received.find((x) => x.id === id);
|
|
3586
|
+
if (r && r.status !== "withdrawn" && r.status !== "revoked") await this.options.storage.update((l) => this.log({
|
|
3587
|
+
...l,
|
|
3588
|
+
received: l.received.map((x) => x.id === id ? {
|
|
3589
|
+
...x,
|
|
3590
|
+
status: "withdrawn"
|
|
3591
|
+
} : x)
|
|
3592
|
+
}, {
|
|
3593
|
+
type: "stopped",
|
|
3594
|
+
side: "theirs",
|
|
3595
|
+
proof: id,
|
|
3596
|
+
provider: r.binding.provider,
|
|
3597
|
+
subject: r.verified.subject,
|
|
3598
|
+
reason: "withdrawn"
|
|
3599
|
+
}));
|
|
3600
|
+
if (this.options.storage.read().challenges.some((c) => c.id === id && !c.withdrawn)) await this.options.storage.update((l) => ({
|
|
3601
|
+
...l,
|
|
3602
|
+
challenges: l.challenges.map((c) => c.id === id ? {
|
|
3603
|
+
...c,
|
|
3604
|
+
withdrawn: true
|
|
3605
|
+
} : c)
|
|
3606
|
+
}));
|
|
3607
|
+
this.send({
|
|
3608
|
+
t: "idp-withdrawn",
|
|
3609
|
+
id
|
|
3610
|
+
});
|
|
3611
|
+
return;
|
|
3612
|
+
}
|
|
3613
|
+
case "idp-withdrawn": {
|
|
3614
|
+
const s = id ? this.shared(id) : void 0;
|
|
3615
|
+
if (s?.status === "withdrawal-pending") await this.setShared({
|
|
3616
|
+
...s,
|
|
3617
|
+
status: "withdrawn",
|
|
3618
|
+
at: this.now()
|
|
3619
|
+
});
|
|
3620
|
+
return;
|
|
3621
|
+
}
|
|
3622
|
+
}
|
|
3623
|
+
} catch (e) {
|
|
3624
|
+
this.options.onError?.(shortError(e));
|
|
3625
|
+
}
|
|
3626
|
+
}
|
|
3627
|
+
async onRequest(id, provider) {
|
|
3628
|
+
if (!id) return;
|
|
3629
|
+
if (typeof provider !== "string" || !this.options.providers().includes(provider)) {
|
|
3630
|
+
const error = UNSUPPORTED_KIND;
|
|
3631
|
+
this.send({
|
|
3632
|
+
t: "idp-result",
|
|
3633
|
+
id,
|
|
3634
|
+
ok: false,
|
|
3635
|
+
error
|
|
3636
|
+
});
|
|
3637
|
+
return;
|
|
3638
|
+
}
|
|
3639
|
+
this.options.scope();
|
|
3640
|
+
const now = this.now();
|
|
3641
|
+
const challenge = {
|
|
3642
|
+
nonce: toBase64Url(randomBytes(32)),
|
|
3643
|
+
issuedAt: now,
|
|
3644
|
+
provider,
|
|
3645
|
+
id
|
|
3646
|
+
};
|
|
3647
|
+
await this.options.storage.update((l) => {
|
|
3648
|
+
if (l.challenges.filter((c) => liveAt(c, now)).length >= MAX_CHALLENGES) throw new Error("Too many identity challenges");
|
|
3649
|
+
return this.withoutExpired(this.log({
|
|
3650
|
+
...l,
|
|
3651
|
+
challenges: [...l.challenges, challenge]
|
|
3652
|
+
}, {
|
|
3653
|
+
type: "shared",
|
|
3654
|
+
side: "theirs",
|
|
3655
|
+
proof: id,
|
|
3656
|
+
provider
|
|
3657
|
+
}), now);
|
|
3658
|
+
});
|
|
3659
|
+
this.send({
|
|
3660
|
+
t: "idp-challenge",
|
|
3661
|
+
id,
|
|
3662
|
+
nonce: challenge.nonce,
|
|
3663
|
+
issuedAt: challenge.issuedAt
|
|
3664
|
+
});
|
|
3665
|
+
}
|
|
3666
|
+
async onChallenge(id, nonce, issuedAt) {
|
|
3667
|
+
if (!id || !this.requested.has(id) || typeof nonce !== "string" || !CHALLENGE_NONCE.test(nonce) || !isInt(issuedAt)) return;
|
|
3668
|
+
const s = this.shared(id);
|
|
3669
|
+
if (!s || s.status !== "queued" && s.status !== "pending") return;
|
|
3670
|
+
this.requested.delete(id);
|
|
3671
|
+
const local = await this.options.localProof(id);
|
|
3672
|
+
if (!local) {
|
|
3673
|
+
const error = NOT_IN_PROFILE;
|
|
3674
|
+
await this.setShared({
|
|
3675
|
+
...s,
|
|
3676
|
+
status: "rejected",
|
|
3677
|
+
at: this.now(),
|
|
3678
|
+
error
|
|
3679
|
+
}, {
|
|
3680
|
+
type: "result",
|
|
3681
|
+
side: "mine",
|
|
3682
|
+
proof: id,
|
|
3683
|
+
ok: false,
|
|
3684
|
+
error
|
|
3685
|
+
});
|
|
3686
|
+
return;
|
|
3687
|
+
}
|
|
3688
|
+
const scope = this.options.scope();
|
|
3689
|
+
const sig = signIdentityPresentation(local.seed, id, scope, nonce, issuedAt);
|
|
3690
|
+
let stopped = false;
|
|
3691
|
+
await this.options.storage.update((l) => {
|
|
3692
|
+
const x = l.shared.find((y) => y.id === id);
|
|
3693
|
+
if (!x || x.status !== "queued" && x.status !== "pending") {
|
|
3694
|
+
stopped = true;
|
|
3695
|
+
return l;
|
|
3696
|
+
}
|
|
3697
|
+
return {
|
|
3698
|
+
...l,
|
|
3699
|
+
shared: [...l.shared.filter((y) => y.id !== id), {
|
|
3700
|
+
...x,
|
|
3701
|
+
status: "pending",
|
|
3702
|
+
at: this.now(),
|
|
3703
|
+
error: void 0
|
|
3704
|
+
}]
|
|
3705
|
+
};
|
|
3706
|
+
});
|
|
3707
|
+
if (stopped) return;
|
|
3708
|
+
this.send({
|
|
3709
|
+
t: "idp-present",
|
|
3710
|
+
nonce,
|
|
3711
|
+
issuedAt,
|
|
3712
|
+
binding: local.statement.binding,
|
|
3713
|
+
evidence: local.evidence,
|
|
3714
|
+
sig
|
|
3715
|
+
});
|
|
3716
|
+
}
|
|
3717
|
+
async onPresent(frame) {
|
|
3718
|
+
const { nonce, issuedAt, binding, evidence, sig } = frame;
|
|
3719
|
+
if (typeof nonce !== "string" || !CHALLENGE_NONCE.test(nonce) || !isInt(issuedAt) || !isIdentityBinding(binding)) return;
|
|
3720
|
+
const statement = identityStatement(binding);
|
|
3721
|
+
const id = statement.id;
|
|
3722
|
+
const scope = this.incomingScope();
|
|
3723
|
+
const now = this.now();
|
|
3724
|
+
const challengeOf = (l) => l.challenges.find((c) => c.nonce === nonce && c.issuedAt === issuedAt && c.provider === binding.provider && liveAt(c, now));
|
|
3725
|
+
const known = (l) => !!challengeOf(l);
|
|
3726
|
+
if (!known(this.options.storage.read())) throw new Error("Unknown or reused identity challenge");
|
|
3727
|
+
let verified;
|
|
3728
|
+
let error;
|
|
3729
|
+
/** The same, as this side's own timeline says it. */
|
|
3730
|
+
let reason;
|
|
3731
|
+
/** Its owner withdrew the proof while this check ran: no result goes out (see below). */
|
|
3732
|
+
let withdrawn = false;
|
|
3733
|
+
/** The check could not run on this side (IdentityCheckUnavailable). */
|
|
3734
|
+
let unavailable = false;
|
|
3735
|
+
/** The same proof this side holds verified, checked recently: its answer is reused (`IDENTITY_REUSE_CHECK_S`). */
|
|
3736
|
+
let reused;
|
|
3737
|
+
try {
|
|
3738
|
+
const mayReplace = (r) => r.binding.provider === binding.provider && (r.binding.subject === binding.subject || r.verified.subject === binding.subject);
|
|
3739
|
+
if (revokedHere(this.options.storage.read().received, id)) throw new Error(REVOKED);
|
|
3740
|
+
if (!receivedWithRoom(this.options.storage.read().received, id, mayReplace, now)) throw new Error(TOO_MANY_RECEIVED);
|
|
3741
|
+
if (JSON.stringify(evidence ?? null).length > 16384) throw new Error("Evidence too large");
|
|
3742
|
+
if (!verifyIdentityPresentation(binding.key, sig, id, scope, nonce, issuedAt)) throw new Error("The proof key did not sign this conversation's challenge");
|
|
3743
|
+
if (binding.issuedAt > now + 300) throw new Error("The proof is dated in the future");
|
|
3744
|
+
const evidenceJson = JSON.stringify(evidence ?? null);
|
|
3745
|
+
reused = this.options.storage.read().received.find((r) => r.id === id && r.status === "verified" && r.presenter === scope.subject && r.audience === scope.audience && r.context === scope.context && r.checkedAt <= now && now - r.checkedAt < 3600 && JSON.stringify(r.evidence ?? null) === evidenceJson);
|
|
3746
|
+
if (!reused) {
|
|
3747
|
+
this.checks = this.checks.filter((t) => t > now - 3600 && t <= now);
|
|
3748
|
+
if (this.checks.length >= 20) throw new IdentityCheckUnavailable("Too many of this contact's proofs to check this hour");
|
|
3749
|
+
this.checks.push(now);
|
|
3750
|
+
}
|
|
3751
|
+
const result = reused ? reused.verified : boundVerifiedIdentity(await this.options.verify(statement, evidence));
|
|
3752
|
+
if (Math.min(binding.expiresAt, result.expiresAt ?? Infinity) <= now) throw new Error(EXPIRED);
|
|
3753
|
+
if (!reused && await this.options.revoked?.(statement).catch(() => false)) throw new Error(REVOKED);
|
|
3754
|
+
verified = result;
|
|
3755
|
+
} catch (e) {
|
|
3756
|
+
error = peerError(e);
|
|
3757
|
+
reason = shortError(e);
|
|
3758
|
+
unavailable = e instanceof IdentityCheckUnavailable;
|
|
3759
|
+
}
|
|
3760
|
+
await this.options.storage.update((l) => {
|
|
3761
|
+
const challenge = challengeOf(l);
|
|
3762
|
+
if (!challenge) throw new Error("Unknown or reused identity challenge");
|
|
3763
|
+
const challenges = l.challenges.filter((c) => c.nonce !== nonce);
|
|
3764
|
+
withdrawn = !!challenge.withdrawn;
|
|
3765
|
+
if (withdrawn && (challenges.some((c) => c.id === challenge.id && !c.withdrawn) || l.received.some((r) => r.id === challenge.id && r.status !== "withdrawn" && r.status !== "revoked"))) return {
|
|
3766
|
+
...l,
|
|
3767
|
+
challenges
|
|
3768
|
+
};
|
|
3769
|
+
const subject = verified?.subject;
|
|
3770
|
+
if (verified && revokedHere(l.received, id)) {
|
|
3771
|
+
verified = void 0;
|
|
3772
|
+
error = reason = REVOKED;
|
|
3773
|
+
}
|
|
3774
|
+
if (verified && l.received.some((r) => r.id !== id && r.binding.provider === binding.provider && r.verified.subject === subject && r.binding.issuedAt > binding.issuedAt && standing(r, now))) {
|
|
3775
|
+
verified = void 0;
|
|
3776
|
+
error = reason = NEWER_HELD;
|
|
3777
|
+
}
|
|
3778
|
+
const prior = reused && l.received.find((r) => r.id === id);
|
|
3779
|
+
if (verified && reused && prior?.status !== (withdrawn ? "withdrawn" : "verified")) {
|
|
3780
|
+
verified = void 0;
|
|
3781
|
+
error = IDENTITY_CHECK_UNAVAILABLE;
|
|
3782
|
+
reason = "It was checked again while it was presented";
|
|
3783
|
+
}
|
|
3784
|
+
const received = verified && receivedWithRoom(l.received, id, (r) => r.binding.provider === binding.provider && r.verified.subject === subject, now);
|
|
3785
|
+
if (verified && !received) {
|
|
3786
|
+
verified = void 0;
|
|
3787
|
+
error = reason = TOO_MANY_RECEIVED;
|
|
3788
|
+
}
|
|
3789
|
+
const held = verified ? void 0 : l.received.find((r) => r.id === id && r.presenter === scope.subject && r.audience === scope.audience && (r.status === "verified" || r.status === "unconfirmed"));
|
|
3790
|
+
const stands = held?.status === "verified" && unavailable;
|
|
3791
|
+
if (stands) {
|
|
3792
|
+
verified = held.verified;
|
|
3793
|
+
error = reason = void 0;
|
|
3794
|
+
}
|
|
3795
|
+
const logged = this.log(l, {
|
|
3796
|
+
type: "result",
|
|
3797
|
+
side: "theirs",
|
|
3798
|
+
proof: id,
|
|
3799
|
+
requested: challenge.id,
|
|
3800
|
+
provider: binding.provider,
|
|
3801
|
+
subject: verified?.subject ?? binding.subject,
|
|
3802
|
+
ok: !!verified,
|
|
3803
|
+
...reason ? { error: reason } : {}
|
|
3804
|
+
});
|
|
3805
|
+
if (stands) return {
|
|
3806
|
+
...logged,
|
|
3807
|
+
challenges
|
|
3808
|
+
};
|
|
3809
|
+
if (!verified || !received) return {
|
|
3810
|
+
...logged,
|
|
3811
|
+
challenges,
|
|
3812
|
+
received: held && !unavailable ? l.received.map((r) => r === held ? {
|
|
3813
|
+
...r,
|
|
3814
|
+
checkedAt: now,
|
|
3815
|
+
status: "unconfirmed",
|
|
3816
|
+
error: reason
|
|
3817
|
+
} : r) : l.received
|
|
3818
|
+
};
|
|
3819
|
+
const record = {
|
|
3820
|
+
id,
|
|
3821
|
+
binding,
|
|
3822
|
+
evidence,
|
|
3823
|
+
verified,
|
|
3824
|
+
presenter: scope.subject,
|
|
3825
|
+
audience: scope.audience,
|
|
3826
|
+
context: scope.context,
|
|
3827
|
+
verifiedAt: prior?.verifiedAt ?? now,
|
|
3828
|
+
checkedAt: prior?.checkedAt ?? now,
|
|
3829
|
+
status: withdrawn ? "withdrawn" : "verified"
|
|
3830
|
+
};
|
|
3831
|
+
const kept = l.received.filter((r) => r.id !== id && !received.includes(r) && r.binding.provider === binding.provider && r.verified.subject === subject && r.status !== "withdrawn" && r.status !== "revoked").reduce((out, r) => this.log(out, {
|
|
3832
|
+
type: "stopped",
|
|
3833
|
+
side: "theirs",
|
|
3834
|
+
proof: r.id,
|
|
3835
|
+
provider: r.binding.provider,
|
|
3836
|
+
subject: r.verified.subject,
|
|
3837
|
+
reason: "withdrawn"
|
|
3838
|
+
}), {
|
|
3839
|
+
...logged,
|
|
3840
|
+
challenges,
|
|
3841
|
+
received: [...received, record]
|
|
3842
|
+
});
|
|
3843
|
+
return withdrawn ? this.log(kept, {
|
|
3844
|
+
type: "stopped",
|
|
3845
|
+
side: "theirs",
|
|
3846
|
+
proof: id,
|
|
3847
|
+
provider: binding.provider,
|
|
3848
|
+
subject: verified.subject,
|
|
3849
|
+
reason: "withdrawn"
|
|
3850
|
+
}) : kept;
|
|
3851
|
+
});
|
|
3852
|
+
if (!withdrawn) this.send(verified ? {
|
|
3853
|
+
t: "idp-result",
|
|
3854
|
+
id,
|
|
3855
|
+
ok: true
|
|
3856
|
+
} : {
|
|
3857
|
+
t: "idp-result",
|
|
3858
|
+
id,
|
|
3859
|
+
ok: false,
|
|
3860
|
+
error
|
|
3861
|
+
});
|
|
3862
|
+
}
|
|
3863
|
+
};
|
|
3864
|
+
// ../core/src/bitcoinAddress.ts
|
|
3865
|
+
var BITCOIN_NETWORKS = [
|
|
3866
|
+
"bitcoin",
|
|
3867
|
+
"signet",
|
|
3868
|
+
"testnet",
|
|
3869
|
+
"regtest",
|
|
3870
|
+
"mutinynet"
|
|
3871
|
+
];
|
|
3872
|
+
/** Mutinynet is a signet: it shares signet's (and testnet's) address prefixes. */
|
|
3873
|
+
var SEGWIT_HRP = {
|
|
3874
|
+
bitcoin: "bc",
|
|
3875
|
+
signet: "tb",
|
|
3876
|
+
testnet: "tb",
|
|
3877
|
+
mutinynet: "tb",
|
|
3878
|
+
regtest: "bcrt"
|
|
3879
|
+
};
|
|
3880
|
+
var BASE58_VERSIONS = {
|
|
3881
|
+
bitcoin: [0, 5],
|
|
3882
|
+
signet: [111, 196],
|
|
3883
|
+
testnet: [111, 196],
|
|
3884
|
+
mutinynet: [111, 196],
|
|
3885
|
+
regtest: [111, 196]
|
|
3886
|
+
};
|
|
3887
|
+
var base58check = createBase58check(sha256);
|
|
3888
|
+
function segwit(address, hrp) {
|
|
3889
|
+
if (address !== address.toLowerCase() && address !== address.toUpperCase()) return false;
|
|
3890
|
+
const lower = address.toLowerCase();
|
|
3891
|
+
if (!lower.startsWith(`${hrp}1`) || lower.length > 90) return false;
|
|
3892
|
+
for (const [coder, v0] of [[bech32, true], [bech32m, false]]) {
|
|
3893
|
+
let decoded;
|
|
3894
|
+
try {
|
|
3895
|
+
decoded = coder.decode(lower, 90);
|
|
3896
|
+
} catch {
|
|
3897
|
+
continue;
|
|
3898
|
+
}
|
|
3899
|
+
if (decoded.prefix !== hrp || decoded.words.length < 1) return false;
|
|
3900
|
+
const version = decoded.words[0];
|
|
3901
|
+
if (version > 16 || version === 0 !== v0) return false;
|
|
3902
|
+
let program;
|
|
3903
|
+
try {
|
|
3904
|
+
program = coder.fromWords(decoded.words.slice(1));
|
|
3905
|
+
} catch {
|
|
3906
|
+
return false;
|
|
3907
|
+
}
|
|
3908
|
+
if (version === 0) return program.length === 20 || program.length === 32;
|
|
3909
|
+
return program.length >= 2 && program.length <= 40;
|
|
3910
|
+
}
|
|
3911
|
+
return false;
|
|
3912
|
+
}
|
|
3913
|
+
/** Whether `address` is a valid address on `network`: checksums, witness versions, lengths and prefixes. */
|
|
3914
|
+
function isBitcoinAddress(address, network) {
|
|
3915
|
+
if (typeof address !== "string" || !BITCOIN_NETWORKS.includes(network) || address.length < 14 || address.length > 90) return false;
|
|
3916
|
+
if (segwit(address, SEGWIT_HRP[network])) return true;
|
|
3917
|
+
let payload;
|
|
3918
|
+
try {
|
|
3919
|
+
payload = base58check.decode(address);
|
|
3920
|
+
} catch {
|
|
3921
|
+
return false;
|
|
3922
|
+
}
|
|
3923
|
+
return payload.length === 21 && BASE58_VERSIONS[network].includes(payload[0]);
|
|
3924
|
+
}
|
|
3925
|
+
export { GROUP_BURST_ONE_LINK as $, IDENTITY_MEMO_SIZE as $t, newIdentityBinding as A, PublishFailedError as At, ANSWER_READ_SPACING_MS as B, createRelayPayload as Bt, emptyIdentityLedger as C, traceLink as Ct, identityStatement as D, BREAKER_THRESHOLD as Dt, identityRevocationValue as E, BREAKER_MAX_MS as Et, IDENTITY_TIMELINE_MAX as F, withRequestOptions as Ft, CHAT_RESERVE as G, readOwnRelayPayload as Gt, BACKGROUND_REQUESTS_PER_MINUTE as H, newerPacket as Ht, applyIdentityEvent as I, MAX_DNS_PACKET_BYTES as It, CHAT_WATCH_SHARE as J, decodeTxtPacket as Jt, CHAT_RESERVE_MOST_SHARE as K, signRelayPayload as Kt, lastSharedWithMe as L, PKARR_FUTURE_SKEW_MS as Lt, revokesIdentity as M, heldBackError as Mt, signIdentityPresentation as N, isDiscoveryBudgetError as Nt, identityStatementText as O, RelayBreaker as Ot, verifyIdentityPresentation as P, publishedNowhere as Pt, GROUP_BURST_MS as Q, IDENTITY_MEMO_MAX as Qt, ANSWER_READ_ALLOWANCE as R, PacketTooLargeError as Rt, boundVerifiedIdentity as S, setLinkTraceSink as St, identityRefusal as T, BREAKER_BASE_MS as Tt, BACKGROUND_WHILE_SIGNALING as U, openRelayPayload as Ut, AWAITED_READS as V, measureRecords as Vt, BACKGROUND_WRITES_SHARE as W, parseRelayPayload as Wt, DEPARTED_READS as X, readDnsName as Xt, DEFAULT_RELAYS as Y, encodeTxtPacket as Yt, FRESH_READ_MS as Z, IDENTITY_MEMO_IDLE_MS as Zt, IDENTITY_STATEMENT_PREFIX as _, WRITE_FIRST_MS as _t, IDENTITY_CLOCK_SKEW as a, sign as an, PREVIOUS_DEFAULT_RELAYS as at, IdentityExchange as b, readRelayBody as bt, IDENTITY_MAX_CHECKS_PER_HOUR as c, dropLoneJoiners as cn, RelayTransport as ct, IDENTITY_MAX_RECEIVED as d, sanitizeNick as dn, SIGNALING_WINDOW_MS as dt, createIdentity as en, GROUP_OVER_PACE_MS as et, IDENTITY_MAX_VALIDITY as f, stripHidden as fn, SLOW_DEMOTE_MS as ft, IDENTITY_REVOCATION_LABEL as g, WAY_BACK_SHARE as gt, IDENTITY_REUSE_CHECK_S as h, WATCH_SHARE as ht, IDENTITY_CHECK_UNAVAILABLE as i, setPublicKeyFastPath as in, HEDGE_MS as it, receivedIdentityStatus as j, budgetRetryMs as jt, isIdentityBinding as k, DiscoveryBudgetError as kt, IDENTITY_MAX_EVIDENCE as l, hasVisible as ln, RelaysFailedError as lt, IDENTITY_PROVIDER_ID as m, unwrapPasted as mn, TURN_SOURCE_TIMEOUT_MS as mt, isBitcoinAddress as n, identityFromSeedB64 as nn, GROUP_SIGNALING_ALLOWANCE_SHARE as nt, IDENTITY_FRAME_LIMIT as o, verify as on, RELAY_REQUESTS_PER_MINUTE as ot, IDENTITY_PROOF_CAPABILITY as p, trimEndOf as pn, SLOW_MS as pt, CHAT_RESERVE_PER_CHAT as q, decodeDnsAnswers as qt, IDENTITY_CHALLENGE_WINDOW as r, publicKeyFromZ32 as rn, HEDGE_MISSING_MS as rt, IDENTITY_FRAME_WINDOW_MS as s, MAX_NICK_LENGTH as sn, REQUESTS_PER_MINUTE as st, BITCOIN_NETWORKS as t, identityFromSeed as tn, GROUP_RATION_WINDOW_MS as tt, IDENTITY_MAX_FRAME as u, sanitizeDisplayText as un, SIGNALING_ALLOWANCE_SHARE as ut, IDENTITY_SUBJECT as v, currentRelays as vt, identityPresentationBytes as w, BREAKER_ALL_DOWN_PROBE_MS as wt, boundIdentityDisplay as x, relayUrlProblem as xt, IdentityCheckUnavailable as y, normalizeRelayUrl as yt, ANSWER_READ_FIRST_MS as z, RELAY_PAYLOAD_MAX_BYTES as zt };
|
|
3926
|
+
|
|
3927
|
+
//# sourceMappingURL=bitcoinAddress-BbPG0DF7.js.map
|