Skip to content

Commit 0256795

Browse files
committed
nits
1 parent 3126fb4 commit 0256795

File tree

1 file changed

+37
-45
lines changed

1 file changed

+37
-45
lines changed

crates/mpc-attestation/build.rs

Lines changed: 37 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::env;
22
use std::fs::{self, File};
33
use std::io::Write;
4-
use std::path::Path;
54
use std::path::PathBuf;
65

76
const ASSETS_DIR_NAME: &str = "assets";
@@ -96,13 +95,23 @@ fn main() {
9695
.unwrap_or_else(|e| panic!("Failed to parse JSON file '{}': {}", path.display(), e));
9796

9897
// Extract RTMRs + MRTD
99-
let mrtd = decode_measurement(&tcb, "mrtd", &path);
100-
let rtmr0 = decode_measurement(&tcb, "rtmr0", &path);
101-
let rtmr1 = decode_measurement(&tcb, "rtmr1", &path);
102-
let rtmr2 = decode_measurement(&tcb, "rtmr2", &path);
98+
let mrtd = decode_measurement(&tcb, "mrtd")
99+
.map_err(|e| format!("Error in file '{}': {}", path.display(), e))
100+
.unwrap();
101+
let rtmr0 = decode_measurement(&tcb, "rtmr0")
102+
.map_err(|e| format!("Error in file '{}': {}", path.display(), e))
103+
.unwrap();
104+
let rtmr1 = decode_measurement(&tcb, "rtmr1")
105+
.map_err(|e| format!("Error in file '{}': {}", path.display(), e))
106+
.unwrap();
107+
let rtmr2 = decode_measurement(&tcb, "rtmr2")
108+
.map_err(|e| format!("Error in file '{}': {}", path.display(), e))
109+
.unwrap();
103110

104111
// Extract key-provider digest
105-
let key_provider_digest = extract_key_provider_digest(&tcb, &path);
112+
let key_provider_digest = extract_key_provider_digest(&tcb)
113+
.map_err(|e| format!("Error in file '{}': {}", path.display(), e))
114+
.unwrap();
106115

107116
// Emit Rust struct
108117
writeln!(
@@ -124,64 +133,47 @@ fn main() {
124133
writeln!(f, "];").expect("failed writing closing bracket");
125134
}
126135

127-
/// Extract a measurement field and decode hex, with good error messages.
128-
fn decode_measurement(tcb: &serde_json::Value, field: &str, path: &Path) -> [u8; 48] {
129-
let hex = tcb[field].as_str().unwrap_or_else(|| {
130-
panic!(
131-
"Field '{}' missing or not a string in '{}'",
132-
field,
133-
path.display()
134-
)
135-
});
136+
/// Extract 48-byte measurement from JSON (mrtd, rtmr0, rtmr1, rtmr2)
137+
fn decode_measurement(tcb: &serde_json::Value, field: &str) -> Result<[u8; 48], String> {
138+
let hex = tcb[field]
139+
.as_str()
140+
.ok_or_else(|| format!("Field '{}' missing or not a string", field))?;
136141

137-
decode_hex(hex, field, path)
142+
decode_hex(hex, field)
138143
}
139144

140-
/// Extract the key-provider hash with clear diagnostics
141-
fn extract_key_provider_digest(tcb: &serde_json::Value, path: &Path) -> [u8; 48] {
145+
/// Extract the key-provider event digest
146+
fn extract_key_provider_digest(tcb: &serde_json::Value) -> Result<[u8; 48], String> {
142147
let events = tcb["event_log"]
143148
.as_array()
144-
.unwrap_or_else(|| panic!("event_log missing or not an array in '{}'", path.display()));
149+
.ok_or_else(|| "event_log missing or not an array".to_string())?;
145150

146151
for event in events {
147-
let event_name = event["event"].as_str().unwrap_or("");
148-
149-
if event_name == "key-provider" {
150-
let digest_hex = event["digest"].as_str().unwrap_or_else(|| {
151-
panic!(
152-
"key-provider event in '{}' does not contain a valid digest",
153-
path.display()
154-
)
155-
});
152+
if event["event"].as_str().unwrap_or("") == "key-provider" {
153+
let digest_hex = event["digest"]
154+
.as_str()
155+
.ok_or_else(|| "key-provider event missing digest".to_string())?;
156156

157-
return decode_hex(digest_hex, "key-provider digest", path);
157+
return decode_hex(digest_hex, "key-provider digest");
158158
}
159159
}
160160

161-
panic!("No key-provider event found in '{}'", path.display());
161+
Err("No key-provider event found".to_string())
162162
}
163163

164-
/// Decode a hex string into a 48-byte array with validation
165-
fn decode_hex(hex: &str, field: &str, path: &Path) -> [u8; 48] {
166-
let bytes = hex::decode(hex).unwrap_or_else(|e| {
167-
panic!(
168-
"Invalid hex in field '{}' in '{}': {}",
169-
field,
170-
path.display(),
171-
e
172-
)
173-
});
164+
/// Decode a hex field into a 48-byte array
165+
fn decode_hex(hex: &str, field: &str) -> Result<[u8; 48], String> {
166+
let bytes = hex::decode(hex).map_err(|e| format!("Invalid hex in '{}': {}", field, e))?;
174167

175168
if bytes.len() != 48 {
176-
panic!(
177-
"Expected 48-byte measurement for field '{}' in '{}', got {} bytes",
169+
return Err(format!(
170+
"Expected 48-byte measurement for '{}', got {} bytes",
178171
field,
179-
path.display(),
180172
bytes.len()
181-
);
173+
));
182174
}
183175

184176
let mut arr = [0u8; 48];
185177
arr.copy_from_slice(&bytes);
186-
arr
178+
Ok(arr)
187179
}

0 commit comments

Comments
 (0)