Add public_data
This commit is contained in:
parent
f0c52f6597
commit
d9d2bcd9d0
49
src/api.rs
49
src/api.rs
@ -766,12 +766,12 @@ fn create_diffs(process: &Process, new_state: &ProcessState) -> AnyhowResult<Vec
|
||||
|
||||
let new_state_root = &new_state.state_id;
|
||||
|
||||
let new_state_descriptions = &new_state.descriptions;
|
||||
let new_public_data = &new_state.public_data;
|
||||
|
||||
let process_id = process.get_process_id()?.to_string();
|
||||
let mut diffs = vec![];
|
||||
for (field, hash) in new_state_commitments {
|
||||
let description = new_state_descriptions.get(field).map(|d| d.to_string());
|
||||
let description = new_public_data.get(field).map(|d| d.to_string());
|
||||
let need_validation = fields_to_validate.contains(field);
|
||||
diffs.push(UserDiff {
|
||||
process_id: process_id.clone(),
|
||||
@ -954,7 +954,8 @@ fn handle_prd(
|
||||
let mut commit_msg = CommitMessage::new_update_commitment(
|
||||
OutPoint::from_str(&prd.process_id)?,
|
||||
updated_state.pcd_commitment,
|
||||
updated_state.roles.clone().into_iter().collect()
|
||||
updated_state.roles.clone().into_iter().collect(),
|
||||
updated_state.public_data.clone()
|
||||
);
|
||||
|
||||
commit_msg.set_validation_tokens(updated_state.validation_tokens);
|
||||
@ -1222,14 +1223,14 @@ pub fn create_connect_transaction(addresses: Vec<String>, fee_rate: u32) -> ApiR
|
||||
#[wasm_bindgen]
|
||||
pub fn create_new_process(
|
||||
init_state_str: String,
|
||||
roles: String,
|
||||
descriptions_str: Option<String>,
|
||||
roles: JsValue,
|
||||
public_data: JsValue,
|
||||
relay_address: String,
|
||||
fee_rate: u32,
|
||||
) -> ApiResult<ApiReturn> {
|
||||
let init_state = <Value as Pcd>::new_from_string(&init_state_str)?;
|
||||
let roles: BTreeMap<String, RoleDefinition> = serde_json::from_str(&roles)?;
|
||||
let descriptions = if let Some(d) = descriptions_str { <Value as Pcd>::new_from_string(&d)? } else { Value::Object(Map::new()) };
|
||||
let init_state: Value = <Value as Pcd>::new_from_string(&init_state_str)?;
|
||||
let roles: BTreeMap<String, RoleDefinition> = serde_wasm_bindgen::from_value(roles)?;
|
||||
let public_data: BTreeMap<String, String> = 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<ApiReturn> {
|
||||
let mut process: Process = serde_wasm_bindgen::from_value(process)?;
|
||||
let new_attributes: Value = serde_wasm_bindgen::from_value(new_attributes)?;
|
||||
let roles: BTreeMap<String, RoleDefinition> = serde_wasm_bindgen::from_value(roles)?;
|
||||
debug!("{:#?}", process);
|
||||
let new_public_data: BTreeMap<String, String> = 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<String> = 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<String>, 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<String>) -> ApiResult<()> {
|
||||
let roles: BTreeMap<String, RoleDefinition> = serde_json::from_str(&roles)?;
|
||||
@ -1862,8 +1865,6 @@ pub fn decrypt_data(key: &[u8], data: &[u8]) -> ApiResult<String> {
|
||||
|
||||
key_buf.copy_from_slice(key);
|
||||
|
||||
debug!("{}", key_buf.as_hex());
|
||||
|
||||
// decrypt the data
|
||||
let clear = decrypt_with_key(&key_buf, data)?;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user