Update decode_value to zstd

This commit is contained in:
Sosthene 2025-06-24 17:30:51 +02:00
parent 3346c7f0af
commit 6d308e90d2

View File

@ -65,7 +65,7 @@ use sdk_common::network::{
NewTxMessage,
};
use sdk_common::pcd::{
FileBlob, Member, Pcd, PcdCommitments, RoleDefinition, Roles, ValidationRule
DataType, FileBlob, Member, Pcd, PcdCommitments, RoleDefinition, Roles, ValidationRule, PCD_VERSION, PcdSerializable
};
use sdk_common::prd::{AnkPrdHash, Prd, PrdType};
use sdk_common::silentpayments::{create_transaction as internal_create_transaction, sign_transaction as internal_sign_tx, TsUnsignedTransaction};
@ -73,7 +73,7 @@ use sdk_common::sp_client::{FeeRate, OutputSpendStatus, OwnedOutput, Recipient,
use sdk_common::secrets::SecretsStore;
use crate::user::{lock_local_device, set_new_device, LOCAL_DEVICE};
use crate::wallet::{generate_sp_wallet, lock_freezed_utxos};
use crate::wallet::{generate_sp_wallet, lock_freezed_utxos, scan_blocks};
const EMPTYSTATEID: &str = "0000000000000000000000000000000000000000000000000000000000000000";
@ -1711,18 +1711,29 @@ pub fn encode_json(json_data: JsValue) -> ApiResult<Pcd> {
#[wasm_bindgen]
pub fn decode_value(value: Vec<u8>) -> ApiResult<JsValue> {
if let Ok(deserialized) = sdk_common::serialization::ciborium_deserialize::<FileBlob>(&value) {
let u8IntArray = Uint8Array::from(deserialized.data.as_slice());
// Try FileBlob first
if let Ok(file_blob) = sdk_common::pcd::FileBlob::deserialize_from_pcd(&value) {
let u8IntArray = Uint8Array::from(file_blob.data.as_slice());
let res = Object::new();
Reflect::set(&res, &JsValue::from_str("type"), &JsValue::from_str(&deserialized.r#type));
Reflect::set(&res, &JsValue::from_str("data"), &JsValue::from(u8IntArray));
Ok(JsValue::from(res))
} else {
let res: Value = sdk_common::serialization::ciborium_deserialize(&value)?;
return Ok(serde_wasm_bindgen::to_value(&res)?);
Reflect::set(&res, &JsValue::from_str("type"), &JsValue::from_str(&file_blob.r#type)).unwrap();
Reflect::set(&res, &JsValue::from_str("data"), &JsValue::from(u8IntArray)).unwrap();
return Ok(JsValue::from(res));
}
// Try JSON next
if let Ok(json) = serde_json::Value::deserialize_from_pcd(&value) {
return Ok(serde_wasm_bindgen::to_value(&json)?);
}
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)?;