sigelo-contact 0.1.13 → 0.1.14

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/enrol.mjs CHANGED
@@ -10,20 +10,26 @@
10
10
  // bytes at every call: `created` pinned, the nonce from the key (SPEC §4 `nonce`),
11
11
  // rotations m → m+1 voluntary with iat 0. A second copy of a rotation is the same
12
12
  // rotation, never a fork.
13
- // operator: issue({ carrier, file, i, n, name, keeper, relay? }) → a short code (short.mjs, 10
14
- // minutes, once) for a normal invite (at `relay`, else the carrier's), and a pending
15
- // enrolment kept in `file` for 24 hours, with the operator attestation signed now (`days`,
16
- // ≤ 90; a device's 14)
13
+ // operator: issue({ carrier, file, i, n, name, keeper, relay?, operatorName?, started? }) → a short
14
+ // code (short.mjs, 10 minutes, once) for a normal invite (at `relay`, else the carrier's), and
15
+ // a pending enrolment kept in `file` for 24 hours, with the operator attestation signed now
16
+ // (`days`, ≤ 90; a device's 14). operatorName: how the worker files its operator (default
17
+ // "operator"); started: the carrier's (a search the human started, carrier.mjs POW_SEARCHES);
18
+ // confirmWords: kept with it, handed to confirm() as `ask` (the optional words switch)
17
19
  // machine: accept({ carrier, file, code, temp }) → `carrier` is a TEMPORARY identity (`temp`, its
18
20
  // file): it fetches the code, pairs, and once paired sends a request, label LABEL_ENROL,
19
21
  // {v, typ: "enrol-request", proof}
20
22
  // both: complete({ carrier, file, wait, until, keeper | place, temp, confirm }) → one inbox or more:
21
23
  // the operator answers a request whose proof matches an enrolment it issued with the
22
24
  // worker's identity, label LABEL_IDENTITY, end to end, once per enrolment (after
23
- // confirm({ words, … }) says yes, when given: the device words gate), with a long
25
+ // confirm({ words, … }) says yes, when given: the device words gate, or the operator's
26
+ // optional "confirm the four words" switch; null: not yet, the request comes back in
27
+ // `held`), with a long
24
28
  // invite the worker accepts; once the worker has paired, the operator files it (observe:
25
29
  // ours by the attestation; tier own unless the operator chose one) and forgets the
26
- // temporary DID. The machine takes the identity only from the DID it paired with
30
+ // temporary DID. `verified`: the worker that paired is the DID derived HERE from K (own
31
+ // enrolment needs no four words: the operator's side checks the result itself; THREAT-MODEL
32
+ // §2.11 says what is left). The machine takes the identity only from the DID it paired with
27
33
  // through the code, checks it (checkIdentity)
28
34
  // and hands it to place(), which writes the file and pairs as the worker; then it
29
35
  // forgets the operator on the temporary identity, which gives its mailboxes up.
@@ -155,8 +161,11 @@ export function matches(content, key, pair) {
155
161
  return d === 0;
156
162
  }
157
163
 
158
- /** The message that carries the worker: its identity file and a long invite to pair as it. */
159
- export const identityMessage = (identity, invite, worker = null) => ({ v: ENROL_V, typ: TYP_IDENTITY, identity, invite, worker });
164
+ /** The message that carries the worker: its identity file, a long invite to pair as it, and how it files its operator (optional). */
165
+ export const identityMessage = (identity, invite, worker = null, operatorName = null) => ({ v: ENROL_V, typ: TYP_IDENTITY, identity, invite, worker, ...(operatorName !== null && { operator_name: operatorName }) });
166
+ // The operator's label for itself, as the worker files it: a contact name (1 to 64, no control or bidi characters).
167
+ const LABEL_BAD = /[\u0000-\u001f\u007f-\u009f\u061c\u200e\u200f\u2028\u2029\u202a-\u202e\u2066-\u2069]/;
168
+ export const operatorLabel = (v) => (typeof v === 'string' && v.trim() !== '' && [...v.trim()].length <= 64 && !LABEL_BAD.test(v) ? v.trim() : null);
160
169
 
161
170
  /**
162
171
  * The machine: an identity message from `operator` (the contact store's record of whoever it paired
@@ -186,7 +195,8 @@ export function checkIdentity(content, { operator, relay, now }) {
186
195
  try { code = readCode(content.invite, now); } catch (e) { fail('enrol_invite', `the worker's invite does not parse (${e.message})`); }
187
196
  if (code.did !== operator.current_did) fail('enrol_invite', `the worker's invite names ${code.did}, not the operator ${operator.current_did}`);
188
197
  const worker = Number.isSafeInteger(content.worker) && content.worker >= 0 ? content.worker : null;
189
- return { identity: { v: 'sigelo/0', secret: x.secret, genesis: x.genesis, rotations: x.rotations, attestations: [{ body: b, sig: a.sig }], issuers: x.issuers }, did: r.did, original: r.chain[0], invite: content.invite, worker, exp: b.exp };
198
+ return { identity: { v: 'sigelo/0', secret: x.secret, genesis: x.genesis, rotations: x.rotations, attestations: [{ body: b, sig: a.sig }], issuers: x.issuers }, did: r.did, original: r.chain[0], invite: content.invite, worker, exp: b.exp,
199
+ operator_name: operatorLabel(content.operator_name) ?? 'operator' };
190
200
  }
191
201
 
192
202
  // ---------------------------------------------------------------- the pending file
@@ -244,17 +254,18 @@ const checkKeeper = (k) => {
244
254
  * stands for (24 hours). The operator attestation is signed here, at derivation time.
245
255
  * → { short, short_exp, code, id, did (the worker's current DID), original }.
246
256
  */
247
- export async function issue({ carrier, file, i, n, name, keeper, days = OPERATOR_DAYS, relay }) {
257
+ export async function issue({ carrier, file, i, n, name, keeper, days = OPERATOR_DAYS, relay, operatorName, started, confirmWords = false }) {
248
258
  checkKeeper(keeper);
259
+ if (operatorName !== undefined && operatorName !== null && operatorLabel(operatorName) === null) fail('enrol', 'operatorName: 1 to 64 characters, no control characters');
249
260
  const w = workerChain(keeper.K, i, n, keeper.recovery);
250
261
  // The temporary identity's contact, named apart from the worker's (forgotten once it has paired).
251
- const r = await carrier.invite({ name: `${name ?? `worker ${i}`} (enrolling)`, short: true, ...(relay !== undefined && relay !== carrier.relay && { relay }) });
262
+ const r = await carrier.invite({ name: `${name ?? `worker ${i}`} (enrolling)`, short: true, ...(relay !== undefined && relay !== carrier.relay && { relay }), ...(started !== undefined && { started }) });
252
263
  const now = nowOf(carrier), code = readCode(r.code, now), me = self(carrier, now);
253
264
  const attestation = operatorAttestation({ sign: me.sign, iss: me.did, sub: w.original, relay: code.relay, now, days });
254
265
  const old = liveHolding(carrier, w.original);
255
266
  pendingOf(carrier)(file, now, (s) => {
256
267
  s.issued[code.nonce] = { key: bytesToHex(enrolKey(code.secret)), i, n, name: name ?? null, did: w.did, original: w.original, old_pair: old ? pairOf(old) : null,
257
- relay: code.relay, exp: now + PENDING_TTL, attestation, issuer: me.genesis };
268
+ relay: code.relay, exp: now + PENDING_TTL, attestation, issuer: me.genesis, ...(operatorLabel(operatorName) !== null && { operator_name: operatorLabel(operatorName) }), ...(confirmWords && { confirm_words: true }) };
258
269
  });
259
270
  return { ...r, id: code.nonce, did: w.did, original: w.original };
260
271
  }
@@ -287,7 +298,7 @@ export async function accept({ carrier, file, code: text, name, wait = 10, temp
287
298
  */
288
299
  export async function complete({ carrier, file, wait = 0, until = () => false, keeper = null, place = null, temp = null, confirm = null }) {
289
300
  if (keeper) checkKeeper(keeper);
290
- const out = { delivered: [], placed: [], refused: [], sent: [], rest: { messages: [], events: [], dropped: {} } };
301
+ const out = { delivered: [], placed: [], refused: [], sent: [], held: [], rest: { messages: [], events: [], dropped: {} } };
291
302
  const end = Date.now() + wait * 1000;
292
303
  for (let first = true; ; first = false) {
293
304
  if (first && place) await sendRequests(carrier, file, temp, out, true);
@@ -354,12 +365,16 @@ async function onRequest(carrier, file, keeper, m, out, confirm) {
354
365
  const { id, e } = r;
355
366
  // The words gate (devices.md §5.1): the human compares the pairing's four words on this side
356
367
  // before the key goes. Declined: the enrolment is dropped, nothing is ever sent for it.
357
- if (confirm && !e.confirmed && !(await confirm({ id, name: e.name, i: e.i, did: e.did, temp: c.did, words: fingerprint(pair) }))) {
368
+ // An enrolment made with the switch on is never sent unasked: without a confirm() here, it waits.
369
+ const said = e.confirmed ? true : confirm ? await confirm({ id, name: e.name, i: e.i, did: e.did, temp: c.did, words: fingerprint(pair), ask: e.confirm_words === true }) : e.confirm_words ? null : true;
370
+ // Not yet (the page waits for the human's tap): the request is kept, nothing goes.
371
+ if (said === null || said === undefined) { out.held.push(m); return; }
372
+ if (!said) {
358
373
  pendingOf(carrier)(file, nowOf(carrier), (s) => { delete s.issued[id]; });
359
374
  out.refused.push({ check: 'device_words', detail: `the four words of ${who} were not confirmed: worker ${e.i} not sent, the code is dropped; make a new one`, from: m.from });
360
375
  return;
361
376
  }
362
- if (confirm) pendingOf(carrier)(file, nowOf(carrier), (s) => { if (s.issued[id]) s.issued[id].confirmed = true; });
377
+ if (confirm || e.confirm_words) pendingOf(carrier)(file, nowOf(carrier), (s) => { if (s.issued[id]) s.issued[id].confirmed = true; });
363
378
  try {
364
379
  // The worker's own invite, made once per enrolment: a repeated request gets the same.
365
380
  let invite = e.invite;
@@ -369,7 +384,7 @@ async function onRequest(carrier, file, keeper, m, out, confirm) {
369
384
  }
370
385
  const identity = workerIdentity({ K: keeper.K, i: e.i, n: e.n, recovery: keeper.recovery, attestation: e.attestation, issuer: e.issuer });
371
386
  if (didOf([identity.genesis, ...identity.rotations.map((x) => x.next_genesis)].at(-1)) !== e.did) fail('keeper', `worker ${e.i} derives to another DID than at enrol: the keeper root changed since (sigelo-agent keeper)`);
372
- const sent = await carrier.send(c.did, identityMessage(identity, invite, e.i), { label: LABEL_IDENTITY });
387
+ const sent = await carrier.send(c.did, identityMessage(identity, invite, e.i, e.operator_name ?? null), { label: LABEL_IDENTITY });
373
388
  out.delivered.push({ id, name: e.name, temp: c.did, did: e.did, status: sent.status });
374
389
  } catch (x) {
375
390
  if (!(x instanceof MsgError)) throw x;
@@ -387,10 +402,12 @@ async function settle(carrier, file, keeper, out) {
387
402
  const identity = workerIdentity({ K: keeper.K, i: e.i, n: e.n, recovery: keeper.recovery, attestation: e.attestation, issuer: e.issuer });
388
403
  const bundle = { v: 'sigelo/0', typ: 'bundle', genesis: identity.genesis, rotations: identity.rotations, bindings: [], attestations: identity.attestations, issuers: identity.issuers };
389
404
  const seen = carrier.observe(bundle), tier = seen?.tier ?? null, ours = seen?.ours ?? null;
405
+ // Derived here from K, not read from the pending file: the DID that paired IS worker i of these words.
406
+ const verified = c.did === didOf(identity.genesis) && c.did === workerChain(keeper.K, e.i, 0, keeper.recovery).original;
390
407
  let released = false;
391
408
  try { released = (await carrier.forget(e.temp)).released; } catch (x) { if (!(x instanceof MsgError) || x.check !== 'not_contact') throw x; }
392
409
  pendingOf(carrier)(file, nowOf(carrier), (s) => { if (s.issued[id]) s.issued[id].done = nowOf(carrier); });
393
- out.placed.push({ id, name: c.name, did: c.current_did, original: c.did, i: e.i, n: e.n, ours, tier, temp: e.temp, released, exp: e.attestation.body.exp });
410
+ out.placed.push({ id, name: c.name, did: c.current_did, original: c.did, i: e.i, n: e.n, ours, tier, temp: e.temp, released, exp: e.attestation.body.exp, verified, words: c.pair_key ? fingerprint(pairOf(c)) : null });
394
411
  }
395
412
  }
396
413