Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
23 changes: 18 additions & 5 deletions packages/wasm-sdk/src/dpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,18 @@ impl IdentityWasm {
#[wasm_bindgen]
pub struct DataContractWasm(DataContract);

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.
///
/// This is the primary constructor for wrapping a data contract.
/// Note that serialization via `to_json()` requires providing an explicit
/// platform version to ensure version-specific fields (e.g., `groups` and
/// `tokens` for token contracts) are correctly included.
///
/// # Arguments
/// * `data_contract` - The data contract to wrap
pub(crate) fn from_data_contract(data_contract: DataContract) -> Self {
Self(data_contract)
}
}

Expand All @@ -310,8 +319,12 @@ impl DataContractWasm {
}

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

let json = self.0.to_json(platform_version).map_err(|e| {
WasmSdkError::serialization(format!(
Expand Down
7 changes: 4 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,11 @@ 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))
}

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

response.map(DataContractWasm::from)
response.map(DataContractWasm::from_data_contract)
}

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