From 5e5de35a425b94bd6d59aa20c9f14b9405f5e5c7 Mon Sep 17 00:00:00 2001 From: hosted-fornet Date: Thu, 18 Jan 2024 16:32:09 -0800 Subject: [PATCH 1/3] fix `get_capability()` params check because json is unordered, the stored json params could sometimes be a different order than the one checked by the caller; now convert to json and check equality of all fields --- src/lib.rs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 3ea744d..28f100f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,6 +15,7 @@ //! pub use crate::kinode::process::standard::*; use serde::{Deserialize, Serialize}; +use serde_json::Value; wit_bindgen::generate!({ path: "kinode-wit", @@ -206,8 +207,36 @@ pub fn can_message(address: &Address) -> bool { /// Get a capability in our store /// NOTE unfortunatly this is O(n), not sure if wit let's us do any better pub fn get_capability(our: &Address, params: &str) -> Option { + let params = serde_json::from_str::(params).unwrap_or_default(); crate::our_capabilities() .iter() - .find(|cap| cap.issuer == *our && cap.params == params) + .find(|cap| { + let cap_params = serde_json::from_str::(&cap.params).unwrap_or_default(); + cap.issuer == *our && are_equal_json_values(¶ms, &cap_params) + }) .cloned() } + +fn are_equal_json_objects(obj1: &Value, obj2: &Value) -> bool { + match (obj1.as_object(), obj2.as_object()) { + (Some(map1), Some(map2)) => { + if map1.len() != map2.len() { + return false; + } + + map1.iter().all(|(key, val1)| { + map2.get(key) + .map_or(false, |val2| are_equal_json_values(val1, val2)) + }) + } + _ => false, + } +} + +fn are_equal_json_values(val1: &Value, val2: &Value) -> bool { + match (val1, val2) { + (Value::Object(_), Value::Object(_)) => are_equal_json_objects(val1, val2), + _ => val1 == val2, + } +} + From 8d98e670124d6b77eaeab8db63949bc798e55273 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 19 Jan 2024 00:33:03 +0000 Subject: [PATCH 2/3] Format Rust code using rustfmt --- src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 28f100f..b3ed519 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -239,4 +239,3 @@ fn are_equal_json_values(val1: &Value, val2: &Value) -> bool { _ => val1 == val2, } } - From 437093e884deefc436a71978f5422a47bdddc5a5 Mon Sep 17 00:00:00 2001 From: bitful-pannul Date: Sat, 20 Jan 2024 21:51:11 -0300 Subject: [PATCH 3/3] get_capabilit(): use serde_json Eq --- src/lib.rs | 26 +------------------------- 1 file changed, 1 insertion(+), 25 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b3ed519..19b3ba1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -205,37 +205,13 @@ pub fn can_message(address: &Address) -> bool { } /// Get a capability in our store -/// NOTE unfortunatly this is O(n), not sure if wit let's us do any better pub fn get_capability(our: &Address, params: &str) -> Option { let params = serde_json::from_str::(params).unwrap_or_default(); crate::our_capabilities() .iter() .find(|cap| { let cap_params = serde_json::from_str::(&cap.params).unwrap_or_default(); - cap.issuer == *our && are_equal_json_values(¶ms, &cap_params) + cap.issuer == *our && params == cap_params }) .cloned() } - -fn are_equal_json_objects(obj1: &Value, obj2: &Value) -> bool { - match (obj1.as_object(), obj2.as_object()) { - (Some(map1), Some(map2)) => { - if map1.len() != map2.len() { - return false; - } - - map1.iter().all(|(key, val1)| { - map2.get(key) - .map_or(false, |val2| are_equal_json_values(val1, val2)) - }) - } - _ => false, - } -} - -fn are_equal_json_values(val1: &Value, val2: &Value) -> bool { - match (val1, val2) { - (Value::Object(_), Value::Object(_)) => are_equal_json_objects(val1, val2), - _ => val1 == val2, - } -}