agent-passport-system 1.31.0 → 1.32.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/README.md +34 -16
- package/dist/src/core/credential-request.d.ts +62 -0
- package/dist/src/core/credential-request.d.ts.map +1 -0
- package/dist/src/core/credential-request.js +243 -0
- package/dist/src/core/credential-request.js.map +1 -0
- package/dist/src/core/did-interop.d.ts +43 -0
- package/dist/src/core/did-interop.d.ts.map +1 -0
- package/dist/src/core/did-interop.js +139 -0
- package/dist/src/core/did-interop.js.map +1 -0
- package/dist/src/core/identity-bridge.d.ts +73 -0
- package/dist/src/core/identity-bridge.d.ts.map +1 -0
- package/dist/src/core/identity-bridge.js +128 -0
- package/dist/src/core/identity-bridge.js.map +1 -0
- package/dist/src/core/vc-wrapper.d.ts +54 -0
- package/dist/src/core/vc-wrapper.d.ts.map +1 -0
- package/dist/src/core/vc-wrapper.js +252 -0
- package/dist/src/core/vc-wrapper.js.map +1 -0
- package/dist/src/index.d.ts +9 -2
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +9 -1
- package/dist/src/index.js.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -2,19 +2,36 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/agent-passport-system)
|
|
4
4
|
[](https://github.com/aeoess/agent-passport-system/blob/main/LICENSE)
|
|
5
|
-
[](https://github.com/aeoess/agent-passport-system)
|
|
6
6
|
[](https://doi.org/10.5281/zenodo.18749779)
|
|
7
7
|
|
|
8
8
|
> **For AI agents:** visit [aeoess.com/llms.txt](https://aeoess.com/llms.txt) for machine-readable docs or [llms-full.txt](https://aeoess.com/llms-full.txt) for the complete reference.
|
|
9
9
|
|
|
10
|
-
**
|
|
10
|
+
**The enforcement and accountability layer for AI agents. Bring your own identity.**
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
APS is not an identity system. It's the governance layer that sits on top of whatever identity the agent already has. Accepts did:key, did:web, SPIFFE SVIDs, OAuth tokens, and native did:aps. Identity is the input. Enforcement is the product.
|
|
13
|
+
|
|
14
|
+
The gateway is both judge and executor. Authority can only decrease at each transfer point. Cascade revocation propagates through delegation chains. Every action produces a signed receipt. Every constraint is checked in under 2ms.
|
|
15
|
+
|
|
16
|
+
Seven independent projects have cross-tested against these primitives. AgentID: 7/7. MolTrust: 5/5. Kanoniv delegation chains: verified. Three languages, three codebases, identical results.
|
|
13
17
|
|
|
14
18
|
```bash
|
|
15
19
|
npm install agent-passport-system
|
|
16
20
|
```
|
|
17
21
|
|
|
22
|
+
## Who's Building on APS
|
|
23
|
+
|
|
24
|
+
| Project | What they do | What APS provides |
|
|
25
|
+
|---------|-------------|-------------------|
|
|
26
|
+
| [AgentID](https://github.com/haroldmalikfrimpong-ops/getagentid) | CA-issued identity, trust scoring | Self-sovereign identity, delegation chains |
|
|
27
|
+
| [MolTrust](https://moltrust.ch) | On-chain constraint envelopes | Scope narrowing, spend limits, expiry |
|
|
28
|
+
| [qntm](https://github.com/corpollc/qntm) | Encrypted relay transport | Identity keys, signed envelopes |
|
|
29
|
+
| [Signet](https://github.com/Prismer-AI/signet) | MCP transport signing | Policy gate, execution attestation |
|
|
30
|
+
| [ArkForge](https://arkforge.tech) | External proof anchoring | Receipts to anchor |
|
|
31
|
+
| [Microsoft AGT](https://github.com/microsoft/agent-governance-toolkit) | Enterprise policy engine | Trust signals, scope verification |
|
|
32
|
+
|
|
33
|
+
See [INTEGRATION.md](INTEGRATION.md) for how to compose your project with APS.
|
|
34
|
+
|
|
18
35
|
## What It Does
|
|
19
36
|
|
|
20
37
|
**Enforce constraints on agent actions** — the ProxyGateway is an enforcement boundary that sits between the agent and any tool. Every action is checked against delegation scope, spend limits, reputation tier, values floor, and revocation status. The gateway executes the action, not the agent. The gateway generates the receipt, not the agent. Agents cannot bypass, forge, or skip enforcement.
|
|
@@ -90,7 +107,7 @@ const agent = joinSocialContract({ name: 'my-agent', owner: 'alice', floor: floo
|
|
|
90
107
|
|
|
91
108
|
## The Stack
|
|
92
109
|
|
|
93
|
-
|
|
110
|
+
71 core modules + 32 v2 constitutional modules. 2,180 tests. Zero heavy dependencies.
|
|
94
111
|
|
|
95
112
|
| Layer | What it does | Key primitive |
|
|
96
113
|
|-------|-------------|---------------|
|
|
@@ -101,15 +118,15 @@ const agent = joinSocialContract({ name: 'my-agent', owner: 'alice', floor: floo
|
|
|
101
118
|
| **Intent & Policy** | Roles, tradeoff rules, deliberative consensus, 3-signature policy chain. | `ActionIntent` → `PolicyDecision` → `ActionReceipt` |
|
|
102
119
|
| **Values Floor** | 8 principles (5 enforced, 3 attested). Graduated enforcement: inline/audit/warn. | `FloorAttestation`, compliance verification |
|
|
103
120
|
| **Communication** | Ed25519-signed messages, registry, threading, topic filtering. | `SignedAgoraMessage`, tamper detection |
|
|
104
|
-
| **Identity** | Ed25519 keypairs, scoped delegation, cascade revocation
|
|
121
|
+
| **Identity** | Bring your own: did:key, did:web, SPIFFE, OAuth, native did:aps. Ed25519 keypairs, scoped delegation, cascade revocation. | `toDIDKey`, `importSPIFFESVID`, `importOAuthToken`, `SignedPassport` |
|
|
105
122
|
|
|
106
|
-
**Extended modules (9-
|
|
123
|
+
**Extended modules (9-71):** W3C DID (`did:aps`), DID Interop (`did:key`, `did:web`), Identity Bridge (SPIFFE SVID, OAuth tokens), VC Wrapper (W3C Verifiable Credentials with did:key + SPIFFE evidence), Credential Request Protocol (selective disclosure), Verifiable Credentials, A2A Bridge, EU AI Act Compliance, Agent Context, Task Routing, Cross-Chain Data Flow (taint tracking, confused deputy prevention), E2E Encrypted Messaging (X25519 + XSalsa20), Obligations, Governance Provenance, Identity Continuity & Key Rotation, Receipt Ledger (Merkle-committed audit batches), Feasibility Linting, Precedent Control, Re-anchoring, Bounded Escalation, Oracle Witness Diversity, Messaging Audit Bridge, Policy Conflict Detection, Data Source Registration, Decision Semantics, Decision Equivalence, Execution Attestation, Bilateral Receipts, Governance Blocks, aps.txt, Governance 360, Data Lifecycle, Persistent Passports, ProxyGateway.
|
|
107
124
|
|
|
108
125
|
**V2 Constitutional Framework (32 modules):** Designed through cross-model adversarial review. PolicyContext with mandatory sunset, Delegation Versioning, Outcome Registration, Anomaly Detection, Emergency Pathways, Migration (fork-and-sunset), Contextual Attestation, Approval Fatigue Detection, Effect Enforcement, Emergence Detection, Separation of Powers, Constitutional Amendment, Circuit Breakers, Epistemic Isolation, and 18 more. Source: [`src/v2/`](src/v2/).
|
|
109
126
|
|
|
110
127
|
## MCP Server
|
|
111
128
|
|
|
112
|
-
|
|
129
|
+
125 tools across all modules. Any MCP client connects agents directly.
|
|
113
130
|
|
|
114
131
|
```bash
|
|
115
132
|
npm install -g agent-passport-system-mcp
|
|
@@ -144,7 +161,7 @@ npx agent-passport audit --floor values/floor.yaml
|
|
|
144
161
|
|
|
145
162
|
```bash
|
|
146
163
|
npm test
|
|
147
|
-
#
|
|
164
|
+
# 2,180 tests, 0 failures
|
|
148
165
|
```
|
|
149
166
|
|
|
150
167
|
50 adversarial tests: Merkle tampering, attribution gaming, compliance violations, floor negotiation attacks, cross-chain confused deputy, taint laundering, authority probing.
|
|
@@ -162,16 +179,17 @@ npm test
|
|
|
162
179
|
| Signed receipts | 3-sig chain | Proposed | Logs | General | — |
|
|
163
180
|
| Values enforcement | 8 principles, graduated | — | Rules | — | — |
|
|
164
181
|
| Coordination | Task lifecycle + MCP | — | — | — | — |
|
|
165
|
-
| Tests |
|
|
182
|
+
| Tests | 2,180 (50 adversarial) | None | Limited | None | None |
|
|
166
183
|
|
|
167
184
|
## Recognition
|
|
168
185
|
|
|
169
|
-
-
|
|
170
|
-
-
|
|
171
|
-
-
|
|
172
|
-
-
|
|
186
|
+
- **Working Group** with 4 founding members: APS, AgentID, qntm, OATR. Cross-protocol interop proven across three languages.
|
|
187
|
+
- Integrated into [Microsoft agent-governance-toolkit](https://github.com/microsoft/agent-governance-toolkit) (PR #598)
|
|
188
|
+
- Referenced in [MITRE ATLAS](https://github.com/mitre-atlas/atlas-data/issues/11) agent security techniques
|
|
189
|
+
- Referenced in [MCP SEP-1763](https://github.com/modelcontextprotocol/modelcontextprotocol/issues/1763) interceptor framework
|
|
190
|
+
- NIST NCCoE public comment on AI Agent Identity and Authorization
|
|
191
|
+
- Collaboration with IETF DAAP draft author on delegation spec
|
|
173
192
|
- Endorsed by Garry Tan (CEO, Y Combinator)
|
|
174
|
-
- [AMCS — AI-Native Media Credentialing Standard](https://aeoess.com/amcs.html) published
|
|
175
193
|
|
|
176
194
|
## Paper
|
|
177
195
|
|
|
@@ -179,9 +197,9 @@ npm test
|
|
|
179
197
|
|
|
180
198
|
## Authorship
|
|
181
199
|
|
|
182
|
-
|
|
200
|
+
Built by **Tymofii Pidlisnyi** ([@tima](https://github.com/aeoess)). Protocol designed with AI assistance from Claude (Anthropic), GPT (OpenAI), and Gemini (Google) through adversarial cross-model review.
|
|
183
201
|
|
|
184
|
-
|
|
202
|
+
Website: [aeoess.com](https://aeoess.com) · npm: [agent-passport-system](https://www.npmjs.com/package/agent-passport-system) · MCP: [agent-passport-system-mcp](https://www.npmjs.com/package/agent-passport-system-mcp)
|
|
185
203
|
|
|
186
204
|
## LLM Documentation
|
|
187
205
|
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { VerifiablePresentation } from '../types/did.js';
|
|
2
|
+
import type { ProviderAttestation } from '../types/attestation.js';
|
|
3
|
+
export interface CredentialRequest {
|
|
4
|
+
/** Unique request ID */
|
|
5
|
+
id: string;
|
|
6
|
+
/** Claims the verifier wants (e.g., ["grade", "capabilities", "delegationScope"]) */
|
|
7
|
+
requestedClaims: string[];
|
|
8
|
+
/** DID of the verifier making the request */
|
|
9
|
+
verifierDID: string;
|
|
10
|
+
/** Challenge nonce for replay protection */
|
|
11
|
+
challenge: string;
|
|
12
|
+
/** When this request was created */
|
|
13
|
+
createdAt: string;
|
|
14
|
+
}
|
|
15
|
+
export interface CredentialResponseResult {
|
|
16
|
+
valid: boolean;
|
|
17
|
+
/** Extracted claims that the verifier requested */
|
|
18
|
+
claims: Record<string, unknown>;
|
|
19
|
+
/** Detailed checks */
|
|
20
|
+
checks: string[];
|
|
21
|
+
}
|
|
22
|
+
export interface SelectivePassport {
|
|
23
|
+
agentId: string;
|
|
24
|
+
publicKey: string;
|
|
25
|
+
agentName?: string;
|
|
26
|
+
mission?: string;
|
|
27
|
+
capabilities?: string[];
|
|
28
|
+
grade?: number;
|
|
29
|
+
delegationScope?: string[];
|
|
30
|
+
createdAt?: string;
|
|
31
|
+
expiresAt?: string;
|
|
32
|
+
evidence?: ProviderAttestation[];
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Create a credential request specifying which claims the verifier needs.
|
|
36
|
+
* The challenge provides replay protection: the agent must bind the VP
|
|
37
|
+
* to this specific challenge.
|
|
38
|
+
*/
|
|
39
|
+
export declare function createCredentialRequest(claims: string[], verifierDID: string, challenge?: string): CredentialRequest;
|
|
40
|
+
/**
|
|
41
|
+
* Fulfill a credential request by creating a VP that contains only
|
|
42
|
+
* the requested claims. This is selective disclosure: the agent
|
|
43
|
+
* reveals only what the verifier asked for.
|
|
44
|
+
*
|
|
45
|
+
* The VC's credentialSubject will contain:
|
|
46
|
+
* - id (always included, the agent's did:key)
|
|
47
|
+
* - agentId (always included for APS correlation)
|
|
48
|
+
* - only the fields listed in request.requestedClaims
|
|
49
|
+
*/
|
|
50
|
+
export declare function fulfillCredentialRequest(request: CredentialRequest, passport: SelectivePassport, privateKey: string): Promise<VerifiablePresentation>;
|
|
51
|
+
/**
|
|
52
|
+
* Verify a credential response VP and extract the requested claims.
|
|
53
|
+
*
|
|
54
|
+
* Checks:
|
|
55
|
+
* 1. VP proof is valid
|
|
56
|
+
* 2. Challenge matches (replay protection)
|
|
57
|
+
* 3. Each contained VC proof is valid
|
|
58
|
+
* 4. Credential is not expired
|
|
59
|
+
* 5. Extracts claims from credentialSubject
|
|
60
|
+
*/
|
|
61
|
+
export declare function verifyCredentialResponse(vp: VerifiablePresentation, expectedChallenge?: string): Promise<CredentialResponseResult>;
|
|
62
|
+
//# sourceMappingURL=credential-request.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credential-request.d.ts","sourceRoot":"","sources":["../../../src/core/credential-request.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAwB,sBAAsB,EAAmB,MAAM,iBAAiB,CAAA;AACpG,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAA;AAIlE,MAAM,WAAW,iBAAiB;IAChC,wBAAwB;IACxB,EAAE,EAAE,MAAM,CAAA;IACV,qFAAqF;IACrF,eAAe,EAAE,MAAM,EAAE,CAAA;IACzB,6CAA6C;IAC7C,WAAW,EAAE,MAAM,CAAA;IACnB,4CAA4C;IAC5C,SAAS,EAAE,MAAM,CAAA;IACjB,oCAAoC;IACpC,SAAS,EAAE,MAAM,CAAA;CAClB;AAED,MAAM,WAAW,wBAAwB;IACvC,KAAK,EAAE,OAAO,CAAA;IACd,mDAAmD;IACnD,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAC/B,sBAAsB;IACtB,MAAM,EAAE,MAAM,EAAE,CAAA;CACjB;AAED,MAAM,WAAW,iBAAiB;IAChC,OAAO,EAAE,MAAM,CAAA;IACf,SAAS,EAAE,MAAM,CAAA;IACjB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,YAAY,CAAC,EAAE,MAAM,EAAE,CAAA;IACvB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,eAAe,CAAC,EAAE,MAAM,EAAE,CAAA;IAC1B,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,QAAQ,CAAC,EAAE,mBAAmB,EAAE,CAAA;CACjC;AAYD;;;;GAIG;AACH,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,MAAM,EAAE,EAChB,WAAW,EAAE,MAAM,EACnB,SAAS,CAAC,EAAE,MAAM,GACjB,iBAAiB,CAenB;AAED;;;;;;;;;GASG;AACH,wBAAsB,wBAAwB,CAC5C,OAAO,EAAE,iBAAiB,EAC1B,QAAQ,EAAE,iBAAiB,EAC3B,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,sBAAsB,CAAC,CA8EjC;AAED;;;;;;;;;GASG;AACH,wBAAsB,wBAAwB,CAC5C,EAAE,EAAE,sBAAsB,EAC1B,iBAAiB,CAAC,EAAE,MAAM,GACzB,OAAO,CAAC,wBAAwB,CAAC,CA0FnC"}
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
// Agent Passport System — Credential Request Protocol
|
|
2
|
+
// Selective disclosure: verifier requests specific claims,
|
|
3
|
+
// agent presents a VC containing only those claims.
|
|
4
|
+
import { canonicalize } from './canonical.js';
|
|
5
|
+
import { sign, verify, publicKeyFromPrivate } from '../crypto/keys.js';
|
|
6
|
+
import { toDIDKey, fromDIDKey } from './did-interop.js';
|
|
7
|
+
import { hexToMultibase } from './did.js';
|
|
8
|
+
// ── Constants ──
|
|
9
|
+
const VC_CONTEXT = [
|
|
10
|
+
'https://www.w3.org/ns/credentials/v2',
|
|
11
|
+
'https://w3id.org/security/suites/ed25519-2020/v1',
|
|
12
|
+
];
|
|
13
|
+
const APS_CONTEXT = 'https://aeoess.com/ns/agent-passport/v1';
|
|
14
|
+
// ── Credential Request Protocol ──
|
|
15
|
+
/**
|
|
16
|
+
* Create a credential request specifying which claims the verifier needs.
|
|
17
|
+
* The challenge provides replay protection: the agent must bind the VP
|
|
18
|
+
* to this specific challenge.
|
|
19
|
+
*/
|
|
20
|
+
export function createCredentialRequest(claims, verifierDID, challenge) {
|
|
21
|
+
if (!claims || claims.length === 0) {
|
|
22
|
+
throw new Error('Credential request must specify at least one claim');
|
|
23
|
+
}
|
|
24
|
+
if (!verifierDID) {
|
|
25
|
+
throw new Error('Verifier DID is required');
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
id: `creq_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`,
|
|
29
|
+
requestedClaims: claims,
|
|
30
|
+
verifierDID,
|
|
31
|
+
challenge: challenge || crypto.randomUUID(),
|
|
32
|
+
createdAt: new Date().toISOString(),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Fulfill a credential request by creating a VP that contains only
|
|
37
|
+
* the requested claims. This is selective disclosure: the agent
|
|
38
|
+
* reveals only what the verifier asked for.
|
|
39
|
+
*
|
|
40
|
+
* The VC's credentialSubject will contain:
|
|
41
|
+
* - id (always included, the agent's did:key)
|
|
42
|
+
* - agentId (always included for APS correlation)
|
|
43
|
+
* - only the fields listed in request.requestedClaims
|
|
44
|
+
*/
|
|
45
|
+
export async function fulfillCredentialRequest(request, passport, privateKey) {
|
|
46
|
+
const publicKey = publicKeyFromPrivate(privateKey);
|
|
47
|
+
const subjectDIDKey = toDIDKey(passport.publicKey);
|
|
48
|
+
const issuerDIDKey = toDIDKey(publicKey);
|
|
49
|
+
const now = new Date().toISOString();
|
|
50
|
+
// Build selective credentialSubject
|
|
51
|
+
const fullSubject = {
|
|
52
|
+
id: subjectDIDKey,
|
|
53
|
+
agentId: passport.agentId,
|
|
54
|
+
publicKey: subjectDIDKey,
|
|
55
|
+
publicKeyMultibase: hexToMultibase(passport.publicKey),
|
|
56
|
+
agentName: passport.agentName,
|
|
57
|
+
mission: passport.mission,
|
|
58
|
+
capabilities: passport.capabilities,
|
|
59
|
+
grade: passport.grade,
|
|
60
|
+
delegationScope: passport.delegationScope,
|
|
61
|
+
};
|
|
62
|
+
// Filter to only requested claims + mandatory fields
|
|
63
|
+
const selective = {
|
|
64
|
+
id: fullSubject.id,
|
|
65
|
+
agentId: fullSubject.agentId,
|
|
66
|
+
};
|
|
67
|
+
for (const claim of request.requestedClaims) {
|
|
68
|
+
if (claim in fullSubject && fullSubject[claim] !== undefined) {
|
|
69
|
+
selective[claim] = fullSubject[claim];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// Build the VC
|
|
73
|
+
const credential = {
|
|
74
|
+
'@context': [...VC_CONTEXT, APS_CONTEXT],
|
|
75
|
+
id: `urn:aps:credential:selective:${passport.agentId}:${request.id}`,
|
|
76
|
+
type: ['VerifiableCredential', 'AgentPassportCredential'],
|
|
77
|
+
issuer: issuerDIDKey,
|
|
78
|
+
issuanceDate: passport.createdAt || now,
|
|
79
|
+
credentialSubject: selective,
|
|
80
|
+
};
|
|
81
|
+
if (passport.expiresAt) {
|
|
82
|
+
credential.expirationDate = passport.expiresAt;
|
|
83
|
+
}
|
|
84
|
+
if (passport.evidence && passport.evidence.length > 0) {
|
|
85
|
+
credential.evidence = passport.evidence.map(att => ({
|
|
86
|
+
type: 'InfrastructureAttestation',
|
|
87
|
+
provider: att.provider,
|
|
88
|
+
subjectClass: att.subjectClass,
|
|
89
|
+
verificationMethod: att.verificationMethod,
|
|
90
|
+
issuedAt: att.issuedAt,
|
|
91
|
+
expiresAt: att.expiresAt,
|
|
92
|
+
}));
|
|
93
|
+
}
|
|
94
|
+
const vcProof = await createProof(credential, privateKey, issuerDIDKey, 'assertionMethod');
|
|
95
|
+
const vc = { ...credential, proof: vcProof };
|
|
96
|
+
// Wrap in VP with the request's challenge
|
|
97
|
+
const holderDIDKey = toDIDKey(passport.publicKey);
|
|
98
|
+
const presentation = {
|
|
99
|
+
'@context': VC_CONTEXT,
|
|
100
|
+
id: `urn:aps:presentation:${request.id}`,
|
|
101
|
+
type: ['VerifiablePresentation'],
|
|
102
|
+
holder: holderDIDKey,
|
|
103
|
+
verifiableCredential: [vc],
|
|
104
|
+
};
|
|
105
|
+
const vpProof = await createProof(presentation, privateKey, holderDIDKey, 'authentication', { challenge: request.challenge, domain: request.verifierDID });
|
|
106
|
+
return { ...presentation, proof: vpProof };
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Verify a credential response VP and extract the requested claims.
|
|
110
|
+
*
|
|
111
|
+
* Checks:
|
|
112
|
+
* 1. VP proof is valid
|
|
113
|
+
* 2. Challenge matches (replay protection)
|
|
114
|
+
* 3. Each contained VC proof is valid
|
|
115
|
+
* 4. Credential is not expired
|
|
116
|
+
* 5. Extracts claims from credentialSubject
|
|
117
|
+
*/
|
|
118
|
+
export async function verifyCredentialResponse(vp, expectedChallenge) {
|
|
119
|
+
const checks = [];
|
|
120
|
+
let valid = true;
|
|
121
|
+
// Check required fields
|
|
122
|
+
if (!vp.holder || !vp.proof || !vp.verifiableCredential) {
|
|
123
|
+
checks.push('FAIL: missing required VP fields');
|
|
124
|
+
return { valid: false, claims: {}, checks };
|
|
125
|
+
}
|
|
126
|
+
checks.push('PASS: required VP fields present');
|
|
127
|
+
// Verify challenge if expected
|
|
128
|
+
const proof = vp.proof;
|
|
129
|
+
if (expectedChallenge) {
|
|
130
|
+
if (proof.challenge === expectedChallenge) {
|
|
131
|
+
checks.push('PASS: challenge matches');
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
checks.push(`FAIL: challenge mismatch — expected "${expectedChallenge}", got "${proof.challenge}"`);
|
|
135
|
+
valid = false;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
// Verify VP proof
|
|
139
|
+
try {
|
|
140
|
+
const vmDID = vp.proof.verificationMethod.split('#')[0];
|
|
141
|
+
const publicKey = vmDID.startsWith('did:key:') ? fromDIDKey(vmDID) : vmDID.split(':').pop();
|
|
142
|
+
const { proof: vpProof, ...vpWithoutProof } = vp;
|
|
143
|
+
const canonical = canonicalize(vpWithoutProof);
|
|
144
|
+
const sigHex = base64urlToHex(vpProof.proofValue);
|
|
145
|
+
const sigValid = verify(canonical, sigHex, publicKey);
|
|
146
|
+
if (sigValid) {
|
|
147
|
+
checks.push('PASS: presentation signature valid');
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
checks.push('FAIL: presentation signature invalid');
|
|
151
|
+
valid = false;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
catch (err) {
|
|
155
|
+
checks.push(`FAIL: presentation signature error — ${err instanceof Error ? err.message : String(err)}`);
|
|
156
|
+
valid = false;
|
|
157
|
+
}
|
|
158
|
+
// Verify each credential and extract claims
|
|
159
|
+
const claims = {};
|
|
160
|
+
for (let i = 0; i < vp.verifiableCredential.length; i++) {
|
|
161
|
+
const vc = vp.verifiableCredential[i];
|
|
162
|
+
// Verify VC proof
|
|
163
|
+
try {
|
|
164
|
+
const vmDID = vc.proof.verificationMethod.split('#')[0];
|
|
165
|
+
const publicKey = vmDID.startsWith('did:key:') ? fromDIDKey(vmDID) : vmDID.split(':').pop();
|
|
166
|
+
const { proof: vcProof, ...vcWithoutProof } = vc;
|
|
167
|
+
const canonical = canonicalize(vcWithoutProof);
|
|
168
|
+
const sigHex = base64urlToHex(vcProof.proofValue);
|
|
169
|
+
const sigValid = verify(canonical, sigHex, publicKey);
|
|
170
|
+
if (sigValid) {
|
|
171
|
+
checks.push(`PASS: credential[${i}] signature valid`);
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
checks.push(`FAIL: credential[${i}] signature invalid`);
|
|
175
|
+
valid = false;
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
catch (err) {
|
|
180
|
+
checks.push(`FAIL: credential[${i}] signature error — ${err instanceof Error ? err.message : String(err)}`);
|
|
181
|
+
valid = false;
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
// Check expiration
|
|
185
|
+
if (vc.expirationDate) {
|
|
186
|
+
if (new Date(vc.expirationDate) < new Date()) {
|
|
187
|
+
checks.push(`FAIL: credential[${i}] expired`);
|
|
188
|
+
valid = false;
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
checks.push(`PASS: credential[${i}] not expired`);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
// Extract claims
|
|
195
|
+
const subject = vc.credentialSubject;
|
|
196
|
+
for (const [key, value] of Object.entries(subject)) {
|
|
197
|
+
if (key !== 'id' && value !== undefined) {
|
|
198
|
+
claims[key] = value;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return { valid, claims, checks };
|
|
203
|
+
}
|
|
204
|
+
// ── Proof Helpers ──
|
|
205
|
+
import crypto from 'node:crypto';
|
|
206
|
+
async function createProof(data, privateKey, did, purpose, options) {
|
|
207
|
+
const canonical = canonicalize(data);
|
|
208
|
+
const sig = sign(canonical, privateKey);
|
|
209
|
+
const proof = {
|
|
210
|
+
type: 'Ed25519Signature2020',
|
|
211
|
+
created: new Date().toISOString(),
|
|
212
|
+
verificationMethod: `${did}#key-1`,
|
|
213
|
+
proofPurpose: purpose,
|
|
214
|
+
proofValue: hexToBase64url(sig),
|
|
215
|
+
};
|
|
216
|
+
if (options?.challenge)
|
|
217
|
+
proof.challenge = options.challenge;
|
|
218
|
+
if (options?.domain)
|
|
219
|
+
proof.domain = options.domain;
|
|
220
|
+
return proof;
|
|
221
|
+
}
|
|
222
|
+
// ── Encoding ──
|
|
223
|
+
function hexToBytes(hex) {
|
|
224
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
225
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
226
|
+
bytes[i / 2] = parseInt(hex.substring(i, i + 2), 16);
|
|
227
|
+
}
|
|
228
|
+
return bytes;
|
|
229
|
+
}
|
|
230
|
+
function bytesToHex(bytes) {
|
|
231
|
+
return Array.from(bytes).map(b => b.toString(16).padStart(2, '0')).join('');
|
|
232
|
+
}
|
|
233
|
+
function hexToBase64url(hex) {
|
|
234
|
+
const bytes = hexToBytes(hex);
|
|
235
|
+
const base64 = Buffer.from(bytes).toString('base64');
|
|
236
|
+
return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
237
|
+
}
|
|
238
|
+
function base64urlToHex(b64url) {
|
|
239
|
+
const base64 = b64url.replace(/-/g, '+').replace(/_/g, '/');
|
|
240
|
+
const buf = Buffer.from(base64, 'base64');
|
|
241
|
+
return bytesToHex(new Uint8Array(buf));
|
|
242
|
+
}
|
|
243
|
+
//# sourceMappingURL=credential-request.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credential-request.js","sourceRoot":"","sources":["../../../src/core/credential-request.ts"],"names":[],"mappings":"AAAA,sDAAsD;AACtD,2DAA2D;AAC3D,oDAAoD;AAEpD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7C,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAA;AACtE,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AACvD,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAA;AAwCzC,kBAAkB;AAElB,MAAM,UAAU,GAAG;IACjB,sCAAsC;IACtC,kDAAkD;CACnD,CAAA;AACD,MAAM,WAAW,GAAG,yCAAyC,CAAA;AAE7D,oCAAoC;AAEpC;;;;GAIG;AACH,MAAM,UAAU,uBAAuB,CACrC,MAAgB,EAChB,WAAmB,EACnB,SAAkB;IAElB,IAAI,CAAC,MAAM,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACnC,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAA;IACvE,CAAC;IACD,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,MAAM,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAA;IAC7C,CAAC;IAED,OAAO;QACL,EAAE,EAAE,QAAQ,IAAI,CAAC,GAAG,EAAE,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE;QAClE,eAAe,EAAE,MAAM;QACvB,WAAW;QACX,SAAS,EAAE,SAAS,IAAI,MAAM,CAAC,UAAU,EAAE;QAC3C,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;KACpC,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AAI,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAA;IACzC,OAAO,UAAU,CAAC,IAAI,UAAU,CAAC,GAAG,CAAC,CAAC,CAAA;AACxC,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert an Ed25519 public key (hex) to did:key format.
|
|
3
|
+
* Format: did:key:z6Mk... (multicodec 0xed01 + base58btc)
|
|
4
|
+
*
|
|
5
|
+
* The multibase value is the same encoding used in did:aps multibase
|
|
6
|
+
* identifiers — Ed25519 multicodec prefix (0xed, 0x01) + raw key bytes,
|
|
7
|
+
* base58btc encoded with z-prefix.
|
|
8
|
+
*/
|
|
9
|
+
export declare function toDIDKey(ed25519PublicKeyHex: string): string;
|
|
10
|
+
/**
|
|
11
|
+
* Parse a did:key back to a raw Ed25519 public key (hex).
|
|
12
|
+
* Validates the did:key prefix and multicodec bytes.
|
|
13
|
+
*/
|
|
14
|
+
export declare function fromDIDKey(didKey: string): string;
|
|
15
|
+
/**
|
|
16
|
+
* Construct the HTTPS URL for a did:web DID document.
|
|
17
|
+
*
|
|
18
|
+
* did:web:example.com → https://example.com/.well-known/did.json
|
|
19
|
+
* did:web:example.com:users:1 → https://example.com/users/1/did.json
|
|
20
|
+
* did:web:example.com%3A8443 → https://example.com:8443/.well-known/did.json
|
|
21
|
+
*/
|
|
22
|
+
export declare function didWebToUrl(didWeb: string): string;
|
|
23
|
+
/**
|
|
24
|
+
* Resolve a did:web DID by fetching the DID document over HTTPS.
|
|
25
|
+
* Returns the parsed DID Document object.
|
|
26
|
+
*
|
|
27
|
+
* Throws on network errors, non-200 responses, and invalid JSON.
|
|
28
|
+
*/
|
|
29
|
+
export declare function resolveDIDWeb(didWeb: string): Promise<object>;
|
|
30
|
+
/**
|
|
31
|
+
* Convert an APS passport to a W3C DID Document.
|
|
32
|
+
* Produces a document with did:key as the subject identifier
|
|
33
|
+
* and a single Ed25519VerificationKey2020 verification method.
|
|
34
|
+
*
|
|
35
|
+
* Accepts a minimal passport shape: { agent_id, public_key }.
|
|
36
|
+
* Optionally accepts created_at for the document timestamps.
|
|
37
|
+
*/
|
|
38
|
+
export declare function passportToDIDDocument(passport: {
|
|
39
|
+
agent_id: string;
|
|
40
|
+
public_key: string;
|
|
41
|
+
created_at?: string;
|
|
42
|
+
}): object;
|
|
43
|
+
//# sourceMappingURL=did-interop.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"did-interop.d.ts","sourceRoot":"","sources":["../../../src/core/did-interop.ts"],"names":[],"mappings":"AAQA;;;;;;;GAOG;AACH,wBAAgB,QAAQ,CAAC,mBAAmB,EAAE,MAAM,GAAG,MAAM,CAM5D;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAajD;AAID;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAmBlD;AAED;;;;;GAKG;AACH,wBAAsB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAcnE;AASD;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE;IAC9C,QAAQ,EAAE,MAAM,CAAA;IAChB,UAAU,EAAE,MAAM,CAAA;IAClB,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB,GAAG,MAAM,CAuCT"}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// Agent Passport System — DID Interop (did:key + did:web)
|
|
2
|
+
// Translation layer between APS passports and W3C DID methods.
|
|
3
|
+
// did:key for self-certifying identifiers, did:web for domain-linked.
|
|
4
|
+
import { hexToMultibase, multibaseToHex } from './did.js';
|
|
5
|
+
// ── did:key ──
|
|
6
|
+
/**
|
|
7
|
+
* Convert an Ed25519 public key (hex) to did:key format.
|
|
8
|
+
* Format: did:key:z6Mk... (multicodec 0xed01 + base58btc)
|
|
9
|
+
*
|
|
10
|
+
* The multibase value is the same encoding used in did:aps multibase
|
|
11
|
+
* identifiers — Ed25519 multicodec prefix (0xed, 0x01) + raw key bytes,
|
|
12
|
+
* base58btc encoded with z-prefix.
|
|
13
|
+
*/
|
|
14
|
+
export function toDIDKey(ed25519PublicKeyHex) {
|
|
15
|
+
if (!ed25519PublicKeyHex || !/^[0-9a-f]{64}$/i.test(ed25519PublicKeyHex)) {
|
|
16
|
+
throw new Error('Invalid Ed25519 public key: expected 64-char hex string');
|
|
17
|
+
}
|
|
18
|
+
const multibase = hexToMultibase(ed25519PublicKeyHex);
|
|
19
|
+
return `did:key:${multibase}`;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Parse a did:key back to a raw Ed25519 public key (hex).
|
|
23
|
+
* Validates the did:key prefix and multicodec bytes.
|
|
24
|
+
*/
|
|
25
|
+
export function fromDIDKey(didKey) {
|
|
26
|
+
if (typeof didKey !== 'string') {
|
|
27
|
+
throw new Error('did:key must be a string');
|
|
28
|
+
}
|
|
29
|
+
const parts = didKey.split(':');
|
|
30
|
+
if (parts.length !== 3 || parts[0] !== 'did' || parts[1] !== 'key') {
|
|
31
|
+
throw new Error(`Invalid did:key format: ${didKey}`);
|
|
32
|
+
}
|
|
33
|
+
const multibase = parts[2];
|
|
34
|
+
if (!multibase.startsWith('z')) {
|
|
35
|
+
throw new Error('did:key identifier must use z-prefix (base58btc) multibase');
|
|
36
|
+
}
|
|
37
|
+
return multibaseToHex(multibase);
|
|
38
|
+
}
|
|
39
|
+
// ── did:web ──
|
|
40
|
+
/**
|
|
41
|
+
* Construct the HTTPS URL for a did:web DID document.
|
|
42
|
+
*
|
|
43
|
+
* did:web:example.com → https://example.com/.well-known/did.json
|
|
44
|
+
* did:web:example.com:users:1 → https://example.com/users/1/did.json
|
|
45
|
+
* did:web:example.com%3A8443 → https://example.com:8443/.well-known/did.json
|
|
46
|
+
*/
|
|
47
|
+
export function didWebToUrl(didWeb) {
|
|
48
|
+
if (typeof didWeb !== 'string') {
|
|
49
|
+
throw new Error('did:web must be a string');
|
|
50
|
+
}
|
|
51
|
+
const parts = didWeb.split(':');
|
|
52
|
+
if (parts.length < 3 || parts[0] !== 'did' || parts[1] !== 'web') {
|
|
53
|
+
throw new Error(`Invalid did:web format: ${didWeb}`);
|
|
54
|
+
}
|
|
55
|
+
// Everything after "did:web:" is the domain-and-path, colon-separated
|
|
56
|
+
const segments = parts.slice(2).map(s => decodeURIComponent(s));
|
|
57
|
+
const domain = segments[0];
|
|
58
|
+
if (!domain) {
|
|
59
|
+
throw new Error('did:web must include a domain');
|
|
60
|
+
}
|
|
61
|
+
if (segments.length === 1) {
|
|
62
|
+
return `https://${domain}/.well-known/did.json`;
|
|
63
|
+
}
|
|
64
|
+
const path = segments.slice(1).join('/');
|
|
65
|
+
return `https://${domain}/${path}/did.json`;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Resolve a did:web DID by fetching the DID document over HTTPS.
|
|
69
|
+
* Returns the parsed DID Document object.
|
|
70
|
+
*
|
|
71
|
+
* Throws on network errors, non-200 responses, and invalid JSON.
|
|
72
|
+
*/
|
|
73
|
+
export async function resolveDIDWeb(didWeb) {
|
|
74
|
+
const url = didWebToUrl(didWeb);
|
|
75
|
+
const response = await fetch(url, {
|
|
76
|
+
headers: { 'Accept': 'application/did+ld+json, application/json' },
|
|
77
|
+
signal: AbortSignal.timeout(10_000),
|
|
78
|
+
});
|
|
79
|
+
if (!response.ok) {
|
|
80
|
+
throw new Error(`did:web resolution failed: HTTP ${response.status} from ${url}`);
|
|
81
|
+
}
|
|
82
|
+
const doc = await response.json();
|
|
83
|
+
if (!doc || typeof doc !== 'object' || !('id' in doc)) {
|
|
84
|
+
throw new Error(`did:web resolution returned invalid DID Document from ${url}`);
|
|
85
|
+
}
|
|
86
|
+
return doc;
|
|
87
|
+
}
|
|
88
|
+
// ── Passport ↔ DID Document ──
|
|
89
|
+
const DID_CONTEXT = [
|
|
90
|
+
'https://www.w3.org/ns/did/v1',
|
|
91
|
+
'https://w3id.org/security/suites/ed25519-2020/v1',
|
|
92
|
+
];
|
|
93
|
+
/**
|
|
94
|
+
* Convert an APS passport to a W3C DID Document.
|
|
95
|
+
* Produces a document with did:key as the subject identifier
|
|
96
|
+
* and a single Ed25519VerificationKey2020 verification method.
|
|
97
|
+
*
|
|
98
|
+
* Accepts a minimal passport shape: { agent_id, public_key }.
|
|
99
|
+
* Optionally accepts created_at for the document timestamps.
|
|
100
|
+
*/
|
|
101
|
+
export function passportToDIDDocument(passport) {
|
|
102
|
+
if (!passport.public_key || !/^[0-9a-f]{64}$/i.test(passport.public_key)) {
|
|
103
|
+
throw new Error('Invalid passport: public_key must be 64-char hex');
|
|
104
|
+
}
|
|
105
|
+
if (!passport.agent_id) {
|
|
106
|
+
throw new Error('Invalid passport: agent_id is required');
|
|
107
|
+
}
|
|
108
|
+
const did = toDIDKey(passport.public_key);
|
|
109
|
+
const keyId = `${did}#key-1`;
|
|
110
|
+
const publicKeyMultibase = hexToMultibase(passport.public_key);
|
|
111
|
+
const now = passport.created_at || new Date().toISOString();
|
|
112
|
+
return {
|
|
113
|
+
'@context': DID_CONTEXT,
|
|
114
|
+
id: did,
|
|
115
|
+
controller: did,
|
|
116
|
+
alsoKnownAs: [`did:aps:${publicKeyMultibase}`],
|
|
117
|
+
verificationMethod: [{
|
|
118
|
+
id: keyId,
|
|
119
|
+
type: 'Ed25519VerificationKey2020',
|
|
120
|
+
controller: did,
|
|
121
|
+
publicKeyMultibase,
|
|
122
|
+
}],
|
|
123
|
+
authentication: [keyId],
|
|
124
|
+
assertionMethod: [keyId],
|
|
125
|
+
capabilityDelegation: [keyId],
|
|
126
|
+
service: [{
|
|
127
|
+
id: `${did}#aps`,
|
|
128
|
+
type: 'AgentPassportService',
|
|
129
|
+
serviceEndpoint: {
|
|
130
|
+
agentId: passport.agent_id,
|
|
131
|
+
protocol: 'aps',
|
|
132
|
+
version: '1.0.0',
|
|
133
|
+
},
|
|
134
|
+
}],
|
|
135
|
+
created: now,
|
|
136
|
+
updated: now,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
//# sourceMappingURL=did-interop.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"did-interop.js","sourceRoot":"","sources":["../../../src/core/did-interop.ts"],"names":[],"mappings":"AAAA,0DAA0D;AAC1D,+DAA+D;AAC/D,sEAAsE;AAEtE,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,UAAU,CAAA;AAEzD,gBAAgB;AAEhB;;;;;;;GAOG;AACH,MAAM,UAAU,QAAQ,CAAC,mBAA2B;IAClD,IAAI,CAAC,mBAAmB,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,mBAAmB,CAAC,EAAE,CAAC;QACzE,MAAM,IAAI,KAAK,CAAC,yDAAyD,CAAC,CAAA;IAC5E,CAAC;IACD,MAAM,SAAS,GAAG,cAAc,CAAC,mBAAmB,CAAC,CAAA;IACrD,OAAO,WAAW,SAAS,EAAE,CAAA;AAC/B,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,UAAU,CAAC,MAAc;IACvC,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;QAC/B,MAAM,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAA;IAC7C,CAAC;IACD,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAC/B,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,KAAK,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,KAAK,EAAE,CAAC;QACnE,MAAM,IAAI,KAAK,CAAC,2BAA2B,MAAM,EAAE,CAAC,CAAA;IACtD,CAAC;IACD,MAAM,SAAS,GAAG,KAAK,CAAC,CAAC,CAAC,CAAA;IAC1B,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;QAC/B,MAAM,IAAI,KAAK,CAAC,4DAA4D,CAAC,CAAA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