Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 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
24 changes: 17 additions & 7 deletions packages/wasm-dpp/src/data_contract/data_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ impl std::convert::Into<DataContract> for DataContractWasm {
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct DataContractParameters {
#[serde(rename = "$schema")]
schema: String,
#[serde(rename = "$id")]
id: Vec<u8>,
owner_id: Vec<u8>,
#[serde(
rename = "$schema",
skip_serializing_if = "serde_json::Value::is_null",
default
)]
schema: serde_json::Value,
#[serde(rename = "$id", skip_serializing_if = "Option::is_none")]
id: Option<Vec<u8>>,
#[serde(skip_serializing_if = "Option::is_none")]
owner_id: Option<Vec<u8>>,
#[serde(skip_serializing_if = "serde_json::Value::is_null", default)]
documents: serde_json::Value,
#[serde(
Expand All @@ -47,8 +52,13 @@ pub(crate) struct DataContractParameters {
rename = "$defs"
)]
defs: serde_json::Value,
protocol_version: u32,
version: u32,
#[serde(skip_serializing_if = "serde_json::Value::is_null", default)]
protocol_version: serde_json::Value,
#[serde(skip_serializing_if = "serde_json::Value::is_null", default)]
version: serde_json::Value,

#[serde(flatten)]
_extras: serde_json::Value // Captures excess fields to trigger validation failure later.
}

#[wasm_bindgen(js_class=DataContract)]
Expand Down
58 changes: 58 additions & 0 deletions packages/wasm-dpp/src/data_contract/index.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use dpp::{data_contract::extra::IndexProperty, util::json_schema::Index};
use serde::{
ser::{SerializeMap, SerializeSeq},
Comment thread
fominok marked this conversation as resolved.
Outdated
Serialize, Serializer,
};
use wasm_bindgen::prelude::*;

use crate::{errors::RustConversionError, with_js_error};

#[wasm_bindgen(js_name=IndexProperty)]
#[derive(Debug, Clone)]
pub struct IndexPropertyWasm {
Expand Down Expand Up @@ -38,6 +44,51 @@ impl From<Index> for IndexDefinitionWasm {
}
}

/// Wrapper structure to serialize `Index` the way js-dpp expects it.
#[derive(Debug)]
struct IndexSerializeJs<'a>(&'a Index);

impl Serialize for IndexSerializeJs<'_> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut map = serializer.serialize_map(Some(3))?;
map.serialize_entry("name", &self.0.name)?;
if self.0.unique {
map.serialize_entry("unique", &true)?;
}
map.serialize_entry(
"properties",
&self
.0
.properties
.iter()
.map(IndexPropertySerializeJs)
.collect::<Vec<_>>(),
)?;

map.end()
}
}

/// Wrapper structure to serialize `InderProperty` the way js-dpp expects it.
#[derive(Debug)]
struct IndexPropertySerializeJs<'a>(&'a IndexProperty);

impl Serialize for IndexPropertySerializeJs<'_> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut map = serializer.serialize_map(Some(1))?;
let order_str = if self.0.ascending { "asc" } else { "desc" };
map.serialize_entry(&self.0.name, order_str)?;

map.end()
}
}

#[wasm_bindgen(js_class=IndexDefinition)]
impl IndexDefinitionWasm {
#[wasm_bindgen(js_name=getName)]
Expand All @@ -58,4 +109,11 @@ impl IndexDefinitionWasm {
pub fn is_unique(&self) -> bool {
self.inner.unique
}

#[wasm_bindgen(js_name=toObject)]
pub fn to_object(&self) -> Result<JsValue, JsValue> {
let serializer = serde_wasm_bindgen::Serializer::json_compatible();
let object = with_js_error!(IndexSerializeJs(&self.inner).serialize(&serializer))?;
Ok(object)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ 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");

self.0
.validate(&json_object)
.map(Into::into)
Expand Down
8 changes: 4 additions & 4 deletions packages/wasm-dpp/src/errors/protocol_error.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use wasm_bindgen::JsValue;
use wasm_bindgen::{JsError, JsValue};

use super::consensus_error::from_consensus_error;

pub fn from_protocol_error(e: dpp::ProtocolError) -> JsValue {
match e {
pub fn from_protocol_error(protocol_error: dpp::ProtocolError) -> JsValue {
match protocol_error {
dpp::ProtocolError::AbstractConsensusError(consensus_error) => {
from_consensus_error(*consensus_error)
}
_ => unimplemented!(),
e => JsError::new(&format!("Protocol error: {e}")).into(),
Comment thread
fominok marked this conversation as resolved.
Outdated
}
}
1 change: 1 addition & 0 deletions packages/wasm-dpp/src/validation/validation_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use js_sys::JsString;
use wasm_bindgen::prelude::*;

#[wasm_bindgen(js_name=ValidationResult)]
#[derive(Debug)]
pub struct ValidationResultWasm(ValidationResult<()>);

#[wasm_bindgen(js_class=ValidationResult)]
Expand Down
Loading