From e824b74e086931dbaccc31f98a1d25f8b3fd0f03 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Wed, 4 Jun 2025 02:47:24 +0200 Subject: [PATCH 1/3] feat: add document transitions for create, delete, purchase, replace, set price, and transfer --- packages/rs-sdk/src/platform.rs | 6 +- .../{ => documents}/document_query.rs | 2 +- packages/rs-sdk/src/platform/documents/mod.rs | 2 + .../platform/documents/transitions/create.rs | 219 ++++++++++++++ .../platform/documents/transitions/delete.rs | 262 +++++++++++++++++ .../src/platform/documents/transitions/mod.rs | 13 + .../documents/transitions/purchase.rs | 273 ++++++++++++++++++ .../platform/documents/transitions/replace.rs | 215 ++++++++++++++ .../documents/transitions/set_price.rs | 263 +++++++++++++++++ .../documents/transitions/transfer.rs | 263 +++++++++++++++++ packages/rs-sdk/src/platform/fetch_many.rs | 11 +- packages/rs-sdk/src/platform/query.rs | 3 +- 12 files changed, 1519 insertions(+), 13 deletions(-) rename packages/rs-sdk/src/platform/{ => documents}/document_query.rs (99%) create mode 100644 packages/rs-sdk/src/platform/documents/mod.rs create mode 100644 packages/rs-sdk/src/platform/documents/transitions/create.rs create mode 100644 packages/rs-sdk/src/platform/documents/transitions/delete.rs create mode 100644 packages/rs-sdk/src/platform/documents/transitions/mod.rs create mode 100644 packages/rs-sdk/src/platform/documents/transitions/purchase.rs create mode 100644 packages/rs-sdk/src/platform/documents/transitions/replace.rs create mode 100644 packages/rs-sdk/src/platform/documents/transitions/set_price.rs create mode 100644 packages/rs-sdk/src/platform/documents/transitions/transfer.rs diff --git a/packages/rs-sdk/src/platform.rs b/packages/rs-sdk/src/platform.rs index a5141f22060..233242abd09 100644 --- a/packages/rs-sdk/src/platform.rs +++ b/packages/rs-sdk/src/platform.rs @@ -7,7 +7,6 @@ pub mod block_info_from_metadata; mod delegate; -mod document_query; mod fetch; pub mod fetch_current_no_parameters; mod fetch_many; @@ -17,10 +16,12 @@ pub mod query; pub mod transition; pub mod types; +pub mod documents; pub mod group_actions; pub mod tokens; -pub use dapi_grpc::platform::v0::{self as proto}; +pub use dapi_grpc::platform::v0 as proto; +pub use documents::document_query::DocumentQuery; pub use dpp::{ self as dpp, document::Document, @@ -32,7 +33,6 @@ pub use drive_proof_verifier::ContextProvider; pub use drive_proof_verifier::MockContextProvider; pub use rs_dapi_client as dapi; pub use { - document_query::DocumentQuery, fetch::Fetch, fetch_many::FetchMany, fetch_unproved::FetchUnproved, diff --git a/packages/rs-sdk/src/platform/document_query.rs b/packages/rs-sdk/src/platform/documents/document_query.rs similarity index 99% rename from packages/rs-sdk/src/platform/document_query.rs rename to packages/rs-sdk/src/platform/documents/document_query.rs index 39ec37d736a..491d0cdf3b6 100644 --- a/packages/rs-sdk/src/platform/document_query.rs +++ b/packages/rs-sdk/src/platform/documents/document_query.rs @@ -27,7 +27,7 @@ use rs_dapi_client::transport::{ AppliedRequestSettings, BoxFuture, TransportError, TransportRequest, }; -use super::fetch::Fetch; +use crate::platform::Fetch; // TODO: remove DocumentQuery once ContextProvider that provides data contracts is merged. diff --git a/packages/rs-sdk/src/platform/documents/mod.rs b/packages/rs-sdk/src/platform/documents/mod.rs new file mode 100644 index 00000000000..94f890f14a0 --- /dev/null +++ b/packages/rs-sdk/src/platform/documents/mod.rs @@ -0,0 +1,2 @@ +pub mod document_query; +pub mod transitions; diff --git a/packages/rs-sdk/src/platform/documents/transitions/create.rs b/packages/rs-sdk/src/platform/documents/transitions/create.rs new file mode 100644 index 00000000000..2017a4b13eb --- /dev/null +++ b/packages/rs-sdk/src/platform/documents/transitions/create.rs @@ -0,0 +1,219 @@ +use crate::platform::transition::broadcast::BroadcastStateTransition; +use crate::platform::transition::put_settings::PutSettings; +use crate::{Error, Sdk}; +use dpp::data_contract::accessors::v0::DataContractV0Getters; +use dpp::data_contract::document_type::DocumentType; +use dpp::data_contract::DataContract; +use dpp::document::{Document, DocumentV0Getters}; +use dpp::identity::signer::Signer; +use dpp::identity::IdentityPublicKey; +use dpp::prelude::UserFeeIncrease; +use dpp::state_transition::batch_transition::methods::v0::DocumentsBatchTransitionMethodsV0; +use dpp::state_transition::batch_transition::methods::StateTransitionCreationOptions; +use dpp::state_transition::batch_transition::BatchTransition; +use dpp::state_transition::proof_result::StateTransitionProofResult; +use dpp::state_transition::StateTransition; +use dpp::tokens::token_payment_info::TokenPaymentInfo; +use dpp::version::PlatformVersion; + +/// A builder to configure and broadcast document create transitions +pub struct DocumentCreateTransitionBuilder<'a> { + data_contract: &'a DataContract, + document_type: DocumentType, + document: Document, + document_state_transition_entropy: [u8; 32], + token_payment_info: Option, + settings: Option, + user_fee_increase: Option, +} + +impl<'a> DocumentCreateTransitionBuilder<'a> { + /// Start building a create document request for the provided DataContract. + /// + /// # Arguments + /// + /// * `data_contract` - A reference to the data contract + /// * `document_type` - The document type to create + /// * `document` - The document to create + /// * `document_state_transition_entropy` - Entropy for the state transition + /// + /// # Returns + /// + /// * `Self` - The new builder instance + pub fn new( + data_contract: &'a DataContract, + document_type: DocumentType, + document: Document, + document_state_transition_entropy: [u8; 32], + ) -> Self { + Self { + data_contract, + document_type, + document, + document_state_transition_entropy, + token_payment_info: None, + settings: None, + user_fee_increase: None, + } + } + + /// Adds token payment info to the document create transition + /// + /// # Arguments + /// + /// * `token_payment_info` - The token payment info to add + /// + /// # Returns + /// + /// * `Self` - The updated builder + pub fn with_token_payment_info(mut self, token_payment_info: TokenPaymentInfo) -> Self { + self.token_payment_info = Some(token_payment_info); + self + } + + /// Adds a user fee increase to the document create transition + /// + /// # Arguments + /// + /// * `user_fee_increase` - The user fee increase to add + /// + /// # Returns + /// + /// * `Self` - The updated builder + pub fn with_user_fee_increase(mut self, user_fee_increase: UserFeeIncrease) -> Self { + self.user_fee_increase = Some(user_fee_increase); + self + } + + /// Adds settings to the document create transition + /// + /// # Arguments + /// + /// * `settings` - The settings to add + /// + /// # Returns + /// + /// * `Self` - The updated builder + pub fn with_settings(mut self, settings: PutSettings) -> Self { + self.settings = Some(settings); + self + } + + /// Signs the document create transition + /// + /// # Arguments + /// + /// * `sdk` - The SDK instance + /// * `identity_public_key` - The public key of the identity + /// * `signer` - The signer instance + /// * `platform_version` - The platform version + /// * `options` - Optional state transition creation options + /// + /// # Returns + /// + /// * `Result` - The signed state transition or an error + pub async fn sign( + &self, + sdk: &Sdk, + identity_public_key: &IdentityPublicKey, + signer: &impl Signer, + platform_version: &PlatformVersion, + options: Option, + ) -> Result { + let identity_contract_nonce = sdk + .get_identity_contract_nonce( + self.document.owner_id(), + self.data_contract.id(), + true, + self.settings, + ) + .await?; + + let state_transition = BatchTransition::new_document_creation_transition_from_document( + self.document.clone(), + self.document_type.as_ref(), + self.document_state_transition_entropy, + identity_public_key, + identity_contract_nonce, + self.user_fee_increase.unwrap_or_default(), + self.token_payment_info, + signer, + platform_version, + options, + )?; + + Ok(state_transition) + } +} + +/// Result types returned from document creation operations. +#[derive(Debug)] +pub enum DocumentCreateResult { + /// Document creation result containing the created document. + Document(Document), +} + +impl Sdk { + /// Creates a new document on the platform. + /// + /// This method broadcasts a document creation transition to add a new document + /// to the specified data contract. The result contains the created document. + /// + /// # Arguments + /// + /// * `create_document_transition_builder` - Builder containing document creation parameters + /// * `signing_key` - The identity public key for signing the transition + /// * `signer` - Implementation of the Signer trait for cryptographic signing + /// + /// # Returns + /// + /// Returns a `Result` containing a `DocumentCreateResult` on success, or an `Error` on failure. + /// + /// # Errors + /// + /// This function will return an error if: + /// - The transition signing fails + /// - Broadcasting the transition fails + /// - The proof verification returns an unexpected result type + /// - Document validation fails + pub async fn document_create( + &self, + create_document_transition_builder: DocumentCreateTransitionBuilder<'_>, + signing_key: &IdentityPublicKey, + signer: &S, + ) -> Result { + let platform_version = self.version(); + + let state_transition = create_document_transition_builder + .sign(self, signing_key, signer, platform_version, None) + .await?; + + let proof_result = state_transition + .broadcast_and_wait::(self, None) + .await?; + + match proof_result { + StateTransitionProofResult::VerifiedDocuments(documents) => { + if let Some((_, Some(document))) = documents.into_iter().next() { + Ok(DocumentCreateResult::Document(document)) + } else { + Err(Error::DriveProofError( + drive::error::proof::ProofError::UnexpectedResultProof( + "Expected document in VerifiedDocuments result for create transition" + .to_string(), + ), + vec![], + Default::default(), + )) + } + } + _ => Err(Error::DriveProofError( + drive::error::proof::ProofError::UnexpectedResultProof( + "Expected VerifiedDocuments for document create transition".to_string(), + ), + vec![], + Default::default(), + )), + } + } +} diff --git a/packages/rs-sdk/src/platform/documents/transitions/delete.rs b/packages/rs-sdk/src/platform/documents/transitions/delete.rs new file mode 100644 index 00000000000..61193959748 --- /dev/null +++ b/packages/rs-sdk/src/platform/documents/transitions/delete.rs @@ -0,0 +1,262 @@ +use crate::platform::transition::broadcast::BroadcastStateTransition; +use crate::platform::transition::put_settings::PutSettings; +use crate::platform::Identifier; +use crate::{Error, Sdk}; +use dpp::data_contract::accessors::v0::DataContractV0Getters; +use dpp::data_contract::document_type::DocumentType; +use dpp::data_contract::DataContract; +use dpp::document::Document; +use dpp::identity::signer::Signer; +use dpp::identity::IdentityPublicKey; +use dpp::prelude::UserFeeIncrease; +use dpp::state_transition::batch_transition::methods::v0::DocumentsBatchTransitionMethodsV0; +use dpp::state_transition::batch_transition::methods::StateTransitionCreationOptions; +use dpp::state_transition::batch_transition::BatchTransition; +use dpp::state_transition::proof_result::StateTransitionProofResult; +use dpp::state_transition::StateTransition; +use dpp::tokens::token_payment_info::TokenPaymentInfo; +use dpp::version::PlatformVersion; + +/// A builder to configure and broadcast document delete transitions +pub struct DocumentDeleteTransitionBuilder<'a> { + data_contract: &'a DataContract, + document_type: DocumentType, + document_id: Identifier, + owner_id: Identifier, + token_payment_info: Option, + settings: Option, + user_fee_increase: Option, +} + +impl<'a> DocumentDeleteTransitionBuilder<'a> { + /// Start building a delete document request for the provided DataContract. + /// + /// # Arguments + /// + /// * `data_contract` - A reference to the data contract + /// * `document_type` - The document type to delete + /// * `document_id` - The ID of the document to delete + /// * `owner_id` - The owner ID of the document + /// + /// # Returns + /// + /// * `Self` - The new builder instance + pub fn new( + data_contract: &'a DataContract, + document_type: DocumentType, + document_id: Identifier, + owner_id: Identifier, + ) -> Self { + Self { + data_contract, + document_type, + document_id, + owner_id, + token_payment_info: None, + settings: None, + user_fee_increase: None, + } + } + + /// Creates a new builder from an existing document + /// + /// # Arguments + /// + /// * `data_contract` - A reference to the data contract + /// * `document_type` - The document type to delete + /// * `document` - The document to delete + /// + /// # Returns + /// + /// * `Self` - The new builder instance + pub fn from_document( + data_contract: &'a DataContract, + document_type: DocumentType, + document: &Document, + ) -> Self { + use dpp::document::DocumentV0Getters; + Self::new( + data_contract, + document_type, + document.id(), + document.owner_id(), + ) + } + + /// Adds token payment info to the document delete transition + /// + /// # Arguments + /// + /// * `token_payment_info` - The token payment info to add + /// + /// # Returns + /// + /// * `Self` - The updated builder + pub fn with_token_payment_info(mut self, token_payment_info: TokenPaymentInfo) -> Self { + self.token_payment_info = Some(token_payment_info); + self + } + + /// Adds a user fee increase to the document delete transition + /// + /// # Arguments + /// + /// * `user_fee_increase` - The user fee increase to add + /// + /// # Returns + /// + /// * `Self` - The updated builder + pub fn with_user_fee_increase(mut self, user_fee_increase: UserFeeIncrease) -> Self { + self.user_fee_increase = Some(user_fee_increase); + self + } + + /// Adds settings to the document delete transition + /// + /// # Arguments + /// + /// * `settings` - The settings to add + /// + /// # Returns + /// + /// * `Self` - The updated builder + pub fn with_settings(mut self, settings: PutSettings) -> Self { + self.settings = Some(settings); + self + } + + /// Signs the document delete transition + /// + /// # Arguments + /// + /// * `sdk` - The SDK instance + /// * `identity_public_key` - The public key of the identity + /// * `signer` - The signer instance + /// * `platform_version` - The platform version + /// * `options` - Optional state transition creation options + /// + /// # Returns + /// + /// * `Result` - The signed state transition or an error + pub async fn sign( + &self, + sdk: &Sdk, + identity_public_key: &IdentityPublicKey, + signer: &impl Signer, + platform_version: &PlatformVersion, + options: Option, + ) -> Result { + let identity_contract_nonce = sdk + .get_identity_contract_nonce( + self.owner_id, + self.data_contract.id(), + true, + self.settings, + ) + .await?; + + // Create a minimal document for deletion + let document = Document::V0(dpp::document::DocumentV0 { + id: self.document_id, + owner_id: self.owner_id, + properties: Default::default(), + revision: Some(1), // Will be updated during transition + created_at: None, + updated_at: None, + transferred_at: None, + created_at_block_height: None, + updated_at_block_height: None, + transferred_at_block_height: None, + created_at_core_block_height: None, + updated_at_core_block_height: None, + transferred_at_core_block_height: None, + }); + + let state_transition = BatchTransition::new_document_deletion_transition_from_document( + document, + self.document_type.as_ref(), + identity_public_key, + identity_contract_nonce, + self.user_fee_increase.unwrap_or_default(), + self.token_payment_info, + signer, + platform_version, + options, + )?; + + Ok(state_transition) + } +} + +/// Result types returned from document delete operations. +#[derive(Debug)] +pub enum DocumentDeleteResult { + /// Document deletion confirmed (document no longer exists). + Deleted(Identifier), +} + +impl Sdk { + /// Deletes an existing document from the platform. + /// + /// This method broadcasts a document deletion transition to permanently remove + /// a document from the platform. The result confirms the deletion. + /// + /// # Arguments + /// + /// * `delete_document_transition_builder` - Builder containing document deletion parameters + /// * `signing_key` - The identity public key for signing the transition + /// * `signer` - Implementation of the Signer trait for cryptographic signing + /// + /// # Returns + /// + /// Returns a `Result` containing a `DocumentDeleteResult` on success, or an `Error` on failure. + /// + /// # Errors + /// + /// This function will return an error if: + /// - The transition signing fails + /// - Broadcasting the transition fails + /// - The proof verification returns an unexpected result type + /// - Document not found or already deleted + /// - Insufficient permissions to delete the document + pub async fn document_delete( + &self, + delete_document_transition_builder: DocumentDeleteTransitionBuilder<'_>, + signing_key: &IdentityPublicKey, + signer: &S, + ) -> Result { + let platform_version = self.version(); + + let state_transition = delete_document_transition_builder + .sign(self, signing_key, signer, platform_version, None) + .await?; + + let proof_result = state_transition + .broadcast_and_wait::(self, None) + .await?; + + match proof_result { + StateTransitionProofResult::VerifiedDocuments(documents) => { + if let Some((document_id, None)) = documents.into_iter().next() { + // None indicates the document has been deleted + Ok(DocumentDeleteResult::Deleted(document_id)) + } else { + Err(Error::DriveProofError( + drive::error::proof::ProofError::UnexpectedResultProof( + "Expected deleted document (None) in VerifiedDocuments result for delete transition".to_string(), + ), + vec![], + Default::default(), + )) + } + } + _ => Err(Error::DriveProofError( + drive::error::proof::ProofError::UnexpectedResultProof( + "Expected VerifiedDocuments for document delete transition".to_string(), + ), + vec![], + Default::default(), + )), + } + } +} diff --git a/packages/rs-sdk/src/platform/documents/transitions/mod.rs b/packages/rs-sdk/src/platform/documents/transitions/mod.rs new file mode 100644 index 00000000000..200a2164267 --- /dev/null +++ b/packages/rs-sdk/src/platform/documents/transitions/mod.rs @@ -0,0 +1,13 @@ +pub mod create; +pub mod delete; +pub mod purchase; +pub mod replace; +pub mod set_price; +pub mod transfer; + +pub use create::{DocumentCreateResult, DocumentCreateTransitionBuilder}; +pub use delete::{DocumentDeleteResult, DocumentDeleteTransitionBuilder}; +pub use purchase::{DocumentPurchaseResult, DocumentPurchaseTransitionBuilder}; +pub use replace::{DocumentReplaceResult, DocumentReplaceTransitionBuilder}; +pub use set_price::{DocumentSetPriceResult, DocumentSetPriceTransitionBuilder}; +pub use transfer::{DocumentTransferResult, DocumentTransferTransitionBuilder}; diff --git a/packages/rs-sdk/src/platform/documents/transitions/purchase.rs b/packages/rs-sdk/src/platform/documents/transitions/purchase.rs new file mode 100644 index 00000000000..c882f07f3d2 --- /dev/null +++ b/packages/rs-sdk/src/platform/documents/transitions/purchase.rs @@ -0,0 +1,273 @@ +use crate::platform::transition::broadcast::BroadcastStateTransition; +use crate::platform::transition::put_settings::PutSettings; +use crate::platform::Identifier; +use crate::{Error, Sdk}; +use dpp::data_contract::accessors::v0::DataContractV0Getters; +use dpp::data_contract::document_type::DocumentType; +use dpp::data_contract::DataContract; +use dpp::document::Document; +use dpp::fee::Credits; +use dpp::identity::signer::Signer; +use dpp::identity::IdentityPublicKey; +use dpp::prelude::UserFeeIncrease; +use dpp::state_transition::batch_transition::methods::v0::DocumentsBatchTransitionMethodsV0; +use dpp::state_transition::batch_transition::methods::StateTransitionCreationOptions; +use dpp::state_transition::batch_transition::BatchTransition; +use dpp::state_transition::proof_result::StateTransitionProofResult; +use dpp::state_transition::StateTransition; +use dpp::tokens::token_payment_info::TokenPaymentInfo; +use dpp::version::PlatformVersion; + +/// A builder to configure and broadcast document purchase transitions +pub struct DocumentPurchaseTransitionBuilder<'a> { + data_contract: &'a DataContract, + document_type: DocumentType, + document: Document, + purchaser_id: Identifier, + price: Credits, + token_payment_info: Option, + settings: Option, + user_fee_increase: Option, +} + +impl<'a> DocumentPurchaseTransitionBuilder<'a> { + /// Start building a purchase document request for the provided DataContract. + /// + /// # Arguments + /// + /// * `data_contract` - A reference to the data contract + /// * `document_type` - The document type + /// * `document` - The document to purchase + /// * `purchaser_id` - The identifier of the purchaser + /// * `price` - The price to pay for the document + /// + /// # Returns + /// + /// * `Self` - The new builder instance + pub fn new( + data_contract: &'a DataContract, + document_type: DocumentType, + document: Document, + purchaser_id: Identifier, + price: Credits, + ) -> Self { + Self { + data_contract, + document_type, + document, + purchaser_id, + price, + token_payment_info: None, + settings: None, + user_fee_increase: None, + } + } + + /// Creates a new builder from document ID + /// + /// # Arguments + /// + /// * `data_contract` - A reference to the data contract + /// * `document_type` - The document type + /// * `document_id` - The ID of the document + /// * `current_owner_id` - The current owner ID of the document + /// * `purchaser_id` - The identifier of the purchaser + /// * `price` - The price to pay for the document + /// + /// # Returns + /// + /// * `Self` - The new builder instance + pub fn from_document_info( + data_contract: &'a DataContract, + document_type: DocumentType, + document_id: Identifier, + current_owner_id: Identifier, + purchaser_id: Identifier, + price: Credits, + ) -> Self { + // Create a minimal document with just the required fields + // The actual document will be fetched during the transition + let document = Document::V0(dpp::document::DocumentV0 { + id: document_id, + owner_id: current_owner_id, + properties: Default::default(), + revision: Some(1), // Will be updated during transition + created_at: None, + updated_at: None, + transferred_at: None, + created_at_block_height: None, + updated_at_block_height: None, + transferred_at_block_height: None, + created_at_core_block_height: None, + updated_at_core_block_height: None, + transferred_at_core_block_height: None, + }); + + Self::new(data_contract, document_type, document, purchaser_id, price) + } + + /// Adds token payment info to the document purchase transition + /// + /// # Arguments + /// + /// * `token_payment_info` - The token payment info to add + /// + /// # Returns + /// + /// * `Self` - The updated builder + pub fn with_token_payment_info(mut self, token_payment_info: TokenPaymentInfo) -> Self { + self.token_payment_info = Some(token_payment_info); + self + } + + /// Adds a user fee increase to the document purchase transition + /// + /// # Arguments + /// + /// * `user_fee_increase` - The user fee increase to add + /// + /// # Returns + /// + /// * `Self` - The updated builder + pub fn with_user_fee_increase(mut self, user_fee_increase: UserFeeIncrease) -> Self { + self.user_fee_increase = Some(user_fee_increase); + self + } + + /// Adds settings to the document purchase transition + /// + /// # Arguments + /// + /// * `settings` - The settings to add + /// + /// # Returns + /// + /// * `Self` - The updated builder + pub fn with_settings(mut self, settings: PutSettings) -> Self { + self.settings = Some(settings); + self + } + + /// Signs the document purchase transition + /// + /// # Arguments + /// + /// * `sdk` - The SDK instance + /// * `identity_public_key` - The public key of the identity + /// * `signer` - The signer instance + /// * `platform_version` - The platform version + /// * `options` - Optional state transition creation options + /// + /// # Returns + /// + /// * `Result` - The signed state transition or an error + pub async fn sign( + &self, + sdk: &Sdk, + identity_public_key: &IdentityPublicKey, + signer: &impl Signer, + platform_version: &PlatformVersion, + options: Option, + ) -> Result { + let identity_contract_nonce = sdk + .get_identity_contract_nonce( + self.purchaser_id, + self.data_contract.id(), + true, + self.settings, + ) + .await?; + + let state_transition = BatchTransition::new_document_purchase_transition_from_document( + self.document.clone(), + self.document_type.as_ref(), + self.purchaser_id, + self.price, + identity_public_key, + identity_contract_nonce, + self.user_fee_increase.unwrap_or_default(), + self.token_payment_info, + signer, + platform_version, + options, + )?; + + Ok(state_transition) + } +} + +/// Result types returned from document purchase operations. +#[derive(Debug)] +pub enum DocumentPurchaseResult { + /// Document purchased successfully, containing the document with new owner. + Document(Document), +} + +impl Sdk { + /// Purchases a document from its current owner. + /// + /// This method broadcasts a document purchase transition to buy a document + /// at its set price and transfer ownership to the purchaser. The result + /// contains the purchased document with updated ownership. + /// + /// # Arguments + /// + /// * `purchase_document_transition_builder` - Builder containing purchase parameters + /// * `signing_key` - The identity public key for signing the transition + /// * `signer` - Implementation of the Signer trait for cryptographic signing + /// + /// # Returns + /// + /// Returns a `Result` containing a `DocumentPurchaseResult` on success, or an `Error` on failure. + /// + /// # Errors + /// + /// This function will return an error if: + /// - The transition signing fails + /// - Broadcasting the transition fails + /// - The proof verification returns an unexpected result type + /// - Document not found or not for sale + /// - Insufficient funds to complete the purchase + /// - Price mismatch (document price changed) + /// - Invalid purchaser identity + pub async fn document_purchase( + &self, + purchase_document_transition_builder: DocumentPurchaseTransitionBuilder<'_>, + signing_key: &IdentityPublicKey, + signer: &S, + ) -> Result { + let platform_version = self.version(); + + let state_transition = purchase_document_transition_builder + .sign(self, signing_key, signer, platform_version, None) + .await?; + + let proof_result = state_transition + .broadcast_and_wait::(self, None) + .await?; + + match proof_result { + StateTransitionProofResult::VerifiedDocuments(documents) => { + if let Some((_, Some(document))) = documents.into_iter().next() { + Ok(DocumentPurchaseResult::Document(document)) + } else { + Err(Error::DriveProofError( + drive::error::proof::ProofError::UnexpectedResultProof( + "Expected document in VerifiedDocuments result for purchase transition" + .to_string(), + ), + vec![], + Default::default(), + )) + } + } + _ => Err(Error::DriveProofError( + drive::error::proof::ProofError::UnexpectedResultProof( + "Expected VerifiedDocuments for document purchase transition".to_string(), + ), + vec![], + Default::default(), + )), + } + } +} diff --git a/packages/rs-sdk/src/platform/documents/transitions/replace.rs b/packages/rs-sdk/src/platform/documents/transitions/replace.rs new file mode 100644 index 00000000000..7690aecfb36 --- /dev/null +++ b/packages/rs-sdk/src/platform/documents/transitions/replace.rs @@ -0,0 +1,215 @@ +use crate::platform::transition::broadcast::BroadcastStateTransition; +use crate::platform::transition::put_settings::PutSettings; +use crate::{Error, Sdk}; +use dpp::data_contract::accessors::v0::DataContractV0Getters; +use dpp::data_contract::document_type::DocumentType; +use dpp::data_contract::DataContract; +use dpp::document::{Document, DocumentV0Getters}; +use dpp::identity::signer::Signer; +use dpp::identity::IdentityPublicKey; +use dpp::prelude::UserFeeIncrease; +use dpp::state_transition::batch_transition::methods::v0::DocumentsBatchTransitionMethodsV0; +use dpp::state_transition::batch_transition::methods::StateTransitionCreationOptions; +use dpp::state_transition::batch_transition::BatchTransition; +use dpp::state_transition::proof_result::StateTransitionProofResult; +use dpp::state_transition::StateTransition; +use dpp::tokens::token_payment_info::TokenPaymentInfo; +use dpp::version::PlatformVersion; + +/// A builder to configure and broadcast document replace transitions +pub struct DocumentReplaceTransitionBuilder<'a> { + data_contract: &'a DataContract, + document_type: DocumentType, + document: Document, + token_payment_info: Option, + settings: Option, + user_fee_increase: Option, +} + +impl<'a> DocumentReplaceTransitionBuilder<'a> { + /// Start building a replace document request for the provided DataContract. + /// + /// # Arguments + /// + /// * `data_contract` - A reference to the data contract + /// * `document_type` - The document type to replace + /// * `document` - The document with updated values + /// + /// # Returns + /// + /// * `Self` - The new builder instance + pub fn new( + data_contract: &'a DataContract, + document_type: DocumentType, + document: Document, + ) -> Self { + Self { + data_contract, + document_type, + document, + token_payment_info: None, + settings: None, + user_fee_increase: None, + } + } + + /// Adds token payment info to the document replace transition + /// + /// # Arguments + /// + /// * `token_payment_info` - The token payment info to add + /// + /// # Returns + /// + /// * `Self` - The updated builder + pub fn with_token_payment_info(mut self, token_payment_info: TokenPaymentInfo) -> Self { + self.token_payment_info = Some(token_payment_info); + self + } + + /// Adds a user fee increase to the document replace transition + /// + /// # Arguments + /// + /// * `user_fee_increase` - The user fee increase to add + /// + /// # Returns + /// + /// * `Self` - The updated builder + pub fn with_user_fee_increase(mut self, user_fee_increase: UserFeeIncrease) -> Self { + self.user_fee_increase = Some(user_fee_increase); + self + } + + /// Adds settings to the document replace transition + /// + /// # Arguments + /// + /// * `settings` - The settings to add + /// + /// # Returns + /// + /// * `Self` - The updated builder + pub fn with_settings(mut self, settings: PutSettings) -> Self { + self.settings = Some(settings); + self + } + + /// Signs the document replace transition + /// + /// # Arguments + /// + /// * `sdk` - The SDK instance + /// * `identity_public_key` - The public key of the identity + /// * `signer` - The signer instance + /// * `platform_version` - The platform version + /// * `options` - Optional state transition creation options + /// + /// # Returns + /// + /// * `Result` - The signed state transition or an error + pub async fn sign( + &self, + sdk: &Sdk, + identity_public_key: &IdentityPublicKey, + signer: &impl Signer, + platform_version: &PlatformVersion, + options: Option, + ) -> Result { + let identity_contract_nonce = sdk + .get_identity_contract_nonce( + self.document.owner_id(), + self.data_contract.id(), + true, + self.settings, + ) + .await?; + + let state_transition = BatchTransition::new_document_replacement_transition_from_document( + self.document.clone(), + self.document_type.as_ref(), + identity_public_key, + identity_contract_nonce, + self.user_fee_increase.unwrap_or_default(), + self.token_payment_info, + signer, + platform_version, + options, + )?; + + Ok(state_transition) + } +} + +/// Result types returned from document replace operations. +#[derive(Debug)] +pub enum DocumentReplaceResult { + /// Document replace result containing the updated document. + Document(Document), +} + +impl Sdk { + /// Replaces an existing document on the platform. + /// + /// This method broadcasts a document replacement transition to update an existing + /// document with new data. The result contains the updated document. + /// + /// # Arguments + /// + /// * `replace_document_transition_builder` - Builder containing document replacement parameters + /// * `signing_key` - The identity public key for signing the transition + /// * `signer` - Implementation of the Signer trait for cryptographic signing + /// + /// # Returns + /// + /// Returns a `Result` containing a `DocumentReplaceResult` on success, or an `Error` on failure. + /// + /// # Errors + /// + /// This function will return an error if: + /// - The transition signing fails + /// - Broadcasting the transition fails + /// - The proof verification returns an unexpected result type + /// - Document validation fails + /// - Document not found or revision mismatch + pub async fn document_replace( + &self, + replace_document_transition_builder: DocumentReplaceTransitionBuilder<'_>, + signing_key: &IdentityPublicKey, + signer: &S, + ) -> Result { + let platform_version = self.version(); + + let state_transition = replace_document_transition_builder + .sign(self, signing_key, signer, platform_version, None) + .await?; + + let proof_result = state_transition + .broadcast_and_wait::(self, None) + .await?; + + match proof_result { + StateTransitionProofResult::VerifiedDocuments(documents) => { + if let Some((_, Some(document))) = documents.into_iter().next() { + Ok(DocumentReplaceResult::Document(document)) + } else { + Err(Error::DriveProofError( + drive::error::proof::ProofError::UnexpectedResultProof( + "Expected document in VerifiedDocuments result for replace transition" + .to_string(), + ), + vec![], + Default::default(), + )) + } + } + _ => Err(Error::DriveProofError( + drive::error::proof::ProofError::UnexpectedResultProof( + "Expected VerifiedDocuments for document replace transition".to_string(), + ), + vec![], + Default::default(), + )), + } + } +} diff --git a/packages/rs-sdk/src/platform/documents/transitions/set_price.rs b/packages/rs-sdk/src/platform/documents/transitions/set_price.rs new file mode 100644 index 00000000000..5e3f7326fd8 --- /dev/null +++ b/packages/rs-sdk/src/platform/documents/transitions/set_price.rs @@ -0,0 +1,263 @@ +use crate::platform::transition::broadcast::BroadcastStateTransition; +use crate::platform::transition::put_settings::PutSettings; +use crate::platform::Identifier; +use crate::{Error, Sdk}; +use dpp::data_contract::accessors::v0::DataContractV0Getters; +use dpp::data_contract::document_type::DocumentType; +use dpp::data_contract::DataContract; +use dpp::document::{Document, DocumentV0Getters}; +use dpp::fee::Credits; +use dpp::identity::signer::Signer; +use dpp::identity::IdentityPublicKey; +use dpp::prelude::UserFeeIncrease; +use dpp::state_transition::batch_transition::methods::v0::DocumentsBatchTransitionMethodsV0; +use dpp::state_transition::batch_transition::methods::StateTransitionCreationOptions; +use dpp::state_transition::batch_transition::BatchTransition; +use dpp::state_transition::proof_result::StateTransitionProofResult; +use dpp::state_transition::StateTransition; +use dpp::tokens::token_payment_info::TokenPaymentInfo; +use dpp::version::PlatformVersion; + +/// A builder to configure and broadcast document set price transitions +pub struct DocumentSetPriceTransitionBuilder<'a> { + data_contract: &'a DataContract, + document_type: DocumentType, + document: Document, + price: Credits, + token_payment_info: Option, + settings: Option, + user_fee_increase: Option, +} + +impl<'a> DocumentSetPriceTransitionBuilder<'a> { + /// Start building a set price document request for the provided DataContract. + /// + /// # Arguments + /// + /// * `data_contract` - A reference to the data contract + /// * `document_type` - The document type + /// * `document` - The document to update price for + /// * `price` - The new price in credits + /// + /// # Returns + /// + /// * `Self` - The new builder instance + pub fn new( + data_contract: &'a DataContract, + document_type: DocumentType, + document: Document, + price: Credits, + ) -> Self { + Self { + data_contract, + document_type, + document, + price, + token_payment_info: None, + settings: None, + user_fee_increase: None, + } + } + + /// Creates a new builder from document ID + /// + /// # Arguments + /// + /// * `data_contract` - A reference to the data contract + /// * `document_type` - The document type + /// * `document_id` - The ID of the document + /// * `owner_id` - The owner ID of the document + /// * `price` - The new price in credits + /// + /// # Returns + /// + /// * `Self` - The new builder instance + pub fn from_document_info( + data_contract: &'a DataContract, + document_type: DocumentType, + document_id: Identifier, + owner_id: Identifier, + price: Credits, + ) -> Self { + // Create a minimal document with just the required fields + // The actual document will be fetched during the transition + let document = Document::V0(dpp::document::DocumentV0 { + id: document_id, + owner_id, + properties: Default::default(), + revision: Some(1), // Will be updated during transition + created_at: None, + updated_at: None, + transferred_at: None, + created_at_block_height: None, + updated_at_block_height: None, + transferred_at_block_height: None, + created_at_core_block_height: None, + updated_at_core_block_height: None, + transferred_at_core_block_height: None, + }); + + Self::new(data_contract, document_type, document, price) + } + + /// Adds token payment info to the document set price transition + /// + /// # Arguments + /// + /// * `token_payment_info` - The token payment info to add + /// + /// # Returns + /// + /// * `Self` - The updated builder + pub fn with_token_payment_info(mut self, token_payment_info: TokenPaymentInfo) -> Self { + self.token_payment_info = Some(token_payment_info); + self + } + + /// Adds a user fee increase to the document set price transition + /// + /// # Arguments + /// + /// * `user_fee_increase` - The user fee increase to add + /// + /// # Returns + /// + /// * `Self` - The updated builder + pub fn with_user_fee_increase(mut self, user_fee_increase: UserFeeIncrease) -> Self { + self.user_fee_increase = Some(user_fee_increase); + self + } + + /// Adds settings to the document set price transition + /// + /// # Arguments + /// + /// * `settings` - The settings to add + /// + /// # Returns + /// + /// * `Self` - The updated builder + pub fn with_settings(mut self, settings: PutSettings) -> Self { + self.settings = Some(settings); + self + } + + /// Signs the document set price transition + /// + /// # Arguments + /// + /// * `sdk` - The SDK instance + /// * `identity_public_key` - The public key of the identity + /// * `signer` - The signer instance + /// * `platform_version` - The platform version + /// * `options` - Optional state transition creation options + /// + /// # Returns + /// + /// * `Result` - The signed state transition or an error + pub async fn sign( + &self, + sdk: &Sdk, + identity_public_key: &IdentityPublicKey, + signer: &impl Signer, + platform_version: &PlatformVersion, + options: Option, + ) -> Result { + let identity_contract_nonce = sdk + .get_identity_contract_nonce( + self.document.owner_id(), + self.data_contract.id(), + true, + self.settings, + ) + .await?; + + let state_transition = BatchTransition::new_document_update_price_transition_from_document( + self.document.clone(), + self.document_type.as_ref(), + self.price, + identity_public_key, + identity_contract_nonce, + self.user_fee_increase.unwrap_or_default(), + self.token_payment_info, + signer, + platform_version, + options, + )?; + + Ok(state_transition) + } +} + +/// Result types returned from document set price operations. +#[derive(Debug)] +pub enum DocumentSetPriceResult { + /// Document price updated, containing the document with new price. + Document(Document), +} + +impl Sdk { + /// Sets the price for a document on the platform. + /// + /// This method broadcasts a document price update transition to set or change + /// the price of an existing document. The result contains the updated document. + /// + /// # Arguments + /// + /// * `set_price_document_transition_builder` - Builder containing price update parameters + /// * `signing_key` - The identity public key for signing the transition + /// * `signer` - Implementation of the Signer trait for cryptographic signing + /// + /// # Returns + /// + /// Returns a `Result` containing a `DocumentSetPriceResult` on success, or an `Error` on failure. + /// + /// # Errors + /// + /// This function will return an error if: + /// - The transition signing fails + /// - Broadcasting the transition fails + /// - The proof verification returns an unexpected result type + /// - Document not found + /// - Insufficient permissions to set price + /// - Invalid price value + pub async fn document_set_price( + &self, + set_price_document_transition_builder: DocumentSetPriceTransitionBuilder<'_>, + signing_key: &IdentityPublicKey, + signer: &S, + ) -> Result { + let platform_version = self.version(); + + let state_transition = set_price_document_transition_builder + .sign(self, signing_key, signer, platform_version, None) + .await?; + + let proof_result = state_transition + .broadcast_and_wait::(self, None) + .await?; + + match proof_result { + StateTransitionProofResult::VerifiedDocuments(documents) => { + if let Some((_, Some(document))) = documents.into_iter().next() { + Ok(DocumentSetPriceResult::Document(document)) + } else { + Err(Error::DriveProofError( + drive::error::proof::ProofError::UnexpectedResultProof( + "Expected document in VerifiedDocuments result for set price transition".to_string(), + ), + vec![], + Default::default(), + )) + } + } + _ => Err(Error::DriveProofError( + drive::error::proof::ProofError::UnexpectedResultProof( + "Expected VerifiedDocuments for document set price transition".to_string(), + ), + vec![], + Default::default(), + )), + } + } +} diff --git a/packages/rs-sdk/src/platform/documents/transitions/transfer.rs b/packages/rs-sdk/src/platform/documents/transitions/transfer.rs new file mode 100644 index 00000000000..94a585e2c07 --- /dev/null +++ b/packages/rs-sdk/src/platform/documents/transitions/transfer.rs @@ -0,0 +1,263 @@ +use crate::platform::transition::broadcast::BroadcastStateTransition; +use crate::platform::transition::put_settings::PutSettings; +use crate::platform::Identifier; +use crate::{Error, Sdk}; +use dpp::data_contract::accessors::v0::DataContractV0Getters; +use dpp::data_contract::document_type::DocumentType; +use dpp::data_contract::DataContract; +use dpp::document::{Document, DocumentV0Getters}; +use dpp::identity::signer::Signer; +use dpp::identity::IdentityPublicKey; +use dpp::prelude::UserFeeIncrease; +use dpp::state_transition::batch_transition::methods::v0::DocumentsBatchTransitionMethodsV0; +use dpp::state_transition::batch_transition::methods::StateTransitionCreationOptions; +use dpp::state_transition::batch_transition::BatchTransition; +use dpp::state_transition::proof_result::StateTransitionProofResult; +use dpp::state_transition::StateTransition; +use dpp::tokens::token_payment_info::TokenPaymentInfo; +use dpp::version::PlatformVersion; + +/// A builder to configure and broadcast document transfer transitions +pub struct DocumentTransferTransitionBuilder<'a> { + data_contract: &'a DataContract, + document_type: DocumentType, + document: Document, + recipient_id: Identifier, + token_payment_info: Option, + settings: Option, + user_fee_increase: Option, +} + +impl<'a> DocumentTransferTransitionBuilder<'a> { + /// Start building a transfer document request for the provided DataContract. + /// + /// # Arguments + /// + /// * `data_contract` - A reference to the data contract + /// * `document_type` - The document type + /// * `document` - The document to transfer + /// * `recipient_id` - The identifier of the recipient + /// + /// # Returns + /// + /// * `Self` - The new builder instance + pub fn new( + data_contract: &'a DataContract, + document_type: DocumentType, + document: Document, + recipient_id: Identifier, + ) -> Self { + Self { + data_contract, + document_type, + document, + recipient_id, + token_payment_info: None, + settings: None, + user_fee_increase: None, + } + } + + /// Creates a new builder from document ID + /// + /// # Arguments + /// + /// * `data_contract` - A reference to the data contract + /// * `document_type` - The document type + /// * `document_id` - The ID of the document + /// * `owner_id` - The current owner ID of the document + /// * `recipient_id` - The identifier of the recipient + /// + /// # Returns + /// + /// * `Self` - The new builder instance + pub fn from_document_info( + data_contract: &'a DataContract, + document_type: DocumentType, + document_id: Identifier, + owner_id: Identifier, + recipient_id: Identifier, + ) -> Self { + // Create a minimal document with just the required fields + // The actual document will be fetched during the transition + let document = Document::V0(dpp::document::DocumentV0 { + id: document_id, + owner_id, + properties: Default::default(), + revision: Some(1), // Will be updated during transition + created_at: None, + updated_at: None, + transferred_at: None, + created_at_block_height: None, + updated_at_block_height: None, + transferred_at_block_height: None, + created_at_core_block_height: None, + updated_at_core_block_height: None, + transferred_at_core_block_height: None, + }); + + Self::new(data_contract, document_type, document, recipient_id) + } + + /// Adds token payment info to the document transfer transition + /// + /// # Arguments + /// + /// * `token_payment_info` - The token payment info to add + /// + /// # Returns + /// + /// * `Self` - The updated builder + pub fn with_token_payment_info(mut self, token_payment_info: TokenPaymentInfo) -> Self { + self.token_payment_info = Some(token_payment_info); + self + } + + /// Adds a user fee increase to the document transfer transition + /// + /// # Arguments + /// + /// * `user_fee_increase` - The user fee increase to add + /// + /// # Returns + /// + /// * `Self` - The updated builder + pub fn with_user_fee_increase(mut self, user_fee_increase: UserFeeIncrease) -> Self { + self.user_fee_increase = Some(user_fee_increase); + self + } + + /// Adds settings to the document transfer transition + /// + /// # Arguments + /// + /// * `settings` - The settings to add + /// + /// # Returns + /// + /// * `Self` - The updated builder + pub fn with_settings(mut self, settings: PutSettings) -> Self { + self.settings = Some(settings); + self + } + + /// Signs the document transfer transition + /// + /// # Arguments + /// + /// * `sdk` - The SDK instance + /// * `identity_public_key` - The public key of the identity + /// * `signer` - The signer instance + /// * `platform_version` - The platform version + /// * `options` - Optional state transition creation options + /// + /// # Returns + /// + /// * `Result` - The signed state transition or an error + pub async fn sign( + &self, + sdk: &Sdk, + identity_public_key: &IdentityPublicKey, + signer: &impl Signer, + platform_version: &PlatformVersion, + options: Option, + ) -> Result { + let identity_contract_nonce = sdk + .get_identity_contract_nonce( + self.document.owner_id(), + self.data_contract.id(), + true, + self.settings, + ) + .await?; + + let state_transition = BatchTransition::new_document_transfer_transition_from_document( + self.document.clone(), + self.document_type.as_ref(), + self.recipient_id, + identity_public_key, + identity_contract_nonce, + self.user_fee_increase.unwrap_or_default(), + self.token_payment_info, + signer, + platform_version, + options, + )?; + + Ok(state_transition) + } +} + +/// Result types returned from document transfer operations. +#[derive(Debug)] +pub enum DocumentTransferResult { + /// Document transferred successfully, containing the document with new owner. + Document(Document), +} + +impl Sdk { + /// Transfers ownership of a document to another identity. + /// + /// This method broadcasts a document transfer transition to change the ownership + /// of an existing document to a new identity. The result contains the transferred document. + /// + /// # Arguments + /// + /// * `transfer_document_transition_builder` - Builder containing transfer parameters + /// * `signing_key` - The identity public key for signing the transition + /// * `signer` - Implementation of the Signer trait for cryptographic signing + /// + /// # Returns + /// + /// Returns a `Result` containing a `DocumentTransferResult` on success, or an `Error` on failure. + /// + /// # Errors + /// + /// This function will return an error if: + /// - The transition signing fails + /// - Broadcasting the transition fails + /// - The proof verification returns an unexpected result type + /// - Document not found + /// - Insufficient permissions to transfer the document + /// - Invalid recipient identity + pub async fn document_transfer( + &self, + transfer_document_transition_builder: DocumentTransferTransitionBuilder<'_>, + signing_key: &IdentityPublicKey, + signer: &S, + ) -> Result { + let platform_version = self.version(); + + let state_transition = transfer_document_transition_builder + .sign(self, signing_key, signer, platform_version, None) + .await?; + + let proof_result = state_transition + .broadcast_and_wait::(self, None) + .await?; + + match proof_result { + StateTransitionProofResult::VerifiedDocuments(documents) => { + if let Some((_, Some(document))) = documents.into_iter().next() { + Ok(DocumentTransferResult::Document(document)) + } else { + Err(Error::DriveProofError( + drive::error::proof::ProofError::UnexpectedResultProof( + "Expected document in VerifiedDocuments result for transfer transition" + .to_string(), + ), + vec![], + Default::default(), + )) + } + } + _ => Err(Error::DriveProofError( + drive::error::proof::ProofError::UnexpectedResultProof( + "Expected VerifiedDocuments for document transfer transition".to_string(), + ), + vec![], + Default::default(), + )), + } + } +} diff --git a/packages/rs-sdk/src/platform/fetch_many.rs b/packages/rs-sdk/src/platform/fetch_many.rs index 59767c6acbc..b6541439898 100644 --- a/packages/rs-sdk/src/platform/fetch_many.rs +++ b/packages/rs-sdk/src/platform/fetch_many.rs @@ -6,13 +6,8 @@ //! - `[FetchMany]`: An async trait that fetches multiple items of a specific type from Platform. use super::LimitQuery; -use crate::{ - error::Error, - mock::MockResponse, - platform::{document_query::DocumentQuery, query::Query}, - sync::retry, - Sdk, -}; +use crate::platform::documents::document_query::DocumentQuery; +use crate::{error::Error, mock::MockResponse, platform::query::Query, sync::retry, Sdk}; use dapi_grpc::platform::v0::{ GetContestedResourceIdentityVotesRequest, GetContestedResourceVoteStateRequest, GetContestedResourceVotersForIdentityRequest, GetContestedResourcesRequest, @@ -315,7 +310,7 @@ where /// ## Supported query types /// /// * [DriveQuery](crate::platform::DriveDocumentQuery) - query that specifies document matching criteria -/// * [DocumentQuery](crate::platform::document_query::DocumentQuery) +/// * [DocumentQuery](crate::platform::documents::document_query::DocumentQuery) #[async_trait::async_trait] impl FetchMany for Document { // We need to use the DocumentQuery type here because the DocumentQuery diff --git a/packages/rs-sdk/src/platform/query.rs b/packages/rs-sdk/src/platform/query.rs index 0cdfbc81e9c..44a366895db 100644 --- a/packages/rs-sdk/src/platform/query.rs +++ b/packages/rs-sdk/src/platform/query.rs @@ -3,7 +3,8 @@ //! [Query] trait is used to specify individual objects as well as search criteria for fetching multiple objects from Platform. use super::types::epoch::EpochQuery; use super::types::evonode::EvoNode; -use crate::{error::Error, platform::document_query::DocumentQuery}; +use crate::error::Error; +use crate::platform::documents::document_query::DocumentQuery; use dapi_grpc::mock::Mockable; use dapi_grpc::platform::v0::get_contested_resource_identity_votes_request::GetContestedResourceIdentityVotesRequestV0; use dapi_grpc::platform::v0::get_contested_resource_voters_for_identity_request::GetContestedResourceVotersForIdentityRequestV0; From 0a14661ec52a07bd1f5a8d528e34d7e6e68997b6 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Wed, 4 Jun 2025 03:18:08 +0200 Subject: [PATCH 2/3] more work --- .../platform/documents/transitions/create.rs | 29 +++++++----- .../platform/documents/transitions/delete.rs | 43 ++++++++++-------- .../documents/transitions/purchase.rs | 45 ++++++++++++------- .../platform/documents/transitions/replace.rs | 29 +++++++----- .../documents/transitions/set_price.rs | 39 +++++++++------- .../documents/transitions/transfer.rs | 39 +++++++++------- 6 files changed, 130 insertions(+), 94 deletions(-) diff --git a/packages/rs-sdk/src/platform/documents/transitions/create.rs b/packages/rs-sdk/src/platform/documents/transitions/create.rs index 2017a4b13eb..6a74013305e 100644 --- a/packages/rs-sdk/src/platform/documents/transitions/create.rs +++ b/packages/rs-sdk/src/platform/documents/transitions/create.rs @@ -2,7 +2,6 @@ use crate::platform::transition::broadcast::BroadcastStateTransition; use crate::platform::transition::put_settings::PutSettings; use crate::{Error, Sdk}; use dpp::data_contract::accessors::v0::DataContractV0Getters; -use dpp::data_contract::document_type::DocumentType; use dpp::data_contract::DataContract; use dpp::document::{Document, DocumentV0Getters}; use dpp::identity::signer::Signer; @@ -15,11 +14,12 @@ use dpp::state_transition::proof_result::StateTransitionProofResult; use dpp::state_transition::StateTransition; use dpp::tokens::token_payment_info::TokenPaymentInfo; use dpp::version::PlatformVersion; +use std::sync::Arc; /// A builder to configure and broadcast document create transitions -pub struct DocumentCreateTransitionBuilder<'a> { - data_contract: &'a DataContract, - document_type: DocumentType, +pub struct DocumentCreateTransitionBuilder { + data_contract: Arc, + document_type_name: String, document: Document, document_state_transition_entropy: [u8; 32], token_payment_info: Option, @@ -27,13 +27,13 @@ pub struct DocumentCreateTransitionBuilder<'a> { user_fee_increase: Option, } -impl<'a> DocumentCreateTransitionBuilder<'a> { +impl DocumentCreateTransitionBuilder { /// Start building a create document request for the provided DataContract. /// /// # Arguments /// - /// * `data_contract` - A reference to the data contract - /// * `document_type` - The document type to create + /// * `data_contract` - The data contract + /// * `document_type_name` - The name of the document type to create /// * `document` - The document to create /// * `document_state_transition_entropy` - Entropy for the state transition /// @@ -41,14 +41,14 @@ impl<'a> DocumentCreateTransitionBuilder<'a> { /// /// * `Self` - The new builder instance pub fn new( - data_contract: &'a DataContract, - document_type: DocumentType, + data_contract: Arc, + document_type_name: String, document: Document, document_state_transition_entropy: [u8; 32], ) -> Self { Self { data_contract, - document_type, + document_type_name, document, document_state_transition_entropy, token_payment_info: None, @@ -129,9 +129,14 @@ impl<'a> DocumentCreateTransitionBuilder<'a> { ) .await?; + let document_type = self + .data_contract + .document_type_for_name(&self.document_type_name) + .map_err(|e| Error::Protocol(e.into()))?; + let state_transition = BatchTransition::new_document_creation_transition_from_document( self.document.clone(), - self.document_type.as_ref(), + document_type, self.document_state_transition_entropy, identity_public_key, identity_contract_nonce, @@ -178,7 +183,7 @@ impl Sdk { /// - Document validation fails pub async fn document_create( &self, - create_document_transition_builder: DocumentCreateTransitionBuilder<'_>, + create_document_transition_builder: DocumentCreateTransitionBuilder, signing_key: &IdentityPublicKey, signer: &S, ) -> Result { diff --git a/packages/rs-sdk/src/platform/documents/transitions/delete.rs b/packages/rs-sdk/src/platform/documents/transitions/delete.rs index 61193959748..ac91d407575 100644 --- a/packages/rs-sdk/src/platform/documents/transitions/delete.rs +++ b/packages/rs-sdk/src/platform/documents/transitions/delete.rs @@ -3,9 +3,8 @@ use crate::platform::transition::put_settings::PutSettings; use crate::platform::Identifier; use crate::{Error, Sdk}; use dpp::data_contract::accessors::v0::DataContractV0Getters; -use dpp::data_contract::document_type::DocumentType; use dpp::data_contract::DataContract; -use dpp::document::Document; +use dpp::document::{Document, INITIAL_REVISION}; use dpp::identity::signer::Signer; use dpp::identity::IdentityPublicKey; use dpp::prelude::UserFeeIncrease; @@ -16,11 +15,12 @@ use dpp::state_transition::proof_result::StateTransitionProofResult; use dpp::state_transition::StateTransition; use dpp::tokens::token_payment_info::TokenPaymentInfo; use dpp::version::PlatformVersion; +use std::sync::Arc; /// A builder to configure and broadcast document delete transitions -pub struct DocumentDeleteTransitionBuilder<'a> { - data_contract: &'a DataContract, - document_type: DocumentType, +pub struct DocumentDeleteTransitionBuilder { + data_contract: Arc, + document_type_name: String, document_id: Identifier, owner_id: Identifier, token_payment_info: Option, @@ -28,13 +28,13 @@ pub struct DocumentDeleteTransitionBuilder<'a> { user_fee_increase: Option, } -impl<'a> DocumentDeleteTransitionBuilder<'a> { +impl DocumentDeleteTransitionBuilder { /// Start building a delete document request for the provided DataContract. /// /// # Arguments /// - /// * `data_contract` - A reference to the data contract - /// * `document_type` - The document type to delete + /// * `data_contract` - The data contract + /// * `document_type_name` - The name of the document type to delete /// * `document_id` - The ID of the document to delete /// * `owner_id` - The owner ID of the document /// @@ -42,14 +42,14 @@ impl<'a> DocumentDeleteTransitionBuilder<'a> { /// /// * `Self` - The new builder instance pub fn new( - data_contract: &'a DataContract, - document_type: DocumentType, + data_contract: Arc, + document_type_name: String, document_id: Identifier, owner_id: Identifier, ) -> Self { Self { data_contract, - document_type, + document_type_name, document_id, owner_id, token_payment_info: None, @@ -62,22 +62,22 @@ impl<'a> DocumentDeleteTransitionBuilder<'a> { /// /// # Arguments /// - /// * `data_contract` - A reference to the data contract - /// * `document_type` - The document type to delete + /// * `data_contract` - The data contract + /// * `document_type_name` - The name of the document type to delete /// * `document` - The document to delete /// /// # Returns /// /// * `Self` - The new builder instance pub fn from_document( - data_contract: &'a DataContract, - document_type: DocumentType, + data_contract: Arc, + document_type_name: String, document: &Document, ) -> Self { use dpp::document::DocumentV0Getters; Self::new( data_contract, - document_type, + document_type_name, document.id(), document.owner_id(), ) @@ -155,12 +155,17 @@ impl<'a> DocumentDeleteTransitionBuilder<'a> { ) .await?; + let document_type = self + .data_contract + .document_type_for_name(&self.document_type_name) + .map_err(|e| Error::Protocol(e.into()))?; + // Create a minimal document for deletion let document = Document::V0(dpp::document::DocumentV0 { id: self.document_id, owner_id: self.owner_id, properties: Default::default(), - revision: Some(1), // Will be updated during transition + revision: Some(INITIAL_REVISION), created_at: None, updated_at: None, transferred_at: None, @@ -174,7 +179,7 @@ impl<'a> DocumentDeleteTransitionBuilder<'a> { let state_transition = BatchTransition::new_document_deletion_transition_from_document( document, - self.document_type.as_ref(), + document_type, identity_public_key, identity_contract_nonce, self.user_fee_increase.unwrap_or_default(), @@ -221,7 +226,7 @@ impl Sdk { /// - Insufficient permissions to delete the document pub async fn document_delete( &self, - delete_document_transition_builder: DocumentDeleteTransitionBuilder<'_>, + delete_document_transition_builder: DocumentDeleteTransitionBuilder, signing_key: &IdentityPublicKey, signer: &S, ) -> Result { diff --git a/packages/rs-sdk/src/platform/documents/transitions/purchase.rs b/packages/rs-sdk/src/platform/documents/transitions/purchase.rs index c882f07f3d2..42301154139 100644 --- a/packages/rs-sdk/src/platform/documents/transitions/purchase.rs +++ b/packages/rs-sdk/src/platform/documents/transitions/purchase.rs @@ -3,7 +3,6 @@ use crate::platform::transition::put_settings::PutSettings; use crate::platform::Identifier; use crate::{Error, Sdk}; use dpp::data_contract::accessors::v0::DataContractV0Getters; -use dpp::data_contract::document_type::DocumentType; use dpp::data_contract::DataContract; use dpp::document::Document; use dpp::fee::Credits; @@ -17,11 +16,12 @@ use dpp::state_transition::proof_result::StateTransitionProofResult; use dpp::state_transition::StateTransition; use dpp::tokens::token_payment_info::TokenPaymentInfo; use dpp::version::PlatformVersion; +use std::sync::Arc; /// A builder to configure and broadcast document purchase transitions -pub struct DocumentPurchaseTransitionBuilder<'a> { - data_contract: &'a DataContract, - document_type: DocumentType, +pub struct DocumentPurchaseTransitionBuilder { + data_contract: Arc, + document_type_name: String, document: Document, purchaser_id: Identifier, price: Credits, @@ -30,13 +30,13 @@ pub struct DocumentPurchaseTransitionBuilder<'a> { user_fee_increase: Option, } -impl<'a> DocumentPurchaseTransitionBuilder<'a> { +impl DocumentPurchaseTransitionBuilder { /// Start building a purchase document request for the provided DataContract. /// /// # Arguments /// - /// * `data_contract` - A reference to the data contract - /// * `document_type` - The document type + /// * `data_contract` - The data contract + /// * `document_type_name` - The name of the document type /// * `document` - The document to purchase /// * `purchaser_id` - The identifier of the purchaser /// * `price` - The price to pay for the document @@ -45,15 +45,15 @@ impl<'a> DocumentPurchaseTransitionBuilder<'a> { /// /// * `Self` - The new builder instance pub fn new( - data_contract: &'a DataContract, - document_type: DocumentType, + data_contract: Arc, + document_type_name: String, document: Document, purchaser_id: Identifier, price: Credits, ) -> Self { Self { data_contract, - document_type, + document_type_name, document, purchaser_id, price, @@ -67,8 +67,8 @@ impl<'a> DocumentPurchaseTransitionBuilder<'a> { /// /// # Arguments /// - /// * `data_contract` - A reference to the data contract - /// * `document_type` - The document type + /// * `data_contract` - The data contract + /// * `document_type_name` - The name of the document type /// * `document_id` - The ID of the document /// * `current_owner_id` - The current owner ID of the document /// * `purchaser_id` - The identifier of the purchaser @@ -78,8 +78,8 @@ impl<'a> DocumentPurchaseTransitionBuilder<'a> { /// /// * `Self` - The new builder instance pub fn from_document_info( - data_contract: &'a DataContract, - document_type: DocumentType, + data_contract: Arc, + document_type_name: String, document_id: Identifier, current_owner_id: Identifier, purchaser_id: Identifier, @@ -103,7 +103,13 @@ impl<'a> DocumentPurchaseTransitionBuilder<'a> { transferred_at_core_block_height: None, }); - Self::new(data_contract, document_type, document, purchaser_id, price) + Self::new( + data_contract, + document_type_name, + document, + purchaser_id, + price, + ) } /// Adds token payment info to the document purchase transition @@ -178,9 +184,14 @@ impl<'a> DocumentPurchaseTransitionBuilder<'a> { ) .await?; + let document_type = self + .data_contract + .document_type_for_name(&self.document_type_name) + .map_err(|e| Error::Protocol(e.into()))?; + let state_transition = BatchTransition::new_document_purchase_transition_from_document( self.document.clone(), - self.document_type.as_ref(), + document_type, self.purchaser_id, self.price, identity_public_key, @@ -232,7 +243,7 @@ impl Sdk { /// - Invalid purchaser identity pub async fn document_purchase( &self, - purchase_document_transition_builder: DocumentPurchaseTransitionBuilder<'_>, + purchase_document_transition_builder: DocumentPurchaseTransitionBuilder, signing_key: &IdentityPublicKey, signer: &S, ) -> Result { diff --git a/packages/rs-sdk/src/platform/documents/transitions/replace.rs b/packages/rs-sdk/src/platform/documents/transitions/replace.rs index 7690aecfb36..c98299886ba 100644 --- a/packages/rs-sdk/src/platform/documents/transitions/replace.rs +++ b/packages/rs-sdk/src/platform/documents/transitions/replace.rs @@ -2,7 +2,6 @@ use crate::platform::transition::broadcast::BroadcastStateTransition; use crate::platform::transition::put_settings::PutSettings; use crate::{Error, Sdk}; use dpp::data_contract::accessors::v0::DataContractV0Getters; -use dpp::data_contract::document_type::DocumentType; use dpp::data_contract::DataContract; use dpp::document::{Document, DocumentV0Getters}; use dpp::identity::signer::Signer; @@ -15,37 +14,38 @@ use dpp::state_transition::proof_result::StateTransitionProofResult; use dpp::state_transition::StateTransition; use dpp::tokens::token_payment_info::TokenPaymentInfo; use dpp::version::PlatformVersion; +use std::sync::Arc; /// A builder to configure and broadcast document replace transitions -pub struct DocumentReplaceTransitionBuilder<'a> { - data_contract: &'a DataContract, - document_type: DocumentType, +pub struct DocumentReplaceTransitionBuilder { + data_contract: Arc, + document_type_name: String, document: Document, token_payment_info: Option, settings: Option, user_fee_increase: Option, } -impl<'a> DocumentReplaceTransitionBuilder<'a> { +impl DocumentReplaceTransitionBuilder { /// Start building a replace document request for the provided DataContract. /// /// # Arguments /// - /// * `data_contract` - A reference to the data contract - /// * `document_type` - The document type to replace + /// * `data_contract` - The data contract + /// * `document_type_name` - The name of the document type to replace /// * `document` - The document with updated values /// /// # Returns /// /// * `Self` - The new builder instance pub fn new( - data_contract: &'a DataContract, - document_type: DocumentType, + data_contract: Arc, + document_type_name: String, document: Document, ) -> Self { Self { data_contract, - document_type, + document_type_name, document, token_payment_info: None, settings: None, @@ -125,9 +125,14 @@ impl<'a> DocumentReplaceTransitionBuilder<'a> { ) .await?; + let document_type = self + .data_contract + .document_type_for_name(&self.document_type_name) + .map_err(|e| Error::Protocol(e.into()))?; + let state_transition = BatchTransition::new_document_replacement_transition_from_document( self.document.clone(), - self.document_type.as_ref(), + document_type, identity_public_key, identity_contract_nonce, self.user_fee_increase.unwrap_or_default(), @@ -174,7 +179,7 @@ impl Sdk { /// - Document not found or revision mismatch pub async fn document_replace( &self, - replace_document_transition_builder: DocumentReplaceTransitionBuilder<'_>, + replace_document_transition_builder: DocumentReplaceTransitionBuilder, signing_key: &IdentityPublicKey, signer: &S, ) -> Result { diff --git a/packages/rs-sdk/src/platform/documents/transitions/set_price.rs b/packages/rs-sdk/src/platform/documents/transitions/set_price.rs index 5e3f7326fd8..92b199e548c 100644 --- a/packages/rs-sdk/src/platform/documents/transitions/set_price.rs +++ b/packages/rs-sdk/src/platform/documents/transitions/set_price.rs @@ -3,7 +3,6 @@ use crate::platform::transition::put_settings::PutSettings; use crate::platform::Identifier; use crate::{Error, Sdk}; use dpp::data_contract::accessors::v0::DataContractV0Getters; -use dpp::data_contract::document_type::DocumentType; use dpp::data_contract::DataContract; use dpp::document::{Document, DocumentV0Getters}; use dpp::fee::Credits; @@ -17,11 +16,12 @@ use dpp::state_transition::proof_result::StateTransitionProofResult; use dpp::state_transition::StateTransition; use dpp::tokens::token_payment_info::TokenPaymentInfo; use dpp::version::PlatformVersion; +use std::sync::Arc; /// A builder to configure and broadcast document set price transitions -pub struct DocumentSetPriceTransitionBuilder<'a> { - data_contract: &'a DataContract, - document_type: DocumentType, +pub struct DocumentSetPriceTransitionBuilder { + data_contract: Arc, + document_type_name: String, document: Document, price: Credits, token_payment_info: Option, @@ -29,13 +29,13 @@ pub struct DocumentSetPriceTransitionBuilder<'a> { user_fee_increase: Option, } -impl<'a> DocumentSetPriceTransitionBuilder<'a> { +impl DocumentSetPriceTransitionBuilder { /// Start building a set price document request for the provided DataContract. /// /// # Arguments /// - /// * `data_contract` - A reference to the data contract - /// * `document_type` - The document type + /// * `data_contract` - The data contract + /// * `document_type_name` - The name of the document type /// * `document` - The document to update price for /// * `price` - The new price in credits /// @@ -43,14 +43,14 @@ impl<'a> DocumentSetPriceTransitionBuilder<'a> { /// /// * `Self` - The new builder instance pub fn new( - data_contract: &'a DataContract, - document_type: DocumentType, + data_contract: Arc, + document_type_name: String, document: Document, price: Credits, ) -> Self { Self { data_contract, - document_type, + document_type_name, document, price, token_payment_info: None, @@ -63,8 +63,8 @@ impl<'a> DocumentSetPriceTransitionBuilder<'a> { /// /// # Arguments /// - /// * `data_contract` - A reference to the data contract - /// * `document_type` - The document type + /// * `data_contract` - The data contract + /// * `document_type_name` - The name of the document type /// * `document_id` - The ID of the document /// * `owner_id` - The owner ID of the document /// * `price` - The new price in credits @@ -73,8 +73,8 @@ impl<'a> DocumentSetPriceTransitionBuilder<'a> { /// /// * `Self` - The new builder instance pub fn from_document_info( - data_contract: &'a DataContract, - document_type: DocumentType, + data_contract: Arc, + document_type_name: String, document_id: Identifier, owner_id: Identifier, price: Credits, @@ -97,7 +97,7 @@ impl<'a> DocumentSetPriceTransitionBuilder<'a> { transferred_at_core_block_height: None, }); - Self::new(data_contract, document_type, document, price) + Self::new(data_contract, document_type_name, document, price) } /// Adds token payment info to the document set price transition @@ -172,9 +172,14 @@ impl<'a> DocumentSetPriceTransitionBuilder<'a> { ) .await?; + let document_type = self + .data_contract + .document_type_for_name(&self.document_type_name) + .map_err(|e| Error::Protocol(e.into()))?; + let state_transition = BatchTransition::new_document_update_price_transition_from_document( self.document.clone(), - self.document_type.as_ref(), + document_type, self.price, identity_public_key, identity_contract_nonce, @@ -223,7 +228,7 @@ impl Sdk { /// - Invalid price value pub async fn document_set_price( &self, - set_price_document_transition_builder: DocumentSetPriceTransitionBuilder<'_>, + set_price_document_transition_builder: DocumentSetPriceTransitionBuilder, signing_key: &IdentityPublicKey, signer: &S, ) -> Result { diff --git a/packages/rs-sdk/src/platform/documents/transitions/transfer.rs b/packages/rs-sdk/src/platform/documents/transitions/transfer.rs index 94a585e2c07..01724e44651 100644 --- a/packages/rs-sdk/src/platform/documents/transitions/transfer.rs +++ b/packages/rs-sdk/src/platform/documents/transitions/transfer.rs @@ -3,7 +3,6 @@ use crate::platform::transition::put_settings::PutSettings; use crate::platform::Identifier; use crate::{Error, Sdk}; use dpp::data_contract::accessors::v0::DataContractV0Getters; -use dpp::data_contract::document_type::DocumentType; use dpp::data_contract::DataContract; use dpp::document::{Document, DocumentV0Getters}; use dpp::identity::signer::Signer; @@ -16,11 +15,12 @@ use dpp::state_transition::proof_result::StateTransitionProofResult; use dpp::state_transition::StateTransition; use dpp::tokens::token_payment_info::TokenPaymentInfo; use dpp::version::PlatformVersion; +use std::sync::Arc; /// A builder to configure and broadcast document transfer transitions -pub struct DocumentTransferTransitionBuilder<'a> { - data_contract: &'a DataContract, - document_type: DocumentType, +pub struct DocumentTransferTransitionBuilder { + data_contract: Arc, + document_type_name: String, document: Document, recipient_id: Identifier, token_payment_info: Option, @@ -28,13 +28,13 @@ pub struct DocumentTransferTransitionBuilder<'a> { user_fee_increase: Option, } -impl<'a> DocumentTransferTransitionBuilder<'a> { +impl DocumentTransferTransitionBuilder { /// Start building a transfer document request for the provided DataContract. /// /// # Arguments /// - /// * `data_contract` - A reference to the data contract - /// * `document_type` - The document type + /// * `data_contract` - The data contract + /// * `document_type_name` - The name of the document type /// * `document` - The document to transfer /// * `recipient_id` - The identifier of the recipient /// @@ -42,14 +42,14 @@ impl<'a> DocumentTransferTransitionBuilder<'a> { /// /// * `Self` - The new builder instance pub fn new( - data_contract: &'a DataContract, - document_type: DocumentType, + data_contract: Arc, + document_type_name: String, document: Document, recipient_id: Identifier, ) -> Self { Self { data_contract, - document_type, + document_type_name, document, recipient_id, token_payment_info: None, @@ -62,8 +62,8 @@ impl<'a> DocumentTransferTransitionBuilder<'a> { /// /// # Arguments /// - /// * `data_contract` - A reference to the data contract - /// * `document_type` - The document type + /// * `data_contract` - The data contract + /// * `document_type_name` - The name of the document type /// * `document_id` - The ID of the document /// * `owner_id` - The current owner ID of the document /// * `recipient_id` - The identifier of the recipient @@ -72,8 +72,8 @@ impl<'a> DocumentTransferTransitionBuilder<'a> { /// /// * `Self` - The new builder instance pub fn from_document_info( - data_contract: &'a DataContract, - document_type: DocumentType, + data_contract: Arc, + document_type_name: String, document_id: Identifier, owner_id: Identifier, recipient_id: Identifier, @@ -96,7 +96,7 @@ impl<'a> DocumentTransferTransitionBuilder<'a> { transferred_at_core_block_height: None, }); - Self::new(data_contract, document_type, document, recipient_id) + Self::new(data_contract, document_type_name, document, recipient_id) } /// Adds token payment info to the document transfer transition @@ -171,9 +171,14 @@ impl<'a> DocumentTransferTransitionBuilder<'a> { ) .await?; + let document_type = self + .data_contract + .document_type_for_name(&self.document_type_name) + .map_err(|e| Error::Protocol(e.into()))?; + let state_transition = BatchTransition::new_document_transfer_transition_from_document( self.document.clone(), - self.document_type.as_ref(), + document_type, self.recipient_id, identity_public_key, identity_contract_nonce, @@ -222,7 +227,7 @@ impl Sdk { /// - Invalid recipient identity pub async fn document_transfer( &self, - transfer_document_transition_builder: DocumentTransferTransitionBuilder<'_>, + transfer_document_transition_builder: DocumentTransferTransitionBuilder, signing_key: &IdentityPublicKey, signer: &S, ) -> Result { From d87cf1154a95aa44020740e66d3062e214c29e0d Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Wed, 4 Jun 2025 10:14:45 +0200 Subject: [PATCH 3/3] more work --- .../platform/documents/transitions/create.rs | 42 ++++++++++++----- .../platform/documents/transitions/delete.rs | 43 +++++++++++++----- .../documents/transitions/purchase.rs | 45 +++++++++++++------ .../platform/documents/transitions/replace.rs | 41 ++++++++++++----- .../documents/transitions/set_price.rs | 43 +++++++++++++----- .../documents/transitions/transfer.rs | 43 +++++++++++++----- 6 files changed, 186 insertions(+), 71 deletions(-) diff --git a/packages/rs-sdk/src/platform/documents/transitions/create.rs b/packages/rs-sdk/src/platform/documents/transitions/create.rs index 6a74013305e..8c38cc45496 100644 --- a/packages/rs-sdk/src/platform/documents/transitions/create.rs +++ b/packages/rs-sdk/src/platform/documents/transitions/create.rs @@ -18,13 +18,14 @@ use std::sync::Arc; /// A builder to configure and broadcast document create transitions pub struct DocumentCreateTransitionBuilder { - data_contract: Arc, - document_type_name: String, - document: Document, - document_state_transition_entropy: [u8; 32], - token_payment_info: Option, - settings: Option, - user_fee_increase: Option, + pub data_contract: Arc, + pub document_type_name: String, + pub document: Document, + pub document_state_transition_entropy: [u8; 32], + pub token_payment_info: Option, + pub settings: Option, + pub user_fee_increase: Option, + pub state_transition_creation_options: Option, } impl DocumentCreateTransitionBuilder { @@ -54,6 +55,7 @@ impl DocumentCreateTransitionBuilder { token_payment_info: None, settings: None, user_fee_increase: None, + state_transition_creation_options: None, } } @@ -99,6 +101,23 @@ impl DocumentCreateTransitionBuilder { self } + /// Adds creation_options to the document create transition + /// + /// # Arguments + /// + /// * `settings` - The settings to add + /// + /// # Returns + /// + /// * `Self` - The updated builder + pub fn with_state_transition_creation_options( + mut self, + creation_options: StateTransitionCreationOptions, + ) -> Self { + self.state_transition_creation_options = Some(creation_options); + self + } + /// Signs the document create transition /// /// # Arguments @@ -118,7 +137,6 @@ impl DocumentCreateTransitionBuilder { identity_public_key: &IdentityPublicKey, signer: &impl Signer, platform_version: &PlatformVersion, - options: Option, ) -> Result { let identity_contract_nonce = sdk .get_identity_contract_nonce( @@ -144,7 +162,7 @@ impl DocumentCreateTransitionBuilder { self.token_payment_info, signer, platform_version, - options, + self.state_transition_creation_options, )?; Ok(state_transition) @@ -189,12 +207,14 @@ impl Sdk { ) -> Result { let platform_version = self.version(); + let put_settings = create_document_transition_builder.settings; + let state_transition = create_document_transition_builder - .sign(self, signing_key, signer, platform_version, None) + .sign(self, signing_key, signer, platform_version) .await?; let proof_result = state_transition - .broadcast_and_wait::(self, None) + .broadcast_and_wait::(self, put_settings) .await?; match proof_result { diff --git a/packages/rs-sdk/src/platform/documents/transitions/delete.rs b/packages/rs-sdk/src/platform/documents/transitions/delete.rs index ac91d407575..55ccf4d8371 100644 --- a/packages/rs-sdk/src/platform/documents/transitions/delete.rs +++ b/packages/rs-sdk/src/platform/documents/transitions/delete.rs @@ -19,13 +19,14 @@ use std::sync::Arc; /// A builder to configure and broadcast document delete transitions pub struct DocumentDeleteTransitionBuilder { - data_contract: Arc, - document_type_name: String, - document_id: Identifier, - owner_id: Identifier, - token_payment_info: Option, - settings: Option, - user_fee_increase: Option, + pub data_contract: Arc, + pub document_type_name: String, + pub document_id: Identifier, + pub owner_id: Identifier, + pub token_payment_info: Option, + pub settings: Option, + pub user_fee_increase: Option, + pub state_transition_creation_options: Option, } impl DocumentDeleteTransitionBuilder { @@ -55,6 +56,7 @@ impl DocumentDeleteTransitionBuilder { token_payment_info: None, settings: None, user_fee_increase: None, + state_transition_creation_options: None, } } @@ -125,6 +127,23 @@ impl DocumentDeleteTransitionBuilder { self } + /// Adds creation_options to the document delete transition + /// + /// # Arguments + /// + /// * `creation_options` - The creation options to add + /// + /// # Returns + /// + /// * `Self` - The updated builder + pub fn with_state_transition_creation_options( + mut self, + creation_options: StateTransitionCreationOptions, + ) -> Self { + self.state_transition_creation_options = Some(creation_options); + self + } + /// Signs the document delete transition /// /// # Arguments @@ -133,7 +152,6 @@ impl DocumentDeleteTransitionBuilder { /// * `identity_public_key` - The public key of the identity /// * `signer` - The signer instance /// * `platform_version` - The platform version - /// * `options` - Optional state transition creation options /// /// # Returns /// @@ -144,7 +162,6 @@ impl DocumentDeleteTransitionBuilder { identity_public_key: &IdentityPublicKey, signer: &impl Signer, platform_version: &PlatformVersion, - options: Option, ) -> Result { let identity_contract_nonce = sdk .get_identity_contract_nonce( @@ -186,7 +203,7 @@ impl DocumentDeleteTransitionBuilder { self.token_payment_info, signer, platform_version, - options, + self.state_transition_creation_options, )?; Ok(state_transition) @@ -232,12 +249,14 @@ impl Sdk { ) -> Result { let platform_version = self.version(); + let put_settings = delete_document_transition_builder.settings; + let state_transition = delete_document_transition_builder - .sign(self, signing_key, signer, platform_version, None) + .sign(self, signing_key, signer, platform_version) .await?; let proof_result = state_transition - .broadcast_and_wait::(self, None) + .broadcast_and_wait::(self, put_settings) .await?; match proof_result { diff --git a/packages/rs-sdk/src/platform/documents/transitions/purchase.rs b/packages/rs-sdk/src/platform/documents/transitions/purchase.rs index 42301154139..83ec7117ed8 100644 --- a/packages/rs-sdk/src/platform/documents/transitions/purchase.rs +++ b/packages/rs-sdk/src/platform/documents/transitions/purchase.rs @@ -20,14 +20,15 @@ use std::sync::Arc; /// A builder to configure and broadcast document purchase transitions pub struct DocumentPurchaseTransitionBuilder { - data_contract: Arc, - document_type_name: String, - document: Document, - purchaser_id: Identifier, - price: Credits, - token_payment_info: Option, - settings: Option, - user_fee_increase: Option, + pub data_contract: Arc, + pub document_type_name: String, + pub document: Document, + pub purchaser_id: Identifier, + pub price: Credits, + pub token_payment_info: Option, + pub settings: Option, + pub user_fee_increase: Option, + pub state_transition_creation_options: Option, } impl DocumentPurchaseTransitionBuilder { @@ -60,6 +61,7 @@ impl DocumentPurchaseTransitionBuilder { token_payment_info: None, settings: None, user_fee_increase: None, + state_transition_creation_options: None, } } @@ -154,6 +156,23 @@ impl DocumentPurchaseTransitionBuilder { self } + /// Adds creation_options to the document purchase transition + /// + /// # Arguments + /// + /// * `creation_options` - The creation options to add + /// + /// # Returns + /// + /// * `Self` - The updated builder + pub fn with_state_transition_creation_options( + mut self, + creation_options: StateTransitionCreationOptions, + ) -> Self { + self.state_transition_creation_options = Some(creation_options); + self + } + /// Signs the document purchase transition /// /// # Arguments @@ -162,7 +181,6 @@ impl DocumentPurchaseTransitionBuilder { /// * `identity_public_key` - The public key of the identity /// * `signer` - The signer instance /// * `platform_version` - The platform version - /// * `options` - Optional state transition creation options /// /// # Returns /// @@ -173,7 +191,6 @@ impl DocumentPurchaseTransitionBuilder { identity_public_key: &IdentityPublicKey, signer: &impl Signer, platform_version: &PlatformVersion, - options: Option, ) -> Result { let identity_contract_nonce = sdk .get_identity_contract_nonce( @@ -200,7 +217,7 @@ impl DocumentPurchaseTransitionBuilder { self.token_payment_info, signer, platform_version, - options, + self.state_transition_creation_options, )?; Ok(state_transition) @@ -249,12 +266,14 @@ impl Sdk { ) -> Result { let platform_version = self.version(); + let put_settings = purchase_document_transition_builder.settings; + let state_transition = purchase_document_transition_builder - .sign(self, signing_key, signer, platform_version, None) + .sign(self, signing_key, signer, platform_version) .await?; let proof_result = state_transition - .broadcast_and_wait::(self, None) + .broadcast_and_wait::(self, put_settings) .await?; match proof_result { diff --git a/packages/rs-sdk/src/platform/documents/transitions/replace.rs b/packages/rs-sdk/src/platform/documents/transitions/replace.rs index c98299886ba..097b305ddd0 100644 --- a/packages/rs-sdk/src/platform/documents/transitions/replace.rs +++ b/packages/rs-sdk/src/platform/documents/transitions/replace.rs @@ -18,12 +18,13 @@ use std::sync::Arc; /// A builder to configure and broadcast document replace transitions pub struct DocumentReplaceTransitionBuilder { - data_contract: Arc, - document_type_name: String, - document: Document, - token_payment_info: Option, - settings: Option, - user_fee_increase: Option, + pub data_contract: Arc, + pub document_type_name: String, + pub document: Document, + pub token_payment_info: Option, + pub settings: Option, + pub user_fee_increase: Option, + pub state_transition_creation_options: Option, } impl DocumentReplaceTransitionBuilder { @@ -50,6 +51,7 @@ impl DocumentReplaceTransitionBuilder { token_payment_info: None, settings: None, user_fee_increase: None, + state_transition_creation_options: None, } } @@ -95,6 +97,23 @@ impl DocumentReplaceTransitionBuilder { self } + /// Adds creation_options to the document replace transition + /// + /// # Arguments + /// + /// * `creation_options` - The creation options to add + /// + /// # Returns + /// + /// * `Self` - The updated builder + pub fn with_state_transition_creation_options( + mut self, + creation_options: StateTransitionCreationOptions, + ) -> Self { + self.state_transition_creation_options = Some(creation_options); + self + } + /// Signs the document replace transition /// /// # Arguments @@ -103,7 +122,6 @@ impl DocumentReplaceTransitionBuilder { /// * `identity_public_key` - The public key of the identity /// * `signer` - The signer instance /// * `platform_version` - The platform version - /// * `options` - Optional state transition creation options /// /// # Returns /// @@ -114,7 +132,6 @@ impl DocumentReplaceTransitionBuilder { identity_public_key: &IdentityPublicKey, signer: &impl Signer, platform_version: &PlatformVersion, - options: Option, ) -> Result { let identity_contract_nonce = sdk .get_identity_contract_nonce( @@ -139,7 +156,7 @@ impl DocumentReplaceTransitionBuilder { self.token_payment_info, signer, platform_version, - options, + self.state_transition_creation_options, )?; Ok(state_transition) @@ -185,12 +202,14 @@ impl Sdk { ) -> Result { let platform_version = self.version(); + let put_settings = replace_document_transition_builder.settings; + let state_transition = replace_document_transition_builder - .sign(self, signing_key, signer, platform_version, None) + .sign(self, signing_key, signer, platform_version) .await?; let proof_result = state_transition - .broadcast_and_wait::(self, None) + .broadcast_and_wait::(self, put_settings) .await?; match proof_result { diff --git a/packages/rs-sdk/src/platform/documents/transitions/set_price.rs b/packages/rs-sdk/src/platform/documents/transitions/set_price.rs index 92b199e548c..8b38a1ff24e 100644 --- a/packages/rs-sdk/src/platform/documents/transitions/set_price.rs +++ b/packages/rs-sdk/src/platform/documents/transitions/set_price.rs @@ -20,13 +20,14 @@ use std::sync::Arc; /// A builder to configure and broadcast document set price transitions pub struct DocumentSetPriceTransitionBuilder { - data_contract: Arc, - document_type_name: String, - document: Document, - price: Credits, - token_payment_info: Option, - settings: Option, - user_fee_increase: Option, + pub data_contract: Arc, + pub document_type_name: String, + pub document: Document, + pub price: Credits, + pub token_payment_info: Option, + pub settings: Option, + pub user_fee_increase: Option, + pub state_transition_creation_options: Option, } impl DocumentSetPriceTransitionBuilder { @@ -56,6 +57,7 @@ impl DocumentSetPriceTransitionBuilder { token_payment_info: None, settings: None, user_fee_increase: None, + state_transition_creation_options: None, } } @@ -142,6 +144,23 @@ impl DocumentSetPriceTransitionBuilder { self } + /// Adds creation_options to the document set price transition + /// + /// # Arguments + /// + /// * `creation_options` - The creation options to add + /// + /// # Returns + /// + /// * `Self` - The updated builder + pub fn with_state_transition_creation_options( + mut self, + creation_options: StateTransitionCreationOptions, + ) -> Self { + self.state_transition_creation_options = Some(creation_options); + self + } + /// Signs the document set price transition /// /// # Arguments @@ -150,7 +169,6 @@ impl DocumentSetPriceTransitionBuilder { /// * `identity_public_key` - The public key of the identity /// * `signer` - The signer instance /// * `platform_version` - The platform version - /// * `options` - Optional state transition creation options /// /// # Returns /// @@ -161,7 +179,6 @@ impl DocumentSetPriceTransitionBuilder { identity_public_key: &IdentityPublicKey, signer: &impl Signer, platform_version: &PlatformVersion, - options: Option, ) -> Result { let identity_contract_nonce = sdk .get_identity_contract_nonce( @@ -187,7 +204,7 @@ impl DocumentSetPriceTransitionBuilder { self.token_payment_info, signer, platform_version, - options, + self.state_transition_creation_options, )?; Ok(state_transition) @@ -234,12 +251,14 @@ impl Sdk { ) -> Result { let platform_version = self.version(); + let put_settings = set_price_document_transition_builder.settings; + let state_transition = set_price_document_transition_builder - .sign(self, signing_key, signer, platform_version, None) + .sign(self, signing_key, signer, platform_version) .await?; let proof_result = state_transition - .broadcast_and_wait::(self, None) + .broadcast_and_wait::(self, put_settings) .await?; match proof_result { diff --git a/packages/rs-sdk/src/platform/documents/transitions/transfer.rs b/packages/rs-sdk/src/platform/documents/transitions/transfer.rs index 01724e44651..f75237927c4 100644 --- a/packages/rs-sdk/src/platform/documents/transitions/transfer.rs +++ b/packages/rs-sdk/src/platform/documents/transitions/transfer.rs @@ -19,13 +19,14 @@ use std::sync::Arc; /// A builder to configure and broadcast document transfer transitions pub struct DocumentTransferTransitionBuilder { - data_contract: Arc, - document_type_name: String, - document: Document, - recipient_id: Identifier, - token_payment_info: Option, - settings: Option, - user_fee_increase: Option, + pub data_contract: Arc, + pub document_type_name: String, + pub document: Document, + pub recipient_id: Identifier, + pub token_payment_info: Option, + pub settings: Option, + pub user_fee_increase: Option, + pub state_transition_creation_options: Option, } impl DocumentTransferTransitionBuilder { @@ -55,6 +56,7 @@ impl DocumentTransferTransitionBuilder { token_payment_info: None, settings: None, user_fee_increase: None, + state_transition_creation_options: None, } } @@ -141,6 +143,23 @@ impl DocumentTransferTransitionBuilder { self } + /// Adds creation_options to the document transfer transition + /// + /// # Arguments + /// + /// * `creation_options` - The creation options to add + /// + /// # Returns + /// + /// * `Self` - The updated builder + pub fn with_state_transition_creation_options( + mut self, + creation_options: StateTransitionCreationOptions, + ) -> Self { + self.state_transition_creation_options = Some(creation_options); + self + } + /// Signs the document transfer transition /// /// # Arguments @@ -149,7 +168,6 @@ impl DocumentTransferTransitionBuilder { /// * `identity_public_key` - The public key of the identity /// * `signer` - The signer instance /// * `platform_version` - The platform version - /// * `options` - Optional state transition creation options /// /// # Returns /// @@ -160,7 +178,6 @@ impl DocumentTransferTransitionBuilder { identity_public_key: &IdentityPublicKey, signer: &impl Signer, platform_version: &PlatformVersion, - options: Option, ) -> Result { let identity_contract_nonce = sdk .get_identity_contract_nonce( @@ -186,7 +203,7 @@ impl DocumentTransferTransitionBuilder { self.token_payment_info, signer, platform_version, - options, + self.state_transition_creation_options, )?; Ok(state_transition) @@ -233,12 +250,14 @@ impl Sdk { ) -> Result { let platform_version = self.version(); + let put_settings = transfer_document_transition_builder.settings; + let state_transition = transfer_document_transition_builder - .sign(self, signing_key, signer, platform_version, None) + .sign(self, signing_key, signer, platform_version) .await?; let proof_result = state_transition - .broadcast_and_wait::(self, None) + .broadcast_and_wait::(self, put_settings) .await?; match proof_result {