Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions packages/wasm-sdk/src/dpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,18 +295,34 @@ impl IdentityWasm {
// }

#[wasm_bindgen(js_name=DataContract)]
pub struct DataContractWasm(DataContract);
pub struct DataContractWasm {
contract: DataContract,
platform_version: u32,
}

impl From<DataContract> for DataContractWasm {
fn from(value: DataContract) -> Self {
Self(value)
impl DataContractWasm {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went through DPP methods. Actually, I think your idea to pass the platfrom version to the constructor was the best option we have. But all constructors should always require platform version then. We shouldn't create with default or first version in any case. I'm sorry, that I wasn't clear enough in my first comment, so you reverted half correct code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about fd435e3?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No there actually is a trait FromWithVersion that should be used instead

/// Create a DataContractWasm from a DataContract with platform version.
///
/// This is the primary constructor for wrapping a data contract.
/// The platform version is stored and used for consistent serialization
/// to ensure version-specific fields (e.g., `groups` and `tokens` for
/// token contracts) are correctly included.
///
/// # Arguments
/// * `data_contract` - The data contract to wrap
/// * `platform_version` - The platform version to use for operations
pub(crate) fn from_data_contract(data_contract: DataContract, platform_version: u32) -> Self {
Self {
contract: data_contract,
platform_version,
}
}
}

#[wasm_bindgen(js_class=DataContract)]
impl DataContractWasm {
pub fn id(&self) -> String {
self.0.id().to_string(Encoding::Base58)
self.contract.id().to_string(Encoding::Base58)
}

#[wasm_bindgen(js_name=fromJSON)]
Expand All @@ -331,14 +347,22 @@ impl DataContractWasm {
WasmSdkError::serialization(format!("failed to create DataContract from json: {}", e))
})?;

Ok(data_contract.into())
Ok(DataContractWasm::from_data_contract(
data_contract,
platform_version.protocol_version,
))
}

#[wasm_bindgen(js_name=toJSON)]
pub fn to_json(&self) -> Result<JsValue, WasmSdkError> {
let platform_version = PlatformVersion::first();
let platform_version = &PlatformVersion::get(self.platform_version).map_err(|e| {
WasmSdkError::invalid_argument(format!(
"unknown platform version {}: {e}",
self.platform_version
))
})?;

let json = self.0.to_json(platform_version).map_err(|e| {
let json = self.contract.to_json(platform_version).map_err(|e| {
WasmSdkError::serialization(format!(
"failed to convert data contract convert to json: {}",
e
Expand Down
10 changes: 7 additions & 3 deletions packages/wasm-sdk/src/queries/data_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ impl WasmSdk {
)
.map_err(|e| WasmSdkError::invalid_argument(format!("Invalid data contract ID: {}", e)))?;

DataContract::fetch_by_identifier(self.as_ref(), id)
let contract = DataContract::fetch_by_identifier(self.as_ref(), id)
.await?
.ok_or_else(|| WasmSdkError::not_found("Data contract not found"))
.map(Into::into)
.ok_or_else(|| WasmSdkError::not_found("Data contract not found"))?;

Ok(DataContractWasm::from_data_contract(
contract,
self.version(),
))
}

#[wasm_bindgen(js_name = "getDataContractWithProofInfo")]
Expand Down
4 changes: 3 additions & 1 deletion packages/wasm-sdk/src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ pub async fn verify_data_contract() -> Option<DataContractWasm> {
)
.expect("parse proof");

response.map(DataContractWasm::from)
response.map(|contract| {
DataContractWasm::from_data_contract(contract, PlatformVersion::latest().protocol_version)
})
}

#[wasm_bindgen(js_name = "verifyDocuments")]
Expand Down
Loading