Skip to content

Commit 9e761b8

Browse files
committed
refactor: use serde_jcs for RFC 8785 compliant canonical JSON
Replace manual BTreeMap + serde_json with serde_jcs::to_string which handles all JCS requirements (key ordering, number formatting, string escaping) per RFC 8785 specification.
1 parent 4712710 commit 9e761b8

3 files changed

Lines changed: 27 additions & 7 deletions

File tree

Cargo.lock

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cc-eventlog/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ hex.workspace = true
2020
scale.workspace = true
2121
serde.workspace = true
2222
serde-human-bytes.workspace = true
23+
serde_jcs = "0.2.0"
2324
serde_json = { workspace = true, features = ["alloc"] }
2425
sha2.workspace = true
2526

cc-eventlog/src/runtime_events.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,16 @@ impl RuntimeEvent {
140140

141141
/// Construct JCS (RFC 8785) canonical JSON for a runtime event.
142142
///
143-
/// Uses `BTreeMap` to guarantee alphabetical key ordering per JCS.
143+
/// Uses `serde_jcs` for deterministic serialization per RFC 8785,
144+
/// including alphabetical key ordering and canonical number/string formatting.
144145
/// The payload is hex-encoded for human readability.
145146
pub fn canonical_event_json(event: &str, event_type: u32, payload: &[u8]) -> String {
146-
use std::collections::BTreeMap;
147-
let mut map = BTreeMap::new();
148-
map.insert("event", serde_json::Value::String(event.to_string()));
149-
map.insert("event_type", serde_json::Value::Number(event_type.into()));
150-
map.insert("payload", serde_json::Value::String(hex::encode(payload)));
151-
serde_json::to_string(&map).unwrap_or_default()
147+
let obj = serde_json::json!({
148+
"event": event,
149+
"event_type": event_type,
150+
"payload": hex::encode(payload),
151+
});
152+
serde_jcs::to_string(&obj).unwrap_or_default()
152153
}
153154

154155
/// Replay event logs

0 commit comments

Comments
 (0)