From d578292471c5db9ec3fa84960a8c353f73d34837 Mon Sep 17 00:00:00 2001 From: Jimmy Cuadra Date: Fri, 12 Jul 2019 02:53:21 -0700 Subject: [PATCH] Rename to_canonical_json to canonical_json. --- src/functions.rs | 22 ++++++++++------------ src/lib.rs | 8 ++++---- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/functions.rs b/src/functions.rs index 92abbcf6..7390a566 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -160,7 +160,8 @@ where 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 /// @@ -169,11 +170,11 @@ where /// # Errors /// /// Returns an error if the provided JSON value is not a JSON object. -pub fn to_canonical_json(value: &Value) -> Result { - to_canonical_json_with_fields_to_remove(value, CANONICAL_JSON_FIELDS_TO_REMOVE) +pub fn canonical_json(value: &Value) -> Result { + 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 /// @@ -321,7 +322,7 @@ pub fn verify_json_with( where 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. @@ -339,7 +340,7 @@ where /// /// Returns an error if the provided JSON value is not a JSON object. pub fn content_hash(value: &Value) -> Result { - 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()); @@ -365,7 +366,7 @@ pub fn reference_hash(value: &Value) -> Result { let redacted_value = redact(value)?; 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()); @@ -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)?; @@ -672,10 +673,7 @@ where /// Internal implementation detail of the canonical JSON algorithm. Allows customization of the /// fields that will be removed before serializing. -fn to_canonical_json_with_fields_to_remove( - value: &Value, - fields: &[&str], -) -> Result { +fn canonical_json_with_fields_to_remove(value: &Value, fields: &[&str]) -> Result { if !value.is_object() { return Err(Error::new("JSON value must be a JSON object")); } diff --git a/src/lib.rs b/src/lib.rs index 5eadf6fd..9c834df5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -101,7 +101,7 @@ use std::{ }; 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, }; pub use keys::{Ed25519KeyPair, KeyPair}; @@ -186,8 +186,8 @@ mod test { use serde_json::{from_str, to_string, to_value, Value}; use super::{ - hash_and_sign_event, sign_json, to_canonical_json, verify_event, verify_json, - Ed25519KeyPair, Ed25519Verifier, + canonical_json, hash_and_sign_event, sign_json, verify_event, verify_json, Ed25519KeyPair, + Ed25519Verifier, }; const PUBLIC_KEY: &str = "XGX0JRS2Af3be3knz2fBiRbApjm2Dh61gXDJA8kcJNI"; @@ -197,7 +197,7 @@ mod test { fn test_canonical_json(input: &str) -> String { let value = from_str::(input).unwrap(); - to_canonical_json(&value).unwrap() + canonical_json(&value).unwrap() } #[test]