@attested-intelligence/aga-mcp-server 0.1.0 → 2.0.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/PATENTS.md +28 -0
- package/README.md +84 -23
- package/dist/context.d.ts +39 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +113 -0
- package/dist/context.js.map +1 -0
- package/dist/core/identity.d.ts +14 -0
- package/dist/core/identity.d.ts.map +1 -0
- package/dist/core/identity.js +16 -0
- package/dist/core/identity.js.map +1 -0
- package/dist/core/index.d.ts +3 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +3 -0
- package/dist/core/index.js.map +1 -1
- package/dist/core/measurement.d.ts +16 -0
- package/dist/core/measurement.d.ts.map +1 -0
- package/dist/core/measurement.js +18 -0
- package/dist/core/measurement.js.map +1 -0
- package/dist/core/portal.d.ts +1 -1
- package/dist/core/portal.d.ts.map +1 -1
- package/dist/core/portal.js +10 -5
- package/dist/core/portal.js.map +1 -1
- package/dist/core/types.d.ts +2 -2
- package/dist/core/types.d.ts.map +1 -1
- package/dist/crypto/canonicalize.d.ts +7 -0
- package/dist/crypto/canonicalize.d.ts.map +1 -0
- package/dist/crypto/canonicalize.js +21 -0
- package/dist/crypto/canonicalize.js.map +1 -0
- package/dist/crypto/index.d.ts +6 -5
- package/dist/crypto/index.d.ts.map +1 -1
- package/dist/crypto/index.js +6 -5
- package/dist/crypto/index.js.map +1 -1
- package/dist/crypto/keys.d.ts +10 -0
- package/dist/crypto/keys.d.ts.map +1 -0
- package/dist/crypto/keys.js +19 -0
- package/dist/crypto/keys.js.map +1 -0
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/middleware/governance.d.ts +1 -7
- package/dist/middleware/governance.d.ts.map +1 -1
- package/dist/middleware/governance.js +11 -18
- package/dist/middleware/governance.js.map +1 -1
- package/dist/prompts/drift-analysis.d.ts +13 -0
- package/dist/prompts/drift-analysis.d.ts.map +1 -0
- package/dist/prompts/drift-analysis.js +43 -0
- package/dist/prompts/drift-analysis.js.map +1 -0
- package/dist/prompts/governance-report.d.ts +7 -0
- package/dist/prompts/governance-report.d.ts.map +1 -0
- package/dist/prompts/governance-report.js +26 -0
- package/dist/prompts/governance-report.js.map +1 -0
- package/dist/prompts/nccoe-demo.d.ts +14 -0
- package/dist/prompts/nccoe-demo.d.ts.map +1 -0
- package/dist/prompts/nccoe-demo.js +48 -0
- package/dist/prompts/nccoe-demo.js.map +1 -0
- package/dist/resources/crypto-primitives.d.ts +3 -0
- package/dist/resources/crypto-primitives.d.ts.map +1 -0
- package/dist/resources/crypto-primitives.js +52 -0
- package/dist/resources/crypto-primitives.js.map +1 -0
- package/dist/resources/patent-claims.d.ts +3 -0
- package/dist/resources/patent-claims.d.ts.map +1 -0
- package/dist/resources/patent-claims.js +67 -0
- package/dist/resources/patent-claims.js.map +1 -0
- package/dist/resources/sample-bundle.d.ts +6 -0
- package/dist/resources/sample-bundle.d.ts.map +1 -0
- package/dist/resources/sample-bundle.js +58 -0
- package/dist/resources/sample-bundle.js.map +1 -0
- package/dist/resources/specification.d.ts +3 -0
- package/dist/resources/specification.d.ts.map +1 -0
- package/dist/resources/specification.js +107 -0
- package/dist/resources/specification.js.map +1 -0
- package/dist/server.d.ts +4 -7
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +217 -343
- package/dist/server.js.map +1 -1
- package/dist/storage/sqlite.js +1 -1
- package/dist/tools/create-artifact.d.ts +25 -0
- package/dist/tools/create-artifact.d.ts.map +1 -0
- package/dist/tools/create-artifact.js +85 -0
- package/dist/tools/create-artifact.js.map +1 -0
- package/dist/tools/delegate-subagent.d.ts +18 -0
- package/dist/tools/delegate-subagent.d.ts.map +1 -0
- package/dist/tools/delegate-subagent.js +50 -0
- package/dist/tools/delegate-subagent.js.map +1 -0
- package/dist/tools/disclose-claim.d.ts +14 -0
- package/dist/tools/disclose-claim.d.ts.map +1 -0
- package/dist/tools/disclose-claim.js +23 -0
- package/dist/tools/disclose-claim.js.map +1 -0
- package/dist/tools/export-bundle.d.ts +8 -0
- package/dist/tools/export-bundle.d.ts.map +1 -0
- package/dist/tools/export-bundle.js +25 -0
- package/dist/tools/export-bundle.js.map +1 -0
- package/dist/tools/full-lifecycle.d.ts +16 -0
- package/dist/tools/full-lifecycle.d.ts.map +1 -0
- package/dist/tools/full-lifecycle.js +121 -0
- package/dist/tools/full-lifecycle.js.map +1 -0
- package/dist/tools/generate-receipt.d.ts +16 -0
- package/dist/tools/generate-receipt.d.ts.map +1 -0
- package/dist/tools/generate-receipt.js +31 -0
- package/dist/tools/generate-receipt.js.map +1 -0
- package/dist/tools/get-chain.d.ts +14 -0
- package/dist/tools/get-chain.d.ts.map +1 -0
- package/dist/tools/get-chain.js +45 -0
- package/dist/tools/get-chain.js.map +1 -0
- package/dist/tools/get-portal-state.d.ts +8 -0
- package/dist/tools/get-portal-state.d.ts.map +1 -0
- package/dist/tools/get-portal-state.js +15 -0
- package/dist/tools/get-portal-state.js.map +1 -0
- package/dist/tools/init-chain.d.ts +10 -0
- package/dist/tools/init-chain.d.ts.map +1 -0
- package/dist/tools/init-chain.js +13 -0
- package/dist/tools/init-chain.js.map +1 -0
- package/dist/tools/measure-behavior.d.ts +12 -0
- package/dist/tools/measure-behavior.d.ts.map +1 -0
- package/dist/tools/measure-behavior.js +29 -0
- package/dist/tools/measure-behavior.js.map +1 -0
- package/dist/tools/measure-subject.d.ts +15 -0
- package/dist/tools/measure-subject.d.ts.map +1 -0
- package/dist/tools/measure-subject.js +106 -0
- package/dist/tools/measure-subject.js.map +1 -0
- package/dist/tools/quarantine-status.d.ts +8 -0
- package/dist/tools/quarantine-status.d.ts.map +1 -0
- package/dist/tools/quarantine-status.js +16 -0
- package/dist/tools/quarantine-status.js.map +1 -0
- package/dist/tools/revoke-artifact.d.ts +13 -0
- package/dist/tools/revoke-artifact.d.ts.map +1 -0
- package/dist/tools/revoke-artifact.js +24 -0
- package/dist/tools/revoke-artifact.js.map +1 -0
- package/dist/tools/rotate-keys.d.ts +13 -0
- package/dist/tools/rotate-keys.d.ts.map +1 -0
- package/dist/tools/rotate-keys.js +39 -0
- package/dist/tools/rotate-keys.js.map +1 -0
- package/dist/tools/server-info.d.ts +8 -0
- package/dist/tools/server-info.d.ts.map +1 -0
- package/dist/tools/server-info.js +24 -0
- package/dist/tools/server-info.js.map +1 -0
- package/dist/tools/set-verification-tier.d.ts +11 -0
- package/dist/tools/set-verification-tier.d.ts.map +1 -0
- package/dist/tools/set-verification-tier.js +31 -0
- package/dist/tools/set-verification-tier.js.map +1 -0
- package/dist/tools/start-monitoring.d.ts +12 -0
- package/dist/tools/start-monitoring.d.ts.map +1 -0
- package/dist/tools/start-monitoring.js +17 -0
- package/dist/tools/start-monitoring.js.map +1 -0
- package/dist/tools/trigger-measurement.d.ts +15 -0
- package/dist/tools/trigger-measurement.d.ts.map +1 -0
- package/dist/tools/trigger-measurement.js +86 -0
- package/dist/tools/trigger-measurement.js.map +1 -0
- package/dist/tools/verify-artifact.d.ts +13 -0
- package/dist/tools/verify-artifact.d.ts.map +1 -0
- package/dist/tools/verify-artifact.js +6 -0
- package/dist/tools/verify-artifact.js.map +1 -0
- package/dist/tools/verify-bundle.d.ts +13 -0
- package/dist/tools/verify-bundle.d.ts.map +1 -0
- package/dist/tools/verify-bundle.js +6 -0
- package/dist/tools/verify-bundle.js.map +1 -0
- package/dist/types.d.ts +262 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -0
- package/package.json +33 -6
- package/AGA_MCP_SERVER_SPEC.md +0 -632
- package/src/core/artifact.ts +0 -45
- package/src/core/attestation.ts +0 -33
- package/src/core/behavioral.ts +0 -132
- package/src/core/bundle.ts +0 -31
- package/src/core/chain.ts +0 -72
- package/src/core/checkpoint.ts +0 -22
- package/src/core/delegation.ts +0 -146
- package/src/core/disclosure.ts +0 -32
- package/src/core/index.ts +0 -11
- package/src/core/portal.ts +0 -96
- package/src/core/quarantine.ts +0 -16
- package/src/core/receipt.ts +0 -33
- package/src/core/subject.ts +0 -11
- package/src/core/types.ts +0 -244
- package/src/crypto/hash.ts +0 -33
- package/src/crypto/index.ts +0 -5
- package/src/crypto/merkle.ts +0 -43
- package/src/crypto/salt.ts +0 -18
- package/src/crypto/sign.ts +0 -35
- package/src/crypto/types.ts +0 -19
- package/src/index.ts +0 -12
- package/src/middleware/governance.ts +0 -95
- package/src/middleware/index.ts +0 -1
- package/src/server.ts +0 -436
- package/src/storage/index.ts +0 -3
- package/src/storage/interface.ts +0 -21
- package/src/storage/memory.ts +0 -27
- package/src/storage/sqlite.ts +0 -45
- package/src/tools/README.md +0 -13
- package/src/utils/canonical.ts +0 -14
- package/src/utils/constants.ts +0 -3
- package/src/utils/timestamp.ts +0 -12
- package/src/utils/uuid.ts +0 -2
package/src/core/attestation.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { saltedCommitment, generateSalt } from '../crypto/salt.js';
|
|
2
|
-
import { sha256HexCat } from '../crypto/hash.js';
|
|
3
|
-
import type { SubjectIdentifier, EvidenceCommitmentRecord } from './types.js';
|
|
4
|
-
import type { HashHex, SaltHex } from '../crypto/types.js';
|
|
5
|
-
|
|
6
|
-
export interface AttestationInput {
|
|
7
|
-
subject_identifier: SubjectIdentifier;
|
|
8
|
-
policy_reference: HashHex;
|
|
9
|
-
evidence_items: Array<{ label: string; content: string }>;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface AttestationResult {
|
|
13
|
-
success: boolean;
|
|
14
|
-
sealed_hash: HashHex | null;
|
|
15
|
-
seal_salt: SaltHex | null;
|
|
16
|
-
evidence_commitments: EvidenceCommitmentRecord[];
|
|
17
|
-
rejection_reason: string | null;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function performAttestation(input: AttestationInput): AttestationResult {
|
|
21
|
-
const evidence_commitments = input.evidence_items.map(item => {
|
|
22
|
-
const { commitment, salt } = saltedCommitment(item.content);
|
|
23
|
-
return { commitment, salt, label: item.label };
|
|
24
|
-
});
|
|
25
|
-
const seal_salt = generateSalt();
|
|
26
|
-
const sealed_hash = sha256HexCat(
|
|
27
|
-
input.subject_identifier.bytes_hash,
|
|
28
|
-
input.subject_identifier.metadata_hash,
|
|
29
|
-
input.policy_reference,
|
|
30
|
-
seal_salt
|
|
31
|
-
);
|
|
32
|
-
return { success: true, sealed_hash, seal_salt, evidence_commitments, rejection_reason: null };
|
|
33
|
-
}
|
package/src/core/behavioral.ts
DELETED
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Behavioral Drift Detection.
|
|
3
|
-
* NIST-2025-0035: "Governance mechanisms must measure behavioral outputs
|
|
4
|
-
* and decision patterns in addition to static artifacts."
|
|
5
|
-
*
|
|
6
|
-
* Tracks tool invocation patterns and compares against a behavioral
|
|
7
|
-
* baseline sealed in the policy artifact.
|
|
8
|
-
*/
|
|
9
|
-
import { sha256Str } from '../crypto/hash.js';
|
|
10
|
-
import type { HashHex } from '../crypto/types.js';
|
|
11
|
-
|
|
12
|
-
export interface ToolInvocation {
|
|
13
|
-
tool_name: string;
|
|
14
|
-
timestamp: string;
|
|
15
|
-
args_hash: HashHex; // hash of args, not args themselves (privacy)
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export interface BehavioralBaseline {
|
|
19
|
-
/** Allowed tools in this policy context */
|
|
20
|
-
permitted_tools: string[];
|
|
21
|
-
/** Maximum invocations per tool per measurement window */
|
|
22
|
-
rate_limits: Record<string, number>;
|
|
23
|
-
/** Forbidden tool sequences (e.g., read_secret then send_email) */
|
|
24
|
-
forbidden_sequences: string[][];
|
|
25
|
-
/** Measurement window in milliseconds */
|
|
26
|
-
window_ms: number;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export interface BehavioralMeasurement {
|
|
30
|
-
window_start: string;
|
|
31
|
-
window_end: string;
|
|
32
|
-
invocations: ToolInvocation[];
|
|
33
|
-
violations: BehavioralViolation[];
|
|
34
|
-
behavioral_hash: HashHex; // hash of the behavioral pattern
|
|
35
|
-
drift_detected: boolean;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export type BehavioralViolation =
|
|
39
|
-
| { type: 'UNAUTHORIZED_TOOL'; tool: string }
|
|
40
|
-
| { type: 'RATE_EXCEEDED'; tool: string; count: number; limit: number }
|
|
41
|
-
| { type: 'FORBIDDEN_SEQUENCE'; sequence: string[] };
|
|
42
|
-
|
|
43
|
-
export class BehavioralMonitor {
|
|
44
|
-
private invocations: ToolInvocation[] = [];
|
|
45
|
-
private baseline: BehavioralBaseline | null = null;
|
|
46
|
-
|
|
47
|
-
setBaseline(baseline: BehavioralBaseline): void {
|
|
48
|
-
this.baseline = baseline;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
recordInvocation(toolName: string, argsHash: HashHex): void {
|
|
52
|
-
this.invocations.push({
|
|
53
|
-
tool_name: toolName,
|
|
54
|
-
timestamp: new Date().toISOString(),
|
|
55
|
-
args_hash: argsHash,
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
measure(): BehavioralMeasurement {
|
|
60
|
-
if (!this.baseline) {
|
|
61
|
-
return {
|
|
62
|
-
window_start: '', window_end: '', invocations: [],
|
|
63
|
-
violations: [], behavioral_hash: sha256Str('no-baseline'),
|
|
64
|
-
drift_detected: false,
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const now = Date.now();
|
|
69
|
-
const windowStart = now - this.baseline.window_ms;
|
|
70
|
-
const windowInvocations = this.invocations.filter(
|
|
71
|
-
i => Date.parse(i.timestamp) >= windowStart
|
|
72
|
-
);
|
|
73
|
-
|
|
74
|
-
const violations: BehavioralViolation[] = [];
|
|
75
|
-
|
|
76
|
-
// Check unauthorized tools
|
|
77
|
-
for (const inv of windowInvocations) {
|
|
78
|
-
if (!this.baseline.permitted_tools.includes(inv.tool_name)) {
|
|
79
|
-
violations.push({ type: 'UNAUTHORIZED_TOOL', tool: inv.tool_name });
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// Check rate limits
|
|
84
|
-
const counts: Record<string, number> = {};
|
|
85
|
-
for (const inv of windowInvocations) {
|
|
86
|
-
counts[inv.tool_name] = (counts[inv.tool_name] ?? 0) + 1;
|
|
87
|
-
}
|
|
88
|
-
for (const [tool, count] of Object.entries(counts)) {
|
|
89
|
-
const limit = this.baseline.rate_limits[tool];
|
|
90
|
-
if (limit !== undefined && count > limit) {
|
|
91
|
-
violations.push({ type: 'RATE_EXCEEDED', tool, count, limit });
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// Check forbidden sequences
|
|
96
|
-
const toolSequence = windowInvocations.map(i => i.tool_name);
|
|
97
|
-
for (const forbidden of this.baseline.forbidden_sequences) {
|
|
98
|
-
if (containsSubsequence(toolSequence, forbidden)) {
|
|
99
|
-
violations.push({ type: 'FORBIDDEN_SEQUENCE', sequence: forbidden });
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// Compute behavioral hash (pattern fingerprint)
|
|
104
|
-
const pattern = windowInvocations.map(i => i.tool_name).join('|');
|
|
105
|
-
const behavioral_hash = sha256Str(pattern);
|
|
106
|
-
|
|
107
|
-
return {
|
|
108
|
-
window_start: new Date(windowStart).toISOString(),
|
|
109
|
-
window_end: new Date(now).toISOString(),
|
|
110
|
-
invocations: windowInvocations,
|
|
111
|
-
violations,
|
|
112
|
-
behavioral_hash,
|
|
113
|
-
drift_detected: violations.length > 0,
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
reset(): void {
|
|
118
|
-
this.invocations = [];
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
function containsSubsequence(haystack: string[], needle: string[]): boolean {
|
|
123
|
-
if (needle.length === 0) return true;
|
|
124
|
-
let ni = 0;
|
|
125
|
-
for (const h of haystack) {
|
|
126
|
-
if (h === needle[ni]) {
|
|
127
|
-
ni++;
|
|
128
|
-
if (ni === needle.length) return true;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
return false;
|
|
132
|
-
}
|
package/src/core/bundle.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { signStr, sigToB64, b64ToSig, hexToPk, verifyStr, pkToHex } from '../crypto/sign.js';
|
|
2
|
-
import { verifyProof } from '../crypto/merkle.js';
|
|
3
|
-
import { canonicalize } from '../utils/canonical.js';
|
|
4
|
-
import type { KeyPair, MerkleInclusionProof } from '../crypto/types.js';
|
|
5
|
-
import type { EvidenceBundle, PolicyArtifact, SignedReceipt, CheckpointReference } from './types.js';
|
|
6
|
-
|
|
7
|
-
export function generateBundle(artifact: PolicyArtifact, receipts: SignedReceipt[], proofs: MerkleInclusionProof[], checkpoint: CheckpointReference, kp: KeyPair): EvidenceBundle {
|
|
8
|
-
const unsigned = { artifact, receipts, merkle_proofs: proofs, checkpoint_reference: checkpoint, public_key: pkToHex(kp.publicKey) };
|
|
9
|
-
return { ...unsigned, bundle_signature: sigToB64(signStr(canonicalize(unsigned), kp.secretKey)) };
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface VerificationResult {
|
|
13
|
-
step1_artifact_sig: boolean; step2_receipt_sigs: boolean;
|
|
14
|
-
step3_merkle_proofs: boolean; step4_anchor: 'SKIPPED_OFFLINE' | boolean;
|
|
15
|
-
overall: boolean; errors: string[];
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export function verifyBundleOffline(bundle: EvidenceBundle, pinnedPkHex: string): VerificationResult {
|
|
19
|
-
const errors: string[] = [];
|
|
20
|
-
const { signature: aSig, ...aU } = bundle.artifact;
|
|
21
|
-
const s1 = verifyStr(b64ToSig(aSig), canonicalize(aU), hexToPk(pinnedPkHex));
|
|
22
|
-
if (!s1) errors.push('Artifact signature failed');
|
|
23
|
-
let s2 = true;
|
|
24
|
-
for (const r of bundle.receipts) {
|
|
25
|
-
const { portal_signature, ...rU } = r;
|
|
26
|
-
if (!verifyStr(b64ToSig(portal_signature), canonicalize(rU), hexToPk(bundle.public_key))) { s2 = false; errors.push(`Receipt ${r.receipt_id} sig failed`); }
|
|
27
|
-
}
|
|
28
|
-
let s3 = true;
|
|
29
|
-
for (const p of bundle.merkle_proofs) { if (!verifyProof(p)) { s3 = false; errors.push(`Merkle proof failed leaf ${p.leafIndex}`); } }
|
|
30
|
-
return { step1_artifact_sig: s1, step2_receipt_sigs: s2, step3_merkle_proofs: s3, step4_anchor: 'SKIPPED_OFFLINE', overall: s1 && s2 && s3, errors };
|
|
31
|
-
}
|
package/src/core/chain.ts
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { sha256Str } from '../crypto/hash.js';
|
|
2
|
-
import { signStr, sigToB64, pkToHex } from '../crypto/sign.js';
|
|
3
|
-
import { canonicalize } from '../utils/canonical.js';
|
|
4
|
-
import { utcNow } from '../utils/timestamp.js';
|
|
5
|
-
import { uuid } from '../utils/uuid.js';
|
|
6
|
-
import { SCHEMA_VERSION, PROTOCOL_VERSION, TAXONOMY_VERSION } from '../utils/constants.js';
|
|
7
|
-
import type { KeyPair, HashHex } from '../crypto/types.js';
|
|
8
|
-
import type { ContinuityEvent, GenesisPayload, StructuralMetadata, EventType } from './types.js';
|
|
9
|
-
|
|
10
|
-
/** Leaf hash from structural metadata ONLY. Payload EXCLUDED. Uses "||" delimiter. */
|
|
11
|
-
export function computeLeafHash(m: StructuralMetadata): HashHex {
|
|
12
|
-
return sha256Str([
|
|
13
|
-
m.schema_version, m.protocol_version, m.event_type, m.event_id,
|
|
14
|
-
String(m.sequence_number), m.timestamp, m.previous_leaf_hash ?? 'NULL'
|
|
15
|
-
].join('||'));
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export function computePayloadHash(payload: unknown): HashHex {
|
|
19
|
-
return sha256Str(canonicalize(payload));
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function buildEvent(type: EventType, payload: unknown, seq: number, prevLeaf: HashHex | null, kp: KeyPair): ContinuityEvent {
|
|
23
|
-
const id = uuid(), ts = utcNow();
|
|
24
|
-
const meta: StructuralMetadata = {
|
|
25
|
-
schema_version: SCHEMA_VERSION, protocol_version: PROTOCOL_VERSION,
|
|
26
|
-
event_type: type, event_id: id, sequence_number: seq,
|
|
27
|
-
timestamp: ts, previous_leaf_hash: prevLeaf,
|
|
28
|
-
};
|
|
29
|
-
const leafHash = computeLeafHash(meta);
|
|
30
|
-
const payloadHash = computePayloadHash(payload);
|
|
31
|
-
const sig = signStr(canonicalize({ ...meta, leaf_hash: leafHash, payload, payload_hash: payloadHash }), kp.secretKey);
|
|
32
|
-
return {
|
|
33
|
-
schema_version: SCHEMA_VERSION, protocol_version: PROTOCOL_VERSION,
|
|
34
|
-
event_type: type, event_id: id, sequence_number: seq,
|
|
35
|
-
timestamp: ts, previous_leaf_hash: prevLeaf,
|
|
36
|
-
leaf_hash: leafHash, payload, payload_hash: payloadHash,
|
|
37
|
-
event_signature: sigToB64(sig),
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export function createGenesisEvent(kp: KeyPair, specHash: HashHex): ContinuityEvent {
|
|
42
|
-
const payload: GenesisPayload = {
|
|
43
|
-
protocol_version: PROTOCOL_VERSION, taxonomy_version: TAXONOMY_VERSION,
|
|
44
|
-
root_fingerprint: pkToHex(kp.publicKey), specification_hash: specHash, marker: 'GENESIS',
|
|
45
|
-
};
|
|
46
|
-
return buildEvent('GENESIS', payload, 0, null, kp);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export function appendEvent(type: EventType, payload: unknown, prev: ContinuityEvent, kp: KeyPair): ContinuityEvent {
|
|
50
|
-
return buildEvent(type, payload, prev.sequence_number + 1, prev.leaf_hash, kp);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export function verifyChainIntegrity(events: ContinuityEvent[]): {
|
|
54
|
-
valid: boolean; brokenAt: number | null; error: string | null;
|
|
55
|
-
} {
|
|
56
|
-
for (let i = 0; i < events.length; i++) {
|
|
57
|
-
const e = events[i];
|
|
58
|
-
const meta: StructuralMetadata = {
|
|
59
|
-
schema_version: e.schema_version, protocol_version: e.protocol_version,
|
|
60
|
-
event_type: e.event_type, event_id: e.event_id,
|
|
61
|
-
sequence_number: e.sequence_number, timestamp: e.timestamp,
|
|
62
|
-
previous_leaf_hash: e.previous_leaf_hash,
|
|
63
|
-
};
|
|
64
|
-
if (e.leaf_hash !== computeLeafHash(meta))
|
|
65
|
-
return { valid: false, brokenAt: e.sequence_number, error: `Leaf hash mismatch at seq ${e.sequence_number}` };
|
|
66
|
-
if (i > 0 && e.previous_leaf_hash !== events[i - 1].leaf_hash)
|
|
67
|
-
return { valid: false, brokenAt: e.sequence_number, error: `Chain linkage broken at seq ${e.sequence_number}` };
|
|
68
|
-
if (e.payload_hash !== computePayloadHash(e.payload))
|
|
69
|
-
return { valid: false, brokenAt: e.sequence_number, error: `Payload hash mismatch at seq ${e.sequence_number}` };
|
|
70
|
-
}
|
|
71
|
-
return { valid: true, brokenAt: null, error: null };
|
|
72
|
-
}
|
package/src/core/checkpoint.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { buildMerkleTree, inclusionProof } from '../crypto/merkle.js';
|
|
2
|
-
import { utcNow } from '../utils/timestamp.js';
|
|
3
|
-
import { uuid } from '../utils/uuid.js';
|
|
4
|
-
import type { ContinuityEvent, CheckpointReference, AnchorBatchPayload } from './types.js';
|
|
5
|
-
import type { MerkleInclusionProof } from '../crypto/types.js';
|
|
6
|
-
|
|
7
|
-
export function createCheckpoint(events: ContinuityEvent[], anchorNetwork = 'local'): { checkpoint: CheckpointReference; payload: AnchorBatchPayload } {
|
|
8
|
-
if (!events.length) throw new Error('No events to checkpoint');
|
|
9
|
-
const { root } = buildMerkleTree(events.map(e => e.leaf_hash));
|
|
10
|
-
const checkpoint: CheckpointReference = {
|
|
11
|
-
merkle_root: root, batch_start_sequence: events[0].sequence_number,
|
|
12
|
-
batch_end_sequence: events[events.length - 1].sequence_number,
|
|
13
|
-
anchor_network: anchorNetwork, transaction_id: `${anchorNetwork}:${uuid()}`, timestamp: utcNow(),
|
|
14
|
-
};
|
|
15
|
-
return { checkpoint, payload: { checkpoint_reference: checkpoint, leaf_count: events.length } };
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export function eventInclusionProof(events: ContinuityEvent[], targetSeq: number): MerkleInclusionProof {
|
|
19
|
-
const idx = events.findIndex(e => e.sequence_number === targetSeq);
|
|
20
|
-
if (idx === -1) throw new Error(`Sequence ${targetSeq} not in batch`);
|
|
21
|
-
return inclusionProof(events.map(e => e.leaf_hash), idx);
|
|
22
|
-
}
|
package/src/core/delegation.ts
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Constrained Sub-Agent Delegation.
|
|
3
|
-
* NCCoE filing: "Scope can only diminish through delegation, never expand."
|
|
4
|
-
*
|
|
5
|
-
* Primary agent's portal issues a derived artifact to secondary agent:
|
|
6
|
-
* - TTL <= parent's remaining TTL
|
|
7
|
-
* - Scope can only diminish, never expand
|
|
8
|
-
* - Secondary's genesis links to parent's chain
|
|
9
|
-
*/
|
|
10
|
-
import { generateArtifact, hashArtifact } from './artifact.js';
|
|
11
|
-
import { isExpired } from '../utils/timestamp.js';
|
|
12
|
-
import type { PolicyArtifact, EnforcementAction, MeasurementType } from './types.js';
|
|
13
|
-
import type { KeyPair } from '../crypto/types.js';
|
|
14
|
-
|
|
15
|
-
export interface DelegationRequest {
|
|
16
|
-
/** Subset of parent's enforcement triggers */
|
|
17
|
-
enforcement_triggers: EnforcementAction[];
|
|
18
|
-
/** Subset of parent's measurement types */
|
|
19
|
-
measurement_types: MeasurementType[];
|
|
20
|
-
/** Requested TTL in seconds (will be clamped to parent remaining) */
|
|
21
|
-
requested_ttl_seconds: number;
|
|
22
|
-
/** Description of the delegation purpose */
|
|
23
|
-
delegation_purpose: string;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export interface DelegationResult {
|
|
27
|
-
success: boolean;
|
|
28
|
-
child_artifact?: PolicyArtifact;
|
|
29
|
-
child_artifact_hash?: string;
|
|
30
|
-
parent_artifact_hash: string;
|
|
31
|
-
effective_ttl_seconds?: number;
|
|
32
|
-
scope_reduction?: {
|
|
33
|
-
triggers_removed: string[];
|
|
34
|
-
measurement_types_removed: string[];
|
|
35
|
-
};
|
|
36
|
-
error?: string;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Derive a constrained artifact from a parent artifact.
|
|
41
|
-
* Key rule: scope can only diminish, never expand.
|
|
42
|
-
*/
|
|
43
|
-
export function deriveArtifact(
|
|
44
|
-
parentArtifact: PolicyArtifact,
|
|
45
|
-
request: DelegationRequest,
|
|
46
|
-
issuerKP: KeyPair
|
|
47
|
-
): DelegationResult {
|
|
48
|
-
const parentHash = hashArtifact(parentArtifact);
|
|
49
|
-
|
|
50
|
-
// Validate parent is not expired
|
|
51
|
-
if (isExpired(parentArtifact.issued_timestamp, parentArtifact.enforcement_parameters.ttl_seconds)) {
|
|
52
|
-
return { success: false, parent_artifact_hash: parentHash, error: 'Parent artifact TTL has expired' };
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// Calculate parent remaining TTL
|
|
56
|
-
const parentIssuedMs = Date.parse(parentArtifact.issued_timestamp);
|
|
57
|
-
const parentExpiresMs = parentIssuedMs + (parentArtifact.enforcement_parameters.ttl_seconds * 1000);
|
|
58
|
-
const remainingMs = parentExpiresMs - Date.now();
|
|
59
|
-
const remainingSeconds = Math.max(0, Math.floor(remainingMs / 1000));
|
|
60
|
-
|
|
61
|
-
// Clamp child TTL to parent remaining
|
|
62
|
-
const effectiveTTL = Math.min(request.requested_ttl_seconds, remainingSeconds);
|
|
63
|
-
if (effectiveTTL <= 0) {
|
|
64
|
-
return { success: false, parent_artifact_hash: parentHash, error: 'No remaining TTL to delegate' };
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// Validate triggers are subset of parent
|
|
68
|
-
const parentTriggers = new Set(parentArtifact.enforcement_parameters.enforcement_triggers);
|
|
69
|
-
const invalidTriggers = request.enforcement_triggers.filter(t => !parentTriggers.has(t));
|
|
70
|
-
if (invalidTriggers.length > 0) {
|
|
71
|
-
return { success: false, parent_artifact_hash: parentHash, error: `Cannot expand scope: triggers [${invalidTriggers.join(', ')}] not in parent` };
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// Validate measurement types are subset of parent
|
|
75
|
-
const parentTypes = new Set<string>(parentArtifact.enforcement_parameters.measurement_types);
|
|
76
|
-
const invalidTypes = request.measurement_types.filter(t => !parentTypes.has(t));
|
|
77
|
-
if (invalidTypes.length > 0) {
|
|
78
|
-
return { success: false, parent_artifact_hash: parentHash, error: `Cannot expand scope: measurement types [${invalidTypes.join(', ')}] not in parent` };
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// Build constrained child artifact
|
|
82
|
-
const childArtifact = generateArtifact({
|
|
83
|
-
subject_identifier: parentArtifact.subject_identifier,
|
|
84
|
-
policy_reference: parentArtifact.policy_reference,
|
|
85
|
-
policy_version: parentArtifact.policy_version,
|
|
86
|
-
sealed_hash: parentArtifact.sealed_hash,
|
|
87
|
-
seal_salt: parentArtifact.seal_salt,
|
|
88
|
-
enforcement_parameters: {
|
|
89
|
-
measurement_cadence_ms: parentArtifact.enforcement_parameters.measurement_cadence_ms,
|
|
90
|
-
ttl_seconds: effectiveTTL,
|
|
91
|
-
enforcement_triggers: request.enforcement_triggers,
|
|
92
|
-
re_attestation_required: parentArtifact.enforcement_parameters.re_attestation_required,
|
|
93
|
-
measurement_types: request.measurement_types,
|
|
94
|
-
},
|
|
95
|
-
disclosure_policy: parentArtifact.disclosure_policy, // cannot expand
|
|
96
|
-
evidence_commitments: parentArtifact.evidence_commitments,
|
|
97
|
-
issuer_keypair: issuerKP,
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
// Track scope reduction
|
|
101
|
-
const triggersRemoved = [...parentTriggers].filter(t => !request.enforcement_triggers.includes(t as EnforcementAction));
|
|
102
|
-
const typesRemoved = [...parentTypes].filter(t => !request.measurement_types.includes(t as MeasurementType));
|
|
103
|
-
|
|
104
|
-
return {
|
|
105
|
-
success: true,
|
|
106
|
-
child_artifact: childArtifact,
|
|
107
|
-
child_artifact_hash: hashArtifact(childArtifact),
|
|
108
|
-
parent_artifact_hash: parentHash,
|
|
109
|
-
effective_ttl_seconds: effectiveTTL,
|
|
110
|
-
scope_reduction: {
|
|
111
|
-
triggers_removed: triggersRemoved,
|
|
112
|
-
measurement_types_removed: typesRemoved,
|
|
113
|
-
},
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Validate that a child artifact is a valid delegation of a parent.
|
|
119
|
-
*/
|
|
120
|
-
export function validateDelegation(parent: PolicyArtifact, child: PolicyArtifact): { valid: boolean; errors: string[] } {
|
|
121
|
-
const errors: string[] = [];
|
|
122
|
-
|
|
123
|
-
// TTL must be <= parent TTL
|
|
124
|
-
if (child.enforcement_parameters.ttl_seconds > parent.enforcement_parameters.ttl_seconds) {
|
|
125
|
-
errors.push(`Child TTL (${child.enforcement_parameters.ttl_seconds}s) exceeds parent (${parent.enforcement_parameters.ttl_seconds}s)`);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
// Triggers must be subset
|
|
129
|
-
const pTriggers = new Set<string>(parent.enforcement_parameters.enforcement_triggers);
|
|
130
|
-
for (const t of child.enforcement_parameters.enforcement_triggers) {
|
|
131
|
-
if (!pTriggers.has(t)) errors.push(`Child trigger '${t}' not in parent scope`);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// Measurement types must be subset
|
|
135
|
-
const pTypes = new Set<string>(parent.enforcement_parameters.measurement_types);
|
|
136
|
-
for (const t of child.enforcement_parameters.measurement_types) {
|
|
137
|
-
if (!pTypes.has(t)) errors.push(`Child measurement type '${t}' not in parent scope`);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// Subject must match
|
|
141
|
-
if (child.subject_identifier.bytes_hash !== parent.subject_identifier.bytes_hash) {
|
|
142
|
-
errors.push('Child subject bytes_hash does not match parent');
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
return { valid: errors.length === 0, errors };
|
|
146
|
-
}
|
package/src/core/disclosure.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { signStr, sigToB64 } from '../crypto/sign.js';
|
|
2
|
-
import { canonicalize } from '../utils/canonical.js';
|
|
3
|
-
import { utcNow } from '../utils/timestamp.js';
|
|
4
|
-
import { uuid } from '../utils/uuid.js';
|
|
5
|
-
import type { KeyPair } from '../crypto/types.js';
|
|
6
|
-
import type { DisclosureRequest, DisclosurePolicy, SubstitutionReceipt, DisclosureMode } from './types.js';
|
|
7
|
-
|
|
8
|
-
export interface DisclosureResult {
|
|
9
|
-
permitted: boolean; disclosed_claim_id: string | null; disclosed_value: unknown;
|
|
10
|
-
mode: DisclosureMode; was_substituted: boolean; substitution_receipt: SubstitutionReceipt | null;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function processDisclosure(req: DisclosureRequest, policy: DisclosurePolicy, values: Record<string, unknown>, policyVersion: number, chainSeq: number, kp: KeyPair): DisclosureResult {
|
|
14
|
-
const claim = policy.claims_taxonomy.find(c => c.claim_id === req.requested_claim_id);
|
|
15
|
-
if (!claim) return { permitted: false, disclosed_claim_id: null, disclosed_value: null, mode: req.mode, was_substituted: false, substitution_receipt: null };
|
|
16
|
-
if (claim.permitted_modes.includes(req.mode))
|
|
17
|
-
return { permitted: true, disclosed_claim_id: claim.claim_id, disclosed_value: fv(values[claim.claim_id], req.mode), mode: req.mode, was_substituted: false, substitution_receipt: null };
|
|
18
|
-
for (const subId of claim.substitutes) {
|
|
19
|
-
const sub = policy.claims_taxonomy.find(c => c.claim_id === subId);
|
|
20
|
-
if (sub?.permitted_modes.includes(req.mode) && !sub.inference_risks.includes(req.requested_claim_id))
|
|
21
|
-
return { permitted: true, disclosed_claim_id: subId, disclosed_value: fv(values[subId], req.mode), mode: req.mode, was_substituted: true,
|
|
22
|
-
substitution_receipt: sr(req.requested_claim_id, subId, policyVersion, 'SENSITIVITY_DENIED', chainSeq, kp) };
|
|
23
|
-
}
|
|
24
|
-
return { permitted: false, disclosed_claim_id: null, disclosed_value: null, mode: req.mode, was_substituted: false,
|
|
25
|
-
substitution_receipt: sr(req.requested_claim_id, null, policyVersion, 'NO_PERMITTED_SUBSTITUTE', chainSeq, kp) };
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function fv(v: unknown, m: DisclosureMode): unknown { return m === 'PROOF_ONLY' ? v != null : v; }
|
|
29
|
-
function sr(orig: string, sub: string | null, pv: number, reason: string, seq: number, kp: KeyPair): SubstitutionReceipt {
|
|
30
|
-
const u = { receipt_id: uuid(), original_claim_id: orig, substitute_claim_id: sub, policy_version: pv, reason_code: reason, timestamp: utcNow(), chain_sequence_ref: seq };
|
|
31
|
-
return { ...u, signature: sigToB64(signStr(canonicalize(u), kp.secretKey)) };
|
|
32
|
-
}
|
package/src/core/index.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export * from './types.js';
|
|
2
|
-
export * from './subject.js';
|
|
3
|
-
export * from './attestation.js';
|
|
4
|
-
export * from './artifact.js';
|
|
5
|
-
export * from './receipt.js';
|
|
6
|
-
export * from './chain.js';
|
|
7
|
-
export * from './portal.js';
|
|
8
|
-
export * from './quarantine.js';
|
|
9
|
-
export * from './checkpoint.js';
|
|
10
|
-
export * from './bundle.js';
|
|
11
|
-
export * from './disclosure.js';
|
package/src/core/portal.ts
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Portal (Sentinel) — Runtime Enforcement Boundary. Ref 150, 270-280.
|
|
3
|
-
* V3: TTL + revocation checked every measurement. Fail-closed semantics.
|
|
4
|
-
* Aligned with NCCoE filing Sections 3-4 and NIST-2025-0035.
|
|
5
|
-
*/
|
|
6
|
-
import { sha256Bytes, sha256Str } from '../crypto/hash.js';
|
|
7
|
-
import { b64ToSig, hexToPk, verifyStr } from '../crypto/sign.js';
|
|
8
|
-
import { canonicalize } from '../utils/canonical.js';
|
|
9
|
-
import { isWithinPeriod, isExpired, utcNow } from '../utils/timestamp.js';
|
|
10
|
-
import type { PolicyArtifact, PortalState, EnforcementAction, SubjectMetadata } from './types.js';
|
|
11
|
-
import type { HashHex } from '../crypto/types.js';
|
|
12
|
-
|
|
13
|
-
export interface MeasurementResult {
|
|
14
|
-
match: boolean;
|
|
15
|
-
currentBytesHash: HashHex;
|
|
16
|
-
currentMetaHash: HashHex;
|
|
17
|
-
expectedBytesHash: HashHex;
|
|
18
|
-
expectedMetaHash: HashHex;
|
|
19
|
-
ttl_ok: boolean;
|
|
20
|
-
revoked: boolean;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export class Portal {
|
|
24
|
-
state: PortalState = 'INITIALIZATION';
|
|
25
|
-
artifact: PolicyArtifact | null = null;
|
|
26
|
-
sequenceCounter = 0;
|
|
27
|
-
lastLeafHash: HashHex | null = null;
|
|
28
|
-
revocations: Set<string> = new Set();
|
|
29
|
-
|
|
30
|
-
loadArtifact(artifact: PolicyArtifact, pinnedPkHex: string): { ok: boolean; error?: string } {
|
|
31
|
-
this.state = 'ARTIFACT_VERIFICATION';
|
|
32
|
-
const { signature, ...unsigned } = artifact;
|
|
33
|
-
if (!verifyStr(b64ToSig(signature), canonicalize(unsigned), hexToPk(pinnedPkHex))) {
|
|
34
|
-
this.state = 'TERMINATED'; return { ok: false, error: 'Signature verification failed' };
|
|
35
|
-
}
|
|
36
|
-
if (!isWithinPeriod(utcNow(), artifact.effective_timestamp, artifact.expiration_timestamp)) {
|
|
37
|
-
this.state = 'TERMINATED'; return { ok: false, error: 'Artifact outside effective period' };
|
|
38
|
-
}
|
|
39
|
-
if (this.revocations.has(artifact.sealed_hash)) {
|
|
40
|
-
this.state = 'TERMINATED'; return { ok: false, error: 'Artifact has been revoked' };
|
|
41
|
-
}
|
|
42
|
-
this.artifact = artifact;
|
|
43
|
-
this.state = 'ACTIVE_MONITORING';
|
|
44
|
-
return { ok: true };
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
measure(subjectBytes: Uint8Array, meta: SubjectMetadata): MeasurementResult {
|
|
48
|
-
if (!this.artifact) throw new Error('No artifact loaded');
|
|
49
|
-
if (this.state === 'TERMINATED') throw new Error('Portal is terminated');
|
|
50
|
-
const empty = { currentBytesHash: '', currentMetaHash: '',
|
|
51
|
-
expectedBytesHash: this.artifact.subject_identifier.bytes_hash,
|
|
52
|
-
expectedMetaHash: this.artifact.subject_identifier.metadata_hash };
|
|
53
|
-
|
|
54
|
-
// Fail-closed: TTL check
|
|
55
|
-
const ttl_ok = !isExpired(this.artifact.issued_timestamp, this.artifact.enforcement_parameters.ttl_seconds);
|
|
56
|
-
if (!ttl_ok) { this.state = 'TERMINATED'; return { match: false, ttl_ok: false, revoked: false, ...empty }; }
|
|
57
|
-
|
|
58
|
-
// Fail-closed: revocation check
|
|
59
|
-
if (this.revocations.has(this.artifact.sealed_hash)) {
|
|
60
|
-
this.state = 'TERMINATED'; return { match: false, ttl_ok: true, revoked: true, ...empty };
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const currentBytesHash = sha256Bytes(subjectBytes);
|
|
64
|
-
const currentMetaHash = sha256Str(canonicalize(meta));
|
|
65
|
-
const match = currentBytesHash === this.artifact.subject_identifier.bytes_hash &&
|
|
66
|
-
currentMetaHash === this.artifact.subject_identifier.metadata_hash;
|
|
67
|
-
|
|
68
|
-
if (!match && this.state === 'ACTIVE_MONITORING') this.state = 'DRIFT_DETECTED';
|
|
69
|
-
return { match, currentBytesHash, currentMetaHash,
|
|
70
|
-
expectedBytesHash: this.artifact.subject_identifier.bytes_hash,
|
|
71
|
-
expectedMetaHash: this.artifact.subject_identifier.metadata_hash,
|
|
72
|
-
ttl_ok: true, revoked: false };
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
enforce(action: EnforcementAction): void {
|
|
76
|
-
if (this.state !== 'DRIFT_DETECTED') throw new Error(`Cannot enforce in state ${this.state}`);
|
|
77
|
-
switch (action) {
|
|
78
|
-
case 'TERMINATE': case 'SAFE_STATE': this.state = 'TERMINATED'; break;
|
|
79
|
-
case 'QUARANTINE': this.state = 'PHANTOM_QUARANTINE'; break;
|
|
80
|
-
case 'ALERT_ONLY': this.state = 'ACTIVE_MONITORING'; break;
|
|
81
|
-
default: break;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
revoke(sealedHash: string): void {
|
|
86
|
-
this.revocations.add(sealedHash);
|
|
87
|
-
if (this.artifact?.sealed_hash === sealedHash) this.state = 'TERMINATED';
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
isRevoked(sealedHash: string): boolean { return this.revocations.has(sealedHash); }
|
|
91
|
-
|
|
92
|
-
reset(): void {
|
|
93
|
-
this.state = 'INITIALIZATION'; this.artifact = null;
|
|
94
|
-
this.sequenceCounter = 0; this.lastLeafHash = null;
|
|
95
|
-
}
|
|
96
|
-
}
|
package/src/core/quarantine.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { utcNow } from '../utils/timestamp.js';
|
|
2
|
-
import type { QuarantineState } from './types.js';
|
|
3
|
-
|
|
4
|
-
export function initQuarantine(): QuarantineState {
|
|
5
|
-
return { active: true, started_at: utcNow(), inputs_captured: 0, outputs_severed: true, forensic_buffer: [] };
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export function captureInput(q: QuarantineState, inputType: string, data: unknown): void {
|
|
9
|
-
q.forensic_buffer.push({ timestamp: utcNow(), type: inputType, data });
|
|
10
|
-
q.inputs_captured++;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function releaseQuarantine(q: QuarantineState): { duration_ms: number; total_captures: number } {
|
|
14
|
-
q.active = false;
|
|
15
|
-
return { duration_ms: q.started_at ? Date.now() - Date.parse(q.started_at) : 0, total_captures: q.inputs_captured };
|
|
16
|
-
}
|
package/src/core/receipt.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
/** V3: measurement_type field. Receipts generated for EVERY measurement. */
|
|
2
|
-
import { signStr, sigToB64 } from '../crypto/sign.js';
|
|
3
|
-
import { canonicalize } from '../utils/canonical.js';
|
|
4
|
-
import { utcNow } from '../utils/timestamp.js';
|
|
5
|
-
import { uuid } from '../utils/uuid.js';
|
|
6
|
-
import type { KeyPair, HashHex } from '../crypto/types.js';
|
|
7
|
-
import type { SignedReceipt, SubjectIdentifier, EnforcementAction } from './types.js';
|
|
8
|
-
|
|
9
|
-
export interface ReceiptInput {
|
|
10
|
-
subjectId: SubjectIdentifier;
|
|
11
|
-
artifactRef: HashHex;
|
|
12
|
-
currentHash: string;
|
|
13
|
-
sealedHash: string;
|
|
14
|
-
driftDetected: boolean;
|
|
15
|
-
driftDescription: string | null;
|
|
16
|
-
action: EnforcementAction | null;
|
|
17
|
-
measurementType: string;
|
|
18
|
-
seq: number;
|
|
19
|
-
prevLeaf: HashHex | null;
|
|
20
|
-
portalKP: KeyPair;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function generateReceipt(input: ReceiptInput): SignedReceipt {
|
|
24
|
-
const unsigned = {
|
|
25
|
-
receipt_id: uuid(), subject_identifier: input.subjectId,
|
|
26
|
-
artifact_reference: input.artifactRef, current_hash: input.currentHash,
|
|
27
|
-
sealed_hash: input.sealedHash, drift_detected: input.driftDetected,
|
|
28
|
-
drift_description: input.driftDescription, enforcement_action: input.action,
|
|
29
|
-
measurement_type: input.measurementType, timestamp: utcNow(),
|
|
30
|
-
sequence_number: input.seq, previous_leaf_hash: input.prevLeaf,
|
|
31
|
-
};
|
|
32
|
-
return { ...unsigned, portal_signature: sigToB64(signStr(canonicalize(unsigned), input.portalKP.secretKey)) };
|
|
33
|
-
}
|
package/src/core/subject.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { sha256Bytes, sha256Str } from '../crypto/hash.js';
|
|
2
|
-
import { canonicalize } from '../utils/canonical.js';
|
|
3
|
-
import type { SubjectIdentifier, SubjectMetadata } from './types.js';
|
|
4
|
-
|
|
5
|
-
export function computeSubjectId(bytes: Uint8Array, meta: SubjectMetadata): SubjectIdentifier {
|
|
6
|
-
return { bytes_hash: sha256Bytes(bytes), metadata_hash: sha256Str(canonicalize(meta)) };
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export function computeSubjectIdFromString(content: string, meta: SubjectMetadata): SubjectIdentifier {
|
|
10
|
-
return computeSubjectId(new TextEncoder().encode(content), meta);
|
|
11
|
-
}
|