Skip to content
Merged
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
6 changes: 1 addition & 5 deletions packages/wasm-sdk/src/context_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ use std::sync::Arc;

use dash_sdk::platform::ContextProvider;
use dash_sdk::{
dpp::{
data_contract::TokenConfiguration,
prelude::CoreBlockHeight,
version::PlatformVersion,
},
dpp::{data_contract::TokenConfiguration, prelude::CoreBlockHeight, version::PlatformVersion},
error::ContextProviderError,
platform::{DataContract, Identifier},
};
Expand Down
56 changes: 29 additions & 27 deletions packages/wasm-sdk/src/dpns.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::{JsError, JsValue};
use crate::sdk::WasmSdk;
use serde::{Serialize, Deserialize};
use dash_sdk::platform::dpns_usernames::{convert_to_homograph_safe_chars, is_contested_username, is_valid_username, RegisterDpnsNameInput};
use dash_sdk::platform::{Fetch, Identity};
use dash_sdk::dpp::document::{Document, DocumentV0Getters};
use dash_sdk::dpp::identity::accessors::IdentityGettersV0;
use dash_sdk::dpp::prelude::Identifier;
use dash_sdk::platform::dpns_usernames::{
convert_to_homograph_safe_chars, is_contested_username, is_valid_username,
RegisterDpnsNameInput,
};
use dash_sdk::platform::{Fetch, Identity};
use serde::{Deserialize, Serialize};
use simple_signer::SingleKeySigner;
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::{JsError, JsValue};

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -49,7 +52,8 @@ pub async fn dpns_register_name(
let identity_id_parsed = Identifier::from_string(
identity_id,
dash_sdk::dpp::platform_value::string_encoding::Encoding::Base58,
).map_err(|e| JsError::new(&format!("Invalid identity ID: {}", e)))?;
)
.map_err(|e| JsError::new(&format!("Invalid identity ID: {}", e)))?;

// Fetch the identity
let identity = Identity::fetch(sdk.as_ref(), identity_id_parsed)
Expand Down Expand Up @@ -114,7 +118,8 @@ pub async fn dpns_register_name(
};

// Register the name
let result = sdk.as_ref()
let result = sdk
.as_ref()
.register_dpns_name(input)
.await
.map_err(|e| JsError::new(&format!("Failed to register DPNS name: {}", e)))?;
Expand All @@ -126,27 +131,27 @@ pub async fn dpns_register_name(

// Convert result to JS-friendly format
let js_result = RegisterDpnsNameResult {
preorder_document_id: result.preorder_document.id().to_string(
dash_sdk::dpp::platform_value::string_encoding::Encoding::Base58
),
domain_document_id: result.domain_document.id().to_string(
dash_sdk::dpp::platform_value::string_encoding::Encoding::Base58
),
preorder_document_id: result
.preorder_document
.id()
.to_string(dash_sdk::dpp::platform_value::string_encoding::Encoding::Base58),
domain_document_id: result
.domain_document
.id()
.to_string(dash_sdk::dpp::platform_value::string_encoding::Encoding::Base58),
full_domain_name: result.full_domain_name,
};

// Serialize to JsValue
let serializer = serde_wasm_bindgen::Serializer::json_compatible();
js_result.serialize(&serializer)
js_result
.serialize(&serializer)
.map_err(|e| JsError::new(&format!("Failed to serialize result: {}", e)))
}

/// Check if a DPNS name is available
#[wasm_bindgen]
pub async fn dpns_is_name_available(
sdk: &WasmSdk,
label: &str,
) -> Result<bool, JsError> {
pub async fn dpns_is_name_available(sdk: &WasmSdk, label: &str) -> Result<bool, JsError> {
sdk.as_ref()
.is_dpns_name_available(label)
.await
Expand All @@ -155,22 +160,19 @@ pub async fn dpns_is_name_available(

/// Resolve a DPNS name to an identity ID
#[wasm_bindgen]
pub async fn dpns_resolve_name(
sdk: &WasmSdk,
name: &str,
) -> Result<JsValue, JsError> {
let result = sdk.as_ref()
pub async fn dpns_resolve_name(sdk: &WasmSdk, name: &str) -> Result<JsValue, JsError> {
let result = sdk
.as_ref()
.resolve_dpns_name(name)
.await
.map_err(|e| JsError::new(&format!("Failed to resolve DPNS name: {}", e)))?;

match result {
Some(identity_id) => {
let id_string = identity_id.to_string(
dash_sdk::dpp::platform_value::string_encoding::Encoding::Base58
);
let id_string = identity_id
.to_string(dash_sdk::dpp::platform_value::string_encoding::Encoding::Base58);
Ok(JsValue::from_str(&id_string))
},
}
None => Ok(JsValue::NULL),
}
}
12 changes: 6 additions & 6 deletions packages/wasm-sdk/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};

pub mod context_provider;
pub mod dpp;
pub mod dpns;
pub mod dpp;
pub mod error;
pub mod queries;
pub mod sdk;
pub mod state_transitions;
pub mod verify;
pub mod queries;
pub mod wallet;

// Re-export commonly used items
pub use sdk::{WasmSdk, WasmSdkBuilder};
pub use dpns::*;
pub use queries::{
data_contract::*, document::*, dpns::*, epoch::*, group::*, protocol::*, system::*, token::*, voting::*,
identity as query_identity
data_contract::*, document::*, dpns::*, epoch::*, group::*, identity as query_identity,
protocol::*, system::*, token::*, voting::*,
};
pub use sdk::{WasmSdk, WasmSdkBuilder};
pub use state_transitions::identity as state_transition_identity;
pub use dpns::*;
pub use wallet::*;

#[global_allocator]
Expand Down
67 changes: 45 additions & 22 deletions packages/wasm-sdk/src/queries/data_contract.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::dpp::DataContractWasm;
use crate::sdk::WasmSdk;
use crate::queries::ProofMetadataResponse;
use dash_sdk::platform::{DataContract, Fetch, FetchMany, Identifier};
use crate::sdk::WasmSdk;
use dash_sdk::dpp::data_contract::conversion::json::DataContractJsonConversionMethodsV0;
use dash_sdk::platform::query::LimitQuery;
use dash_sdk::platform::{DataContract, Fetch, FetchMany, Identifier};
use drive_proof_verifier::types::{DataContractHistory, DataContracts};
use dash_sdk::dpp::data_contract::conversion::json::DataContractJsonConversionMethodsV0;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::{JsError, JsValue};
use std::collections::BTreeMap;

#[wasm_bindgen]
pub async fn data_contract_fetch(
Expand Down Expand Up @@ -36,20 +36,23 @@ pub async fn data_contract_fetch_with_proof_info(
dash_sdk::dpp::platform_value::string_encoding::Encoding::Base58,
)?;

let (contract, metadata, proof) = DataContract::fetch_with_metadata_and_proof(sdk, id, None)
.await?;
let (contract, metadata, proof) =
DataContract::fetch_with_metadata_and_proof(sdk, id, None).await?;

match contract {
Some(contract) => {
let response = ProofMetadataResponse {
data: contract.to_json(&dash_sdk::dpp::version::PlatformVersion::get(sdk.version()).unwrap())?,
data: contract.to_json(
&dash_sdk::dpp::version::PlatformVersion::get(sdk.version()).unwrap(),
)?,
metadata: metadata.into(),
proof: proof.into(),
};

// Use json_compatible serializer
let serializer = serde_wasm_bindgen::Serializer::json_compatible();
response.serialize(&serializer)
response
.serialize(&serializer)
.map_err(|e| JsError::new(&format!("Failed to serialize response: {}", e)))
}
None => Err(JsError::new("Data contract not found")),
Expand Down Expand Up @@ -102,7 +105,8 @@ pub async fn get_data_contract_history(

// Use json_compatible serializer
let serializer = serde_wasm_bindgen::Serializer::json_compatible();
response.serialize(&serializer)
response
.serialize(&serializer)
.map_err(|e| JsError::new(&format!("Failed to serialize response: {}", e)))
}

Expand All @@ -117,10 +121,12 @@ pub async fn get_data_contracts(sdk: &WasmSdk, ids: Vec<String>) -> Result<JsVal
// Parse all contract IDs
let identifiers: Result<Vec<Identifier>, _> = ids
.iter()
.map(|id| Identifier::from_string(
.map(|id| {
Identifier::from_string(
id,
dash_sdk::dpp::platform_value::string_encoding::Encoding::Base58,
))
)
})
.collect();
let identifiers = identifiers?;

Expand All @@ -145,7 +151,8 @@ pub async fn get_data_contracts(sdk: &WasmSdk, ids: Vec<String>) -> Result<JsVal

// Use json_compatible serializer
let serializer = serde_wasm_bindgen::Serializer::json_compatible();
response.serialize(&serializer)
response
.serialize(&serializer)
.map_err(|e| JsError::new(&format!("Failed to serialize response: {}", e)))
}

Expand Down Expand Up @@ -173,9 +180,15 @@ pub async fn get_data_contract_history_with_proof_info(
};

// Fetch contract history with proof
let (history_result, metadata, proof) = DataContractHistory::fetch_with_metadata_and_proof(sdk.as_ref(), query, None)
let (history_result, metadata, proof) =
DataContractHistory::fetch_with_metadata_and_proof(sdk.as_ref(), query, None)
.await
.map_err(|e| JsError::new(&format!("Failed to fetch data contract history with proof: {}", e)))?;
.map_err(|e| {
JsError::new(&format!(
"Failed to fetch data contract history with proof: {}",
e
))
})?;

// Convert to response format
let mut versions = BTreeMap::new();
Expand All @@ -197,26 +210,35 @@ pub async fn get_data_contract_history_with_proof_info(

// Use json_compatible serializer
let serializer = serde_wasm_bindgen::Serializer::json_compatible();
response.serialize(&serializer)
response
.serialize(&serializer)
.map_err(|e| JsError::new(&format!("Failed to serialize response: {}", e)))
}

#[wasm_bindgen]
pub async fn get_data_contracts_with_proof_info(sdk: &WasmSdk, ids: Vec<String>) -> Result<JsValue, JsError> {
pub async fn get_data_contracts_with_proof_info(
sdk: &WasmSdk,
ids: Vec<String>,
) -> Result<JsValue, JsError> {
// Parse all contract IDs
let identifiers: Result<Vec<Identifier>, _> = ids
.iter()
.map(|id| Identifier::from_string(
.map(|id| {
Identifier::from_string(
id,
dash_sdk::dpp::platform_value::string_encoding::Encoding::Base58,
))
)
})
.collect();
let identifiers = identifiers?;

// Fetch all contracts with proof
let (contracts_result, metadata, proof) = DataContract::fetch_many_with_metadata_and_proof(sdk.as_ref(), identifiers, None)
let (contracts_result, metadata, proof) =
DataContract::fetch_many_with_metadata_and_proof(sdk.as_ref(), identifiers, None)
.await
.map_err(|e| JsError::new(&format!("Failed to fetch data contracts with proof: {}", e)))?;
.map_err(|e| {
JsError::new(&format!("Failed to fetch data contracts with proof: {}", e))
})?;

// Convert to response format
let mut data_contracts = BTreeMap::new();
Expand All @@ -240,6 +262,7 @@ pub async fn get_data_contracts_with_proof_info(sdk: &WasmSdk, ids: Vec<String>)

// Use json_compatible serializer
let serializer = serde_wasm_bindgen::Serializer::json_compatible();
response.serialize(&serializer)
response
.serialize(&serializer)
.map_err(|e| JsError::new(&format!("Failed to serialize response: {}", e)))
}
Loading
Loading