From 3b511ce9b5dd4d4a0aba426e8a9b8c0a3589413b Mon Sep 17 00:00:00 2001 From: pshenmic Date: Sat, 5 Oct 2024 23:45:52 +0700 Subject: [PATCH 01/15] feat(wasm-dpp): add transfer transition to documents batch wip --- .../src/document/document_factory/v0/mod.rs | 87 +++++ packages/wasm-dpp/src/document/factory.rs | 8 + .../document_transfer_transition.rs | 300 ++++++++++++++++++ .../document_transition/mod.rs | 1 + 4 files changed, 396 insertions(+) create mode 100644 packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/document_transfer_transition.rs diff --git a/packages/rs-dpp/src/document/document_factory/v0/mod.rs b/packages/rs-dpp/src/document/document_factory/v0/mod.rs index 58fc96de91a..5aa64b775a3 100644 --- a/packages/rs-dpp/src/document/document_factory/v0/mod.rs +++ b/packages/rs-dpp/src/document/document_factory/v0/mod.rs @@ -507,6 +507,93 @@ impl DocumentFactoryV0 { // Ok(raw_transitions) } + #[cfg(feature = "state-transitions")] + fn document_transfer_transitions( + documents: Vec<(Document, DocumentTypeRef)>, + nonce_counter: &mut BTreeMap<(Identifier, Identifier), u64>, //IdentityID/ContractID -> nonce + platform_version: &PlatformVersion, + ) -> Result, ProtocolError> { + documents + .into_iter() + .map(|(mut document, document_type)| { + if !document_type.documents_mutable() { + return Err(DocumentError::TryingToReplaceImmutableDocument { + document: Box::new(document), + } + .into()); + } + if document.revision().is_none() { + return Err(DocumentError::RevisionAbsentError { + document: Box::new(document), + } + .into()); + }; + + document.increment_revision()?; + document.set_updated_at(Some(Utc::now().timestamp_millis() as TimestampMillis)); + + let recipient_owner_id = document.owner_id(); + + let nonce = nonce_counter + .entry((document.owner_id(), document_type.data_contract_id())) + .or_default(); + + let transition = DocumentTransferTransition::from_document( + document, + document_type, + *nonce, + recipient_owner_id, + platform_version, + None, + None, + )?; + + *nonce += 1; + + Ok(transition.into()) + }) + .collect() + // let mut raw_transitions = vec![]; + // for (document, document_type) in documents { + // if !document_type.documents_mutable() { + // return Err(DocumentError::TryingToReplaceImmutableDocument { + // document: Box::new(document), + // } + // .into()); + // } + // let Some(document_revision) = document.revision() else { + // return Err(DocumentError::RevisionAbsentError { + // document: Box::new(document), + // }.into()); + // }; + // let mut map = document.to_map_value()?; + // + // map.retain(|key, _| { + // !key.starts_with('$') || DOCUMENT_REPLACE_KEYS_TO_STAY.contains(&key.as_str()) + // }); + // map.insert( + // PROPERTY_ACTION.to_string(), + // Value::U8(DocumentTransitionActionType::Replace as u8), + // ); + // let new_revision = document_revision + 1; + // map.insert(PROPERTY_REVISION.to_string(), Value::U64(new_revision)); + // + // // If document have an originally set `updatedAt` + // // we should update it then + // let contains_updated_at = document_type + // .required_fields() + // .contains(PROPERTY_UPDATED_AT); + // + // if contains_updated_at { + // let now = Utc::now().timestamp_millis() as TimestampMillis; + // map.insert(PROPERTY_UPDATED_AT.to_string(), Value::U64(now)); + // } + // + // raw_transitions.push(map.into()); + // } + // Ok(raw_transitions) + } + #[cfg(feature = "state-transitions")] fn document_delete_transitions( documents: Vec<(Document, DocumentTypeRef)>, diff --git a/packages/wasm-dpp/src/document/factory.rs b/packages/wasm-dpp/src/document/factory.rs index 0b927a60803..038fb8db998 100644 --- a/packages/wasm-dpp/src/document/factory.rs +++ b/packages/wasm-dpp/src/document/factory.rs @@ -32,6 +32,7 @@ pub struct DocumentTransitions { create: Vec, replace: Vec, delete: Vec, + transfer: Vec, } #[wasm_bindgen(js_class=DocumentTransitions)] @@ -55,6 +56,11 @@ impl DocumentTransitions { pub fn add_transition_delete(&mut self, transition: ExtendedDocumentWasm) { self.delete.push(transition) } + + #[wasm_bindgen(js_name = "addTransitionTransfer")] + pub fn add_transition_transfer(&mut self, transition: ExtendedDocumentWasm) { + self.transfer.push(transition) + } } #[wasm_bindgen(js_name = DocumentFactory)] @@ -278,10 +284,12 @@ fn extract_documents_by_action( let documents_create = extract_documents_of_action(documents, "create").with_js_error()?; let documents_replace = extract_documents_of_action(documents, "replace").with_js_error()?; let documents_delete = extract_documents_of_action(documents, "delete").with_js_error()?; + let documents_transfer = extract_documents_of_action(documents, "transfer").with_js_error()?; documents_by_action.insert(DocumentTransitionActionType::Create, documents_create); documents_by_action.insert(DocumentTransitionActionType::Replace, documents_replace); documents_by_action.insert(DocumentTransitionActionType::Delete, documents_delete); + documents_by_action.insert(DocumentTransitionActionType::Transfer, documents_transfer); Ok(documents_by_action) } diff --git a/packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/document_transfer_transition.rs b/packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/document_transfer_transition.rs new file mode 100644 index 00000000000..b6e9cdfba7e --- /dev/null +++ b/packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/document_transfer_transition.rs @@ -0,0 +1,300 @@ +use std::convert; + +use serde_json::Value as JsonValue; + +use dpp::platform_value::btreemap_extensions::{ + BTreeValueMapHelper, BTreeValueMapReplacementPathHelper, +}; +use dpp::platform_value::ReplacementType; +use dpp::prelude::Revision; +use dpp::{ + prelude::{Identifier}, + ProtocolError, +}; +use wasm_bindgen::prelude::*; +use dpp::data_contract::accessors::v0::DataContractV0Getters; +use dpp::state_transition::documents_batch_transition::document_base_transition::v0::v0_methods::DocumentBaseTransitionV0Methods; + +use crate::{ + buffer::Buffer, + document::state_transition::document_batch_transition::document_transition::to_object, + identifier::IdentifierWrapper, + lodash::lodash_set, + utils::{ToSerdeJSONExt, WithJsError}, + BinaryType, DataContractWasm, +}; +use dpp::state_transition::documents_batch_transition::document_transition::action_type::DocumentTransitionActionType; +use dpp::state_transition::documents_batch_transition::document_transition::document_transfer_transition::v0::v0_methods::DocumentTransferTransitionV0Methods; +use dpp::state_transition::documents_batch_transition::document_transition::DocumentTransferTransition; + +#[wasm_bindgen(js_name=DocumentTransferTransition)] +#[derive(Debug, Clone)] +pub struct DocumentTransferTransitionWasm { + inner: DocumentTransferTransition, +} + +impl From for DocumentTransferTransitionWasm { + fn from(v: DocumentTransferTransition) -> Self { + Self { inner: v } + } +} + +impl From for DocumentTransferTransition { + fn from(v: DocumentTransferTransitionWasm) -> Self { + v.inner + } +} + +#[wasm_bindgen(js_class=DocumentTransferTransition)] +impl DocumentTransferTransitionWasm { + // #[wasm_bindgen(constructor)] + // pub fn from_object( + // raw_object: JsValue, + // data_contract: &DataContractWasm, + // ) -> Result { + // let mut value = raw_object.with_serde_to_platform_value_map()?; + // let document_type_name = value + // .get_string(dpp::document::extended_document::property_names::DOCUMENT_TYPE_NAME) + // .map_err(ProtocolError::ValueError) + // .with_js_error()?; + // + // let document_type = data_contract + // .inner() + // .document_type_for_name(&document_type_name) + // .with_js_error()?; + // let identifier_paths = document_type.identifier_paths(); + // let binary_paths = document_type.binary_paths(); + // + // value + // .replace_at_paths(identifier_paths, ReplacementType::Identifier) + // .map_err(ProtocolError::ValueError) + // .with_js_error()?; + // + // value + // .replace_at_paths(binary_paths, ReplacementType::BinaryBytes) + // .map_err(ProtocolError::ValueError) + // .with_js_error()?; + // let transition = + // DocumentTransferTransition::from_value_map(value, data_contract).with_js_error()?; + // + // Ok(transition.into()) + // } + // + // #[wasm_bindgen(js_name=getAction)] + // pub fn action(&self) -> u8 { + // DocumentTransitionActionType::Transfer as u8; + // } + // + // #[wasm_bindgen(js_name=getRevision)] + // pub fn revision(&self) -> Revision { + // self.inner.revision() + // } + // + // #[wasm_bindgen(js_name=getUpdatedAt)] + // pub fn updated_at(&self) -> Option { + // self.inner + // .updated_at() + // .map(|timestamp| js_sys::Date::new(&JsValue::from_f64(timestamp as f64))) + // } + // + // #[wasm_bindgen(js_name=toObject)] + // pub fn to_object( + // &self, + // options: &JsValue, + // data_contract: DataContractWasm, + // ) -> Result { + // let document_type = data_contract + // .inner() + // .document_type_for_name(self.inner.base().document_type_name()) + // .with_js_error()?; + // let identifier_paths = document_type.identifier_paths(); + // let binary_paths = document_type.binary_paths(); + // + // to_object( + // self.inner.to_object().with_js_error()?, + // options, + // identifier_paths + // .into_iter() + // .chain(document_transfer_transition::v0::IDENTIFIER_FIELDS), + // binary_paths, + // ) + // } + // + // #[wasm_bindgen(js_name=toJSON)] + // pub fn to_json(&self) -> Result { + // let value = self.inner.to_json().with_js_error()?; + // let serializer = serde_wasm_bindgen::Serializer::json_compatible(); + // let js_value = value.serialize(&serializer)?; + // Ok(js_value) + // } + // + // // AbstractDataDocumentTransition + // #[wasm_bindgen(js_name=getData)] + // pub fn get_data(&self, data_contract: DataContractWasm) -> Result { + // let data = if let Some(ref data) = self.inner.data() { + // data + // } else { + // return Ok(JsValue::undefined()); + // }; + // + // let js_value = data.serialize(&serde_wasm_bindgen::Serializer::json_compatible())?; + // let document_type = data_contract + // .inner() + // .document_type_for_name(self.inner.base().document_type_name()) + // .with_js_error()?; + // let identifier_paths = document_type.identifier_paths(); + // let binary_paths = document_type.binary_paths(); + // + // for path in identifier_paths { + // let bytes = data + // .get_identifier_bytes_at_path(path) + // .map_err(ProtocolError::ValueError) + // .with_js_error()?; + // let id = >::from( + // Identifier::from_bytes(&bytes).unwrap(), + // ); + // lodash_set(&js_value, path, id.into()); + // } + // for path in binary_paths { + // let bytes = data + // .get_binary_bytes_at_path(path) + // .map_err(ProtocolError::ValueError) + // .with_js_error()?; + // let buffer = Buffer::from_bytes(&bytes); + // lodash_set(&js_value, path, buffer.into()); + // } + // + // Ok(js_value) + // } + // + // // AbstractDocumentTransition + // #[wasm_bindgen(js_name=getId)] + // pub fn id(&self) -> IdentifierWrapper { + // self.inner.base().id().into() + // } + // + // #[wasm_bindgen(js_name=getType)] + // pub fn document_type(&self) -> String { + // self.inner.base().document_type_name().clone() + // } + // + // #[wasm_bindgen(js_name=getDataContractId)] + // pub fn data_contract_id(&self) -> IdentifierWrapper { + // self.inner.base().data_contract_id().into() + // } + // + // #[wasm_bindgen(js_name=get)] + // pub fn get(&self, path: String) -> Result { + // let document_data = if let Some(ref data) = self.inner.data() { + // data + // } else { + // return Ok(JsValue::undefined()); + // }; + // + // let value = if let Ok(value) = document_data.get_at_path(&path) { + // value.to_owned() + // } else { + // return Ok(JsValue::undefined()); + // }; + // + // match self.get_binary_type_of_path(&path) { + // BinaryType::Buffer => { + // let bytes: Vec = serde_json::from_value( + // value + // .try_into() + // .map_err(ProtocolError::ValueError) + // .with_js_error()?, + // ) + // .unwrap(); + // let buffer = Buffer::from_bytes(&bytes); + // return Ok(buffer.into()); + // } + // BinaryType::Identifier => { + // let bytes: Vec = serde_json::from_value( + // value + // .try_into() + // .map_err(ProtocolError::ValueError) + // .with_js_error()?, + // ) + // .unwrap(); + // let id = >::from( + // Identifier::from_bytes(&bytes).unwrap(), + // ); + // return Ok(id.into()); + // } + // BinaryType::None => { + // // Do nothing. If is 'None' it means that binary may contain binary data + // // or may not captain it at all + // } + // } + // + // let json_value: JsonValue = value + // .clone() + // .try_into() + // .map_err(ProtocolError::ValueError) + // .with_js_error()?; + // let map = value + // .to_btree_ref_string_map() + // .map_err(ProtocolError::ValueError) + // .with_js_error()?; + // let js_value = json_value.serialize(&serde_wasm_bindgen::Serializer::json_compatible())?; + // let (identifier_paths, binary_paths) = self + // .inner + // .base() + // .data_contract() + // .get_identifiers_and_binary_paths(&self.inner.base().document_type_name()) + // .with_js_error()?; + // + // for property_path in identifier_paths { + // if property_path.starts_with(&path) { + // let (_, suffix) = property_path.split_at(path.len() + 1); + // + // if let Some(bytes) = map + // .get_optional_bytes_at_path(suffix) + // .map_err(ProtocolError::ValueError) + // .with_js_error()? + // { + // let id = >::from( + // Identifier::from_bytes(&bytes).unwrap(), + // ); + // lodash_set(&js_value, suffix, id.into()); + // } + // } + // } + // + // for property_path in binary_paths { + // if property_path.starts_with(&path) { + // let (_, suffix) = property_path.split_at(path.len() + 1); + // + // if let Some(bytes) = map + // .get_optional_bytes_at_path(suffix) + // .map_err(ProtocolError::ValueError) + // .with_js_error()? + // { + // let buffer = Buffer::from_bytes(&bytes); + // lodash_set(&js_value, suffix, buffer.into()); + // } + // } + // } + // + // Ok(js_value) + // } +} + +impl DocumentTransferTransitionWasm { + // fn get_binary_type_of_path(&self, path: &String) -> BinaryType { + // let maybe_binary_properties = self + // .inner + // .get_binary_properties(&self.inner.base().document_type_name()); + // + // if let Ok(binary_properties) = maybe_binary_properties { + // if let Some(data) = binary_properties.get(path) { + // if data.is_type_of_identifier() { + // return BinaryType::Identifier; + // } + // return BinaryType::Buffer; + // } + // } + // BinaryType::None + // } +} diff --git a/packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/mod.rs b/packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/mod.rs index a26d579d7de..36199f0fb0c 100644 --- a/packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/mod.rs +++ b/packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/mod.rs @@ -1,4 +1,5 @@ mod document_create_transition; +mod document_transfer_transition; // mod document_delete_transition; // mod document_replace_transition; From d3383a19b3de297ae349267b01861a40b77c4847 Mon Sep 17 00:00:00 2001 From: pshenmic Date: Sat, 5 Oct 2024 23:46:30 +0700 Subject: [PATCH 02/15] feat(js-dash-sk): add transfer to document broadcast --- .../src/SDK/Client/Platform/methods/documents/broadcast.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/broadcast.ts b/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/broadcast.ts index 4bbe6b6479d..10031938e11 100644 --- a/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/broadcast.ts +++ b/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/broadcast.ts @@ -18,7 +18,8 @@ export default async function broadcast( documents: { create?: ExtendedDocument[], replace?: ExtendedDocument[], - delete?: ExtendedDocument[] + delete?: ExtendedDocument[], + transfer?: ExtendedDocument[] }, identity: any, ): Promise { @@ -26,6 +27,7 @@ export default async function broadcast( create: documents.create?.length || 0, replace: documents.replace?.length || 0, delete: documents.delete?.length || 0, + transfer: documents.transfer?.length || 0, }); await this.initialize(); @@ -36,6 +38,7 @@ export default async function broadcast( ...(documents.create || []), ...(documents.replace || []), ...(documents.delete || []), + ...(documents.transfer || []), ][0]?.getDataContractId(); if (!dataContractId) { @@ -79,6 +82,7 @@ export default async function broadcast( create: documents.create?.length || 0, replace: documents.replace?.length || 0, delete: documents.delete?.length || 0, + transfer: documents.transfer?.length || 0, }); return documentsBatchTransition; From fae59153a23d6bdc483b8d7bea72eef59dea01e2 Mon Sep 17 00:00:00 2001 From: pshenmic Date: Sat, 5 Oct 2024 23:59:01 +0700 Subject: [PATCH 03/15] fix(dpp): add missing import --- packages/rs-dpp/src/document/document_factory/v0/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/rs-dpp/src/document/document_factory/v0/mod.rs b/packages/rs-dpp/src/document/document_factory/v0/mod.rs index 5aa64b775a3..68be2142af8 100644 --- a/packages/rs-dpp/src/document/document_factory/v0/mod.rs +++ b/packages/rs-dpp/src/document/document_factory/v0/mod.rs @@ -32,6 +32,7 @@ use crate::state_transition::documents_batch_transition::{ DocumentsBatchTransition, DocumentsBatchTransitionV0, }; use itertools::Itertools; +use crate::state_transition::documents_batch_transition::document_transition::DocumentTransferTransition; const PROPERTY_FEATURE_VERSION: &str = "$version"; const PROPERTY_ENTROPY: &str = "$entropy"; From fd48355ff013ca72b8cfa2b126733eeefb515ee8 Mon Sep 17 00:00:00 2001 From: pshenmic Date: Sun, 6 Oct 2024 00:01:16 +0700 Subject: [PATCH 04/15] feat(js-dash-sdk): add transfer function wip --- .../src/SDK/Client/Platform/Platform.ts | 3 ++ .../Platform/methods/documents/transfer.ts | 40 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/transfer.ts diff --git a/packages/js-dash-sdk/src/SDK/Client/Platform/Platform.ts b/packages/js-dash-sdk/src/SDK/Client/Platform/Platform.ts index 873fe62c53d..cd105ed87b7 100644 --- a/packages/js-dash-sdk/src/SDK/Client/Platform/Platform.ts +++ b/packages/js-dash-sdk/src/SDK/Client/Platform/Platform.ts @@ -9,6 +9,7 @@ import createAssetLockTransaction from './createAssetLockTransaction'; import broadcastDocument from './methods/documents/broadcast'; import createDocument from './methods/documents/create'; +import transferDocument from './methods/documents/transfer'; import getDocument from './methods/documents/get'; import publishContract from './methods/contracts/publish'; @@ -58,6 +59,7 @@ export interface PlatformOpts { interface Records { broadcast: Function, create: Function, + transfer: Function, get: Function, } @@ -165,6 +167,7 @@ export class Platform { this.documents = { broadcast: broadcastDocument.bind(this), create: createDocument.bind(this), + transfer: transferDocument.bind(this), get: getDocument.bind(this), }; this.contracts = { diff --git a/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/transfer.ts b/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/transfer.ts new file mode 100644 index 00000000000..0e27ce2bb6e --- /dev/null +++ b/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/transfer.ts @@ -0,0 +1,40 @@ +import { Platform } from '../../Platform'; +import broadcastStateTransition from '../../broadcastStateTransition'; + +/** + * Transfer document in the platform + * + * @param {Platform} this - bound instance class + * @param {string} typeLocator - type locator + * @param identity - identity + * @param {Object} [data] - options + */ +export async function transfer( + this: Platform, + documentId: string, + identity: any, +): Promise { + this.logger.debug(`[Document#transfer] Transfer document`); + await this.initialize(); + + const { dpp } = this; + + const document = await this.documents.get(documentId); + + this.logger.silly(`[Document#create] Obtained document ${document.getId()}`); + + if (document === null) { + throw new Error(`Document ${documentId} not found. Ensure contractId ${documentId} is correct.`); + } + + document.setOwnerId(identity); + + const transition = dpp.document.createStateTransition( + { transfer: [document] }, + identity.getId(), + ); + + await broadcastStateTransition(this, transition); +} + +export default transfer; From fbe5dbcc47dec94826ff2fa322fc732a5a0f5ef5 Mon Sep 17 00:00:00 2001 From: pshenmic Date: Wed, 25 Dec 2024 00:21:50 +0700 Subject: [PATCH 05/15] feat(js-sdk): implement working document transfers --- .../Platform/methods/documents/broadcast.ts | 9 ++--- .../Platform/methods/documents/transfer.ts | 32 +++++++---------- .../src/document/document_factory/v0/mod.rs | 20 +++++++++-- .../specialized_document_factory/v0/mod.rs | 17 +++++++-- .../documents_batch/transformer/v0/mod.rs | 2 +- .../src/document/extended_document.rs | 36 ++++++++++++++++--- .../document_transfer_transition.rs | 28 +-------------- 7 files changed, 83 insertions(+), 61 deletions(-) diff --git a/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/broadcast.ts b/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/broadcast.ts index 10031938e11..fdcd18d51d2 100644 --- a/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/broadcast.ts +++ b/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/broadcast.ts @@ -6,12 +6,12 @@ import { signStateTransition } from '../../signStateTransition'; /** * Broadcast document onto the platform * - * @param {Platform} this - bound instance class * @param {Object} documents * @param {ExtendedDocument[]} [documents.create] * @param {ExtendedDocument[]} [documents.replace] * @param {ExtendedDocument[]} [documents.delete] * @param identity - identity + * @param keyIndex - identity key index */ export default async function broadcast( this: Platform, @@ -19,15 +19,14 @@ export default async function broadcast( create?: ExtendedDocument[], replace?: ExtendedDocument[], delete?: ExtendedDocument[], - transfer?: ExtendedDocument[] }, identity: any, + keyIndex : number, ): Promise { this.logger.debug('[Document#broadcast] Broadcast documents', { create: documents.create?.length || 0, replace: documents.replace?.length || 0, delete: documents.delete?.length || 0, - transfer: documents.transfer?.length || 0, }); await this.initialize(); @@ -38,7 +37,6 @@ export default async function broadcast( ...(documents.create || []), ...(documents.replace || []), ...(documents.delete || []), - ...(documents.transfer || []), ][0]?.getDataContractId(); if (!dataContractId) { @@ -56,7 +54,7 @@ export default async function broadcast( this.logger.silly('[Document#broadcast] Created documents batch transition'); - await signStateTransition(this, documentsBatchTransition, identity, 1); + await signStateTransition(this, documentsBatchTransition, identity, keyIndex ?? 1); // Broadcast state transition also wait for the result to be obtained await broadcastStateTransition(this, documentsBatchTransition); @@ -82,7 +80,6 @@ export default async function broadcast( create: documents.create?.length || 0, replace: documents.replace?.length || 0, delete: documents.delete?.length || 0, - transfer: documents.transfer?.length || 0, }); return documentsBatchTransition; diff --git a/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/transfer.ts b/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/transfer.ts index 0e27ce2bb6e..43ccb1b39a7 100644 --- a/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/transfer.ts +++ b/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/transfer.ts @@ -1,6 +1,7 @@ +import { Identity, ExtendedDocument } from '@dashevo/wasm-dpp'; import { Platform } from '../../Platform'; import broadcastStateTransition from '../../broadcastStateTransition'; - +import { signStateTransition } from '../../signStateTransition'; /** * Transfer document in the platform * @@ -8,33 +9,26 @@ import broadcastStateTransition from '../../broadcastStateTransition'; * @param {string} typeLocator - type locator * @param identity - identity * @param {Object} [data] - options + * @returns {StateTransition} */ export async function transfer( this: Platform, - documentId: string, - identity: any, + document: ExtendedDocument, + receiver: Identity, + sender: Identity, ): Promise { - this.logger.debug(`[Document#transfer] Transfer document`); + this.logger.debug('[Document#transfer] Transfer document'); await this.initialize(); - const { dpp } = this; - - const document = await this.documents.get(documentId); - - this.logger.silly(`[Document#create] Obtained document ${document.getId()}`); - - if (document === null) { - throw new Error(`Document ${documentId} not found. Ensure contractId ${documentId} is correct.`); - } + const identityContractNonce = await this.nonceManager + .bumpIdentityContractNonce(sender.getId(), document.getDataContractId()); - document.setOwnerId(identity); + const documentsBatchTransition = document + .createTransferTransition(receiver.getId(), BigInt(identityContractNonce)); - const transition = dpp.document.createStateTransition( - { transfer: [document] }, - identity.getId(), - ); + await signStateTransition(this, documentsBatchTransition, sender, 1); - await broadcastStateTransition(this, transition); + await broadcastStateTransition(this, documentsBatchTransition); } export default transfer; diff --git a/packages/rs-dpp/src/document/document_factory/v0/mod.rs b/packages/rs-dpp/src/document/document_factory/v0/mod.rs index 68be2142af8..0280286f346 100644 --- a/packages/rs-dpp/src/document/document_factory/v0/mod.rs +++ b/packages/rs-dpp/src/document/document_factory/v0/mod.rs @@ -263,9 +263,25 @@ impl DocumentFactoryV0 { nonce_counter, platform_version, ), - _ => Err(ProtocolError::InvalidStateTransitionType( - "action type not accounted for".to_string(), + DocumentTransitionActionType::Transfer => Self::document_transfer_transitions( + documents + .into_iter() + .map(|(document, document_type, _)| (document, document_type)) + .collect(), + nonce_counter, + platform_version, + ), + DocumentTransitionActionType::Purchase => { + Err(ProtocolError::InvalidStateTransitionType( + "action type not accounted for Transfer".to_string(), + )) + } + DocumentTransitionActionType::UpdatePrice => Err(ProtocolError::InvalidStateTransitionType( + "action type not accounted for UpdatePrice".to_string(), )), + DocumentTransitionActionType::IgnoreWhileBumpingRevision => Err(ProtocolError::InvalidStateTransitionType( + "action type not accounted for IgnoreWhileBumpingRevision".to_string(), + )) }) .collect::, ProtocolError>>()? .into_iter() diff --git a/packages/rs-dpp/src/document/specialized_document_factory/v0/mod.rs b/packages/rs-dpp/src/document/specialized_document_factory/v0/mod.rs index e482d3822a7..56193af6ad8 100644 --- a/packages/rs-dpp/src/document/specialized_document_factory/v0/mod.rs +++ b/packages/rs-dpp/src/document/specialized_document_factory/v0/mod.rs @@ -270,9 +270,22 @@ impl SpecializedDocumentFactoryV0 { nonce_counter, platform_version, ), - _ => Err(ProtocolError::InvalidStateTransitionType( - "action type not accounted for".to_string(), + DocumentTransitionActionType::Transfer => { + Err(ProtocolError::InvalidStateTransitionType( + "action type not accounted for Transfer".to_string(), + )) + }, + DocumentTransitionActionType::Purchase => { + Err(ProtocolError::InvalidStateTransitionType( + "action type not accounted for Purchase".to_string(), + )) + } + DocumentTransitionActionType::UpdatePrice => Err(ProtocolError::InvalidStateTransitionType( + "action type not accounted for UpdatePrice".to_string(), )), + DocumentTransitionActionType::IgnoreWhileBumpingRevision => Err(ProtocolError::InvalidStateTransitionType( + "action type not accounted for IgnoreWhileBumpingRevision".to_string(), + )) }) .collect::, ProtocolError>>()? .into_iter() diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/documents_batch/transformer/v0/mod.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/documents_batch/transformer/v0/mod.rs index 68ec4de478e..1de6aa765cd 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/documents_batch/transformer/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/documents_batch/transformer/v0/mod.rs @@ -720,7 +720,7 @@ impl DocumentsBatchTransitionInternalTransformerV0 for DocumentsBatchTransition StateError::InvalidDocumentRevisionError(InvalidDocumentRevisionError::new( document_id, Some(previous_revision), - transition_revision, + expected_revision, )), )) } diff --git a/packages/wasm-dpp/src/document/extended_document.rs b/packages/wasm-dpp/src/document/extended_document.rs index 08d81ff81cd..cc321418b72 100644 --- a/packages/wasm-dpp/src/document/extended_document.rs +++ b/packages/wasm-dpp/src/document/extended_document.rs @@ -1,10 +1,8 @@ -use dpp::document::{ - DocumentV0Getters, DocumentV0Setters, ExtendedDocument, EXTENDED_DOCUMENT_IDENTIFIER_FIELDS, -}; +use dpp::document::{DocumentV0Getters, DocumentV0Setters, ExtendedDocument, EXTENDED_DOCUMENT_IDENTIFIER_FIELDS}; use serde_json::Value as JsonValue; use dpp::platform_value::{Bytes32, Value}; -use dpp::prelude::{Identifier, Revision, TimestampMillis}; +use dpp::prelude::{Identifier, IdentityNonce, Revision, TimestampMillis, UserFeeIncrease}; use dpp::util::json_value::JsonValueExt; @@ -16,12 +14,15 @@ use dpp::ProtocolError; use serde::{Deserialize, Serialize}; use std::convert::TryInto; use wasm_bindgen::prelude::*; +use dpp::state_transition::documents_batch_transition::document_transition::DocumentTransferTransition; +use dpp::state_transition::documents_batch_transition::{DocumentsBatchTransition, DocumentsBatchTransitionV0}; use crate::buffer::Buffer; use crate::data_contract::DataContractWasm; #[allow(deprecated)] // BinaryType is unsed in unused code below use crate::document::BinaryType; use crate::document::{ConversionOptions, DocumentWasm}; +use crate::document_batch_transition::DocumentsBatchTransitionWasm; use crate::errors::RustConversionError; use crate::identifier::{identifier_from_js_value, IdentifierWrapper}; use crate::lodash::lodash_set; @@ -235,6 +236,33 @@ impl ExtendedDocumentWasm { .set_created_at(ts.map(|t| t.get_time() as TimestampMillis)); } + #[wasm_bindgen(js_name=createTransferTransition)] + pub fn create_transfer_transition(&mut self, recipient: IdentifierWrapper, identity_contract_nonce: IdentityNonce) -> DocumentsBatchTransitionWasm { + let mut cloned_document = self.0.document().clone(); + + cloned_document.set_revision(Some(cloned_document.revision().unwrap() + 1)); + + let transfer_transition = DocumentTransferTransition::from_document( + cloned_document, + self.0.document_type().unwrap(), + identity_contract_nonce, + recipient.try_into().expect("identity into failed"), + PlatformVersion::latest(), + None, + None, + ).unwrap(); + + let documents_batch_transition: DocumentsBatchTransition = DocumentsBatchTransitionV0 { + owner_id: self.0.owner_id(), + transitions: vec![transfer_transition.into()], + user_fee_increase: Default::default(), + signature_public_key_id: Default::default(), + signature: Default::default(), + }.try_into().expect("Failed to convert into DocumentsBatchTransition"); + + documents_batch_transition.try_into().expect("Failed to convert into DocumentsBatchTransitionWasm") + } + #[wasm_bindgen(js_name=setUpdatedAt)] pub fn set_updated_at(&mut self, ts: Option) { self.0 diff --git a/packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/document_transfer_transition.rs b/packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/document_transfer_transition.rs index b6e9cdfba7e..0ebbf8e508c 100644 --- a/packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/document_transfer_transition.rs +++ b/packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/document_transfer_transition.rs @@ -1,30 +1,4 @@ -use std::convert; - -use serde_json::Value as JsonValue; - -use dpp::platform_value::btreemap_extensions::{ - BTreeValueMapHelper, BTreeValueMapReplacementPathHelper, -}; -use dpp::platform_value::ReplacementType; -use dpp::prelude::Revision; -use dpp::{ - prelude::{Identifier}, - ProtocolError, -}; -use wasm_bindgen::prelude::*; -use dpp::data_contract::accessors::v0::DataContractV0Getters; -use dpp::state_transition::documents_batch_transition::document_base_transition::v0::v0_methods::DocumentBaseTransitionV0Methods; - -use crate::{ - buffer::Buffer, - document::state_transition::document_batch_transition::document_transition::to_object, - identifier::IdentifierWrapper, - lodash::lodash_set, - utils::{ToSerdeJSONExt, WithJsError}, - BinaryType, DataContractWasm, -}; -use dpp::state_transition::documents_batch_transition::document_transition::action_type::DocumentTransitionActionType; -use dpp::state_transition::documents_batch_transition::document_transition::document_transfer_transition::v0::v0_methods::DocumentTransferTransitionV0Methods; +use wasm_bindgen::prelude::wasm_bindgen; use dpp::state_transition::documents_batch_transition::document_transition::DocumentTransferTransition; #[wasm_bindgen(js_name=DocumentTransferTransition)] From 8f79b69a9d12d20098a819aea0e157fbd7a3914c Mon Sep 17 00:00:00 2001 From: pshenmic Date: Wed, 25 Dec 2024 00:28:32 +0700 Subject: [PATCH 06/15] chore(js-sdk): cleanup --- packages/wasm-dpp/src/document/extended_document.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/wasm-dpp/src/document/extended_document.rs b/packages/wasm-dpp/src/document/extended_document.rs index cc321418b72..92f7e164e07 100644 --- a/packages/wasm-dpp/src/document/extended_document.rs +++ b/packages/wasm-dpp/src/document/extended_document.rs @@ -236,8 +236,8 @@ impl ExtendedDocumentWasm { .set_created_at(ts.map(|t| t.get_time() as TimestampMillis)); } - #[wasm_bindgen(js_name=createTransferTransition)] - pub fn create_transfer_transition(&mut self, recipient: IdentifierWrapper, identity_contract_nonce: IdentityNonce) -> DocumentsBatchTransitionWasm { + #[wasm_bindgen(js_name=createTransferStateTransition)] + pub fn create_transfer_state_transition(&mut self, recipient: IdentifierWrapper, identity_contract_nonce: IdentityNonce) -> DocumentsBatchTransitionWasm { let mut cloned_document = self.0.document().clone(); cloned_document.set_revision(Some(cloned_document.revision().unwrap() + 1)); @@ -246,7 +246,7 @@ impl ExtendedDocumentWasm { cloned_document, self.0.document_type().unwrap(), identity_contract_nonce, - recipient.try_into().expect("identity into failed"), + recipient.into(), PlatformVersion::latest(), None, None, @@ -258,9 +258,9 @@ impl ExtendedDocumentWasm { user_fee_increase: Default::default(), signature_public_key_id: Default::default(), signature: Default::default(), - }.try_into().expect("Failed to convert into DocumentsBatchTransition"); + }.into(); - documents_batch_transition.try_into().expect("Failed to convert into DocumentsBatchTransitionWasm") + documents_batch_transition.into() } #[wasm_bindgen(js_name=setUpdatedAt)] From 9925a685cdf8be3d64db9d3033393be9104af8c5 Mon Sep 17 00:00:00 2001 From: pshenmic Date: Wed, 25 Dec 2024 00:48:47 +0700 Subject: [PATCH 07/15] chore(js-sdk): cleanup --- .../src/SDK/Client/Platform/methods/documents/broadcast.ts | 5 ++--- .../src/SDK/Client/Platform/methods/documents/transfer.ts | 5 +++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/broadcast.ts b/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/broadcast.ts index fdcd18d51d2..e01cf3e2174 100644 --- a/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/broadcast.ts +++ b/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/broadcast.ts @@ -6,12 +6,12 @@ import { signStateTransition } from '../../signStateTransition'; /** * Broadcast document onto the platform * + * @param {Platform} this - bound instance class * @param {Object} documents * @param {ExtendedDocument[]} [documents.create] * @param {ExtendedDocument[]} [documents.replace] * @param {ExtendedDocument[]} [documents.delete] * @param identity - identity - * @param keyIndex - identity key index */ export default async function broadcast( this: Platform, @@ -21,7 +21,6 @@ export default async function broadcast( delete?: ExtendedDocument[], }, identity: any, - keyIndex : number, ): Promise { this.logger.debug('[Document#broadcast] Broadcast documents', { create: documents.create?.length || 0, @@ -54,7 +53,7 @@ export default async function broadcast( this.logger.silly('[Document#broadcast] Created documents batch transition'); - await signStateTransition(this, documentsBatchTransition, identity, keyIndex ?? 1); + await signStateTransition(this, documentsBatchTransition, identity, 1); // Broadcast state transition also wait for the result to be obtained await broadcastStateTransition(this, documentsBatchTransition); diff --git a/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/transfer.ts b/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/transfer.ts index 43ccb1b39a7..31fa427d113 100644 --- a/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/transfer.ts +++ b/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/transfer.ts @@ -17,14 +17,15 @@ export async function transfer( receiver: Identity, sender: Identity, ): Promise { - this.logger.debug('[Document#transfer] Transfer document'); + this.logger.debug(`[Document#transfer] Transfer document ${document.getId().toString()} + from ${sender.getId().toString} to {${receiver.getId().toString()}`); await this.initialize(); const identityContractNonce = await this.nonceManager .bumpIdentityContractNonce(sender.getId(), document.getDataContractId()); const documentsBatchTransition = document - .createTransferTransition(receiver.getId(), BigInt(identityContractNonce)); + .createTransferStateTransition(receiver.getId(), BigInt(identityContractNonce)); await signStateTransition(this, documentsBatchTransition, sender, 1); From e8defe89aebe2271824af6ca1d02c9cbbb9dd791 Mon Sep 17 00:00:00 2001 From: pshenmic Date: Wed, 25 Dec 2024 00:49:10 +0700 Subject: [PATCH 08/15] chore(wasm-dpp): cleanup --- .../src/document/document_factory/v0/mod.rs | 107 +------- .../specialized_document_factory/v0/mod.rs | 17 +- packages/wasm-dpp/src/document/factory.rs | 8 - .../document_transfer_transition.rs | 247 ------------------ 4 files changed, 4 insertions(+), 375 deletions(-) diff --git a/packages/rs-dpp/src/document/document_factory/v0/mod.rs b/packages/rs-dpp/src/document/document_factory/v0/mod.rs index 0280286f346..6dc531398c7 100644 --- a/packages/rs-dpp/src/document/document_factory/v0/mod.rs +++ b/packages/rs-dpp/src/document/document_factory/v0/mod.rs @@ -263,25 +263,9 @@ impl DocumentFactoryV0 { nonce_counter, platform_version, ), - DocumentTransitionActionType::Transfer => Self::document_transfer_transitions( - documents - .into_iter() - .map(|(document, document_type, _)| (document, document_type)) - .collect(), - nonce_counter, - platform_version, - ), - DocumentTransitionActionType::Purchase => { - Err(ProtocolError::InvalidStateTransitionType( - "action type not accounted for Transfer".to_string(), - )) - } - DocumentTransitionActionType::UpdatePrice => Err(ProtocolError::InvalidStateTransitionType( - "action type not accounted for UpdatePrice".to_string(), + _ => Err(ProtocolError::InvalidStateTransitionType( + "action type not accounted for".to_string(), )), - DocumentTransitionActionType::IgnoreWhileBumpingRevision => Err(ProtocolError::InvalidStateTransitionType( - "action type not accounted for IgnoreWhileBumpingRevision".to_string(), - )) }) .collect::, ProtocolError>>()? .into_iter() @@ -524,93 +508,6 @@ impl DocumentFactoryV0 { // Ok(raw_transitions) } - #[cfg(feature = "state-transitions")] - fn document_transfer_transitions( - documents: Vec<(Document, DocumentTypeRef)>, - nonce_counter: &mut BTreeMap<(Identifier, Identifier), u64>, //IdentityID/ContractID -> nonce - platform_version: &PlatformVersion, - ) -> Result, ProtocolError> { - documents - .into_iter() - .map(|(mut document, document_type)| { - if !document_type.documents_mutable() { - return Err(DocumentError::TryingToReplaceImmutableDocument { - document: Box::new(document), - } - .into()); - } - if document.revision().is_none() { - return Err(DocumentError::RevisionAbsentError { - document: Box::new(document), - } - .into()); - }; - - document.increment_revision()?; - document.set_updated_at(Some(Utc::now().timestamp_millis() as TimestampMillis)); - - let recipient_owner_id = document.owner_id(); - - let nonce = nonce_counter - .entry((document.owner_id(), document_type.data_contract_id())) - .or_default(); - - let transition = DocumentTransferTransition::from_document( - document, - document_type, - *nonce, - recipient_owner_id, - platform_version, - None, - None, - )?; - - *nonce += 1; - - Ok(transition.into()) - }) - .collect() - // let mut raw_transitions = vec![]; - // for (document, document_type) in documents { - // if !document_type.documents_mutable() { - // return Err(DocumentError::TryingToReplaceImmutableDocument { - // document: Box::new(document), - // } - // .into()); - // } - // let Some(document_revision) = document.revision() else { - // return Err(DocumentError::RevisionAbsentError { - // document: Box::new(document), - // }.into()); - // }; - // let mut map = document.to_map_value()?; - // - // map.retain(|key, _| { - // !key.starts_with('$') || DOCUMENT_REPLACE_KEYS_TO_STAY.contains(&key.as_str()) - // }); - // map.insert( - // PROPERTY_ACTION.to_string(), - // Value::U8(DocumentTransitionActionType::Replace as u8), - // ); - // let new_revision = document_revision + 1; - // map.insert(PROPERTY_REVISION.to_string(), Value::U64(new_revision)); - // - // // If document have an originally set `updatedAt` - // // we should update it then - // let contains_updated_at = document_type - // .required_fields() - // .contains(PROPERTY_UPDATED_AT); - // - // if contains_updated_at { - // let now = Utc::now().timestamp_millis() as TimestampMillis; - // map.insert(PROPERTY_UPDATED_AT.to_string(), Value::U64(now)); - // } - // - // raw_transitions.push(map.into()); - // } - // Ok(raw_transitions) - } - #[cfg(feature = "state-transitions")] fn document_delete_transitions( documents: Vec<(Document, DocumentTypeRef)>, diff --git a/packages/rs-dpp/src/document/specialized_document_factory/v0/mod.rs b/packages/rs-dpp/src/document/specialized_document_factory/v0/mod.rs index 56193af6ad8..e482d3822a7 100644 --- a/packages/rs-dpp/src/document/specialized_document_factory/v0/mod.rs +++ b/packages/rs-dpp/src/document/specialized_document_factory/v0/mod.rs @@ -270,22 +270,9 @@ impl SpecializedDocumentFactoryV0 { nonce_counter, platform_version, ), - DocumentTransitionActionType::Transfer => { - Err(ProtocolError::InvalidStateTransitionType( - "action type not accounted for Transfer".to_string(), - )) - }, - DocumentTransitionActionType::Purchase => { - Err(ProtocolError::InvalidStateTransitionType( - "action type not accounted for Purchase".to_string(), - )) - } - DocumentTransitionActionType::UpdatePrice => Err(ProtocolError::InvalidStateTransitionType( - "action type not accounted for UpdatePrice".to_string(), + _ => Err(ProtocolError::InvalidStateTransitionType( + "action type not accounted for".to_string(), )), - DocumentTransitionActionType::IgnoreWhileBumpingRevision => Err(ProtocolError::InvalidStateTransitionType( - "action type not accounted for IgnoreWhileBumpingRevision".to_string(), - )) }) .collect::, ProtocolError>>()? .into_iter() diff --git a/packages/wasm-dpp/src/document/factory.rs b/packages/wasm-dpp/src/document/factory.rs index 038fb8db998..0b927a60803 100644 --- a/packages/wasm-dpp/src/document/factory.rs +++ b/packages/wasm-dpp/src/document/factory.rs @@ -32,7 +32,6 @@ pub struct DocumentTransitions { create: Vec, replace: Vec, delete: Vec, - transfer: Vec, } #[wasm_bindgen(js_class=DocumentTransitions)] @@ -56,11 +55,6 @@ impl DocumentTransitions { pub fn add_transition_delete(&mut self, transition: ExtendedDocumentWasm) { self.delete.push(transition) } - - #[wasm_bindgen(js_name = "addTransitionTransfer")] - pub fn add_transition_transfer(&mut self, transition: ExtendedDocumentWasm) { - self.transfer.push(transition) - } } #[wasm_bindgen(js_name = DocumentFactory)] @@ -284,12 +278,10 @@ fn extract_documents_by_action( let documents_create = extract_documents_of_action(documents, "create").with_js_error()?; let documents_replace = extract_documents_of_action(documents, "replace").with_js_error()?; let documents_delete = extract_documents_of_action(documents, "delete").with_js_error()?; - let documents_transfer = extract_documents_of_action(documents, "transfer").with_js_error()?; documents_by_action.insert(DocumentTransitionActionType::Create, documents_create); documents_by_action.insert(DocumentTransitionActionType::Replace, documents_replace); documents_by_action.insert(DocumentTransitionActionType::Delete, documents_delete); - documents_by_action.insert(DocumentTransitionActionType::Transfer, documents_transfer); Ok(documents_by_action) } diff --git a/packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/document_transfer_transition.rs b/packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/document_transfer_transition.rs index 0ebbf8e508c..c9c745d4098 100644 --- a/packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/document_transfer_transition.rs +++ b/packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/document_transfer_transition.rs @@ -21,254 +21,7 @@ impl From for DocumentTransferTransition { #[wasm_bindgen(js_class=DocumentTransferTransition)] impl DocumentTransferTransitionWasm { - // #[wasm_bindgen(constructor)] - // pub fn from_object( - // raw_object: JsValue, - // data_contract: &DataContractWasm, - // ) -> Result { - // let mut value = raw_object.with_serde_to_platform_value_map()?; - // let document_type_name = value - // .get_string(dpp::document::extended_document::property_names::DOCUMENT_TYPE_NAME) - // .map_err(ProtocolError::ValueError) - // .with_js_error()?; - // - // let document_type = data_contract - // .inner() - // .document_type_for_name(&document_type_name) - // .with_js_error()?; - // let identifier_paths = document_type.identifier_paths(); - // let binary_paths = document_type.binary_paths(); - // - // value - // .replace_at_paths(identifier_paths, ReplacementType::Identifier) - // .map_err(ProtocolError::ValueError) - // .with_js_error()?; - // - // value - // .replace_at_paths(binary_paths, ReplacementType::BinaryBytes) - // .map_err(ProtocolError::ValueError) - // .with_js_error()?; - // let transition = - // DocumentTransferTransition::from_value_map(value, data_contract).with_js_error()?; - // - // Ok(transition.into()) - // } - // - // #[wasm_bindgen(js_name=getAction)] - // pub fn action(&self) -> u8 { - // DocumentTransitionActionType::Transfer as u8; - // } - // - // #[wasm_bindgen(js_name=getRevision)] - // pub fn revision(&self) -> Revision { - // self.inner.revision() - // } - // - // #[wasm_bindgen(js_name=getUpdatedAt)] - // pub fn updated_at(&self) -> Option { - // self.inner - // .updated_at() - // .map(|timestamp| js_sys::Date::new(&JsValue::from_f64(timestamp as f64))) - // } - // - // #[wasm_bindgen(js_name=toObject)] - // pub fn to_object( - // &self, - // options: &JsValue, - // data_contract: DataContractWasm, - // ) -> Result { - // let document_type = data_contract - // .inner() - // .document_type_for_name(self.inner.base().document_type_name()) - // .with_js_error()?; - // let identifier_paths = document_type.identifier_paths(); - // let binary_paths = document_type.binary_paths(); - // - // to_object( - // self.inner.to_object().with_js_error()?, - // options, - // identifier_paths - // .into_iter() - // .chain(document_transfer_transition::v0::IDENTIFIER_FIELDS), - // binary_paths, - // ) - // } - // - // #[wasm_bindgen(js_name=toJSON)] - // pub fn to_json(&self) -> Result { - // let value = self.inner.to_json().with_js_error()?; - // let serializer = serde_wasm_bindgen::Serializer::json_compatible(); - // let js_value = value.serialize(&serializer)?; - // Ok(js_value) - // } - // - // // AbstractDataDocumentTransition - // #[wasm_bindgen(js_name=getData)] - // pub fn get_data(&self, data_contract: DataContractWasm) -> Result { - // let data = if let Some(ref data) = self.inner.data() { - // data - // } else { - // return Ok(JsValue::undefined()); - // }; - // - // let js_value = data.serialize(&serde_wasm_bindgen::Serializer::json_compatible())?; - // let document_type = data_contract - // .inner() - // .document_type_for_name(self.inner.base().document_type_name()) - // .with_js_error()?; - // let identifier_paths = document_type.identifier_paths(); - // let binary_paths = document_type.binary_paths(); - // - // for path in identifier_paths { - // let bytes = data - // .get_identifier_bytes_at_path(path) - // .map_err(ProtocolError::ValueError) - // .with_js_error()?; - // let id = >::from( - // Identifier::from_bytes(&bytes).unwrap(), - // ); - // lodash_set(&js_value, path, id.into()); - // } - // for path in binary_paths { - // let bytes = data - // .get_binary_bytes_at_path(path) - // .map_err(ProtocolError::ValueError) - // .with_js_error()?; - // let buffer = Buffer::from_bytes(&bytes); - // lodash_set(&js_value, path, buffer.into()); - // } - // - // Ok(js_value) - // } - // - // // AbstractDocumentTransition - // #[wasm_bindgen(js_name=getId)] - // pub fn id(&self) -> IdentifierWrapper { - // self.inner.base().id().into() - // } - // - // #[wasm_bindgen(js_name=getType)] - // pub fn document_type(&self) -> String { - // self.inner.base().document_type_name().clone() - // } - // - // #[wasm_bindgen(js_name=getDataContractId)] - // pub fn data_contract_id(&self) -> IdentifierWrapper { - // self.inner.base().data_contract_id().into() - // } - // - // #[wasm_bindgen(js_name=get)] - // pub fn get(&self, path: String) -> Result { - // let document_data = if let Some(ref data) = self.inner.data() { - // data - // } else { - // return Ok(JsValue::undefined()); - // }; - // - // let value = if let Ok(value) = document_data.get_at_path(&path) { - // value.to_owned() - // } else { - // return Ok(JsValue::undefined()); - // }; - // - // match self.get_binary_type_of_path(&path) { - // BinaryType::Buffer => { - // let bytes: Vec = serde_json::from_value( - // value - // .try_into() - // .map_err(ProtocolError::ValueError) - // .with_js_error()?, - // ) - // .unwrap(); - // let buffer = Buffer::from_bytes(&bytes); - // return Ok(buffer.into()); - // } - // BinaryType::Identifier => { - // let bytes: Vec = serde_json::from_value( - // value - // .try_into() - // .map_err(ProtocolError::ValueError) - // .with_js_error()?, - // ) - // .unwrap(); - // let id = >::from( - // Identifier::from_bytes(&bytes).unwrap(), - // ); - // return Ok(id.into()); - // } - // BinaryType::None => { - // // Do nothing. If is 'None' it means that binary may contain binary data - // // or may not captain it at all - // } - // } - // - // let json_value: JsonValue = value - // .clone() - // .try_into() - // .map_err(ProtocolError::ValueError) - // .with_js_error()?; - // let map = value - // .to_btree_ref_string_map() - // .map_err(ProtocolError::ValueError) - // .with_js_error()?; - // let js_value = json_value.serialize(&serde_wasm_bindgen::Serializer::json_compatible())?; - // let (identifier_paths, binary_paths) = self - // .inner - // .base() - // .data_contract() - // .get_identifiers_and_binary_paths(&self.inner.base().document_type_name()) - // .with_js_error()?; - // - // for property_path in identifier_paths { - // if property_path.starts_with(&path) { - // let (_, suffix) = property_path.split_at(path.len() + 1); - // - // if let Some(bytes) = map - // .get_optional_bytes_at_path(suffix) - // .map_err(ProtocolError::ValueError) - // .with_js_error()? - // { - // let id = >::from( - // Identifier::from_bytes(&bytes).unwrap(), - // ); - // lodash_set(&js_value, suffix, id.into()); - // } - // } - // } - // - // for property_path in binary_paths { - // if property_path.starts_with(&path) { - // let (_, suffix) = property_path.split_at(path.len() + 1); - // - // if let Some(bytes) = map - // .get_optional_bytes_at_path(suffix) - // .map_err(ProtocolError::ValueError) - // .with_js_error()? - // { - // let buffer = Buffer::from_bytes(&bytes); - // lodash_set(&js_value, suffix, buffer.into()); - // } - // } - // } - // - // Ok(js_value) - // } } impl DocumentTransferTransitionWasm { - // fn get_binary_type_of_path(&self, path: &String) -> BinaryType { - // let maybe_binary_properties = self - // .inner - // .get_binary_properties(&self.inner.base().document_type_name()); - // - // if let Ok(binary_properties) = maybe_binary_properties { - // if let Some(data) = binary_properties.get(path) { - // if data.is_type_of_identifier() { - // return BinaryType::Identifier; - // } - // return BinaryType::Buffer; - // } - // } - // BinaryType::None - // } } From 1f0f7c697497264d231a35ba947e1ebe378e5913 Mon Sep 17 00:00:00 2001 From: pshenmic Date: Wed, 25 Dec 2024 03:30:02 +0700 Subject: [PATCH 09/15] chore(js-sdk): cleanup --- .../src/SDK/Client/Platform/methods/documents/transfer.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/transfer.ts b/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/transfer.ts index 31fa427d113..6f8f847d096 100644 --- a/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/transfer.ts +++ b/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/transfer.ts @@ -17,8 +17,7 @@ export async function transfer( receiver: Identity, sender: Identity, ): Promise { - this.logger.debug(`[Document#transfer] Transfer document ${document.getId().toString()} - from ${sender.getId().toString} to {${receiver.getId().toString()}`); + this.logger.debug(`[Document#transfer] Transfer document ${document.getId().toString()} from ${sender.getId().toString} to {${receiver.getId().toString()}`); await this.initialize(); const identityContractNonce = await this.nonceManager From 43cc7f1d3d94d1b096a64e27ee488478a7a38dfa Mon Sep 17 00:00:00 2001 From: pshenmic Date: Wed, 25 Dec 2024 03:33:10 +0700 Subject: [PATCH 10/15] chore(js-sdk): revert --- .../src/SDK/Client/Platform/methods/documents/broadcast.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/broadcast.ts b/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/broadcast.ts index e01cf3e2174..eeab6b36fad 100644 --- a/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/broadcast.ts +++ b/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/broadcast.ts @@ -25,7 +25,7 @@ export default async function broadcast( this.logger.debug('[Document#broadcast] Broadcast documents', { create: documents.create?.length || 0, replace: documents.replace?.length || 0, - delete: documents.delete?.length || 0, + delete: documents.delete?.length || 0 }); await this.initialize(); From cf99fe6ca9e7e7bae5bd15912ec5660d53b5841b Mon Sep 17 00:00:00 2001 From: pshenmic Date: Wed, 25 Dec 2024 03:33:51 +0700 Subject: [PATCH 11/15] chore(js-sdk): cleanup --- .../src/SDK/Client/Platform/methods/documents/broadcast.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/broadcast.ts b/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/broadcast.ts index eeab6b36fad..7b6bc0fc1c5 100644 --- a/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/broadcast.ts +++ b/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/broadcast.ts @@ -25,7 +25,7 @@ export default async function broadcast( this.logger.debug('[Document#broadcast] Broadcast documents', { create: documents.create?.length || 0, replace: documents.replace?.length || 0, - delete: documents.delete?.length || 0 + delete: documents.delete?.length || 0, }); await this.initialize(); @@ -35,7 +35,7 @@ export default async function broadcast( const dataContractId = [ ...(documents.create || []), ...(documents.replace || []), - ...(documents.delete || []), + ...(documents.delete || []) ][0]?.getDataContractId(); if (!dataContractId) { From 44df2e63ff64a7194f6bfaba07a6098accc0e77d Mon Sep 17 00:00:00 2001 From: pshenmic Date: Wed, 25 Dec 2024 03:34:14 +0700 Subject: [PATCH 12/15] chore(js-sdk): cleanup --- .../src/SDK/Client/Platform/methods/documents/broadcast.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/broadcast.ts b/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/broadcast.ts index 7b6bc0fc1c5..eeab6b36fad 100644 --- a/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/broadcast.ts +++ b/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/broadcast.ts @@ -25,7 +25,7 @@ export default async function broadcast( this.logger.debug('[Document#broadcast] Broadcast documents', { create: documents.create?.length || 0, replace: documents.replace?.length || 0, - delete: documents.delete?.length || 0, + delete: documents.delete?.length || 0 }); await this.initialize(); @@ -35,7 +35,7 @@ export default async function broadcast( const dataContractId = [ ...(documents.create || []), ...(documents.replace || []), - ...(documents.delete || []) + ...(documents.delete || []), ][0]?.getDataContractId(); if (!dataContractId) { From 5894a7f8fd389edc7eae5159fb84fb900579091a Mon Sep 17 00:00:00 2001 From: pshenmic Date: Wed, 25 Dec 2024 03:35:06 +0700 Subject: [PATCH 13/15] chore(js-sdk): cleanup --- .../src/SDK/Client/Platform/methods/documents/broadcast.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/broadcast.ts b/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/broadcast.ts index eeab6b36fad..4bbe6b6479d 100644 --- a/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/broadcast.ts +++ b/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/broadcast.ts @@ -18,14 +18,14 @@ export default async function broadcast( documents: { create?: ExtendedDocument[], replace?: ExtendedDocument[], - delete?: ExtendedDocument[], + delete?: ExtendedDocument[] }, identity: any, ): Promise { this.logger.debug('[Document#broadcast] Broadcast documents', { create: documents.create?.length || 0, replace: documents.replace?.length || 0, - delete: documents.delete?.length || 0 + delete: documents.delete?.length || 0, }); await this.initialize(); From d7d5d9012afce9c3db683b9c42aea076b22ad916 Mon Sep 17 00:00:00 2001 From: pshenmic Date: Wed, 25 Dec 2024 03:37:10 +0700 Subject: [PATCH 14/15] chore(js-sdk): update doc --- .../src/SDK/Client/Platform/methods/documents/transfer.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/transfer.ts b/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/transfer.ts index 6f8f847d096..e211364b27b 100644 --- a/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/transfer.ts +++ b/packages/js-dash-sdk/src/SDK/Client/Platform/methods/documents/transfer.ts @@ -6,10 +6,9 @@ import { signStateTransition } from '../../signStateTransition'; * Transfer document in the platform * * @param {Platform} this - bound instance class - * @param {string} typeLocator - type locator - * @param identity - identity - * @param {Object} [data] - options - * @returns {StateTransition} + * @param {ExtendedDocument} document - document from the DAPI + * @param {Identifier} receiver - identifier of the document recipient ownership + * @param {Identifier} sender - identifier of the document owner */ export async function transfer( this: Platform, From 2131f00d9b8b7cdba7a24cf4cffc8070aaede56c Mon Sep 17 00:00:00 2001 From: pshenmic Date: Wed, 25 Dec 2024 03:38:29 +0700 Subject: [PATCH 15/15] chore(wasm-dpp): cleanup --- packages/rs-dpp/src/document/document_factory/v0/mod.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/rs-dpp/src/document/document_factory/v0/mod.rs b/packages/rs-dpp/src/document/document_factory/v0/mod.rs index 6dc531398c7..58fc96de91a 100644 --- a/packages/rs-dpp/src/document/document_factory/v0/mod.rs +++ b/packages/rs-dpp/src/document/document_factory/v0/mod.rs @@ -32,7 +32,6 @@ use crate::state_transition::documents_batch_transition::{ DocumentsBatchTransition, DocumentsBatchTransitionV0, }; use itertools::Itertools; -use crate::state_transition::documents_batch_transition::document_transition::DocumentTransferTransition; const PROPERTY_FEATURE_VERSION: &str = "$version"; const PROPERTY_ENTROPY: &str = "$entropy";