provared 0.5.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1496 -0
- package/README.md +18 -3
- package/bin/provared-check.mjs +128 -13
- package/docs/guide.md +346 -24
- package/docs/threat-model.md +299 -21
- package/net/headers.js +1 -0
- package/package.json +2 -2
- package/page/check-page.js +18 -32
- package/page/index.html +6 -0
- package/page/render.js +183 -10
- package/spec/provared-format.md +771 -37
- package/src/book.js +544 -19
- package/src/check.d.ts +4 -0
- package/src/check.js +3 -1
- package/src/denial.js +123 -0
- package/src/der.js +56 -6
- package/src/dev.d.ts +1 -1
- package/src/dev.js +1 -1
- package/src/documents.js +8 -3
- package/src/guard.js +12 -1
- package/src/index.js +10 -0
- package/src/jws.js +8 -0
- package/src/keys.js +48 -1
- package/src/recorder.js +131 -5
- package/src/seal.js +32 -4
- package/src/sealer.js +812 -0
- package/src/signatures.js +12 -6
- package/src/slip.js +72 -5
- package/src/timestamp.js +55 -16
- package/src/tools.js +30 -13
- package/src/tree.js +139 -7
- package/src/types.d.ts +272 -11
- package/src/witness.js +63 -0
- package/src/witnessing.js +362 -0
package/src/recorder.js
CHANGED
|
@@ -38,12 +38,18 @@ import { KEY_SET_METHODS } from './keys.js';
|
|
|
38
38
|
import { CLOCK_ALLOWANCE_MS, MAX_STAMPS } from './seal.js';
|
|
39
39
|
import { holdSigningKeys } from './signatures.js';
|
|
40
40
|
import { writeAcknowledgement } from './standing.js';
|
|
41
|
+
import { denialCodes, writeDenial } from './denial.js';
|
|
41
42
|
import { validateRequest, writeStub } from './stub.js';
|
|
42
43
|
|
|
43
44
|
/** How long the other side is given to hand back its countersignature, unless the caller says otherwise: 30 seconds. */
|
|
44
45
|
export const COUNTERSIGN_WITHIN_MS = 30000;
|
|
45
46
|
/** How long a signer is given for each signature, unless the caller says otherwise: 30 seconds. */
|
|
46
47
|
export const SIGN_WITHIN_MS = 30000;
|
|
48
|
+
/** The most denials a writer writes in any hour, unless the caller says otherwise: 600. */
|
|
49
|
+
export const DENIALS_PER_HOUR = 600;
|
|
50
|
+
// The most a caller may set it to: as many as a book may hold entries.
|
|
51
|
+
const MAX_DENIALS_PER_HOUR = 100000;
|
|
52
|
+
const HOUR_MS = 3600000;
|
|
47
53
|
// The longest wait a timer can hold.
|
|
48
54
|
const MAX_WAIT_MS = 2 ** 31 - 1;
|
|
49
55
|
// "detailsAfter" in "act": the most documents it may add, and the most the
|
|
@@ -227,11 +233,29 @@ function plainHeld(copy) {
|
|
|
227
233
|
* @param {number} [o.countersignWithin] how long the other side is given to countersign, in milliseconds; defaults to 30,000
|
|
228
234
|
* @param {number} [o.signWithin] how long a signer is given for each signature, in milliseconds; defaults to 30,000.
|
|
229
235
|
* After that the action is not taken and nothing is written; a late answer is not used
|
|
230
|
-
* @
|
|
236
|
+
* @param {boolean} [o.denials] true to write a denial (format description, section 28) whenever "act" does not take an
|
|
237
|
+
* action, and to let "deny" write one; defaults to false, and then no denial is written
|
|
238
|
+
* @param {number} [o.denialsPerHour] the most denials written in any hour, by the clock; defaults to 600. Each
|
|
239
|
+
* denial this writer writes, or is handed through "add", counts at the clock's time when it joins the book, whatever
|
|
240
|
+
* date it states; those the book held when the writer was opened count at the dates they state. Past the limit, a
|
|
241
|
+
* denial is not written, and the answers count those not written
|
|
242
|
+
* @returns {Promise<object>} the writer: before, record, act, deny, book, check, cancellations, add
|
|
231
243
|
* @throws {Refusal} "record-not-sound" if the book so far has a problem; "bad-field" or "bad-key" if an item of
|
|
232
244
|
* privateKeys is neither a key nor a signer, or is a signer that holds another key than the slip, or the pass, names
|
|
233
245
|
*/
|
|
234
|
-
export async function openRecorder({
|
|
246
|
+
export async function openRecorder({
|
|
247
|
+
book,
|
|
248
|
+
slip,
|
|
249
|
+
privateKeys,
|
|
250
|
+
issuerKeys,
|
|
251
|
+
pass,
|
|
252
|
+
options = {},
|
|
253
|
+
now = Date.now,
|
|
254
|
+
countersignWithin = COUNTERSIGN_WITHIN_MS,
|
|
255
|
+
signWithin = SIGN_WITHIN_MS,
|
|
256
|
+
denials = false,
|
|
257
|
+
denialsPerHour = DENIALS_PER_HOUR,
|
|
258
|
+
}) {
|
|
235
259
|
if (typeof now !== 'function') throw new Refusal('bad-field', 'now: must be a function that gives the time in milliseconds.');
|
|
236
260
|
// A clock that gives anything but a time is a clock that failed: no comparison with it would hold.
|
|
237
261
|
const device = now;
|
|
@@ -246,6 +270,10 @@ export async function openRecorder({ book, slip, privateKeys, issuerKeys, pass,
|
|
|
246
270
|
if (!(Number.isFinite(signWithin) && signWithin >= 1 && signWithin <= MAX_WAIT_MS)) {
|
|
247
271
|
throw new Refusal('bad-field', 'signWithin: must be a number of milliseconds from 1 to 2,147,483,647.');
|
|
248
272
|
}
|
|
273
|
+
if (typeof denials !== 'boolean') throw new Refusal('bad-field', 'denials: must be true or false.');
|
|
274
|
+
if (!(Number.isInteger(denialsPerHour) && denialsPerHour >= 1 && denialsPerHour <= MAX_DENIALS_PER_HOUR)) {
|
|
275
|
+
throw new Refusal('bad-field', 'denialsPerHour: must be a whole number from 1 to 100,000.');
|
|
276
|
+
}
|
|
249
277
|
// Whom the writer trusts is fixed when it is opened: read once, as a whole
|
|
250
278
|
// check reads it, and copied. The passkeys it trusts are its own
|
|
251
279
|
// "issuerKeys", whatever the options say, read through the same fixed
|
|
@@ -360,12 +388,23 @@ export async function openRecorder({ book, slip, privateKeys, issuerKeys, pass,
|
|
|
360
388
|
const { result } = await standing();
|
|
361
389
|
if (result.summary.problemFound) throw notWritten('A problem was found in the record so far. It must be looked at before the agent acts again.');
|
|
362
390
|
let after = lastStub(result.entries);
|
|
391
|
+
// The times of the denials of this writer's own chain, which the hourly
|
|
392
|
+
// limit counts: those the book held when the writer was opened, at the
|
|
393
|
+
// dates they state; and each one that joins the book since, at the
|
|
394
|
+
// clock's time when it joined, whatever date it states (a denial may be
|
|
395
|
+
// dated up to 300 seconds back, and counting it there would let more
|
|
396
|
+
// than the limit be written in an hour of the clock).
|
|
397
|
+
const ownDenial = (e) => e.kind === 'denial' && e.problems.length === 0 && e.slip === slip && (e.pass ?? null) === (pass ?? null);
|
|
398
|
+
const denialTimes = result.entries.filter(ownDenial).map((e) => parseTime(e.content.when));
|
|
399
|
+
// How many denials were not written because of the hourly limit, since the writer was opened.
|
|
400
|
+
let notWrittenDenials = 0;
|
|
363
401
|
// What was given at opening: the first sound acknowledgement of this
|
|
364
402
|
// writer's own agent that came with a cancellation is the one it goes on
|
|
365
403
|
// handing back; a cancellation that the book does not hold waits to be
|
|
366
404
|
// written.
|
|
405
|
+
// (Seals held elsewhere, handed over with the options, come after the cancellations, and are not these.)
|
|
367
406
|
result.held.forEach((h, i) => {
|
|
368
|
-
if (!h.used) return;
|
|
407
|
+
if (h.kind !== 'cancellation' || !h.used) return;
|
|
369
408
|
// The person's cancellation of this writer's own slip, given when it was opened, stops it from the start.
|
|
370
409
|
if (h.slip === slip) stopped = true;
|
|
371
410
|
h.acknowledgements.forEach((a, j) => {
|
|
@@ -390,7 +429,7 @@ export async function openRecorder({ book, slip, privateKeys, issuerKeys, pass,
|
|
|
390
429
|
// holds without it, follows the cancellation into the book: it was still
|
|
391
430
|
// waiting when the writer that signed it handed it back.
|
|
392
431
|
result.held.forEach((h, i) => {
|
|
393
|
-
if (h.used && h.slip === slip && h.inBook !== null && acknowledged.has(h.fingerprint) && !written.has(h.fingerprint)) waiting.set(h.fingerprint, given[i].cancellation);
|
|
432
|
+
if (h.kind === 'cancellation' && h.used && h.slip === slip && h.inBook !== null && acknowledged.has(h.fingerprint) && !written.has(h.fingerprint)) waiting.set(h.fingerprint, given[i].cancellation);
|
|
394
433
|
});
|
|
395
434
|
// The services the slip names, to check a countersignature before it goes into the book.
|
|
396
435
|
const slipEntry = result.entries.find((e) => e.kind === 'slip' && e.fingerprint === slip);
|
|
@@ -446,6 +485,10 @@ export async function openRecorder({ book, slip, privateKeys, issuerKeys, pass,
|
|
|
446
485
|
detailsAfter: more.detailsAfter,
|
|
447
486
|
hasApproval: more.approval !== undefined && more.approval !== null,
|
|
448
487
|
approval: undefined,
|
|
488
|
+
// For "act": false writes no denial for this call. For "deny": who said no, and why.
|
|
489
|
+
denial: more.denial,
|
|
490
|
+
source: more.source,
|
|
491
|
+
codes: more.codes === undefined ? undefined : plain(more.codes, 'codes'),
|
|
449
492
|
};
|
|
450
493
|
if (call.hasApproval) {
|
|
451
494
|
try {
|
|
@@ -615,6 +658,54 @@ export async function openRecorder({ book, slip, privateKeys, issuerKeys, pass,
|
|
|
615
658
|
return { fits: true, checked };
|
|
616
659
|
};
|
|
617
660
|
|
|
661
|
+
// A denial: the agent's side states that it did not take an action, and
|
|
662
|
+
// why. It is written into the book only if the book still passes its check
|
|
663
|
+
// with it and it is not dated ahead of the clock; it is tried on a reader
|
|
664
|
+
// split off the book's own, as a stub is, because a denial changes nothing
|
|
665
|
+
// read before it. At most "denialsPerHour" are written in any hour, by
|
|
666
|
+
// the clock: past it, none is written and the writer counts it. Whatever
|
|
667
|
+
// happens, the answer says what was done; nothing is thrown, so a denial
|
|
668
|
+
// never stands in the way of what the writer does next. The request is the
|
|
669
|
+
// copy made when the call was made, and only its action, amount, service
|
|
670
|
+
// and documents are written: the documents are fingerprints, never the
|
|
671
|
+
// documents themselves.
|
|
672
|
+
const denialAnswer = (problem) => ({ written: false, problem, notWritten: notWrittenDenials });
|
|
673
|
+
const writeDenialLine = async (request, source, codes, when) => {
|
|
674
|
+
try {
|
|
675
|
+
const at = now();
|
|
676
|
+
const t = when ?? at;
|
|
677
|
+
if (!Number.isFinite(t)) return denialAnswer({ code: 'bad-field', message: 'when: does not give a time.' });
|
|
678
|
+
if (aheadOfClock(t) || behindClock(t)) {
|
|
679
|
+
return denialAnswer({ code: 'record-not-sound', message: 'The denial was not written: a denial is written when the action is refused, so its date must be within 300 seconds of the stub writer\'s clock.' });
|
|
680
|
+
}
|
|
681
|
+
const inHour = denialTimes.filter((d) => d > at - HOUR_MS).length;
|
|
682
|
+
if (inHour >= denialsPerHour) {
|
|
683
|
+
notWrittenDenials++;
|
|
684
|
+
return denialAnswer({
|
|
685
|
+
code: 'denials-over-limit',
|
|
686
|
+
message: `The denial was not written: this stub writer has written ${denialsPerHour} denials in the last hour, the most it may. Since it was opened, ${notWrittenDenials} were not written for that reason.`,
|
|
687
|
+
});
|
|
688
|
+
}
|
|
689
|
+
const fields = { slip, action: request.action, source, when: t };
|
|
690
|
+
for (const name of ['amount', 'with', 'details']) if (request[name] !== undefined) fields[name] = request[name];
|
|
691
|
+
if (pass) fields.pass = pass;
|
|
692
|
+
if (codes) fields.codes = codes;
|
|
693
|
+
const made = await writeDenial(fields, signing);
|
|
694
|
+
const line = entryLine({ denial: made.record });
|
|
695
|
+
if (utf8(line).length > MAX_LINE_BYTES) return denialAnswer({ code: 'too-large', message: 'The denial is longer than a line of a book may be.' });
|
|
696
|
+
const tried = forkCarried(carried);
|
|
697
|
+
await carryOn(tried, line + '\n');
|
|
698
|
+
const checked = (await standing(tried)).result;
|
|
699
|
+
if (checked.summary.problemFound) return denialAnswer({ code: 'record-not-sound', message: 'The denial was not written: with it, the book would not pass its check.' });
|
|
700
|
+
carried = promoteCarried(tried);
|
|
701
|
+
text += line + '\n';
|
|
702
|
+
denialTimes.push(at);
|
|
703
|
+
return { written: true, fingerprint: made.fingerprint, line, notWritten: notWrittenDenials };
|
|
704
|
+
} catch (e) {
|
|
705
|
+
return denialAnswer(problemFrom(e));
|
|
706
|
+
}
|
|
707
|
+
};
|
|
708
|
+
|
|
618
709
|
// A cancellation held beside the book is written into the book as soon as
|
|
619
710
|
// it can be: once the date it gives is no longer ahead of the clock. It
|
|
620
711
|
// goes in as the entry it is, without the acknowledgements that came with
|
|
@@ -924,7 +1015,10 @@ export async function openRecorder({ book, slip, privateKeys, issuerKeys, pass,
|
|
|
924
1015
|
stop(problemFrom(e));
|
|
925
1016
|
}
|
|
926
1017
|
}
|
|
927
|
-
if (!answer.allowed)
|
|
1018
|
+
if (!answer.allowed) {
|
|
1019
|
+
if (!denials || call.denial === false) return { done: false, answer };
|
|
1020
|
+
return { done: false, answer, denial: await writeDenialLine(call.request, 'check', denialCodes(answer), undefined) };
|
|
1021
|
+
}
|
|
928
1022
|
const outcome = await runInside(perform);
|
|
929
1023
|
if (!hasLater) {
|
|
930
1024
|
const stub = await commit(await countersigned(made, call), made, tried);
|
|
@@ -980,6 +1074,35 @@ export async function openRecorder({ book, slip, privateKeys, issuerKeys, pass,
|
|
|
980
1074
|
});
|
|
981
1075
|
},
|
|
982
1076
|
|
|
1077
|
+
/**
|
|
1078
|
+
* Write a denial for an action that was not taken, where "act" did not
|
|
1079
|
+
* write one: the person refused, or gave no approval when asked
|
|
1080
|
+
* ("person"); a rule of the software around the agent, outside the slip,
|
|
1081
|
+
* said no ("rule"); or the check before acting said no, and the caller
|
|
1082
|
+
* asked "act" to write no denial for that call ("check", with the codes
|
|
1083
|
+
* of that answer: denialCodes gives them). Nothing is taken and no stub
|
|
1084
|
+
* is written. A writer opened without "denials: true" writes none.
|
|
1085
|
+
* @param {{action: string, amount?: object, with?: string, details?: object[]}} request what was asked for
|
|
1086
|
+
* @param {{source: 'check'|'person'|'rule', codes?: string[], when?: number|Date}} more "codes" only for "check";
|
|
1087
|
+
* "when" defaults to the clock, and must be within 300 seconds of it
|
|
1088
|
+
* @returns {Promise<{written: boolean, fingerprint?: string, line?: string, problem?: object, notWritten: number}>}
|
|
1089
|
+
* "notWritten": how many denials were not written because of the hourly limit since the writer was opened
|
|
1090
|
+
*/
|
|
1091
|
+
deny(request, more = {}) {
|
|
1092
|
+
return calling(request, more, async (call) => {
|
|
1093
|
+
if (!denials) return denialAnswer({ code: 'bad-field', message: 'This stub writer was opened without "denials: true", so it writes no denial.' });
|
|
1094
|
+
if (call.source !== 'check' && call.source !== 'person' && call.source !== 'rule') return denialAnswer({ code: 'bad-field', message: 'source: must be "check", "person" or "rule".' });
|
|
1095
|
+
let at;
|
|
1096
|
+
try {
|
|
1097
|
+
at = now();
|
|
1098
|
+
} catch (e) {
|
|
1099
|
+
return denialAnswer(problemFrom(e));
|
|
1100
|
+
}
|
|
1101
|
+
await writeWaiting(at);
|
|
1102
|
+
return writeDenialLine(call.request, call.source, call.codes, call.when);
|
|
1103
|
+
});
|
|
1104
|
+
},
|
|
1105
|
+
|
|
983
1106
|
/** The book as it now stands. */
|
|
984
1107
|
book() {
|
|
985
1108
|
return text;
|
|
@@ -1220,6 +1343,7 @@ export async function openRecorder({ book, slip, privateKeys, issuerKeys, pass,
|
|
|
1220
1343
|
// An added stub of this writer's own chain moves its place in the chain on.
|
|
1221
1344
|
after = lastStub(whole.checked.entries);
|
|
1222
1345
|
const last = whole.checked.entries.at(-1);
|
|
1346
|
+
if (ownDenial(last)) denialTimes.push(at);
|
|
1223
1347
|
if (last.kind === 'cancellation' && last.slip === slip && last.verified === 'all') {
|
|
1224
1348
|
stopped = true;
|
|
1225
1349
|
return { acknowledgement: await acknowledge(last.fingerprint, JSON.parse(line).cancellation, true, at) };
|
|
@@ -1275,5 +1399,7 @@ export async function openRecorder({ book, slip, privateKeys, issuerKeys, pass,
|
|
|
1275
1399
|
});
|
|
1276
1400
|
},
|
|
1277
1401
|
};
|
|
1402
|
+
// Whether this writer writes denials, as it was opened. It cannot be changed.
|
|
1403
|
+
Object.defineProperty(writer, 'denials', { value: denials, enumerable: true });
|
|
1278
1404
|
return writer;
|
|
1279
1405
|
}
|
package/src/seal.js
CHANGED
|
@@ -25,7 +25,7 @@ export const CLOCK_ALLOWANCE_MS = 300 * 1000;
|
|
|
25
25
|
* @param {any} c
|
|
26
26
|
*/
|
|
27
27
|
export function validateSealContent(c) {
|
|
28
|
-
f.members(c, ['by', 'id', 'root', 'size', 'type', 'when'], ['previous'], 'seal');
|
|
28
|
+
f.members(c, ['by', 'id', 'root', 'size', 'type', 'when'], ['book', 'previous'], 'seal');
|
|
29
29
|
f.id(c.id, 'id');
|
|
30
30
|
f.members(c.by, ['keys', 'name'], [], 'by');
|
|
31
31
|
f.label(c.by.name, 'by.name');
|
|
@@ -34,6 +34,13 @@ export function validateSealContent(c) {
|
|
|
34
34
|
if (c.size < 1) throw f.fail('size', 'must be 1 or more.');
|
|
35
35
|
f.fingerprintText(c.root, 'root');
|
|
36
36
|
if (Object.hasOwn(c, 'previous')) f.fingerprintText(c.previous, 'previous');
|
|
37
|
+
// The number of the book among the books a recorder keeps under one permission (section 21.6). Absent for the
|
|
38
|
+
// first book, so that every seal written before numbers existed reads as it did; written only from 2.
|
|
39
|
+
if (Object.hasOwn(c, 'book')) {
|
|
40
|
+
f.wholeNumber(c.book, 'book');
|
|
41
|
+
if (c.book < 2) throw f.fail('book', 'must be 2 or more: the first book has no number.');
|
|
42
|
+
if (!Object.hasOwn(c, 'previous')) throw f.fail('previous', 'is missing: every seal of a book after the first names the seal before it, and the first names the last seal of the book before.');
|
|
43
|
+
}
|
|
37
44
|
f.time(c.when, 'when');
|
|
38
45
|
}
|
|
39
46
|
|
|
@@ -102,7 +109,10 @@ export async function keySetFingerprint(keys) {
|
|
|
102
109
|
* @param {string} book the book so far
|
|
103
110
|
* @param {object} fields
|
|
104
111
|
* @param {{keys: object[], name: string}} fields.by the recorder's public key set and its name (a label)
|
|
105
|
-
* @param {string} [fields.previous] the fingerprint of the seal before this one in the book, if there is one
|
|
112
|
+
* @param {string} [fields.previous] the fingerprint of the seal before this one in the book, if there is one; for
|
|
113
|
+
* the first seal of a book after the first, the fingerprint of the last seal of the book before
|
|
114
|
+
* @param {number} [fields.book] the book's number among the books the recorder keeps under one permission (section
|
|
115
|
+
* 21.6): 2 or more for a book after the first; 1 or none for the first, which carries no number
|
|
106
116
|
* @param {number|Date} [fields.when] defaults to now
|
|
107
117
|
* @param {any[]} privateKeys the recorder's private keys, or signers (signatures.js), in the order of the key set. A signer must hold the key "by" names in its place
|
|
108
118
|
* @returns {Promise<{record: object, fingerprint: string, fingerprintBytes: Uint8Array}>}
|
|
@@ -111,15 +121,33 @@ export async function keySetFingerprint(keys) {
|
|
|
111
121
|
export async function writeSeal(book, fields, privateKeys) {
|
|
112
122
|
const lines = book.split('\n');
|
|
113
123
|
if (lines[lines.length - 1] === '') lines.pop();
|
|
124
|
+
return sealOver(lines.length, await treeRoot(lines.map(utf8)), fields, privateKeys);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Sign a seal over a number of entries and their top fingerprint, worked out
|
|
129
|
+
* by the caller: writeSeal from the book, and the central sealer (sealer.js)
|
|
130
|
+
* from the tree it built from the leaf fingerprints it was sent. One path, so
|
|
131
|
+
* that the two give the same seal for the same book. Not part of the public
|
|
132
|
+
* interface: a size and a top fingerprint handed in from outside are never
|
|
133
|
+
* signed (choice 388).
|
|
134
|
+
* @param {number} size
|
|
135
|
+
* @param {Uint8Array} root
|
|
136
|
+
* @param {object} fields as writeSeal takes them
|
|
137
|
+
* @param {any[]} privateKeys
|
|
138
|
+
*/
|
|
139
|
+
export async function sealOver(size, root, fields, privateKeys) {
|
|
114
140
|
const content = {
|
|
115
141
|
type: KINDS.seal.type,
|
|
116
142
|
id: fields.id ?? randomId(),
|
|
117
143
|
by: fields.by,
|
|
118
|
-
size
|
|
119
|
-
root: toBase64url(
|
|
144
|
+
size,
|
|
145
|
+
root: toBase64url(root),
|
|
120
146
|
when: formatTime(fields.when ?? Date.now()),
|
|
121
147
|
};
|
|
122
148
|
if (fields.previous) content.previous = fields.previous;
|
|
149
|
+
// The book's number: 1, or none, for the first book, which then carries no number.
|
|
150
|
+
if (fields.book !== undefined && fields.book !== null && fields.book !== 1) content.book = fields.book;
|
|
123
151
|
validateSealContent(content);
|
|
124
152
|
const { contentBytes, payloadB64 } = encodeContent(content);
|
|
125
153
|
const headers = protectedHeaders('seal');
|