@emilia-protocol/gate 0.10.0 → 0.13.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/CHANGELOG.md +91 -0
- package/README.md +272 -0
- package/action-control-manifest.js +3 -268
- package/action-escrow-custodian.js +4 -0
- package/action-escrow-evidence.js +4 -0
- package/action-escrow-package.js +4 -0
- package/action-escrow-postgres.js +4 -0
- package/action-escrow-state.js +4 -0
- package/action-escrow-verifiers.js +4 -0
- package/action-escrow.js +4 -0
- package/action-packs.js +3 -140
- package/adapters/_kit.js +3 -104
- package/adapters/aws.js +3 -108
- package/adapters/cloudflare.js +3 -54
- package/adapters/gcp.js +3 -70
- package/adapters/github.js +3 -109
- package/adapters/jira.js +3 -55
- package/adapters/k8s.js +3 -70
- package/adapters/linear.js +3 -55
- package/adapters/salesforce.js +3 -55
- package/adapters/stripe.js +3 -81
- package/adapters/supabase.js +3 -121
- package/adapters/terraform.js +3 -59
- package/adapters/vercel.js +3 -84
- package/aec-execution.js +3 -312
- package/breakglass.js +3 -582
- package/capability-receipt.js +4 -0
- package/cf1-conformance.js +3 -106
- package/challenge-store.js +3 -69
- package/control-plane.js +3 -340
- package/coverage.js +3 -721
- package/demo.mjs +1 -1
- package/deployment-attestation.js +3 -247
- package/dist/action-control-manifest.d.ts +82 -0
- package/dist/action-control-manifest.d.ts.map +1 -0
- package/dist/action-control-manifest.js +422 -0
- package/dist/action-control-manifest.js.map +1 -0
- package/dist/action-escrow-custodian.d.ts +74 -0
- package/dist/action-escrow-custodian.d.ts.map +1 -0
- package/dist/action-escrow-custodian.js +363 -0
- package/dist/action-escrow-custodian.js.map +1 -0
- package/dist/action-escrow-evidence.d.ts +130 -0
- package/dist/action-escrow-evidence.d.ts.map +1 -0
- package/dist/action-escrow-evidence.js +1012 -0
- package/dist/action-escrow-evidence.js.map +1 -0
- package/dist/action-escrow-package.d.ts +10 -0
- package/dist/action-escrow-package.d.ts.map +1 -0
- package/dist/action-escrow-package.js +1583 -0
- package/dist/action-escrow-package.js.map +1 -0
- package/dist/action-escrow-postgres.d.ts +62 -0
- package/dist/action-escrow-postgres.d.ts.map +1 -0
- package/dist/action-escrow-postgres.js +339 -0
- package/dist/action-escrow-postgres.js.map +1 -0
- package/dist/action-escrow-state.d.ts +141 -0
- package/dist/action-escrow-state.d.ts.map +1 -0
- package/dist/action-escrow-state.js +413 -0
- package/dist/action-escrow-state.js.map +1 -0
- package/dist/action-escrow-verifiers.d.ts +65 -0
- package/dist/action-escrow-verifiers.d.ts.map +1 -0
- package/dist/action-escrow-verifiers.js +421 -0
- package/dist/action-escrow-verifiers.js.map +1 -0
- package/dist/action-escrow.d.ts +112 -0
- package/dist/action-escrow.d.ts.map +1 -0
- package/dist/action-escrow.js +2661 -0
- package/dist/action-escrow.js.map +1 -0
- package/dist/action-packs.d.ts +54 -0
- package/dist/action-packs.d.ts.map +1 -0
- package/dist/action-packs.js +143 -0
- package/dist/action-packs.js.map +1 -0
- package/dist/adapters/_kit.d.ts +50 -0
- package/dist/adapters/_kit.d.ts.map +1 -0
- package/dist/adapters/_kit.js +98 -0
- package/dist/adapters/_kit.js.map +1 -0
- package/dist/adapters/aws.d.ts +158 -0
- package/dist/adapters/aws.d.ts.map +1 -0
- package/dist/adapters/aws.js +136 -0
- package/dist/adapters/aws.js.map +1 -0
- package/dist/adapters/cloudflare.d.ts +116 -0
- package/dist/adapters/cloudflare.d.ts.map +1 -0
- package/dist/adapters/cloudflare.js +75 -0
- package/dist/adapters/cloudflare.js.map +1 -0
- package/dist/adapters/gcp.d.ts +139 -0
- package/dist/adapters/gcp.d.ts.map +1 -0
- package/dist/adapters/gcp.js +69 -0
- package/dist/adapters/gcp.js.map +1 -0
- package/dist/adapters/github.d.ts +123 -0
- package/dist/adapters/github.d.ts.map +1 -0
- package/dist/adapters/github.js +105 -0
- package/dist/adapters/github.js.map +1 -0
- package/dist/adapters/jira.d.ts +109 -0
- package/dist/adapters/jira.d.ts.map +1 -0
- package/dist/adapters/jira.js +54 -0
- package/dist/adapters/jira.js.map +1 -0
- package/dist/adapters/k8s.d.ts +139 -0
- package/dist/adapters/k8s.d.ts.map +1 -0
- package/dist/adapters/k8s.js +69 -0
- package/dist/adapters/k8s.js.map +1 -0
- package/dist/adapters/linear.d.ts +109 -0
- package/dist/adapters/linear.d.ts.map +1 -0
- package/dist/adapters/linear.js +54 -0
- package/dist/adapters/linear.js.map +1 -0
- package/dist/adapters/salesforce.d.ts +109 -0
- package/dist/adapters/salesforce.d.ts.map +1 -0
- package/dist/adapters/salesforce.js +54 -0
- package/dist/adapters/salesforce.js.map +1 -0
- package/dist/adapters/stripe.d.ts +116 -0
- package/dist/adapters/stripe.d.ts.map +1 -0
- package/dist/adapters/stripe.js +77 -0
- package/dist/adapters/stripe.js.map +1 -0
- package/dist/adapters/supabase.d.ts +127 -0
- package/dist/adapters/supabase.d.ts.map +1 -0
- package/dist/adapters/supabase.js +112 -0
- package/dist/adapters/supabase.js.map +1 -0
- package/dist/adapters/terraform.d.ts +109 -0
- package/dist/adapters/terraform.d.ts.map +1 -0
- package/dist/adapters/terraform.js +58 -0
- package/dist/adapters/terraform.js.map +1 -0
- package/dist/adapters/vercel.d.ts +114 -0
- package/dist/adapters/vercel.d.ts.map +1 -0
- package/dist/adapters/vercel.js +81 -0
- package/dist/adapters/vercel.js.map +1 -0
- package/dist/aec-execution.d.ts +107 -0
- package/dist/aec-execution.d.ts.map +1 -0
- package/dist/aec-execution.js +334 -0
- package/dist/aec-execution.js.map +1 -0
- package/dist/breakglass.d.ts +205 -0
- package/dist/breakglass.d.ts.map +1 -0
- package/dist/breakglass.js +627 -0
- package/dist/breakglass.js.map +1 -0
- package/dist/capability-receipt.d.ts +601 -0
- package/dist/capability-receipt.d.ts.map +1 -0
- package/dist/capability-receipt.js +1177 -0
- package/dist/capability-receipt.js.map +1 -0
- package/dist/cf1-conformance.d.ts +83 -0
- package/dist/cf1-conformance.d.ts.map +1 -0
- package/dist/cf1-conformance.js +94 -0
- package/dist/cf1-conformance.js.map +1 -0
- package/dist/challenge-store.d.ts +20 -0
- package/dist/challenge-store.d.ts.map +1 -0
- package/dist/challenge-store.js +62 -0
- package/dist/challenge-store.js.map +1 -0
- package/dist/control-plane.d.ts +136 -0
- package/dist/control-plane.d.ts.map +1 -0
- package/dist/control-plane.js +349 -0
- package/dist/control-plane.js.map +1 -0
- package/dist/coverage.d.ts +156 -0
- package/dist/coverage.d.ts.map +1 -0
- package/dist/coverage.js +768 -0
- package/dist/coverage.js.map +1 -0
- package/dist/deployment-attestation.d.ts +93 -0
- package/dist/deployment-attestation.d.ts.map +1 -0
- package/dist/deployment-attestation.js +262 -0
- package/dist/deployment-attestation.js.map +1 -0
- package/dist/eg1-conformance.d.ts +286 -0
- package/dist/eg1-conformance.d.ts.map +1 -0
- package/dist/eg1-conformance.js +379 -0
- package/dist/eg1-conformance.js.map +1 -0
- package/dist/enterprise.d.ts +88 -0
- package/dist/enterprise.d.ts.map +1 -0
- package/dist/enterprise.js +194 -0
- package/dist/enterprise.js.map +1 -0
- package/dist/evidence-postgres.d.ts +123 -0
- package/dist/evidence-postgres.d.ts.map +1 -0
- package/dist/evidence-postgres.js +343 -0
- package/dist/evidence-postgres.js.map +1 -0
- package/dist/evidence.d.ts +113 -0
- package/dist/evidence.d.ts.map +1 -0
- package/dist/evidence.js +369 -0
- package/dist/evidence.js.map +1 -0
- package/dist/execution-binding.d.ts +49 -0
- package/dist/execution-binding.d.ts.map +1 -0
- package/dist/execution-binding.js +243 -0
- package/dist/execution-binding.js.map +1 -0
- package/dist/formal-runtime-map.d.ts +42 -0
- package/dist/formal-runtime-map.d.ts.map +1 -0
- package/dist/formal-runtime-map.js +23 -0
- package/dist/formal-runtime-map.js.map +1 -0
- package/dist/index.d.ts +1002 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1736 -0
- package/dist/index.js.map +1 -0
- package/dist/key-registry.d.ts +60 -0
- package/dist/key-registry.d.ts.map +1 -0
- package/dist/key-registry.js +144 -0
- package/dist/key-registry.js.map +1 -0
- package/dist/mcp.d.ts +53 -0
- package/dist/mcp.d.ts.map +1 -0
- package/dist/mcp.js +112 -0
- package/dist/mcp.js.map +1 -0
- package/dist/metering.d.ts +80 -0
- package/dist/metering.d.ts.map +1 -0
- package/dist/metering.js +227 -0
- package/dist/metering.js.map +1 -0
- package/dist/metrics.d.ts +68 -0
- package/dist/metrics.d.ts.map +1 -0
- package/dist/metrics.js +242 -0
- package/dist/metrics.js.map +1 -0
- package/dist/network-witness.d.ts +167 -0
- package/dist/network-witness.d.ts.map +1 -0
- package/dist/network-witness.js +520 -0
- package/dist/network-witness.js.map +1 -0
- package/dist/protocol-state.d.ts +141 -0
- package/dist/protocol-state.d.ts.map +1 -0
- package/dist/protocol-state.js +106 -0
- package/dist/protocol-state.js.map +1 -0
- package/dist/receipt-program.d.ts +59 -0
- package/dist/receipt-program.d.ts.map +1 -0
- package/dist/receipt-program.js +1053 -0
- package/dist/receipt-program.js.map +1 -0
- package/dist/reliance-kernel.d.ts +66 -0
- package/dist/reliance-kernel.d.ts.map +1 -0
- package/dist/reliance-kernel.js +103 -0
- package/dist/reliance-kernel.js.map +1 -0
- package/dist/reliance-packet.d.ts +67 -0
- package/dist/reliance-packet.d.ts.map +1 -0
- package/dist/reliance-packet.js +176 -0
- package/dist/reliance-packet.js.map +1 -0
- package/dist/remedy-program-postgres.d.ts +37 -0
- package/dist/remedy-program-postgres.d.ts.map +1 -0
- package/dist/remedy-program-postgres.js +291 -0
- package/dist/remedy-program-postgres.js.map +1 -0
- package/dist/remedy-program-receipt.d.ts +97 -0
- package/dist/remedy-program-receipt.d.ts.map +1 -0
- package/dist/remedy-program-receipt.js +817 -0
- package/dist/remedy-program-receipt.js.map +1 -0
- package/dist/remedy-program.d.ts +76 -0
- package/dist/remedy-program.d.ts.map +1 -0
- package/dist/remedy-program.js +1346 -0
- package/dist/remedy-program.js.map +1 -0
- package/dist/reports/art14.d.ts +101 -0
- package/dist/reports/art14.d.ts.map +1 -0
- package/dist/reports/art14.js +340 -0
- package/dist/reports/art14.js.map +1 -0
- package/dist/reports/assurance-package.d.ts +197 -0
- package/dist/reports/assurance-package.d.ts.map +1 -0
- package/dist/reports/assurance-package.js +313 -0
- package/dist/reports/assurance-package.js.map +1 -0
- package/dist/reports/auditor-workpaper.d.ts +194 -0
- package/dist/reports/auditor-workpaper.d.ts.map +1 -0
- package/dist/reports/auditor-workpaper.js +545 -0
- package/dist/reports/auditor-workpaper.js.map +1 -0
- package/dist/reports/external-verification.d.ts +76 -0
- package/dist/reports/external-verification.d.ts.map +1 -0
- package/dist/reports/external-verification.js +209 -0
- package/dist/reports/external-verification.js.map +1 -0
- package/dist/reports/reperform.d.ts +122 -0
- package/dist/reports/reperform.d.ts.map +1 -0
- package/dist/reports/reperform.js +432 -0
- package/dist/reports/reperform.js.map +1 -0
- package/dist/reports/underwriter.d.ts +127 -0
- package/dist/reports/underwriter.d.ts.map +1 -0
- package/dist/reports/underwriter.js +348 -0
- package/dist/reports/underwriter.js.map +1 -0
- package/dist/retention.d.ts +88 -0
- package/dist/retention.d.ts.map +1 -0
- package/dist/retention.js +90 -0
- package/dist/retention.js.map +1 -0
- package/dist/roster.d.ts +113 -0
- package/dist/roster.d.ts.map +1 -0
- package/dist/roster.js +261 -0
- package/dist/roster.js.map +1 -0
- package/dist/runtime-monitor.d.ts +132 -0
- package/dist/runtime-monitor.d.ts.map +1 -0
- package/dist/runtime-monitor.js +197 -0
- package/dist/runtime-monitor.js.map +1 -0
- package/dist/settlement.d.ts +68 -0
- package/dist/settlement.d.ts.map +1 -0
- package/dist/settlement.js +306 -0
- package/dist/settlement.js.map +1 -0
- package/dist/siem.d.ts +261 -0
- package/dist/siem.d.ts.map +1 -0
- package/dist/siem.js +247 -0
- package/dist/siem.js.map +1 -0
- package/dist/store-postgres.d.ts +107 -0
- package/dist/store-postgres.d.ts.map +1 -0
- package/dist/store-postgres.js +152 -0
- package/dist/store-postgres.js.map +1 -0
- package/dist/store.d.ts +95 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +202 -0
- package/dist/store.js.map +1 -0
- package/dist/strict-json.d.ts +14 -0
- package/dist/strict-json.d.ts.map +1 -0
- package/dist/strict-json.js +132 -0
- package/dist/strict-json.js.map +1 -0
- package/dist/trust-program-adapters.d.ts +122 -0
- package/dist/trust-program-adapters.d.ts.map +1 -0
- package/dist/trust-program-adapters.js +802 -0
- package/dist/trust-program-adapters.js.map +1 -0
- package/dist/trust-program-postgres.d.ts +41 -0
- package/dist/trust-program-postgres.d.ts.map +1 -0
- package/dist/trust-program-postgres.js +346 -0
- package/dist/trust-program-postgres.js.map +1 -0
- package/dist/trust-program-revocation.d.ts +206 -0
- package/dist/trust-program-revocation.d.ts.map +1 -0
- package/dist/trust-program-revocation.js +600 -0
- package/dist/trust-program-revocation.js.map +1 -0
- package/dist/trust-program.d.ts +197 -0
- package/dist/trust-program.d.ts.map +1 -0
- package/dist/trust-program.js +1351 -0
- package/dist/trust-program.js.map +1 -0
- package/dist/witness-postgres.d.ts +43 -0
- package/dist/witness-postgres.d.ts.map +1 -0
- package/dist/witness-postgres.js +92 -0
- package/dist/witness-postgres.js.map +1 -0
- package/dist/zk-range-proof.d.ts +78 -0
- package/dist/zk-range-proof.d.ts.map +1 -0
- package/dist/zk-range-proof.js +228 -0
- package/dist/zk-range-proof.js.map +1 -0
- package/eg1-conformance.js +3 -404
- package/enterprise.js +3 -180
- package/ep-assure.mjs +1 -0
- package/evidence-postgres.js +3 -356
- package/evidence.js +4 -360
- package/execution-binding.js +3 -212
- package/formal-runtime-map.js +4 -0
- package/index.js +4 -1249
- package/key-registry.js +3 -134
- package/mcp.js +3 -109
- package/metering.js +3 -226
- package/metrics.js +3 -231
- package/network-witness.js +3 -499
- package/package.json +47 -4
- package/receipt-program.js +4 -0
- package/reliance-kernel.js +3 -112
- package/reliance-packet.js +3 -175
- package/remedy-program-postgres.js +4 -0
- package/remedy-program-receipt.js +4 -0
- package/remedy-program.js +4 -0
- package/reports/art14.js +0 -0
- package/reports/assurance-package.js +2 -312
- package/reports/auditor-workpaper.js +3 -537
- package/reports/external-verification.js +3 -222
- package/reports/reperform.js +3 -424
- package/reports/underwriter.js +3 -339
- package/retention.js +3 -84
- package/roster.js +3 -264
- package/runtime-monitor.js +4 -0
- package/settlement.js +3 -299
- package/siem.js +3 -235
- package/src/action-control-manifest.ts +424 -0
- package/src/action-escrow-custodian.ts +427 -0
- package/src/action-escrow-evidence.ts +1178 -0
- package/src/action-escrow-package.ts +1821 -0
- package/src/action-escrow-postgres.ts +366 -0
- package/src/action-escrow-state.ts +498 -0
- package/src/action-escrow-verifiers.ts +484 -0
- package/src/action-escrow.ts +3039 -0
- package/src/action-packs.ts +170 -0
- package/src/adapters/_kit.ts +117 -0
- package/src/adapters/aws.ts +142 -0
- package/src/adapters/cloudflare.ts +78 -0
- package/src/adapters/gcp.ts +71 -0
- package/src/adapters/github.ts +110 -0
- package/src/adapters/jira.ts +56 -0
- package/src/adapters/k8s.ts +71 -0
- package/src/adapters/linear.ts +56 -0
- package/src/adapters/salesforce.ts +56 -0
- package/src/adapters/stripe.ts +82 -0
- package/src/adapters/supabase.ts +122 -0
- package/src/adapters/terraform.ts +60 -0
- package/src/adapters/vercel.ts +85 -0
- package/src/aec-execution.ts +354 -0
- package/src/breakglass.ts +647 -0
- package/src/capability-receipt.ts +1258 -0
- package/src/cf1-conformance.ts +120 -0
- package/src/challenge-store.ts +70 -0
- package/src/control-plane.ts +374 -0
- package/src/coverage.ts +832 -0
- package/src/deployment-attestation.ts +259 -0
- package/src/eg1-conformance.ts +471 -0
- package/src/enterprise.ts +195 -0
- package/src/evidence-postgres.ts +374 -0
- package/src/evidence.ts +367 -0
- package/src/execution-binding.ts +236 -0
- package/src/formal-runtime-map.ts +24 -0
- package/src/index.ts +2122 -0
- package/src/key-registry.ts +150 -0
- package/src/mcp.ts +120 -0
- package/src/metering.ts +237 -0
- package/src/metrics.ts +232 -0
- package/src/network-witness.ts +585 -0
- package/src/protocol-state.ts +143 -0
- package/src/protocol-state.typecheck.ts +94 -0
- package/src/receipt-program.ts +1105 -0
- package/src/reliance-kernel.ts +119 -0
- package/src/reliance-packet.ts +206 -0
- package/src/remedy-program-postgres.ts +367 -0
- package/src/remedy-program-receipt.ts +891 -0
- package/src/remedy-program.ts +1388 -0
- package/src/reports/art14.ts +361 -0
- package/src/reports/assurance-package.ts +330 -0
- package/src/reports/auditor-workpaper.ts +558 -0
- package/src/reports/external-verification.ts +232 -0
- package/src/reports/reperform.ts +441 -0
- package/src/reports/underwriter.ts +356 -0
- package/src/retention.ts +100 -0
- package/src/roster.ts +273 -0
- package/src/runtime-monitor.ts +217 -0
- package/src/settlement.ts +313 -0
- package/src/siem.ts +247 -0
- package/src/store-postgres.ts +167 -0
- package/src/store.ts +214 -0
- package/src/strict-json.ts +115 -0
- package/src/trust-program-adapters.ts +847 -0
- package/src/trust-program-postgres.ts +429 -0
- package/src/trust-program-revocation.ts +751 -0
- package/src/trust-program.ts +1460 -0
- package/src/witness-postgres.ts +106 -0
- package/src/zk-range-proof.ts +250 -0
- package/store-postgres.js +3 -161
- package/store.js +4 -198
- package/strict-json.js +3 -85
- package/trust-program-adapters.js +4 -0
- package/trust-program-postgres.js +4 -0
- package/trust-program-revocation.js +4 -0
- package/trust-program.js +4 -0
- package/witness-postgres.js +3 -96
- package/zk-range-proof.js +4 -0
|
@@ -0,0 +1,585 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
/**
|
|
3
|
+
* Independent, privacy-minimized observation evidence for EMILIA Gate.
|
|
4
|
+
*
|
|
5
|
+
* A network witness is deliberately NOT an enforcement point. It can prove a
|
|
6
|
+
* pinned sensor observed bytes associated with an action digest at a named
|
|
7
|
+
* capture point. It cannot prove the action was authorized, blocked, executed,
|
|
8
|
+
* or physically completed. Keeping that boundary in the artifact prevents a
|
|
9
|
+
* passive TAP from being marketed as a firewall.
|
|
10
|
+
*/
|
|
11
|
+
import crypto from 'node:crypto';
|
|
12
|
+
import { canonicalize } from './execution-binding.js';
|
|
13
|
+
import { strictJsonGate } from './strict-json.js';
|
|
14
|
+
|
|
15
|
+
export const NETWORK_WITNESS_VERSION = 'EP-GATE-NETWORK-WITNESS-v1';
|
|
16
|
+
export const NETWORK_WITNESS_ACCEPTANCE_VERSION = 'EP-GATE-NETWORK-WITNESS-ACCEPTANCE-v1';
|
|
17
|
+
export const NETWORK_WITNESS_DOMAIN = `${NETWORK_WITNESS_VERSION}\0`;
|
|
18
|
+
export const NETWORK_WITNESS_EVENTS = Object.freeze([
|
|
19
|
+
'request_observed',
|
|
20
|
+
'response_observed',
|
|
21
|
+
'effect_observed',
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
const DIGEST_RE = /^sha256:[0-9a-f]{64}$/;
|
|
25
|
+
const DIRECTIONS = new Set(['ingress', 'egress', 'internal']);
|
|
26
|
+
const RFC3339 = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d{1,9}))?Z$/;
|
|
27
|
+
|
|
28
|
+
type NetworkWitnessChecks = {
|
|
29
|
+
shape: boolean;
|
|
30
|
+
pin: boolean;
|
|
31
|
+
signature: boolean;
|
|
32
|
+
action_binding: boolean;
|
|
33
|
+
freshness: boolean;
|
|
34
|
+
config_binding: boolean;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
type NetworkWitnessVerifyFailure = {
|
|
38
|
+
verified: false;
|
|
39
|
+
accepted: false;
|
|
40
|
+
reason: string;
|
|
41
|
+
checks: NetworkWitnessChecks;
|
|
42
|
+
statement_digest?: undefined;
|
|
43
|
+
stream_id?: undefined;
|
|
44
|
+
sequence?: undefined;
|
|
45
|
+
action_digest?: undefined;
|
|
46
|
+
event?: undefined;
|
|
47
|
+
observed_at?: undefined;
|
|
48
|
+
witness_id?: undefined;
|
|
49
|
+
capture_point_id?: undefined;
|
|
50
|
+
limitation?: undefined;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
type NetworkWitnessVerifySuccess = {
|
|
54
|
+
verified: true;
|
|
55
|
+
accepted: true;
|
|
56
|
+
reason: null;
|
|
57
|
+
statement_digest: string;
|
|
58
|
+
stream_id: string;
|
|
59
|
+
sequence: number;
|
|
60
|
+
action_digest: string;
|
|
61
|
+
event: string;
|
|
62
|
+
observed_at: string;
|
|
63
|
+
witness_id: string;
|
|
64
|
+
capture_point_id: string;
|
|
65
|
+
checks: NetworkWitnessChecks;
|
|
66
|
+
limitation: string;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
type NetworkWitnessVerifyResult = NetworkWitnessVerifyFailure | NetworkWitnessVerifySuccess;
|
|
70
|
+
|
|
71
|
+
type NetworkWitnessVerifyOptions = {
|
|
72
|
+
pinnedWitnesses?: any[];
|
|
73
|
+
expectedActionDigest?: string;
|
|
74
|
+
expectedEvent?: string;
|
|
75
|
+
now?: number;
|
|
76
|
+
maxAgeSec?: number;
|
|
77
|
+
maxFutureSkewSec?: number;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
type NetworkWitnessAcceptanceOptions = {
|
|
81
|
+
allowEphemeralStore?: boolean;
|
|
82
|
+
expectedStatementDigest?: string;
|
|
83
|
+
expectedActionDigest?: string;
|
|
84
|
+
expectedEvent?: string;
|
|
85
|
+
now?: number;
|
|
86
|
+
maxAgeSec?: number;
|
|
87
|
+
maxFutureSkewSec?: number;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
interface NetworkWitnessSequenceStore {
|
|
91
|
+
durable: boolean;
|
|
92
|
+
advance: (
|
|
93
|
+
streamId: string,
|
|
94
|
+
sequence: number,
|
|
95
|
+
statementDigest: string,
|
|
96
|
+
) => Promise<{ accepted: boolean; reason: string | null }>;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
type NetworkWitnessAcceptOptions = NetworkWitnessVerifyOptions & {
|
|
100
|
+
sequenceStore?: NetworkWitnessSequenceStore;
|
|
101
|
+
allowEphemeralStore?: boolean;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
function strictInstantMs(value) {
|
|
105
|
+
if (typeof value !== 'string') return NaN;
|
|
106
|
+
const match = value.match(RFC3339);
|
|
107
|
+
if (!match) return NaN;
|
|
108
|
+
const [, year, month, day, hour, minute, second] = match;
|
|
109
|
+
const base = new Date(0);
|
|
110
|
+
base.setUTCFullYear(Number(year), Number(month) - 1, Number(day));
|
|
111
|
+
base.setUTCHours(Number(hour), Number(minute), Number(second), 0);
|
|
112
|
+
if (base.toISOString().slice(0, 19) !== `${year}-${month}-${day}T${hour}:${minute}:${second}`) return NaN;
|
|
113
|
+
const parsed = Date.parse(value);
|
|
114
|
+
return Number.isFinite(parsed) ? parsed : NaN;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function isPlainObject(value) {
|
|
118
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) return false;
|
|
119
|
+
const prototype = Object.getPrototypeOf(value);
|
|
120
|
+
return prototype === Object.prototype || prototype === null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function exactKeys(value, allowed) {
|
|
124
|
+
return isPlainObject(value) && Object.keys(value).every((key) => allowed.has(key));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function nonEmptyString(value, max = 256) {
|
|
128
|
+
return typeof value === 'string' && value.length > 0 && value.length <= max && !/[\u0000-\u001f\u007f]/.test(value);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function digest(value) {
|
|
132
|
+
return typeof value === 'string' && DIGEST_RE.test(value);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function sha256(bytes) {
|
|
136
|
+
return crypto.createHash('sha256').update(bytes).digest('hex');
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function decodeBase64Url(value, maxBytes) {
|
|
140
|
+
if (typeof value !== 'string' || value.length === 0 || !/^[A-Za-z0-9_-]+$/.test(value)) return null;
|
|
141
|
+
try {
|
|
142
|
+
const bytes = Buffer.from(value, 'base64url');
|
|
143
|
+
if (bytes.length === 0 || bytes.length > maxBytes || bytes.toString('base64url') !== value) return null;
|
|
144
|
+
return bytes;
|
|
145
|
+
} catch { return null; }
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function keyIdFor(key) {
|
|
149
|
+
const der = crypto.createPublicKey(key).export({ type: 'spki', format: 'der' });
|
|
150
|
+
return `ep:witness-key:sha256:${sha256(der).slice(0, 16)}`;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function signingBytes(body) {
|
|
154
|
+
return Buffer.from(NETWORK_WITNESS_DOMAIN + canonicalize(body), 'utf8');
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function unsigned(statement) {
|
|
158
|
+
if (!isPlainObject(statement)) throw new TypeError('network witness statement must be an object');
|
|
159
|
+
const { signature: _signature, ...body } = statement;
|
|
160
|
+
return body;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function networkWitnessDigest(statement) {
|
|
164
|
+
return `sha256:${sha256(signingBytes(unsigned(statement)))}`;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Duplicate-key-safe parser for an untrusted serialized witness artifact. */
|
|
168
|
+
export function parseNetworkWitnessStatement(raw, { maxBytes = 64 * 1024 } = {}) {
|
|
169
|
+
if (typeof raw !== 'string' || !Number.isSafeInteger(maxBytes) || maxBytes < 1
|
|
170
|
+
|| Buffer.byteLength(raw, 'utf8') > maxBytes) return null;
|
|
171
|
+
const gated = strictJsonGate(raw);
|
|
172
|
+
if (!gated.ok) return null;
|
|
173
|
+
try {
|
|
174
|
+
const parsed = JSON.parse(raw);
|
|
175
|
+
return isPlainObject(parsed) ? parsed : null;
|
|
176
|
+
} catch { return null; }
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function validateBody(body) {
|
|
180
|
+
if (!exactKeys(body, new Set(['@version', 'witness', 'observation', 'deployment', 'privacy', 'limitations']))) {
|
|
181
|
+
return 'statement_shape_invalid';
|
|
182
|
+
}
|
|
183
|
+
if (body['@version'] !== NETWORK_WITNESS_VERSION) return 'version_invalid';
|
|
184
|
+
if (!exactKeys(body.witness, new Set(['id', 'key_id', 'capture_point_id']))) return 'witness_shape_invalid';
|
|
185
|
+
if (!nonEmptyString(body.witness.id) || !nonEmptyString(body.witness.key_id)
|
|
186
|
+
|| !nonEmptyString(body.witness.capture_point_id)) return 'witness_identity_invalid';
|
|
187
|
+
if (!exactKeys(body.observation, new Set([
|
|
188
|
+
'sequence', 'observed_at', 'event', 'direction', 'action_digest', 'flow_digest', 'byte_count',
|
|
189
|
+
]))) return 'observation_shape_invalid';
|
|
190
|
+
if (!Number.isSafeInteger(body.observation.sequence) || body.observation.sequence < 0) return 'sequence_invalid';
|
|
191
|
+
if (!Number.isFinite(strictInstantMs(body.observation.observed_at))) return 'observed_at_invalid';
|
|
192
|
+
if (!NETWORK_WITNESS_EVENTS.includes(body.observation.event)) return 'event_invalid';
|
|
193
|
+
if (!DIRECTIONS.has(body.observation.direction)) return 'direction_invalid';
|
|
194
|
+
if (!digest(body.observation.action_digest)) return 'action_digest_invalid';
|
|
195
|
+
if (body.observation.flow_digest !== undefined && !digest(body.observation.flow_digest)) return 'flow_digest_invalid';
|
|
196
|
+
if (body.observation.byte_count !== undefined
|
|
197
|
+
&& (!Number.isSafeInteger(body.observation.byte_count) || body.observation.byte_count < 0)) return 'byte_count_invalid';
|
|
198
|
+
if (!exactKeys(body.deployment, new Set(['config_digest', 'attestation_ref']))) return 'deployment_shape_invalid';
|
|
199
|
+
if (!digest(body.deployment.config_digest)) return 'config_digest_invalid';
|
|
200
|
+
if (body.deployment.attestation_ref !== undefined && !digest(body.deployment.attestation_ref)) {
|
|
201
|
+
return 'attestation_ref_invalid';
|
|
202
|
+
}
|
|
203
|
+
if (!exactKeys(body.privacy, new Set(['payload_captured']))) return 'privacy_shape_invalid';
|
|
204
|
+
if (body.privacy.payload_captured !== false) return 'payload_capture_forbidden';
|
|
205
|
+
if (!Array.isArray(body.limitations) || body.limitations.length < 2 || body.limitations.length > 8
|
|
206
|
+
|| body.limitations.some((item) => !nonEmptyString(item, 512))) return 'limitations_invalid';
|
|
207
|
+
try { canonicalize(body); } catch { return 'canonical_body_invalid'; }
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** Create a signed observation. The public key is intentionally not embedded. */
|
|
212
|
+
export function signNetworkWitnessStatement(input, privateKey) {
|
|
213
|
+
if (!privateKey) throw new TypeError('privateKey is required');
|
|
214
|
+
const keyId = input?.key_id ?? keyIdFor(privateKey);
|
|
215
|
+
const body = {
|
|
216
|
+
'@version': NETWORK_WITNESS_VERSION,
|
|
217
|
+
witness: {
|
|
218
|
+
id: input?.witness_id,
|
|
219
|
+
key_id: keyId,
|
|
220
|
+
capture_point_id: input?.capture_point_id,
|
|
221
|
+
},
|
|
222
|
+
observation: {
|
|
223
|
+
sequence: input?.sequence,
|
|
224
|
+
observed_at: input?.observed_at,
|
|
225
|
+
event: input?.event,
|
|
226
|
+
direction: input?.direction,
|
|
227
|
+
action_digest: input?.action_digest,
|
|
228
|
+
...(input?.flow_digest !== undefined ? { flow_digest: input.flow_digest } : {}),
|
|
229
|
+
...(input?.byte_count !== undefined ? { byte_count: input.byte_count } : {}),
|
|
230
|
+
},
|
|
231
|
+
deployment: {
|
|
232
|
+
config_digest: input?.config_digest,
|
|
233
|
+
...(input?.attestation_ref !== undefined ? { attestation_ref: input.attestation_ref } : {}),
|
|
234
|
+
},
|
|
235
|
+
privacy: { payload_captured: false },
|
|
236
|
+
limitations: [
|
|
237
|
+
'This artifact proves only that a pinned witness key signed an observation at a named capture point.',
|
|
238
|
+
'A passive network witness does not authorize, block, execute, or prove the physical outcome of an action.',
|
|
239
|
+
'Coverage and completeness depend on the relying party pinning the capture topology and expected witness set.',
|
|
240
|
+
'Rollback detection begins at a relying-party-pinned stream checkpoint; first-seen sequence numbers are not self-authenticating history.',
|
|
241
|
+
],
|
|
242
|
+
};
|
|
243
|
+
const invalid = validateBody(body);
|
|
244
|
+
if (invalid) throw new TypeError(invalid);
|
|
245
|
+
const statementDigest = networkWitnessDigest(body);
|
|
246
|
+
return Object.freeze({
|
|
247
|
+
...body,
|
|
248
|
+
signature: Object.freeze({
|
|
249
|
+
algorithm: 'Ed25519',
|
|
250
|
+
key_id: keyId,
|
|
251
|
+
statement_digest: statementDigest,
|
|
252
|
+
signature_b64u: crypto.sign(null, signingBytes(body), privateKey).toString('base64url'),
|
|
253
|
+
}),
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function findPin(pins, witness) {
|
|
258
|
+
if (!Array.isArray(pins)) return null;
|
|
259
|
+
return pins.find((pin) => isPlainObject(pin)
|
|
260
|
+
&& pin.witness_id === witness.id
|
|
261
|
+
&& pin.key_id === witness.key_id
|
|
262
|
+
&& Array.isArray(pin.capture_point_ids)
|
|
263
|
+
&& pin.capture_point_ids.includes(witness.capture_point_id)) ?? null;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Offline signature and context verification. This function never throws on a
|
|
268
|
+
* presenter-controlled statement. Sequence consumption is a separate online
|
|
269
|
+
* operation performed by acceptNetworkWitnessStatement.
|
|
270
|
+
*/
|
|
271
|
+
export function verifyNetworkWitnessStatement(
|
|
272
|
+
statement: any,
|
|
273
|
+
options: NetworkWitnessVerifyOptions = {},
|
|
274
|
+
): NetworkWitnessVerifyResult {
|
|
275
|
+
const fail = (reason: string, checks: Partial<NetworkWitnessChecks> = {}): NetworkWitnessVerifyFailure => ({
|
|
276
|
+
verified: false,
|
|
277
|
+
accepted: false,
|
|
278
|
+
reason,
|
|
279
|
+
checks: {
|
|
280
|
+
shape: false,
|
|
281
|
+
pin: false,
|
|
282
|
+
signature: false,
|
|
283
|
+
action_binding: false,
|
|
284
|
+
freshness: false,
|
|
285
|
+
config_binding: false,
|
|
286
|
+
...checks,
|
|
287
|
+
},
|
|
288
|
+
});
|
|
289
|
+
try {
|
|
290
|
+
if (!isPlainObject(statement)
|
|
291
|
+
|| !exactKeys(statement, new Set(['@version', 'witness', 'observation', 'deployment', 'privacy', 'limitations', 'signature']))) {
|
|
292
|
+
return fail('statement_shape_invalid');
|
|
293
|
+
}
|
|
294
|
+
const body = unsigned(statement);
|
|
295
|
+
const invalid = validateBody(body);
|
|
296
|
+
if (invalid) return fail(invalid);
|
|
297
|
+
if (!exactKeys(statement.signature, new Set(['algorithm', 'key_id', 'statement_digest', 'signature_b64u']))) {
|
|
298
|
+
return fail('signature_shape_invalid', { shape: true });
|
|
299
|
+
}
|
|
300
|
+
if (statement.signature.algorithm !== 'Ed25519'
|
|
301
|
+
|| statement.signature.key_id !== body.witness.key_id
|
|
302
|
+
|| !digest(statement.signature.statement_digest)
|
|
303
|
+
|| !nonEmptyString(statement.signature.signature_b64u, 512)) {
|
|
304
|
+
return fail('signature_envelope_invalid', { shape: true });
|
|
305
|
+
}
|
|
306
|
+
const pin = findPin(options.pinnedWitnesses, body.witness);
|
|
307
|
+
if (!pin || !nonEmptyString(pin.public_key, 4096)) return fail('witness_key_unpinned', { shape: true });
|
|
308
|
+
if (!Array.isArray(pin.config_digests) || pin.config_digests.length === 0
|
|
309
|
+
|| !pin.config_digests.includes(body.deployment.config_digest)) {
|
|
310
|
+
return fail('witness_config_unpinned', { shape: true, pin: true });
|
|
311
|
+
}
|
|
312
|
+
if (options.expectedActionDigest !== undefined
|
|
313
|
+
&& body.observation.action_digest !== options.expectedActionDigest) {
|
|
314
|
+
return fail('action_digest_mismatch', { shape: true, pin: true, config_binding: true });
|
|
315
|
+
}
|
|
316
|
+
if (options.expectedEvent !== undefined && body.observation.event !== options.expectedEvent) {
|
|
317
|
+
return fail('event_mismatch', { shape: true, pin: true, config_binding: true });
|
|
318
|
+
}
|
|
319
|
+
const now = options.now === undefined ? Date.now() : Number(options.now);
|
|
320
|
+
const maxAgeSec = options.maxAgeSec === undefined ? 300 : options.maxAgeSec;
|
|
321
|
+
const maxFutureSkewSec = options.maxFutureSkewSec === undefined ? 30 : options.maxFutureSkewSec;
|
|
322
|
+
if (!Number.isFinite(now) || !Number.isSafeInteger(maxAgeSec) || maxAgeSec < 0
|
|
323
|
+
|| !Number.isSafeInteger(maxFutureSkewSec) || maxFutureSkewSec < 0) {
|
|
324
|
+
return fail('verification_profile_invalid', { shape: true, pin: true, config_binding: true });
|
|
325
|
+
}
|
|
326
|
+
const observedMs = strictInstantMs(body.observation.observed_at);
|
|
327
|
+
if (observedMs > now + (maxFutureSkewSec * 1000)) {
|
|
328
|
+
return fail('observation_from_future', { shape: true, pin: true, action_binding: true, config_binding: true });
|
|
329
|
+
}
|
|
330
|
+
if (now - observedMs > maxAgeSec * 1000) {
|
|
331
|
+
return fail('observation_stale', { shape: true, pin: true, action_binding: true, config_binding: true });
|
|
332
|
+
}
|
|
333
|
+
const computedDigest = networkWitnessDigest(body);
|
|
334
|
+
if (computedDigest !== statement.signature.statement_digest) {
|
|
335
|
+
return fail('statement_digest_mismatch', {
|
|
336
|
+
shape: true, pin: true, action_binding: true, freshness: true, config_binding: true,
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
let publicKey;
|
|
340
|
+
try {
|
|
341
|
+
const keyBytes = decodeBase64Url(pin.public_key, 4096);
|
|
342
|
+
if (!keyBytes) throw new TypeError('invalid base64url key');
|
|
343
|
+
publicKey = crypto.createPublicKey({
|
|
344
|
+
key: keyBytes,
|
|
345
|
+
type: 'spki',
|
|
346
|
+
format: 'der',
|
|
347
|
+
});
|
|
348
|
+
if (publicKey.asymmetricKeyType !== 'ed25519') throw new TypeError('witness key must be Ed25519');
|
|
349
|
+
} catch {
|
|
350
|
+
return fail('pinned_key_invalid', {
|
|
351
|
+
shape: true, pin: true, action_binding: true, freshness: true, config_binding: true,
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
const signature = decodeBase64Url(statement.signature.signature_b64u, 64);
|
|
355
|
+
if (!signature || signature.length !== 64) {
|
|
356
|
+
return fail('signature_invalid', {
|
|
357
|
+
shape: true, pin: true, action_binding: true, freshness: true, config_binding: true,
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
if (!crypto.verify(null, signingBytes(body), publicKey, signature)) {
|
|
361
|
+
return fail('signature_invalid', {
|
|
362
|
+
shape: true, pin: true, action_binding: true, freshness: true, config_binding: true,
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
return {
|
|
366
|
+
verified: true,
|
|
367
|
+
accepted: true,
|
|
368
|
+
reason: null,
|
|
369
|
+
statement_digest: computedDigest,
|
|
370
|
+
stream_id: `${body.witness.id}\0${body.witness.capture_point_id}`,
|
|
371
|
+
sequence: body.observation.sequence,
|
|
372
|
+
action_digest: body.observation.action_digest,
|
|
373
|
+
event: body.observation.event,
|
|
374
|
+
observed_at: body.observation.observed_at,
|
|
375
|
+
witness_id: body.witness.id,
|
|
376
|
+
capture_point_id: body.witness.capture_point_id,
|
|
377
|
+
checks: {
|
|
378
|
+
shape: true,
|
|
379
|
+
pin: true,
|
|
380
|
+
signature: true,
|
|
381
|
+
action_binding: true,
|
|
382
|
+
freshness: true,
|
|
383
|
+
config_binding: true,
|
|
384
|
+
},
|
|
385
|
+
limitation: 'Observation is not authorization, enforcement, execution, or physical truth.',
|
|
386
|
+
};
|
|
387
|
+
} catch {
|
|
388
|
+
return fail('hostile_input_refused');
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
export function createMemoryWitnessSequenceStore() {
|
|
393
|
+
const streams = new Map();
|
|
394
|
+
return {
|
|
395
|
+
durable: false,
|
|
396
|
+
async advance(streamId, sequence, statementDigest) {
|
|
397
|
+
const previous = streams.get(streamId);
|
|
398
|
+
if (previous) {
|
|
399
|
+
if (previous.equivocated === true) {
|
|
400
|
+
return { accepted: false, reason: 'sequence_equivocation' };
|
|
401
|
+
}
|
|
402
|
+
if (sequence < previous.sequence) return { accepted: false, reason: 'sequence_rollback' };
|
|
403
|
+
if (sequence === previous.sequence) {
|
|
404
|
+
if (previous.digest === statementDigest) {
|
|
405
|
+
return { accepted: false, reason: 'statement_replay' };
|
|
406
|
+
}
|
|
407
|
+
streams.set(streamId, { ...previous, equivocated: true });
|
|
408
|
+
return { accepted: false, reason: 'sequence_equivocation' };
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
streams.set(streamId, { sequence, digest: statementDigest, equivocated: false });
|
|
412
|
+
return { accepted: true, reason: null };
|
|
413
|
+
},
|
|
414
|
+
snapshot() { return [...streams.entries()].map(([stream_id, value]) => ({ stream_id, ...value })); },
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
function acceptanceResult(verified, {
|
|
419
|
+
accepted,
|
|
420
|
+
consumed,
|
|
421
|
+
reason,
|
|
422
|
+
sequenceStoreDurable,
|
|
423
|
+
}) {
|
|
424
|
+
return Object.freeze({
|
|
425
|
+
...verified,
|
|
426
|
+
acceptance_version: NETWORK_WITNESS_ACCEPTANCE_VERSION,
|
|
427
|
+
accepted,
|
|
428
|
+
consumed,
|
|
429
|
+
reason,
|
|
430
|
+
sequence_store_durable: sequenceStoreDurable === true,
|
|
431
|
+
...(isPlainObject(verified.checks) ? { checks: Object.freeze({ ...verified.checks }) } : {}),
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* Validate an ingestion result supplied through a relying-party-trusted option.
|
|
437
|
+
* This does not authenticate presenter-controlled JSON; callers must never move
|
|
438
|
+
* an untrusted bundle field into this trust channel.
|
|
439
|
+
*/
|
|
440
|
+
export function validateTrustedNetworkWitnessAcceptance(
|
|
441
|
+
result: any,
|
|
442
|
+
options: NetworkWitnessAcceptanceOptions = {},
|
|
443
|
+
) {
|
|
444
|
+
const fail = (reason: string, fields: Record<string, any> = {}, statementVerified = false) => ({
|
|
445
|
+
verified: statementVerified === true,
|
|
446
|
+
accepted: false,
|
|
447
|
+
consumed: false,
|
|
448
|
+
reason,
|
|
449
|
+
...fields,
|
|
450
|
+
});
|
|
451
|
+
try {
|
|
452
|
+
if (!isPlainObject(result)
|
|
453
|
+
|| result.acceptance_version !== NETWORK_WITNESS_ACCEPTANCE_VERSION) {
|
|
454
|
+
return fail('trusted_witness_acceptance_invalid');
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
const common = {
|
|
458
|
+
acceptance_version: NETWORK_WITNESS_ACCEPTANCE_VERSION,
|
|
459
|
+
statement_digest: result.statement_digest,
|
|
460
|
+
stream_id: result.stream_id,
|
|
461
|
+
sequence: result.sequence,
|
|
462
|
+
action_digest: result.action_digest,
|
|
463
|
+
event: result.event,
|
|
464
|
+
observed_at: result.observed_at,
|
|
465
|
+
witness_id: result.witness_id,
|
|
466
|
+
capture_point_id: result.capture_point_id,
|
|
467
|
+
sequence_store_durable: result.sequence_store_durable === true,
|
|
468
|
+
};
|
|
469
|
+
const statementVerified = result.verified === true
|
|
470
|
+
&& digest(common.statement_digest) && digest(common.action_digest)
|
|
471
|
+
&& Number.isSafeInteger(common.sequence) && common.sequence >= 0
|
|
472
|
+
&& nonEmptyString(common.witness_id) && nonEmptyString(common.capture_point_id)
|
|
473
|
+
&& common.stream_id === `${common.witness_id}\0${common.capture_point_id}`
|
|
474
|
+
&& NETWORK_WITNESS_EVENTS.includes(common.event)
|
|
475
|
+
&& Number.isFinite(strictInstantMs(common.observed_at));
|
|
476
|
+
if (result.accepted !== true || result.consumed !== true) {
|
|
477
|
+
return fail(
|
|
478
|
+
nonEmptyString(result.reason) ? result.reason : 'trusted_witness_acceptance_rejected',
|
|
479
|
+
common,
|
|
480
|
+
statementVerified,
|
|
481
|
+
);
|
|
482
|
+
}
|
|
483
|
+
if (!statementVerified || result.reason !== null) {
|
|
484
|
+
return fail('trusted_witness_acceptance_invalid');
|
|
485
|
+
}
|
|
486
|
+
if (!common.sequence_store_durable && options.allowEphemeralStore !== true) {
|
|
487
|
+
return fail('durable_sequence_store_required', common);
|
|
488
|
+
}
|
|
489
|
+
if (options.expectedStatementDigest !== undefined) {
|
|
490
|
+
if (!digest(options.expectedStatementDigest)) return fail('verification_profile_invalid', common);
|
|
491
|
+
if (common.statement_digest !== options.expectedStatementDigest) {
|
|
492
|
+
return fail('witness_acceptance_digest_mismatch', common);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
if (options.expectedActionDigest !== undefined
|
|
496
|
+
&& common.action_digest !== options.expectedActionDigest) {
|
|
497
|
+
return fail('action_digest_mismatch', common);
|
|
498
|
+
}
|
|
499
|
+
if (options.expectedEvent !== undefined && common.event !== options.expectedEvent) {
|
|
500
|
+
return fail('event_mismatch', common);
|
|
501
|
+
}
|
|
502
|
+
const now = options.now === undefined ? Date.now() : Number(options.now);
|
|
503
|
+
const maxAgeSec = options.maxAgeSec === undefined ? 300 : options.maxAgeSec;
|
|
504
|
+
const maxFutureSkewSec = options.maxFutureSkewSec === undefined ? 30 : options.maxFutureSkewSec;
|
|
505
|
+
if (!Number.isFinite(now) || !Number.isSafeInteger(maxAgeSec) || maxAgeSec < 0
|
|
506
|
+
|| !Number.isSafeInteger(maxFutureSkewSec) || maxFutureSkewSec < 0) {
|
|
507
|
+
return fail('verification_profile_invalid', common);
|
|
508
|
+
}
|
|
509
|
+
const observedMs = strictInstantMs(common.observed_at);
|
|
510
|
+
if (observedMs > now + (maxFutureSkewSec * 1000)) return fail('observation_from_future', common);
|
|
511
|
+
if (now - observedMs > maxAgeSec * 1000) return fail('observation_stale', common);
|
|
512
|
+
return {
|
|
513
|
+
...common,
|
|
514
|
+
verified: true,
|
|
515
|
+
accepted: true,
|
|
516
|
+
consumed: true,
|
|
517
|
+
reason: null,
|
|
518
|
+
};
|
|
519
|
+
} catch {
|
|
520
|
+
return fail('trusted_witness_acceptance_invalid');
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
/** Verify and atomically advance a witness stream for online ingestion. */
|
|
525
|
+
export async function acceptNetworkWitnessStatement(
|
|
526
|
+
statement: any,
|
|
527
|
+
options: NetworkWitnessAcceptOptions = {},
|
|
528
|
+
) {
|
|
529
|
+
const verified = verifyNetworkWitnessStatement(statement, options);
|
|
530
|
+
if (!verified.accepted) {
|
|
531
|
+
return acceptanceResult(verified, {
|
|
532
|
+
accepted: false,
|
|
533
|
+
consumed: false,
|
|
534
|
+
reason: verified.reason,
|
|
535
|
+
sequenceStoreDurable: false,
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
const store = options.sequenceStore;
|
|
539
|
+
if (!store || typeof store.advance !== 'function'
|
|
540
|
+
|| (store.durable !== true && options.allowEphemeralStore !== true)) {
|
|
541
|
+
return acceptanceResult(verified, {
|
|
542
|
+
accepted: false,
|
|
543
|
+
consumed: false,
|
|
544
|
+
reason: 'durable_sequence_store_required',
|
|
545
|
+
sequenceStoreDurable: false,
|
|
546
|
+
});
|
|
547
|
+
}
|
|
548
|
+
try {
|
|
549
|
+
const advanced = await store.advance(verified.stream_id, verified.sequence, verified.statement_digest);
|
|
550
|
+
if (!isPlainObject(advanced) || advanced.accepted !== true || advanced.reason !== null) {
|
|
551
|
+
return acceptanceResult(verified, {
|
|
552
|
+
accepted: false,
|
|
553
|
+
consumed: false,
|
|
554
|
+
reason: nonEmptyString(advanced?.reason) ? advanced.reason : 'sequence_store_refused',
|
|
555
|
+
sequenceStoreDurable: store.durable === true,
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
return acceptanceResult(verified, {
|
|
559
|
+
accepted: true,
|
|
560
|
+
consumed: true,
|
|
561
|
+
reason: null,
|
|
562
|
+
sequenceStoreDurable: store.durable === true,
|
|
563
|
+
});
|
|
564
|
+
} catch {
|
|
565
|
+
return acceptanceResult(verified, {
|
|
566
|
+
accepted: false,
|
|
567
|
+
consumed: false,
|
|
568
|
+
reason: 'sequence_store_unavailable',
|
|
569
|
+
sequenceStoreDurable: store.durable === true,
|
|
570
|
+
});
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
export default {
|
|
575
|
+
NETWORK_WITNESS_VERSION,
|
|
576
|
+
NETWORK_WITNESS_ACCEPTANCE_VERSION,
|
|
577
|
+
NETWORK_WITNESS_EVENTS,
|
|
578
|
+
parseNetworkWitnessStatement,
|
|
579
|
+
networkWitnessDigest,
|
|
580
|
+
signNetworkWitnessStatement,
|
|
581
|
+
verifyNetworkWitnessStatement,
|
|
582
|
+
acceptNetworkWitnessStatement,
|
|
583
|
+
validateTrustedNetworkWitnessAcceptance,
|
|
584
|
+
createMemoryWitnessSequenceStore,
|
|
585
|
+
};
|