Add children process validation method

This commit is contained in:
NicolasCantu 2025-01-15 13:05:01 +01:00
parent 3dab6ef2d6
commit 7d0aa89387

View File

@ -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<Vec<String>> {
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<String>) -> ApiResult<()> {
let json = json!({
"roles": serde_json::from_str(&roles)?
});
let roles = json.extract_roles()?;
let addresses: anyhow::Result<Vec<SilentPaymentAddress>> = 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(())
}