@thehonestmachine/oath-sdk 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 +7 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/localLedger.d.ts +73 -0
- package/dist/localLedger.d.ts.map +1 -0
- package/dist/localLedger.js +195 -0
- package/dist/localLedger.js.map +1 -0
- package/dist/remote.d.ts +38 -0
- package/dist/remote.d.ts.map +1 -0
- package/dist/remote.js +78 -0
- package/dist/remote.js.map +1 -0
- package/dist/verify.d.ts +43 -0
- package/dist/verify.d.ts.map +1 -0
- package/dist/verify.js +278 -0
- package/dist/verify.js.map +1 -0
- package/package.json +55 -0
- package/src/index.ts +17 -0
- package/src/localLedger.ts +255 -0
- package/src/remote.ts +112 -0
- package/src/verify.ts +331 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# @thehonestmachine/oath-sdk
|
|
2
|
+
|
|
3
|
+
The OATH developer surface: swear an oath, append signed entries, amend
|
|
4
|
+
forward (including key rotation), and keep a verifiable local ledger on
|
|
5
|
+
disk ([spec](https://oath.sh/spec), Appendix A). Includes the trust-nothing
|
|
6
|
+
monitors (`verifyLocalLedger`, `verifyRemoteLedger`) and `RemoteLedger`,
|
|
7
|
+
the submission client that signature-checks every receipt. Apache-2.0.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { LocalLedger, LEDGER_FILES, readOathVersions, readLeafLines, type EntryFields, type AppendResult, } from './localLedger.js';
|
|
2
|
+
export { RemoteLedger, type LedgerDiscovery, type SubmitResult } from './remote.js';
|
|
3
|
+
export { verifyLedgerData, verifyLocalLedger, verifyRemoteLedger, type LedgerData, type VerificationReport, type CheckResult, } from './verify.js';
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,YAAY,EACZ,gBAAgB,EAChB,aAAa,EACb,KAAK,WAAW,EAChB,KAAK,YAAY,GAClB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,YAAY,EAAE,KAAK,eAAe,EAAE,KAAK,YAAY,EAAE,MAAM,aAAa,CAAC;AACpF,OAAO,EACL,gBAAgB,EAChB,iBAAiB,EACjB,kBAAkB,EAClB,KAAK,UAAU,EACf,KAAK,kBAAkB,EACvB,KAAK,WAAW,GACjB,MAAM,aAAa,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,YAAY,EACZ,gBAAgB,EAChB,aAAa,GAGd,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,YAAY,EAA2C,MAAM,aAAa,CAAC;AACpF,OAAO,EACL,gBAAgB,EAChB,iBAAiB,EACjB,kBAAkB,GAInB,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { type Action, type Adjudication, type Confession, type Entry, type OathDoc, type SignedTreeHead, type UnsignedOathDoc, type Verdict } from '@thehonestmachine/oath-core';
|
|
2
|
+
/** File layout per SPEC.md Appendix A. */
|
|
3
|
+
export declare const LEDGER_FILES: {
|
|
4
|
+
readonly oath: "oath.json";
|
|
5
|
+
readonly history: "oath.history";
|
|
6
|
+
readonly entries: "entries.jsonl";
|
|
7
|
+
readonly heads: "heads.json";
|
|
8
|
+
};
|
|
9
|
+
export interface EntryFields {
|
|
10
|
+
action: Action;
|
|
11
|
+
verdict: Verdict;
|
|
12
|
+
law_cited: number | null;
|
|
13
|
+
adjudication: Adjudication;
|
|
14
|
+
confession?: Confession;
|
|
15
|
+
}
|
|
16
|
+
export interface AppendResult {
|
|
17
|
+
entry: Entry;
|
|
18
|
+
head: SignedTreeHead;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A local ledger (SPEC.md §4 storage note, Appendix A): the agent-operated,
|
|
22
|
+
* on-disk, offline-verifiable form of the OATH log. The agent's key doubles
|
|
23
|
+
* as the ledger key (§5).
|
|
24
|
+
*/
|
|
25
|
+
export declare class LocalLedger {
|
|
26
|
+
readonly dir: string;
|
|
27
|
+
private seed;
|
|
28
|
+
private readonly oathVersions;
|
|
29
|
+
private readonly leafHashes;
|
|
30
|
+
private readonly heads;
|
|
31
|
+
private readonly ledgerName;
|
|
32
|
+
private readonly now;
|
|
33
|
+
private constructor();
|
|
34
|
+
/**
|
|
35
|
+
* Take the oath: sign version 1, create the ledger directory, and log the
|
|
36
|
+
* `oath.swear` entry as leaf 0 (SPEC.md §2.4).
|
|
37
|
+
*/
|
|
38
|
+
static swear(options: {
|
|
39
|
+
dir: string;
|
|
40
|
+
oath: UnsignedOathDoc;
|
|
41
|
+
seed: string;
|
|
42
|
+
ledgerName?: string;
|
|
43
|
+
now?: () => string;
|
|
44
|
+
}): LocalLedger;
|
|
45
|
+
/** Open an existing ledger for appending. Verification is the monitor's job. */
|
|
46
|
+
static open(options: {
|
|
47
|
+
dir: string;
|
|
48
|
+
seed: string;
|
|
49
|
+
now?: () => string;
|
|
50
|
+
}): LocalLedger;
|
|
51
|
+
get currentOath(): OathDoc;
|
|
52
|
+
get treeSize(): number;
|
|
53
|
+
latestHead(): SignedTreeHead | null;
|
|
54
|
+
/**
|
|
55
|
+
* Sign and append an entry, then emit a fresh STH — the local ledger emits
|
|
56
|
+
* one per append so its contents are always presentable (SPEC.md §5).
|
|
57
|
+
*/
|
|
58
|
+
append(fields: EntryFields): AppendResult;
|
|
59
|
+
/**
|
|
60
|
+
* Amend the oath (SPEC.md §9): sign the next version with the current key,
|
|
61
|
+
* write it, and log the `oath.amend` entry. For key rotation pass the new
|
|
62
|
+
* key's seed; future entries and tree heads are signed with it.
|
|
63
|
+
*/
|
|
64
|
+
amend(next: UnsignedOathDoc, options: {
|
|
65
|
+
summary: string;
|
|
66
|
+
newSeed?: string;
|
|
67
|
+
}): AppendResult;
|
|
68
|
+
}
|
|
69
|
+
/** Read and structurally validate the oath version chain from disk. */
|
|
70
|
+
export declare function readOathVersions(dir: string): OathDoc[];
|
|
71
|
+
/** Read raw entry lines (the exact leaf bytes, newline-separated). */
|
|
72
|
+
export declare function readLeafLines(dir: string): string[];
|
|
73
|
+
//# sourceMappingURL=localLedger.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"localLedger.d.ts","sourceRoot":"","sources":["../src/localLedger.ts"],"names":[],"mappings":"AAEA,OAAO,EAiBL,KAAK,MAAM,EACX,KAAK,YAAY,EACjB,KAAK,UAAU,EACf,KAAK,KAAK,EACV,KAAK,OAAO,EACZ,KAAK,cAAc,EAEnB,KAAK,eAAe,EACpB,KAAK,OAAO,EACb,MAAM,6BAA6B,CAAC;AAErC,0CAA0C;AAC1C,eAAO,MAAM,YAAY;;;;;CAKf,CAAC;AAEX,MAAM,WAAW,WAAW;IAC1B,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;CACzB;A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EAAsE;AACtE,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,CAGnD"}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { assertValidAmendment, assertValidOathDoc, assertValidUnsignedOathDoc, bytesToHex, canonicalize, edPublicKey, leafBytesOf, leafHash, oathHash, ProtocolError, rootFromLeafHashes, signEntry, signOathDoc, signSth, SPEC_VERSION, utf8Bytes, } from '@thehonestmachine/oath-core';
|
|
4
|
+
/** File layout per SPEC.md Appendix A. */
|
|
5
|
+
export const LEDGER_FILES = {
|
|
6
|
+
oath: 'oath.json',
|
|
7
|
+
history: 'oath.history',
|
|
8
|
+
entries: 'entries.jsonl',
|
|
9
|
+
heads: 'heads.json',
|
|
10
|
+
};
|
|
11
|
+
/** RFC 3339 UTC now — Date#toISOString emits exactly the spec's format. */
|
|
12
|
+
function defaultNow() {
|
|
13
|
+
return new Date().toISOString();
|
|
14
|
+
}
|
|
15
|
+
function historyPath(dir, version) {
|
|
16
|
+
return join(dir, LEDGER_FILES.history, `v${version}.json`);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* A local ledger (SPEC.md §4 storage note, Appendix A): the agent-operated,
|
|
20
|
+
* on-disk, offline-verifiable form of the OATH log. The agent's key doubles
|
|
21
|
+
* as the ledger key (§5).
|
|
22
|
+
*/
|
|
23
|
+
export class LocalLedger {
|
|
24
|
+
dir;
|
|
25
|
+
seed;
|
|
26
|
+
oathVersions;
|
|
27
|
+
leafHashes;
|
|
28
|
+
heads;
|
|
29
|
+
ledgerName;
|
|
30
|
+
now;
|
|
31
|
+
constructor(dir, seed, oathVersions, leafHashes, heads, ledgerName, now) {
|
|
32
|
+
this.dir = dir;
|
|
33
|
+
this.seed = seed;
|
|
34
|
+
this.oathVersions = oathVersions;
|
|
35
|
+
this.leafHashes = leafHashes;
|
|
36
|
+
this.heads = heads;
|
|
37
|
+
this.ledgerName = ledgerName;
|
|
38
|
+
this.now = now;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Take the oath: sign version 1, create the ledger directory, and log the
|
|
42
|
+
* `oath.swear` entry as leaf 0 (SPEC.md §2.4).
|
|
43
|
+
*/
|
|
44
|
+
static swear(options) {
|
|
45
|
+
const { dir, oath, seed } = options;
|
|
46
|
+
const now = options.now ?? defaultNow;
|
|
47
|
+
assertValidUnsignedOathDoc(oath);
|
|
48
|
+
if (oath.version !== 1) {
|
|
49
|
+
throw new ProtocolError('an agent swears at version 1; later versions are amendments (SPEC.md §9)');
|
|
50
|
+
}
|
|
51
|
+
if (oath.pubkey !== edPublicKey(seed)) {
|
|
52
|
+
throw new ProtocolError('oath pubkey does not match the signing seed');
|
|
53
|
+
}
|
|
54
|
+
if (existsSync(join(dir, LEDGER_FILES.oath))) {
|
|
55
|
+
throw new ProtocolError(`a ledger already exists at ${dir}`);
|
|
56
|
+
}
|
|
57
|
+
const doc = signOathDoc(oath, seed);
|
|
58
|
+
// Write oath.json LAST: it is the sentinel the swear/open guards key on
|
|
59
|
+
// (existsSync above, and open() reads it first). Everything else is
|
|
60
|
+
// written and the swear leaf appended before it, so a crash mid-write
|
|
61
|
+
// leaves a directory that a retried swear() cleanly overwrites rather
|
|
62
|
+
// than a half-built ledger that can be neither sworn nor opened.
|
|
63
|
+
mkdirSync(join(dir, LEDGER_FILES.history), { recursive: true });
|
|
64
|
+
writeFileSync(historyPath(dir, 1), canonicalize(doc) + '\n');
|
|
65
|
+
writeFileSync(join(dir, LEDGER_FILES.entries), '');
|
|
66
|
+
writeFileSync(join(dir, LEDGER_FILES.heads), '[]\n');
|
|
67
|
+
const ledger = new LocalLedger(dir, seed, [doc], [], [], options.ledgerName ?? `${doc.agent}.local`, now);
|
|
68
|
+
const occurredAt = now();
|
|
69
|
+
ledger.append({
|
|
70
|
+
action: {
|
|
71
|
+
type: 'oath.swear',
|
|
72
|
+
summary: `Swore the oath of ${doc.agent}, version 1.`,
|
|
73
|
+
content_hash: oathHash(doc),
|
|
74
|
+
occurred_at: occurredAt,
|
|
75
|
+
},
|
|
76
|
+
verdict: 'compliant',
|
|
77
|
+
law_cited: null,
|
|
78
|
+
adjudication: { mode: 'machine', check: 'oath.swear' },
|
|
79
|
+
});
|
|
80
|
+
writeFileSync(join(dir, LEDGER_FILES.oath), canonicalize(doc) + '\n');
|
|
81
|
+
return ledger;
|
|
82
|
+
}
|
|
83
|
+
/** Open an existing ledger for appending. Verification is the monitor's job. */
|
|
84
|
+
static open(options) {
|
|
85
|
+
const { dir, seed } = options;
|
|
86
|
+
const oathVersions = readOathVersions(dir);
|
|
87
|
+
const current = oathVersions[oathVersions.length - 1];
|
|
88
|
+
if (current.pubkey !== edPublicKey(seed)) {
|
|
89
|
+
throw new ProtocolError('seed does not match the current oath pubkey');
|
|
90
|
+
}
|
|
91
|
+
const leafHashes = readLeafLines(dir).map((line) => leafHash(utf8Bytes(line)));
|
|
92
|
+
const heads = JSON.parse(readFileSync(join(dir, LEDGER_FILES.heads), 'utf8'));
|
|
93
|
+
const ledgerName = heads.length > 0 ? heads[heads.length - 1].ledger : `${current.agent}.local`;
|
|
94
|
+
return new LocalLedger(dir, seed, oathVersions, leafHashes, heads, ledgerName, options.now ?? defaultNow);
|
|
95
|
+
}
|
|
96
|
+
get currentOath() {
|
|
97
|
+
return this.oathVersions[this.oathVersions.length - 1];
|
|
98
|
+
}
|
|
99
|
+
get treeSize() {
|
|
100
|
+
return this.leafHashes.length;
|
|
101
|
+
}
|
|
102
|
+
latestHead() {
|
|
103
|
+
return this.heads.length > 0 ? this.heads[this.heads.length - 1] : null;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Sign and append an entry, then emit a fresh STH — the local ledger emits
|
|
107
|
+
* one per append so its contents are always presentable (SPEC.md §5).
|
|
108
|
+
*/
|
|
109
|
+
append(fields) {
|
|
110
|
+
const current = this.currentOath;
|
|
111
|
+
const unsigned = {
|
|
112
|
+
spec_version: SPEC_VERSION,
|
|
113
|
+
agent: current.agent,
|
|
114
|
+
oath_version: current.version,
|
|
115
|
+
...fields,
|
|
116
|
+
timestamp: this.now(),
|
|
117
|
+
};
|
|
118
|
+
if (fields.confession === undefined)
|
|
119
|
+
delete unsigned.confession;
|
|
120
|
+
const entry = signEntry(unsigned, this.seed);
|
|
121
|
+
const line = canonicalize(entry);
|
|
122
|
+
appendFileSync(join(this.dir, LEDGER_FILES.entries), line + '\n');
|
|
123
|
+
this.leafHashes.push(leafHash(utf8Bytes(line)));
|
|
124
|
+
const head = signSth({
|
|
125
|
+
ledger: this.ledgerName,
|
|
126
|
+
tree_size: this.leafHashes.length,
|
|
127
|
+
root_hash: bytesToHex(rootFromLeafHashes(this.leafHashes)),
|
|
128
|
+
timestamp: this.now(),
|
|
129
|
+
}, this.seed);
|
|
130
|
+
this.heads.push(head);
|
|
131
|
+
writeFileSync(join(this.dir, LEDGER_FILES.heads), JSON.stringify(this.heads, null, 2) + '\n');
|
|
132
|
+
return { entry, head };
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Amend the oath (SPEC.md §9): sign the next version with the current key,
|
|
136
|
+
* write it, and log the `oath.amend` entry. For key rotation pass the new
|
|
137
|
+
* key's seed; future entries and tree heads are signed with it.
|
|
138
|
+
*/
|
|
139
|
+
amend(next, options) {
|
|
140
|
+
const signed = signOathDoc(next, this.seed);
|
|
141
|
+
assertValidAmendment(this.currentOath, signed);
|
|
142
|
+
const activeSeed = options.newSeed ?? this.seed;
|
|
143
|
+
if (signed.pubkey !== edPublicKey(activeSeed)) {
|
|
144
|
+
throw new ProtocolError('amended oath pubkey does not match the active seed (pass newSeed when rotating keys)');
|
|
145
|
+
}
|
|
146
|
+
// Write order matters for crash recovery. oath.json is the sentinel that
|
|
147
|
+
// readOathVersions keys its version loop on, so it goes LAST. First log
|
|
148
|
+
// the amend entry (still adjudicated under the PRIOR version — the last
|
|
149
|
+
// act of the old oath, §9), then the new history file, then oath.json.
|
|
150
|
+
// A crash before oath.json leaves an OPENABLE ledger at the prior version
|
|
151
|
+
// (with an orphan amend leaf a monitor will flag), never an un-openable
|
|
152
|
+
// one pointing at a version whose history file does not yet exist.
|
|
153
|
+
const result = this.append({
|
|
154
|
+
action: {
|
|
155
|
+
type: 'oath.amend',
|
|
156
|
+
summary: options.summary,
|
|
157
|
+
content_hash: oathHash(signed),
|
|
158
|
+
occurred_at: this.now(),
|
|
159
|
+
},
|
|
160
|
+
verdict: 'compliant',
|
|
161
|
+
law_cited: null,
|
|
162
|
+
adjudication: { mode: 'machine', check: 'oath.amend' },
|
|
163
|
+
});
|
|
164
|
+
writeFileSync(historyPath(this.dir, signed.version), canonicalize(signed) + '\n');
|
|
165
|
+
writeFileSync(join(this.dir, LEDGER_FILES.oath), canonicalize(signed) + '\n');
|
|
166
|
+
this.oathVersions.push(signed);
|
|
167
|
+
this.seed = activeSeed;
|
|
168
|
+
return result;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
/** Read and structurally validate the oath version chain from disk. */
|
|
172
|
+
export function readOathVersions(dir) {
|
|
173
|
+
const currentRaw = readFileSync(join(dir, LEDGER_FILES.oath), 'utf8');
|
|
174
|
+
const current = JSON.parse(currentRaw);
|
|
175
|
+
assertValidOathDoc(current);
|
|
176
|
+
const versions = [];
|
|
177
|
+
for (let v = 1; v <= current.version; v++) {
|
|
178
|
+
const doc = JSON.parse(readFileSync(historyPath(dir, v), 'utf8'));
|
|
179
|
+
assertValidOathDoc(doc);
|
|
180
|
+
if (doc.version !== v) {
|
|
181
|
+
throw new ProtocolError(`${LEDGER_FILES.history}/v${v}.json contains version ${doc.version}`);
|
|
182
|
+
}
|
|
183
|
+
versions.push(doc);
|
|
184
|
+
}
|
|
185
|
+
if (canonicalize(versions[versions.length - 1]) !== canonicalize(current)) {
|
|
186
|
+
throw new ProtocolError('oath.json does not match the latest history version');
|
|
187
|
+
}
|
|
188
|
+
return versions;
|
|
189
|
+
}
|
|
190
|
+
/** Read raw entry lines (the exact leaf bytes, newline-separated). */
|
|
191
|
+
export function readLeafLines(dir) {
|
|
192
|
+
const raw = readFileSync(join(dir, LEDGER_FILES.entries), 'utf8');
|
|
193
|
+
return raw === '' ? [] : raw.replace(/\n$/, '').split('\n');
|
|
194
|
+
}
|
|
195
|
+
//# sourceMappingURL=localLedger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"localLedger.js","sourceRoot":"","sources":["../src/localLedger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC7F,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EACL,oBAAoB,EACpB,kBAAkB,EAClB,0BAA0B,EAC1B,UAAU,EACV,YAAY,EACZ,WAAW,EACX,WAAW,EACX,QAAQ,EACR,QAAQ,EACR,aAAa,EACb,kBAAkB,EAClB,SAAS,EACT,WAAW,EACX,OAAO,EACP,YAAY,EACZ,SAAS,GAUV,MAAM,6BAA6B,CAAC;AAErC,0CAA0C;AAC1C,MAAM,CAAC,MAAM,YAAY,GAAG;IAC1B,IAAI,EAAE,WAAW;IACjB,OAAO,EAAE,cAAc;IACvB,OAAO,EAAE,eAAe;IACxB,KAAK,EAAE,YAAY;CACX,CAAC;AAeX,2EAA2E;AAC3E,SAAS,UAAU;IACjB,OAAO,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;AAClC,CAAC;AAED,SAAS,WAAW,CAAC,GAAW,EAAE,OAAe;IAC/C,OAAO,IAAI,CAAC,GAAG,EAAE,YAAY,CAAC,OAAO,EAAE,IAAI,OAAO,OAAO,CAAC,CAAC;AAC7D,CAAC;AAED;;;;GAIG;AACH,MAAM,OAAO,WAAW;IAEX;IACD;IACS;IACA;IACA;IACA;IACA;IAPnB,YACW,GAAW,EACZ,I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AE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;AAC9D,CAAC"}
|
package/dist/remote.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type Entry, type OathDoc, type Receipt } from '@thehonestmachine/oath-core';
|
|
2
|
+
export interface LedgerDiscovery {
|
|
3
|
+
ledger: string;
|
|
4
|
+
pubkey: string;
|
|
5
|
+
api: string;
|
|
6
|
+
mmd_seconds: number;
|
|
7
|
+
spec_version: string;
|
|
8
|
+
}
|
|
9
|
+
export interface SubmitResult {
|
|
10
|
+
leaf_index: number;
|
|
11
|
+
leaf_hash: string;
|
|
12
|
+
receipt: Receipt;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* A client for a public ledger's API (SPEC.md §11.2) that trusts nothing:
|
|
16
|
+
* every receipt is validated and signature-checked against the ledger's
|
|
17
|
+
* published key before it is returned to the caller.
|
|
18
|
+
*/
|
|
19
|
+
export declare class RemoteLedger {
|
|
20
|
+
private readonly base;
|
|
21
|
+
private readonly f;
|
|
22
|
+
private cachedDiscovery;
|
|
23
|
+
constructor(baseUrl: string, options?: {
|
|
24
|
+
fetch?: typeof fetch;
|
|
25
|
+
});
|
|
26
|
+
private request;
|
|
27
|
+
discovery(): Promise<LedgerDiscovery>;
|
|
28
|
+
private checkedResult;
|
|
29
|
+
/** Swear an agent: submit the signed oath v1 plus its oath.swear entry (§2.4). */
|
|
30
|
+
swear(oath: OathDoc, entry: Entry): Promise<SubmitResult>;
|
|
31
|
+
/** Submit a signed entry; idempotent by leaf hash on the ledger side. */
|
|
32
|
+
submit(entry: Entry): Promise<SubmitResult>;
|
|
33
|
+
/** Submit an amendment: the new oath version plus its oath.amend entry (§9). */
|
|
34
|
+
amend(handle: string, oath: OathDoc, entry: Entry): Promise<SubmitResult>;
|
|
35
|
+
/** The agent's public record: oath versions, entry count, and index. */
|
|
36
|
+
agent(handle: string): Promise<Record<string, unknown>>;
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=remote.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"remote.d.ts","sourceRoot":"","sources":["../src/remote.ts"],"names":[],"mappings":"AAAA,OAAO,EAOL,KAAK,KAAK,EACV,KAAK,OAAO,EACZ,KAAK,OAAO,EACb,MAAM,6BAA6B,CAAC;AAErC,MAAM,WAAW,eAAe;IAC9B,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,EAAE,MAAM,CAAC;IACZ,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,YAAY;IAC3B,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,OAAO,CAAC;CAClB;AAED;;;;GAIG;AACH,qBAAa,YAAY;IACvB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAS;IAC9B,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAe;IACjC,OAAO,CAAC,eAAe,CAAgC;gBAE3C,OAAO,EAAE,MAAM,EAAE,OAAO,GAAE;QAAE,KAAK,CAAC,EAAE,OAAO,KAAK,CAAA;KAAO;YAKrD,OAAO;IAWf,SAAS,IAAI,OAAO,CAAC,eAAe,CAAC;YAO7B,aAAa;IAa3B,kFAAkF;IAC5E,KAAK,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,GAAG,OAAO,CAAC,YAAY,CAAC;IAW/D,yEAAyE;IACnE,MAAM,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAAC,YAAY,CAAC;IAUjD,gFAAgF;IAC1E,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,GAAG,OAAO,CAAC,YAAY,CAAC;IAW/E,wEAAwE;IAClE,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAG9D"}
|
package/dist/remote.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { assertValidEntry, assertValidOathDoc, assertValidReceipt, entryLeafHash, ProtocolError, verifyReceiptSignature, } from '@thehonestmachine/oath-core';
|
|
2
|
+
/**
|
|
3
|
+
* A client for a public ledger's API (SPEC.md §11.2) that trusts nothing:
|
|
4
|
+
* every receipt is validated and signature-checked against the ledger's
|
|
5
|
+
* published key before it is returned to the caller.
|
|
6
|
+
*/
|
|
7
|
+
export class RemoteLedger {
|
|
8
|
+
base;
|
|
9
|
+
f;
|
|
10
|
+
cachedDiscovery = null;
|
|
11
|
+
constructor(baseUrl, options = {}) {
|
|
12
|
+
this.base = baseUrl.replace(/\/+$/, '');
|
|
13
|
+
this.f = options.fetch ?? fetch;
|
|
14
|
+
}
|
|
15
|
+
async request(path, init) {
|
|
16
|
+
const response = await this.f(`${this.base}${path}`, init);
|
|
17
|
+
const body = (await response.json().catch(() => ({})));
|
|
18
|
+
if (!response.ok) {
|
|
19
|
+
throw new ProtocolError(`ledger ${path} -> ${response.status}${typeof body['error'] === 'string' ? `: ${body['error']}` : ''}`);
|
|
20
|
+
}
|
|
21
|
+
return body;
|
|
22
|
+
}
|
|
23
|
+
async discovery() {
|
|
24
|
+
if (this.cachedDiscovery === null) {
|
|
25
|
+
this.cachedDiscovery = (await this.request('/v0/ledger.json'));
|
|
26
|
+
}
|
|
27
|
+
return this.cachedDiscovery;
|
|
28
|
+
}
|
|
29
|
+
async checkedResult(entry, raw) {
|
|
30
|
+
const receipt = raw['receipt'];
|
|
31
|
+
assertValidReceipt(receipt);
|
|
32
|
+
const { pubkey } = await this.discovery();
|
|
33
|
+
if (!verifyReceiptSignature(receipt, pubkey)) {
|
|
34
|
+
throw new ProtocolError("the ledger's receipt signature does not verify against its published key");
|
|
35
|
+
}
|
|
36
|
+
if (receipt.leaf_hash !== entryLeafHash(entry)) {
|
|
37
|
+
throw new ProtocolError('the receipt commits to a different leaf than the submitted entry');
|
|
38
|
+
}
|
|
39
|
+
return { leaf_index: Number(raw['leaf_index']), leaf_hash: receipt.leaf_hash, receipt };
|
|
40
|
+
}
|
|
41
|
+
/** Swear an agent: submit the signed oath v1 plus its oath.swear entry (§2.4). */
|
|
42
|
+
async swear(oath, entry) {
|
|
43
|
+
assertValidOathDoc(oath);
|
|
44
|
+
assertValidEntry(entry);
|
|
45
|
+
const raw = (await this.request('/v0/agents', {
|
|
46
|
+
method: 'POST',
|
|
47
|
+
headers: { 'content-type': 'application/json' },
|
|
48
|
+
body: JSON.stringify({ oath, entry }),
|
|
49
|
+
}));
|
|
50
|
+
return this.checkedResult(entry, raw);
|
|
51
|
+
}
|
|
52
|
+
/** Submit a signed entry; idempotent by leaf hash on the ledger side. */
|
|
53
|
+
async submit(entry) {
|
|
54
|
+
assertValidEntry(entry);
|
|
55
|
+
const raw = (await this.request('/v0/entries', {
|
|
56
|
+
method: 'POST',
|
|
57
|
+
headers: { 'content-type': 'application/json' },
|
|
58
|
+
body: JSON.stringify({ entry }),
|
|
59
|
+
}));
|
|
60
|
+
return this.checkedResult(entry, raw);
|
|
61
|
+
}
|
|
62
|
+
/** Submit an amendment: the new oath version plus its oath.amend entry (§9). */
|
|
63
|
+
async amend(handle, oath, entry) {
|
|
64
|
+
assertValidOathDoc(oath);
|
|
65
|
+
assertValidEntry(entry);
|
|
66
|
+
const raw = (await this.request(`/v0/agents/${handle}/oath`, {
|
|
67
|
+
method: 'POST',
|
|
68
|
+
headers: { 'content-type': 'application/json' },
|
|
69
|
+
body: JSON.stringify({ oath, entry }),
|
|
70
|
+
}));
|
|
71
|
+
return this.checkedResult(entry, raw);
|
|
72
|
+
}
|
|
73
|
+
/** The agent's public record: oath versions, entry count, and index. */
|
|
74
|
+
async agent(handle) {
|
|
75
|
+
return (await this.request(`/v0/agents/${handle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