Rm dead code and minor fixes

This commit is contained in:
Sosthene 2024-12-11 23:39:35 +01:00
parent 1106d25ea1
commit a919c6d83d

View File

@ -670,86 +670,6 @@ pub fn parse_new_tx(new_tx_msg: String, block_height: u32) -> ApiResult<ApiRetur
)?)
}
// #[wasm_bindgen]
// pub fn response_prd(
// root_commitment: String,
// prd_commitment: String, // The commitment to the Prd we respond to
// approval: bool,
// ) -> ApiResult<ApiReturn> {
// let prd_hash = AnkPrdHash::from_str(&prd_commitment)?;
// let outpoint = OutPoint::from_str(&root_commitment)?;
// let local_device = lock_local_device()?;
// let member = local_device
// .to_member();
// // find the prd in the registered process
// let mut processes = lock_processes()?;
// let process = processes
// .get_mut(&outpoint)
// .ok_or(ApiError::new("Unknown process".to_owned()))?;
// let prd_ref = process
// .get_impending_requests_mut()
// .into_iter()
// .find(|r| r.create_commitment() == prd_hash)
// .ok_or(ApiError::new(
// "Failed to find the prd in registered processes".to_owned(),
// ))?;
// match prd_ref.prd_type {
// PrdType::Update => {
// let pcd = Value::from_str(&prd_ref.payload)?;
// let pcd_hash: AnkPcdHash = AnkPcdHash::from_value(&pcd);
// let prd_response = Prd::new_response(
// OutPoint::from_str(&root_commitment)?,
// serde_json::to_string(&member)?,
// prd_ref.validation_tokens.clone(),
// pcd_hash,
// );
// let prd_msg = prd_response.to_network_msg(local_device.get_wallet())?;
// let roles = &pcd
// .get("roles")
// .ok_or(ApiError::new("No roles in pcd we respond to".to_owned()))?;
// let roles_map = roles
// .as_object()
// .ok_or(ApiError::new("roles is not an object".to_owned()))?
// .clone();
// let shared_secrets = lock_shared_secrets()?;
// let mut ciphers = vec![];
// for (_, role_def) in roles_map {
// let role: RoleDefinition = serde_json::from_str(&role_def.to_string())?;
// for member in role.members {
// for sp_address in member.get_addresses() {
// if sp_address.to_string()
// == local_device
// .get_wallet()
// .get_client()
// .get_receiving_address()
// {
// continue;
// }
// if let Some(shared_secret) = shared_secrets.get_secret_for_address(sp_address.try_into()?) {
// let cipher = encrypt_with_key(shared_secret.as_byte_array(), prd_msg.as_bytes())?;
// ciphers.push(cipher.to_lower_hex_string());
// } else {
// continue;
// }
// }
// }
// }
// return Ok(ApiReturn {
// ciphers_to_send: ciphers,
// ..Default::default()
// });
// }
// _ => unimplemented!(),
// };
// }
fn confirm_prd(prd: &Prd, shared_secret: &AnkSharedSecretHash) -> AnyhowResult<String> {
match prd.prd_type {
PrdType::Confirm | PrdType::Response | PrdType::List => {
@ -1234,7 +1154,7 @@ pub fn create_new_process(
// check that we have a proper roles map
let roles = pcd.extract_roles()?;
// Step 1: we create the encryption keys for each field and encrypt them
// We create the encryption keys for each field and encrypt them
let mut fields2keys = Map::new();
let mut fields2cipher = Map::new();
let fields_to_encrypt: Vec<String> = pcd
@ -1486,7 +1406,7 @@ pub fn evaluate_state(init_commitment: String, previous_state: Option<String>, s
process_state.is_valid(prev_state.as_ref())?;
let clear_pcd = process_state.decrypt_pcd()?;
let roles = clear_pcd.extract_roles()?;
let roles = Value::Object(clear_pcd).extract_roles()?;
// We create a commit msg with the valid state
let outpoint: OutPoint = OutPoint::from_str(&init_commitment)?;
@ -1647,111 +1567,9 @@ pub fn create_faucet_msg() -> ApiResult<String> {
Ok(faucet_msg.to_string())
}
#[derive(Debug, PartialEq, Tsify, Serialize, Deserialize, Default)]
#[tsify(into_wasm_abi)]
#[allow(non_camel_case_types)]
struct UserDiff {
new_state_merkle_root: String, // TODO add a merkle proof that the new_value belongs to that state
field: String,
previous_value: Value,
new_value: Value,
notify_user: bool,
need_validation: bool,
// validated: bool,
proof: Option<Proof>, // This is only validation (or refusal) for that specific diff, not the whole state. It can't be commited as such
}
#[derive(Debug, PartialEq, Tsify, Serialize, Deserialize, Default)]
#[tsify(into_wasm_abi)]
#[allow(non_camel_case_types)]
pub struct PcdUpdates {
pub previous_pcd: Option<Value>, // We don't have a previous state for creation
pub decrypted_pcds: HashMap<String, Value>, // Key is the merkle root of the whole state
pub modified_values: Vec<UserDiff>,
// pub proofs: HashMap<String, Option<Proof>>, // key is the merkle root of the whole state,
}
/// Get active update proposals for a given process outpoint
/// Returns a vector with the latest commited state first, if any, and all active proposals
#[wasm_bindgen]
pub fn get_update_proposals(process_outpoint: String) -> ApiResult<PcdUpdates> {
pub fn get_storages(process_outpoint: String) -> ApiResult<Vec<String>> {
let outpoint = OutPoint::from_str(&process_outpoint)?;
let mut processes = lock_processes()?;
let relevant_process = processes
.get(&outpoint)
.ok_or(ApiError::new("process not found".to_owned()))?;
let mut decrypted_pcds = HashMap::new();
let mut modified_values = Vec::new();
let previous_pcd = match relevant_process.get_latest_commited_state() {
Some(state) => {
let mut decrypted_pcd = Map::new();
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
};
let device = lock_local_device()?;
let member = device.to_member();
let is_pairing = device.get_pairing_commitment().is_none();
for state in relevant_process.get_latest_concurrent_states()? {
if state.encrypted_pcd == Value::Null {
// This is the last empty state, ignore it
continue;
}
let fields_to_validate = state.get_fields_to_validate_for_member(&member)?;
let mut decrypted_pcd = Map::new();
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 {
for (key, value) in &decrypted_pcd {
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(Some(32)).is_err() { true } else { false };
let diff = UserDiff {
new_state_merkle_root: root.clone(),
field: key.to_owned(),
previous_value: previous_value.clone(),
new_value: value.clone(),
need_validation,
notify_user,
proof: None
};
modified_values.push(diff);
}
} 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(Some(32)).is_err() { true } else { false };
let diff = UserDiff {
new_state_merkle_root: root.clone(),
field: key.to_owned(),
previous_value: Value::Null,
new_value: value.clone(),
need_validation,
notify_user,
proof: None
};
modified_values.push(diff);
}
}
decrypted_pcds.insert(root, Value::Object(decrypted_pcd));
}
Ok(PcdUpdates {
previous_pcd,
decrypted_pcds,
modified_values,
})
Ok(vec![])
}