@thehonestmachine/oath-core 0.1.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/LICENSE +202 -0
- package/README.md +9 -0
- package/dist/bytes.d.ts +7 -0
- package/dist/bytes.d.ts.map +1 -0
- package/dist/bytes.js +46 -0
- package/dist/bytes.js.map +1 -0
- package/dist/ed25519.d.ts +11 -0
- package/dist/ed25519.d.ts.map +1 -0
- package/dist/ed25519.js +37 -0
- package/dist/ed25519.js.map +1 -0
- package/dist/entry.d.ts +27 -0
- package/dist/entry.d.ts.map +1 -0
- package/dist/entry.js +183 -0
- package/dist/entry.js.map +1 -0
- package/dist/errors.d.ts +5 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +5 -0
- package/dist/errors.js.map +1 -0
- package/dist/hash.d.ts +3 -0
- package/dist/hash.d.ts.map +1 -0
- package/dist/hash.js +9 -0
- package/dist/hash.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/jcs.d.ts +19 -0
- package/dist/jcs.d.ts.map +1 -0
- package/dist/jcs.js +63 -0
- package/dist/jcs.js.map +1 -0
- package/dist/merkle.d.ts +35 -0
- package/dist/merkle.d.ts.map +1 -0
- package/dist/merkle.js +177 -0
- package/dist/merkle.js.map +1 -0
- package/dist/oath.d.ts +34 -0
- package/dist/oath.d.ts.map +1 -0
- package/dist/oath.js +128 -0
- package/dist/oath.js.map +1 -0
- package/dist/sth.d.ts +16 -0
- package/dist/sth.d.ts.map +1 -0
- package/dist/sth.js +95 -0
- package/dist/sth.js.map +1 -0
- package/dist/types.d.ts +90 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/validate.d.ts +17 -0
- package/dist/validate.d.ts.map +1 -0
- package/dist/validate.js +65 -0
- package/dist/validate.js.map +1 -0
- package/package.json +55 -0
- package/src/bytes.ts +46 -0
- package/src/ed25519.ts +40 -0
- package/src/entry.ts +208 -0
- package/src/errors.ts +4 -0
- package/src/hash.ts +10 -0
- package/src/index.ts +82 -0
- package/src/jcs.ts +60 -0
- package/src/merkle.ts +184 -0
- package/src/oath.ts +148 -0
- package/src/sth.ts +126 -0
- package/src/types.ts +108 -0
- package/src/validate.ts +81 -0
package/dist/sth.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sth.js","sourceRoot":"","sources":["../src/sth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAC1C,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EACL,UAAU,EACV,iBAAiB,EACjB,KAAK,EACL,gBAAgB,EAChB,oBAAoB,EACpB,iBAAiB,EACjB,WAAW,GACZ,MAAM,eAAe,CAAC;AAGvB,MAAM,cAAc,GAAG,UAAU,CAAC,UAAU,CAAC,CAAC;AAE9C,SAAS,gBAAgB,CAAC,KAAc,EAAE,gBAAyB;IACjE,MAAM,GAAG,GAAG,iBAAiB,CAAC,KAAK,EAAE,kBAAkB,CAAC,CAAC;IACzD,MAAM,QAAQ,GAAG,CAAC,QAAQ,EAAE,WAAW,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC;IACnE,UAAU,CACR,GAAG,EACH,kBAAkB,EAClB,gBAAgB,CAAC,CAAC,CAAC,CAAC,GAAG,QAAQ,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC,QAAQ,EACxD,gBAAgB,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,CACtC,CAAC;IACF,IAAI,CAAC,gB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AC,OAAO,CAAC,SAAS,EAAE,mBAAmB,CAAC,OAAO,CAAC,EAAE,eAAe,CAAC,CAAC;AACpF,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/** Structures defined by SPEC.md v0.1. Field names match the canonical JSON. */
|
|
2
|
+
export type Verdict = 'compliant' | 'violation' | 'uncertain';
|
|
3
|
+
export type LawMode = 'machine' | 'judgment';
|
|
4
|
+
export interface Law {
|
|
5
|
+
n: number;
|
|
6
|
+
mode: LawMode;
|
|
7
|
+
text: string;
|
|
8
|
+
}
|
|
9
|
+
export interface OathCommitments {
|
|
10
|
+
entries_per_30d?: number;
|
|
11
|
+
max_latency_seconds?: number;
|
|
12
|
+
}
|
|
13
|
+
export interface UnsignedOathDoc {
|
|
14
|
+
spec_version: string;
|
|
15
|
+
agent: string;
|
|
16
|
+
display_name?: string;
|
|
17
|
+
version: number;
|
|
18
|
+
pubkey: string;
|
|
19
|
+
scope: string;
|
|
20
|
+
commitments?: OathCommitments;
|
|
21
|
+
laws: Law[];
|
|
22
|
+
created_at: string;
|
|
23
|
+
}
|
|
24
|
+
export interface OathDoc extends UnsignedOathDoc {
|
|
25
|
+
signature: string;
|
|
26
|
+
}
|
|
27
|
+
export interface Action {
|
|
28
|
+
type: string;
|
|
29
|
+
summary: string;
|
|
30
|
+
content_hash: string | null;
|
|
31
|
+
occurred_at: string;
|
|
32
|
+
}
|
|
33
|
+
export interface MachineAdjudication {
|
|
34
|
+
mode: 'machine';
|
|
35
|
+
check: string;
|
|
36
|
+
check_version?: string;
|
|
37
|
+
}
|
|
38
|
+
export interface ModelAdjudication {
|
|
39
|
+
mode: 'model';
|
|
40
|
+
model: string;
|
|
41
|
+
prompt_hash: string;
|
|
42
|
+
inputs_hash: string;
|
|
43
|
+
}
|
|
44
|
+
export type Adjudication = MachineAdjudication | ModelAdjudication;
|
|
45
|
+
export interface Confession {
|
|
46
|
+
note: string;
|
|
47
|
+
}
|
|
48
|
+
export interface UnsignedEntry {
|
|
49
|
+
spec_version: string;
|
|
50
|
+
agent: string;
|
|
51
|
+
oath_version: number;
|
|
52
|
+
action: Action;
|
|
53
|
+
verdict: Verdict;
|
|
54
|
+
law_cited: number | null;
|
|
55
|
+
adjudication: Adjudication;
|
|
56
|
+
confession?: Confession;
|
|
57
|
+
timestamp: string;
|
|
58
|
+
}
|
|
59
|
+
export interface Entry extends UnsignedEntry {
|
|
60
|
+
signature: string;
|
|
61
|
+
}
|
|
62
|
+
export interface UnsignedTreeHead {
|
|
63
|
+
ledger: string;
|
|
64
|
+
tree_size: number;
|
|
65
|
+
root_hash: string;
|
|
66
|
+
timestamp: string;
|
|
67
|
+
}
|
|
68
|
+
export interface SignedTreeHead extends UnsignedTreeHead {
|
|
69
|
+
signature: string;
|
|
70
|
+
}
|
|
71
|
+
export interface UnsignedReceipt {
|
|
72
|
+
ledger: string;
|
|
73
|
+
leaf_hash: string;
|
|
74
|
+
received_at: string;
|
|
75
|
+
mmd_seconds: number;
|
|
76
|
+
}
|
|
77
|
+
export interface Receipt extends UnsignedReceipt {
|
|
78
|
+
signature: string;
|
|
79
|
+
}
|
|
80
|
+
export interface InclusionProof {
|
|
81
|
+
leaf_index: number;
|
|
82
|
+
tree_size: number;
|
|
83
|
+
audit_path: string[];
|
|
84
|
+
}
|
|
85
|
+
export interface ConsistencyProof {
|
|
86
|
+
old_size: number;
|
|
87
|
+
new_size: number;
|
|
88
|
+
path: string[];
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAEhF,MAAM,MAAM,OAAO,GAAG,WAAW,GAAG,WAAW,GAAG,WAAW,CAAC;AAE9D,MAAM,MAAM,OAAO,GAAG,SAAS,GAAG,UAAU,CAAC;AAE7C,MAAM,WAAW,GAAG;IAClB,CAAC,EAAE,M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CAAC;IACrB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,OAAO,CAAC;IACjB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,YAAY,EAAE,YAAY,CAAC;IAC3B,UAAU,CAAC,EAAE,UAAU,CAAC;IACxB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,KAAM,SAAQ,aAAa;IAC1C,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,gBAAgB;IAC/B,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,cAAe,SAAQ,gBAAgB;IACtD,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,eAAe;IAC9B,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,OAAQ,SAAQ,eAAe;IAC9C,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,cAAc;IAC7B,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,EAAE,CAAC;CACtB;AAED,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,EAAE,CAAC;CAChB"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,gFAAgF"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** Spec §1.2: lowercase alphanumeric + hyphens, 1–64 chars, no edge hyphens. */
|
|
2
|
+
export declare const HANDLE_RE: RegExp;
|
|
3
|
+
export declare function isHandle(value: unknown): value is string;
|
|
4
|
+
/**
|
|
5
|
+
* Spec §1.2: RFC 3339 UTC with Z suffix. Calendar validity is checked
|
|
6
|
+
* explicitly — Date.parse silently rolls over impossible dates like Feb 30.
|
|
7
|
+
*/
|
|
8
|
+
export declare function isTimestamp(value: unknown): value is string;
|
|
9
|
+
/** Lowercase hex of exactly `byteLength` bytes. */
|
|
10
|
+
export declare function isHex(value: unknown, byteLength: number): value is string;
|
|
11
|
+
export declare function isPositiveInteger(value: unknown): value is number;
|
|
12
|
+
export declare function isNonNegativeInteger(value: unknown): value is number;
|
|
13
|
+
export declare function isNonEmptyString(value: unknown): value is string;
|
|
14
|
+
export declare function assertPlainObject(value: unknown, label: string): Record<string, unknown>;
|
|
15
|
+
/** Spec §1.2: unknown fields MUST be rejected. */
|
|
16
|
+
export declare function assertKeys(obj: Record<string, unknown>, label: string, required: readonly string[], optional: readonly string[]): void;
|
|
17
|
+
//# sourceMappingURL=validate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate.d.ts","sourceRoot":"","sources":["../src/validate.ts"],"names":[],"mappings":"AAEA,gFAAgF;AAChF,eAAO,MAAM,SAAS,QAAyC,CAAC;AAEhE,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAExD;AAWD;;;GAGG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAS3D;AAED,mDAAmD;AACnD,wBAAgB,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,GAAG,KAAK,IAAI,MAAM,CAMzE;AAED,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAEjE;AAED,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAEpE;AAED,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAEhE;AAED,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CASxF;AAED,kDAAkD;AAClD,wBAAgB,UAAU,CACxB,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC5B,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,SAAS,MAAM,EAAE,EAC3B,QAAQ,EAAE,SAAS,MAAM,EAAE,GAC1B,IAAI,CASN"}
|
package/dist/validate.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { ProtocolError } from './errors.js';
|
|
2
|
+
/** Spec §1.2: lowercase alphanumeric + hyphens, 1–64 chars, no edge hyphens. */
|
|
3
|
+
export const HANDLE_RE = /^[a-z0-9]([a-z0-9-]{0,62}[a-z0-9])?$/;
|
|
4
|
+
export function isHandle(value) {
|
|
5
|
+
return typeof value === 'string' && HANDLE_RE.test(value);
|
|
6
|
+
}
|
|
7
|
+
const TIMESTAMP_RE = /^(\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])T([01]\d|2[0-3]):[0-5]\d:[0-5]\d(\.\d{1,9})?Z$/;
|
|
8
|
+
const DAYS_IN_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
|
9
|
+
function isLeapYear(year) {
|
|
10
|
+
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Spec §1.2: RFC 3339 UTC with Z suffix. Calendar validity is checked
|
|
14
|
+
* explicitly — Date.parse silently rolls over impossible dates like Feb 30.
|
|
15
|
+
*/
|
|
16
|
+
export function isTimestamp(value) {
|
|
17
|
+
if (typeof value !== 'string')
|
|
18
|
+
return false;
|
|
19
|
+
const m = TIMESTAMP_RE.exec(value);
|
|
20
|
+
if (m === null)
|
|
21
|
+
return false;
|
|
22
|
+
const year = Number(m[1]);
|
|
23
|
+
const month = Number(m[2]);
|
|
24
|
+
const day = Number(m[3]);
|
|
25
|
+
const maxDay = month === 2 && isLeapYear(year) ? 29 : DAYS_IN_MONTH[month - 1];
|
|
26
|
+
return day <= maxDay;
|
|
27
|
+
}
|
|
28
|
+
/** Lowercase hex of exactly `byteLength` bytes. */
|
|
29
|
+
export function isHex(value, byteLength) {
|
|
30
|
+
return (typeof value === 'string' &&
|
|
31
|
+
value.length === byteLength * 2 &&
|
|
32
|
+
/^[0-9a-f]*$/.test(value));
|
|
33
|
+
}
|
|
34
|
+
export function isPositiveInteger(value) {
|
|
35
|
+
return typeof value === 'number' && Number.isSafeInteger(value) && value >= 1;
|
|
36
|
+
}
|
|
37
|
+
export function isNonNegativeInteger(value) {
|
|
38
|
+
return typeof value === 'number' && Number.isSafeInteger(value) && value >= 0;
|
|
39
|
+
}
|
|
40
|
+
export function isNonEmptyString(value) {
|
|
41
|
+
return typeof value === 'string' && value.length > 0;
|
|
42
|
+
}
|
|
43
|
+
export function assertPlainObject(value, label) {
|
|
44
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
45
|
+
throw new ProtocolError(`${label} must be a JSON object`);
|
|
46
|
+
}
|
|
47
|
+
const proto = Object.getPrototypeOf(value);
|
|
48
|
+
if (proto !== Object.prototype && proto !== null) {
|
|
49
|
+
throw new ProtocolError(`${label} must be a plain JSON object`);
|
|
50
|
+
}
|
|
51
|
+
return value;
|
|
52
|
+
}
|
|
53
|
+
/** Spec §1.2: unknown fields MUST be rejected. */
|
|
54
|
+
export function assertKeys(obj, label, required, optional) {
|
|
55
|
+
for (const key of required) {
|
|
56
|
+
if (!(key in obj))
|
|
57
|
+
throw new ProtocolError(`${label} is missing required field "${key}"`);
|
|
58
|
+
}
|
|
59
|
+
for (const key of Object.keys(obj)) {
|
|
60
|
+
if (!required.includes(key) && !optional.includes(key)) {
|
|
61
|
+
throw new ProtocolError(`${label} has unknown field "${key}"`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=validate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate.js","sourceRoot":"","sources":["../src/validate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,gFAAgF;AAChF,MAAM,CAAC,MAAM,SAAS,GAAG,sCAAsC,CAAC;AAEhE,MAAM,UAAU,QAAQ,CAAC,KAAc;IACrC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAC5D,CAAC;AAED,MAAM,YAAY,GAChB,8FAA8F,CAAC;AAEjG,MAAM,aAAa,GAAG,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,CAAU,CAAC;AAEhF,SAAS,UAAU,CAAC,IAAY;IAC9B,OAAO,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,IAAI,IAAI,GAAG,GAAG,KAAK,CAAC,CAAC,IAAI,IAAI,GAAG,GAAG,KAAK,CAAC,CAAC;AAClE,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,WAAW,CAAC,KAAc;IACxC,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC;IAC5C,MAAM,CAAC,GAAG,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACnC,IAAI,CAAC,KAAK,IAAI;QAAE,OAAO,KAAK,CAAC;IAC7B,MAAM,IAAI,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAC1B,MAAM,KAAK,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;I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AC,KAAK,CAAC,CAAC;IAC3C,IAAI,KAAK,KAAK,MAAM,CAAC,SAAS,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QACjD,MAAM,IAAI,aAAa,CAAC,GAAG,KAAK,8BAA8B,CAAC,CAAC;IAClE,CAAC;IACD,OAAO,KAAgC,CAAC;AAC1C,CAAC;AAED,kDAAkD;AAClD,MAAM,UAAU,UAAU,CACxB,GAA4B,EAC5B,KAAa,EACb,QAA2B,EAC3B,QAA2B;IAE3B,KAAK,MAAM,GAAG,IAAI,QAAQ,EAAE,CAAC;QAC3B,IAAI,CAAC,CAAC,GAAG,IAAI,GAAG,CAAC;YAAE,MAAM,IAAI,aAAa,CAAC,GAAG,KAAK,+BAA+B,GAAG,GAAG,CAAC,CAAC;IAC5F,CAAC;IACD,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;QACnC,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;YACvD,MAAM,IAAI,aAAa,CAAC,GAAG,KAAK,uBAAuB,GAAG,GAAG,CAAC,CAAC;QACjE,CAAC;IACH,CAAC;AACH,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@thehonestmachine/oath-core",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "The OATH protocol, no I/O: canonicalization, hashing, the Merkle log, proofs, signatures, and validation of oaths, entries, tree heads, and receipts.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src",
|
|
18
|
+
"LICENSE"
|
|
19
|
+
],
|
|
20
|
+
"homepage": "https://oath.sh",
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@noble/ed25519": "^2.3.0",
|
|
23
|
+
"@noble/hashes": "^1.8.0"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
27
|
+
"typescript": "^5.8.3",
|
|
28
|
+
"vitest": "^3.2.4"
|
|
29
|
+
},
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "git+https://github.com/aamir222686/oath.git",
|
|
33
|
+
"directory": "packages/core"
|
|
34
|
+
},
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
},
|
|
38
|
+
"engines": {
|
|
39
|
+
"node": ">=20"
|
|
40
|
+
},
|
|
41
|
+
"keywords": [
|
|
42
|
+
"oath",
|
|
43
|
+
"ai-agents",
|
|
44
|
+
"accountability",
|
|
45
|
+
"transparency",
|
|
46
|
+
"merkle",
|
|
47
|
+
"certificate-transparency"
|
|
48
|
+
],
|
|
49
|
+
"scripts": {
|
|
50
|
+
"build": "tsc -p tsconfig.json",
|
|
51
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
52
|
+
"test": "vitest run",
|
|
53
|
+
"coverage": "vitest run --coverage"
|
|
54
|
+
}
|
|
55
|
+
}
|
package/src/bytes.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { utf8ToBytes } from '@noble/hashes/utils';
|
|
2
|
+
import { ProtocolError } from './errors.js';
|
|
3
|
+
|
|
4
|
+
export function utf8Bytes(s: string): Uint8Array {
|
|
5
|
+
return utf8ToBytes(s);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const HEX_CHARS = '0123456789abcdef';
|
|
9
|
+
|
|
10
|
+
export function bytesToHex(bytes: Uint8Array): string {
|
|
11
|
+
let out = '';
|
|
12
|
+
for (const b of bytes) {
|
|
13
|
+
out += HEX_CHARS[b >> 4]! + HEX_CHARS[b & 0x0f]!;
|
|
14
|
+
}
|
|
15
|
+
return out;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Strict per spec §1.2: lowercase hex only, even length. */
|
|
19
|
+
export function hexToBytes(hex: string): Uint8Array {
|
|
20
|
+
if (hex.length % 2 !== 0) throw new ProtocolError('hex string has odd length');
|
|
21
|
+
if (!/^[0-9a-f]*$/.test(hex)) throw new ProtocolError('hex string must be lowercase 0-9a-f');
|
|
22
|
+
const out = new Uint8Array(hex.length / 2);
|
|
23
|
+
for (let i = 0; i < out.length; i++) {
|
|
24
|
+
out[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
|
25
|
+
}
|
|
26
|
+
return out;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function bytesEqual(a: Uint8Array, b: Uint8Array): boolean {
|
|
30
|
+
if (a.length !== b.length) return false;
|
|
31
|
+
let diff = 0;
|
|
32
|
+
for (let i = 0; i < a.length; i++) diff |= a[i]! ^ b[i]!;
|
|
33
|
+
return diff === 0;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function concatBytes(...arrays: Uint8Array[]): Uint8Array {
|
|
37
|
+
let total = 0;
|
|
38
|
+
for (const a of arrays) total += a.length;
|
|
39
|
+
const out = new Uint8Array(total);
|
|
40
|
+
let offset = 0;
|
|
41
|
+
for (const a of arrays) {
|
|
42
|
+
out.set(a, offset);
|
|
43
|
+
offset += a.length;
|
|
44
|
+
}
|
|
45
|
+
return out;
|
|
46
|
+
}
|
package/src/ed25519.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import * as ed from '@noble/ed25519';
|
|
2
|
+
import { sha512 } from '@noble/hashes/sha512';
|
|
3
|
+
import { ProtocolError } from './errors.js';
|
|
4
|
+
import { bytesToHex, hexToBytes } from './bytes.js';
|
|
5
|
+
|
|
6
|
+
// @noble/ed25519 v2 needs a synchronous SHA-512 wired in for the sync API.
|
|
7
|
+
ed.etc.sha512Sync = (...messages: Uint8Array[]) => sha512(ed.etc.concatBytes(...messages));
|
|
8
|
+
|
|
9
|
+
function keyBytes(hex: string, byteLength: number, label: string): Uint8Array {
|
|
10
|
+
const bytes = hexToBytes(hex);
|
|
11
|
+
if (bytes.length !== byteLength) {
|
|
12
|
+
throw new ProtocolError(`${label} must be ${byteLength} bytes of lowercase hex`);
|
|
13
|
+
}
|
|
14
|
+
return bytes;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Derive the public key (hex) from a 32-byte private seed (hex). */
|
|
18
|
+
export function edPublicKey(seedHex: string): string {
|
|
19
|
+
return bytesToHex(ed.getPublicKey(keyBytes(seedHex, 32, 'private key seed')));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Sign a message with a 32-byte private seed; returns the 64-byte signature as hex. */
|
|
23
|
+
export function edSign(message: Uint8Array, seedHex: string): string {
|
|
24
|
+
return bytesToHex(ed.sign(message, keyBytes(seedHex, 32, 'private key seed')));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Verify an Ed25519 signature. Returns false (never throws) for signatures
|
|
29
|
+
* that fail, including structurally invalid curve points — every verifier
|
|
30
|
+
* must reach the same answer on the same bytes.
|
|
31
|
+
*/
|
|
32
|
+
export function edVerify(signatureHex: string, message: Uint8Array, pubkeyHex: string): boolean {
|
|
33
|
+
const signature = keyBytes(signatureHex, 64, 'signature');
|
|
34
|
+
const pubkey = keyBytes(pubkeyHex, 32, 'public key');
|
|
35
|
+
try {
|
|
36
|
+
return ed.verify(signature, message, pubkey);
|
|
37
|
+
} catch {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/entry.ts
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { ProtocolError } from './errors.js';
|
|
2
|
+
import { canonicalBytes } from './jcs.js';
|
|
3
|
+
import { leafHash } from './merkle.js';
|
|
4
|
+
import { bytesToHex } from './bytes.js';
|
|
5
|
+
import { edSign, edVerify } from './ed25519.js';
|
|
6
|
+
import { DEFAULT_MAX_LATENCY_SECONDS, SPEC_VERSION } from './oath.js';
|
|
7
|
+
import {
|
|
8
|
+
assertKeys,
|
|
9
|
+
assertPlainObject,
|
|
10
|
+
isHandle,
|
|
11
|
+
isHex,
|
|
12
|
+
isNonEmptyString,
|
|
13
|
+
isPositiveInteger,
|
|
14
|
+
isTimestamp,
|
|
15
|
+
} from './validate.js';
|
|
16
|
+
import type { Entry, OathDoc, UnsignedEntry } from './types.js';
|
|
17
|
+
|
|
18
|
+
const REQUIRED = [
|
|
19
|
+
'spec_version',
|
|
20
|
+
'agent',
|
|
21
|
+
'oath_version',
|
|
22
|
+
'action',
|
|
23
|
+
'verdict',
|
|
24
|
+
'law_cited',
|
|
25
|
+
'adjudication',
|
|
26
|
+
'timestamp',
|
|
27
|
+
];
|
|
28
|
+
const OPTIONAL = ['confession'];
|
|
29
|
+
|
|
30
|
+
/** Dotted lowercase identifier (SPEC.md §3.1), e.g. "cycle.publish". */
|
|
31
|
+
const ACTION_TYPE_RE = /^[a-z0-9-]+(\.[a-z0-9-]+)*$/;
|
|
32
|
+
|
|
33
|
+
/** Action types reserved by the spec (§3.4). */
|
|
34
|
+
export const RESERVED_ACTION_TYPES = ['oath.swear', 'oath.amend'] as const;
|
|
35
|
+
|
|
36
|
+
function validateAction(value: unknown): void {
|
|
37
|
+
const action = assertPlainObject(value, 'entry action');
|
|
38
|
+
assertKeys(action, 'entry action', ['type', 'summary', 'content_hash', 'occurred_at'], []);
|
|
39
|
+
const type = action['type'];
|
|
40
|
+
if (!isNonEmptyString(type) || !ACTION_TYPE_RE.test(type)) {
|
|
41
|
+
throw new ProtocolError('action type must be a dotted lowercase identifier');
|
|
42
|
+
}
|
|
43
|
+
if (!isNonEmptyString(action['summary'])) {
|
|
44
|
+
throw new ProtocolError('action summary must be a non-empty string');
|
|
45
|
+
}
|
|
46
|
+
if (action['content_hash'] !== null && !isHex(action['content_hash'], 32)) {
|
|
47
|
+
throw new ProtocolError('action content_hash must be 32 bytes of lowercase hex or null');
|
|
48
|
+
}
|
|
49
|
+
if (!isTimestamp(action['occurred_at'])) {
|
|
50
|
+
throw new ProtocolError('action occurred_at must be an RFC 3339 UTC timestamp');
|
|
51
|
+
}
|
|
52
|
+
if (type.startsWith('oath.')) {
|
|
53
|
+
if (!(RESERVED_ACTION_TYPES as readonly string[]).includes(type)) {
|
|
54
|
+
throw new ProtocolError('action types beginning "oath." are reserved by the spec');
|
|
55
|
+
}
|
|
56
|
+
if (action['content_hash'] === null) {
|
|
57
|
+
throw new ProtocolError(`${type} entries must carry the oath hash in content_hash`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function validateAdjudication(value: unknown): void {
|
|
63
|
+
const adjudication = assertPlainObject(value, 'entry adjudication');
|
|
64
|
+
const mode = adjudication['mode'];
|
|
65
|
+
if (mode === 'machine') {
|
|
66
|
+
assertKeys(adjudication, 'machine adjudication', ['mode', 'check'], ['check_version']);
|
|
67
|
+
if (!isNonEmptyString(adjudication['check'])) {
|
|
68
|
+
throw new ProtocolError('machine adjudication check must be a non-empty string');
|
|
69
|
+
}
|
|
70
|
+
if ('check_version' in adjudication && !isNonEmptyString(adjudication['check_version'])) {
|
|
71
|
+
throw new ProtocolError('machine adjudication check_version must be a non-empty string');
|
|
72
|
+
}
|
|
73
|
+
} else if (mode === 'model') {
|
|
74
|
+
assertKeys(adjudication, 'model adjudication', ['mode', 'model', 'prompt_hash', 'inputs_hash'], []);
|
|
75
|
+
if (!isNonEmptyString(adjudication['model'])) {
|
|
76
|
+
throw new ProtocolError('model adjudication model must be a non-empty string');
|
|
77
|
+
}
|
|
78
|
+
if (!isHex(adjudication['prompt_hash'], 32)) {
|
|
79
|
+
throw new ProtocolError('model adjudication prompt_hash must be 32 bytes of lowercase hex');
|
|
80
|
+
}
|
|
81
|
+
if (!isHex(adjudication['inputs_hash'], 32)) {
|
|
82
|
+
throw new ProtocolError('model adjudication inputs_hash must be 32 bytes of lowercase hex');
|
|
83
|
+
}
|
|
84
|
+
} else {
|
|
85
|
+
throw new ProtocolError('adjudication mode must be "machine" or "model"');
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function validateEntryShape(value: unknown, requireSignature: boolean): void {
|
|
90
|
+
const entry = assertPlainObject(value, 'entry');
|
|
91
|
+
const required = requireSignature ? [...REQUIRED, 'signature'] : REQUIRED;
|
|
92
|
+
const optional = requireSignature ? OPTIONAL : [...OPTIONAL, 'signature'];
|
|
93
|
+
assertKeys(entry, 'entry', required, optional);
|
|
94
|
+
|
|
95
|
+
if (entry['spec_version'] !== SPEC_VERSION) {
|
|
96
|
+
throw new ProtocolError(`entry spec_version must be "${SPEC_VERSION}"`);
|
|
97
|
+
}
|
|
98
|
+
if (!isHandle(entry['agent'])) throw new ProtocolError('entry agent must be a valid handle');
|
|
99
|
+
if (!isPositiveInteger(entry['oath_version'])) {
|
|
100
|
+
throw new ProtocolError('entry oath_version must be an integer >= 1');
|
|
101
|
+
}
|
|
102
|
+
validateAction(entry['action']);
|
|
103
|
+
|
|
104
|
+
const verdict = entry['verdict'];
|
|
105
|
+
if (verdict !== 'compliant' && verdict !== 'violation' && verdict !== 'uncertain') {
|
|
106
|
+
throw new ProtocolError('verdict must be "compliant", "violation", or "uncertain"');
|
|
107
|
+
}
|
|
108
|
+
const lawCited = entry['law_cited'];
|
|
109
|
+
if (lawCited !== null && !isPositiveInteger(lawCited)) {
|
|
110
|
+
throw new ProtocolError('law_cited must be a law number or null');
|
|
111
|
+
}
|
|
112
|
+
if (verdict === 'violation' && lawCited === null) {
|
|
113
|
+
throw new ProtocolError('a violation must cite the violated law (SPEC.md §3.3)');
|
|
114
|
+
}
|
|
115
|
+
if ('confession' in entry) {
|
|
116
|
+
const confession = assertPlainObject(entry['confession'], 'confession');
|
|
117
|
+
assertKeys(confession, 'confession', ['note'], []);
|
|
118
|
+
if (!isNonEmptyString(confession['note'])) {
|
|
119
|
+
throw new ProtocolError('confession note must be a non-empty string');
|
|
120
|
+
}
|
|
121
|
+
} else if (verdict !== 'compliant') {
|
|
122
|
+
throw new ProtocolError(`a ${verdict} verdict requires a confession (SPEC.md §3.3)`);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
validateAdjudication(entry['adjudication']);
|
|
126
|
+
if (!isTimestamp(entry['timestamp'])) {
|
|
127
|
+
throw new ProtocolError('entry timestamp must be an RFC 3339 UTC timestamp');
|
|
128
|
+
}
|
|
129
|
+
if (requireSignature && !isHex(entry['signature'], 64)) {
|
|
130
|
+
throw new ProtocolError('entry signature must be 64 bytes of lowercase hex');
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Validate a complete signed entry (SPEC.md §3). Structural only. */
|
|
135
|
+
export function assertValidEntry(value: unknown): asserts value is Entry {
|
|
136
|
+
validateEntryShape(value, true);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Validate an entry that has not been signed yet. */
|
|
140
|
+
export function assertValidUnsignedEntry(value: unknown): asserts value is UnsignedEntry {
|
|
141
|
+
validateEntryShape(value, false);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Canonical bytes of the entry without its signature — the signing input. */
|
|
145
|
+
export function entrySigningInput(entry: UnsignedEntry | Entry): Uint8Array {
|
|
146
|
+
const { signature: _signature, ...unsigned } = entry as Entry;
|
|
147
|
+
assertValidUnsignedEntry(unsigned);
|
|
148
|
+
return canonicalBytes(unsigned);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** The leaf bytes (SPEC.md §3.2): canonical bytes of the complete signed entry. */
|
|
152
|
+
export function leafBytesOf(entry: Entry): Uint8Array {
|
|
153
|
+
assertValidEntry(entry);
|
|
154
|
+
return canonicalBytes(entry);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** The entry's Merkle leaf hash as hex: SHA-256(0x00 || leaf bytes). */
|
|
158
|
+
export function entryLeafHash(entry: Entry): string {
|
|
159
|
+
return bytesToHex(leafHash(leafBytesOf(entry)));
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export function signEntry(unsigned: UnsignedEntry, seedHex: string): Entry {
|
|
163
|
+
assertValidUnsignedEntry(unsigned);
|
|
164
|
+
const signature = edSign(canonicalBytes(unsigned), seedHex);
|
|
165
|
+
return { ...unsigned, signature };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function verifyEntrySignature(entry: Entry, pubkeyHex: string): boolean {
|
|
169
|
+
assertValidEntry(entry);
|
|
170
|
+
return edVerify(entry.signature, entrySigningInput(entry), pubkeyHex);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Cross-check an entry against the oath version it cites (SPEC.md §3, §7):
|
|
175
|
+
* agent and version match, any cited law exists, and the signature verifies
|
|
176
|
+
* against the oath's key.
|
|
177
|
+
*/
|
|
178
|
+
export function assertEntryUnderOath(entry: Entry, oath: OathDoc): void {
|
|
179
|
+
assertValidEntry(entry);
|
|
180
|
+
if (entry.agent !== oath.agent) {
|
|
181
|
+
throw new ProtocolError('entry agent does not match the oath');
|
|
182
|
+
}
|
|
183
|
+
if (entry.oath_version !== oath.version) {
|
|
184
|
+
throw new ProtocolError('entry oath_version does not match the oath');
|
|
185
|
+
}
|
|
186
|
+
if (entry.law_cited !== null && entry.law_cited > oath.laws.length) {
|
|
187
|
+
throw new ProtocolError(`entry cites law ${entry.law_cited}, but the oath has ${oath.laws.length} laws`);
|
|
188
|
+
}
|
|
189
|
+
if (!verifyEntrySignature(entry, oath.pubkey)) {
|
|
190
|
+
throw new ProtocolError("entry signature does not verify against the oath's key");
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Promptness (SPEC.md §8.2): the entry was authored within the oath's
|
|
196
|
+
* max_latency_seconds (default 300) of the action occurring.
|
|
197
|
+
*/
|
|
198
|
+
export function isPrompt(
|
|
199
|
+
entry: Entry,
|
|
200
|
+
maxLatencySeconds: number = DEFAULT_MAX_LATENCY_SECONDS,
|
|
201
|
+
): boolean {
|
|
202
|
+
assertValidEntry(entry);
|
|
203
|
+
if (!isPositiveInteger(maxLatencySeconds)) {
|
|
204
|
+
throw new ProtocolError('maxLatencySeconds must be an integer >= 1');
|
|
205
|
+
}
|
|
206
|
+
const delta = Date.parse(entry.timestamp) - Date.parse(entry.action.occurred_at);
|
|
207
|
+
return delta <= maxLatencySeconds * 1000;
|
|
208
|
+
}
|
package/src/errors.ts
ADDED
package/src/hash.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { sha256 as nobleSha256 } from '@noble/hashes/sha256';
|
|
2
|
+
import { bytesToHex } from './bytes.js';
|
|
3
|
+
|
|
4
|
+
export function sha256(bytes: Uint8Array): Uint8Array {
|
|
5
|
+
return nobleSha256(bytes);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function sha256Hex(bytes: Uint8Array): string {
|
|
9
|
+
return bytesToHex(nobleSha256(bytes));
|
|
10
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
export { ProtocolError } from './errors.js';
|
|
2
|
+
export { bytesToHex, hexToBytes, bytesEqual, concatBytes, utf8Bytes } from './bytes.js';
|
|
3
|
+
export { sha256, sha256Hex } from './hash.js';
|
|
4
|
+
export { canonicalize, canonicalBytes } from './jcs.js';
|
|
5
|
+
export {
|
|
6
|
+
HANDLE_RE,
|
|
7
|
+
isHandle,
|
|
8
|
+
isTimestamp,
|
|
9
|
+
isHex,
|
|
10
|
+
isPositiveInteger,
|
|
11
|
+
isNonNegativeInteger,
|
|
12
|
+
isNonEmptyString,
|
|
13
|
+
assertPlainObject,
|
|
14
|
+
assertKeys,
|
|
15
|
+
} from './validate.js';
|
|
16
|
+
export { edPublicKey, edSign, edVerify } from './ed25519.js';
|
|
17
|
+
export {
|
|
18
|
+
EMPTY_ROOT,
|
|
19
|
+
leafHash,
|
|
20
|
+
nodeHash,
|
|
21
|
+
rootFromLeafHashes,
|
|
22
|
+
inclusionPath,
|
|
23
|
+
consistencyPath,
|
|
24
|
+
verifyInclusion,
|
|
25
|
+
verifyConsistency,
|
|
26
|
+
} from './merkle.js';
|
|
27
|
+
export {
|
|
28
|
+
SPEC_VERSION,
|
|
29
|
+
DEFAULT_MAX_LATENCY_SECONDS,
|
|
30
|
+
assertValidOathDoc,
|
|
31
|
+
assertValidUnsignedOathDoc,
|
|
32
|
+
oathSigningInput,
|
|
33
|
+
oathHash,
|
|
34
|
+
signOathDoc,
|
|
35
|
+
verifyOathSignature,
|
|
36
|
+
assertValidAmendment,
|
|
37
|
+
} from './oath.js';
|
|
38
|
+
export {
|
|
39
|
+
RESERVED_ACTION_TYPES,
|
|
40
|
+
assertValidEntry,
|
|
41
|
+
assertValidUnsignedEntry,
|
|
42
|
+
entrySigningInput,
|
|
43
|
+
leafBytesOf,
|
|
44
|
+
entryLeafHash,
|
|
45
|
+
signEntry,
|
|
46
|
+
verifyEntrySignature,
|
|
47
|
+
assertEntryUnderOath,
|
|
48
|
+
isPrompt,
|
|
49
|
+
} from './entry.js';
|
|
50
|
+
export {
|
|
51
|
+
assertValidSth,
|
|
52
|
+
assertValidUnsignedSth,
|
|
53
|
+
sthSigningInput,
|
|
54
|
+
signSth,
|
|
55
|
+
verifySthSignature,
|
|
56
|
+
assertValidReceipt,
|
|
57
|
+
assertValidUnsignedReceipt,
|
|
58
|
+
receiptSigningInput,
|
|
59
|
+
signReceipt,
|
|
60
|
+
verifyReceiptSignature,
|
|
61
|
+
} from './sth.js';
|
|
62
|
+
export type {
|
|
63
|
+
Verdict,
|
|
64
|
+
LawMode,
|
|
65
|
+
Law,
|
|
66
|
+
OathCommitments,
|
|
67
|
+
UnsignedOathDoc,
|
|
68
|
+
OathDoc,
|
|
69
|
+
Action,
|
|
70
|
+
MachineAdjudication,
|
|
71
|
+
ModelAdjudication,
|
|
72
|
+
Adjudication,
|
|
73
|
+
Confession,
|
|
74
|
+
UnsignedEntry,
|
|
75
|
+
Entry,
|
|
76
|
+
UnsignedTreeHead,
|
|
77
|
+
SignedTreeHead,
|
|
78
|
+
UnsignedReceipt,
|
|
79
|
+
Receipt,
|
|
80
|
+
InclusionProof,
|
|
81
|
+
ConsistencyProof,
|
|
82
|
+
} from './types.js';
|