@peers-app/peers-sdk 0.25.13 → 0.25.15
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/context/user-context-identity.test.d.ts +1 -0
- package/dist/context/user-context-identity.test.js +220 -0
- package/dist/context/user-context.d.ts +5 -1
- package/dist/context/user-context.js +63 -8
- package/dist/contracts/index.d.ts +1 -1
- package/dist/contracts/system/identity-contract.d.ts +156 -0
- package/dist/contracts/system/identity-contract.js +172 -0
- package/dist/contracts/system/index.d.ts +1 -1
- package/dist/data/assistants.d.ts +4 -4
- package/dist/data/devices.d.ts +70 -4
- package/dist/data/devices.js +3 -0
- package/dist/data/group-members.d.ts +2 -2
- package/dist/data/groups.d.ts +2 -2
- package/dist/data/invites.d.ts +93 -25
- package/dist/data/persistent-vars.d.ts +8 -0
- package/dist/data/persistent-vars.js +16 -0
- package/dist/data/tool-access-approvals.d.ts +2 -2
- package/dist/data/user-permissions.d.ts +10 -2
- package/dist/data/user-permissions.js +35 -4
- package/dist/data/user-permissions.test.js +26 -0
- package/dist/data/users.d.ts +48 -4
- package/dist/data/users.js +61 -5
- package/dist/data/workflows.d.ts +1 -1
- package/dist/device/connection.d.ts +7 -1
- package/dist/device/connection.js +38 -6
- package/dist/device/connection.test.js +33 -4
- package/dist/device/device.d.ts +2 -2
- package/dist/device/device.js +4 -2
- package/dist/device/get-trust-level-fn.d.ts +1 -0
- package/dist/device/get-trust-level-fn.js +28 -4
- package/dist/device/get-trust-level-fn.test.js +95 -0
- package/dist/device-pairing/types.d.ts +26 -26
- package/dist/host-credentials.d.ts +78 -0
- package/dist/host-credentials.js +128 -0
- package/dist/host-credentials.test.d.ts +1 -0
- package/dist/host-credentials.test.js +88 -0
- package/dist/identity/account-email.d.ts +59 -0
- package/dist/identity/account-email.js +87 -0
- package/dist/identity/account-email.test.d.ts +1 -0
- package/dist/identity/account-email.test.js +59 -0
- package/dist/identity/anchor-document.d.ts +205 -0
- package/dist/identity/anchor-document.js +119 -0
- package/dist/identity/anchor-document.test.d.ts +1 -0
- package/dist/identity/anchor-document.test.js +16 -0
- package/dist/identity/anchor-resolution.d.ts +61 -0
- package/dist/identity/anchor-resolution.js +117 -0
- package/dist/identity/anchor-resolution.test.d.ts +1 -0
- package/dist/identity/anchor-resolution.test.js +130 -0
- package/dist/identity/key-change.d.ts +142 -0
- package/dist/identity/key-change.js +472 -0
- package/dist/identity/key-change.test.d.ts +1 -0
- package/dist/identity/key-change.test.js +811 -0
- package/dist/identity/key-contest.d.ts +91 -0
- package/dist/identity/key-contest.js +55 -0
- package/dist/identity/key-resolutions.d.ts +55 -0
- package/dist/identity/key-resolutions.js +42 -0
- package/dist/identity/passkey.d.ts +22 -0
- package/dist/identity/passkey.js +33 -0
- package/dist/identity/user-row-change.d.ts +45 -0
- package/dist/identity/user-row-change.js +81 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +9 -0
- package/dist/invites/invite-messages.d.ts +512 -116
- package/dist/invites/invite-token.d.ts +114 -24
- package/dist/invites/invite-token.js +6 -0
- package/dist/keys.d.ts +7 -0
- package/dist/keys.js +20 -0
- package/dist/package-installer/package-author-signing.test.js +11 -0
- package/dist/package-installer/package-publisher.d.ts +5 -1
- package/dist/package-installer/package-publisher.js +1 -1
- package/dist/package-installer/package-publisher.test.js +11 -0
- package/dist/package-installer/package-remote-checker.js +3 -0
- package/dist/system-ids.d.ts +16 -0
- package/dist/system-ids.js +16 -0
- package/dist/types/workflow.d.ts +2 -2
- package/dist/utils.d.ts +10 -0
- package/dist/utils.js +43 -0
- package/dist/utils.test.js +23 -0
- package/package.json +1 -1
|
@@ -0,0 +1,811 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const SQLiteDB = require("better-sqlite3");
|
|
4
|
+
const user_context_1 = require("../context/user-context");
|
|
5
|
+
const user_context_singleton_1 = require("../context/user-context-singleton");
|
|
6
|
+
const contract_resolvers_1 = require("../contracts/contract-resolvers");
|
|
7
|
+
const sql_data_source_1 = require("../data/orm/sql.data-source");
|
|
8
|
+
const persistent_vars_1 = require("../data/persistent-vars");
|
|
9
|
+
const user_permissions_1 = require("../data/user-permissions");
|
|
10
|
+
const users_1 = require("../data/users");
|
|
11
|
+
const keys_1 = require("../keys");
|
|
12
|
+
const utils_1 = require("../utils");
|
|
13
|
+
const anchor_document_1 = require("./anchor-document");
|
|
14
|
+
const anchor_resolution_1 = require("./anchor-resolution");
|
|
15
|
+
const key_change_1 = require("./key-change");
|
|
16
|
+
const key_contest_1 = require("./key-contest");
|
|
17
|
+
const key_resolutions_1 = require("./key-resolutions");
|
|
18
|
+
// Installs the UsersTable guard the save tests below rely on.
|
|
19
|
+
require("./user-row-change");
|
|
20
|
+
class DBHarness {
|
|
21
|
+
_db = null;
|
|
22
|
+
get db() {
|
|
23
|
+
if (!this._db) {
|
|
24
|
+
this._db = new SQLiteDB(":memory:");
|
|
25
|
+
this._db.pragma("journal_mode = WAL");
|
|
26
|
+
}
|
|
27
|
+
return this._db;
|
|
28
|
+
}
|
|
29
|
+
async get(sql, params = []) {
|
|
30
|
+
return this.db.prepare(sql).get(params);
|
|
31
|
+
}
|
|
32
|
+
async all(sql, params = []) {
|
|
33
|
+
return this.db.prepare(sql).all(params);
|
|
34
|
+
}
|
|
35
|
+
async exec(sql, params = []) {
|
|
36
|
+
const result = this.db.prepare(sql).run(params);
|
|
37
|
+
return { changes: result.changes };
|
|
38
|
+
}
|
|
39
|
+
async close() {
|
|
40
|
+
this._db?.close();
|
|
41
|
+
this._db = null;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
describe("evaluatePresentedKey", () => {
|
|
45
|
+
const userId = (0, utils_1.newid)();
|
|
46
|
+
const record = (0, keys_1.newKeys)();
|
|
47
|
+
const next = (0, keys_1.newKeys)();
|
|
48
|
+
const anchor = {
|
|
49
|
+
type: "https",
|
|
50
|
+
url: "https://example.com/.well-known/peers-identity.json",
|
|
51
|
+
};
|
|
52
|
+
let userContext;
|
|
53
|
+
const db = new DBHarness();
|
|
54
|
+
beforeAll(async () => {
|
|
55
|
+
const dataSourceFactory = (metaData, schema) => new sql_data_source_1.SQLDataSource(db, metaData, schema);
|
|
56
|
+
userContext = new user_context_1.UserContext(userId, dataSourceFactory, true);
|
|
57
|
+
await userContext.loadingPromise;
|
|
58
|
+
userContext.deviceId((0, utils_1.newid)());
|
|
59
|
+
(0, user_context_singleton_1.setUserContext)(userContext);
|
|
60
|
+
await (0, users_1.Users)(userContext.userDataContext).save({
|
|
61
|
+
userId,
|
|
62
|
+
name: "Alice",
|
|
63
|
+
publicKey: record.publicKey,
|
|
64
|
+
publicBoxKey: record.publicBoxKey,
|
|
65
|
+
anchors: [anchor],
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
afterAll(async () => {
|
|
69
|
+
userContext.dispose();
|
|
70
|
+
await db.close();
|
|
71
|
+
(0, anchor_resolution_1.setAnchorFetcher)(undefined);
|
|
72
|
+
});
|
|
73
|
+
afterEach(() => {
|
|
74
|
+
(0, anchor_resolution_1.setAnchorFetcher)(undefined);
|
|
75
|
+
});
|
|
76
|
+
function doc(publicKey, authorizedBy) {
|
|
77
|
+
return {
|
|
78
|
+
version: 1,
|
|
79
|
+
userId,
|
|
80
|
+
keys: [{ publicKey, issuedAt: "2026-10-03T18:00:00.000Z", authorizedBy }],
|
|
81
|
+
anchors: [anchor],
|
|
82
|
+
updatedAt: "2026-10-03T18:00:00.000Z",
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
it("switches the personal row when the anchor confirms a rotation, and reuses the cache", async () => {
|
|
86
|
+
const issuedAt = "2026-10-03T18:00:00.000Z";
|
|
87
|
+
const signed = (0, anchor_document_1.signSuccession)({ purpose: "peers-key-succession", userId, publicKey: next.publicKey, issuedAt }, record.secretKey);
|
|
88
|
+
const fetcher = jest.fn(async () => ({
|
|
89
|
+
outcome: "document",
|
|
90
|
+
document: doc(next.publicKey, { publicKey: signed.publicKey, signature: signed.signature }),
|
|
91
|
+
maxAgeSeconds: 60,
|
|
92
|
+
}));
|
|
93
|
+
(0, anchor_resolution_1.setAnchorFetcher)(fetcher);
|
|
94
|
+
const decision = await (0, key_change_1.evaluatePresentedKey)({
|
|
95
|
+
dataContext: userContext.userDataContext,
|
|
96
|
+
userId,
|
|
97
|
+
presentedKey: next.publicKey,
|
|
98
|
+
presentedBoxKey: next.publicBoxKey,
|
|
99
|
+
});
|
|
100
|
+
expect(decision).toEqual({ accepted: true, state: "confirmed-rotation" });
|
|
101
|
+
const stored = await (0, users_1.Users)(userContext.userDataContext).get(userId);
|
|
102
|
+
expect(stored?.publicKey).toBe(next.publicKey);
|
|
103
|
+
expect(stored?.publicBoxKey).toBe(next.publicBoxKey);
|
|
104
|
+
expect((0, keys_1.derivePublicBoxKey)(next.publicKey)).toBe(next.publicBoxKey);
|
|
105
|
+
const again = await (0, key_change_1.evaluatePresentedKey)({
|
|
106
|
+
dataContext: userContext.userDataContext,
|
|
107
|
+
userId,
|
|
108
|
+
presentedKey: next.publicKey,
|
|
109
|
+
presentedBoxKey: next.publicBoxKey,
|
|
110
|
+
});
|
|
111
|
+
expect(again.state).toBe("current");
|
|
112
|
+
expect(fetcher).toHaveBeenCalledTimes(1);
|
|
113
|
+
const cached = await (0, key_resolutions_1.KeyResolutions)(userContext.userDataContext).findOne({
|
|
114
|
+
userId,
|
|
115
|
+
presentedKey: next.publicKey,
|
|
116
|
+
});
|
|
117
|
+
expect(cached?.state).toBe("confirmed-rotation");
|
|
118
|
+
expect(cached?.firstSeenAt).toBeTruthy();
|
|
119
|
+
});
|
|
120
|
+
it("reports a confirmed rotation without replacing the row when adopt is false", async () => {
|
|
121
|
+
const held = (0, keys_1.newKeys)();
|
|
122
|
+
const issuedAt = "2026-10-03T18:00:00.000Z";
|
|
123
|
+
const signed = (0, anchor_document_1.signSuccession)({ purpose: "peers-key-succession", userId, publicKey: held.publicKey, issuedAt }, next.secretKey);
|
|
124
|
+
(0, anchor_resolution_1.setAnchorFetcher)(async () => ({
|
|
125
|
+
outcome: "document",
|
|
126
|
+
document: doc(held.publicKey, { publicKey: signed.publicKey, signature: signed.signature }),
|
|
127
|
+
}));
|
|
128
|
+
const decision = await (0, key_change_1.evaluatePresentedKey)({
|
|
129
|
+
dataContext: userContext.userDataContext,
|
|
130
|
+
userId,
|
|
131
|
+
presentedKey: held.publicKey,
|
|
132
|
+
presentedBoxKey: held.publicBoxKey,
|
|
133
|
+
adopt: false,
|
|
134
|
+
});
|
|
135
|
+
expect(decision).toEqual({ accepted: true, state: "confirmed-rotation" });
|
|
136
|
+
const stored = await (0, users_1.Users)(userContext.userDataContext).get(userId);
|
|
137
|
+
expect(stored?.publicKey).toBe(next.publicKey);
|
|
138
|
+
});
|
|
139
|
+
it("keeps the key on record when an anchor vetoes", async () => {
|
|
140
|
+
const stranger = (0, keys_1.newKeys)();
|
|
141
|
+
(0, anchor_resolution_1.setAnchorFetcher)(async () => ({
|
|
142
|
+
outcome: "document",
|
|
143
|
+
document: doc(record.publicKey),
|
|
144
|
+
}));
|
|
145
|
+
const decision = await (0, key_change_1.evaluatePresentedKey)({
|
|
146
|
+
dataContext: userContext.userDataContext,
|
|
147
|
+
userId,
|
|
148
|
+
presentedKey: stranger.publicKey,
|
|
149
|
+
presentedBoxKey: stranger.publicBoxKey,
|
|
150
|
+
});
|
|
151
|
+
expect(decision).toEqual({ accepted: false, state: "vetoed" });
|
|
152
|
+
const stored = await (0, users_1.Users)(userContext.userDataContext).get(userId);
|
|
153
|
+
expect(stored?.publicKey).toBe(next.publicKey);
|
|
154
|
+
});
|
|
155
|
+
it("leaves a recovery confirmed but not accepted", async () => {
|
|
156
|
+
const recovered = (0, keys_1.newKeys)();
|
|
157
|
+
(0, anchor_resolution_1.setAnchorFetcher)(async () => ({ outcome: "document", document: doc(recovered.publicKey) }));
|
|
158
|
+
const decision = await (0, key_change_1.evaluatePresentedKey)({
|
|
159
|
+
dataContext: userContext.userDataContext,
|
|
160
|
+
userId,
|
|
161
|
+
presentedKey: recovered.publicKey,
|
|
162
|
+
presentedBoxKey: recovered.publicBoxKey,
|
|
163
|
+
});
|
|
164
|
+
expect(decision).toEqual({ accepted: false, state: "recovery-pending" });
|
|
165
|
+
const cached = await (0, key_resolutions_1.KeyResolutions)(userContext.userDataContext).findOne({
|
|
166
|
+
userId,
|
|
167
|
+
presentedKey: recovered.publicKey,
|
|
168
|
+
});
|
|
169
|
+
expect(cached?.state).toBe("confirmed-recovery");
|
|
170
|
+
const stored = await (0, users_1.Users)(userContext.userDataContext).get(userId);
|
|
171
|
+
expect(stored?.publicKey).toBe(next.publicKey);
|
|
172
|
+
});
|
|
173
|
+
it("refuses a group row key change until anchors confirm it", async () => {
|
|
174
|
+
const group = userContext.getDataContext((0, utils_1.newid)());
|
|
175
|
+
const personal = await (0, users_1.Users)(userContext.userDataContext).get(userId);
|
|
176
|
+
if (!personal)
|
|
177
|
+
throw new Error("missing personal row");
|
|
178
|
+
await (0, users_1.Users)(group).save((0, user_permissions_1.signUserObject)({
|
|
179
|
+
userId,
|
|
180
|
+
name: personal.name,
|
|
181
|
+
publicKey: personal.publicKey,
|
|
182
|
+
publicBoxKey: personal.publicBoxKey,
|
|
183
|
+
anchors: personal.anchors,
|
|
184
|
+
}, next.secretKey));
|
|
185
|
+
const stranger = (0, keys_1.newKeys)();
|
|
186
|
+
await expect((0, users_1.Users)(group).save((0, user_permissions_1.signUserObject)({
|
|
187
|
+
userId,
|
|
188
|
+
name: personal.name,
|
|
189
|
+
publicKey: stranger.publicKey,
|
|
190
|
+
publicBoxKey: stranger.publicBoxKey,
|
|
191
|
+
anchors: [{ type: "https", url: "https://attacker.example/peers-identity.json" }],
|
|
192
|
+
}, stranger.secretKey))).rejects.toThrow(/not confirmed/);
|
|
193
|
+
const issuedAt = "2026-10-03T19:00:00.000Z";
|
|
194
|
+
const rotated = (0, keys_1.newKeys)();
|
|
195
|
+
const signed = (0, anchor_document_1.signSuccession)({ purpose: "peers-key-succession", userId, publicKey: rotated.publicKey, issuedAt }, next.secretKey);
|
|
196
|
+
(0, anchor_resolution_1.setAnchorFetcher)(async () => ({
|
|
197
|
+
outcome: "document",
|
|
198
|
+
document: {
|
|
199
|
+
version: 1,
|
|
200
|
+
userId,
|
|
201
|
+
keys: [
|
|
202
|
+
{
|
|
203
|
+
publicKey: rotated.publicKey,
|
|
204
|
+
issuedAt,
|
|
205
|
+
authorizedBy: { publicKey: signed.publicKey, signature: signed.signature },
|
|
206
|
+
},
|
|
207
|
+
],
|
|
208
|
+
anchors: [anchor],
|
|
209
|
+
updatedAt: issuedAt,
|
|
210
|
+
},
|
|
211
|
+
}));
|
|
212
|
+
const saved = await (0, users_1.Users)(group).save((0, user_permissions_1.signUserObject)({
|
|
213
|
+
userId,
|
|
214
|
+
name: "Alice",
|
|
215
|
+
publicKey: rotated.publicKey,
|
|
216
|
+
publicBoxKey: rotated.publicBoxKey,
|
|
217
|
+
anchors: [{ type: "https", url: "https://attacker.example/peers-identity.json" }],
|
|
218
|
+
}, rotated.secretKey));
|
|
219
|
+
expect(saved.publicKey).toBe(rotated.publicKey);
|
|
220
|
+
expect(saved.anchors).toEqual([anchor]);
|
|
221
|
+
});
|
|
222
|
+
it("refuses a contact key change and a replacement anchor list in the personal table", async () => {
|
|
223
|
+
const contactId = (0, utils_1.newid)();
|
|
224
|
+
const contactKeys = (0, keys_1.newKeys)();
|
|
225
|
+
await (0, users_1.Users)(userContext.userDataContext).save({
|
|
226
|
+
userId: contactId,
|
|
227
|
+
name: "Carol",
|
|
228
|
+
publicKey: contactKeys.publicKey,
|
|
229
|
+
publicBoxKey: contactKeys.publicBoxKey,
|
|
230
|
+
anchors: [anchor],
|
|
231
|
+
});
|
|
232
|
+
const thief = (0, keys_1.newKeys)();
|
|
233
|
+
await expect((0, users_1.Users)(userContext.userDataContext).save((0, user_permissions_1.signUserObject)({
|
|
234
|
+
userId: contactId,
|
|
235
|
+
name: "Carol",
|
|
236
|
+
publicKey: thief.publicKey,
|
|
237
|
+
publicBoxKey: thief.publicBoxKey,
|
|
238
|
+
anchors: [{ type: "https", url: "https://attacker.example/peers-identity.json" }],
|
|
239
|
+
}, thief.secretKey))).rejects.toThrow(/not confirmed/);
|
|
240
|
+
// Same key, replacement list, anchors on record have not answered: a direct save
|
|
241
|
+
// cannot wait, so it is refused.
|
|
242
|
+
const replacement = (0, user_permissions_1.signUserObject)({
|
|
243
|
+
userId: contactId,
|
|
244
|
+
name: "Carol",
|
|
245
|
+
publicKey: contactKeys.publicKey,
|
|
246
|
+
publicBoxKey: contactKeys.publicBoxKey,
|
|
247
|
+
anchors: [{ type: "https", url: "https://attacker.example/peers-identity.json" }],
|
|
248
|
+
}, contactKeys.secretKey);
|
|
249
|
+
(0, anchor_resolution_1.setAnchorFetcher)(async () => ({ outcome: "abstain" }));
|
|
250
|
+
await expect((0, users_1.Users)(userContext.userDataContext).save(replacement)).rejects.toThrow(/not confirmed/);
|
|
251
|
+
// Once the anchor on record answers with a different list, the row is still the key on
|
|
252
|
+
// record's: the profile is stored and the list on record is kept.
|
|
253
|
+
(0, anchor_resolution_1.setAnchorFetcher)(async () => ({
|
|
254
|
+
outcome: "document",
|
|
255
|
+
document: {
|
|
256
|
+
version: 1,
|
|
257
|
+
userId: contactId,
|
|
258
|
+
keys: [{ publicKey: contactKeys.publicKey, issuedAt: "2026-10-03T18:00:00.000Z" }],
|
|
259
|
+
anchors: [anchor],
|
|
260
|
+
updatedAt: "2026-10-03T18:00:00.000Z",
|
|
261
|
+
},
|
|
262
|
+
}));
|
|
263
|
+
const saved = await (0, users_1.Users)(userContext.userDataContext).save((0, user_permissions_1.signUserObject)({ ...replacement, name: "Carol B" }, contactKeys.secretKey));
|
|
264
|
+
expect(saved.name).toBe("Carol B");
|
|
265
|
+
expect(saved.anchors).toEqual([anchor]);
|
|
266
|
+
const stored = await (0, users_1.Users)(userContext.userDataContext).get(contactId);
|
|
267
|
+
expect(stored?.publicKey).toBe(contactKeys.publicKey);
|
|
268
|
+
expect(stored?.anchors).toEqual([anchor]);
|
|
269
|
+
// The list on record is not what Carol signed, but the list is the anchors' decision, not
|
|
270
|
+
// part of her claim: her signature still verifies, so she can still call our contracts.
|
|
271
|
+
expect(() => (0, user_permissions_1.verifyUserSignature)(stored)).not.toThrow();
|
|
272
|
+
await expect((0, contract_resolvers_1.verifyRemoteContractIdentity)(userContext, {
|
|
273
|
+
userId: contactId,
|
|
274
|
+
deviceId: (0, utils_1.newid)(),
|
|
275
|
+
publicKey: contactKeys.publicKey,
|
|
276
|
+
publicBoxKey: contactKeys.publicBoxKey,
|
|
277
|
+
})).resolves.toBe(true);
|
|
278
|
+
});
|
|
279
|
+
it("lets a row with the same key supply the first anchor list when nothing is on record", async () => {
|
|
280
|
+
const contactId = (0, utils_1.newid)();
|
|
281
|
+
const keys = (0, keys_1.newKeys)();
|
|
282
|
+
await (0, users_1.Users)(userContext.userDataContext).save({
|
|
283
|
+
userId: contactId,
|
|
284
|
+
name: "Hal",
|
|
285
|
+
publicKey: keys.publicKey,
|
|
286
|
+
publicBoxKey: keys.publicBoxKey,
|
|
287
|
+
});
|
|
288
|
+
const saved = await (0, users_1.Users)(userContext.userDataContext).save((0, user_permissions_1.signUserObject)({
|
|
289
|
+
userId: contactId,
|
|
290
|
+
name: "Hal",
|
|
291
|
+
publicKey: keys.publicKey,
|
|
292
|
+
publicBoxKey: keys.publicBoxKey,
|
|
293
|
+
anchors: [anchor],
|
|
294
|
+
}, keys.secretKey));
|
|
295
|
+
expect(saved.anchors).toEqual([anchor]);
|
|
296
|
+
// From here on the list is on record and a replacement needs the anchors to publish it.
|
|
297
|
+
(0, anchor_resolution_1.setAnchorFetcher)(async () => ({ outcome: "abstain" }));
|
|
298
|
+
await expect((0, users_1.Users)(userContext.userDataContext).save((0, user_permissions_1.signUserObject)({
|
|
299
|
+
userId: contactId,
|
|
300
|
+
name: "Hal",
|
|
301
|
+
publicKey: keys.publicKey,
|
|
302
|
+
publicBoxKey: keys.publicBoxKey,
|
|
303
|
+
anchors: [{ type: "https", url: "https://example.com/other.json" }],
|
|
304
|
+
}, keys.secretKey))).rejects.toThrow(/not confirmed/);
|
|
305
|
+
});
|
|
306
|
+
it("adopts an anchor list the anchors on record publish", async () => {
|
|
307
|
+
const contactId = (0, utils_1.newid)();
|
|
308
|
+
const keys = (0, keys_1.newKeys)();
|
|
309
|
+
const nextAnchors = [anchor, { type: "https", url: "https://example.com/extra.json" }];
|
|
310
|
+
const signedOnRecord = (0, user_permissions_1.signUserObject)({
|
|
311
|
+
userId: contactId,
|
|
312
|
+
name: "Gina",
|
|
313
|
+
publicKey: keys.publicKey,
|
|
314
|
+
publicBoxKey: keys.publicBoxKey,
|
|
315
|
+
anchors: [anchor],
|
|
316
|
+
}, keys.secretKey);
|
|
317
|
+
await (0, users_1.Users)(userContext.userDataContext).save(signedOnRecord);
|
|
318
|
+
// The list is confirmed on its own (a handshake, a group row): the signed personal row
|
|
319
|
+
// keeps its signature, since the list is not under it.
|
|
320
|
+
(0, anchor_resolution_1.setAnchorFetcher)(async () => ({
|
|
321
|
+
outcome: "document",
|
|
322
|
+
document: {
|
|
323
|
+
version: 1,
|
|
324
|
+
userId: contactId,
|
|
325
|
+
keys: [],
|
|
326
|
+
anchors: nextAnchors,
|
|
327
|
+
updatedAt: "2026-10-03T18:00:00.000Z",
|
|
328
|
+
},
|
|
329
|
+
}));
|
|
330
|
+
await expect((0, key_change_1.evaluatePresentedAnchors)({
|
|
331
|
+
dataContext: userContext.userDataContext,
|
|
332
|
+
userId: contactId,
|
|
333
|
+
recordAnchors: [anchor],
|
|
334
|
+
presentedAnchors: nextAnchors,
|
|
335
|
+
})).resolves.toBe("confirmed");
|
|
336
|
+
const adopted = await (0, users_1.Users)(userContext.userDataContext).get(contactId);
|
|
337
|
+
expect(adopted?.anchors).toEqual(nextAnchors);
|
|
338
|
+
expect(adopted?.signature).toBe(signedOnRecord.signature);
|
|
339
|
+
expect(() => (0, user_permissions_1.verifyUserSignature)(adopted)).not.toThrow();
|
|
340
|
+
await (0, users_1.writeResolvedUserKey)(userContext.userDataContext, {
|
|
341
|
+
...signedOnRecord,
|
|
342
|
+
anchors: [anchor],
|
|
343
|
+
});
|
|
344
|
+
(0, anchor_resolution_1.setAnchorFetcher)(async () => ({
|
|
345
|
+
outcome: "document",
|
|
346
|
+
document: {
|
|
347
|
+
version: 1,
|
|
348
|
+
userId: contactId,
|
|
349
|
+
keys: [],
|
|
350
|
+
anchors: nextAnchors,
|
|
351
|
+
updatedAt: "2026-10-03T18:00:00.000Z",
|
|
352
|
+
},
|
|
353
|
+
}));
|
|
354
|
+
const saved = await (0, users_1.Users)(userContext.userDataContext).save((0, user_permissions_1.signUserObject)({
|
|
355
|
+
userId: contactId,
|
|
356
|
+
name: "Gina",
|
|
357
|
+
publicKey: keys.publicKey,
|
|
358
|
+
publicBoxKey: keys.publicBoxKey,
|
|
359
|
+
anchors: nextAnchors,
|
|
360
|
+
}, keys.secretKey));
|
|
361
|
+
expect(saved.anchors).toEqual(nextAnchors);
|
|
362
|
+
});
|
|
363
|
+
it("copies a confirmed anchor list onto a group row", async () => {
|
|
364
|
+
const contactId = (0, utils_1.newid)();
|
|
365
|
+
const keys = (0, keys_1.newKeys)();
|
|
366
|
+
const oldAnchors = [anchor];
|
|
367
|
+
const profile = {
|
|
368
|
+
userId: contactId,
|
|
369
|
+
name: "Fran",
|
|
370
|
+
publicKey: keys.publicKey,
|
|
371
|
+
publicBoxKey: keys.publicBoxKey,
|
|
372
|
+
};
|
|
373
|
+
await (0, users_1.Users)(userContext.userDataContext).save({ ...profile, anchors: oldAnchors });
|
|
374
|
+
const group = userContext.getDataContext((0, utils_1.newid)());
|
|
375
|
+
await (0, users_1.Users)(group).save((0, user_permissions_1.signUserObject)({ ...profile, anchors: oldAnchors }, keys.secretKey));
|
|
376
|
+
const nextAnchors = [
|
|
377
|
+
...oldAnchors,
|
|
378
|
+
{ type: "https", url: "https://example.com/extra.json" },
|
|
379
|
+
];
|
|
380
|
+
await (0, users_1.writeResolvedUserKey)(userContext.userDataContext, { ...profile, anchors: nextAnchors });
|
|
381
|
+
const saved = await (0, users_1.Users)(group).save((0, user_permissions_1.signUserObject)({ ...profile, anchors: nextAnchors }, keys.secretKey));
|
|
382
|
+
expect(saved.anchors).toEqual(nextAnchors);
|
|
383
|
+
});
|
|
384
|
+
it("adopts a recovery when the delay has elapsed and holds a contested one", async () => {
|
|
385
|
+
const contactId = (0, utils_1.newid)();
|
|
386
|
+
const contactKeys = (0, keys_1.newKeys)();
|
|
387
|
+
const recovered = (0, keys_1.newKeys)();
|
|
388
|
+
await (0, users_1.Users)(userContext.userDataContext).save({
|
|
389
|
+
userId: contactId,
|
|
390
|
+
name: "Dana",
|
|
391
|
+
publicKey: contactKeys.publicKey,
|
|
392
|
+
publicBoxKey: contactKeys.publicBoxKey,
|
|
393
|
+
anchors: [anchor],
|
|
394
|
+
});
|
|
395
|
+
(0, anchor_resolution_1.setAnchorFetcher)(async () => ({
|
|
396
|
+
outcome: "document",
|
|
397
|
+
document: {
|
|
398
|
+
version: 1,
|
|
399
|
+
userId: contactId,
|
|
400
|
+
keys: [{ publicKey: recovered.publicKey, issuedAt: "2026-10-03T18:00:00.000Z" }],
|
|
401
|
+
anchors: [anchor],
|
|
402
|
+
updatedAt: "2026-10-03T18:00:00.000Z",
|
|
403
|
+
},
|
|
404
|
+
}));
|
|
405
|
+
process.env.PEERS_RECOVERY_DELAY_MS = "0";
|
|
406
|
+
try {
|
|
407
|
+
const decision = await (0, key_change_1.evaluatePresentedKey)({
|
|
408
|
+
dataContext: userContext.userDataContext,
|
|
409
|
+
userId: contactId,
|
|
410
|
+
presentedKey: recovered.publicKey,
|
|
411
|
+
presentedBoxKey: recovered.publicBoxKey,
|
|
412
|
+
});
|
|
413
|
+
expect(decision).toEqual({ accepted: true, state: "confirmed-recovery" });
|
|
414
|
+
const stored = await (0, users_1.Users)(userContext.userDataContext).get(contactId);
|
|
415
|
+
expect(stored?.publicKey).toBe(recovered.publicKey);
|
|
416
|
+
}
|
|
417
|
+
finally {
|
|
418
|
+
delete process.env.PEERS_RECOVERY_DELAY_MS;
|
|
419
|
+
}
|
|
420
|
+
const heldId = (0, utils_1.newid)();
|
|
421
|
+
const heldKeys = (0, keys_1.newKeys)();
|
|
422
|
+
const attack = (0, keys_1.newKeys)();
|
|
423
|
+
await (0, users_1.Users)(userContext.userDataContext).save({
|
|
424
|
+
userId: heldId,
|
|
425
|
+
name: "Eve",
|
|
426
|
+
publicKey: heldKeys.publicKey,
|
|
427
|
+
publicBoxKey: heldKeys.publicBoxKey,
|
|
428
|
+
anchors: [anchor],
|
|
429
|
+
});
|
|
430
|
+
(0, anchor_resolution_1.setAnchorFetcher)(async () => ({
|
|
431
|
+
outcome: "document",
|
|
432
|
+
document: {
|
|
433
|
+
version: 1,
|
|
434
|
+
userId: heldId,
|
|
435
|
+
keys: [{ publicKey: attack.publicKey, issuedAt: "2026-10-03T18:00:00.000Z" }],
|
|
436
|
+
anchors: [anchor],
|
|
437
|
+
updatedAt: "2026-10-03T18:00:00.000Z",
|
|
438
|
+
},
|
|
439
|
+
}));
|
|
440
|
+
(0, key_change_1.holdKeyContest)((0, key_contest_1.signKeyContest)({
|
|
441
|
+
purpose: "peers-key-contest",
|
|
442
|
+
userId: heldId,
|
|
443
|
+
publicKey: heldKeys.publicKey,
|
|
444
|
+
pendingKey: attack.publicKey,
|
|
445
|
+
at: new Date().toISOString(),
|
|
446
|
+
}, heldKeys.secretKey));
|
|
447
|
+
process.env.PEERS_RECOVERY_DELAY_MS = "0";
|
|
448
|
+
try {
|
|
449
|
+
const decision = await (0, key_change_1.evaluatePresentedKey)({
|
|
450
|
+
dataContext: userContext.userDataContext,
|
|
451
|
+
userId: heldId,
|
|
452
|
+
presentedKey: attack.publicKey,
|
|
453
|
+
presentedBoxKey: attack.publicBoxKey,
|
|
454
|
+
});
|
|
455
|
+
expect(decision).toEqual({ accepted: false, state: "recovery-pending" });
|
|
456
|
+
const stored = await (0, users_1.Users)(userContext.userDataContext).get(heldId);
|
|
457
|
+
expect(stored?.publicKey).toBe(heldKeys.publicKey);
|
|
458
|
+
}
|
|
459
|
+
finally {
|
|
460
|
+
delete process.env.PEERS_RECOVERY_DELAY_MS;
|
|
461
|
+
}
|
|
462
|
+
});
|
|
463
|
+
it("asks the anchor for contests once per cache window while a recovery waits", async () => {
|
|
464
|
+
// The recovering device handshakes often; each one re-evaluates the cached row and must
|
|
465
|
+
// not turn into a contest lookup at the anchor every time.
|
|
466
|
+
const waitingId = (0, utils_1.newid)();
|
|
467
|
+
const waitingKeys = (0, keys_1.newKeys)();
|
|
468
|
+
const recovered = (0, keys_1.newKeys)();
|
|
469
|
+
await (0, users_1.Users)(userContext.userDataContext).save({
|
|
470
|
+
userId: waitingId,
|
|
471
|
+
name: "Finn",
|
|
472
|
+
publicKey: waitingKeys.publicKey,
|
|
473
|
+
publicBoxKey: waitingKeys.publicBoxKey,
|
|
474
|
+
anchors: [anchor],
|
|
475
|
+
});
|
|
476
|
+
(0, anchor_resolution_1.setAnchorFetcher)(async () => ({
|
|
477
|
+
outcome: "document",
|
|
478
|
+
document: {
|
|
479
|
+
version: 1,
|
|
480
|
+
userId: waitingId,
|
|
481
|
+
keys: [{ publicKey: recovered.publicKey, issuedAt: "2026-10-03T18:00:00.000Z" }],
|
|
482
|
+
anchors: [anchor],
|
|
483
|
+
updatedAt: "2026-10-03T18:00:00.000Z",
|
|
484
|
+
},
|
|
485
|
+
}));
|
|
486
|
+
const contests = jest.fn(async () => []);
|
|
487
|
+
(0, key_change_1.setContestFetcher)(contests);
|
|
488
|
+
try {
|
|
489
|
+
for (let i = 0; i < 3; i++) {
|
|
490
|
+
const decision = await (0, key_change_1.evaluatePresentedKey)({
|
|
491
|
+
dataContext: userContext.userDataContext,
|
|
492
|
+
userId: waitingId,
|
|
493
|
+
presentedKey: recovered.publicKey,
|
|
494
|
+
presentedBoxKey: recovered.publicBoxKey,
|
|
495
|
+
});
|
|
496
|
+
expect(decision).toEqual({ accepted: false, state: "recovery-pending" });
|
|
497
|
+
}
|
|
498
|
+
expect(contests).toHaveBeenCalledTimes(1);
|
|
499
|
+
// Once the delay is up it asks the anchor one more time before adopting, so a contest
|
|
500
|
+
// posted there during the wait is not missed.
|
|
501
|
+
contests.mockResolvedValueOnce([
|
|
502
|
+
(0, key_contest_1.signKeyContest)({
|
|
503
|
+
purpose: "peers-key-contest",
|
|
504
|
+
userId: waitingId,
|
|
505
|
+
publicKey: waitingKeys.publicKey,
|
|
506
|
+
pendingKey: recovered.publicKey,
|
|
507
|
+
at: new Date().toISOString(),
|
|
508
|
+
}, waitingKeys.secretKey),
|
|
509
|
+
]);
|
|
510
|
+
process.env.PEERS_RECOVERY_DELAY_MS = "0";
|
|
511
|
+
const due = await (0, key_change_1.evaluatePresentedKey)({
|
|
512
|
+
dataContext: userContext.userDataContext,
|
|
513
|
+
userId: waitingId,
|
|
514
|
+
presentedKey: recovered.publicKey,
|
|
515
|
+
presentedBoxKey: recovered.publicBoxKey,
|
|
516
|
+
});
|
|
517
|
+
expect(contests).toHaveBeenCalledTimes(2);
|
|
518
|
+
expect(due).toEqual({ accepted: false, state: "recovery-pending" });
|
|
519
|
+
const stored = await (0, users_1.Users)(userContext.userDataContext).get(waitingId);
|
|
520
|
+
expect(stored?.publicKey).toBe(waitingKeys.publicKey);
|
|
521
|
+
}
|
|
522
|
+
finally {
|
|
523
|
+
delete process.env.PEERS_RECOVERY_DELAY_MS;
|
|
524
|
+
(0, key_change_1.setContestFetcher)(undefined);
|
|
525
|
+
}
|
|
526
|
+
});
|
|
527
|
+
it("keeps the contest this device signed so a restart still carries it", async () => {
|
|
528
|
+
// Services holds a posted contest, but with services off the handshake is the only
|
|
529
|
+
// carrier, and a device that restarts must not fall silent.
|
|
530
|
+
const pendingKey = (0, keys_1.newKeys)().publicKey;
|
|
531
|
+
const signed = (0, key_contest_1.signKeyContest)({
|
|
532
|
+
purpose: "peers-key-contest",
|
|
533
|
+
userId,
|
|
534
|
+
publicKey: next.publicKey,
|
|
535
|
+
pendingKey,
|
|
536
|
+
at: new Date().toISOString(),
|
|
537
|
+
}, next.secretKey);
|
|
538
|
+
await (0, key_change_1.setLocalKeyContest)(signed);
|
|
539
|
+
expect((0, key_change_1.peekLocalKeyContest)()).toEqual(signed);
|
|
540
|
+
const stored = await (0, persistent_vars_1.PersistentVars)(userContext.userDataContext).findOne({
|
|
541
|
+
name: "identityLocalKeyContest",
|
|
542
|
+
});
|
|
543
|
+
expect(stored?.value.value).toEqual(signed);
|
|
544
|
+
await (0, key_change_1.setLocalKeyContest)(undefined);
|
|
545
|
+
expect((0, key_change_1.peekLocalKeyContest)()).toBeUndefined();
|
|
546
|
+
});
|
|
547
|
+
it("tells this device once that a due recovery rotated its key away", async () => {
|
|
548
|
+
const before = await (0, users_1.Users)(userContext.userDataContext).get(userId);
|
|
549
|
+
const recovered = (0, keys_1.newKeys)();
|
|
550
|
+
(0, anchor_resolution_1.setAnchorFetcher)(async () => ({ outcome: "document", document: doc(recovered.publicKey) }));
|
|
551
|
+
const seen = [];
|
|
552
|
+
const subscription = key_change_1.identityRotatedAwayEvent.event.subscribe((payload) => {
|
|
553
|
+
seen.push(payload);
|
|
554
|
+
});
|
|
555
|
+
process.env.PEERS_RECOVERY_DELAY_MS = "0";
|
|
556
|
+
try {
|
|
557
|
+
const payload = await (0, key_change_1.detectIdentityRotatedAway)({ userId, publicKey: next.publicKey }, {
|
|
558
|
+
userId,
|
|
559
|
+
publicKey: recovered.publicKey,
|
|
560
|
+
publicBoxKey: recovered.publicBoxKey,
|
|
561
|
+
});
|
|
562
|
+
expect(payload).toMatchObject({
|
|
563
|
+
oldPublicKey: next.publicKey,
|
|
564
|
+
newPublicKey: recovered.publicKey,
|
|
565
|
+
});
|
|
566
|
+
expect(seen).toHaveLength(1);
|
|
567
|
+
// The device does not adopt the key on its own row; it has to be paired again.
|
|
568
|
+
const stored = await (0, users_1.Users)(userContext.userDataContext).get(userId);
|
|
569
|
+
expect(stored?.publicKey).toBe(before?.publicKey);
|
|
570
|
+
}
|
|
571
|
+
finally {
|
|
572
|
+
delete process.env.PEERS_RECOVERY_DELAY_MS;
|
|
573
|
+
subscription.unsubscribe();
|
|
574
|
+
}
|
|
575
|
+
});
|
|
576
|
+
/** A contact row with the given anchors, returning its ids and keys. */
|
|
577
|
+
async function contact(name) {
|
|
578
|
+
const contactId = (0, utils_1.newid)();
|
|
579
|
+
const keys = (0, keys_1.newKeys)();
|
|
580
|
+
await (0, users_1.Users)(userContext.userDataContext).save({
|
|
581
|
+
userId: contactId,
|
|
582
|
+
name,
|
|
583
|
+
publicKey: keys.publicKey,
|
|
584
|
+
publicBoxKey: keys.publicBoxKey,
|
|
585
|
+
anchors: [anchor],
|
|
586
|
+
});
|
|
587
|
+
return { contactId, keys };
|
|
588
|
+
}
|
|
589
|
+
function documentFor(contactId, publicKey, maxAgeSeconds) {
|
|
590
|
+
return {
|
|
591
|
+
outcome: "document",
|
|
592
|
+
document: {
|
|
593
|
+
version: 1,
|
|
594
|
+
userId: contactId,
|
|
595
|
+
keys: [{ publicKey, issuedAt: "2026-10-03T18:00:00.000Z" }],
|
|
596
|
+
anchors: [anchor],
|
|
597
|
+
updatedAt: "2026-10-03T18:00:00.000Z",
|
|
598
|
+
},
|
|
599
|
+
...(maxAgeSeconds === undefined ? {} : { maxAgeSeconds }),
|
|
600
|
+
};
|
|
601
|
+
}
|
|
602
|
+
it("does not ask the anchors about a key whose box key is not its own", async () => {
|
|
603
|
+
// A handshake carries both keys. If they do not belong together nothing it says can be
|
|
604
|
+
// trusted, so the anchors are not consulted and nothing is cached about the key.
|
|
605
|
+
const { contactId, keys } = await contact("Gail");
|
|
606
|
+
const presented = (0, keys_1.newKeys)();
|
|
607
|
+
const fetcher = jest.fn(async () => documentFor(contactId, presented.publicKey));
|
|
608
|
+
(0, anchor_resolution_1.setAnchorFetcher)(fetcher);
|
|
609
|
+
const mismatch = await (0, key_change_1.evaluatePresentedKey)({
|
|
610
|
+
dataContext: userContext.userDataContext,
|
|
611
|
+
userId: contactId,
|
|
612
|
+
presentedKey: presented.publicKey,
|
|
613
|
+
presentedBoxKey: (0, keys_1.newKeys)().publicBoxKey,
|
|
614
|
+
});
|
|
615
|
+
expect(mismatch).toEqual({ accepted: false, state: "box-mismatch" });
|
|
616
|
+
const sameKey = await (0, key_change_1.evaluatePresentedKey)({
|
|
617
|
+
dataContext: userContext.userDataContext,
|
|
618
|
+
userId: contactId,
|
|
619
|
+
presentedKey: keys.publicKey,
|
|
620
|
+
presentedBoxKey: (0, keys_1.newKeys)().publicBoxKey,
|
|
621
|
+
});
|
|
622
|
+
expect(sameKey).toEqual({ accepted: false, state: "box-mismatch" });
|
|
623
|
+
expect(fetcher).not.toHaveBeenCalled();
|
|
624
|
+
expect(await (0, key_resolutions_1.KeyResolutions)(userContext.userDataContext).findOne({
|
|
625
|
+
userId: contactId,
|
|
626
|
+
presentedKey: presented.publicKey,
|
|
627
|
+
})).toBeFalsy();
|
|
628
|
+
expect((await (0, users_1.Users)(userContext.userDataContext).get(contactId))?.publicKey).toBe(keys.publicKey);
|
|
629
|
+
});
|
|
630
|
+
it("keeps a veto for the document's max-age, then asks the anchors again", async () => {
|
|
631
|
+
const { contactId, keys } = await contact("Hugo");
|
|
632
|
+
const presented = (0, keys_1.newKeys)();
|
|
633
|
+
const succession = (0, anchor_document_1.signSuccession)({
|
|
634
|
+
purpose: "peers-key-succession",
|
|
635
|
+
userId: contactId,
|
|
636
|
+
publicKey: presented.publicKey,
|
|
637
|
+
issuedAt: "2026-10-03T18:00:00.000Z",
|
|
638
|
+
}, keys.secretKey);
|
|
639
|
+
// The anchor still lists the old key with a 30 s max-age, so the new key is vetoed.
|
|
640
|
+
const fetcher = jest.fn(async () => documentFor(contactId, keys.publicKey, 30));
|
|
641
|
+
(0, anchor_resolution_1.setAnchorFetcher)(fetcher);
|
|
642
|
+
const evaluate = () => (0, key_change_1.evaluatePresentedKey)({
|
|
643
|
+
dataContext: userContext.userDataContext,
|
|
644
|
+
userId: contactId,
|
|
645
|
+
presentedKey: presented.publicKey,
|
|
646
|
+
presentedBoxKey: presented.publicBoxKey,
|
|
647
|
+
});
|
|
648
|
+
expect(await evaluate()).toEqual({ accepted: false, state: "vetoed" });
|
|
649
|
+
expect(await evaluate()).toEqual({ accepted: false, state: "vetoed" });
|
|
650
|
+
expect(fetcher).toHaveBeenCalledTimes(1);
|
|
651
|
+
// The anchor now lists the new key with the old key's co-signature. Within the cache
|
|
652
|
+
// window the veto stands; once it expires the anchors are asked and the row switches.
|
|
653
|
+
fetcher.mockImplementation(async () => ({
|
|
654
|
+
outcome: "document",
|
|
655
|
+
document: {
|
|
656
|
+
version: 1,
|
|
657
|
+
userId: contactId,
|
|
658
|
+
keys: [
|
|
659
|
+
{
|
|
660
|
+
publicKey: presented.publicKey,
|
|
661
|
+
issuedAt: "2026-10-03T18:00:00.000Z",
|
|
662
|
+
authorizedBy: { publicKey: succession.publicKey, signature: succession.signature },
|
|
663
|
+
},
|
|
664
|
+
],
|
|
665
|
+
anchors: [anchor],
|
|
666
|
+
updatedAt: "2026-10-03T18:00:00.000Z",
|
|
667
|
+
},
|
|
668
|
+
}));
|
|
669
|
+
expect(await evaluate()).toEqual({ accepted: false, state: "vetoed" });
|
|
670
|
+
const realNow = Date.now;
|
|
671
|
+
jest.spyOn(Date, "now").mockImplementation(() => realNow() + 31_000);
|
|
672
|
+
try {
|
|
673
|
+
expect(await evaluate()).toEqual({ accepted: true, state: "confirmed-rotation" });
|
|
674
|
+
}
|
|
675
|
+
finally {
|
|
676
|
+
jest.restoreAllMocks();
|
|
677
|
+
}
|
|
678
|
+
expect(fetcher).toHaveBeenCalledTimes(2);
|
|
679
|
+
expect((await (0, users_1.Users)(userContext.userDataContext).get(contactId))?.publicKey).toBe(presented.publicKey);
|
|
680
|
+
});
|
|
681
|
+
it("leaves the row alone while no anchor answers, and does not treat silence as a veto", async () => {
|
|
682
|
+
const { contactId, keys } = await contact("Ida");
|
|
683
|
+
const presented = (0, keys_1.newKeys)();
|
|
684
|
+
(0, anchor_resolution_1.setAnchorFetcher)(async () => ({ outcome: "abstain" }));
|
|
685
|
+
const decision = await (0, key_change_1.evaluatePresentedKey)({
|
|
686
|
+
dataContext: userContext.userDataContext,
|
|
687
|
+
userId: contactId,
|
|
688
|
+
presentedKey: presented.publicKey,
|
|
689
|
+
presentedBoxKey: presented.publicBoxKey,
|
|
690
|
+
});
|
|
691
|
+
expect(decision).toEqual({ accepted: false, state: "pending" });
|
|
692
|
+
expect((await (0, users_1.Users)(userContext.userDataContext).get(contactId))?.publicKey).toBe(keys.publicKey);
|
|
693
|
+
const cached = await (0, key_resolutions_1.KeyResolutions)(userContext.userDataContext).findOne({
|
|
694
|
+
userId: contactId,
|
|
695
|
+
presentedKey: presented.publicKey,
|
|
696
|
+
});
|
|
697
|
+
expect(cached?.state).toBe("pending");
|
|
698
|
+
});
|
|
699
|
+
it("ignores a contest that is not signed by the key on record", async () => {
|
|
700
|
+
const { contactId, keys } = await contact("Jude");
|
|
701
|
+
const recovered = (0, keys_1.newKeys)();
|
|
702
|
+
const stranger = (0, keys_1.newKeys)();
|
|
703
|
+
(0, anchor_resolution_1.setAnchorFetcher)(async () => documentFor(contactId, recovered.publicKey));
|
|
704
|
+
(0, key_change_1.holdKeyContest)((0, key_contest_1.signKeyContest)({
|
|
705
|
+
purpose: "peers-key-contest",
|
|
706
|
+
userId: contactId,
|
|
707
|
+
publicKey: stranger.publicKey,
|
|
708
|
+
pendingKey: recovered.publicKey,
|
|
709
|
+
at: new Date().toISOString(),
|
|
710
|
+
}, stranger.secretKey));
|
|
711
|
+
// A contest from the right key but naming a different pending key does not count either.
|
|
712
|
+
(0, key_change_1.holdKeyContest)((0, key_contest_1.signKeyContest)({
|
|
713
|
+
purpose: "peers-key-contest",
|
|
714
|
+
userId: contactId,
|
|
715
|
+
publicKey: keys.publicKey,
|
|
716
|
+
pendingKey: (0, keys_1.newKeys)().publicKey,
|
|
717
|
+
at: new Date().toISOString(),
|
|
718
|
+
}, keys.secretKey));
|
|
719
|
+
process.env.PEERS_RECOVERY_DELAY_MS = "0";
|
|
720
|
+
try {
|
|
721
|
+
const decision = await (0, key_change_1.evaluatePresentedKey)({
|
|
722
|
+
dataContext: userContext.userDataContext,
|
|
723
|
+
userId: contactId,
|
|
724
|
+
presentedKey: recovered.publicKey,
|
|
725
|
+
presentedBoxKey: recovered.publicBoxKey,
|
|
726
|
+
});
|
|
727
|
+
expect(decision).toEqual({ accepted: true, state: "confirmed-recovery" });
|
|
728
|
+
}
|
|
729
|
+
finally {
|
|
730
|
+
delete process.env.PEERS_RECOVERY_DELAY_MS;
|
|
731
|
+
}
|
|
732
|
+
});
|
|
733
|
+
it("adopts a waiting recovery on its own when the delay runs out", async () => {
|
|
734
|
+
// Nobody has to handshake again: the timer set when the recovery was first seen does it.
|
|
735
|
+
const { contactId, keys } = await contact("Kai");
|
|
736
|
+
const recovered = (0, keys_1.newKeys)();
|
|
737
|
+
(0, anchor_resolution_1.setAnchorFetcher)(async () => documentFor(contactId, recovered.publicKey));
|
|
738
|
+
process.env.PEERS_RECOVERY_DELAY_MS = "150";
|
|
739
|
+
try {
|
|
740
|
+
const decision = await (0, key_change_1.evaluatePresentedKey)({
|
|
741
|
+
dataContext: userContext.userDataContext,
|
|
742
|
+
userId: contactId,
|
|
743
|
+
presentedKey: recovered.publicKey,
|
|
744
|
+
presentedBoxKey: recovered.publicBoxKey,
|
|
745
|
+
});
|
|
746
|
+
expect(decision).toEqual({ accepted: false, state: "recovery-pending" });
|
|
747
|
+
expect((await (0, users_1.Users)(userContext.userDataContext).get(contactId))?.publicKey).toBe(keys.publicKey);
|
|
748
|
+
expect((0, key_change_1.identityRecoveryNoticesVar)()).toContainEqual(expect.objectContaining({ userId: contactId, pendingPublicKey: recovered.publicKey }));
|
|
749
|
+
await new Promise((resolve) => setTimeout(resolve, 400));
|
|
750
|
+
expect((await (0, users_1.Users)(userContext.userDataContext).get(contactId))?.publicKey).toBe(recovered.publicKey);
|
|
751
|
+
expect((0, key_change_1.identityRecoveryNoticesVar)()).not.toContainEqual(expect.objectContaining({ userId: contactId, pendingPublicKey: recovered.publicKey }));
|
|
752
|
+
}
|
|
753
|
+
finally {
|
|
754
|
+
delete process.env.PEERS_RECOVERY_DELAY_MS;
|
|
755
|
+
}
|
|
756
|
+
});
|
|
757
|
+
it("records a contest from the key on record and refuses one from anyone else", async () => {
|
|
758
|
+
const { contactId, keys } = await contact("Lena");
|
|
759
|
+
const recovered = (0, keys_1.newKeys)();
|
|
760
|
+
const stranger = (0, keys_1.newKeys)();
|
|
761
|
+
(0, anchor_resolution_1.setAnchorFetcher)(async () => documentFor(contactId, recovered.publicKey));
|
|
762
|
+
process.env.PEERS_RECOVERY_DELAY_MS = String(60 * 60 * 1000);
|
|
763
|
+
try {
|
|
764
|
+
await (0, key_change_1.evaluatePresentedKey)({
|
|
765
|
+
dataContext: userContext.userDataContext,
|
|
766
|
+
userId: contactId,
|
|
767
|
+
presentedKey: recovered.publicKey,
|
|
768
|
+
presentedBoxKey: recovered.publicBoxKey,
|
|
769
|
+
});
|
|
770
|
+
const statement = {
|
|
771
|
+
purpose: "peers-key-contest",
|
|
772
|
+
userId: contactId,
|
|
773
|
+
pendingKey: recovered.publicKey,
|
|
774
|
+
at: new Date().toISOString(),
|
|
775
|
+
};
|
|
776
|
+
expect(await (0, key_change_1.applyKeyContest)(userContext.userDataContext, (0, key_contest_1.signKeyContest)({ ...statement, publicKey: stranger.publicKey }, stranger.secretKey))).toBe(false);
|
|
777
|
+
let row = await (0, key_resolutions_1.KeyResolutions)(userContext.userDataContext).findOne({
|
|
778
|
+
userId: contactId,
|
|
779
|
+
presentedKey: recovered.publicKey,
|
|
780
|
+
});
|
|
781
|
+
expect(row?.contested).toBe(false);
|
|
782
|
+
expect(await (0, key_change_1.applyKeyContest)(userContext.userDataContext, (0, key_contest_1.signKeyContest)({ ...statement, publicKey: keys.publicKey }, keys.secretKey))).toBe(true);
|
|
783
|
+
row = await (0, key_resolutions_1.KeyResolutions)(userContext.userDataContext).findOne({
|
|
784
|
+
userId: contactId,
|
|
785
|
+
presentedKey: recovered.publicKey,
|
|
786
|
+
});
|
|
787
|
+
expect(row?.contested).toBe(true);
|
|
788
|
+
expect((0, key_change_1.identityRecoveryNoticesVar)()).toContainEqual(expect.objectContaining({ pendingPublicKey: recovered.publicKey, contested: true }));
|
|
789
|
+
// Even with the delay gone the contested recovery stays held...
|
|
790
|
+
process.env.PEERS_RECOVERY_DELAY_MS = "0";
|
|
791
|
+
const held = await (0, key_change_1.evaluatePresentedKey)({
|
|
792
|
+
dataContext: userContext.userDataContext,
|
|
793
|
+
userId: contactId,
|
|
794
|
+
presentedKey: recovered.publicKey,
|
|
795
|
+
presentedBoxKey: recovered.publicBoxKey,
|
|
796
|
+
});
|
|
797
|
+
expect(held).toEqual({ accepted: false, state: "recovery-pending" });
|
|
798
|
+
// ...until the person at this device accepts it.
|
|
799
|
+
await (0, key_change_1.acceptRecovery)(userContext.userDataContext, contactId, recovered.publicKey);
|
|
800
|
+
expect((await (0, users_1.Users)(userContext.userDataContext).get(contactId))?.publicKey).toBe(recovered.publicKey);
|
|
801
|
+
expect((0, key_change_1.identityRecoveryNoticesVar)()).not.toContainEqual(expect.objectContaining({ pendingPublicKey: recovered.publicKey }));
|
|
802
|
+
}
|
|
803
|
+
finally {
|
|
804
|
+
delete process.env.PEERS_RECOVERY_DELAY_MS;
|
|
805
|
+
}
|
|
806
|
+
});
|
|
807
|
+
it("refuses to accept a recovery that is not waiting", async () => {
|
|
808
|
+
const { contactId } = await contact("Mo");
|
|
809
|
+
await expect((0, key_change_1.acceptRecovery)(userContext.userDataContext, contactId, (0, keys_1.newKeys)().publicKey)).rejects.toThrow(/No recovery is waiting/);
|
|
810
|
+
});
|
|
811
|
+
});
|