Fix hash_value with new serialization method

This commit is contained in:
Sosthene 2025-06-30 15:59:37 +02:00
parent 6d308e90d2
commit 4f278278b7

View File

@ -1726,18 +1726,16 @@ pub fn decode_value(value: Vec<u8>) -> ApiResult<JsValue> {
Err(ApiError::new("Invalid or unsupported PCD data".to_owned()))
}
// #[wasm_bindgen]
// pub fn hash_value(value: JsValue, commited_in: String, label: String) -> ApiResult<String> {
// let outpoint = OutPoint::from_str(&commited_in)?;
// let encoded_value = sdk_common::serialization::ciborium_serialize(&serde_wasm_bindgen::from_value::<FileBlob>(value)?)?;
// let hash = AnkPcdHash::from_pcd_value(encoded_value.as_slice(), label.as_bytes(), &outpoint);
// Ok(hash.as_byte_array().to_lower_hex_string())
// }
#[wasm_bindgen]
pub fn hash_value(value: JsValue, commited_in: String, label: String) -> ApiResult<String> {
let outpoint = OutPoint::from_str(&commited_in)?;
let encoded_value = sdk_common::serialization::ciborium_serialize(&serde_wasm_bindgen::from_value::<FileBlob>(value)?)?;
let encoded_value = if let Ok(file_blob) = serde_wasm_bindgen::from_value::<FileBlob>(value.clone()) {
file_blob.serialize_to_pcd()?
} else if let Ok(json) = serde_wasm_bindgen::from_value::<Value>(value) {
json.serialize_to_pcd()?
} else {
return Err(ApiError::new("Invalid or unsupported PCD data".to_owned()));
};
let hash = AnkPcdHash::from_pcd_value(encoded_value.as_slice(), label.as_bytes(), &outpoint);
Ok(hash.as_byte_array().to_lower_hex_string())
}