@zanii/succession 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 +159 -0
- package/README.md +54 -0
- package/dist/index.d.ts +83 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +198 -0
- package/dist/index.js.map +1 -0
- package/package.json +44 -0
- package/src/index.ts +256 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or Derivative
|
|
95
|
+
Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work; and
|
|
103
|
+
|
|
104
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
105
|
+
distribution, then any Derivative Works that You distribute must
|
|
106
|
+
include a readable copy of the attribution notices contained
|
|
107
|
+
within such NOTICE file.
|
|
108
|
+
|
|
109
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
110
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
111
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
112
|
+
this License, without any additional terms or conditions.
|
|
113
|
+
|
|
114
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
115
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
116
|
+
except as required for reasonable and customary use in describing the
|
|
117
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
118
|
+
|
|
119
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed
|
|
120
|
+
to in writing, Licensor provides the Work (and each Contributor
|
|
121
|
+
provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES
|
|
122
|
+
OR CONDITIONS OF ANY KIND, either express or implied, including,
|
|
123
|
+
without limitation, any warranties or conditions of TITLE,
|
|
124
|
+
NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE.
|
|
125
|
+
|
|
126
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
127
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
128
|
+
unless required by applicable law (such as deliberate and grossly
|
|
129
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
130
|
+
liable to You for damages, including any direct, indirect, special,
|
|
131
|
+
incidental, or consequential damages of any character arising as a
|
|
132
|
+
result of this License or out of the use or inability to use the
|
|
133
|
+
Work.
|
|
134
|
+
|
|
135
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
136
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
137
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
138
|
+
or other liability obligations and/or rights consistent with this
|
|
139
|
+
License. However, in accepting such obligations, You may act only
|
|
140
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
141
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
142
|
+
defend, and hold each Contributor harmless for any liability
|
|
143
|
+
incurred by, or claims asserted against, such Contributor.
|
|
144
|
+
|
|
145
|
+
END OF TERMS AND CONDITIONS
|
|
146
|
+
|
|
147
|
+
Copyright 2026 Zanii
|
|
148
|
+
|
|
149
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
150
|
+
you may not use this file except in compliance with the License.
|
|
151
|
+
You may obtain a copy of the License at
|
|
152
|
+
|
|
153
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
154
|
+
|
|
155
|
+
Unless required by applicable law or agreed to in writing, software
|
|
156
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
157
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
158
|
+
See the License for the specific language governing permissions and
|
|
159
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# @zanii/succession
|
|
2
|
+
|
|
3
|
+
Agent lifecycle: **key rotation with signed continuity**. An agent's did:key IS its
|
|
4
|
+
key, so rotating a compromised key normally kills its reputation, CV history, and
|
|
5
|
+
counterparty trust. Succession fixes it: the **owner** — the same root authority whose
|
|
6
|
+
delegation empowered the old key — signs "agent B is the successor of agent A", and
|
|
7
|
+
verifiers follow the lineage instead of starting from zero.
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @zanii/succession @zanii/core @zanii/a2a-directory
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { buildSuccessionBody, signSuccession, assembleSuccession, verifySuccession, verifyLineage, combinedSummary } from '@zanii/succession';
|
|
15
|
+
|
|
16
|
+
// planned rotation — old key still safe, so it countersigns (the strong form)
|
|
17
|
+
const body = buildSuccessionBody({ predecessor: oldAgent.did, successor: newAgent.did, owner: owner.did, reason: 'rotation', ts: now });
|
|
18
|
+
const s = assembleSuccession(body, [
|
|
19
|
+
signSuccession(body, owner.did, ownerKey), // REQUIRED — the trust anchor
|
|
20
|
+
signSuccession(body, oldAgent.did, oldAgentKey), // optional — sets `cosigned`
|
|
21
|
+
]);
|
|
22
|
+
verifySuccession(s); // { ok, reasons, cosigned }
|
|
23
|
+
|
|
24
|
+
// after a compromise, compromised_at is REQUIRED — it brackets the trusted window
|
|
25
|
+
buildSuccessionBody({ ..., reason: 'compromise', compromisedAt: '2026-07-20T00:00:00Z' });
|
|
26
|
+
|
|
27
|
+
// walk a chain A→B→C and aggregate reputation across it (conservatively)
|
|
28
|
+
verifyLineage(links, currentDid); // { ok, lineage: [oldest…newest] }
|
|
29
|
+
combinedSummary(links, trusts, currentDid); // sums + explicit caveats
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Python: `from zanii.succession import build_succession_body, verify_lineage, combined_summary, ...`
|
|
33
|
+
— byte-identical JCS objects and hashes.
|
|
34
|
+
|
|
35
|
+
## The rules that keep it honest
|
|
36
|
+
|
|
37
|
+
- **No owner, no succession** — anyone can *claim* to succeed anyone; only the owner's
|
|
38
|
+
signature makes it real. After a compromise the predecessor's signature is
|
|
39
|
+
deliberately NOT required (the attacker holds that key).
|
|
40
|
+
- **`compromise` requires `compromised_at`** — history transfers, but receipts after
|
|
41
|
+
that instant under the old key may be the attacker's; `combinedSummary` says so in
|
|
42
|
+
its `caveats` instead of silently inheriting them. Pair with a revocation of the old
|
|
43
|
+
delegation: succession records continuity, revocation kills the old authority.
|
|
44
|
+
- **One lineage, one owner** — a lineage spanning owners is refused; an ownership
|
|
45
|
+
transfer is a sale, not a succession, and conflating them would launder history.
|
|
46
|
+
- **Conservative aggregation** — combined receipt counts are an upper bound and say
|
|
47
|
+
so; counterparties are deliberately not summed (overlap is unknowable).
|
|
48
|
+
|
|
49
|
+
## The limit, stated up front
|
|
50
|
+
|
|
51
|
+
Succession proves **continuity of authority** — the owner vouches that B carries A's
|
|
52
|
+
mandate. It does NOT prove B runs the same code or behaves like A (`@zanii/attest` is
|
|
53
|
+
for which-code-ran). And it is the owner's attestation: a lying owner's lie is signed
|
|
54
|
+
and permanent — attributable, not impossible.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { AgentTrust } from '@zanii/a2a-directory';
|
|
2
|
+
export type SuccessionReason = 'rotation' | 'compromise' | 'retirement';
|
|
3
|
+
export interface SuccessionBody {
|
|
4
|
+
v: 1;
|
|
5
|
+
type: 'succession';
|
|
6
|
+
/** The agent being succeeded (the old did:key). */
|
|
7
|
+
predecessor: string;
|
|
8
|
+
/** The agent taking over (the new did:key). */
|
|
9
|
+
successor: string;
|
|
10
|
+
/** The root authority attesting the handover — the trust anchor. */
|
|
11
|
+
owner: string;
|
|
12
|
+
reason: SuccessionReason;
|
|
13
|
+
/** REQUIRED when reason is 'compromise': receipts after this instant under the old key are suspect. */
|
|
14
|
+
compromised_at?: string;
|
|
15
|
+
ts: string;
|
|
16
|
+
note?: string;
|
|
17
|
+
}
|
|
18
|
+
export interface SuccessionSignature {
|
|
19
|
+
did: string;
|
|
20
|
+
/** `ed25519:<hex>` over the canonical (JCS) bytes of the body. */
|
|
21
|
+
sig: string;
|
|
22
|
+
}
|
|
23
|
+
export interface Succession {
|
|
24
|
+
body: SuccessionBody;
|
|
25
|
+
signatures: SuccessionSignature[];
|
|
26
|
+
}
|
|
27
|
+
export interface SuccessionInput {
|
|
28
|
+
predecessor: string;
|
|
29
|
+
successor: string;
|
|
30
|
+
owner: string;
|
|
31
|
+
reason: SuccessionReason;
|
|
32
|
+
compromisedAt?: string;
|
|
33
|
+
ts: string;
|
|
34
|
+
note?: string;
|
|
35
|
+
}
|
|
36
|
+
export declare function buildSuccessionBody(input: SuccessionInput): SuccessionBody;
|
|
37
|
+
/** Sign as the owner (required) or as the predecessor (optional, strengthens a planned handover). */
|
|
38
|
+
export declare function signSuccession(body: SuccessionBody, did: string, privateKey: Uint8Array): SuccessionSignature;
|
|
39
|
+
export declare function assembleSuccession(body: SuccessionBody, signatures: SuccessionSignature[]): Succession;
|
|
40
|
+
export declare function successionHash(s: Succession): string;
|
|
41
|
+
export interface SuccessionCheck {
|
|
42
|
+
ok: boolean;
|
|
43
|
+
reasons: string[];
|
|
44
|
+
/** true when the predecessor ALSO signed — the strong (planned) form. */
|
|
45
|
+
cosigned: boolean;
|
|
46
|
+
}
|
|
47
|
+
/** Verify: structure sane + the OWNER signed. The predecessor's countersignature sets `cosigned`. */
|
|
48
|
+
export declare function verifySuccession(s: Succession | undefined): SuccessionCheck;
|
|
49
|
+
export interface LineageCheck {
|
|
50
|
+
ok: boolean;
|
|
51
|
+
reasons: string[];
|
|
52
|
+
/** Oldest → newest DIDs, ending at the queried agent when ok. */
|
|
53
|
+
lineage: string[];
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Walk a chain of successions back from `did` (newest agent). Requires: every link
|
|
57
|
+
* verifies, links connect (predecessor of one = successor of the previous), and ALL
|
|
58
|
+
* links share one owner — a lineage is one authority's history. (An ownership transfer
|
|
59
|
+
* is a sale, not a succession; this deliberately refuses to conflate them.)
|
|
60
|
+
*/
|
|
61
|
+
export declare function verifyLineage(successions: Succession[], did: string): LineageCheck;
|
|
62
|
+
export interface CombinedSummary {
|
|
63
|
+
/** Oldest → newest. */
|
|
64
|
+
lineage: string[];
|
|
65
|
+
/** Sum across the lineage — an UPPER BOUND (see caveats). */
|
|
66
|
+
receipts: number;
|
|
67
|
+
first_ts: string | null;
|
|
68
|
+
last_ts: string | null;
|
|
69
|
+
/** Current agent's status — a retired predecessor being revoked is normal. */
|
|
70
|
+
revoked: boolean;
|
|
71
|
+
fully_anchored: boolean;
|
|
72
|
+
/** Facts a reader must see before trusting the combined numbers. */
|
|
73
|
+
caveats: string[];
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Aggregate ledger reputation across a verified lineage. Deliberately conservative:
|
|
77
|
+
* receipt counts SUM (an upper bound — cross-key duplicates can't be detected from
|
|
78
|
+
* aggregates), counterparties are NOT summed (overlap is unknowable here), and any
|
|
79
|
+
* compromise link adds a caveat that the predecessor's post-compromise receipts are
|
|
80
|
+
* suspect. Pass `trusts` in the same order as `lineage`.
|
|
81
|
+
*/
|
|
82
|
+
export declare function combinedSummary(successions: Succession[], trusts: AgentTrust[], did: string): CombinedSummary;
|
|
83
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAkCA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAGvD,MAAM,MAAM,gBAAgB,GAAG,UAAU,GAAG,YAAY,GAAG,YAAY,CAAC;AAExE,MAAM,WAAW,cAAc;IAC7B,CAAC,EAAE,CAAC,CAAC;IACL,IAAI,EAAE,YAAY,CAAC;IACnB,mDAAmD;IACnD,WAAW,EAAE,MAAM,CAAC;IACpB,+CAA+C;IAC/C,SAAS,EAAE,MAAM,CAAC;IAClB,oEAAoE;IACpE,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,gBAAgB,CAAC;IACzB,uGAAuG;IACvG,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,mBAAmB;IAClC,GAAG,EAAE,MAAM,CAAC;IACZ,kEAAkE;IAClE,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,cAAc,CAAC;IACrB,UAAU,EAAE,mBAAmB,EAAE,CAAC;CACnC;AAED,MAAM,WAAW,eAAe;IAC9B,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,gBAAgB,CAAC;IACzB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,eAAe,GAAG,cAAc,CAwB1E;AAgBD,qGAAqG;AACrG,wBAAgB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}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zanii/succession — agent lifecycle: key rotation with signed continuity.
|
|
3
|
+
*
|
|
4
|
+
* An agent's did:key IS its key, so rotating a key normally means the reputation, CV
|
|
5
|
+
* history, and counterparty trust die with it. The first time a customer rotates a
|
|
6
|
+
* compromised key, that's an incident. Succession fixes it: the **owner** — the same
|
|
7
|
+
* root authority whose delegation gave the old key its power — signs a continuity
|
|
8
|
+
* certificate: "agent B is the successor of agent A." Verifiers, `@zanii/cv`, and
|
|
9
|
+
* reputation readers can then follow the lineage instead of starting from zero.
|
|
10
|
+
*
|
|
11
|
+
* Two signatures, two strengths:
|
|
12
|
+
* - **Owner signature (required)** — the trust anchor. Succession without the owner is
|
|
13
|
+
* meaningless: anyone can claim to succeed anyone.
|
|
14
|
+
* - **Predecessor signature (optional, `cosigned`)** — when the old key is still safe
|
|
15
|
+
* (planned rotation, retirement), the old agent countersigning is strictly stronger.
|
|
16
|
+
* After a compromise it is deliberately NOT required — the attacker holds that key.
|
|
17
|
+
*
|
|
18
|
+
* ## The rule that keeps compromise honest
|
|
19
|
+
*
|
|
20
|
+
* A succession with `reason: 'compromise'` **requires `compromised_at`**. History
|
|
21
|
+
* transfers, but the compromise timestamp brackets the trusted window: receipts after
|
|
22
|
+
* `compromised_at` under the old key may be the attacker's, and `combinedSummary`
|
|
23
|
+
* surfaces that instead of silently inheriting them. (Pair with a revocation of the
|
|
24
|
+
* old delegation — succession records continuity; revocation kills the old authority.)
|
|
25
|
+
*
|
|
26
|
+
* ## The limit, stated up front
|
|
27
|
+
*
|
|
28
|
+
* Succession proves **continuity of authority** — the owner vouches that B carries A's
|
|
29
|
+
* mandate. It does NOT prove B runs the same code, weights, or behavior as A
|
|
30
|
+
* (`@zanii/attest` is for which-code-ran). And it is the owner's attestation: an owner
|
|
31
|
+
* can lie, in which case the lie is signed and permanent — attributable, not impossible.
|
|
32
|
+
*/
|
|
33
|
+
import { ed25519 } from '@noble/curves/ed25519';
|
|
34
|
+
import { bytesToHex, hexToBytes } from '@noble/hashes/utils';
|
|
35
|
+
import { canonicalBytes, jcsHash, publicKeyFromDid } from '@zanii/core';
|
|
36
|
+
export function buildSuccessionBody(input) {
|
|
37
|
+
if (!input.predecessor || !input.successor || !input.owner)
|
|
38
|
+
throw new Error('predecessor, successor, and owner are required');
|
|
39
|
+
if (input.predecessor === input.successor)
|
|
40
|
+
throw new Error('an agent cannot succeed itself');
|
|
41
|
+
if (!['rotation', 'compromise', 'retirement'].includes(input.reason))
|
|
42
|
+
throw new Error(`invalid reason '${input.reason}'`);
|
|
43
|
+
if (Number.isNaN(Date.parse(input.ts)))
|
|
44
|
+
throw new Error('ts must be an ISO timestamp');
|
|
45
|
+
if (input.reason === 'compromise') {
|
|
46
|
+
if (!input.compromisedAt || Number.isNaN(Date.parse(input.compromisedAt))) {
|
|
47
|
+
throw new Error("a 'compromise' succession REQUIRES compromised_at — it brackets the trusted window; without it the inherited history cannot be honestly assessed");
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
else if (input.compromisedAt !== undefined) {
|
|
51
|
+
throw new Error("compromised_at is only meaningful with reason 'compromise'");
|
|
52
|
+
}
|
|
53
|
+
const body = {
|
|
54
|
+
v: 1,
|
|
55
|
+
type: 'succession',
|
|
56
|
+
predecessor: input.predecessor,
|
|
57
|
+
successor: input.successor,
|
|
58
|
+
owner: input.owner,
|
|
59
|
+
reason: input.reason,
|
|
60
|
+
ts: input.ts,
|
|
61
|
+
};
|
|
62
|
+
if (input.compromisedAt !== undefined)
|
|
63
|
+
body.compromised_at = input.compromisedAt;
|
|
64
|
+
if (input.note !== undefined)
|
|
65
|
+
body.note = input.note;
|
|
66
|
+
return body;
|
|
67
|
+
}
|
|
68
|
+
const sign = (obj, priv) => `ed25519:${bytesToHex(ed25519.sign(canonicalBytes(obj), priv))}`;
|
|
69
|
+
const sigValid = (obj, did, sig) => {
|
|
70
|
+
const pub = publicKeyFromDid(did);
|
|
71
|
+
const hex = typeof sig === 'string' && sig.startsWith('ed25519:') ? sig.slice(8) : '';
|
|
72
|
+
if (!pub || !/^[0-9a-f]{128}$/.test(hex))
|
|
73
|
+
return false;
|
|
74
|
+
try {
|
|
75
|
+
return ed25519.verify(hexToBytes(hex), canonicalBytes(obj), pub);
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
/** Sign as the owner (required) or as the predecessor (optional, strengthens a planned handover). */
|
|
82
|
+
export function signSuccession(body, did, privateKey) {
|
|
83
|
+
return { did, sig: sign(body, privateKey) };
|
|
84
|
+
}
|
|
85
|
+
export function assembleSuccession(body, signatures) {
|
|
86
|
+
return { body, signatures };
|
|
87
|
+
}
|
|
88
|
+
export function successionHash(s) {
|
|
89
|
+
return jcsHash(s);
|
|
90
|
+
}
|
|
91
|
+
/** Verify: structure sane + the OWNER signed. The predecessor's countersignature sets `cosigned`. */
|
|
92
|
+
export function verifySuccession(s) {
|
|
93
|
+
const reasons = [];
|
|
94
|
+
const e = s ?? {};
|
|
95
|
+
const b = e.body ?? {};
|
|
96
|
+
if (b.v !== 1 || b.type !== 'succession')
|
|
97
|
+
reasons.push('not a succession (v1)');
|
|
98
|
+
if (!b.predecessor || !b.successor || !b.owner || b.predecessor === b.successor)
|
|
99
|
+
reasons.push('missing/invalid parties');
|
|
100
|
+
if (!['rotation', 'compromise', 'retirement'].includes(b.reason))
|
|
101
|
+
reasons.push('invalid reason');
|
|
102
|
+
if (b.reason === 'compromise' && (typeof b.compromised_at !== 'string' || Number.isNaN(Date.parse(b.compromised_at)))) {
|
|
103
|
+
reasons.push("a 'compromise' succession requires a valid compromised_at");
|
|
104
|
+
}
|
|
105
|
+
if (typeof b.ts !== 'string' || Number.isNaN(Date.parse(b.ts)))
|
|
106
|
+
reasons.push('missing/invalid ts');
|
|
107
|
+
let cosigned = false;
|
|
108
|
+
if (reasons.length === 0) {
|
|
109
|
+
const sigs = e.signatures ?? [];
|
|
110
|
+
const ownerSig = sigs.find((x) => x.did === b.owner);
|
|
111
|
+
if (!ownerSig || !sigValid(b, ownerSig.did, ownerSig.sig)) {
|
|
112
|
+
reasons.push('missing/invalid OWNER signature — succession without the owner is just a claim');
|
|
113
|
+
}
|
|
114
|
+
const predSig = sigs.find((x) => x.did === b.predecessor);
|
|
115
|
+
cosigned = !!predSig && sigValid(b, predSig.did, predSig.sig);
|
|
116
|
+
}
|
|
117
|
+
return { ok: reasons.length === 0, reasons, cosigned };
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Walk a chain of successions back from `did` (newest agent). Requires: every link
|
|
121
|
+
* verifies, links connect (predecessor of one = successor of the previous), and ALL
|
|
122
|
+
* links share one owner — a lineage is one authority's history. (An ownership transfer
|
|
123
|
+
* is a sale, not a succession; this deliberately refuses to conflate them.)
|
|
124
|
+
*/
|
|
125
|
+
export function verifyLineage(successions, did) {
|
|
126
|
+
const reasons = [];
|
|
127
|
+
if (successions.length === 0)
|
|
128
|
+
return { ok: true, reasons: [], lineage: [did] };
|
|
129
|
+
const bySuccessor = new Map();
|
|
130
|
+
for (const s of successions) {
|
|
131
|
+
const check = verifySuccession(s);
|
|
132
|
+
if (!check.ok)
|
|
133
|
+
return { ok: false, reasons: check.reasons.map((r) => `link ${s?.body?.successor ?? '?'}: ${r}`), lineage: [] };
|
|
134
|
+
if (bySuccessor.has(s.body.successor))
|
|
135
|
+
return { ok: false, reasons: [`two successions claim successor ${s.body.successor}`], lineage: [] };
|
|
136
|
+
bySuccessor.set(s.body.successor, s);
|
|
137
|
+
}
|
|
138
|
+
const owner = successions[0].body.owner;
|
|
139
|
+
if (!successions.every((s) => s.body.owner === owner)) {
|
|
140
|
+
return { ok: false, reasons: ['lineage spans multiple owners — an ownership transfer is not a succession'], lineage: [] };
|
|
141
|
+
}
|
|
142
|
+
const lineage = [did];
|
|
143
|
+
const seen = new Set([did]);
|
|
144
|
+
let cursor = did;
|
|
145
|
+
while (bySuccessor.has(cursor)) {
|
|
146
|
+
const link = bySuccessor.get(cursor);
|
|
147
|
+
const prev = link.body.predecessor;
|
|
148
|
+
if (seen.has(prev))
|
|
149
|
+
return { ok: false, reasons: ['succession cycle detected'], lineage: [] };
|
|
150
|
+
seen.add(prev);
|
|
151
|
+
lineage.unshift(prev);
|
|
152
|
+
cursor = prev;
|
|
153
|
+
}
|
|
154
|
+
if (lineage.length !== successions.length + 1) {
|
|
155
|
+
reasons.push(`${successions.length + 1 - lineage.length} succession(s) do not connect to ${did}'s lineage`);
|
|
156
|
+
}
|
|
157
|
+
return { ok: reasons.length === 0, reasons, lineage };
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Aggregate ledger reputation across a verified lineage. Deliberately conservative:
|
|
161
|
+
* receipt counts SUM (an upper bound — cross-key duplicates can't be detected from
|
|
162
|
+
* aggregates), counterparties are NOT summed (overlap is unknowable here), and any
|
|
163
|
+
* compromise link adds a caveat that the predecessor's post-compromise receipts are
|
|
164
|
+
* suspect. Pass `trusts` in the same order as `lineage`.
|
|
165
|
+
*/
|
|
166
|
+
export function combinedSummary(successions, trusts, did) {
|
|
167
|
+
const lin = verifyLineage(successions, did);
|
|
168
|
+
if (!lin.ok)
|
|
169
|
+
throw new Error(`lineage does not verify: ${lin.reasons.join('; ')}`);
|
|
170
|
+
if (trusts.length !== lin.lineage.length)
|
|
171
|
+
throw new Error(`need one AgentTrust per lineage member (${lin.lineage.length}), got ${trusts.length}`);
|
|
172
|
+
trusts.forEach((t, i) => {
|
|
173
|
+
if (t.agent_id !== lin.lineage[i])
|
|
174
|
+
throw new Error(`trusts[${i}] is for ${t.agent_id}, expected ${lin.lineage[i]}`);
|
|
175
|
+
});
|
|
176
|
+
const caveats = [
|
|
177
|
+
'receipt count is a sum across keys — an upper bound, not a deduplicated total',
|
|
178
|
+
'distinct counterparties are per-key and deliberately not summed (overlap unknowable)',
|
|
179
|
+
];
|
|
180
|
+
for (const s of successions) {
|
|
181
|
+
if (s.body.reason === 'compromise') {
|
|
182
|
+
caveats.push(`${s.body.predecessor} was COMPROMISED at ${s.body.compromised_at} — its receipts after that instant may be an attacker's`);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
const firsts = trusts.map((t) => t.first_ts).filter((x) => !!x);
|
|
186
|
+
const lasts = trusts.map((t) => t.last_ts).filter((x) => !!x);
|
|
187
|
+
const current = trusts[trusts.length - 1];
|
|
188
|
+
return {
|
|
189
|
+
lineage: lin.lineage,
|
|
190
|
+
receipts: trusts.reduce((n, t) => n + t.receipts, 0),
|
|
191
|
+
first_ts: firsts.length ? firsts.reduce((a, b) => (Date.parse(a) <= Date.parse(b) ? a : b)) : null,
|
|
192
|
+
last_ts: lasts.length ? lasts.reduce((a, b) => (Date.parse(a) >= Date.parse(b) ? a : b)) : null,
|
|
193
|
+
revoked: current.revoked,
|
|
194
|
+
fully_anchored: trusts.every((t) => t.fully_anchored),
|
|
195
|
+
caveats,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAChD,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAE7D,OAAO,EAAE,cAAc,EAAE,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAyCxE,MAAM,UAAU,mBAAmB,CAAC,KAAsB;IACxD,IAAI,CAAC,KAAK,CAAC,WAAW,IAAI,CAAC,KAAK,CAAC,SAAS,IAAI,CAAC,KAAK,CAAC,KAAK;QAAE,MAAM,IAAI,KAAK,CAAC,gDAAgD,CAAC,CAAC;IAC9H,IAAI,KAAK,CAAC,WAAW,KAAK,KAAK,CA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CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI;QAClG,OAAO,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI;QAC/F,OAAO,EAAE,OAAO,CAAC,OAAO;QACxB,cAAc,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,cAAc,CAAC;QACrD,OAAO;KACR,CAAC;AACJ,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zanii/succession",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Agent lifecycle and key rotation with signed continuity - the owner attests 'B succeeds A', so reputation and CV history transfer verifiably instead of dying with a key. A compromise succession REQUIRES the compromise timestamp, bracketing the trusted window. Continuity of authority, not identity of code.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"homepage": "https://ledger.zanii.agency",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"src",
|
|
19
|
+
"README.md",
|
|
20
|
+
"LICENSE"
|
|
21
|
+
],
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=18"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/node": "^26.1.0",
|
|
30
|
+
"typescript": "^5.8.2",
|
|
31
|
+
"vitest": "^3.0.9"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@noble/curves": "^1.8.1",
|
|
35
|
+
"@noble/hashes": "^1.7.1",
|
|
36
|
+
"@zanii/a2a-directory": "0.1.0",
|
|
37
|
+
"@zanii/core": "0.4.0"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsc -p tsconfig.build.json",
|
|
41
|
+
"test": "vitest run",
|
|
42
|
+
"typecheck": "tsc --noEmit"
|
|
43
|
+
}
|
|
44
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zanii/succession — agent lifecycle: key rotation with signed continuity.
|
|
3
|
+
*
|
|
4
|
+
* An agent's did:key IS its key, so rotating a key normally means the reputation, CV
|
|
5
|
+
* history, and counterparty trust die with it. The first time a customer rotates a
|
|
6
|
+
* compromised key, that's an incident. Succession fixes it: the **owner** — the same
|
|
7
|
+
* root authority whose delegation gave the old key its power — signs a continuity
|
|
8
|
+
* certificate: "agent B is the successor of agent A." Verifiers, `@zanii/cv`, and
|
|
9
|
+
* reputation readers can then follow the lineage instead of starting from zero.
|
|
10
|
+
*
|
|
11
|
+
* Two signatures, two strengths:
|
|
12
|
+
* - **Owner signature (required)** — the trust anchor. Succession without the owner is
|
|
13
|
+
* meaningless: anyone can claim to succeed anyone.
|
|
14
|
+
* - **Predecessor signature (optional, `cosigned`)** — when the old key is still safe
|
|
15
|
+
* (planned rotation, retirement), the old agent countersigning is strictly stronger.
|
|
16
|
+
* After a compromise it is deliberately NOT required — the attacker holds that key.
|
|
17
|
+
*
|
|
18
|
+
* ## The rule that keeps compromise honest
|
|
19
|
+
*
|
|
20
|
+
* A succession with `reason: 'compromise'` **requires `compromised_at`**. History
|
|
21
|
+
* transfers, but the compromise timestamp brackets the trusted window: receipts after
|
|
22
|
+
* `compromised_at` under the old key may be the attacker's, and `combinedSummary`
|
|
23
|
+
* surfaces that instead of silently inheriting them. (Pair with a revocation of the
|
|
24
|
+
* old delegation — succession records continuity; revocation kills the old authority.)
|
|
25
|
+
*
|
|
26
|
+
* ## The limit, stated up front
|
|
27
|
+
*
|
|
28
|
+
* Succession proves **continuity of authority** — the owner vouches that B carries A's
|
|
29
|
+
* mandate. It does NOT prove B runs the same code, weights, or behavior as A
|
|
30
|
+
* (`@zanii/attest` is for which-code-ran). And it is the owner's attestation: an owner
|
|
31
|
+
* can lie, in which case the lie is signed and permanent — attributable, not impossible.
|
|
32
|
+
*/
|
|
33
|
+
import { ed25519 } from '@noble/curves/ed25519';
|
|
34
|
+
import { bytesToHex, hexToBytes } from '@noble/hashes/utils';
|
|
35
|
+
import type { AgentTrust } from '@zanii/a2a-directory';
|
|
36
|
+
import { canonicalBytes, jcsHash, publicKeyFromDid } from '@zanii/core';
|
|
37
|
+
|
|
38
|
+
export type SuccessionReason = 'rotation' | 'compromise' | 'retirement';
|
|
39
|
+
|
|
40
|
+
export interface SuccessionBody {
|
|
41
|
+
v: 1;
|
|
42
|
+
type: 'succession';
|
|
43
|
+
/** The agent being succeeded (the old did:key). */
|
|
44
|
+
predecessor: string;
|
|
45
|
+
/** The agent taking over (the new did:key). */
|
|
46
|
+
successor: string;
|
|
47
|
+
/** The root authority attesting the handover — the trust anchor. */
|
|
48
|
+
owner: string;
|
|
49
|
+
reason: SuccessionReason;
|
|
50
|
+
/** REQUIRED when reason is 'compromise': receipts after this instant under the old key are suspect. */
|
|
51
|
+
compromised_at?: string;
|
|
52
|
+
ts: string;
|
|
53
|
+
note?: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface SuccessionSignature {
|
|
57
|
+
did: string;
|
|
58
|
+
/** `ed25519:<hex>` over the canonical (JCS) bytes of the body. */
|
|
59
|
+
sig: string;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface Succession {
|
|
63
|
+
body: SuccessionBody;
|
|
64
|
+
signatures: SuccessionSignature[];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface SuccessionInput {
|
|
68
|
+
predecessor: string;
|
|
69
|
+
successor: string;
|
|
70
|
+
owner: string;
|
|
71
|
+
reason: SuccessionReason;
|
|
72
|
+
compromisedAt?: string;
|
|
73
|
+
ts: string;
|
|
74
|
+
note?: string;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function buildSuccessionBody(input: SuccessionInput): SuccessionBody {
|
|
78
|
+
if (!input.predecessor || !input.successor || !input.owner) throw new Error('predecessor, successor, and owner are required');
|
|
79
|
+
if (input.predecessor === input.successor) throw new Error('an agent cannot succeed itself');
|
|
80
|
+
if (!['rotation', 'compromise', 'retirement'].includes(input.reason)) throw new Error(`invalid reason '${input.reason}'`);
|
|
81
|
+
if (Number.isNaN(Date.parse(input.ts))) throw new Error('ts must be an ISO timestamp');
|
|
82
|
+
if (input.reason === 'compromise') {
|
|
83
|
+
if (!input.compromisedAt || Number.isNaN(Date.parse(input.compromisedAt))) {
|
|
84
|
+
throw new Error("a 'compromise' succession REQUIRES compromised_at — it brackets the trusted window; without it the inherited history cannot be honestly assessed");
|
|
85
|
+
}
|
|
86
|
+
} else if (input.compromisedAt !== undefined) {
|
|
87
|
+
throw new Error("compromised_at is only meaningful with reason 'compromise'");
|
|
88
|
+
}
|
|
89
|
+
const body: SuccessionBody = {
|
|
90
|
+
v: 1,
|
|
91
|
+
type: 'succession',
|
|
92
|
+
predecessor: input.predecessor,
|
|
93
|
+
successor: input.successor,
|
|
94
|
+
owner: input.owner,
|
|
95
|
+
reason: input.reason,
|
|
96
|
+
ts: input.ts,
|
|
97
|
+
};
|
|
98
|
+
if (input.compromisedAt !== undefined) body.compromised_at = input.compromisedAt;
|
|
99
|
+
if (input.note !== undefined) body.note = input.note;
|
|
100
|
+
return body;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const sign = (obj: Record<string, unknown>, priv: Uint8Array): string =>
|
|
104
|
+
`ed25519:${bytesToHex(ed25519.sign(canonicalBytes(obj), priv))}`;
|
|
105
|
+
|
|
106
|
+
const sigValid = (obj: Record<string, unknown>, did: string, sig: unknown): boolean => {
|
|
107
|
+
const pub = publicKeyFromDid(did);
|
|
108
|
+
const hex = typeof sig === 'string' && sig.startsWith('ed25519:') ? sig.slice(8) : '';
|
|
109
|
+
if (!pub || !/^[0-9a-f]{128}$/.test(hex)) return false;
|
|
110
|
+
try {
|
|
111
|
+
return ed25519.verify(hexToBytes(hex), canonicalBytes(obj), pub);
|
|
112
|
+
} catch {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
/** Sign as the owner (required) or as the predecessor (optional, strengthens a planned handover). */
|
|
118
|
+
export function signSuccession(body: SuccessionBody, did: string, privateKey: Uint8Array): SuccessionSignature {
|
|
119
|
+
return { did, sig: sign(body as unknown as Record<string, unknown>, privateKey) };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function assembleSuccession(body: SuccessionBody, signatures: SuccessionSignature[]): Succession {
|
|
123
|
+
return { body, signatures };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function successionHash(s: Succession): string {
|
|
127
|
+
return jcsHash(s as unknown as Record<string, unknown>);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export interface SuccessionCheck {
|
|
131
|
+
ok: boolean;
|
|
132
|
+
reasons: string[];
|
|
133
|
+
/** true when the predecessor ALSO signed — the strong (planned) form. */
|
|
134
|
+
cosigned: boolean;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Verify: structure sane + the OWNER signed. The predecessor's countersignature sets `cosigned`. */
|
|
138
|
+
export function verifySuccession(s: Succession | undefined): SuccessionCheck {
|
|
139
|
+
const reasons: string[] = [];
|
|
140
|
+
const e = s ?? ({} as Succession);
|
|
141
|
+
const b = e.body ?? ({} as SuccessionBody);
|
|
142
|
+
if (b.v !== 1 || b.type !== 'succession') reasons.push('not a succession (v1)');
|
|
143
|
+
if (!b.predecessor || !b.successor || !b.owner || b.predecessor === b.successor) reasons.push('missing/invalid parties');
|
|
144
|
+
if (!['rotation', 'compromise', 'retirement'].includes(b.reason)) reasons.push('invalid reason');
|
|
145
|
+
if (b.reason === 'compromise' && (typeof b.compromised_at !== 'string' || Number.isNaN(Date.parse(b.compromised_at)))) {
|
|
146
|
+
reasons.push("a 'compromise' succession requires a valid compromised_at");
|
|
147
|
+
}
|
|
148
|
+
if (typeof b.ts !== 'string' || Number.isNaN(Date.parse(b.ts))) reasons.push('missing/invalid ts');
|
|
149
|
+
|
|
150
|
+
let cosigned = false;
|
|
151
|
+
if (reasons.length === 0) {
|
|
152
|
+
const sigs = e.signatures ?? [];
|
|
153
|
+
const ownerSig = sigs.find((x) => x.did === b.owner);
|
|
154
|
+
if (!ownerSig || !sigValid(b as unknown as Record<string, unknown>, ownerSig.did, ownerSig.sig)) {
|
|
155
|
+
reasons.push('missing/invalid OWNER signature — succession without the owner is just a claim');
|
|
156
|
+
}
|
|
157
|
+
const predSig = sigs.find((x) => x.did === b.predecessor);
|
|
158
|
+
cosigned = !!predSig && sigValid(b as unknown as Record<string, unknown>, predSig.did, predSig.sig);
|
|
159
|
+
}
|
|
160
|
+
return { ok: reasons.length === 0, reasons, cosigned };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export interface LineageCheck {
|
|
164
|
+
ok: boolean;
|
|
165
|
+
reasons: string[];
|
|
166
|
+
/** Oldest → newest DIDs, ending at the queried agent when ok. */
|
|
167
|
+
lineage: string[];
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Walk a chain of successions back from `did` (newest agent). Requires: every link
|
|
172
|
+
* verifies, links connect (predecessor of one = successor of the previous), and ALL
|
|
173
|
+
* links share one owner — a lineage is one authority's history. (An ownership transfer
|
|
174
|
+
* is a sale, not a succession; this deliberately refuses to conflate them.)
|
|
175
|
+
*/
|
|
176
|
+
export function verifyLineage(successions: Succession[], did: string): LineageCheck {
|
|
177
|
+
const reasons: string[] = [];
|
|
178
|
+
if (successions.length === 0) return { ok: true, reasons: [], lineage: [did] };
|
|
179
|
+
const bySuccessor = new Map<string, Succession>();
|
|
180
|
+
for (const s of successions) {
|
|
181
|
+
const check = verifySuccession(s);
|
|
182
|
+
if (!check.ok) return { ok: false, reasons: check.reasons.map((r) => `link ${s?.body?.successor ?? '?'}: ${r}`), lineage: [] };
|
|
183
|
+
if (bySuccessor.has(s.body.successor)) return { ok: false, reasons: [`two successions claim successor ${s.body.successor}`], lineage: [] };
|
|
184
|
+
bySuccessor.set(s.body.successor, s);
|
|
185
|
+
}
|
|
186
|
+
const owner = successions[0]!.body.owner;
|
|
187
|
+
if (!successions.every((s) => s.body.owner === owner)) {
|
|
188
|
+
return { ok: false, reasons: ['lineage spans multiple owners — an ownership transfer is not a succession'], lineage: [] };
|
|
189
|
+
}
|
|
190
|
+
const lineage: string[] = [did];
|
|
191
|
+
const seen = new Set([did]);
|
|
192
|
+
let cursor = did;
|
|
193
|
+
while (bySuccessor.has(cursor)) {
|
|
194
|
+
const link = bySuccessor.get(cursor)!;
|
|
195
|
+
const prev = link.body.predecessor;
|
|
196
|
+
if (seen.has(prev)) return { ok: false, reasons: ['succession cycle detected'], lineage: [] };
|
|
197
|
+
seen.add(prev);
|
|
198
|
+
lineage.unshift(prev);
|
|
199
|
+
cursor = prev;
|
|
200
|
+
}
|
|
201
|
+
if (lineage.length !== successions.length + 1) {
|
|
202
|
+
reasons.push(`${successions.length + 1 - lineage.length} succession(s) do not connect to ${did}'s lineage`);
|
|
203
|
+
}
|
|
204
|
+
return { ok: reasons.length === 0, reasons, lineage };
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export interface CombinedSummary {
|
|
208
|
+
/** Oldest → newest. */
|
|
209
|
+
lineage: string[];
|
|
210
|
+
/** Sum across the lineage — an UPPER BOUND (see caveats). */
|
|
211
|
+
receipts: number;
|
|
212
|
+
first_ts: string | null;
|
|
213
|
+
last_ts: string | null;
|
|
214
|
+
/** Current agent's status — a retired predecessor being revoked is normal. */
|
|
215
|
+
revoked: boolean;
|
|
216
|
+
fully_anchored: boolean;
|
|
217
|
+
/** Facts a reader must see before trusting the combined numbers. */
|
|
218
|
+
caveats: string[];
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Aggregate ledger reputation across a verified lineage. Deliberately conservative:
|
|
223
|
+
* receipt counts SUM (an upper bound — cross-key duplicates can't be detected from
|
|
224
|
+
* aggregates), counterparties are NOT summed (overlap is unknowable here), and any
|
|
225
|
+
* compromise link adds a caveat that the predecessor's post-compromise receipts are
|
|
226
|
+
* suspect. Pass `trusts` in the same order as `lineage`.
|
|
227
|
+
*/
|
|
228
|
+
export function combinedSummary(successions: Succession[], trusts: AgentTrust[], did: string): CombinedSummary {
|
|
229
|
+
const lin = verifyLineage(successions, did);
|
|
230
|
+
if (!lin.ok) throw new Error(`lineage does not verify: ${lin.reasons.join('; ')}`);
|
|
231
|
+
if (trusts.length !== lin.lineage.length) throw new Error(`need one AgentTrust per lineage member (${lin.lineage.length}), got ${trusts.length}`);
|
|
232
|
+
trusts.forEach((t, i) => {
|
|
233
|
+
if (t.agent_id !== lin.lineage[i]) throw new Error(`trusts[${i}] is for ${t.agent_id}, expected ${lin.lineage[i]}`);
|
|
234
|
+
});
|
|
235
|
+
const caveats: string[] = [
|
|
236
|
+
'receipt count is a sum across keys — an upper bound, not a deduplicated total',
|
|
237
|
+
'distinct counterparties are per-key and deliberately not summed (overlap unknowable)',
|
|
238
|
+
];
|
|
239
|
+
for (const s of successions) {
|
|
240
|
+
if (s.body.reason === 'compromise') {
|
|
241
|
+
caveats.push(`${s.body.predecessor} was COMPROMISED at ${s.body.compromised_at} — its receipts after that instant may be an attacker's`);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
const firsts = trusts.map((t) => t.first_ts).filter((x): x is string => !!x);
|
|
245
|
+
const lasts = trusts.map((t) => t.last_ts).filter((x): x is string => !!x);
|
|
246
|
+
const current = trusts[trusts.length - 1]!;
|
|
247
|
+
return {
|
|
248
|
+
lineage: lin.lineage,
|
|
249
|
+
receipts: trusts.reduce((n, t) => n + t.receipts, 0),
|
|
250
|
+
first_ts: firsts.length ? firsts.reduce((a, b) => (Date.parse(a) <= Date.parse(b) ? a : b)) : null,
|
|
251
|
+
last_ts: lasts.length ? lasts.reduce((a, b) => (Date.parse(a) >= Date.parse(b) ? a : b)) : null,
|
|
252
|
+
revoked: current.revoked,
|
|
253
|
+
fully_anchored: trusts.every((t) => t.fully_anchored),
|
|
254
|
+
caveats,
|
|
255
|
+
};
|
|
256
|
+
}
|