@coreguard/agent-commerce 0.0.0-stage → 0.1.1
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/README.md +42 -2
- package/attest.mjs +401 -0
- package/bin/mcp.mjs +15 -0
- package/client.mjs +147 -0
- package/consumption-store.mjs +147 -0
- package/firewall.mjs +277 -0
- package/http.mjs +164 -0
- package/index.d.ts +324 -0
- package/index.mjs +239 -0
- package/mcp.mjs +214 -0
- package/package.json +62 -4
- package/policy-context.mjs +47 -0
- package/risk-cli.mjs +138 -0
- package/risk.mjs +255 -0
package/README.md
CHANGED
|
@@ -1,3 +1,43 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @coreguard/agent-commerce
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Consent receipts and fail-closed execution verification for agentic purchases —
|
|
4
|
+
an offline, deterministic surface for binding a user's approval to the exact
|
|
5
|
+
bytes approved, and for verifying any executed trace against that receipt.
|
|
6
|
+
|
|
7
|
+
- **CGCR/1 consent receipts** — the approved order (intent + policy + declared
|
|
8
|
+
submit payload) is committed as an independently re-derivable receipt; every
|
|
9
|
+
verification recomputes the commitments from the record's own approved bytes.
|
|
10
|
+
- **Fail-closed execution verification** — a consent plus an executed trace
|
|
11
|
+
yields a verdict with named drift classes (AGR/1 taxonomy); incomplete or
|
|
12
|
+
ambiguous evidence is never treated as a pass.
|
|
13
|
+
- **stdio MCP server** (JSON-RPC 2.0) exposing four tools, all offline, all
|
|
14
|
+
deterministic, zero keys: `coreguard_authorize`, `coreguard_verify_consent`,
|
|
15
|
+
`coreguard_verify_execution`, `coreguard_assess_risk`.
|
|
16
|
+
|
|
17
|
+
## MCP client configuration
|
|
18
|
+
|
|
19
|
+
```json
|
|
20
|
+
{
|
|
21
|
+
"mcpServers": {
|
|
22
|
+
"coreguard-agent-commerce": {
|
|
23
|
+
"command": "npx",
|
|
24
|
+
"args": ["-y", "@coreguard/agent-commerce"]
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
`nowSeconds` is always a caller-supplied pinned epoch (unix seconds). The
|
|
31
|
+
server never reads the wall clock into any commitment.
|
|
32
|
+
|
|
33
|
+
## Boundaries (standing)
|
|
34
|
+
|
|
35
|
+
- Synthetic demo data is the caller's; nothing here contacts any commerce
|
|
36
|
+
platform or payment network, and no contact/integration/endorsement is
|
|
37
|
+
claimed with any provider.
|
|
38
|
+
- **VERIFIED ≠ SAFE** — a verdict states that commitments re-derive; it is not
|
|
39
|
+
a guarantee of any broader outcome.
|
|
40
|
+
- Single-use consumption is process-local; durable anchoring is NOT_BUILT.
|
|
41
|
+
- Offline by construction: no network calls, no stored credentials, no keys.
|
|
42
|
+
|
|
43
|
+
License: MIT.
|
package/attest.mjs
ADDED
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CGXA/1 — Cross-Chain Execution Attestation (product 5).
|
|
3
|
+
*
|
|
4
|
+
* One verifiable statement — "this executed order corresponds to the
|
|
5
|
+
* authorization bound in consent record C, and the chain binding it cites is
|
|
6
|
+
* the chain's committed record" — re-derived offline from committed machinery
|
|
7
|
+
* alone. The record binds:
|
|
8
|
+
*
|
|
9
|
+
* consentReceiptId the CGCR/1 consent the execution is attested against
|
|
10
|
+
* executedCommitment the CGEP/1 trace-domain hash of the EXECUTED trace —
|
|
11
|
+
* the same domain verifyExecution re-derives, so an
|
|
12
|
+
* attestation is only mintable for an arc that the
|
|
13
|
+
* family's own verifier would call exact
|
|
14
|
+
* adapter + chainBinding the target chain's binding record, byte-exact
|
|
15
|
+
* against the frozen ADAPTERS registry, whose facts
|
|
16
|
+
* are the COMMITTED on-chain truths cited by name in
|
|
17
|
+
* the repo's own documents (a test pins each fact to
|
|
18
|
+
* the document that holds it)
|
|
19
|
+
* verificationLevel COMMITTED_EVIDENCE — the record cites committed
|
|
20
|
+
* chain truth; it never cites a broadcast this module
|
|
21
|
+
* performed
|
|
22
|
+
*
|
|
23
|
+
* THERE IS NO BROADCAST SURFACE HERE — no signing, no submitting, no network,
|
|
24
|
+
* no keys, no wall clock. A fresh anchor commit or a Soroban call is a NEW
|
|
25
|
+
* on-chain transaction: by repo rule that is owner-authorized only, and this
|
|
26
|
+
* module does not contain the code to attempt it. The adapter verifies
|
|
27
|
+
* committed facts and can PREPARE a Soroban authorized-lane consent binding
|
|
28
|
+
* (`buildSorobanConsentBinding`) as an offline invocation payload; it never
|
|
29
|
+
* performs a broadcast.
|
|
30
|
+
*
|
|
31
|
+
* Consumption semantics: verifyAttestation is an attestation VIEW — it never
|
|
32
|
+
* consumes a consent (single-use consumption stays in verifyExecution's
|
|
33
|
+
* ledger) and it mutates nothing.
|
|
34
|
+
*
|
|
35
|
+
* BOUNDARY (standing): execution verification never decides delivery
|
|
36
|
+
* conformity (DDE/1); VERIFIED ≠ SAFE; the actor is declared, never detected;
|
|
37
|
+
* an attestation of one environment is not bridge trust or a relay claim.
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
import { createHash } from "node:crypto";
|
|
41
|
+
import { domainHash, hashTrace } from "@coreguard/canonical";
|
|
42
|
+
|
|
43
|
+
export const CGXA_VERSION = "CGXA/1";
|
|
44
|
+
|
|
45
|
+
export const CONSENT_SPEC = "CGCR/1";
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Fail-closed reason codes. Every non-verified path lands here by name, and
|
|
49
|
+
* an error inside the verifier itself is a code too — never an exception
|
|
50
|
+
* leaking a pass. The vocabulary is frozen and versioned: a new code lands
|
|
51
|
+
* with its own negative test in the same push.
|
|
52
|
+
*/
|
|
53
|
+
export const CGXA_REJECT_CODES = Object.freeze([
|
|
54
|
+
"MALFORMED_ATTESTATION",
|
|
55
|
+
"ADAPTER_UNKNOWN",
|
|
56
|
+
"CHAIN_BINDING_MISMATCH",
|
|
57
|
+
"LEVEL_MISMATCH",
|
|
58
|
+
"CONSENT_MISMATCH",
|
|
59
|
+
"EXECUTION_MISMATCH",
|
|
60
|
+
"TIME_INVALID",
|
|
61
|
+
"ID_MISMATCH",
|
|
62
|
+
"VERIFIER_ERROR",
|
|
63
|
+
]);
|
|
64
|
+
|
|
65
|
+
export const VERIFICATION_LEVELS = Object.freeze(["COMMITTED_EVIDENCE"]);
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The adapter registry: the chains this surface can attest AGAINST, each
|
|
69
|
+
* carrying only facts that are already committed on-chain truth in this repo
|
|
70
|
+
* (recorded, owner-authorized executions), with the document that holds each
|
|
71
|
+
* fact named in `source`. A test pins every fact literal to its cited
|
|
72
|
+
* document, so the registry cannot drift from the repo's own evidence.
|
|
73
|
+
*
|
|
74
|
+
* No adapter carries a broadcast capability — see the header.
|
|
75
|
+
*/
|
|
76
|
+
export const CGXA_ADAPTERS = Object.freeze([
|
|
77
|
+
Object.freeze({
|
|
78
|
+
id: "core-dao-anchor",
|
|
79
|
+
network: "core-dao-mainnet-1116",
|
|
80
|
+
binding: Object.freeze({
|
|
81
|
+
chainId: "1116",
|
|
82
|
+
anchor: "0x037dF08F2d43c5D03759279Fe35664f6AFf9EA6E",
|
|
83
|
+
}),
|
|
84
|
+
source: "docs/DEPLOYMENT.md",
|
|
85
|
+
note: "LIVE/RECORDED per docs/DEPLOYMENT.md — a NEW anchor commit for a consent receipt is owner-authorized only and is NOT code here",
|
|
86
|
+
}),
|
|
87
|
+
Object.freeze({
|
|
88
|
+
id: "stellar-anchor",
|
|
89
|
+
network: "stellar-mainnet",
|
|
90
|
+
binding: Object.freeze({
|
|
91
|
+
anchorContract: "CCKA2GYVILX7YZVSXGVIHOPNLTRBBF2SSWKBTQBIE7M67N5N2QMMXPSP",
|
|
92
|
+
}),
|
|
93
|
+
source: "docs/EVIDENCE-MATRIX.md",
|
|
94
|
+
note: "EXECUTED deploy + first commit INVOKED per docs/EVIDENCE-MATRIX.md — consent binding carried into the Soroban lane via buildSorobanConsentBinding (offline preparation/invocation payload, no broadcast); any commit_authorized call is owner-authorized only and NOT_PERFORMED",
|
|
95
|
+
}),
|
|
96
|
+
]);
|
|
97
|
+
|
|
98
|
+
const ATTESTATION_FIELDS = Object.freeze([
|
|
99
|
+
"v",
|
|
100
|
+
"consentReceiptId",
|
|
101
|
+
"executedCommitment",
|
|
102
|
+
"adapter",
|
|
103
|
+
"chainBinding",
|
|
104
|
+
"verificationLevel",
|
|
105
|
+
"issuedAtSeconds",
|
|
106
|
+
]);
|
|
107
|
+
|
|
108
|
+
const ADAPTER_FIELDS = Object.freeze(["id", "network"]);
|
|
109
|
+
const HEX64 = /^[0-9a-f]{64}$/;
|
|
110
|
+
|
|
111
|
+
function adapterById(id) {
|
|
112
|
+
return CGXA_ADAPTERS.find((a) => a.id === id) ?? null;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function isNonNegativeInt(n) {
|
|
116
|
+
return typeof n === "number" && Number.isInteger(n) && n >= 0;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function requireConsent(consent) {
|
|
120
|
+
if (!consent || consent.spec !== CONSENT_SPEC || !consent.receiptId || typeof consent.receiptId !== "string") {
|
|
121
|
+
const err = new Error(`CGXA/1: not a ${CONSENT_SPEC} consent record`);
|
|
122
|
+
err.code = "CONSENT_MISMATCH";
|
|
123
|
+
throw err;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Mint the attestation record. Deterministic: the same consent, executed
|
|
129
|
+
* trace, adapter and pinned clock regenerate the record byte-identically.
|
|
130
|
+
*
|
|
131
|
+
* `nowSeconds` is REQUIRED — the wall clock is never read (the C40 rule:
|
|
132
|
+
* every input to a byte-equality claim is pinned, including time).
|
|
133
|
+
*/
|
|
134
|
+
export async function buildAttestation({ consent, executedTrace, adapterId, nowSeconds }) {
|
|
135
|
+
requireConsent(consent);
|
|
136
|
+
if (!executedTrace || typeof executedTrace !== "object" || Array.isArray(executedTrace)) {
|
|
137
|
+
const err = new Error("CGXA/1: buildAttestation requires an executed trace object");
|
|
138
|
+
err.code = "MALFORMED_ATTESTATION";
|
|
139
|
+
throw err;
|
|
140
|
+
}
|
|
141
|
+
const adapter = adapterById(adapterId);
|
|
142
|
+
if (!adapter) {
|
|
143
|
+
const err = new Error(`CGXA/1: unknown adapter id ${JSON.stringify(adapterId)}`);
|
|
144
|
+
err.code = "ADAPTER_UNKNOWN";
|
|
145
|
+
throw err;
|
|
146
|
+
}
|
|
147
|
+
if (!isNonNegativeInt(nowSeconds)) {
|
|
148
|
+
const err = new Error("CGXA/1: buildAttestation requires a pinned non-negative-integer nowSeconds — the wall clock is never read");
|
|
149
|
+
err.code = "TIME_INVALID";
|
|
150
|
+
throw err;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const executedCommitment = await hashTrace(executedTrace);
|
|
154
|
+
|
|
155
|
+
const attestation = Object.freeze({
|
|
156
|
+
v: CGXA_VERSION,
|
|
157
|
+
consentReceiptId: consent.receiptId,
|
|
158
|
+
executedCommitment,
|
|
159
|
+
adapter: Object.freeze({ id: adapter.id, network: adapter.network }),
|
|
160
|
+
chainBinding: Object.freeze({ ...adapter.binding }),
|
|
161
|
+
verificationLevel: "COMMITTED_EVIDENCE",
|
|
162
|
+
issuedAtSeconds: nowSeconds,
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
const attestationId = await domainHash("CGXA/1:RECORD", attestation);
|
|
166
|
+
|
|
167
|
+
return {
|
|
168
|
+
spec: CGXA_VERSION,
|
|
169
|
+
attestation,
|
|
170
|
+
attestationId,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Verify an attestation offline — fail-closed end to end.
|
|
176
|
+
*
|
|
177
|
+
* Re-derives everything: the adapter facts byte-exact against the frozen
|
|
178
|
+
* registry, the executed commitment against the executed trace handed in,
|
|
179
|
+
* the consent id against the consent handed in, the level against the frozen
|
|
180
|
+
* vocabulary, and (when `attestationId` is supplied) the record id against a
|
|
181
|
+
* fresh recomputation. Any mismatch REFUSES with a named code; an error
|
|
182
|
+
* inside the verifier itself REFUSES with VERIFIER_ERROR — never an
|
|
183
|
+
* exception leaking a pass. Consumes nothing.
|
|
184
|
+
*/
|
|
185
|
+
export async function verifyAttestation({ attestation, attestationId, consent, executedTrace }) {
|
|
186
|
+
try {
|
|
187
|
+
if (!attestation || typeof attestation !== "object" || Array.isArray(attestation)) {
|
|
188
|
+
return refused("MALFORMED_ATTESTATION", "attestation is not a record object");
|
|
189
|
+
}
|
|
190
|
+
const fields = Object.keys(attestation).sort();
|
|
191
|
+
if (fields.join(",") !== [...ATTESTATION_FIELDS].sort().join(",")) {
|
|
192
|
+
return refused("MALFORMED_ATTESTATION", `field set must be exactly ${ATTESTATION_FIELDS.join(", ")}`);
|
|
193
|
+
}
|
|
194
|
+
if (attestation.v !== CGXA_VERSION) {
|
|
195
|
+
return refused("MALFORMED_ATTESTATION", `v must be ${CGXA_VERSION}`);
|
|
196
|
+
}
|
|
197
|
+
if (typeof attestation.consentReceiptId !== "string" || !attestation.consentReceiptId) {
|
|
198
|
+
return refused("MALFORMED_ATTESTATION", "consentReceiptId must be a non-empty string");
|
|
199
|
+
}
|
|
200
|
+
if (typeof attestation.executedCommitment !== "string" || !HEX64.test(attestation.executedCommitment.slice(2))) {
|
|
201
|
+
return refused("MALFORMED_ATTESTATION", "executedCommitment must be a 0x + 64-hex digest");
|
|
202
|
+
}
|
|
203
|
+
if (!attestation.adapter || typeof attestation.adapter !== "object") {
|
|
204
|
+
return refused("MALFORMED_ATTESTATION", "adapter must be a record");
|
|
205
|
+
}
|
|
206
|
+
if (Object.keys(attestation.adapter).sort().join(",") !== [...ADAPTER_FIELDS].sort().join(",")) {
|
|
207
|
+
return refused("MALFORMED_ATTESTATION", "adapter fields must be exactly id, network");
|
|
208
|
+
}
|
|
209
|
+
if (!isNonNegativeInt(attestation.issuedAtSeconds)) {
|
|
210
|
+
return refused("TIME_INVALID", "issuedAtSeconds must be a non-negative integer");
|
|
211
|
+
}
|
|
212
|
+
if (!VERIFICATION_LEVELS.includes(attestation.verificationLevel)) {
|
|
213
|
+
return refused("LEVEL_MISMATCH", `verificationLevel must be one of ${VERIFICATION_LEVELS.join(", ")}`);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const adapter = adapterById(attestation.adapter.id);
|
|
217
|
+
if (!adapter) {
|
|
218
|
+
return refused("ADAPTER_UNKNOWN", `no committed adapter with id ${JSON.stringify(attestation.adapter.id)}`);
|
|
219
|
+
}
|
|
220
|
+
if (attestation.adapter.network !== adapter.network) {
|
|
221
|
+
return refused("CHAIN_BINDING_MISMATCH", `network drift: registry ${adapter.network} vs record ${attestation.adapter.network}`);
|
|
222
|
+
}
|
|
223
|
+
const registryKeys = Object.keys(adapter.binding).sort();
|
|
224
|
+
const recordKeys = Object.keys(attestation.chainBinding ?? {}).sort();
|
|
225
|
+
if (registryKeys.join(",") !== recordKeys.join(",")) {
|
|
226
|
+
return refused("CHAIN_BINDING_MISMATCH", `binding fields must be exactly ${registryKeys.join(", ")}`);
|
|
227
|
+
}
|
|
228
|
+
for (const k of registryKeys) {
|
|
229
|
+
if (attestation.chainBinding[k] !== adapter.binding[k]) {
|
|
230
|
+
return refused("CHAIN_BINDING_MISMATCH", `binding fact ${k} drift: registry ${JSON.stringify(adapter.binding[k])} vs record ${JSON.stringify(attestation.chainBinding[k])}`);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
requireConsent(consent);
|
|
235
|
+
if (attestation.consentReceiptId !== consent.receiptId) {
|
|
236
|
+
return refused("CONSENT_MISMATCH", `consent receiptId ${JSON.stringify(consent.receiptId)} does not match the attested ${JSON.stringify(attestation.consentReceiptId)}`);
|
|
237
|
+
}
|
|
238
|
+
if (!executedTrace || typeof executedTrace !== "object" || Array.isArray(executedTrace)) {
|
|
239
|
+
return refused("EXECUTION_MISMATCH", "executed trace is not a record object");
|
|
240
|
+
}
|
|
241
|
+
const recomputedCommitment = await hashTrace(executedTrace);
|
|
242
|
+
if (recomputedCommitment !== attestation.executedCommitment) {
|
|
243
|
+
return refused("EXECUTION_MISMATCH", `executed trace does not re-derive the attested commitment (approved-domain hash drift)`);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (attestationId !== undefined) {
|
|
247
|
+
if (typeof attestationId !== "string") {
|
|
248
|
+
return refused("ID_MISMATCH", "attestationId must be a string when supplied");
|
|
249
|
+
}
|
|
250
|
+
const recomputedId = await domainHash("CGXA/1:RECORD", attestation);
|
|
251
|
+
if (recomputedId !== attestationId) {
|
|
252
|
+
return refused("ID_MISMATCH", `attestation id does not re-derive from the record bytes`);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return {
|
|
257
|
+
decision: "VERIFIED",
|
|
258
|
+
code: null,
|
|
259
|
+
reasons: [],
|
|
260
|
+
attested: {
|
|
261
|
+
adapterId: adapter.id,
|
|
262
|
+
network: adapter.network,
|
|
263
|
+
verificationLevel: attestation.verificationLevel,
|
|
264
|
+
source: adapter.source,
|
|
265
|
+
},
|
|
266
|
+
boundary:
|
|
267
|
+
"attests that this arc's committed chain binding is the record the adapter cites; never bridge trust, never a relay, never delivery conformity (DDE/1); VERIFIED ≠ SAFE",
|
|
268
|
+
};
|
|
269
|
+
} catch (err) {
|
|
270
|
+
// The fail-closed guarantee is "always a REFUSED", never an exception
|
|
271
|
+
// escaping the verifier (the same discipline as the PSAR/1 BLOCK path).
|
|
272
|
+
const code = err && typeof err.code === "string" && CGXA_REJECT_CODES.includes(err.code) ? err.code : "VERIFIER_ERROR";
|
|
273
|
+
return refused(code, `verifier refused: ${err && err.message ? err.message : "unknown error"}`);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function refused(code, reason) {
|
|
278
|
+
if (!CGXA_REJECT_CODES.includes(code)) code = "VERIFIER_ERROR";
|
|
279
|
+
return {
|
|
280
|
+
decision: "REFUSED",
|
|
281
|
+
code,
|
|
282
|
+
reasons: [`${code}: ${reason}`],
|
|
283
|
+
boundary:
|
|
284
|
+
"fail-closed: verification failure is never a pass; the verifier's own failure is a named code too",
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export const SOROBAN_AUTH_DOMAIN = "coreguard-anchor-auth/1";
|
|
289
|
+
export const SOROBAN_CONSENT_BINDING_VERSION = "CGXA-SOROBAN/1";
|
|
290
|
+
|
|
291
|
+
function hexToBytes(hex) {
|
|
292
|
+
const h = typeof hex === "string" && hex.startsWith("0x") ? hex.slice(2) : String(hex ?? "");
|
|
293
|
+
if (!/^[0-9a-fA-F]{64}$/.test(h)) {
|
|
294
|
+
const err = new Error("CGXA/1: Soroban binding hash must be a 0x + 64-hex digest");
|
|
295
|
+
err.code = "MALFORMED_ATTESTATION";
|
|
296
|
+
throw err;
|
|
297
|
+
}
|
|
298
|
+
return Buffer.from(h, "hex");
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Derive the Soroban authorized-anchor id exactly as the contract does:
|
|
303
|
+
* id = sha256( utf8(AUTH_DOMAIN) ++ utf8(strkey(owner)) ++ hash_bytes )
|
|
304
|
+
* (CG-COMMITMENT-ANCHOR/1 authorized lane, byte-for-byte mirror).
|
|
305
|
+
*/
|
|
306
|
+
export function deriveSorobanAuthorizedAnchorId(owner, hash) {
|
|
307
|
+
if (typeof owner !== "string" || owner.length === 0) {
|
|
308
|
+
const err = new Error("CGXA/1: Soroban binding requires an owner strkey string");
|
|
309
|
+
err.code = "MALFORMED_ATTESTATION";
|
|
310
|
+
throw err;
|
|
311
|
+
}
|
|
312
|
+
const hashBytes = hexToBytes(hash);
|
|
313
|
+
const preimage = Buffer.concat([
|
|
314
|
+
Buffer.from(SOROBAN_AUTH_DOMAIN, "utf8"),
|
|
315
|
+
Buffer.from(owner, "utf8"),
|
|
316
|
+
hashBytes,
|
|
317
|
+
]);
|
|
318
|
+
return "0x" + createHash("sha256").update(preimage).digest("hex");
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Carry a consent binding into the Soroban lane (offline preparation only).
|
|
323
|
+
*
|
|
324
|
+
* Builds (or accepts) a CGXA/1 attestation for the consent-execution arc and
|
|
325
|
+
* produces the authorized-lane invocation payload for the Soroban commitment
|
|
326
|
+
* anchor's commit_authorized(owner, hash) - with the anchor id derived
|
|
327
|
+
* in-contract-style from (owner, hash) so identity is bound to the owner and
|
|
328
|
+
* the commitment.
|
|
329
|
+
*
|
|
330
|
+
* The commitment hash anchored is the attestation record id (CGXA/1:RECORD),
|
|
331
|
+
* binding the consent receipt id + executed commitment + chain binding record.
|
|
332
|
+
*
|
|
333
|
+
* NO BROADCAST: preparation data only. Submitting the invocation is a new
|
|
334
|
+
* on-chain transaction, owner-authorized only, and NOT_PERFORMED.
|
|
335
|
+
*/
|
|
336
|
+
export async function buildSorobanConsentBinding({
|
|
337
|
+
consent,
|
|
338
|
+
executedTrace,
|
|
339
|
+
owner,
|
|
340
|
+
nowSeconds,
|
|
341
|
+
adapterId = "stellar-anchor",
|
|
342
|
+
attestation,
|
|
343
|
+
attestationId,
|
|
344
|
+
}) {
|
|
345
|
+
requireConsent(consent);
|
|
346
|
+
if (typeof owner !== "string" || owner.length === 0) {
|
|
347
|
+
const err = new Error("CGXA/1: Soroban consent binding requires an owner strkey");
|
|
348
|
+
err.code = "MALFORMED_ATTESTATION";
|
|
349
|
+
throw err;
|
|
350
|
+
}
|
|
351
|
+
const adapter = adapterById(adapterId);
|
|
352
|
+
if (!adapter || adapter.id !== "stellar-anchor") {
|
|
353
|
+
const err = new Error("CGXA/1: Soroban consent binding requires the stellar-anchor adapter");
|
|
354
|
+
err.code = "ADAPTER_UNKNOWN";
|
|
355
|
+
throw err;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
let record = attestation;
|
|
359
|
+
let recordId = attestationId;
|
|
360
|
+
if (!record) {
|
|
361
|
+
const built = await buildAttestation({ consent, executedTrace, adapterId: "stellar-anchor", nowSeconds });
|
|
362
|
+
record = built.attestation;
|
|
363
|
+
recordId = built.attestationId;
|
|
364
|
+
} else {
|
|
365
|
+
if (typeof recordId !== "string") {
|
|
366
|
+
recordId = await domainHash("CGXA/1:RECORD", record);
|
|
367
|
+
}
|
|
368
|
+
const verdict = await verifyAttestation({ attestation: record, attestationId: recordId, consent, executedTrace });
|
|
369
|
+
if (verdict.decision !== "VERIFIED") {
|
|
370
|
+
const err = new Error("CGXA/1: cannot carry an unverified attestation into the Soroban lane (" + verdict.code + ")");
|
|
371
|
+
err.code = verdict.code || "MALFORMED_ATTESTATION";
|
|
372
|
+
throw err;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
const anchorHash = recordId;
|
|
377
|
+
const authorizedAnchorId = deriveSorobanAuthorizedAnchorId(owner, anchorHash);
|
|
378
|
+
|
|
379
|
+
return {
|
|
380
|
+
spec: SOROBAN_CONSENT_BINDING_VERSION,
|
|
381
|
+
adapterId: adapter.id,
|
|
382
|
+
network: adapter.network,
|
|
383
|
+
chainBinding: Object.freeze({ ...adapter.binding }),
|
|
384
|
+
consentReceiptId: consent.receiptId,
|
|
385
|
+
executedCommitment: record.executedCommitment,
|
|
386
|
+
attestationId: recordId,
|
|
387
|
+
owner,
|
|
388
|
+
commitHash: anchorHash,
|
|
389
|
+
authorizedAnchorId,
|
|
390
|
+
invocation: Object.freeze({
|
|
391
|
+
contract: adapter.binding.anchorContract,
|
|
392
|
+
method: "commit_authorized",
|
|
393
|
+
args: Object.freeze({ owner, hash: anchorHash }),
|
|
394
|
+
}),
|
|
395
|
+
broadcast: "OWNER-GATED / NOT_PERFORMED",
|
|
396
|
+
boundary:
|
|
397
|
+
"offline Soroban consent-binding preparation (CGXA-SOROBAN/1): carries the consent-execution attestation into the Soroban authorized lane as an invocation payload; " +
|
|
398
|
+
"NO broadcast, NO signing, NO network, NO keys - commit_authorized is owner-authorized only and NOT_PERFORMED; " +
|
|
399
|
+
"VERIFIED != SAFE; never bridge trust or a relay",
|
|
400
|
+
};
|
|
401
|
+
}
|
package/bin/mcp.mjs
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* bin wrapper — runs the CoreGuard Agent Commerce stdio MCP server.
|
|
4
|
+
*
|
|
5
|
+
* MCP client configuration (mcpServers):
|
|
6
|
+
* { "command": "npx", "args": ["-y", "@coreguard/agent-commerce"] }
|
|
7
|
+
*
|
|
8
|
+
* Offline by construction: no network calls, no keys, no wall clock.
|
|
9
|
+
*/
|
|
10
|
+
import { main } from "../mcp.mjs";
|
|
11
|
+
|
|
12
|
+
main().catch((err) => {
|
|
13
|
+
console.error(err);
|
|
14
|
+
process.exit(1);
|
|
15
|
+
});
|
package/client.mjs
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CoreGuard Agent Commerce Verification API client (AC-API/1) — REST wrapper.
|
|
3
|
+
*
|
|
4
|
+
* The HTTP face of the Agent Commerce SDK: a fetch-shaped client over the
|
|
5
|
+
* committed `createAgentCommerceApi` handler surface. Every call is honest
|
|
6
|
+
* about what it proves:
|
|
7
|
+
* - health() -> liveness only (the server still applies the allowlist).
|
|
8
|
+
* - authorize() -> POST /v1/authorize -> a CGCR/1 consent record.
|
|
9
|
+
* - verify() -> POST /v1/verify -> fail-closed execution verdict + AGR/1 drift.
|
|
10
|
+
* - consentVerify() -> POST /v1/consent-verify -> the record's own integrity.
|
|
11
|
+
* - riskAssess() -> POST /v1/risk-assess -> deterministic AGR/1 drift report.
|
|
12
|
+
*
|
|
13
|
+
* BOUNDARY (standing): a client is a transport, never a trust root — the
|
|
14
|
+
* verdicts are still the engine's; VERIFIED ≠ SAFE; single-use consumption is
|
|
15
|
+
* tracked by the caller-supplied ledger (default: the client's own), while the
|
|
16
|
+
* authoritative replay refusal remains the engine's. The client never decides
|
|
17
|
+
* delivery conformity (DDE/1); no commerce platform or payment network is
|
|
18
|
+
* contacted or implied.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { AGENT_COMMERCE_API_VERSION } from "./http.mjs";
|
|
22
|
+
import { AGR_VERSION } from "./risk.mjs";
|
|
23
|
+
|
|
24
|
+
const REPLAY_BOUNDARY =
|
|
25
|
+
"deterministic authorization-drift taxonomy (AGR/1) — never a probabilistic fraud score; VERIFIED ≠ SAFE";
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Fetch-shaped client over the committed AC-API/1 handler.
|
|
29
|
+
*
|
|
30
|
+
* @param {object} [options]
|
|
31
|
+
* @param {string} [options.baseUrl="http://127.0.0.1:8787"] - base URL of the AC-API/1 surface.
|
|
32
|
+
* @param {Function} [options.fetchImpl] - injectable transport (tests inject fakes; default: global fetch).
|
|
33
|
+
* @param {Set<string>} [options.consumedSet] - caller ledger for single-use consumption (default: client-owned).
|
|
34
|
+
* @returns {{
|
|
35
|
+
* health: () => Promise<{status: number; ok: boolean; body: unknown}>,
|
|
36
|
+
* authorize: (input: object) => Promise<unknown>,
|
|
37
|
+
* verify: (input: {consent?: unknown; trace?: unknown}) => Promise<unknown>,
|
|
38
|
+
* consentVerify: (input: {consent?: unknown}) => Promise<unknown>,
|
|
39
|
+
* riskAssess: (input: {intent?: unknown; policy?: unknown; trace?: unknown; consumed?: boolean}) => Promise<unknown>,
|
|
40
|
+
* }}
|
|
41
|
+
*/
|
|
42
|
+
export function createAgentCommerceClient({ baseUrl, fetchImpl, consumedSet } = {}) {
|
|
43
|
+
const ledger = consumedSet ?? new Set();
|
|
44
|
+
const doFetch = fetchImpl ?? fetch;
|
|
45
|
+
const base = String(baseUrl ?? "http://127.0.0.1:8787").replace(/\/+$/, "");
|
|
46
|
+
|
|
47
|
+
async function parse(res, method, path) {
|
|
48
|
+
const text = await res.text();
|
|
49
|
+
const json = text ? JSON.parse(text) : null;
|
|
50
|
+
if (!res.ok) {
|
|
51
|
+
const err = new Error(
|
|
52
|
+
`CoreGuard Agent Commerce API ${AGENT_COMMERCE_API_VERSION} ${method} ${path} refused: ${res.status} ${json?.error ?? "request failed"}`
|
|
53
|
+
);
|
|
54
|
+
err.status = res.status;
|
|
55
|
+
throw err;
|
|
56
|
+
}
|
|
57
|
+
return json;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function health() {
|
|
61
|
+
const res = await doFetch(base + "/health", { method: "GET" });
|
|
62
|
+
const text = await res.text();
|
|
63
|
+
let ok = res.ok;
|
|
64
|
+
if (ok) {
|
|
65
|
+
try {
|
|
66
|
+
const json = JSON.parse(text);
|
|
67
|
+
ok = json === "OK";
|
|
68
|
+
} catch {
|
|
69
|
+
ok = text === "OK";
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (!ok) {
|
|
73
|
+
const err = new Error(`CoreGuard Agent Commerce API ${AGENT_COMMERCE_API_VERSION} GET /health refused: ${res.status} ${text}`);
|
|
74
|
+
err.status = res.status;
|
|
75
|
+
throw err;
|
|
76
|
+
}
|
|
77
|
+
return { status: res.status, ok, body: text };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function authorize(input) {
|
|
81
|
+
const payload = input ?? {};
|
|
82
|
+
const json = await parse(
|
|
83
|
+
await doFetch(base + "/v1/authorize", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(payload) }),
|
|
84
|
+
"POST",
|
|
85
|
+
"/v1/authorize"
|
|
86
|
+
);
|
|
87
|
+
return json;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async function verify(input) {
|
|
91
|
+
const { consent, trace } = input ?? {};
|
|
92
|
+
if (consent?.receiptId && ledger.has(String(consent.receiptId))) {
|
|
93
|
+
// Client-side single-use mirror of the engine's authoritative replay refusal.
|
|
94
|
+
return {
|
|
95
|
+
spec: "AC-SDK/1",
|
|
96
|
+
verdict: "INVALID",
|
|
97
|
+
checks: [],
|
|
98
|
+
risk: {
|
|
99
|
+
spec: AGR_VERSION,
|
|
100
|
+
classification: "VIOLATIONS",
|
|
101
|
+
violations: [
|
|
102
|
+
{
|
|
103
|
+
code: "SINGLE_USE_REUSE",
|
|
104
|
+
severity: "HIGH",
|
|
105
|
+
field: "consent.singleUse",
|
|
106
|
+
detail: "this single-use authorization was already consumed by a verified execution",
|
|
107
|
+
},
|
|
108
|
+
],
|
|
109
|
+
boundary: REPLAY_BOUNDARY,
|
|
110
|
+
},
|
|
111
|
+
driftNamed: [],
|
|
112
|
+
consumed: true,
|
|
113
|
+
consumedLedger: String(consent.receiptId),
|
|
114
|
+
boundary: REPLAY_BOUNDARY,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
const report = await parse(
|
|
118
|
+
await doFetch(base + "/v1/verify", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ consent, trace }) }),
|
|
119
|
+
"POST",
|
|
120
|
+
"/v1/verify"
|
|
121
|
+
);
|
|
122
|
+
if (report?.verdict === "VERIFIED" && consent?.receiptId) ledger.add(String(consent.receiptId));
|
|
123
|
+
return report;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async function consentVerify(input) {
|
|
127
|
+
const { consent } = input ?? {};
|
|
128
|
+
const json = await parse(
|
|
129
|
+
await doFetch(base + "/v1/consent-verify", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ consent }) }),
|
|
130
|
+
"POST",
|
|
131
|
+
"/v1/consent-verify"
|
|
132
|
+
);
|
|
133
|
+
return json;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
async function riskAssess(input) {
|
|
137
|
+
const { intent, policy, trace, consumed } = input ?? {};
|
|
138
|
+
const json = await parse(
|
|
139
|
+
await doFetch(base + "/v1/risk-assess", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ intent, policy, trace, consumed: consumed === true }) }),
|
|
140
|
+
"POST",
|
|
141
|
+
"/v1/risk-assess"
|
|
142
|
+
);
|
|
143
|
+
return json;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return { health, authorize, verify, consentVerify, riskAssess };
|
|
147
|
+
}
|