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
64 changes: 64 additions & 0 deletions packages/wasm-dpp/lib/test/mocks/createStateRepositoryMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* @typedef {createStateRepositoryMock}
* @param sinonSandbox
* @return {{
* fetchDataContract: *,
* createDataContract: *,
* updateDataContract: *,
* fetchDocuments: *,
* createDocument: *,
* updateDocument: *,
* removeDocument: *,
* fetchTransaction: *,
* fetchIdentity: *,
* createIdentity: *,
* updateIdentityRevision: *,
* disableIdentityKeys: *,
* addKeysToIdentity: *,
* fetchIdentityBalance: *,
* fetchIdentityBalanceWithDebt: *,
* addToIdentityBalance: *,
* addToSystemCredits: *,
* fetchLatestPlatformBlockHeight: *,
* fetchLatestPlatformCoreChainLockedHeight: *,
* verifyInstantLock: *,
* markAssetLockTransactionOutPointAsUsed: *,
* verifyChainLockHeight: *,
* isAssetLockTransactionOutPointAlreadyUsed: *,
* fetchSMLStore: *,
* fetchLatestWithdrawalTransactionIndex: *,
* enqueueWithdrawalTransaction: *,
* fetchLatestPlatformBlockTime: *,
* }}
*/
module.exports = function createStateRepositoryMock(sinonSandbox) {
return {
fetchDataContract: sinonSandbox.stub(),
createDataContract: sinonSandbox.stub(),
updateDataContract: sinonSandbox.stub(),
fetchDocuments: sinonSandbox.stub(),
createDocument: sinonSandbox.stub(),
updateDocument: sinonSandbox.stub(),
removeDocument: sinonSandbox.stub(),
fetchTransaction: sinonSandbox.stub(),
fetchIdentity: sinonSandbox.stub(),
createIdentity: sinonSandbox.stub(),
addKeysToIdentity: sinonSandbox.stub(),
disableIdentityKeys: sinonSandbox.stub(),
updateIdentityRevision: sinonSandbox.stub(),
addToIdentityBalance: sinonSandbox.stub(),
fetchIdentityBalance: sinonSandbox.stub(),
fetchIdentityBalanceWithDebt: sinonSandbox.stub(),
addToSystemCredits: sinonSandbox.stub(),
fetchLatestPlatformBlockHeight: sinonSandbox.stub(),
fetchLatestPlatformCoreChainLockedHeight: sinonSandbox.stub(),
verifyInstantLock: sinonSandbox.stub(),
markAssetLockTransactionOutPointAsUsed: sinonSandbox.stub(),
verifyChainLockHeight: sinonSandbox.stub(),
isAssetLockTransactionOutPointAlreadyUsed: sinonSandbox.stub(),
fetchSMLStore: sinonSandbox.stub(),
fetchLatestWithdrawalTransactionIndex: sinonSandbox.stub(),
enqueueWithdrawalTransaction: sinonSandbox.stub(),
fetchLatestPlatformBlockTime: sinonSandbox.stub(),
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl InvalidDocumentTypeInDataContractError {
self.doc_type.clone()
}

#[wasm_bindgen(js_name = "getDataContract")]
#[wasm_bindgen(js_name = "getDataContractId")]
pub fn get_data_contract_id(&self) -> IdentifierWrapper {
self.data_contract_id.clone()
}
Expand Down
30 changes: 29 additions & 1 deletion packages/wasm-dpp/src/document/factory.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{collections::HashMap, convert::TryFrom, sync::Arc};

use anyhow::anyhow;

use dpp::{
document::{
self,
Expand All @@ -10,8 +11,11 @@ use dpp::{
},
prelude::Document,
};

use wasm_bindgen::prelude::*;

use crate::document::errors::InvalidActionNameError;

use crate::{
document::document_data_to_bytes,
identifier::identifier_from_js_value,
Expand Down Expand Up @@ -93,7 +97,7 @@ impl DocumentFactoryWASM {
DocumentFactoryWASM(factory)
}

#[wasm_bindgen(js_name=create)]
#[wasm_bindgen]
pub fn create(
&self,
data_contract: &DataContractWasm,
Expand All @@ -103,6 +107,7 @@ impl DocumentFactoryWASM {
) -> Result<DocumentWasm, JsValue> {
let owner_id = identifier_from_js_value(js_owner_id)?;
let dynamic_data = data.with_serde_to_json_value()?;

let document = self
.0
.create(
Expand Down Expand Up @@ -184,6 +189,8 @@ impl DocumentFactoryWASM {
fn extract_documents_by_action(
documents: &JsValue,
) -> Result<HashMap<Action, Vec<Document>>, JsValue> {
check_actions(documents)?;

let mut documents_by_action: HashMap<Action, Vec<Document>> = Default::default();

let documents_create = extract_documents_of_action(documents, "create").with_js_error()?;
Expand All @@ -197,6 +204,27 @@ fn extract_documents_by_action(
Ok(documents_by_action)
}

fn check_actions(documents: &JsValue) -> Result<(), JsValue> {
if !documents.is_object() {
return Err(anyhow!("Expected documents to be an object")).with_js_error();
}

let documents_object = js_sys::Object::from(documents.clone());

let actions: js_sys::Array = js_sys::Object::keys(&documents_object);

for action in actions.iter() {
let action_string: String = action
.as_string()
.ok_or_else(|| anyhow!("Expected all keys to be strings"))
.with_js_error()?;
Action::try_from(action_string)
.map_err(|_| InvalidActionNameError::new(vec![action.clone()]))?;
}

Ok(())
}

fn extract_documents_of_action(
documents: &JsValue,
action: &str,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use dpp::prelude::Identifier;
use wasm_bindgen::prelude::*;

use crate::buffer::Buffer;
use crate::identifier::IdentifierWrapper;

#[wasm_bindgen(js_name=InvalidDocumentTypeError)]
pub struct InvalidDocumentTypeErrorWasm {
Expand All @@ -22,6 +23,19 @@ impl InvalidDocumentTypeErrorWasm {

#[wasm_bindgen(js_class=InvalidDocumentTypeError)]
impl InvalidDocumentTypeErrorWasm {
#[wasm_bindgen(constructor)]
pub fn constructor(
document_type: String,
data_contract_id: IdentifierWrapper,
code: u32,
) -> Self {
Self {
document_type,
data_contract_id: data_contract_id.into(),
code,
}
}

#[wasm_bindgen(js_name=getType)]
pub fn get_document_type(&self) -> String {
self.document_type.clone()
Expand Down
5 changes: 1 addition & 4 deletions packages/wasm-dpp/src/identifier/errors.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use dpp::prelude::Identifier;
use thiserror::Error;
use wasm_bindgen::prelude::*;

use crate::buffer::Buffer;

#[derive(Error, Debug)]
#[wasm_bindgen(js_name=IdentifierError)]
#[error("{message}")]
Expand All @@ -28,6 +25,6 @@ impl IdentifierErrorWasm {

#[wasm_bindgen(js_name=toString)]
pub fn print(&self) -> String {
format!("IdentifierError: {0}", { &self.message }).into()
format!("IdentifierError: {0}", { &self.message })
}
}
Loading