zeroclawlabs 0.6.9

Zero overhead. Zero compromise. 100% Rust. The fastest, smallest AI assistant.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//! L2 and L3 credential issuance.
//!
//! Provides builders for constructing VI credentials with proper SD-JWT
//! serialization and key binding. L1 issuance is out of scope (performed by
//! external credential providers / issuers).

use ring::signature::EcdsaKeyPair;
use serde_json::json;

use crate::verifiable_intent::crypto::{create_disclosure, jws_sign, sd_hash, serialize_sd_jwt};
use crate::verifiable_intent::error::{ViError, ViErrorKind};
use crate::verifiable_intent::types::{
    CheckoutL3Mandate, FinalCheckoutMandate, FinalPaymentMandate, Jwk, OpenCheckoutMandate,
    OpenPaymentMandate, PaymentL3Mandate,
};

// ── L2 Immediate mode ────────────────────────────────────────────────

/// Result of creating an L2 Immediate credential.
#[derive(Debug)]
pub struct ImmediateL2Result {
    /// The serialized SD-JWT string (L1~disclosures~kb_jwt).
    pub serialized: String,
    /// The SD hash of the L1 that was bound.
    pub sd_hash: String,
}

/// Create an L2 Immediate-mode credential binding final checkout and payment values.
///
/// The caller must provide the serialized L1 SD-JWT and the user's signing key
/// (the private key corresponding to L1 `cnf.jwk`).
pub fn create_layer2_immediate(
    serialized_l1: &str,
    checkout: &FinalCheckoutMandate,
    payment: &FinalPaymentMandate,
    audience: &str,
    nonce: &str,
    user_key: &EcdsaKeyPair,
    iat: i64,
    exp: i64,
) -> Result<ImmediateL2Result, ViError> {
    let l1_hash = sd_hash(serialized_l1);

    // Create disclosures for mandates
    let checkout_value = serde_json::to_value(checkout).map_err(|e| {
        ViError::new(
            ViErrorKind::IssuanceInputInvalid,
            format!("checkout serialize: {e}"),
        )
    })?;
    let payment_value = serde_json::to_value(payment).map_err(|e| {
        ViError::new(
            ViErrorKind::IssuanceInputInvalid,
            format!("payment serialize: {e}"),
        )
    })?;

    let (checkout_disc, checkout_hash) = create_disclosure("checkout_mandate", &checkout_value)?;
    let (payment_disc, payment_hash) = create_disclosure("payment_mandate", &payment_value)?;

    let header = json!({
        "alg": "ES256",
        "typ": "kb-sd-jwt"
    });

    let payload = json!({
        "nonce": nonce,
        "aud": audience,
        "iat": iat,
        "exp": exp,
        "sd_hash": l1_hash,
        "_sd_alg": "sha-256",
        "_sd": [checkout_hash, payment_hash],
        "delegate_payload": [
            {"...": checkout_hash},
            {"...": payment_hash}
        ]
    });

    let kb_jwt = jws_sign(
        header.to_string().as_bytes(),
        payload.to_string().as_bytes(),
        user_key,
    )?;

    let serialized = serialize_sd_jwt(serialized_l1, &[checkout_disc, payment_disc], Some(&kb_jwt));

    Ok(ImmediateL2Result {
        serialized,
        sd_hash: l1_hash,
    })
}

// ── L2 Autonomous mode ───────────────────────────────────────────────

/// Result of creating an L2 Autonomous credential.
#[derive(Debug)]
pub struct AutonomousL2Result {
    /// The serialized SD-JWT string.
    pub serialized: String,
    /// The SD hash of the L1 that was bound.
    pub sd_hash: String,
    /// Disclosure hash of the checkout mandate (needed for `payment.reference`).
    pub checkout_disclosure_hash: String,
}

/// Create an L2 Autonomous-mode credential with constraints and agent key binding.
pub fn create_layer2_autonomous(
    serialized_l1: &str,
    checkout: &OpenCheckoutMandate,
    payment: &OpenPaymentMandate,
    audience: &str,
    nonce: &str,
    user_key: &EcdsaKeyPair,
    iat: i64,
    exp: i64,
) -> Result<AutonomousL2Result, ViError> {
    // Validate cnf parity between checkout and payment mandates
    if checkout.cnf != payment.cnf {
        return Err(ViError::new(
            ViErrorKind::ModeMismatch,
            "checkout and payment mandates must bind the same agent key (cnf mismatch)",
        ));
    }

    let l1_hash = sd_hash(serialized_l1);

    let checkout_value = serde_json::to_value(checkout).map_err(|e| {
        ViError::new(
            ViErrorKind::IssuanceInputInvalid,
            format!("checkout serialize: {e}"),
        )
    })?;
    let payment_value = serde_json::to_value(payment).map_err(|e| {
        ViError::new(
            ViErrorKind::IssuanceInputInvalid,
            format!("payment serialize: {e}"),
        )
    })?;

    let (checkout_disc, checkout_hash) = create_disclosure("checkout_mandate", &checkout_value)?;
    let (payment_disc, payment_hash) = create_disclosure("payment_mandate", &payment_value)?;

    let header = json!({
        "alg": "ES256",
        "typ": "kb-sd-jwt+kb"
    });

    let payload = json!({
        "nonce": nonce,
        "aud": audience,
        "iat": iat,
        "exp": exp,
        "sd_hash": l1_hash,
        "_sd_alg": "sha-256",
        "_sd": [checkout_hash, payment_hash],
        "delegate_payload": [
            {"...": checkout_hash},
            {"...": payment_hash}
        ]
    });

    let kb_jwt = jws_sign(
        header.to_string().as_bytes(),
        payload.to_string().as_bytes(),
        user_key,
    )?;

    let serialized = serialize_sd_jwt(serialized_l1, &[checkout_disc, payment_disc], Some(&kb_jwt));

    Ok(AutonomousL2Result {
        serialized,
        sd_hash: l1_hash,
        checkout_disclosure_hash: checkout_hash,
    })
}

// ── L3 Issuance (Autonomous only) ────────────────────────────────────

/// Result of creating an L3 payment credential.
#[derive(Debug)]
pub struct L3PaymentResult {
    /// The serialized KB-SD-JWT for the payment network.
    pub serialized: String,
}

/// Create an L3a payment mandate signed by the agent's key.
pub fn create_layer3_payment(
    serialized_l2: &str,
    mandate: &PaymentL3Mandate,
    agent_key: &EcdsaKeyPair,
    agent_jwk: &Jwk,
    iat: i64,
    exp: i64,
) -> Result<L3PaymentResult, ViError> {
    let l2_hash = sd_hash(serialized_l2);

    let header = json!({
        "alg": "ES256",
        "typ": "kb-sd-jwt",
        "jwk": agent_jwk,
        "kid": agent_jwk.x
    });

    let mandate_value = serde_json::to_value(mandate).map_err(|e| {
        ViError::new(
            ViErrorKind::IssuanceInputInvalid,
            format!("L3a mandate serialize: {e}"),
        )
    })?;

    let payload = json!({
        "iat": iat,
        "exp": exp,
        "sd_hash": l2_hash,
        "mandate": mandate_value
    });

    let jwt = jws_sign(
        header.to_string().as_bytes(),
        payload.to_string().as_bytes(),
        agent_key,
    )?;

    // L3 has no disclosures in the reference implementation
    let serialized = serialize_sd_jwt(&jwt, &[], None);

    Ok(L3PaymentResult { serialized })
}

/// Result of creating an L3 checkout credential.
#[derive(Debug)]
pub struct L3CheckoutResult {
    /// The serialized KB-SD-JWT for the merchant.
    pub serialized: String,
}

/// Create an L3b checkout mandate signed by the agent's key.
pub fn create_layer3_checkout(
    serialized_l2: &str,
    mandate: &CheckoutL3Mandate,
    agent_key: &EcdsaKeyPair,
    agent_jwk: &Jwk,
    iat: i64,
    exp: i64,
) -> Result<L3CheckoutResult, ViError> {
    let l2_hash = sd_hash(serialized_l2);

    let header = json!({
        "alg": "ES256",
        "typ": "kb-sd-jwt",
        "jwk": agent_jwk,
        "kid": agent_jwk.x
    });

    let mandate_value = serde_json::to_value(mandate).map_err(|e| {
        ViError::new(
            ViErrorKind::IssuanceInputInvalid,
            format!("L3b mandate serialize: {e}"),
        )
    })?;

    let payload = json!({
        "iat": iat,
        "exp": exp,
        "sd_hash": l2_hash,
        "mandate": mandate_value
    });

    let jwt = jws_sign(
        header.to_string().as_bytes(),
        payload.to_string().as_bytes(),
        agent_key,
    )?;

    let serialized = serialize_sd_jwt(&jwt, &[], None);

    Ok(L3CheckoutResult { serialized })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::verifiable_intent::crypto::{generate_ec_p256, load_key_pair};
    use crate::verifiable_intent::types::{
        Cnf, Constraint, Entity, FulfillmentLineItem, PaymentAmount, PaymentInstrument,
    };

    fn test_issuer_l1() -> String {
        // Minimal L1 SD-JWT for testing (not cryptographically valid, just structural)
        "eyJhbGciOiJFUzI1NiIsInR5cCI6InNkK2p3dCJ9.eyJpc3MiOiJodHRwczovL2lzc3Vlci5leGFtcGxlLmNvbSJ9.sig~".to_string()
    }

    #[test]
    fn create_immediate_l2() {
        let (pkcs8, _jwk) = generate_ec_p256().unwrap();
        let user_key = load_key_pair(&pkcs8).unwrap();
        let l1 = test_issuer_l1();

        let checkout = FinalCheckoutMandate {
            vct: "mandate.checkout".into(),
            checkout_jwt: "merchant.jwt.here".into(),
            checkout_hash: sd_hash("merchant.jwt.here"),
        };
        let payment = FinalPaymentMandate {
            vct: "mandate.payment".into(),
            payment_instrument: PaymentInstrument {
                instrument_type: "card".into(),
                id: "tok-1".into(),
                description: None,
            },
            currency: "USD".into(),
            amount: 27999,
            payee: Entity {
                id: None,
                name: "Test Store".into(),
                website: "https://store.example.com".into(),
            },
            transaction_id: sd_hash("merchant.jwt.here"),
        };

        let result = create_layer2_immediate(
            &l1,
            &checkout,
            &payment,
            "https://network.example.com",
            "nonce-123",
            &user_key,
            1_700_000_000,
            1_700_000_900,
        )
        .unwrap();

        assert!(!result.serialized.is_empty());
        assert!(!result.sd_hash.is_empty());
        // The serialized form should contain the L1 as prefix
        assert!(result.serialized.starts_with(&l1));
    }

    #[test]
    fn create_autonomous_l2() {
        let (user_pkcs8, _user_jwk) = generate_ec_p256().unwrap();
        let user_key = load_key_pair(&user_pkcs8).unwrap();
        let (_agent_pkcs8, agent_jwk) = generate_ec_p256().unwrap();
        let l1 = test_issuer_l1();

        let cnf = Cnf {
            jwk: agent_jwk,
            kid: Some("agent-key-1".into()),
        };

        let checkout = OpenCheckoutMandate {
            vct: "mandate.checkout.open".into(),
            cnf: cnf.clone(),
            constraints: vec![Constraint::AllowedMerchant {
                allowed_merchants: vec![Entity {
                    id: None,
                    name: "Test Store".into(),
                    website: "https://store.example.com".into(),
                }],
            }],
            prompt_summary: Some("Buy a test product".into()),
        };
        let payment = OpenPaymentMandate {
            vct: "mandate.payment.open".into(),
            cnf,
            payment_instrument: PaymentInstrument {
                instrument_type: "card".into(),
                id: "tok-1".into(),
                description: None,
            },
            constraints: vec![Constraint::PaymentAmount {
                currency: "USD".into(),
                min: Some(10000),
                max: Some(40000),
            }],
        };

        let result = create_layer2_autonomous(
            &l1,
            &checkout,
            &payment,
            "https://network.example.com",
            "nonce-456",
            &user_key,
            1_700_000_000,
            1_700_086_400,
        )
        .unwrap();

        assert!(!result.serialized.is_empty());
        assert!(!result.checkout_disclosure_hash.is_empty());
    }

    #[test]
    fn create_autonomous_l2_cnf_mismatch_fails() {
        let (user_pkcs8, _user_jwk) = generate_ec_p256().unwrap();
        let user_key = load_key_pair(&user_pkcs8).unwrap();
        let (_a1, agent_jwk1) = generate_ec_p256().unwrap();
        let (_a2, agent_jwk2) = generate_ec_p256().unwrap();
        let l1 = test_issuer_l1();

        let checkout = OpenCheckoutMandate {
            vct: "mandate.checkout.open".into(),
            cnf: Cnf {
                jwk: agent_jwk1,
                kid: Some("key-1".into()),
            },
            constraints: vec![],
            prompt_summary: None,
        };
        let payment = OpenPaymentMandate {
            vct: "mandate.payment.open".into(),
            cnf: Cnf {
                jwk: agent_jwk2,
                kid: Some("key-2".into()),
            },
            payment_instrument: PaymentInstrument {
                instrument_type: "card".into(),
                id: "tok-1".into(),
                description: None,
            },
            constraints: vec![],
        };

        let err = create_layer2_autonomous(
            &l1,
            &checkout,
            &payment,
            "https://network.example.com",
            "nonce",
            &user_key,
            1_700_000_000,
            1_700_086_400,
        )
        .unwrap_err();

        assert_eq!(err.kind, ViErrorKind::ModeMismatch);
    }

    #[test]
    fn create_l3_payment_and_checkout() {
        let (agent_pkcs8, agent_jwk) = generate_ec_p256().unwrap();
        let agent_key = load_key_pair(&agent_pkcs8).unwrap();
        let l2_serialized = "l2.serialized.form~disc1~disc2~kb.jwt";

        let checkout_jwt = "merchant.checkout.jwt";
        let checkout_hash = sd_hash(checkout_jwt);

        let l3a_mandate = PaymentL3Mandate {
            vct: "mandate.payment".into(),
            payment_instrument: PaymentInstrument {
                instrument_type: "card".into(),
                id: "tok-1".into(),
                description: None,
            },
            payment_amount: PaymentAmount {
                currency: "USD".into(),
                amount: 27999,
            },
            payee: Entity {
                id: None,
                name: "Test Store".into(),
                website: "https://store.example.com".into(),
            },
            transaction_id: checkout_hash.clone(),
        };

        let l3b_mandate = CheckoutL3Mandate {
            vct: "mandate.checkout".into(),
            checkout_jwt: checkout_jwt.into(),
            checkout_hash,
            line_items: Some(vec![FulfillmentLineItem {
                item_id: "SKU001".into(),
                quantity: 1,
            }]),
        };

        let l3a = create_layer3_payment(
            l2_serialized,
            &l3a_mandate,
            &agent_key,
            &agent_jwk,
            1_700_000_000,
            1_700_000_300,
        )
        .unwrap();
        assert!(!l3a.serialized.is_empty());

        let l3b = create_layer3_checkout(
            l2_serialized,
            &l3b_mandate,
            &agent_key,
            &agent_jwk,
            1_700_000_000,
            1_700_000_300,
        )
        .unwrap();
        assert!(!l3b.serialized.is_empty());
    }
}