ratify-protocol 2.0.0-beta.2

Delegated-authority proofs for human-agent and agent-agent interactions — Rust reference SDK for the Ratify Protocol.
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
//! Strict wire JSON for Protocol v2.
//!
//! The parser enforces the Go reference's `checkWireJSONV2` rules in one pass:
//! valid UTF-8, no unpaired UTF-16 surrogate escapes, no duplicate object keys
//! (compared after unescaping), integer tokens inside the safe-integer range,
//! no negative zero, bounded container nesting, and no trailing data. Every
//! node records its byte span so callers can hash exact embedded bytes.
//!
//! Field access mirrors Go `encoding/json` decoding into structs with
//! `DisallowUnknownFields`: an absent or `null` member yields the zero value,
//! while a wrong JSON type is an error. Required/non-null checks are applied
//! separately by [`Obj::require`], exactly where Go calls `requireV2Fields`.

use base64::{engine::general_purpose::STANDARD, Engine as _};

use super::errors::{plain, V2Error, V2Result};
use crate::canonical::wire_int::MAX_SAFE_INTEGER;
use crate::types::{HybridPublicKey, HybridSignature, MAX_JSON_NESTING_DEPTH};

/// Protocol byte-field member names (Go `wireByteFields`), checked at any depth.
const WIRE_BYTE_FIELDS: &[&str] = &[
    "bundle_hash",
    "chain_hash",
    "challenge",
    "context_hash",
    "ed25519",
    "entry_data",
    "mac",
    "ml_dsa_65",
    "prev_hash",
    "session_context",
    "stream_id",
    "terms_canonical_json",
];

#[derive(Debug, Clone, PartialEq)]
pub(crate) enum JV {
    Null,
    Bool(bool),
    /// Raw number token text.
    Num(String),
    Str(String),
    Arr(Vec<JNode>),
    Obj(Vec<(String, JNode)>),
}

#[derive(Debug, Clone, PartialEq)]
pub(crate) struct JNode {
    pub v: JV,
    pub start: usize,
    pub end: usize,
}

impl JNode {
    pub(crate) fn is_null(&self) -> bool {
        matches!(self.v, JV::Null)
    }
}

struct Parser<'a> {
    s: &'a [u8],
    i: usize,
}

impl<'a> Parser<'a> {
    fn ws(&mut self) {
        while self.i < self.s.len() && matches!(self.s[self.i], b' ' | b'\t' | b'\n' | b'\r') {
            self.i += 1;
        }
    }

    fn value(&mut self, depth: usize) -> V2Result<JNode> {
        self.ws();
        let start = self.i;
        let Some(&c) = self.s.get(self.i) else {
            return plain("wire: invalid JSON: unexpected end of input");
        };
        let v = match c {
            b'{' => {
                if depth >= MAX_JSON_NESTING_DEPTH {
                    return plain("wire: JSON nesting exceeds MAX_JSON_NESTING_DEPTH");
                }
                self.i += 1;
                let mut fields: Vec<(String, JNode)> = Vec::new();
                self.ws();
                if self.s.get(self.i) == Some(&b'}') {
                    self.i += 1;
                } else {
                    loop {
                        self.ws();
                        if self.s.get(self.i) != Some(&b'"') {
                            return plain("wire: invalid JSON: expected object key");
                        }
                        let key = self.string()?;
                        if fields.iter().any(|(k, _)| *k == key) {
                            return plain(format!("wire: duplicate key {key:?} in JSON object"));
                        }
                        self.ws();
                        if self.s.get(self.i) != Some(&b':') {
                            return plain("wire: invalid JSON: expected ':'");
                        }
                        self.i += 1;
                        let value = self.value(depth + 1)?;
                        fields.push((key, value));
                        self.ws();
                        match self.s.get(self.i) {
                            Some(b',') => self.i += 1,
                            Some(b'}') => {
                                self.i += 1;
                                break;
                            }
                            _ => return plain("wire: invalid JSON: expected ',' or '}'"),
                        }
                    }
                }
                JV::Obj(fields)
            }
            b'[' => {
                if depth >= MAX_JSON_NESTING_DEPTH {
                    return plain("wire: JSON nesting exceeds MAX_JSON_NESTING_DEPTH");
                }
                self.i += 1;
                let mut items = Vec::new();
                self.ws();
                if self.s.get(self.i) == Some(&b']') {
                    self.i += 1;
                } else {
                    loop {
                        items.push(self.value(depth + 1)?);
                        self.ws();
                        match self.s.get(self.i) {
                            Some(b',') => self.i += 1,
                            Some(b']') => {
                                self.i += 1;
                                break;
                            }
                            _ => return plain("wire: invalid JSON: expected ',' or ']'"),
                        }
                    }
                }
                JV::Arr(items)
            }
            b'"' => JV::Str(self.string()?),
            b't' => self.literal(b"true", JV::Bool(true))?,
            b'f' => self.literal(b"false", JV::Bool(false))?,
            b'n' => self.literal(b"null", JV::Null)?,
            b'-' | b'0'..=b'9' => JV::Num(self.number()?),
            _ => return plain("wire: invalid JSON: unexpected character"),
        };
        Ok(JNode {
            v,
            start,
            end: self.i,
        })
    }

    fn literal(&mut self, text: &[u8], v: JV) -> V2Result<JV> {
        if self.s[self.i..].starts_with(text) {
            self.i += text.len();
            Ok(v)
        } else {
            plain("wire: invalid JSON literal")
        }
    }

    fn digits(&mut self) -> usize {
        let start = self.i;
        while self.i < self.s.len() && self.s[self.i].is_ascii_digit() {
            self.i += 1;
        }
        self.i - start
    }

    fn number(&mut self) -> V2Result<String> {
        let start = self.i;
        if self.s[self.i] == b'-' {
            self.i += 1;
        }
        match self.s.get(self.i) {
            Some(b'0') => self.i += 1,
            Some(b'1'..=b'9') => {
                self.digits();
            }
            _ => return plain("wire: invalid JSON number"),
        }
        if self.s.get(self.i) == Some(&b'.') {
            self.i += 1;
            if self.digits() == 0 {
                return plain("wire: invalid JSON number");
            }
        }
        if matches!(self.s.get(self.i), Some(b'e' | b'E')) {
            self.i += 1;
            if matches!(self.s.get(self.i), Some(b'+' | b'-')) {
                self.i += 1;
            }
            if self.digits() == 0 {
                return plain("wire: invalid JSON number");
            }
        }
        let text = std::str::from_utf8(&self.s[start..self.i])
            .map_err(|_| V2Error::plain("wire: invalid JSON number"))?;
        // Protocol v2 carries no floating point anywhere on the wire: every
        // number token is an integer spelling, and "-0" is rejected.
        if text.contains(['.', 'e', 'E']) {
            return plain(format!(
                "wire: number {text} is not an integer; v2 forbids floating point"
            ));
        }
        if text == "-0" {
            return plain("wire: negative zero is not a canonical number");
        }
        match text.parse::<i64>() {
            Ok(n) if (-MAX_SAFE_INTEGER..=MAX_SAFE_INTEGER).contains(&n) => {}
            _ => {
                return plain(format!(
                    "wire: integer {text} outside the safe-integer range [-(2^53-1), 2^53-1]"
                ))
            }
        }
        Ok(text.to_string())
    }

    fn hex4(&mut self) -> V2Result<u32> {
        let Some(chunk) = self.s.get(self.i..self.i + 4) else {
            return plain("wire: invalid JSON escape");
        };
        let text =
            std::str::from_utf8(chunk).map_err(|_| V2Error::plain("wire: invalid JSON escape"))?;
        let v = u32::from_str_radix(text, 16)
            .map_err(|_| V2Error::plain("wire: invalid JSON escape"))?;
        self.i += 4;
        Ok(v)
    }

    fn string(&mut self) -> V2Result<String> {
        // Caller guarantees the opening quote.
        self.i += 1;
        let mut out = String::new();
        loop {
            let Some(&c) = self.s.get(self.i) else {
                return plain("wire: invalid JSON: unterminated string");
            };
            match c {
                b'"' => {
                    self.i += 1;
                    return Ok(out);
                }
                b'\\' => {
                    self.i += 1;
                    let Some(&e) = self.s.get(self.i) else {
                        return plain("wire: invalid JSON escape");
                    };
                    self.i += 1;
                    match e {
                        b'"' => out.push('"'),
                        b'\\' => out.push('\\'),
                        b'/' => out.push('/'),
                        b'b' => out.push('\u{0008}'),
                        b'f' => out.push('\u{000c}'),
                        b'n' => out.push('\n'),
                        b'r' => out.push('\r'),
                        b't' => out.push('\t'),
                        b'u' => {
                            let first = self.hex4()?;
                            let code = if (0xD800..0xDC00).contains(&first) {
                                if self.s.get(self.i..self.i + 2) != Some(b"\\u") {
                                    return plain("wire: unpaired UTF-16 surrogate escape");
                                }
                                self.i += 2;
                                let second = self.hex4()?;
                                if !(0xDC00..0xE000).contains(&second) {
                                    return plain("wire: unpaired UTF-16 surrogate escape");
                                }
                                0x10000 + ((first - 0xD800) << 10) + (second - 0xDC00)
                            } else if (0xDC00..0xE000).contains(&first) {
                                return plain("wire: unpaired UTF-16 surrogate escape");
                            } else {
                                first
                            };
                            out.push(
                                char::from_u32(code)
                                    .ok_or_else(|| V2Error::plain("wire: invalid JSON escape"))?,
                            );
                        }
                        _ => return plain("wire: invalid JSON escape"),
                    }
                }
                0x00..=0x1f => return plain("wire: invalid JSON: control character in string"),
                _ => {
                    // Copy one complete UTF-8 scalar (input is validated UTF-8).
                    let len = match c {
                        0x00..=0x7f => 1,
                        0xc0..=0xdf => 2,
                        0xe0..=0xef => 3,
                        _ => 4,
                    };
                    let scalar = self
                        .s
                        .get(self.i..self.i + len)
                        .and_then(|b| std::str::from_utf8(b).ok())
                        .ok_or_else(|| V2Error::plain("wire: invalid UTF-8"))?;
                    out.push_str(scalar);
                    self.i += len;
                }
            }
        }
    }
}

/// Parse untrusted wire JSON under the strict v2 rules and check canonical
/// base64 in byte fields. `top_byte` adds v2-only top-level byte fields and
/// `top_text` exempts top-level members that are text in this schema.
pub(crate) fn parse_wire(data: &[u8], top_byte: &[&str], top_text: &[&str]) -> V2Result<JNode> {
    let root = parse_json(data)?;
    check_byte_fields(&root, 0, top_byte, top_text)?;
    reject_unexpected_null(&root)?;
    Ok(root)
}

/// Parse a status evidence batch (SPEC-v2 §2.1, §6.5 step 3) under the
/// strict JSON value model only (Go `checkWireValueModelV2`), which applies
/// to the whole document, nested objects included. The schema rules of
/// [`parse_wire`] (byte-field spellings and `null`) are not applied, because
/// each entry's `object` is opaque to the batch decoder and is decoded
/// strictly under its code during authentication. The batch decoder itself
/// checks the envelope schema and rejects a `null` envelope, entry, member,
/// or object.
pub(crate) fn parse_wire_envelope(data: &[u8]) -> V2Result<JNode> {
    parse_json(data)
}

/// The only v2 schema members whose value may be `null` (SPEC-v2 §2.1).
const NULLABLE_MEMBERS: &[&str] = &[
    "session_context",
    "stream_id",
    "stream_seq",
    "context_policy_digest",
    "floor",
    "accepted_snapshot",
    "preceding_checkpoint",
];

/// SPEC-v2 §2.1 strict decoding: `null` is malformed as an array element and
/// as the value of any member the schema does not declare nullable, at every
/// nesting level. Values inside a `params` object follow the general value
/// model, where `null` is a value.
pub(crate) fn reject_unexpected_null(node: &JNode) -> V2Result<()> {
    match &node.v {
        JV::Null => plain("wire v2: null is not permitted here"),
        JV::Obj(fields) => {
            for (k, v) in fields {
                if k == "params" || (v.is_null() && NULLABLE_MEMBERS.contains(&k.as_str())) {
                    continue;
                }
                reject_unexpected_null(v)?;
            }
            Ok(())
        }
        JV::Arr(items) => items.iter().try_for_each(reject_unexpected_null),
        _ => Ok(()),
    }
}

/// Parse strict JSON without byte-field checks.
pub(crate) fn parse_json(data: &[u8]) -> V2Result<JNode> {
    if std::str::from_utf8(data).is_err() {
        return plain("wire: invalid UTF-8");
    }
    let mut p = Parser { s: data, i: 0 };
    let root = p.value(0)?;
    p.ws();
    if p.i != data.len() {
        return plain("wire: trailing data after JSON document");
    }
    Ok(root)
}

fn check_byte_fields(
    node: &JNode,
    depth: usize,
    top_byte: &[&str],
    top_text: &[&str],
) -> V2Result<()> {
    match &node.v {
        JV::Obj(fields) => {
            let d = depth + 1;
            for (k, v) in fields {
                if let JV::Str(s) = &v.v {
                    let wire = WIRE_BYTE_FIELDS.contains(&k.as_str())
                        && !(d == 1 && top_text.contains(&k.as_str()));
                    if wire || (d == 1 && top_byte.contains(&k.as_str())) {
                        check_canonical_base64(k, s)?;
                    }
                }
                // A params value is extension or audience params: application
                // data where only the JSON value model applies (SPEC-v2 §2.1,
                // §5.2), so member names inside it are not byte fields.
                if k != "params" {
                    check_byte_fields(v, d, top_byte, top_text)?;
                }
            }
            Ok(())
        }
        JV::Arr(items) => {
            for item in items {
                check_byte_fields(item, depth + 1, top_byte, top_text)?;
            }
            Ok(())
        }
        _ => Ok(()),
    }
}

fn check_canonical_base64(key: &str, s: &str) -> V2Result<()> {
    match STANDARD.decode(s) {
        Ok(decoded) if STANDARD.encode(&decoded) == s => Ok(()),
        _ => plain(format!(
            "wire: field {key:?}: malformed or non-canonical base64"
        )),
    }
}

/// Decode canonical standard base64.
pub(crate) fn b64_decode(s: &str) -> V2Result<Vec<u8>> {
    match STANDARD.decode(s) {
        Ok(decoded) if STANDARD.encode(&decoded) == s => Ok(decoded),
        _ => plain("wire: malformed or non-canonical base64"),
    }
}

/// Object view with Go-struct decoding semantics.
pub(crate) struct Obj<'a> {
    pub fields: &'a [(String, JNode)],
}

pub(crate) fn obj(node: &JNode) -> V2Result<Obj<'_>> {
    match &node.v {
        JV::Obj(fields) => Ok(Obj { fields }),
        _ => plain("wire: expected a JSON object"),
    }
}

impl<'a> Obj<'a> {
    pub(crate) fn get(&self, key: &str) -> Option<&'a JNode> {
        self.fields.iter().find(|(k, _)| k == key).map(|(_, v)| v)
    }

    /// Go `requireV2Fields`: every name present and non-null unless nullable.
    pub(crate) fn require(&self, names: &[&str], nullable: &[&str]) -> V2Result<()> {
        for name in names {
            match self.get(name) {
                None => return plain(format!("wire v2: required field {name:?} is missing")),
                Some(v) if v.is_null() && !nullable.contains(name) => {
                    return plain(format!("wire v2: required field {name:?} must not be null"))
                }
                _ => {}
            }
        }
        Ok(())
    }

    /// Go `DisallowUnknownFields`: reject any member outside `allowed`.
    pub(crate) fn only(&self, allowed: &[&str]) -> V2Result<()> {
        for (k, _) in self.fields {
            if !allowed.contains(&k.as_str()) {
                return plain(format!("wire: unknown field {k:?}"));
            }
        }
        Ok(())
    }

    fn present(&self, key: &str) -> Option<&'a JNode> {
        self.get(key).filter(|v| !v.is_null())
    }

    pub(crate) fn str(&self, key: &str) -> V2Result<String> {
        match self.present(key) {
            None => Ok(String::new()),
            Some(v) => as_str(v),
        }
    }

    pub(crate) fn opt_str(&self, key: &str) -> V2Result<Option<String>> {
        match self.present(key) {
            None => Ok(None),
            Some(v) => as_str(v).map(Some),
        }
    }

    pub(crate) fn i64(&self, key: &str) -> V2Result<i64> {
        match self.present(key) {
            None => Ok(0),
            Some(v) => as_i64(v),
        }
    }

    pub(crate) fn opt_i64(&self, key: &str) -> V2Result<Option<i64>> {
        match self.present(key) {
            None => Ok(None),
            Some(v) => as_i64(v).map(Some),
        }
    }

    pub(crate) fn u16(&self, key: &str) -> V2Result<u16> {
        let n = self.i64(key)?;
        u16::try_from(n).map_err(|_| V2Error::plain(format!("wire: {key} out of range")))
    }

    pub(crate) fn bytes(&self, key: &str) -> V2Result<Vec<u8>> {
        match self.present(key) {
            None => Ok(Vec::new()),
            Some(v) => b64_decode(&as_str(v)?),
        }
    }

    pub(crate) fn opt_bytes(&self, key: &str) -> V2Result<Option<Vec<u8>>> {
        match self.present(key) {
            None => Ok(None),
            Some(v) => b64_decode(&as_str(v)?).map(Some),
        }
    }

    pub(crate) fn str_array(&self, key: &str) -> V2Result<Vec<String>> {
        match self.present(key) {
            None => Ok(Vec::new()),
            Some(v) => match &v.v {
                // A null element is malformed (SPEC-v2 §2.1).
                JV::Arr(items) => items.iter().map(as_str).collect(),
                _ => plain(format!("wire: {key} must be an array of strings")),
            },
        }
    }

    pub(crate) fn array(&self, key: &str) -> V2Result<&'a [JNode]> {
        match self.present(key) {
            None => Ok(&[]),
            Some(v) => match &v.v {
                JV::Arr(items) => Ok(items),
                _ => plain(format!("wire: {key} must be an array")),
            },
        }
    }

    pub(crate) fn pub_key(&self, key: &str) -> V2Result<HybridPublicKey> {
        let (ed25519, ml_dsa_65) = self.pair(key)?;
        Ok(HybridPublicKey { ed25519, ml_dsa_65 })
    }

    pub(crate) fn sig(&self, key: &str) -> V2Result<HybridSignature> {
        let (ed25519, ml_dsa_65) = self.pair(key)?;
        Ok(HybridSignature { ed25519, ml_dsa_65 })
    }

    fn pair(&self, key: &str) -> V2Result<(Vec<u8>, Vec<u8>)> {
        match self.present(key) {
            None => Ok((Vec::new(), Vec::new())),
            Some(v) => {
                let o = obj(v)?;
                o.only(&["ed25519", "ml_dsa_65"])?;
                // A hybrid key or signature needs both members: `{}` or a
                // null component is malformed (SPEC-v2 §2.1).
                o.require(&["ed25519", "ml_dsa_65"], &[])?;
                Ok((o.bytes("ed25519")?, o.bytes("ml_dsa_65")?))
            }
        }
    }
}

pub(crate) fn as_str(v: &JNode) -> V2Result<String> {
    match &v.v {
        JV::Str(s) => Ok(s.clone()),
        _ => plain("wire: expected a JSON string"),
    }
}

pub(crate) fn as_i64(v: &JNode) -> V2Result<i64> {
    match &v.v {
        JV::Num(t) if !t.contains(['.', 'e', 'E']) => t
            .parse::<i64>()
            .map_err(|_| V2Error::plain("wire: integer out of range")),
        _ => plain("wire: expected a JSON integer"),
    }
}

/// Canonical JSON output value. Object keys are sorted by UTF-8 bytes on write.
#[derive(Debug, Clone)]
pub(crate) enum CJ {
    Null,
    Bool(bool),
    Int(i64),
    Str(String),
    Bytes(Vec<u8>),
    /// Pre-encoded canonical JSON (constraints, embedded objects).
    Raw(String),
    Arr(Vec<CJ>),
    Obj(Vec<(String, CJ)>),
}

impl CJ {
    pub(crate) fn obj(fields: Vec<(&str, CJ)>) -> CJ {
        CJ::Obj(
            fields
                .into_iter()
                .map(|(k, v)| (k.to_string(), v))
                .collect(),
        )
    }

    pub(crate) fn s(value: &str) -> CJ {
        CJ::Str(value.to_string())
    }

    pub(crate) fn strs(values: &[String]) -> CJ {
        CJ::Arr(values.iter().map(|v| CJ::Str(v.clone())).collect())
    }

    pub(crate) fn key(k: &HybridPublicKey) -> CJ {
        CJ::obj(vec![
            ("ed25519", CJ::Bytes(k.ed25519.clone())),
            ("ml_dsa_65", CJ::Bytes(k.ml_dsa_65.clone())),
        ])
    }

    pub(crate) fn sig(s: &HybridSignature) -> CJ {
        CJ::obj(vec![
            ("ed25519", CJ::Bytes(s.ed25519.clone())),
            ("ml_dsa_65", CJ::Bytes(s.ml_dsa_65.clone())),
        ])
    }
}

/// RatifyCanonicalJSON (SPEC §6) of `value`. Integers outside the
/// safe-integer range are an error, never emitted.
pub(crate) fn canonical(value: &CJ) -> V2Result<Vec<u8>> {
    let mut out = String::new();
    write(value, &mut out)?;
    Ok(out.into_bytes())
}

fn write(value: &CJ, out: &mut String) -> V2Result<()> {
    match value {
        CJ::Null => out.push_str("null"),
        CJ::Bool(b) => out.push_str(if *b { "true" } else { "false" }),
        CJ::Int(n) => {
            if !(-MAX_SAFE_INTEGER..=MAX_SAFE_INTEGER).contains(n) {
                return plain(format!(
                    "canonical JSON: integer {n} outside the safe-integer range [-(2^53-1), 2^53-1]"
                ));
            }
            out.push_str(&n.to_string());
        }
        CJ::Str(s) => crate::canonical::encode_str(s, out),
        CJ::Bytes(b) => crate::canonical::encode_str(&STANDARD.encode(b), out),
        CJ::Raw(r) => out.push_str(r),
        CJ::Arr(items) => {
            out.push('[');
            for (i, item) in items.iter().enumerate() {
                if i > 0 {
                    out.push(',');
                }
                write(item, out)?;
            }
            out.push(']');
        }
        CJ::Obj(fields) => {
            let mut sorted: Vec<&(String, CJ)> = fields.iter().collect();
            sorted.sort_by(|a, b| a.0.as_bytes().cmp(b.0.as_bytes()));
            out.push('{');
            for (i, (k, v)) in sorted.into_iter().enumerate() {
                if i > 0 {
                    out.push(',');
                }
                crate::canonical::encode_str(k, out);
                out.push(':');
                write(v, out)?;
            }
            out.push('}');
        }
    }
    Ok(())
}

/// Re-encode a parsed node canonically (numbers keep their token text).
pub(crate) fn node_to_cj(node: &JNode) -> CJ {
    match &node.v {
        JV::Null => CJ::Null,
        JV::Bool(b) => CJ::Bool(*b),
        JV::Num(t) => CJ::Raw(t.clone()),
        JV::Str(s) => CJ::Str(s.clone()),
        JV::Arr(items) => CJ::Arr(items.iter().map(node_to_cj).collect()),
        JV::Obj(fields) => CJ::Obj(
            fields
                .iter()
                .map(|(k, v)| (k.clone(), node_to_cj(v)))
                .collect(),
        ),
    }
}

/// Re-encode arbitrary strict JSON as RatifyCanonicalJSON, preserving
/// explicit `null` members (unlike the v1 `canonical_json` helper).
pub fn canonical_json_v2(data: &[u8]) -> V2Result<Vec<u8>> {
    canonical(&node_to_cj(&parse_json(data)?))
}

#[cfg(test)]
mod tests {
    use super::parse_json;

    #[test]
    fn non_integer_and_negative_zero_numbers_are_rejected() {
        for bad in [
            "-0", "-0.0", "-0e5", "-0E0", "-0.5", "1.0", "1e3", "1E3", "0.0", "9007199254740992",
            "-9007199254740992", "01", "+1",
        ] {
            let doc = format!("{{\"a\":{bad}}}");
            assert!(parse_json(doc.as_bytes()).is_err(), "{bad} must be rejected");
        }
        for good in ["0", "-1", "9007199254740991", "-9007199254740991"] {
            let doc = format!("{{\"a\":{good}}}");
            assert!(parse_json(doc.as_bytes()).is_ok(), "{good} must decode");
        }
    }

    #[test]
    fn unpaired_surrogate_escapes_are_rejected() {
        for bad in [r#""\ud800""#, r#""\udc00""#, r#""\ud800\u0041""#, r#""\ud800x""#] {
            assert!(parse_json(bad.as_bytes()).is_err(), "{bad} must be rejected");
        }
        assert!(parse_json(r#""\ud83d\ude00""#.as_bytes()).is_ok());
    }
}