From d9d2bcd9d0db6db2b71aa0a36c539dbd61c083fa Mon Sep 17 00:00:00 2001 From: NicolasCantu Date: Mon, 3 Mar 2025 23:20:46 +0100 Subject: [PATCH] Add public_data --- src/api.rs | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/src/api.rs b/src/api.rs index 53efb6d..9bc2708 100644 --- a/src/api.rs +++ b/src/api.rs @@ -766,12 +766,12 @@ fn create_diffs(process: &Process, new_state: &ProcessState) -> AnyhowResult, fee_rate: u32) -> ApiR #[wasm_bindgen] pub fn create_new_process( init_state_str: String, - roles: String, - descriptions_str: Option, + roles: JsValue, + public_data: JsValue, relay_address: String, fee_rate: u32, ) -> ApiResult { - let init_state = ::new_from_string(&init_state_str)?; - let roles: BTreeMap = serde_json::from_str(&roles)?; - let descriptions = if let Some(d) = descriptions_str { ::new_from_string(&d)? } else { Value::Object(Map::new()) }; + let init_state: Value = ::new_from_string(&init_state_str)?; + let roles: BTreeMap = serde_wasm_bindgen::from_value(roles)?; + let public_data: BTreeMap = serde_wasm_bindgen::from_value(public_data)?; // We create a transaction that spends to the relay address let psbt = create_transaction_for_addresses(vec![relay_address.clone()], fee_rate)?; @@ -1251,7 +1252,7 @@ pub fn create_new_process( let new_tx_msg = NewTxMessage::new(serialize(&transaction).to_lower_hex_string(), None); - let mut new_state = ProcessState::new(process_id, init_state.to_value_object()?, descriptions.to_value_object()?, roles.clone())?; + let mut new_state = ProcessState::new(process_id, init_state.as_object().unwrap().clone(), &public_data, roles.clone())?; let pcd_commitment = new_state.pcd_commitment.clone(); @@ -1288,7 +1289,8 @@ pub fn create_new_process( let commit_msg = CommitMessage::new_update_commitment( process_id, pcd_commitment, - roles.into_iter().collect() + roles.into_iter().collect(), + public_data, ); let updated_process = UpdatedProcess { @@ -1313,12 +1315,14 @@ pub fn create_new_process( pub fn update_process( process: JsValue, new_attributes: JsValue, - roles: JsValue + roles: JsValue, + new_public_data: JsValue, ) -> ApiResult { let mut process: Process = serde_wasm_bindgen::from_value(process)?; let new_attributes: Value = serde_wasm_bindgen::from_value(new_attributes)?; let roles: BTreeMap = serde_wasm_bindgen::from_value(roles)?; - debug!("{:#?}", process); + let new_public_data: BTreeMap = serde_wasm_bindgen::from_value(new_public_data)?; + // debug!("{:#?}", process); // debug!("{:#?}", new_attributes); // debug!("{:#?}", roles); @@ -1327,7 +1331,7 @@ pub fn update_process( let prev_state = process.get_latest_commited_state() .ok_or(ApiError::new("Process must have at least one state already commited".to_owned()))?; - let last_state_descriptions = &prev_state.descriptions; + let public_data = if new_public_data.len() > 0 { new_public_data } else { prev_state.public_data.clone() }; // We expect the whole set of attributes for now, even if value does'nt change since previous state // We rehash everything with the new txid, so we need the clear value @@ -1335,12 +1339,10 @@ pub fn update_process( let mut new_state = ProcessState::new( process.get_process_tip()?, new_attributes.to_value_object()?, - last_state_descriptions.clone(), + &public_data, roles.clone() )?; - debug!("1"); - // We compare the new state with the previous one let last_state_merkle_root = &prev_state.state_id; @@ -1355,14 +1357,11 @@ pub fn update_process( } let diffs = create_diffs(&process, &new_state)?; - debug!("1"); let all_fields: Vec = new_attributes.as_object().unwrap().into_iter().map(|(field, _)| field.clone()).collect(); - debug!("1"); let mut fields2keys = Map::new(); let mut fields2cipher = Map::new(); new_attributes.encrypt_fields(&all_fields, &mut fields2keys, &mut fields2cipher); - debug!("1"); new_state.keys = fields2keys; @@ -1374,7 +1373,6 @@ pub fn update_process( }) .collect(); - debug!("1"); // Add the new state to the process process.insert_concurrent_state(new_state.clone())?; @@ -1396,7 +1394,9 @@ pub fn update_process( let commit_msg = CommitMessage::new_update_commitment( process_id, new_state.pcd_commitment, - roles.into_iter().collect()); + roles.into_iter().collect(), + new_state.public_data, + ); Ok(ApiReturn { updated_process: Some(updated_process), @@ -1570,7 +1570,8 @@ pub fn evaluate_state(process_id: String, previous_state: Option, state: let commit_msg = CommitMessage::new_update_commitment( outpoint, process_state.pcd_commitment, - process_state.roles.clone().into_iter().collect() + process_state.roles.clone().into_iter().collect(), + process_state.public_data, ); Ok(ApiReturn { @@ -1621,6 +1622,7 @@ fn add_validation_token(process_id: String, state_id: String, approval: bool) -> process.get_process_id()?, pcd_commitment, update_state.roles.clone().into_iter().collect(), + update_state.public_data.clone(), ); commit_msg.set_validation_tokens(update_state.validation_tokens.clone()); Some(commit_msg) @@ -1806,6 +1808,7 @@ pub fn roles_contains_us(role: String) -> ApiResult<()> { } } +// TODO this is wrong, and we want to move that in ts anyway #[wasm_bindgen] pub fn roles_contains_member(roles: String, member_str: Vec) -> ApiResult<()> { let roles: BTreeMap = serde_json::from_str(&roles)?; @@ -1862,8 +1865,6 @@ pub fn decrypt_data(key: &[u8], data: &[u8]) -> ApiResult { key_buf.copy_from_slice(key); - debug!("{}", key_buf.as_hex()); - // decrypt the data let clear = decrypt_with_key(&key_buf, data)?;