Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ impl DataContractValidatorWasm {
let parameters: DataContractParameters =
with_js_error!(serde_wasm_bindgen::from_value(raw_data_contract))?;
let json_object = serde_json::to_value(parameters).expect("Implements Serialize");
let validation_result = self.0.validate(&json_object).map_err(from_protocol_error)?;

let validation_result = self.0.validate(&json_object).map_err(from_protocol_error)?;
Ok(validation_result.map(|_| JsValue::undefined()).into())
}
}
Expand Down
40 changes: 40 additions & 0 deletions packages/wasm-dpp/src/document/fetch_and_validate_data_contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use dpp::{
document::{self, fetch_and_validate_data_contract::fetch_and_validate_data_contract},
prelude::DataContract,
state_transition::state_transition_execution_context::StateTransitionExecutionContext,
util::json_value::{JsonValueExt, ReplaceWith},
validation::ValidationResult,
};
use wasm_bindgen::prelude::*;

use crate::{
state_repository::{ExternalStateRepositoryLike, ExternalStateRepositoryLikeWrapper},
utils::{ToSerdeJSONExt, WithJsError},
validation::ValidationResultWasm,
DataContractWasm,
};

#[wasm_bindgen(js_name = fetchAndValidateDataContract)]
pub async fn fetch_and_validate_data_contract_wasm(
state_repository: ExternalStateRepositoryLike,
js_raw_document: JsValue,
) -> Result<ValidationResultWasm, JsValue> {
let wrapped_state_repository = ExternalStateRepositoryLikeWrapper::new(state_repository);
let mut document_value = js_raw_document.with_serde_to_json_value()?;

// Allow to fail as identifier can be type of Buffer or Identifier
// We don't need to replace dynamic values because the function doesn't use them
let _ =
document_value.replace_identifier_paths(document::IDENTIFIER_FIELDS, ReplaceWith::Bytes);

// TODO! remove the context. The the providing the context in state repository should be optional
let ctx = StateTransitionExecutionContext::default();
let validation_result =
fetch_and_validate_data_contract(&wrapped_state_repository, &document_value, &ctx)
.await
.with_js_error()?;
let result_with_js_value: ValidationResult<JsValue> = validation_result
.map(|dc| <DataContractWasm as std::convert::From<DataContract>>::from(dc).into());

Ok(result_with_js_value.into())
}
46 changes: 37 additions & 9 deletions packages/wasm-dpp/src/document/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use dpp::util::json_schema::JsonSchemaExt;
use dpp::util::json_value::{JsonValueExt, ReplaceWith};
use dpp::util::string_encoding::Encoding;
use serde::{Deserialize, Serialize};
use std::convert::TryInto;
use std::convert::{self, TryInto};
use wasm_bindgen::prelude::*;

use dpp::document::{property_names, Document, IDENTIFIER_FIELDS};
Expand All @@ -21,25 +21,27 @@ use crate::{DataContractWasm, MetadataWasm};
pub mod errors;
pub use state_transition::*;
mod factory;
pub mod fetch_and_validate_data_contract;
pub mod state_transition;
mod validator;

pub use document_batch_transition::{DocumentsBatchTransitionWASM, DocumentsContainer};
pub use factory::DocumentFactoryWASM;
pub use validator::DocumentValidatorWasm;

#[derive(Serialize, Deserialize, Debug, Clone, Copy, Default)]
#[serde(rename_all = "camelCase")]
pub(crate) struct ConversionOptions {
#[serde(default)]
pub skip_identifiers_conversion: bool,
}

pub(super) enum BinaryType {
Identifier,
Buffer,
None,
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy, Default)]
#[serde(rename_all = "camelCase")]
pub struct ConversionOptions {
skip_identifiers_conversion: bool,
}

#[wasm_bindgen(js_name=Document)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DocumentWasm(Document);
Expand Down Expand Up @@ -155,9 +157,35 @@ impl DocumentWasm {

#[wasm_bindgen(js_name=getData)]
pub fn get_data(&mut self) -> Result<JsValue, JsValue> {
let serializer = serde_wasm_bindgen::Serializer::json_compatible();
let js_value = self
.0
.data
.serialize(&serde_wasm_bindgen::Serializer::json_compatible())?;

Ok(with_js_error!(self.0.data.serialize(&serializer))?)
let (identifier_paths, binary_paths) = self
.0
.data_contract
.get_identifiers_and_binary_paths(&self.0.document_type)
.with_js_error()?;

for path in identifier_paths {
if let Ok(value) = self.0.data.get_value(path) {
let bytes: Vec<u8> = serde_json::from_value(value.to_owned()).with_js_error()?;
let id = <IdentifierWrapper as convert::From<Identifier>>::from(
Identifier::from_bytes(&bytes).unwrap(),
);
lodash_set(&js_value, path, id.into());
}
}
for path in binary_paths {
if let Ok(value) = self.0.data.get_value(path) {
let bytes: Vec<u8> = serde_json::from_value(value.to_owned()).with_js_error()?;
let buffer = Buffer::from_bytes(&bytes);
lodash_set(&js_value, path, buffer.into());
}
}

Ok(js_value)
}

#[wasm_bindgen(js_name=set)]
Expand Down
Loading