signatures: Simplify sign_json
This commit is contained in:
parent
54ba6d10fe
commit
6c8bac949e
@ -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)?);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user