mdip-agent-kit 0.1.1 → 0.1.3
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 +104 -1
- package/llms.txt +3 -0
- package/package.json +4 -2
- package/src/index.js +34 -2
- package/vectors/README.md +20 -0
- package/vectors/decrypt-openssl.sh +44 -0
- package/vectors/expiry-cases.txt +15 -0
- package/vectors/generate.js +59 -0
- package/vectors/kav-1/SHA256SUMS +5 -0
- package/vectors/kav-1/message-tampered.bin +1 -0
- package/vectors/kav-1/message.bin +1 -0
- package/vectors/kav-1/message.sha256 +1 -0
- package/vectors/kav-1/payload.json +8 -0
- package/vectors/kav-1/pub.pem +4 -0
- package/vectors/kav-1/signature.der +1 -0
- package/vectors/kav-1/signature.hex +1 -0
- package/vectors/kav-1/signed.json +14 -0
- package/vectors/kav-1/test-key.json +18 -0
- package/vectors/make-mdip-fixture-2.js +88 -0
- package/vectors/make-mdip-fixture.js +69 -0
- package/vectors/mdip-challenge-1/SHA256SUMS +10 -0
- package/vectors/mdip-challenge-1/challenge.message.bin +1 -0
- package/vectors/mdip-challenge-1/challenge.op.json +21 -0
- package/vectors/mdip-challenge-1/challenge.sig.der +0 -0
- package/vectors/mdip-challenge-1/dids.json +6 -0
- package/vectors/mdip-challenge-1/responder-agent.message.bin +1 -0
- package/vectors/mdip-challenge-1/responder-agent.op.json +21 -0
- package/vectors/mdip-challenge-1/responder-agent.pub.pem +4 -0
- package/vectors/mdip-challenge-1/responder-agent.sig.der +0 -0
- package/vectors/mdip-challenge-1/response.message.bin +1 -0
- package/vectors/mdip-challenge-1/response.op.json +27 -0
- package/vectors/mdip-challenge-1/response.sig.der +1 -0
- package/vectors/mdip-challenge-1/verifier-agent.message.bin +1 -0
- package/vectors/mdip-challenge-1/verifier-agent.op.json +21 -0
- package/vectors/mdip-challenge-1/verifier-agent.pub.pem +4 -0
- package/vectors/mdip-challenge-1/verifier-agent.sig.der +1 -0
- package/vectors/mdip-challenge-1/verify-response.json +10 -0
- package/vectors/mdip-challenge-2/SHA256SUMS +18 -0
- package/vectors/mdip-challenge-2/challenge.message.bin +1 -0
- package/vectors/mdip-challenge-2/challenge.op.json +21 -0
- package/vectors/mdip-challenge-2/challenge.sig.der +0 -0
- package/vectors/mdip-challenge-2/challenge.signed-op.bin +1 -0
- package/vectors/mdip-challenge-2/dids.json +6 -0
- package/vectors/mdip-challenge-2/responder-agent.message.bin +1 -0
- package/vectors/mdip-challenge-2/responder-agent.op.json +21 -0
- package/vectors/mdip-challenge-2/responder-agent.pub.pem +4 -0
- package/vectors/mdip-challenge-2/responder-agent.sig.der +0 -0
- package/vectors/mdip-challenge-2/responder-agent.signed-op.bin +1 -0
- package/vectors/mdip-challenge-2/response.cipher_receiver.bin +0 -0
- package/vectors/mdip-challenge-2/response.message.bin +1 -0
- package/vectors/mdip-challenge-2/response.op.json +27 -0
- package/vectors/mdip-challenge-2/response.plaintext.json +1 -0
- package/vectors/mdip-challenge-2/response.sig.der +1 -0
- package/vectors/mdip-challenge-2/response.signed-op.bin +1 -0
- package/vectors/mdip-challenge-2/test-verifier.json +21 -0
- package/vectors/mdip-challenge-2/verifier-agent.PUBLIC-TEST-KEY.priv.pem +5 -0
- package/vectors/mdip-challenge-2/verifier-agent.message.bin +1 -0
- package/vectors/mdip-challenge-2/verifier-agent.op.json +21 -0
- package/vectors/mdip-challenge-2/verifier-agent.pub.pem +4 -0
- package/vectors/mdip-challenge-2/verifier-agent.sig.der +1 -0
- package/vectors/mdip-challenge-2/verifier-agent.signed-op.bin +1 -0
- package/vectors/mdip-challenge-2/verify-response.json +13 -0
- package/vectors/verify-openssl.sh +82 -0
- package/vectors/verify-with-kit.js +71 -0
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ Give your AI agent its own **decentralized identity** (`did:mdip`) on the public
|
|
|
13
13
|
|
|
14
14
|
> Install: `npm install mdip-agent-kit` · run: `npx mdip-agent-kit <command>`
|
|
15
15
|
>
|
|
16
|
-
> Status: early (0.1.
|
|
16
|
+
> Status: early (0.1.3: offline challenge↔response binding vector + strict-expiry check). Not affiliated with SELF or Keychain. License MIT (see `LICENSE`; can be changed).
|
|
17
17
|
|
|
18
18
|
## Why
|
|
19
19
|
|
|
@@ -143,6 +143,109 @@ A freshly created DID is normally visible within 1–2 minutes. If the node had
|
|
|
143
143
|
moment, the operation goes out later via the mediator's periodic reconciliation, once the node is
|
|
144
144
|
fully synced. That can take a while on a brand-new node.
|
|
145
145
|
|
|
146
|
+
## Known-answer vectors (verify with stock OpenSSL, no install)
|
|
147
|
+
|
|
148
|
+
Fixed bytes so another agent can check this kit's signature crypto **without running any of its
|
|
149
|
+
code**. You need `curl`, `tar`, `sha256sum` and OpenSSL ≥ 1.1.1 with secp256k1 (stock builds have it).
|
|
150
|
+
Every signed message is non-empty, so `openssl pkeyutl` also works.
|
|
151
|
+
|
|
152
|
+
**What MDIP actually signs** (from `@mdip/cipher` / `@mdip/keymaster` 2.2.0, used by `sign`,
|
|
153
|
+
`verify` and every DID operation): `hash = sha256(JCS(obj without "signature"))` (RFC 8785 JSON
|
|
154
|
+
canonicalization, UTF-8). `signature.value` = secp256k1 ECDSA over that 32-byte hash (RFC 6979,
|
|
155
|
+
low-S), stored as 64-byte compact `r||s` hex. MDIP does **not** use Ed25519. For stock tools,
|
|
156
|
+
each vector ships the exact canonical bytes (`*.message.bin`), the signature re-encoded as DER
|
|
157
|
+
(`*.sig.der`) and the public key as SPKI PEM.
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
# fetch the published files: a plain tarball download, nothing is installed or executed
|
|
161
|
+
curl -sL https://registry.npmjs.org/mdip-agent-kit/-/mdip-agent-kit-0.1.3.tgz | tar xz
|
|
162
|
+
cd package/vectors
|
|
163
|
+
for d in kav-1 mdip-challenge-1 mdip-challenge-2; do (cd $d && sha256sum -c SHA256SUMS); done # all OK
|
|
164
|
+
|
|
165
|
+
# A. kav-1: synthetic vector, PUBLIC TEST KEY (private part published in test-key.json on purpose)
|
|
166
|
+
openssl dgst -sha256 -r kav-1/message.bin
|
|
167
|
+
# 4d4523d99bebdd70689f67a80e865349848f520d19ab9537403e56a280dcf64b *kav-1/message.bin (== kav-1/message.sha256)
|
|
168
|
+
openssl dgst -sha256 -verify kav-1/pub.pem -signature kav-1/signature.der kav-1/message.bin
|
|
169
|
+
# Verified OK (exit 0)
|
|
170
|
+
openssl dgst -sha256 -verify kav-1/pub.pem -signature kav-1/signature.der kav-1/message-tampered.bin
|
|
171
|
+
# Verification failure (exit 1, one byte changed)
|
|
172
|
+
openssl dgst -sha256 -binary kav-1/message.bin > /tmp/h.bin # same check on the raw 32-byte hash
|
|
173
|
+
openssl pkeyutl -verify -pubin -inkey kav-1/pub.pem -in /tmp/h.bin -sigfile kav-1/signature.der
|
|
174
|
+
# Signature Verified Successfully (exit 0)
|
|
175
|
+
|
|
176
|
+
# B. mdip-challenge-1: a REAL challenge -> response captured from this kit (throwaway identities)
|
|
177
|
+
for p in verifier-agent:verifier-agent challenge:verifier-agent responder-agent:responder-agent response:responder-agent; do
|
|
178
|
+
op=${p%%:*} key=${p##*:}
|
|
179
|
+
openssl dgst -sha256 -verify mdip-challenge-1/$key.pub.pem -signature mdip-challenge-1/$op.sig.der mdip-challenge-1/$op.message.bin
|
|
180
|
+
done
|
|
181
|
+
# Verified OK x4 (exit 0 each)
|
|
182
|
+
|
|
183
|
+
# C. mdip-challenge-2 (new in 0.1.3, thanks @homies): offline challenge <-> response BINDING.
|
|
184
|
+
# The verifier is a PUBLIC TEST identity, so anyone can decrypt the response it received.
|
|
185
|
+
d=mdip-challenge-2
|
|
186
|
+
openssl pkey -in $d/verifier-agent.PUBLIC-TEST-KEY.priv.pem -pubout | cmp - $d/verifier-agent.pub.pem # same key
|
|
187
|
+
bash decrypt-openssl.sh $d/verifier-agent.PUBLIC-TEST-KEY.priv.pem $d/responder-agent.pub.pem $d/response.cipher_receiver.bin
|
|
188
|
+
# {"response":{"challenge":"did:mdip:...","credentials":[],...,"match":true,"responseNonce":"..."}}
|
|
189
|
+
# the "challenge" value must equal dids.json "challenge"; verify-openssl.sh checks that, plus:
|
|
190
|
+
# - response.cipher_receiver.bin is exactly cipher_receiver inside the SIGNED response op
|
|
191
|
+
# - every DID == did:mdip:z + base58btc(01 8004 1220 || sha256(<name>.signed-op.bin)) (DID is the op hash)
|
|
192
|
+
# - challenge controller == verifier DID; response controller == encrypted.sender == responder DID
|
|
193
|
+
# - a flipped ciphertext byte fails the Poly1305 tag
|
|
194
|
+
|
|
195
|
+
# D. strict expiry: response.validUntil <= challenge.validUntil. Equal is allowed, 1 ms later is not.
|
|
196
|
+
cat expiry-cases.txt # the table; verify-openssl.sh and verify-with-kit.js both run it
|
|
197
|
+
|
|
198
|
+
# or all of the above in one go (bash + openssl only):
|
|
199
|
+
bash verify-openssl.sh # last line: ALL VECTORS OK, exit 0
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
| File | Value |
|
|
203
|
+
|---|---|
|
|
204
|
+
| `kav-1/message.bin` (240 bytes, JCS of `payload.json`) | sha256 `4d4523d99bebdd70689f67a80e865349848f520d19ab9537403e56a280dcf64b` |
|
|
205
|
+
| `kav-1/signature.hex` (compact r‖s) | `2b61bc63136caf5dbbdfee80d6717d57df09555be0f6c1b0158adc70feb0363c` `2067a7017b2b52fd92fd0e980650118bf5d4e07bc88ee8527f04f6404a37b4d1` |
|
|
206
|
+
| `kav-1/signature.der` | sha256 `fdc8e19ec10ced978a9dbd5ae599c35fa715d561dd1635bb5cf0360e3acd4508` |
|
|
207
|
+
| `kav-1/pub.pem` | sha256 `43d15804c3379b189d1d1fbfb391d3c42abf739587297bdb9987eab382bcd131` |
|
|
208
|
+
| `kav-1/test-key.json` label | `sha256("mdip-agent-kit kav-1 PUBLIC TEST KEY - never use for anything real")` = private key |
|
|
209
|
+
|
|
210
|
+
**What each vector proves, and what it doesn't:**
|
|
211
|
+
|
|
212
|
+
- **A (`kav-1`)** proves your stock OpenSSL agrees with the kit's sign/verify primitive on fixed
|
|
213
|
+
bytes. The payload is example application data, **not** MDIP's challenge wire format. Its signer
|
|
214
|
+
(`did:example:...`) doesn't resolve, so `mdip-agent verify kav-1/signed.json` will fail by design.
|
|
215
|
+
Regenerate it with `node vectors/generate.js` (deterministic, no diff expected).
|
|
216
|
+
- **B (`mdip-challenge-1`)** contains the real signed create operations (`*.op.json`) for two
|
|
217
|
+
throwaway agents, a challenge DID and the response DID, plus the verifier's decrypted
|
|
218
|
+
`verifyResponse` result (`verify-response.json`). OpenSSL proves that the challenge was signed
|
|
219
|
+
by the verifier's key and the response by the responder's key. Read the JSON to check the bindings:
|
|
220
|
+
the challenge `controller` is the verifier DID; the response `controller` and `data.encrypted.sender`
|
|
221
|
+
are the responder DID; the response `mdip.validUntil` ≤ the challenge's (expiry, capped by this kit).
|
|
222
|
+
The response payload (`challenge` DID, `responseNonce`, `match`) is **encrypted to the verifier**,
|
|
223
|
+
so only the verifier can check it; `verify-response.json` is what the verifier saw (see C for an offline-checkable version). These DIDs were short-lived (1 h), so they may no longer resolve. The fixture is for
|
|
224
|
+
offline checks.
|
|
225
|
+
- **C (`mdip-challenge-2`, 0.1.3)** closes the gap @homies found in B: there the binding sat in
|
|
226
|
+
ciphertext only the verifier could open. Here the verifier is a **PUBLIC TEST identity**: its wallet
|
|
227
|
+
mnemonic is derived from a published label (`test-verifier.json`) and its agent private key is
|
|
228
|
+
shipped as `verifier-agent.PUBLIC-TEST-KEY.priv.pem`. `decrypt-openssl.sh` decrypts the response
|
|
229
|
+
with stock OpenSSL only. MDIP's wire format (`@mdip/cipher` 2.2.0): key = first 32 bytes of the
|
|
230
|
+
compressed secp256k1 ECDH point (parity byte + 31 bytes of X), blob = `nonce(24) || ct || tag(16)`,
|
|
231
|
+
XChaCha20-Poly1305, no AAD. HChaCha20 is computed from one raw `openssl enc -chacha20` block and the
|
|
232
|
+
tag with `openssl mac POLY1305`. So an offline stranger can check, from signed bytes only: the verifier
|
|
233
|
+
signed challenge X, the responder signed a response whose plaintext says `"challenge": X`, and each
|
|
234
|
+
DID string is the hash of its op. Real verifiers keep their key private, so in production only the
|
|
235
|
+
verifier can make this check. The vector proves the format and the kit's binding logic, not a stranger's
|
|
236
|
+
view of a live exchange.
|
|
237
|
+
- **D (strict expiry)**: the intended rule is that a response **expires no later than** its challenge
|
|
238
|
+
(`response.validUntil <= challenge.validUntil`). Equal is valid because `respond` caps at exactly the
|
|
239
|
+
challenge expiry, which is why B and C show equal timestamps to the millisecond. One ms later is
|
|
240
|
+
rejected, and so is a missing response expiry when the challenge has one. Since 0.1.3,
|
|
241
|
+
`agent.verifyResponse()` / `mdip-agent verify-response` enforce this, plus "neither already expired",
|
|
242
|
+
and return `expiry: { ok, reason }` with `match: false` on failure (exported as `checkResponseExpiry`).
|
|
243
|
+
- **Not covered offline:** DID resolution, key rotation and revocation. `verifyResponse` re-resolves the
|
|
244
|
+
challenge and each presented credential **at verify time** and ignores deactivated or revoked ones.
|
|
245
|
+
That needs a gatekeeper. Check it with the kit (`npm test`) or your own MDIP node.
|
|
246
|
+
- With the kit installed, `node vectors/verify-with-kit.js` re-checks everything with `@mdip/cipher`
|
|
247
|
+
(recomputes JCS from the JSON instead of trusting the `.bin` files).
|
|
248
|
+
|
|
146
249
|
## Backup & restore
|
|
147
250
|
|
|
148
251
|
`init` and `backup` create:
|
package/llms.txt
CHANGED
|
@@ -7,6 +7,9 @@
|
|
|
7
7
|
- [AGENTS.md](AGENTS.md): step-by-step self-install procedure for AI agents
|
|
8
8
|
- [examples/agent-handshake.js](examples/agent-handshake.js): challenge/response between agents
|
|
9
9
|
- [examples/purchase-authorization.js](examples/purchase-authorization.js): accept and check a human-issued purchase credential
|
|
10
|
+
- [vectors/README.md](vectors/README.md): known-answer vectors (stock OpenSSL, no install)
|
|
11
|
+
|
|
12
|
+
- [README.md#known-answer-vectors-verify-with-stock-openssl-no-install](README.md): install-free known-answer vectors (secp256k1 / sha256(JCS)); files in vectors/, run `bash vectors/verify-openssl.sh`; 0.1.3 adds mdip-challenge-2 (offline challenge<->response binding via a PUBLIC TEST verifier key, stock-OpenSSL decrypt) and a strict-expiry table
|
|
10
13
|
|
|
11
14
|
## Key facts
|
|
12
15
|
- Install: `npm install mdip-agent-kit`; CLI: `npx mdip-agent-kit init --profile-name "Name"`; config via MDIP_HOME, MDIP_GATEKEEPER_URL, MDIP_AGENT_NAME
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mdip-agent-kit",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Give any AI agent its own MDIP (did:mdip) identity on the SELF / Keychain hyperswarm network: keymaster wallet, DID, SELF Public Profile molecule, backups, challenges and delegated-authority credentials.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
"src/",
|
|
17
17
|
"scripts/",
|
|
18
18
|
"examples/",
|
|
19
|
+
"vectors/",
|
|
19
20
|
"README.md",
|
|
20
21
|
"LICENSE",
|
|
21
22
|
"AGENTS.md",
|
|
@@ -37,7 +38,8 @@
|
|
|
37
38
|
"hyperswarm"
|
|
38
39
|
],
|
|
39
40
|
"scripts": {
|
|
40
|
-
"test": "bash test/e2e.sh"
|
|
41
|
+
"test": "bash test/e2e.sh",
|
|
42
|
+
"test:vectors": "bash vectors/verify-openssl.sh && node vectors/verify-with-kit.js"
|
|
41
43
|
},
|
|
42
44
|
"dependencies": {
|
|
43
45
|
"@mdip/cipher": "2.2.0",
|
package/src/index.js
CHANGED
|
@@ -85,6 +85,27 @@ export async function createKeymaster({ gatekeeperUrl, walletDir, passphrase, re
|
|
|
85
85
|
* Open (or create, with create:true) this agent's identity.
|
|
86
86
|
* Returns an Agent with helpers for signing, challenges, credentials and backups.
|
|
87
87
|
*/
|
|
88
|
+
/**
|
|
89
|
+
* Strict-expiry rule for challenge responses (intended semantics, fixtures in vectors/expiry-cases.txt):
|
|
90
|
+
* a response must expire NO LATER than its challenge (response.validUntil <= challenge.validUntil; equal is
|
|
91
|
+
* allowed because respond caps at exactly the challenge's expiry). If the challenge has a validUntil the
|
|
92
|
+
* response must have one too. With `now` (ms), both must also still be in the future.
|
|
93
|
+
* @returns {{ok: boolean, reason?: string}}
|
|
94
|
+
*/
|
|
95
|
+
export function checkResponseExpiry(challengeValidUntil, responseValidUntil, now) {
|
|
96
|
+
const c = challengeValidUntil ? Date.parse(challengeValidUntil) : NaN;
|
|
97
|
+
const r = responseValidUntil ? Date.parse(responseValidUntil) : NaN;
|
|
98
|
+
if (challengeValidUntil && !Number.isFinite(c)) return { ok: false, reason: 'challenge validUntil unparseable' };
|
|
99
|
+
if (responseValidUntil && !Number.isFinite(r)) return { ok: false, reason: 'response validUntil unparseable' };
|
|
100
|
+
if (Number.isFinite(c) && !Number.isFinite(r)) return { ok: false, reason: 'response has no validUntil but challenge does' };
|
|
101
|
+
if (Number.isFinite(c) && r > c) return { ok: false, reason: `response validUntil ${responseValidUntil} is later than challenge validUntil ${challengeValidUntil}` };
|
|
102
|
+
if (now !== undefined) {
|
|
103
|
+
if (Number.isFinite(c) && c <= now) return { ok: false, reason: `challenge expired at ${challengeValidUntil}` };
|
|
104
|
+
if (Number.isFinite(r) && r <= now) return { ok: false, reason: `response expired at ${responseValidUntil}` };
|
|
105
|
+
}
|
|
106
|
+
return { ok: true };
|
|
107
|
+
}
|
|
108
|
+
|
|
88
109
|
export async function openAgent(opts = {}) {
|
|
89
110
|
const cfg = loadConfig(opts);
|
|
90
111
|
ensurePrivateDir(cfg.home);
|
|
@@ -169,8 +190,19 @@ export class Agent {
|
|
|
169
190
|
}
|
|
170
191
|
return new Date(until).toISOString();
|
|
171
192
|
}
|
|
172
|
-
/**
|
|
173
|
-
|
|
193
|
+
/**
|
|
194
|
+
* Verify a response DID to a challenge you created. Result has .match, .responder, .vps...
|
|
195
|
+
* Since 0.1.3 also enforces strict expiry (see checkResponseExpiry): a response that outlives its
|
|
196
|
+
* challenge, or either one already expired, gives match:false with .expiry.reason set.
|
|
197
|
+
*/
|
|
198
|
+
async verifyResponse(responseDid, options = {}) {
|
|
199
|
+
const result = await this.keymaster.verifyResponse(responseDid, options);
|
|
200
|
+
const [respDoc, chDoc] = await Promise.all([this.keymaster.resolveDID(responseDid), this.keymaster.resolveDID(result.challenge)]);
|
|
201
|
+
const expiry = checkResponseExpiry(chDoc?.mdip?.validUntil, respDoc?.mdip?.validUntil, Date.now());
|
|
202
|
+
result.expiry = expiry;
|
|
203
|
+
if (!expiry.ok) result.match = false;
|
|
204
|
+
return result;
|
|
205
|
+
}
|
|
174
206
|
|
|
175
207
|
// ---- credentials -----------------------------------------------------------------------
|
|
176
208
|
/** Accept a credential issued to this agent (e.g. a purchase authorization from a SELF app). */
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# vectors/
|
|
2
|
+
|
|
3
|
+
Known-answer vectors for mdip-agent-kit's signature crypto. Docs and expected outputs are in the
|
|
4
|
+
main README under "Known-answer vectors (verify with stock OpenSSL, no install)".
|
|
5
|
+
|
|
6
|
+
| Path | What it is |
|
|
7
|
+
|---|---|
|
|
8
|
+
| `kav-1/` | Synthetic vector: public test key, non-empty JCS payload, compact + DER signature, tampered negative case |
|
|
9
|
+
| `mdip-challenge-1/` | Real MDIP create operations for verifier agent, responder agent, challenge and response, plus the verifier's decrypted `verifyResponse` result |
|
|
10
|
+
| `mdip-challenge-2/` | Real challenge -> response whose **verifier is a PUBLIC TEST identity** (mnemonic + private key published), so the encrypted response can be decrypted offline and its `challenge` field checked against the challenge DID. Also `*.signed-op.bin` so each DID can be recomputed from its op. Suggested by @homies |
|
|
11
|
+
| `expiry-cases.txt` | Strict-expiry table: response.validUntil must be <= challenge.validUntil (equal allowed, +1 ms rejected) |
|
|
12
|
+
| `decrypt-openssl.sh` | Stock-OpenSSL decrypt of an MDIP encrypted message (secp256k1 ECDH + XChaCha20-Poly1305, tag checked) |
|
|
13
|
+
| `verify-openssl.sh` | Install-free check (bash, sha256sum, openssl). Prints `ALL VECTORS OK` and exits 0 |
|
|
14
|
+
| `verify-with-kit.js` | Same checks via `@mdip/cipher` (needs `npm install`) plus the JSON binding fields |
|
|
15
|
+
| `generate.js` | Regenerates `kav-1/` deterministically |
|
|
16
|
+
| `make-mdip-fixture.js` | Re-captures `mdip-challenge-1/` against a gatekeeper (publishes throwaway DIDs) |
|
|
17
|
+
| `make-mdip-fixture-2.js` | Re-captures `mdip-challenge-2/` (publishes throwaway DIDs; verifier from the published label mnemonic) |
|
|
18
|
+
|
|
19
|
+
No real secrets are in here. `kav-1/test-key.json`, `mdip-challenge-2/test-verifier.json` and
|
|
20
|
+
`mdip-challenge-2/verifier-agent.PUBLIC-TEST-KEY.priv.pem` are test keys published on purpose. Never use them.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Offline decrypt of an MDIP encrypted message with stock OpenSSL (no npm, no network).
|
|
3
|
+
# MDIP (@mdip/cipher 2.2.0 encryptMessage) wire format, as implemented by the kit's dependency:
|
|
4
|
+
# S = secp256k1 ECDH(receiver priv, sender pub), as a COMPRESSED point (33 bytes: 02|03 || X)
|
|
5
|
+
# key = S[0:32] (the parity byte + the first 31 bytes of X; OpenSSL -derive gives X only,
|
|
6
|
+
# so we try prefix 02 and 03 and keep the one whose Poly1305 tag verifies)
|
|
7
|
+
# blob = base64url-decode(cipher_receiver) = nonce(24) || ciphertext || tag(16)
|
|
8
|
+
# cipher = XChaCha20-Poly1305(key, nonce), no AAD. XChaCha = HChaCha20 subkey + IETF ChaCha20-Poly1305
|
|
9
|
+
# with nonce 00000000||nonce[16:24]. HChaCha20 is recovered from one raw ChaCha20 block
|
|
10
|
+
# (keystream minus the initial state words 0-3 and 12-15).
|
|
11
|
+
# Usage: decrypt-openssl.sh <receiver-priv.pem> <sender-pub.pem> <blob.bin> -> plaintext on stdout
|
|
12
|
+
# Exit 0 only if the Poly1305 tag verifies.
|
|
13
|
+
set -euo pipefail
|
|
14
|
+
priv=$1 pub=$2 blob=$3
|
|
15
|
+
hex() { od -An -v -tx1 | tr -d ' \n'; }
|
|
16
|
+
unhex() { printf "$(printf '%s' "$1" | sed 's/../\\x&/g')"; }
|
|
17
|
+
le32() { local h=$1; echo $((16#${h:6:2}${h:4:2}${h:2:2}${h:0:2})); }
|
|
18
|
+
tole32(){ printf '%02x%02x%02x%02x' $(($1&255)) $((($1>>8)&255)) $((($1>>16)&255)) $((($1>>24)&255)); }
|
|
19
|
+
le64() { local n=$1 o= i; for i in 0 1 2 3 4 5 6 7; do o+=$(printf '%02x' $(((n>>(8*i))&255))); done; echo "$o"; }
|
|
20
|
+
ks() { head -c "$3" /dev/zero | openssl enc -chacha20 -K "$1" -iv "$2" | hex; } # raw keystream
|
|
21
|
+
|
|
22
|
+
B=$(hex < "$blob"); N=${B:0:48}; CT=${B:48:$((${#B}-48-32))}; TAG=${B: -32}
|
|
23
|
+
X=$(openssl pkeyutl -derive -inkey "$priv" -peerkey "$pub" | hex)
|
|
24
|
+
tmp=$(mktemp -d); trap 'rm -rf "$tmp"' EXIT
|
|
25
|
+
unhex "$CT" > "$tmp/ct"
|
|
26
|
+
for prefix in 02 03; do
|
|
27
|
+
key=$prefix${X:0:62}
|
|
28
|
+
# HChaCha20(key, nonce[0:16])
|
|
29
|
+
K=$(ks "$key" "${N:0:32}" 64); sub=
|
|
30
|
+
sigma=(1634760805 857760878 2036477234 1797285236)
|
|
31
|
+
for i in 0 1 2 3; do sub+=$(tole32 $(( ($(le32 ${K:$((8*i)):8}) - ${sigma[$i]}) & 0xffffffff ))); done
|
|
32
|
+
for i in 0 1 2 3; do sub+=$(tole32 $(( ($(le32 ${K:$((8*(12+i))):8}) - $(le32 ${N:$((8*i)):8})) & 0xffffffff ))); done
|
|
33
|
+
n12=00000000${N:32:16}
|
|
34
|
+
polykey=$(ks "$sub" "00000000$n12" 32)
|
|
35
|
+
pad=$(( (16 - ${#CT}/2 % 16) % 16 ))
|
|
36
|
+
{ cat "$tmp/ct"; head -c $pad /dev/zero; unhex "$(le64 0)$(le64 $((${#CT}/2)))"; } > "$tmp/macdata"
|
|
37
|
+
t=$(openssl mac -macopt hexkey:"$polykey" -in "$tmp/macdata" POLY1305 | tr 'A-F' 'a-f')
|
|
38
|
+
if [ "$t" = "$TAG" ]; then
|
|
39
|
+
openssl enc -chacha20 -K "$sub" -iv "01000000$n12" < "$tmp/ct"
|
|
40
|
+
exit 0
|
|
41
|
+
fi
|
|
42
|
+
done
|
|
43
|
+
echo "decrypt-openssl.sh: Poly1305 tag did not verify (wrong key or tampered ciphertext)" >&2
|
|
44
|
+
exit 1
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Strict-expiry rule (intended semantics, enforced by mdip-agent-kit >= 0.1.3 verifyResponse):
|
|
2
|
+
# a response MUST expire no later than the challenge it answers: response.validUntil <= challenge.validUntil
|
|
3
|
+
# equality is ALLOWED (the kit caps a response's expiry at exactly the challenge's validUntil);
|
|
4
|
+
# one millisecond later is REJECTED. A missing response validUntil is REJECTED when the challenge has one.
|
|
5
|
+
# Separately, at verify time both must still be in the future (now < validUntil); that needs a clock, not a vector.
|
|
6
|
+
# Timestamps are ISO-8601 UTC with milliseconds (what MDIP writes), so a plain string compare is exact.
|
|
7
|
+
# challenge.validUntil response.validUntil expected
|
|
8
|
+
2026-10-26T16:41:19.930Z 2026-10-26T16:41:19.930Z accept equal (mdip-challenge-1/-2 are this case)
|
|
9
|
+
2026-10-26T16:41:19.930Z 2026-10-26T16:41:19.929Z accept 1 ms earlier
|
|
10
|
+
2026-10-26T16:41:19.930Z 2026-10-19T16:41:19.930Z accept a week earlier
|
|
11
|
+
2026-10-26T16:41:19.930Z 2026-10-26T16:41:19.931Z reject 1 ms later
|
|
12
|
+
2026-10-26T16:41:19.930Z 2026-10-26T17:41:19.930Z reject 1 h later (keymaster's raw default, uncapped)
|
|
13
|
+
2026-10-26T16:41:19.930Z 2027-01-01T00:00:00.000Z reject next year
|
|
14
|
+
2026-10-26T16:41:19.930Z - reject response has no validUntil
|
|
15
|
+
- 2026-10-26T16:41:19.930Z accept challenge has no validUntil (nothing to cap)
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Regenerates vectors/kav-1/* (known-answer vector 1) using the SAME @mdip/cipher calls that
|
|
3
|
+
// @mdip/keymaster addSignature()/verifySignature() use: sha256(JCS(obj)) -> secp256k1 ECDSA
|
|
4
|
+
// (RFC 6979, low-S, compact r||s hex). Output is deterministic; re-running must produce no diff.
|
|
5
|
+
// Usage: node vectors/generate.js (needs the kit's npm deps; verifying needs only openssl)
|
|
6
|
+
import fs from 'node:fs';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import crypto from 'node:crypto';
|
|
9
|
+
import { fileURLToPath } from 'node:url';
|
|
10
|
+
import CipherNode from '@mdip/cipher/node';
|
|
11
|
+
|
|
12
|
+
const dir = path.join(path.dirname(fileURLToPath(import.meta.url)), 'kav-1');
|
|
13
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
14
|
+
const cipher = new CipherNode();
|
|
15
|
+
const w = (f, data) => fs.writeFileSync(path.join(dir, f), data);
|
|
16
|
+
|
|
17
|
+
// PUBLIC TEST KEY. Derived from a published label so anyone can re-derive it. Never use it for anything real.
|
|
18
|
+
const LABEL = 'mdip-agent-kit kav-1 PUBLIC TEST KEY - never use for anything real';
|
|
19
|
+
const d = crypto.createHash('sha256').update(LABEL, 'utf8').digest();
|
|
20
|
+
const { publicJwk, privateJwk } = cipher.generateJwk(new Uint8Array(d));
|
|
21
|
+
|
|
22
|
+
// Example payload (application data, NOT the MDIP challenge wire format; see README).
|
|
23
|
+
const payload = {
|
|
24
|
+
kit: 'mdip-agent-kit',
|
|
25
|
+
vector: 'kav-1',
|
|
26
|
+
message: 'Known-answer vector for MDIP signed JSON. Non-empty on purpose.',
|
|
27
|
+
nonce: '6b61762d312d6e6f6e63652d30303031',
|
|
28
|
+
verifier: 'did:example:kav-1-verifier',
|
|
29
|
+
validUntil: '2026-12-31T23:59:59.000Z',
|
|
30
|
+
};
|
|
31
|
+
const canonical = cipher.canonicalizeJSON(payload);
|
|
32
|
+
const hash = cipher.hashJSON(payload);
|
|
33
|
+
const value = cipher.signHash(hash, privateJwk);
|
|
34
|
+
if (!cipher.verifySig(hash, value, publicJwk)) throw new Error('self-check failed');
|
|
35
|
+
|
|
36
|
+
// Same shape as keymaster.addSignature() / `mdip-agent sign` output. Signer is a placeholder, not resolvable.
|
|
37
|
+
const signed = { ...payload, signature: { signer: 'did:example:kav-1-signer', signed: '2026-10-26T00:00:00.000Z', hash, value } };
|
|
38
|
+
|
|
39
|
+
// Stock-tool encodings: SPKI PEM public key and DER ECDSA signature.
|
|
40
|
+
const pub = crypto.createPublicKey({ key: { ...publicJwk }, format: 'jwk' });
|
|
41
|
+
const r = Buffer.from(value.slice(0, 64), 'hex'), s = Buffer.from(value.slice(64), 'hex');
|
|
42
|
+
const int = b => { let i = 0; while (i < b.length - 1 && b[i] === 0) i++; b = b.subarray(i); if (b[0] & 0x80) b = Buffer.concat([Buffer.from([0]), b]); return Buffer.concat([Buffer.from([0x02, b.length]), b]); };
|
|
43
|
+
const body = Buffer.concat([int(r), int(s)]);
|
|
44
|
+
const der = Buffer.concat([Buffer.from([0x30, body.length]), body]);
|
|
45
|
+
if (!crypto.verify('sha256', Buffer.from(canonical, 'utf8'), { key: pub, dsaEncoding: 'der' }, der)) throw new Error('node DER check failed');
|
|
46
|
+
|
|
47
|
+
w('test-key.json', JSON.stringify({ WARNING: 'PUBLIC TEST KEY - private part is published on purpose; never use it', label: LABEL, privateKeyHex: d.toString('hex'), publicJwk, privateJwk }, null, 2) + '\n');
|
|
48
|
+
w('pub.pem', pub.export({ type: 'spki', format: 'pem' }));
|
|
49
|
+
w('payload.json', JSON.stringify(payload, null, 2) + '\n');
|
|
50
|
+
w('message.bin', canonical); // exact signed-over bytes: JCS(payload), UTF-8, no trailing newline
|
|
51
|
+
w('message.sha256', hash + '\n');
|
|
52
|
+
w('signature.hex', value + '\n'); // what MDIP stores in signature.value (64-byte compact r||s)
|
|
53
|
+
w('signature.der', der);
|
|
54
|
+
w('signed.json', JSON.stringify(signed, null, 2) + '\n');
|
|
55
|
+
w('message-tampered.bin', canonical.replace('Non-empty', 'Non-emptY')); // negative case: must FAIL
|
|
56
|
+
const sums = ['message.bin', 'message-tampered.bin', 'signature.der', 'pub.pem', 'signed.json']
|
|
57
|
+
.map(f => crypto.createHash('sha256').update(fs.readFileSync(path.join(dir, f))).digest('hex') + ' ' + f).join('\n') + '\n';
|
|
58
|
+
w('SHA256SUMS', sums);
|
|
59
|
+
console.log(JSON.stringify({ dir, bytes: Buffer.byteLength(canonical), hash, value }, null, 2));
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
4d4523d99bebdd70689f67a80e865349848f520d19ab9537403e56a280dcf64b message.bin
|
|
2
|
+
17bb3f553bd7a600ad5585015f59c770278b599921eb52b9bfd51e16aa4de476 message-tampered.bin
|
|
3
|
+
fdc8e19ec10ced978a9dbd5ae599c35fa715d561dd1635bb5cf0360e3acd4508 signature.der
|
|
4
|
+
43d15804c3379b189d1d1fbfb391d3c42abf739587297bdb9987eab382bcd131 pub.pem
|
|
5
|
+
a1e3e79bd90f57d7ac991dbd040a436e3fab1bcae648c646536193397bb88c85 signed.json
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"kit":"mdip-agent-kit","message":"Known-answer vector for MDIP signed JSON. Non-emptY on purpose.","nonce":"6b61762d312d6e6f6e63652d30303031","validUntil":"2026-12-31T23:59:59.000Z","vector":"kav-1","verifier":"did:example:kav-1-verifier"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"kit":"mdip-agent-kit","message":"Known-answer vector for MDIP signed JSON. Non-empty on purpose.","nonce":"6b61762d312d6e6f6e63652d30303031","validUntil":"2026-12-31T23:59:59.000Z","vector":"kav-1","verifier":"did:example:kav-1-verifier"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
4d4523d99bebdd70689f67a80e865349848f520d19ab9537403e56a280dcf64b
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
{
|
|
2
|
+
"kit": "mdip-agent-kit",
|
|
3
|
+
"vector": "kav-1",
|
|
4
|
+
"message": "Known-answer vector for MDIP signed JSON. Non-empty on purpose.",
|
|
5
|
+
"nonce": "6b61762d312d6e6f6e63652d30303031",
|
|
6
|
+
"verifier": "did:example:kav-1-verifier",
|
|
7
|
+
"validUntil": "2026-12-31T23:59:59.000Z"
|
|
8
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0D +a�cl�]����q}W� U[������p��6< g�{+R����P����{Ȏ�R�@J7��
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
2b61bc63136caf5dbbdfee80d6717d57df09555be0f6c1b0158adc70feb0363c2067a7017b2b52fd92fd0e980650118bf5d4e07bc88ee8527f04f6404a37b4d1
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"kit": "mdip-agent-kit",
|
|
3
|
+
"vector": "kav-1",
|
|
4
|
+
"message": "Known-answer vector for MDIP signed JSON. Non-empty on purpose.",
|
|
5
|
+
"nonce": "6b61762d312d6e6f6e63652d30303031",
|
|
6
|
+
"verifier": "did:example:kav-1-verifier",
|
|
7
|
+
"validUntil": "2026-12-31T23:59:59.000Z",
|
|
8
|
+
"signature": {
|
|
9
|
+
"signer": "did:example:kav-1-signer",
|
|
10
|
+
"signed": "2026-10-26T00:00:00.000Z",
|
|
11
|
+
"hash": "4d4523d99bebdd70689f67a80e865349848f520d19ab9537403e56a280dcf64b",
|
|
12
|
+
"value": "2b61bc63136caf5dbbdfee80d6717d57df09555be0f6c1b0158adc70feb0363c2067a7017b2b52fd92fd0e980650118bf5d4e07bc88ee8527f04f6404a37b4d1"
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"WARNING": "PUBLIC TEST KEY - private part is published on purpose; never use it",
|
|
3
|
+
"label": "mdip-agent-kit kav-1 PUBLIC TEST KEY - never use for anything real",
|
|
4
|
+
"privateKeyHex": "c1433ed3bcadb796140c3bdbe694698f70ba1cf1f57f4e4e5ab789c22a2e3732",
|
|
5
|
+
"publicJwk": {
|
|
6
|
+
"kty": "EC",
|
|
7
|
+
"crv": "secp256k1",
|
|
8
|
+
"x": "oHkFvCmbWtTbv23XnA3GxISuOAevKWhEqIc6gTrGz3s",
|
|
9
|
+
"y": "f03etbi1OB3A5-7hizstgooDg8O51i7J3bpDY7iDZ8M"
|
|
10
|
+
},
|
|
11
|
+
"privateJwk": {
|
|
12
|
+
"kty": "EC",
|
|
13
|
+
"crv": "secp256k1",
|
|
14
|
+
"x": "oHkFvCmbWtTbv23XnA3GxISuOAevKWhEqIc6gTrGz3s",
|
|
15
|
+
"y": "f03etbi1OB3A5-7hizstgooDg8O51i7J3bpDY7iDZ8M",
|
|
16
|
+
"d": "wUM-07ytt5YUDDvb5pRpj3C6HPH1f05OWreJwiouNzI"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Captures vectors/mdip-challenge-2/: a REAL challenge -> response where the VERIFIER is a PUBLIC TEST
|
|
3
|
+
// IDENTITY (its wallet mnemonic and agent private key are published on purpose), so anyone can decrypt
|
|
4
|
+
// the response offline and check the challenge <-> response binding without running this kit.
|
|
5
|
+
// Suggested by @homies on connectthebots.app (/book/1227): in mdip-challenge-1 the binding lived only
|
|
6
|
+
// inside ciphertext encrypted to a private verifier key.
|
|
7
|
+
// Usage: MDIP_GATEKEEPER_URL=http://localhost:4224 node vectors/make-mdip-fixture-2.js
|
|
8
|
+
// NOTE: publishes throwaway DIDs (two agents, one 1-hour challenge, one response) to the registry.
|
|
9
|
+
import fs from 'node:fs';
|
|
10
|
+
import os from 'node:os';
|
|
11
|
+
import path from 'node:path';
|
|
12
|
+
import crypto from 'node:crypto';
|
|
13
|
+
import { fileURLToPath } from 'node:url';
|
|
14
|
+
import * as bip39 from 'bip39';
|
|
15
|
+
import CipherNode from '@mdip/cipher/node';
|
|
16
|
+
import { openAgent, waitForDid } from '../src/index.js';
|
|
17
|
+
|
|
18
|
+
const out = path.join(path.dirname(fileURLToPath(import.meta.url)), 'mdip-challenge-2');
|
|
19
|
+
fs.rmSync(out, { recursive: true, force: true });
|
|
20
|
+
fs.mkdirSync(out, { recursive: true });
|
|
21
|
+
const cipher = new CipherNode();
|
|
22
|
+
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'kav2-fixture-'));
|
|
23
|
+
const w = (f, data) => fs.writeFileSync(path.join(out, f), data);
|
|
24
|
+
const der = hex => {
|
|
25
|
+
const int = b => { let i = 0; while (i < b.length - 1 && b[i] === 0) i++; b = b.subarray(i); if (b[0] & 0x80) b = Buffer.concat([Buffer.from([0]), b]); return Buffer.concat([Buffer.from([0x02, b.length]), b]); };
|
|
26
|
+
const body = Buffer.concat([int(Buffer.from(hex.slice(0, 64), 'hex')), int(Buffer.from(hex.slice(64), 'hex'))]);
|
|
27
|
+
return Buffer.concat([Buffer.from([0x30, body.length]), body]);
|
|
28
|
+
};
|
|
29
|
+
// PUBLIC TEST VERIFIER. Mnemonic derived from a published label; anyone can re-derive it. Never use it.
|
|
30
|
+
const LABEL = 'mdip-agent-kit mdip-challenge-2 PUBLIC TEST VERIFIER - never use for anything real';
|
|
31
|
+
const mnemonic = bip39.entropyToMnemonic(crypto.createHash('sha256').update(LABEL, 'utf8').digest().subarray(0, 16));
|
|
32
|
+
try {
|
|
33
|
+
const verifier = await openAgent({ home: path.join(tmp, 'v'), name: 'kav2-public-test-verifier', create: true, mnemonic });
|
|
34
|
+
const responder = await openAgent({ home: path.join(tmp, 'r'), name: 'kav2-responder', create: true });
|
|
35
|
+
const vkp = await verifier.keymaster.fetchKeyPair();
|
|
36
|
+
const challengeDid = await verifier.createChallenge({});
|
|
37
|
+
await waitForDid(verifier.keymaster, challengeDid);
|
|
38
|
+
const responseDid = await responder.respondToChallenge(challengeDid); // validUntil capped at challenge's
|
|
39
|
+
await waitForDid(verifier.keymaster, responseDid);
|
|
40
|
+
const result = await verifier.verifyResponse(responseDid);
|
|
41
|
+
if (!result.match) throw new Error('verifyResponse did not match');
|
|
42
|
+
|
|
43
|
+
const names = { 'verifier-agent': verifier.did, 'responder-agent': responder.did, challenge: challengeDid, response: responseDid };
|
|
44
|
+
const exported = await verifier.keymaster.gatekeeper.exportDIDs(Object.values(names));
|
|
45
|
+
const ops = {};
|
|
46
|
+
Object.keys(names).forEach((k, i) => { ops[k] = exported[i][0].operation; });
|
|
47
|
+
if (JSON.stringify(ops['verifier-agent'].publicJwk) !== JSON.stringify(vkp.publicJwk)) throw new Error('verifier key mismatch');
|
|
48
|
+
for (const k of ['verifier-agent', 'responder-agent'])
|
|
49
|
+
w(`${k}.pub.pem`, crypto.createPublicKey({ key: { ...ops[k].publicJwk }, format: 'jwk' }).export({ type: 'spki', format: 'pem' }));
|
|
50
|
+
const priv = crypto.createPrivateKey({ key: { ...vkp.privateJwk }, format: 'jwk' });
|
|
51
|
+
w('verifier-agent.PUBLIC-TEST-KEY.priv.pem', priv.export({ type: 'pkcs8', format: 'pem' }));
|
|
52
|
+
w('test-verifier.json', JSON.stringify({
|
|
53
|
+
WARNING: 'PUBLIC TEST VERIFIER - wallet mnemonic and private key are published on purpose; never use them',
|
|
54
|
+
label: LABEL, mnemonicEntropy: 'first 16 bytes of sha256(label)', mnemonic,
|
|
55
|
+
derivationPath: "m/44'/0'/0'/0/0", did: verifier.did, publicJwk: vkp.publicJwk, privateJwk: vkp.privateJwk,
|
|
56
|
+
}, null, 2) + '\n');
|
|
57
|
+
|
|
58
|
+
const sums = [];
|
|
59
|
+
for (const [k, op] of Object.entries(ops)) {
|
|
60
|
+
const copy = JSON.parse(JSON.stringify(op)); delete copy.signature;
|
|
61
|
+
const msg = cipher.canonicalizeJSON(copy);
|
|
62
|
+
const hash = cipher.hashMessage(msg);
|
|
63
|
+
if (hash !== op.signature.hash) throw new Error(`${k}: hash mismatch`);
|
|
64
|
+
const signerKey = op.mdip.type === 'agent' ? op.publicJwk : ops[op.controller === verifier.did ? 'verifier-agent' : 'responder-agent'].publicJwk;
|
|
65
|
+
if (!cipher.verifySig(hash, op.signature.value, signerKey)) throw new Error(`${k}: sig fails`);
|
|
66
|
+
const full = cipher.canonicalizeJSON(op); // DID suffix = CIDv1(dag-json, sha256(JCS(signed op)))
|
|
67
|
+
w(`${k}.op.json`, JSON.stringify(op, null, 2) + '\n');
|
|
68
|
+
w(`${k}.message.bin`, msg);
|
|
69
|
+
w(`${k}.sig.der`, der(op.signature.value));
|
|
70
|
+
w(`${k}.signed-op.bin`, full);
|
|
71
|
+
sums.push(`${k}.message.bin`, `${k}.sig.der`, `${k}.signed-op.bin`);
|
|
72
|
+
}
|
|
73
|
+
// The response ciphertext the verifier received, and the plaintext it decrypts to (exact bytes).
|
|
74
|
+
const ct = ops.response.data.encrypted.cipher_receiver;
|
|
75
|
+
w('response.cipher_receiver.bin', Buffer.from(ct, 'base64url'));
|
|
76
|
+
const plaintext = cipher.decryptMessage(ops['responder-agent'].publicJwk, vkp.privateJwk, ct);
|
|
77
|
+
w('response.plaintext.json', plaintext);
|
|
78
|
+
if (JSON.parse(plaintext).response.challenge !== challengeDid) throw new Error('binding mismatch');
|
|
79
|
+
sums.push('response.cipher_receiver.bin', 'response.plaintext.json');
|
|
80
|
+
w('dids.json', JSON.stringify(names, null, 2) + '\n');
|
|
81
|
+
w('verify-response.json', JSON.stringify(result, null, 2) + '\n');
|
|
82
|
+
sums.push('verifier-agent.pub.pem', 'responder-agent.pub.pem', 'verifier-agent.PUBLIC-TEST-KEY.priv.pem', 'dids.json');
|
|
83
|
+
w('SHA256SUMS', sums.map(f => crypto.createHash('sha256').update(fs.readFileSync(path.join(out, f))).digest('hex') + ' ' + f).join('\n') + '\n');
|
|
84
|
+
console.log(JSON.stringify({ out, names, match: result.match, plaintext }, null, 2));
|
|
85
|
+
} finally {
|
|
86
|
+
fs.rmSync(tmp, { recursive: true, force: true });
|
|
87
|
+
}
|
|
88
|
+
process.exit(0);
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Captures a REAL MDIP challenge -> response round trip as a fixture (vectors/mdip-challenge-1/).
|
|
3
|
+
// Creates two clearly-named throwaway identities in temp homes (no secrets are written to the repo),
|
|
4
|
+
// runs createChallenge / createResponse / verifyResponse through this kit, then exports the signed
|
|
5
|
+
// create operations from the gatekeeper and writes stock-tool encodings of each signature.
|
|
6
|
+
// Usage: MDIP_GATEKEEPER_URL=http://localhost:4224 node vectors/make-mdip-fixture.js
|
|
7
|
+
// NOTE: like `npm test`, this publishes throwaway DIDs to the configured registry.
|
|
8
|
+
import fs from 'node:fs';
|
|
9
|
+
import os from 'node:os';
|
|
10
|
+
import path from 'node:path';
|
|
11
|
+
import crypto from 'node:crypto';
|
|
12
|
+
import { fileURLToPath } from 'node:url';
|
|
13
|
+
import CipherNode from '@mdip/cipher/node';
|
|
14
|
+
import { openAgent, waitForDid } from '../src/index.js';
|
|
15
|
+
|
|
16
|
+
const out = path.join(path.dirname(fileURLToPath(import.meta.url)), 'mdip-challenge-1');
|
|
17
|
+
fs.rmSync(out, { recursive: true, force: true });
|
|
18
|
+
fs.mkdirSync(out, { recursive: true });
|
|
19
|
+
const cipher = new CipherNode();
|
|
20
|
+
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'kav-fixture-'));
|
|
21
|
+
const w = (f, data) => fs.writeFileSync(path.join(out, f), data);
|
|
22
|
+
const der = hex => {
|
|
23
|
+
const int = b => { let i = 0; while (i < b.length - 1 && b[i] === 0) i++; b = b.subarray(i); if (b[0] & 0x80) b = Buffer.concat([Buffer.from([0]), b]); return Buffer.concat([Buffer.from([0x02, b.length]), b]); };
|
|
24
|
+
const body = Buffer.concat([int(Buffer.from(hex.slice(0, 64), 'hex')), int(Buffer.from(hex.slice(64), 'hex'))]);
|
|
25
|
+
return Buffer.concat([Buffer.from([0x30, body.length]), body]);
|
|
26
|
+
};
|
|
27
|
+
try {
|
|
28
|
+
const verifier = await openAgent({ home: path.join(tmp, 'v'), name: 'mdip-agent-kit-kav-verifier', create: true });
|
|
29
|
+
const responder = await openAgent({ home: path.join(tmp, 'r'), name: 'mdip-agent-kit-kav-responder', create: true });
|
|
30
|
+
const challengeDid = await verifier.createChallenge({}); // no credentials requested
|
|
31
|
+
await waitForDid(verifier.keymaster, challengeDid);
|
|
32
|
+
const responseDid = await responder.respondToChallenge(challengeDid);
|
|
33
|
+
await waitForDid(verifier.keymaster, responseDid);
|
|
34
|
+
const result = await verifier.verifyResponse(responseDid);
|
|
35
|
+
if (!result.match) throw new Error('verifyResponse did not match');
|
|
36
|
+
|
|
37
|
+
const gk = verifier.keymaster.gatekeeper;
|
|
38
|
+
const names = { 'verifier-agent': verifier.did, 'responder-agent': responder.did, challenge: challengeDid, response: responseDid };
|
|
39
|
+
const exported = await gk.exportDIDs(Object.values(names));
|
|
40
|
+
const ops = {}; const dids = {}; const pubs = {};
|
|
41
|
+
Object.keys(names).forEach((k, i) => { ops[k] = exported[i][0].operation; dids[k] = names[k]; });
|
|
42
|
+
for (const k of ['verifier-agent', 'responder-agent']) {
|
|
43
|
+
pubs[names[k]] = crypto.createPublicKey({ key: { ...ops[k].publicJwk }, format: 'jwk' }).export({ type: 'spki', format: 'pem' });
|
|
44
|
+
w(`${k}.pub.pem`, pubs[names[k]]);
|
|
45
|
+
}
|
|
46
|
+
const sums = [];
|
|
47
|
+
for (const [k, op] of Object.entries(ops)) {
|
|
48
|
+
const copy = JSON.parse(JSON.stringify(op)); delete copy.signature;
|
|
49
|
+
const msg = cipher.canonicalizeJSON(copy);
|
|
50
|
+
const hash = cipher.hashMessage(msg);
|
|
51
|
+
if (hash !== op.signature.hash) throw new Error(`${k}: hash mismatch`);
|
|
52
|
+
// Agent create ops are self-signed (key in op.publicJwk); asset ops are signed by their controller agent.
|
|
53
|
+
const signerKey = op.mdip.type === 'agent' ? op.publicJwk : ops[op.controller === verifier.did ? 'verifier-agent' : 'responder-agent'].publicJwk;
|
|
54
|
+
if (!cipher.verifySig(hash, op.signature.value, signerKey)) throw new Error(`${k}: sig fails`);
|
|
55
|
+
w(`${k}.op.json`, JSON.stringify(op, null, 2) + '\n');
|
|
56
|
+
w(`${k}.message.bin`, msg);
|
|
57
|
+
w(`${k}.sig.der`, der(op.signature.value));
|
|
58
|
+
sums.push(`${k}.message.bin`, `${k}.sig.der`);
|
|
59
|
+
}
|
|
60
|
+
w('dids.json', JSON.stringify(dids, null, 2) + '\n');
|
|
61
|
+
// The decrypted response as seen by the verifier (contents are not secret; throwaway identities).
|
|
62
|
+
w('verify-response.json', JSON.stringify(result, null, 2) + '\n');
|
|
63
|
+
w('SHA256SUMS', sums.concat(['verifier-agent.pub.pem', 'responder-agent.pub.pem'])
|
|
64
|
+
.map(f => crypto.createHash('sha256').update(fs.readFileSync(path.join(out, f))).digest('hex') + ' ' + f).join('\n') + '\n');
|
|
65
|
+
console.log(JSON.stringify({ out, dids, match: result.match }, null, 2));
|
|
66
|
+
} finally {
|
|
67
|
+
fs.rmSync(tmp, { recursive: true, force: true });
|
|
68
|
+
}
|
|
69
|
+
process.exit(0);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
365a0ced00c124622d67166d415c76cb54cb6dcace2929684d1a90fc5f4a9ca1 verifier-agent.message.bin
|
|
2
|
+
4a5a78fb38ee318c7cde735e397b125b33f6d45326eb1c4bc915f3b4b3cc2876 verifier-agent.sig.der
|
|
3
|
+
e44267b37efe89f8eedea17454627ac65604f90a0f04aa864ad593e37fd5db05 responder-agent.message.bin
|
|
4
|
+
eee56de72b84f0777a339ab4113d8636cf73685f6ee879d21436331a5f08e279 responder-agent.sig.der
|
|
5
|
+
a81eb39199dbffd68011e623873803b7b707d164610a3985b193da1d316f3c8e challenge.message.bin
|
|
6
|
+
2b16fb43d3cc6deb35d1f5d995e4a3cf303f49df9f2ff1e359606bc0cdd85199 challenge.sig.der
|
|
7
|
+
4263cea2014b2d908ee9110a5b484715ebbe4540e754a7657fa0f55849de6128 response.message.bin
|
|
8
|
+
7b3915c9f99dcb65a75ff501b70a120815a10159df980c8929931acb9182e1d8 response.sig.der
|
|
9
|
+
7a0f8fed9958c043fbd2941be2ac6b8216214d90a095765e24a3b5876f17f1b1 verifier-agent.pub.pem
|
|
10
|
+
a56b1ca4d1af0128e8d44b032a1fa5e164288a0a8a3d94b937b1a0a072fa0197 responder-agent.pub.pem
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"controller":"did:mdip:z3v8AuafKYqAKRVQVArS7VX399DWXp6AqUofefLxE84Ksi5FKjx","created":"2026-10-26T15:19:53.676Z","data":{"challenge":{}},"mdip":{"prefix":"did:mdip","registry":"hyperswarm","type":"asset","validUntil":"2026-10-26T16:19:53.672Z","version":1},"type":"create"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"type": "create",
|
|
3
|
+
"created": "2026-10-26T15:19:53.676Z",
|
|
4
|
+
"mdip": {
|
|
5
|
+
"version": 1,
|
|
6
|
+
"type": "asset",
|
|
7
|
+
"registry": "hyperswarm",
|
|
8
|
+
"validUntil": "2026-10-26T16:19:53.672Z",
|
|
9
|
+
"prefix": "did:mdip"
|
|
10
|
+
},
|
|
11
|
+
"controller": "did:mdip:z3v8AuafKYqAKRVQVArS7VX399DWXp6AqUofefLxE84Ksi5FKjx",
|
|
12
|
+
"data": {
|
|
13
|
+
"challenge": {}
|
|
14
|
+
},
|
|
15
|
+
"signature": {
|
|
16
|
+
"signer": "did:mdip:z3v8AuafKYqAKRVQVArS7VX399DWXp6AqUofefLxE84Ksi5FKjx",
|
|
17
|
+
"signed": "2026-10-26T15:19:53.683Z",
|
|
18
|
+
"hash": "a81eb39199dbffd68011e623873803b7b707d164610a3985b193da1d316f3c8e",
|
|
19
|
+
"value": "e7f94827c6332c246a00cdd35acc233ce71cfb03cd93107cc4dce33d89e3bd6f69fe5dd62d1b259a2164d32479b3fdbc0dc60231daba344821a2e5e4a195101c"
|
|
20
|
+
}
|
|
21
|
+
}
|
|
Binary file
|