Rename to_canonical_json to canonical_json.

This commit is contained in:
Jimmy Cuadra 2019-07-12 02:53:21 -07:00
parent a8b8e70c2d
commit d578292471
2 changed files with 14 additions and 16 deletions

View File

@ -160,7 +160,8 @@ where
Ok(()) Ok(())
} }
/// Converts a JSON object into the "canonical" string form. /// Converts a JSON object into the
/// [canonical](https://matrix.org/docs/spec/appendices#canonical-json) string form.
/// ///
/// # Parameters /// # Parameters
/// ///
@ -169,11 +170,11 @@ where
/// # Errors /// # Errors
/// ///
/// Returns an error if the provided JSON value is not a JSON object. /// Returns an error if the provided JSON value is not a JSON object.
pub fn to_canonical_json(value: &Value) -> Result<String, Error> { pub fn canonical_json(value: &Value) -> Result<String, Error> {
to_canonical_json_with_fields_to_remove(value, CANONICAL_JSON_FIELDS_TO_REMOVE) canonical_json_with_fields_to_remove(value, CANONICAL_JSON_FIELDS_TO_REMOVE)
} }
/// Uses a public key to verify a signature of a JSON object. /// Uses a set of public keys to verify a signed JSON object.
/// ///
/// # Parameters /// # Parameters
/// ///
@ -321,7 +322,7 @@ pub fn verify_json_with<V>(
where where
V: Verifier, V: Verifier,
{ {
verifier.verify_json(public_key, signature, to_canonical_json(value)?.as_bytes()) verifier.verify_json(public_key, signature, canonical_json(value)?.as_bytes())
} }
/// Creates a *content hash* for the JSON representation of an event. /// Creates a *content hash* for the JSON representation of an event.
@ -339,7 +340,7 @@ where
/// ///
/// Returns an error if the provided JSON value is not a JSON object. /// Returns an error if the provided JSON value is not a JSON object.
pub fn content_hash(value: &Value) -> Result<String, Error> { pub fn content_hash(value: &Value) -> Result<String, Error> {
let json = to_canonical_json_with_fields_to_remove(value, CONTENT_HASH_FIELDS_TO_REMOVE)?; let json = canonical_json_with_fields_to_remove(value, CONTENT_HASH_FIELDS_TO_REMOVE)?;
let hash = digest(&SHA256, json.as_bytes()); let hash = digest(&SHA256, json.as_bytes());
@ -365,7 +366,7 @@ pub fn reference_hash(value: &Value) -> Result<String, Error> {
let redacted_value = redact(value)?; let redacted_value = redact(value)?;
let json = let json =
to_canonical_json_with_fields_to_remove(&redacted_value, REFERENCE_HASH_FIELDS_TO_REMOVE)?; canonical_json_with_fields_to_remove(&redacted_value, REFERENCE_HASH_FIELDS_TO_REMOVE)?;
let hash = digest(&SHA256, json.as_bytes()); let hash = digest(&SHA256, json.as_bytes());
@ -647,7 +648,7 @@ where
} }
}; };
let canonical_json = from_str(&to_canonical_json(&redacted)?)?; let canonical_json = from_str(&canonical_json(&redacted)?)?;
let signature_bytes = decode_config(signature, STANDARD_NO_PAD)?; let signature_bytes = decode_config(signature, STANDARD_NO_PAD)?;
@ -672,10 +673,7 @@ where
/// Internal implementation detail of the canonical JSON algorithm. Allows customization of the /// Internal implementation detail of the canonical JSON algorithm. Allows customization of the
/// fields that will be removed before serializing. /// fields that will be removed before serializing.
fn to_canonical_json_with_fields_to_remove( fn canonical_json_with_fields_to_remove(value: &Value, fields: &[&str]) -> Result<String, Error> {
value: &Value,
fields: &[&str],
) -> Result<String, Error> {
if !value.is_object() { if !value.is_object() {
return Err(Error::new("JSON value must be a JSON object")); return Err(Error::new("JSON value must be a JSON object"));
} }

View File

@ -101,7 +101,7 @@ use std::{
}; };
pub use functions::{ pub use functions::{
content_hash, hash_and_sign_event, redact, reference_hash, sign_json, to_canonical_json, canonical_json, content_hash, hash_and_sign_event, redact, reference_hash, sign_json,
verify_event, verify_json, verify_event, verify_json,
}; };
pub use keys::{Ed25519KeyPair, KeyPair}; pub use keys::{Ed25519KeyPair, KeyPair};
@ -186,8 +186,8 @@ mod test {
use serde_json::{from_str, to_string, to_value, Value}; use serde_json::{from_str, to_string, to_value, Value};
use super::{ use super::{
hash_and_sign_event, sign_json, to_canonical_json, verify_event, verify_json, canonical_json, hash_and_sign_event, sign_json, verify_event, verify_json, Ed25519KeyPair,
Ed25519KeyPair, Ed25519Verifier, Ed25519Verifier,
}; };
const PUBLIC_KEY: &str = "XGX0JRS2Af3be3knz2fBiRbApjm2Dh61gXDJA8kcJNI"; const PUBLIC_KEY: &str = "XGX0JRS2Af3be3knz2fBiRbApjm2Dh61gXDJA8kcJNI";
@ -197,7 +197,7 @@ mod test {
fn test_canonical_json(input: &str) -> String { fn test_canonical_json(input: &str) -> String {
let value = from_str::<Value>(input).unwrap(); let value = from_str::<Value>(input).unwrap();
to_canonical_json(&value).unwrap() canonical_json(&value).unwrap()
} }
#[test] #[test]