@tik-choco/mistsign 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +373 -0
- package/README.ja.md +125 -0
- package/README.md +129 -2
- package/README.zh.md +122 -0
- package/dist/agreement/bundle.d.ts +23 -0
- package/dist/agreement/bundle.js +139 -0
- package/dist/agreement/decision.d.ts +7 -0
- package/dist/agreement/decision.js +77 -0
- package/dist/agreement/errors.d.ts +9 -0
- package/dist/agreement/errors.js +19 -0
- package/dist/agreement/evaluate.d.ts +5 -0
- package/dist/agreement/evaluate.js +112 -0
- package/dist/agreement/index.d.ts +9 -0
- package/dist/agreement/index.js +6 -0
- package/dist/agreement/policy.d.ts +31 -0
- package/dist/agreement/policy.js +113 -0
- package/dist/agreement/proposal.d.ts +4 -0
- package/dist/agreement/proposal.js +107 -0
- package/dist/agreement/schema.d.ts +16 -0
- package/dist/agreement/schema.js +93 -0
- package/dist/agreement/strict.d.ts +5 -0
- package/dist/agreement/strict.js +167 -0
- package/dist/agreement/types.d.ts +214 -0
- package/dist/agreement/types.js +3 -0
- package/dist/agreement/verify.d.ts +4 -0
- package/dist/agreement/verify.js +151 -0
- package/dist/bytes.d.ts +6 -0
- package/dist/bytes.js +19 -0
- package/dist/content.d.ts +10 -0
- package/dist/content.js +58 -0
- package/dist/didKey.d.ts +4 -0
- package/dist/didKey.js +48 -0
- package/dist/elements/index.d.ts +1 -0
- package/dist/elements/index.js +2 -0
- package/dist/errors.d.ts +7 -0
- package/dist/errors.js +8 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +6 -0
- package/dist/json.d.ts +20 -0
- package/dist/json.js +170 -0
- package/dist/preact/index.d.ts +1 -0
- package/dist/preact/index.js +2 -0
- package/dist/preact/ui.css +0 -0
- package/dist/sha256.d.ts +11 -0
- package/dist/sha256.js +103 -0
- package/dist/sign.d.ts +33 -0
- package/dist/sign.js +61 -0
- package/dist/testing/index.d.ts +5 -0
- package/dist/testing/index.js +27 -0
- package/dist/time.d.ts +9 -0
- package/dist/time.js +36 -0
- package/dist/timestamp/asnTime.d.ts +2 -0
- package/dist/timestamp/asnTime.js +17 -0
- package/dist/timestamp/cms.d.ts +30 -0
- package/dist/timestamp/cms.js +126 -0
- package/dist/timestamp/constraints.d.ts +17 -0
- package/dist/timestamp/constraints.js +159 -0
- package/dist/timestamp/crypto.d.ts +6 -0
- package/dist/timestamp/crypto.js +101 -0
- package/dist/timestamp/der.d.ts +24 -0
- package/dist/timestamp/der.js +118 -0
- package/dist/timestamp/index.d.ts +2 -0
- package/dist/timestamp/index.js +1 -0
- package/dist/timestamp/name.d.ts +2 -0
- package/dist/timestamp/name.js +55 -0
- package/dist/timestamp/policy.d.ts +9 -0
- package/dist/timestamp/policy.js +56 -0
- package/dist/timestamp/verify.d.ts +33 -0
- package/dist/timestamp/verify.js +74 -0
- package/dist/timestamp/x509.d.ts +35 -0
- package/dist/timestamp/x509.js +232 -0
- package/dist/transport/index.d.ts +1 -0
- package/dist/transport/index.js +2 -0
- package/dist/trust/delegation.d.ts +15 -0
- package/dist/trust/delegation.js +40 -0
- package/dist/trust/index.d.ts +15 -0
- package/dist/trust/index.js +3 -0
- package/dist/trust/revocation.d.ts +33 -0
- package/dist/trust/revocation.js +78 -0
- package/dist/trust/signers.d.ts +31 -0
- package/dist/trust/signers.js +166 -0
- package/dist/verify.d.ts +88 -0
- package/dist/verify.js +195 -0
- package/dist/wire.d.ts +6 -0
- package/dist/wire.js +38 -0
- package/package.json +89 -4
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { isEd25519DidKey } from '../didKey.js';
|
|
2
|
+
import { nowInstant, formatTime } from '../time.js';
|
|
3
|
+
import { verifyTimestamp } from '../timestamp/index.js';
|
|
4
|
+
import { parseRoots } from '../timestamp/x509.js';
|
|
5
|
+
import { parseRevocation } from '../trust/revocation.js';
|
|
6
|
+
import { parseAllowedSigners } from '../trust/signers.js';
|
|
7
|
+
import { AgreementError, check } from './errors.js';
|
|
8
|
+
import { parseProposal } from './proposal.js';
|
|
9
|
+
import { contentId, id, label, namespaced, timestamp } from './schema.js';
|
|
10
|
+
const jsonInput = (v) => typeof v === 'string' || v instanceof Uint8Array;
|
|
11
|
+
const copy = (v) => typeof v === 'string' ? v : new Uint8Array(v);
|
|
12
|
+
const plain = (v) => v !== null && typeof v === 'object' &&
|
|
13
|
+
(Object.getPrototypeOf(v) === null || Object.getPrototypeOf(v) === Object.prototype);
|
|
14
|
+
/** Local inputs never come from the bundle. Snapshot policy before the first await. */
|
|
15
|
+
export function preparePolicy(options) {
|
|
16
|
+
const opts = { ...options };
|
|
17
|
+
const keys = ['now', 'expectedAgreementId', 'previousProposals', 'expectedParties', 'allowedPurposes', 'requireTimestamp', 'timestampVerifier',
|
|
18
|
+
'tsaRoots', 'signers', 'revocations', 'directOnly', 'requireContent', 'contents'];
|
|
19
|
+
check(plain(options) && Object.keys(options).every(k => keys.includes(k)), 'invalid-policy');
|
|
20
|
+
for (const v of [opts.requireTimestamp, opts.requireContent, opts.directOnly]) {
|
|
21
|
+
check(v === undefined || typeof v === 'boolean', 'invalid-policy');
|
|
22
|
+
}
|
|
23
|
+
const nowMs = opts.now instanceof Date ? opts.now.getTime() : opts.now === undefined ? Date.now() : opts.now;
|
|
24
|
+
check(Number.isSafeInteger(nowMs), 'invalid-policy');
|
|
25
|
+
let now;
|
|
26
|
+
try {
|
|
27
|
+
now = nowInstant(nowMs);
|
|
28
|
+
timestamp(formatTime(now));
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
throw new AgreementError('invalid-policy');
|
|
32
|
+
}
|
|
33
|
+
check(opts.expectedAgreementId === undefined || id(opts.expectedAgreementId), 'invalid-policy');
|
|
34
|
+
if (opts.expectedParties !== undefined) {
|
|
35
|
+
check(Array.isArray(opts.expectedParties) && opts.expectedParties.every(p => p && isEd25519DidKey(p.did) && label(p.role)), 'invalid-policy');
|
|
36
|
+
opts.expectedParties = opts.expectedParties.map(({ did, role }) => ({ did, role }));
|
|
37
|
+
check(new Set(opts.expectedParties.map(p => p.did)).size === opts.expectedParties.length, 'invalid-policy');
|
|
38
|
+
}
|
|
39
|
+
if (opts.allowedPurposes !== undefined) {
|
|
40
|
+
check(Array.isArray(opts.allowedPurposes) && opts.allowedPurposes.every(namespaced), 'invalid-policy');
|
|
41
|
+
opts.allowedPurposes = [...opts.allowedPurposes];
|
|
42
|
+
}
|
|
43
|
+
check(opts.timestampVerifier === undefined || opts.timestampVerifier === null || typeof opts.timestampVerifier === 'function', 'invalid-policy');
|
|
44
|
+
if (opts.timestampVerifier === undefined)
|
|
45
|
+
opts.timestampVerifier = verifyTimestamp;
|
|
46
|
+
check(opts.tsaRoots === undefined || Array.isArray(opts.tsaRoots) && opts.tsaRoots.every(jsonInput), 'invalid-policy');
|
|
47
|
+
const roots = (opts.tsaRoots ?? []).map(copy);
|
|
48
|
+
// Malformed locally configured anchors are an input error, not evidence
|
|
49
|
+
// against an otherwise correctly signed decision.
|
|
50
|
+
if (opts.timestampVerifier === verifyTimestamp) {
|
|
51
|
+
try {
|
|
52
|
+
parseRoots(roots);
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
throw new AgreementError('invalid-policy');
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
check(opts.signers === undefined || jsonInput(opts.signers), 'invalid-signers');
|
|
59
|
+
const signers = opts.signers === undefined ? undefined : parseAllowedSigners(opts.signers);
|
|
60
|
+
check(opts.revocations === undefined || Array.isArray(opts.revocations) && opts.revocations.every(jsonInput), 'invalid-policy');
|
|
61
|
+
const revocations = (opts.revocations ?? []).map(copy);
|
|
62
|
+
check(opts.contents === undefined || plain(opts.contents), 'invalid-policy');
|
|
63
|
+
const contents = new Map(Object.entries(opts.contents ?? {}));
|
|
64
|
+
const previous = new Map();
|
|
65
|
+
check(opts.previousProposals === undefined || plain(opts.previousProposals), 'invalid-policy');
|
|
66
|
+
for (const [key, input] of Object.entries(opts.previousProposals ?? {})) {
|
|
67
|
+
let doc;
|
|
68
|
+
try {
|
|
69
|
+
doc = parseProposal(input);
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
throw new AgreementError('invalid-policy');
|
|
73
|
+
}
|
|
74
|
+
check(contentId(doc) === key, 'invalid-policy');
|
|
75
|
+
previous.set(key, doc);
|
|
76
|
+
}
|
|
77
|
+
return { opts, now, nowMs, roots, signers, contents, revocations, previous };
|
|
78
|
+
}
|
|
79
|
+
export function checkedHistory(proposal, agreementId, previous) {
|
|
80
|
+
const seen = new Set([agreementId]);
|
|
81
|
+
let id = proposal.supersedes;
|
|
82
|
+
while (id) {
|
|
83
|
+
check(!seen.has(id), 'invalid-reference');
|
|
84
|
+
seen.add(id);
|
|
85
|
+
const parent = previous.get(id);
|
|
86
|
+
if (!parent)
|
|
87
|
+
return false;
|
|
88
|
+
id = parent.supersedes;
|
|
89
|
+
}
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
export async function localRevocations(policy) {
|
|
93
|
+
const docs = [];
|
|
94
|
+
try {
|
|
95
|
+
for (const input of policy.revocations)
|
|
96
|
+
docs.push(await parseRevocation(input));
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
throw new AgreementError('invalid-revocation', 'error');
|
|
100
|
+
}
|
|
101
|
+
return docs;
|
|
102
|
+
}
|
|
103
|
+
export function checkExpectations(proposal, agreementId, p) {
|
|
104
|
+
const { expectedAgreementId, expectedParties, allowedPurposes } = p.opts;
|
|
105
|
+
check(!expectedAgreementId || expectedAgreementId === agreementId, 'expect-mismatch');
|
|
106
|
+
if (expectedParties) {
|
|
107
|
+
const parties = new Map(proposal.parties.map(party => [party.did, party.role]));
|
|
108
|
+
check(parties.size === expectedParties.length && expectedParties.every(party => parties.get(party.did) === party.role), 'expect-mismatch');
|
|
109
|
+
}
|
|
110
|
+
check(!allowedPurposes || allowedPurposes.includes(proposal.purpose), 'purpose-not-allowed');
|
|
111
|
+
for (const key of p.contents.keys())
|
|
112
|
+
check(proposal.subjects.some(s => key === s.id), 'invalid-policy');
|
|
113
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Proposal, ProposalFields, ProposalInput } from './types.js';
|
|
2
|
+
export declare function parseProposal(input: ProposalInput): Proposal;
|
|
3
|
+
export declare function createProposal(fields: ProposalFields): Proposal;
|
|
4
|
+
export declare function agreementId(proposal: ProposalInput): string;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { hex, utf8 } from '../bytes.js';
|
|
2
|
+
import { AgreementError, check } from './errors.js';
|
|
3
|
+
import { array, base64, contentId, did, digest, digestHex, extensions, fields, header, id, label, mediaType, namespaced, object, timestamp } from './schema.js';
|
|
4
|
+
import { PROPOSAL_LIMIT, readDocument } from './strict.js';
|
|
5
|
+
import { PROPOSAL } from './types.js';
|
|
6
|
+
function validateSubjects(p) {
|
|
7
|
+
const ids = new Set();
|
|
8
|
+
array(p.subjects, 256);
|
|
9
|
+
for (const s of p.subjects) {
|
|
10
|
+
fields(s, 'id sha256 size mediaType', 'name inlineBase64');
|
|
11
|
+
check(label(s.id) && digestHex(s.sha256) && typeof s.size === 'number' && s.size >= 0 && mediaType(s.mediaType));
|
|
12
|
+
check(!ids.has(s.id), 'duplicate-subject');
|
|
13
|
+
ids.add(s.id);
|
|
14
|
+
if ('name' in s) {
|
|
15
|
+
check(typeof s.name === 'string' && !/[\p{Cc}/\\]/u.test(s.name));
|
|
16
|
+
check(utf8.encode(s.name).length <= 255, 'resource-limit');
|
|
17
|
+
}
|
|
18
|
+
if ('inlineBase64' in s) {
|
|
19
|
+
const bytes = base64(s.inlineBase64);
|
|
20
|
+
check(bytes.length === s.size && digest(bytes) === s.sha256, 'content-mismatch');
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return ids;
|
|
24
|
+
}
|
|
25
|
+
function validateParties(p) {
|
|
26
|
+
array(p.parties, 256);
|
|
27
|
+
check(p.parties.length >= 2);
|
|
28
|
+
const parties = new Set();
|
|
29
|
+
const roles = new Map();
|
|
30
|
+
for (const party of p.parties) {
|
|
31
|
+
fields(party, 'did role', 'displayName');
|
|
32
|
+
did(party.did);
|
|
33
|
+
check(!parties.has(party.did), 'duplicate-party');
|
|
34
|
+
parties.add(party.did);
|
|
35
|
+
check(label(party.role));
|
|
36
|
+
roles.set(party.role, (roles.get(party.role) ?? 0) + 1);
|
|
37
|
+
if ('displayName' in party) {
|
|
38
|
+
check(typeof party.displayName === 'string' && party.displayName.length > 0 && !/\p{Cc}/u.test(party.displayName));
|
|
39
|
+
check([...party.displayName].length <= 200, 'resource-limit');
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if ('proposer' in p) {
|
|
43
|
+
did(p.proposer);
|
|
44
|
+
check(parties.has(p.proposer), 'party-unlisted');
|
|
45
|
+
}
|
|
46
|
+
const f = p.formation;
|
|
47
|
+
object(f);
|
|
48
|
+
switch (f.mode) {
|
|
49
|
+
case 'all':
|
|
50
|
+
fields(f, 'mode');
|
|
51
|
+
break;
|
|
52
|
+
case 'threshold':
|
|
53
|
+
fields(f, 'mode minAccept');
|
|
54
|
+
check(typeof f.minAccept === 'number' && f.minAccept >= 1 && f.minAccept <= parties.size, 'invalid-formation');
|
|
55
|
+
break;
|
|
56
|
+
case 'roles':
|
|
57
|
+
fields(f, 'mode required');
|
|
58
|
+
object(f.required);
|
|
59
|
+
check(Object.keys(f.required).length > 0, 'invalid-formation');
|
|
60
|
+
for (const [role, n] of Object.entries(f.required)) {
|
|
61
|
+
check(typeof n === 'number' && n >= 1 && n <= (roles.get(role) ?? 0), 'invalid-formation');
|
|
62
|
+
}
|
|
63
|
+
break;
|
|
64
|
+
default: check(false, 'invalid-formation');
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
export function parseProposal(input) {
|
|
68
|
+
const p = readDocument(input, PROPOSAL_LIMIT);
|
|
69
|
+
header(p, PROPOSAL);
|
|
70
|
+
fields(p, 'type v nonce purpose createdAt subjects terms parties formation timePolicy', 'expiresAt proposer supersedes extensions criticalExtensions');
|
|
71
|
+
check(typeof p.nonce === 'string' && /^[0-9a-f]{32}$/.test(p.nonce) && namespaced(p.purpose));
|
|
72
|
+
const created = timestamp(p.createdAt);
|
|
73
|
+
if ('expiresAt' in p)
|
|
74
|
+
check(timestamp(p.expiresAt) > created, 'invalid-time');
|
|
75
|
+
check(p.timePolicy === 'self-declared-ok' || p.timePolicy === 'timestamp-required');
|
|
76
|
+
const ids = validateSubjects(p);
|
|
77
|
+
const t = p.terms;
|
|
78
|
+
object(t);
|
|
79
|
+
if (t.mode === 'inline') {
|
|
80
|
+
fields(t, 'mode mediaType text');
|
|
81
|
+
check((t.mediaType === 'text/plain' || t.mediaType === 'text/markdown') && typeof t.text === 'string' && t.text.length > 0);
|
|
82
|
+
check(utf8.encode(t.text).length <= 256 * 1024, 'resource-limit');
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
fields(t, 'mode subjectId');
|
|
86
|
+
check(t.mode === 'subject');
|
|
87
|
+
check(label(t.subjectId) && ids.has(t.subjectId), 'invalid-reference');
|
|
88
|
+
}
|
|
89
|
+
validateParties(p);
|
|
90
|
+
extensions(p);
|
|
91
|
+
if ('supersedes' in p)
|
|
92
|
+
check(id(p.supersedes) && p.supersedes !== contentId(p), 'invalid-reference');
|
|
93
|
+
return p;
|
|
94
|
+
}
|
|
95
|
+
export function createProposal(fields) {
|
|
96
|
+
const data = readDocument(fields, PROPOSAL_LIMIT);
|
|
97
|
+
check(!('nonce' in data || 'type' in data || 'v' in data));
|
|
98
|
+
let nonce;
|
|
99
|
+
try {
|
|
100
|
+
nonce = hex(globalThis.crypto.getRandomValues(new Uint8Array(16)));
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
throw new AgreementError('crypto-error');
|
|
104
|
+
}
|
|
105
|
+
return parseProposal({ ...data, type: PROPOSAL, v: 1, nonce });
|
|
106
|
+
}
|
|
107
|
+
export function agreementId(proposal) { return contentId(parseProposal(proposal)); }
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { JsonObject, JsonValue } from '../json.js';
|
|
2
|
+
export declare function digest(bytes: Uint8Array): string;
|
|
3
|
+
export declare const contentId: (obj: object) => string;
|
|
4
|
+
export declare const label: (v: unknown) => v is string;
|
|
5
|
+
export declare const digestHex: (v: unknown) => v is string;
|
|
6
|
+
export declare const id: (v: unknown) => v is string;
|
|
7
|
+
export declare const mediaType: (v: unknown) => boolean;
|
|
8
|
+
export declare function namespaced(v: unknown): boolean;
|
|
9
|
+
export declare function object(v: unknown): asserts v is JsonObject;
|
|
10
|
+
export declare function fields(v: unknown, required: string, optional?: string): asserts v is JsonObject;
|
|
11
|
+
export declare function header(obj: JsonObject, expected: string): void;
|
|
12
|
+
export declare function array(v: unknown, limit: number): asserts v is JsonValue[];
|
|
13
|
+
export declare function did(v: unknown): asserts v is string;
|
|
14
|
+
export declare function timestamp(v: unknown): bigint;
|
|
15
|
+
export declare function base64(v: unknown, maxBytes?: number): Uint8Array;
|
|
16
|
+
export declare function extensions(obj: JsonObject): void;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { hex, utf8 } from '../bytes.js';
|
|
2
|
+
import { isEd25519DidKey } from '../didKey.js';
|
|
3
|
+
import { canonicalJson } from '../json.js';
|
|
4
|
+
import { Sha256 } from '../sha256.js';
|
|
5
|
+
import { parseTime } from '../time.js';
|
|
6
|
+
import { AgreementError, check } from './errors.js';
|
|
7
|
+
import { BUNDLE, DECISION, PROPOSAL } from './types.js';
|
|
8
|
+
export function digest(bytes) {
|
|
9
|
+
const h = new Sha256();
|
|
10
|
+
h.update(bytes);
|
|
11
|
+
return hex(h.digest());
|
|
12
|
+
}
|
|
13
|
+
export const contentId = (obj) => 'sha256:' + digest(utf8.encode(canonicalJson(obj)));
|
|
14
|
+
export const label = (v) => typeof v === 'string' && /^[a-z][a-z0-9-]{0,63}$/.test(v);
|
|
15
|
+
export const digestHex = (v) => typeof v === 'string' && /^[0-9a-f]{64}$/.test(v);
|
|
16
|
+
export const id = (v) => typeof v === 'string' && /^sha256:[0-9a-f]{64}$/.test(v);
|
|
17
|
+
export const mediaType = (v) => typeof v === 'string' &&
|
|
18
|
+
/^[a-z0-9][a-z0-9!#$&^_.+-]{0,126}\/[a-z0-9][a-z0-9!#$&^_.+-]{0,126}$/.test(v);
|
|
19
|
+
const dns = (s) => /^(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/.test(s);
|
|
20
|
+
export function namespaced(v) {
|
|
21
|
+
if (typeof v !== 'string')
|
|
22
|
+
return false;
|
|
23
|
+
const parts = v.split(':');
|
|
24
|
+
return parts.length === 2 && parts[0].length <= 255 && (parts[0] === 'mistsign' || dns(parts[0])) && label(parts[1]);
|
|
25
|
+
}
|
|
26
|
+
export function object(v) {
|
|
27
|
+
check(v !== null && typeof v === 'object' && !Array.isArray(v));
|
|
28
|
+
}
|
|
29
|
+
export function fields(v, required, optional = '') {
|
|
30
|
+
object(v);
|
|
31
|
+
const keys = required.split(' ').filter(Boolean);
|
|
32
|
+
const allowed = new Set([...keys, ...optional.split(' ').filter(Boolean)]);
|
|
33
|
+
check(keys.every(k => Object.hasOwn(v, k)) && Object.keys(v).every(k => allowed.has(k)));
|
|
34
|
+
}
|
|
35
|
+
export function header(obj, expected) {
|
|
36
|
+
check(typeof obj.type === 'string');
|
|
37
|
+
if (obj.type !== expected) {
|
|
38
|
+
throw new AgreementError([PROPOSAL, DECISION, BUNDLE, 'tc-sign:signature', 'tc-sign:revocation'].includes(obj.type)
|
|
39
|
+
? 'invalid-format' : 'unsupported-type');
|
|
40
|
+
}
|
|
41
|
+
check(typeof obj.v === 'number');
|
|
42
|
+
check(obj.v === 1, 'unsupported-version');
|
|
43
|
+
}
|
|
44
|
+
export function array(v, limit) {
|
|
45
|
+
check(Array.isArray(v));
|
|
46
|
+
check(v.length <= limit, 'resource-limit');
|
|
47
|
+
}
|
|
48
|
+
export function did(v) { check(isEd25519DidKey(v), 'invalid-did'); }
|
|
49
|
+
export function timestamp(v) {
|
|
50
|
+
check(typeof v === 'string' && !v.startsWith('0000') &&
|
|
51
|
+
/^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{3}Z$/.test(v), 'invalid-time');
|
|
52
|
+
try {
|
|
53
|
+
return parseTime(v);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
throw new AgreementError('invalid-time');
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export function base64(v, maxBytes) {
|
|
60
|
+
check(typeof v === 'string');
|
|
61
|
+
if (maxBytes !== undefined)
|
|
62
|
+
check(v.length <= Math.ceil(maxBytes / 3) * 4, 'resource-limit');
|
|
63
|
+
let binary;
|
|
64
|
+
try {
|
|
65
|
+
binary = atob(v);
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
throw new AgreementError('invalid-format');
|
|
69
|
+
}
|
|
70
|
+
check(btoa(binary) === v);
|
|
71
|
+
if (maxBytes !== undefined)
|
|
72
|
+
check(binary.length <= maxBytes, 'resource-limit');
|
|
73
|
+
return Uint8Array.from(binary, c => c.charCodeAt(0));
|
|
74
|
+
}
|
|
75
|
+
export function extensions(obj) {
|
|
76
|
+
if ('extensions' in obj) {
|
|
77
|
+
object(obj.extensions);
|
|
78
|
+
for (const [key, ext] of Object.entries(obj.extensions)) {
|
|
79
|
+
const dot = key.lastIndexOf('.');
|
|
80
|
+
const prefix = key.slice(0, dot);
|
|
81
|
+
check(key.length <= 255 && dot > 0 && (prefix === 'mistsign' || dns(prefix)) && label(key.slice(dot + 1)));
|
|
82
|
+
fields(ext, 'v data');
|
|
83
|
+
check(typeof ext.v === 'number' && ext.v >= 1);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if ('criticalExtensions' in obj) {
|
|
87
|
+
const keys = obj.criticalExtensions;
|
|
88
|
+
const ext = obj.extensions;
|
|
89
|
+
check(Array.isArray(keys) && keys.every(k => typeof k === 'string' && ext && typeof ext === 'object' && Object.hasOwn(ext, k)));
|
|
90
|
+
check(keys.length === new Set(keys).size);
|
|
91
|
+
check(!keys.length, 'unsupported-critical-extension');
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { utf8 } from '../bytes.js';
|
|
2
|
+
import { assertScalarString, decodeText } from '../json.js';
|
|
3
|
+
import { MistsignError } from '../errors.js';
|
|
4
|
+
import { AgreementError, check } from './errors.js';
|
|
5
|
+
export const PROPOSAL_LIMIT = 1 << 20;
|
|
6
|
+
export const DECISION_LIMIT = 64 << 10;
|
|
7
|
+
export const BUNDLE_LIMIT = 16 << 20;
|
|
8
|
+
const MAX_DEPTH = 128;
|
|
9
|
+
/** Decode names before duplicate detection. Inspect number tokens before conversion to binary64. */
|
|
10
|
+
function parseRaw(source) {
|
|
11
|
+
let pos = 0;
|
|
12
|
+
const space = /[ \t\n\r]*/y;
|
|
13
|
+
const number = /-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?/y;
|
|
14
|
+
function whitespace() {
|
|
15
|
+
space.lastIndex = pos;
|
|
16
|
+
space.exec(source);
|
|
17
|
+
pos = space.lastIndex;
|
|
18
|
+
}
|
|
19
|
+
function string() {
|
|
20
|
+
check(source[pos] === '"', 'invalid-json');
|
|
21
|
+
const start = pos++;
|
|
22
|
+
while (pos < source.length) {
|
|
23
|
+
const c = source[pos++];
|
|
24
|
+
if (c === '\\')
|
|
25
|
+
pos++;
|
|
26
|
+
else if (c === '"') {
|
|
27
|
+
let value;
|
|
28
|
+
try {
|
|
29
|
+
value = JSON.parse(source.slice(start, pos));
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
throw new AgreementError('invalid-json');
|
|
33
|
+
}
|
|
34
|
+
assertScalarString(value);
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
throw new AgreementError('invalid-json');
|
|
39
|
+
}
|
|
40
|
+
function value(depth) {
|
|
41
|
+
whitespace();
|
|
42
|
+
const c = source[pos];
|
|
43
|
+
if (c === '"')
|
|
44
|
+
return string();
|
|
45
|
+
if (c === '[' || c === '{') {
|
|
46
|
+
check(depth <= MAX_DEPTH, 'resource-limit');
|
|
47
|
+
pos++;
|
|
48
|
+
const array = c === '[';
|
|
49
|
+
const end = array ? ']' : '}';
|
|
50
|
+
const list = [];
|
|
51
|
+
const obj = Object.create(null);
|
|
52
|
+
whitespace();
|
|
53
|
+
if (source[pos] !== end) {
|
|
54
|
+
for (;;) {
|
|
55
|
+
if (array)
|
|
56
|
+
list.push(value(depth + 1));
|
|
57
|
+
else {
|
|
58
|
+
const key = string();
|
|
59
|
+
check(!Object.hasOwn(obj, key), 'duplicate-key');
|
|
60
|
+
whitespace();
|
|
61
|
+
check(source[pos++] === ':', 'invalid-json');
|
|
62
|
+
obj[key] = value(depth + 1);
|
|
63
|
+
}
|
|
64
|
+
whitespace();
|
|
65
|
+
if (source[pos] !== ',')
|
|
66
|
+
break;
|
|
67
|
+
pos++;
|
|
68
|
+
whitespace();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
check(source[pos++] === end, 'invalid-json');
|
|
72
|
+
return array ? list : obj;
|
|
73
|
+
}
|
|
74
|
+
for (const literal of ['true', 'false', 'null']) {
|
|
75
|
+
if (source.startsWith(literal, pos)) {
|
|
76
|
+
pos += literal.length;
|
|
77
|
+
return literal === 'null' ? null : literal === 'true';
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
number.lastIndex = pos;
|
|
81
|
+
const token = number.exec(source)?.[0];
|
|
82
|
+
check(token, 'invalid-json');
|
|
83
|
+
pos += token.length;
|
|
84
|
+
const n = Number(token);
|
|
85
|
+
check(Number.isSafeInteger(n) && String(n) === token, 'invalid-number');
|
|
86
|
+
return n;
|
|
87
|
+
}
|
|
88
|
+
const result = value(1);
|
|
89
|
+
whitespace();
|
|
90
|
+
check(pos === source.length, 'invalid-json');
|
|
91
|
+
return result;
|
|
92
|
+
}
|
|
93
|
+
/** Snapshot without invoking toJSON/getters or allowing a DAG to expand past the byte budget. */
|
|
94
|
+
function snapshot(input, limit) {
|
|
95
|
+
let remaining = limit;
|
|
96
|
+
function charge(bytes) {
|
|
97
|
+
remaining -= bytes;
|
|
98
|
+
check(remaining >= 0, 'document-too-large');
|
|
99
|
+
}
|
|
100
|
+
function string(s) {
|
|
101
|
+
check(s.length <= remaining, 'document-too-large');
|
|
102
|
+
assertScalarString(s);
|
|
103
|
+
charge(utf8.encode(JSON.stringify(s)).length);
|
|
104
|
+
}
|
|
105
|
+
const ancestors = new Set();
|
|
106
|
+
function copy(v, depth) {
|
|
107
|
+
if (typeof v === 'string') {
|
|
108
|
+
string(v);
|
|
109
|
+
return v;
|
|
110
|
+
}
|
|
111
|
+
if (typeof v === 'number') {
|
|
112
|
+
check(Number.isSafeInteger(v) && !Object.is(v, -0), 'invalid-number');
|
|
113
|
+
charge(String(v).length);
|
|
114
|
+
return v;
|
|
115
|
+
}
|
|
116
|
+
if (v === null || typeof v === 'boolean') {
|
|
117
|
+
charge(String(v).length);
|
|
118
|
+
return v;
|
|
119
|
+
}
|
|
120
|
+
check(typeof v === 'object' && !ancestors.has(v), 'invalid-json');
|
|
121
|
+
check(depth <= MAX_DEPTH, 'resource-limit');
|
|
122
|
+
ancestors.add(v);
|
|
123
|
+
charge(2);
|
|
124
|
+
const list = Array.isArray(v);
|
|
125
|
+
const proto = Object.getPrototypeOf(v);
|
|
126
|
+
check(list ? proto === Array.prototype : proto === null || proto === Object.prototype, 'invalid-json');
|
|
127
|
+
const result = list ? [] : Object.create(null);
|
|
128
|
+
const keys = Reflect.ownKeys(v);
|
|
129
|
+
if (list)
|
|
130
|
+
check(keys.length === v.length + 1, 'invalid-json');
|
|
131
|
+
let count = 0;
|
|
132
|
+
for (const key of keys) {
|
|
133
|
+
if (list && key === 'length')
|
|
134
|
+
continue;
|
|
135
|
+
const prop = Object.getOwnPropertyDescriptor(v, key);
|
|
136
|
+
check(typeof key === 'string' && prop.enumerable && 'value' in prop, 'invalid-json');
|
|
137
|
+
if (count++)
|
|
138
|
+
charge(1);
|
|
139
|
+
if (Array.isArray(result)) {
|
|
140
|
+
check(key === String(result.length), 'invalid-json');
|
|
141
|
+
result.push(copy(prop.value, depth + 1));
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
string(key);
|
|
145
|
+
charge(1);
|
|
146
|
+
result[key] = copy(prop.value, depth + 1);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
ancestors.delete(v);
|
|
150
|
+
return result;
|
|
151
|
+
}
|
|
152
|
+
return copy(input, 1);
|
|
153
|
+
}
|
|
154
|
+
export function readDocument(input, limit) {
|
|
155
|
+
try {
|
|
156
|
+
const v = typeof input === 'string' || input instanceof Uint8Array
|
|
157
|
+
? parseRaw(decodeText(input, limit)) : snapshot(input, limit);
|
|
158
|
+
check(v !== null && typeof v === 'object' && !Array.isArray(v));
|
|
159
|
+
return v;
|
|
160
|
+
}
|
|
161
|
+
catch (err) {
|
|
162
|
+
if (err instanceof MistsignError && (err.code === 'invalid-utf8' || err.code === 'invalid-surrogate' ||
|
|
163
|
+
err.code === 'document-too-large'))
|
|
164
|
+
throw new AgreementError(err.code);
|
|
165
|
+
throw err;
|
|
166
|
+
}
|
|
167
|
+
}
|