@peers-app/peers-sdk 0.27.2 → 0.27.4

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.
Files changed (260) hide show
  1. package/dist/contracts/contract-proxy.d.ts +7 -0
  2. package/dist/contracts/contract-proxy.js +19 -2
  3. package/dist/contracts/create-local-consumer.js +16 -1
  4. package/dist/contracts/index.d.ts +2 -0
  5. package/dist/contracts/index.js +2 -0
  6. package/dist/contracts/system/index.d.ts +2 -0
  7. package/dist/contracts/system/index.js +2 -0
  8. package/dist/contracts/system/invites-contract.js +2 -1
  9. package/dist/contracts/system/sync-check-contract.d.ts +97 -0
  10. package/dist/contracts/system/sync-check-contract.js +115 -0
  11. package/dist/contracts/tool-arguments.d.ts +15 -0
  12. package/dist/contracts/tool-arguments.js +41 -0
  13. package/dist/contracts/tool-catalog-service.js +2 -12
  14. package/dist/data/change-tracking.d.ts +40 -0
  15. package/dist/data/change-tracking.js +89 -0
  16. package/dist/data/orm/types.d.ts +24 -7
  17. package/dist/data/orm/types.js +97 -24
  18. package/dist/data/persistent-vars.d.ts +5 -0
  19. package/dist/data/persistent-vars.js +24 -6
  20. package/dist/define/manifest.js +6 -34
  21. package/dist/package-loader/package-loader.js +27 -22
  22. package/dist/system-ids.d.ts +5 -0
  23. package/dist/system-ids.js +6 -1
  24. package/package.json +4 -2
  25. package/dist/context/user-context-identity.test.d.ts +0 -1
  26. package/dist/context/user-context-identity.test.js +0 -220
  27. package/dist/contracts/__tests__/action-dispatch.test.d.ts +0 -1
  28. package/dist/contracts/__tests__/action-dispatch.test.js +0 -319
  29. package/dist/contracts/__tests__/also-implements.test.d.ts +0 -1
  30. package/dist/contracts/__tests__/also-implements.test.js +0 -110
  31. package/dist/contracts/__tests__/builder.test.d.ts +0 -1
  32. package/dist/contracts/__tests__/builder.test.js +0 -669
  33. package/dist/contracts/__tests__/contract-events.test.d.ts +0 -1
  34. package/dist/contracts/__tests__/contract-events.test.js +0 -199
  35. package/dist/contracts/__tests__/contract-observables.test.d.ts +0 -1
  36. package/dist/contracts/__tests__/contract-observables.test.js +0 -290
  37. package/dist/contracts/__tests__/contract-provider-router.test.d.ts +0 -1
  38. package/dist/contracts/__tests__/contract-provider-router.test.js +0 -315
  39. package/dist/contracts/__tests__/contract-proxy.test.d.ts +0 -1
  40. package/dist/contracts/__tests__/contract-proxy.test.js +0 -657
  41. package/dist/contracts/__tests__/contract-resolvers.test.d.ts +0 -1
  42. package/dist/contracts/__tests__/contract-resolvers.test.js +0 -758
  43. package/dist/contracts/__tests__/contract-subscription-lifecycle.test.d.ts +0 -1
  44. package/dist/contracts/__tests__/contract-subscription-lifecycle.test.js +0 -353
  45. package/dist/contracts/__tests__/contract-table-events.test.d.ts +0 -1
  46. package/dist/contracts/__tests__/contract-table-events.test.js +0 -209
  47. package/dist/contracts/__tests__/contract-transport.test.d.ts +0 -1
  48. package/dist/contracts/__tests__/contract-transport.test.js +0 -163
  49. package/dist/contracts/__tests__/extract.test.d.ts +0 -1
  50. package/dist/contracts/__tests__/extract.test.js +0 -145
  51. package/dist/contracts/__tests__/frozen-contracts.test.d.ts +0 -1
  52. package/dist/contracts/__tests__/frozen-contracts.test.js +0 -191
  53. package/dist/contracts/__tests__/generic-tool-dispatch.test.d.ts +0 -1
  54. package/dist/contracts/__tests__/generic-tool-dispatch.test.js +0 -130
  55. package/dist/contracts/__tests__/host-contract-client.test.d.ts +0 -1
  56. package/dist/contracts/__tests__/host-contract-client.test.js +0 -793
  57. package/dist/contracts/__tests__/host-contract-consumer.test.d.ts +0 -1
  58. package/dist/contracts/__tests__/host-contract-consumer.test.js +0 -163
  59. package/dist/contracts/__tests__/host-contract-transport.test.d.ts +0 -1
  60. package/dist/contracts/__tests__/host-contract-transport.test.js +0 -66
  61. package/dist/contracts/__tests__/integration.test.d.ts +0 -1
  62. package/dist/contracts/__tests__/integration.test.js +0 -348
  63. package/dist/contracts/__tests__/legacy-removal.test.d.ts +0 -1
  64. package/dist/contracts/__tests__/legacy-removal.test.js +0 -128
  65. package/dist/contracts/__tests__/persistent-registry.test.d.ts +0 -1
  66. package/dist/contracts/__tests__/persistent-registry.test.js +0 -109
  67. package/dist/contracts/__tests__/playground.test.d.ts +0 -1
  68. package/dist/contracts/__tests__/playground.test.js +0 -216
  69. package/dist/contracts/__tests__/registry.test.d.ts +0 -1
  70. package/dist/contracts/__tests__/registry.test.js +0 -238
  71. package/dist/contracts/__tests__/remote-contract-transport.test.d.ts +0 -1
  72. package/dist/contracts/__tests__/remote-contract-transport.test.js +0 -119
  73. package/dist/contracts/__tests__/system-contracts.test.d.ts +0 -1
  74. package/dist/contracts/__tests__/system-contracts.test.js +0 -247
  75. package/dist/contracts/__tests__/target-dispatch.d.ts +0 -11
  76. package/dist/contracts/__tests__/target-dispatch.js +0 -24
  77. package/dist/contracts/__tests__/tool-catalog-inventory.test.d.ts +0 -1
  78. package/dist/contracts/__tests__/tool-catalog-inventory.test.js +0 -44
  79. package/dist/contracts/__tests__/tool-catalog-service.test.d.ts +0 -1
  80. package/dist/contracts/__tests__/tool-catalog-service.test.js +0 -482
  81. package/dist/contracts/__tests__/tool-catalog.test.d.ts +0 -1
  82. package/dist/contracts/__tests__/tool-catalog.test.js +0 -1199
  83. package/dist/contracts/__tests__/validate.test.d.ts +0 -1
  84. package/dist/contracts/__tests__/validate.test.js +0 -991
  85. package/dist/contracts/__tests__/workflow-context.test.d.ts +0 -1
  86. package/dist/contracts/__tests__/workflow-context.test.js +0 -50
  87. package/dist/data/access-level.test.d.ts +0 -1
  88. package/dist/data/access-level.test.js +0 -53
  89. package/dist/data/change-tracking.test.d.ts +0 -1
  90. package/dist/data/change-tracking.test.js +0 -62
  91. package/dist/data/files/files.test.d.ts +0 -1
  92. package/dist/data/files/files.test.js +0 -770
  93. package/dist/data/group-members.test.d.ts +0 -1
  94. package/dist/data/group-members.test.js +0 -336
  95. package/dist/data/group-share.test.d.ts +0 -1
  96. package/dist/data/group-share.test.js +0 -110
  97. package/dist/data/groups.test.d.ts +0 -1
  98. package/dist/data/groups.test.js +0 -153
  99. package/dist/data/orm/cursor.test.d.ts +0 -1
  100. package/dist/data/orm/cursor.test.js +0 -314
  101. package/dist/data/orm/data-query.mongo.test.d.ts +0 -1
  102. package/dist/data/orm/data-query.mongo.test.js +0 -367
  103. package/dist/data/orm/data-query.sqlite.test.d.ts +0 -1
  104. package/dist/data/orm/data-query.sqlite.test.js +0 -365
  105. package/dist/data/orm/data-query.test.d.ts +0 -1
  106. package/dist/data/orm/data-query.test.js +0 -545
  107. package/dist/data/orm/dependency-injection.test.d.ts +0 -1
  108. package/dist/data/orm/dependency-injection.test.js +0 -171
  109. package/dist/data/orm/event-registry.test.d.ts +0 -1
  110. package/dist/data/orm/event-registry.test.js +0 -44
  111. package/dist/data/orm/multi-cursors.test.d.ts +0 -1
  112. package/dist/data/orm/multi-cursors.test.js +0 -463
  113. package/dist/data/orm/sql.data-source.test.d.ts +0 -1
  114. package/dist/data/orm/sql.data-source.test.js +0 -731
  115. package/dist/data/orm/table-container-events.test.d.ts +0 -1
  116. package/dist/data/orm/table-container-events.test.js +0 -101
  117. package/dist/data/orm/table-container.test.d.ts +0 -1
  118. package/dist/data/orm/table-container.test.js +0 -273
  119. package/dist/data/orm/table.event-source.test.d.ts +0 -1
  120. package/dist/data/orm/table.event-source.test.js +0 -331
  121. package/dist/data/orm/types.test.d.ts +0 -1
  122. package/dist/data/orm/types.test.js +0 -409
  123. package/dist/data/package-table-identity.test.d.ts +0 -1
  124. package/dist/data/package-table-identity.test.js +0 -46
  125. package/dist/data/package-version-permissions.test.d.ts +0 -1
  126. package/dist/data/package-version-permissions.test.js +0 -107
  127. package/dist/data/package-version-resolver.test.d.ts +0 -4
  128. package/dist/data/package-version-resolver.test.js +0 -543
  129. package/dist/data/package-versions.test.d.ts +0 -1
  130. package/dist/data/package-versions.test.js +0 -227
  131. package/dist/data/persistent-vars.conflict.test.d.ts +0 -1
  132. package/dist/data/persistent-vars.conflict.test.js +0 -126
  133. package/dist/data/persistent-vars.migration.test.d.ts +0 -1
  134. package/dist/data/persistent-vars.migration.test.js +0 -133
  135. package/dist/data/persistent-vars.package-scope.test.d.ts +0 -1
  136. package/dist/data/persistent-vars.package-scope.test.js +0 -27
  137. package/dist/data/sync-recovery-queue.test.d.ts +0 -1
  138. package/dist/data/sync-recovery-queue.test.js +0 -215
  139. package/dist/data/table-signing.test.d.ts +0 -1
  140. package/dist/data/table-signing.test.js +0 -118
  141. package/dist/data/tool-access.test.d.ts +0 -1
  142. package/dist/data/tool-access.test.js +0 -166
  143. package/dist/data/user-permissions.test.d.ts +0 -1
  144. package/dist/data/user-permissions.test.js +0 -305
  145. package/dist/data/user-trust-levels.test.d.ts +0 -1
  146. package/dist/data/user-trust-levels.test.js +0 -37
  147. package/dist/define/also-implements.test.d.ts +0 -1
  148. package/dist/define/also-implements.test.js +0 -123
  149. package/dist/define/declare-contract.test.d.ts +0 -1
  150. package/dist/define/declare-contract.test.js +0 -573
  151. package/dist/define/define.test.d.ts +0 -1
  152. package/dist/define/define.test.js +0 -1010
  153. package/dist/define/empty-fields.test.d.ts +0 -1
  154. package/dist/define/empty-fields.test.js +0 -55
  155. package/dist/define/events-observables.test.d.ts +0 -1
  156. package/dist/define/events-observables.test.js +0 -1013
  157. package/dist/define/serve-members.test.d.ts +0 -1
  158. package/dist/define/serve-members.test.js +0 -630
  159. package/dist/define/vars.test.d.ts +0 -1
  160. package/dist/define/vars.test.js +0 -39
  161. package/dist/device/binary-peer-connection-v2.test.d.ts +0 -1
  162. package/dist/device/binary-peer-connection-v2.test.js +0 -730
  163. package/dist/device/binary-peer-connection.test.d.ts +0 -1
  164. package/dist/device/binary-peer-connection.test.js +0 -184
  165. package/dist/device/connection.test.d.ts +0 -1
  166. package/dist/device/connection.test.js +0 -613
  167. package/dist/device/device-election.test.d.ts +0 -1
  168. package/dist/device/device-election.test.js +0 -516
  169. package/dist/device/device.test.d.ts +0 -1
  170. package/dist/device/device.test.js +0 -243
  171. package/dist/device/frame-cipher.test.d.ts +0 -1
  172. package/dist/device/frame-cipher.test.js +0 -219
  173. package/dist/device/get-trust-level-fn.test.d.ts +0 -1
  174. package/dist/device/get-trust-level-fn.test.js +0 -685
  175. package/dist/device/streamed-socket.test.d.ts +0 -1
  176. package/dist/device/streamed-socket.test.js +0 -216
  177. package/dist/device/tx-encoding.test.d.ts +0 -1
  178. package/dist/device/tx-encoding.test.js +0 -295
  179. package/dist/device-pairing/cpace.test.d.ts +0 -1
  180. package/dist/device-pairing/cpace.test.js +0 -116
  181. package/dist/device-pairing/device-pairing.test.d.ts +0 -1
  182. package/dist/device-pairing/device-pairing.test.js +0 -211
  183. package/dist/hlc.test.d.ts +0 -1
  184. package/dist/hlc.test.js +0 -45
  185. package/dist/host-credentials.test.d.ts +0 -1
  186. package/dist/host-credentials.test.js +0 -88
  187. package/dist/identity/account-email.test.d.ts +0 -1
  188. package/dist/identity/account-email.test.js +0 -59
  189. package/dist/identity/anchor-document.test.d.ts +0 -1
  190. package/dist/identity/anchor-document.test.js +0 -16
  191. package/dist/identity/anchor-resolution.test.d.ts +0 -1
  192. package/dist/identity/anchor-resolution.test.js +0 -130
  193. package/dist/identity/key-change.test.d.ts +0 -1
  194. package/dist/identity/key-change.test.js +0 -811
  195. package/dist/invites/group-approval.test.d.ts +0 -1
  196. package/dist/invites/group-approval.test.js +0 -223
  197. package/dist/invites/invite-token.test.d.ts +0 -1
  198. package/dist/invites/invite-token.test.js +0 -123
  199. package/dist/keys.test.d.ts +0 -1
  200. package/dist/keys.test.js +0 -336
  201. package/dist/logging/console-logger.test.d.ts +0 -1
  202. package/dist/logging/console-logger.test.js +0 -300
  203. package/dist/logging/console-logs.table.test.d.ts +0 -1
  204. package/dist/logging/console-logs.table.test.js +0 -156
  205. package/dist/observable.test.d.ts +0 -1
  206. package/dist/observable.test.js +0 -309
  207. package/dist/package-build/build-package.test.d.ts +0 -1
  208. package/dist/package-build/build-package.test.js +0 -204
  209. package/dist/package-build/contract-add.test.d.ts +0 -1
  210. package/dist/package-build/contract-add.test.js +0 -467
  211. package/dist/package-build/freeze-contracts.test.d.ts +0 -1
  212. package/dist/package-build/freeze-contracts.test.js +0 -355
  213. package/dist/package-installer/package-author-signing.test.d.ts +0 -1
  214. package/dist/package-installer/package-author-signing.test.js +0 -200
  215. package/dist/package-installer/package-cloner.test.d.ts +0 -1
  216. package/dist/package-installer/package-cloner.test.js +0 -278
  217. package/dist/package-installer/package-creator.test.d.ts +0 -1
  218. package/dist/package-installer/package-creator.test.js +0 -908
  219. package/dist/package-installer/package-dev-tools.test.d.ts +0 -1
  220. package/dist/package-installer/package-dev-tools.test.js +0 -64
  221. package/dist/package-installer/package-installer.test.d.ts +0 -1
  222. package/dist/package-installer/package-installer.test.js +0 -882
  223. package/dist/package-installer/package-propagation.test.d.ts +0 -1
  224. package/dist/package-installer/package-propagation.test.js +0 -1269
  225. package/dist/package-installer/package-publisher.test.d.ts +0 -1
  226. package/dist/package-installer/package-publisher.test.js +0 -153
  227. package/dist/package-installer/package-remote-checker.test.d.ts +0 -1
  228. package/dist/package-installer/package-remote-checker.test.js +0 -334
  229. package/dist/package-installer/package-seed-installer.test.d.ts +0 -1
  230. package/dist/package-installer/package-seed-installer.test.js +0 -123
  231. package/dist/package-installer/package-tarball.test.d.ts +0 -1
  232. package/dist/package-installer/package-tarball.test.js +0 -75
  233. package/dist/package-installer/peers-core-installer.test.d.ts +0 -1
  234. package/dist/package-installer/peers-core-installer.test.js +0 -49
  235. package/dist/package-installer/peers-core-trust-anchor.test.d.ts +0 -1
  236. package/dist/package-installer/peers-core-trust-anchor.test.js +0 -155
  237. package/dist/package-loader/contract-package-loader.test.d.ts +0 -1
  238. package/dist/package-loader/contract-package-loader.test.js +0 -274
  239. package/dist/package-loader/host-authority-loading.test.d.ts +0 -1
  240. package/dist/package-loader/host-authority-loading.test.js +0 -104
  241. package/dist/package-loader/host-dispatch-policy.test.d.ts +0 -1
  242. package/dist/package-loader/host-dispatch-policy.test.js +0 -56
  243. package/dist/package-loader/isolated-artifact.test.d.ts +0 -1
  244. package/dist/package-loader/isolated-artifact.test.js +0 -688
  245. package/dist/package-loader/isolated-persistent-var-gateway.test.d.ts +0 -1
  246. package/dist/package-loader/isolated-persistent-var-gateway.test.js +0 -56
  247. package/dist/package-loader/isolated-table-gateway.test.d.ts +0 -1
  248. package/dist/package-loader/isolated-table-gateway.test.js +0 -633
  249. package/dist/package-loader/package-loader.test.d.ts +0 -1
  250. package/dist/package-loader/package-loader.test.js +0 -1764
  251. package/dist/package-loader/resolve-consumed-contracts.test.d.ts +0 -1
  252. package/dist/package-loader/resolve-consumed-contracts.test.js +0 -166
  253. package/dist/serial-json.compat.test.d.ts +0 -1
  254. package/dist/serial-json.compat.test.js +0 -369
  255. package/dist/serial-json.test.d.ts +0 -1
  256. package/dist/serial-json.test.js +0 -401
  257. package/dist/testing/mock-host-contract.test.d.ts +0 -1
  258. package/dist/testing/mock-host-contract.test.js +0 -782
  259. package/dist/utils.test.d.ts +0 -1
  260. package/dist/utils.test.js +0 -232
@@ -1,811 +0,0 @@
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
- });