diff --git a/src/api.rs b/src/api.rs index 513f360..22d958f 100644 --- a/src/api.rs +++ b/src/api.rs @@ -63,7 +63,7 @@ use sdk_common::network::{ NewTxMessage, }; use sdk_common::pcd::{ - compare_maps, AnkPcdHash, AnkPcdTag, Member, Pcd, RoleDefinition, ValidationRule, + AnkPcdHash, AnkPcdTag, Member, Pcd, RoleDefinition, ValidationRule, }; use sdk_common::prd::{AnkPrdHash, Prd, PrdType}; use sdk_common::silentpayments::{create_transaction, map_outputs_to_sp_address}; @@ -84,7 +84,6 @@ pub struct UpdatedProcess { pub current_process: Process, pub new_state: Option, pub modified_state: Option<(ProcessState, ProcessState)>, // first the previous state, then the current one - pub user_validation_required: bool, } #[derive(Debug, PartialEq, Tsify, Serialize, Deserialize, Default)] @@ -941,7 +940,7 @@ fn handle_prd( fn handle_pcd(pcd: Value) -> AnyhowResult { // We received an encrypted pcd, so we can compute the merkle root of a tree where all the encrypted values are the leaves // We pass an empty Outpoint as salt, as there's no point salting hash of encrypted values - let encrypted_pcd_commitments = pcd.hash_fields(OutPoint::null())?; + let encrypted_pcd_commitments = pcd.hash_all_fields(OutPoint::null())?; let encrypted_pcd_root = ::create_merkle_tree(&Value::Object(encrypted_pcd_commitments))?.root().unwrap().to_lower_hex_string(); let mut processes = lock_processes()?; let updated_prd: Prd; @@ -1142,7 +1141,7 @@ pub fn create_new_process( relay_address: String, fee_rate: u32, ) -> ApiResult { - let pcd = ::from_string(&init_state)?; + let pcd = ::new_from_string(&init_state)?; // check that we have a proper roles map let roles = pcd.extract_roles()?; @@ -1178,7 +1177,7 @@ pub fn create_new_process( let outpoint = OutPoint::new(transaction.txid(), 0); // We now need a hash that commits to the clear value of each field + the process id (or outpoint) - let fields_commitment = pcd.hash_fields(outpoint)?; + let fields_commitment = pcd.hash_all_fields(outpoint)?; let mut process = Process::new(outpoint); @@ -1238,7 +1237,7 @@ pub fn update_process( let new_state_val = Value::from_str(&new_state)?; // We hash all the new values - let pcd_commitment = Value::Object(new_state_val.hash_fields(outpoint)?); + let pcd_commitment = Value::Object(new_state_val.hash_all_fields(outpoint)?); let new_state_merkle_root = ::create_merkle_tree(&pcd_commitment)?.root().unwrap(); // We compare the new state with the previous one @@ -1329,7 +1328,6 @@ pub fn create_update_message( .flat_map(|rule| rule.fields.clone()) .collect(); for member in role.members { - debug!("member: {:?}", member); // Check that we have a shared_secret with all members if let Some(no_secret_address) = member.get_addresses().iter() .find(|a| shared_secrets.get_secret_for_address(a.as_str().try_into().unwrap()).is_none()) @@ -1352,7 +1350,7 @@ pub fn create_update_message( // To allow the recipient to identify the pcd that contains only encrypted values, we compute the merkle tree of the encrypted pcd // we then put the root in the payload of the prd update - let encrypted_pcd_hash = update_state.encrypted_pcd.hash_fields(OutPoint::null())?; + let encrypted_pcd_hash = update_state.encrypted_pcd.hash_all_fields(OutPoint::null())?; let encrypted_pcd_merkle_root = ::create_merkle_tree(&Value::Object(encrypted_pcd_hash))?.root().unwrap(); let full_prd = Prd::new_update( @@ -1605,6 +1603,7 @@ struct UserDiff { new_value: Value, notify_user: bool, need_validation: bool, + // validated: bool, proof: Option, // This is only validation (or refusal) for that specific diff, not the whole state. It can't be commited as such } @@ -1615,6 +1614,7 @@ pub struct PcdUpdates { pub previous_pcd: Option, // We don't have a previous state for creation pub decrypted_pcds: HashMap, // Key is the merkle root of the whole state pub modified_values: Vec, + // pub proofs: HashMap>, // key is the merkle root of the whole state, } /// Get active update proposals for a given process outpoint @@ -1635,7 +1635,7 @@ pub fn get_update_proposals(process_outpoint: String) -> ApiResult { let previous_pcd = match relevant_process.get_latest_commited_state() { Some(state) => { let mut decrypted_pcd = Map::new(); - state.encrypted_pcd.decrypt_fields(&state.pcd_commitment.to_value_object()?, &state.keys, &mut decrypted_pcd); + state.encrypted_pcd.decrypt_all(state.commited_in, &state.pcd_commitment.to_value_object()?, &state.keys, &mut decrypted_pcd); Some(Value::Object(decrypted_pcd)) } None => None @@ -1655,7 +1655,7 @@ pub fn get_update_proposals(process_outpoint: String) -> ApiResult { let fields_to_validate = state.get_fields_to_validate_for_member(&member)?; let mut decrypted_pcd = Map::new(); - state.encrypted_pcd.decrypt_fields(&state.pcd_commitment.to_value_object()?, &state.keys, &mut decrypted_pcd)?; + state.encrypted_pcd.decrypt_all(state.commited_in, &state.pcd_commitment.to_value_object()?, &state.keys, &mut decrypted_pcd)?; let root = state.pcd_commitment.create_merkle_tree()?.root_hex().unwrap(); if let Some(ref previous_state) = previous_pcd { @@ -1663,7 +1663,7 @@ pub fn get_update_proposals(process_outpoint: String) -> ApiResult { let previous_value = previous_state.get(key).or_else(|| Some(&Value::Null)).unwrap(); if previous_value == value { continue; } let need_validation = if is_pairing && key.as_str() == "roles" { true } else { fields_to_validate.iter().any(|f| *key == **f) }; - let notify_user = if need_validation { true } else if !value.is_hex_string() { true } else { false }; + let notify_user = if need_validation { true } else if value.is_hex_string(Some(32)).is_err() { true } else { false }; let diff = UserDiff { new_state_merkle_root: root.clone(), field: key.to_owned(), @@ -1678,7 +1678,7 @@ pub fn get_update_proposals(process_outpoint: String) -> ApiResult { } else { for (key, value) in &decrypted_pcd { let need_validation = if is_pairing && key.as_str() == "roles" { true } else { fields_to_validate.iter().any(|f| *key == **f) }; - let notify_user = if need_validation { true } else if !value.is_hex_string() { true } else { false }; + let notify_user = if need_validation { true } else if value.is_hex_string(Some(32)).is_err() { true } else { false }; let diff = UserDiff { new_state_merkle_root: root.clone(), field: key.to_owned(),