signatures: Simplify sign_json

This commit is contained in:
Jonas Platte 2020-09-29 23:23:47 +02:00
parent 54ba6d10fe
commit 6c8bac949e
No known key found for this signature in database
GPG Key ID: 7D261D771D915378

View File

@ -134,27 +134,25 @@ where
let mut signature_map; let mut signature_map;
let maybe_unsigned; let maybe_unsigned;
// Pull `signatures` and `unsigned` out of the object, and limit the scope of the mutable // Pull `signatures` and `unsigned` out of the object.
// borrow of `value` so we can call `to_string` with it below. let map = match value {
{ Value::Object(map) => map,
let map = match value { _ => return Err(Error::new("JSON value must be a JSON object")),
Value::Object(ref mut map) => map, };
_ => return Err(Error::new("JSON value must be a JSON object")),
};
signature_map = match map.remove("signatures") { // FIXME: Once MSRV >= 1.45.0, use remove_key and don't allocate new `String`s below.
Some(signatures_value) => match signatures_value.as_object() { signature_map = match map.remove("signatures") {
Some(signatures) => from_value(Value::Object(signatures.clone()))?, Some(signatures_value) => match signatures_value.as_object() {
None => return Err(Error::new("field `signatures` must be a JSON object")), Some(signatures) => from_value(Value::Object(signatures.clone()))?,
}, None => return Err(Error::new("field `signatures` must be a JSON object")),
None => BTreeMap::new(), },
}; None => BTreeMap::new(),
};
maybe_unsigned = map.remove("unsigned"); maybe_unsigned = map.remove("unsigned");
}
// Get the canonical JSON. // Get the canonical JSON.
let json = to_string(&value)?; let json = to_string(map)?;
// Sign the canonical JSON. // Sign the canonical JSON.
let signature = key_pair.sign(json.as_bytes()); let signature = key_pair.sign(json.as_bytes());
@ -164,9 +162,6 @@ where
signature_set.insert(signature.id(), signature.base64()); signature_set.insert(signature.id(), signature.base64());
// Safe to unwrap because we did this exact check at the beginning of the function.
let map = value.as_object_mut().unwrap();
// Put `signatures` and `unsigned` back in. // Put `signatures` and `unsigned` back in.
map.insert("signatures".into(), to_value(signature_map)?); map.insert("signatures".into(), to_value(signature_map)?);