Add evaluate_state api
This commit is contained in:
parent
bb1e5afb2f
commit
4362012fcb
26
src/api.rs
26
src/api.rs
@ -1413,6 +1413,26 @@ pub fn refuse_state(init_commitment: String, merkle_root_hex: String) -> ApiResu
|
||||
add_validation_token(init_commitment, merkle_root_hex, false)
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn evaluate_state(init_commitment: String, previous_state: Option<String>, state: String) -> ApiResult<ApiReturn> {
|
||||
let prev_state: Option<ProcessState> = if let Some(s) = previous_state { Some(serde_json::from_str(&s)?) } else { None };
|
||||
let process_state: ProcessState = serde_json::from_str(&state)?;
|
||||
|
||||
process_state.is_valid(prev_state.as_ref())?;
|
||||
|
||||
let clear_pcd = process_state.decrypt_pcd()?;
|
||||
let roles = clear_pcd.extract_roles()?;
|
||||
|
||||
// We create a commit msg with the valid state
|
||||
let outpoint: OutPoint = OutPoint::from_str(&init_commitment)?;
|
||||
let commit_msg = CommitMessage::new_update_commitment(outpoint, process_state.pcd_commitment, roles);
|
||||
|
||||
Ok(ApiReturn {
|
||||
commit_to_send: Some(commit_msg),
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
|
||||
fn add_validation_token(init_commitment: String, merkle_root_hex: String, approval: bool) -> ApiResult<ApiReturn> {
|
||||
let mut merkle_root = [0u8; 32];
|
||||
let pcd_commitment_vec = Vec::from_hex(&merkle_root_hex)?;
|
||||
@ -1440,11 +1460,17 @@ fn add_validation_token(init_commitment: String, merkle_root_hex: String, approv
|
||||
let sp_wallet = local_device.get_wallet();
|
||||
let proof = Proof::new(message_hash, sp_wallet.get_client().get_spend_key().try_into()?);
|
||||
|
||||
// we copy the state before modifying it
|
||||
let previous_state = update_state.clone();
|
||||
|
||||
update_state.validation_tokens.push(proof);
|
||||
|
||||
let new_state = update_state.clone();
|
||||
|
||||
let updated_process = UpdatedProcess {
|
||||
commitment_tx: OutPoint::from_str(&init_commitment)?,
|
||||
current_process: process.clone(),
|
||||
modified_state: Some((previous_state, new_state)),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user