From 7d0aa89387f847adef459985cba9dacd8858134e Mon Sep 17 00:00:00 2001 From: NicolasCantu Date: Wed, 15 Jan 2025 13:05:01 +0100 Subject: [PATCH] Add children process validation method --- src/api.rs | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 1 deletion(-) diff --git a/src/api.rs b/src/api.rs index 1700656..73fa898 100644 --- a/src/api.rs +++ b/src/api.rs @@ -50,7 +50,7 @@ use sdk_common::sp_client::silentpayments::{ Error as SpError, }; use sdk_common::{signature, MutexExt, MAX_PRD_PAYLOAD_SIZE}; -use serde_json::{Error as SerdeJsonError, Map, Value}; +use serde_json::{json, Error as SerdeJsonError, Map, Value}; use serde::{de, Deserialize, Serialize}; use tsify::{JsValueSerdeExt, Tsify}; @@ -1606,3 +1606,86 @@ pub fn get_storages(process_outpoint: String) -> ApiResult> { Ok(vec![]) } + +#[wasm_bindgen] +pub fn is_child_role(parent_role: String, child_role: String) -> ApiResult<()> { + let parent_role_def: Value = serde_json::from_str(&parent_role)?; + let child_role_def: Value = serde_json::from_str(&child_role)?; + let json = json!({ + "roles": parent_role_def + }); + + let parent_roles = json.extract_roles()?; + let json = json!({ + "roles": child_role_def + }); + let child_roles = json.extract_roles()?; + + for (_, child_role) in &child_roles { + for child_member in &child_role.members { + let mut is_in_parent = false; + for (_, parent_role) in &parent_roles { + if parent_role.members.contains(&child_member) { + is_in_parent = true; + } + if is_in_parent { break } + } + if !is_in_parent { + return Err(ApiError::new("child role contains a member not in parent".to_owned())); + } + } + } + + Ok(()) +} + +#[wasm_bindgen] +pub fn roles_contains_us(role: String) -> ApiResult<()> { + let roles: Value = serde_json::from_str(&role)?; + + let json = json!({ + "roles": roles + }); + + let roles = json.extract_roles()?; + + let device = lock_local_device()?; + + let us = device.to_member(); + + let mut contains_us = false; + for (_, role) in roles { + if role.members.contains(&us) { + contains_us = true; + break; + } + } + + if !contains_us { + Err(ApiError::new("We're not part of that role".to_owned())) + } else { + Ok(()) + } +} + +#[wasm_bindgen] +pub fn roles_contains_member(roles: String, member_str: Vec) -> ApiResult<()> { + let json = json!({ + "roles": serde_json::from_str(&roles)? + }); + + let roles = json.extract_roles()?; + + let addresses: anyhow::Result> = member_str.iter() + .map(|s| SilentPaymentAddress::try_from(s.as_str()).map_err(|_| anyhow::Error::msg("Invalid string"))) + .collect(); + let member = Member::new(addresses?)?; + + for (_, role) in roles { + if !role.members.contains(&member) { + return Err(ApiError::new("member is not part of that role".to_owned())); + } + } + + Ok(()) +}