@@ -185,6 +185,15 @@ impl WasmSdk {
185185
186186 Ok ( result_obj. into ( ) )
187187 }
188+
189+ /// Get the next revision for a document, handling errors for missing revisions and overflow
190+ fn get_next_revision ( document : & dash_sdk:: platform:: Document ) -> Result < u64 , JsValue > {
191+ let current_revision = document. revision ( )
192+ . ok_or_else ( || JsValue :: from_str ( "Document revision is missing" ) ) ?;
193+
194+ current_revision. checked_add ( 1 )
195+ . ok_or_else ( || JsValue :: from_str ( "Document revision overflow" ) )
196+ }
188197}
189198
190199#[ wasm_bindgen]
@@ -835,7 +844,8 @@ impl WasmSdk {
835844 . map_err ( |e| JsValue :: from_str ( & format ! ( "Failed to fetch document: {}" , e) ) ) ?
836845 . ok_or_else ( || JsValue :: from_str ( "Document not found" ) ) ?;
837846
838- let current_revision = existing_doc. revision ( ) . unwrap_or ( 0 ) ;
847+ let current_revision = existing_doc. revision ( )
848+ . ok_or_else ( || JsValue :: from_str ( "Document revision is missing" ) ) ?;
839849
840850 // Fetch the identity to get the correct key
841851 let identity = dash_sdk:: platform:: Identity :: fetch ( & sdk, owner_identifier)
@@ -965,6 +975,26 @@ impl WasmSdk {
965975 . map_err ( |e| JsValue :: from_str ( & format ! ( "Failed to fetch document: {}" , e) ) ) ?
966976 . ok_or_else ( || JsValue :: from_str ( "Document not found" ) ) ?;
967977
978+ // Get the current revision and increment it
979+ let next_revision = Self :: get_next_revision ( & document) ?;
980+
981+ // Create a modified document with incremented revision for the transfer transition
982+ let transfer_document = Document :: V0 ( DocumentV0 {
983+ id : document. id ( ) ,
984+ owner_id : document. owner_id ( ) ,
985+ properties : document. properties ( ) . clone ( ) ,
986+ revision : Some ( next_revision) ,
987+ created_at : document. created_at ( ) ,
988+ updated_at : document. updated_at ( ) ,
989+ transferred_at : document. transferred_at ( ) ,
990+ created_at_block_height : document. created_at_block_height ( ) ,
991+ updated_at_block_height : document. updated_at_block_height ( ) ,
992+ transferred_at_block_height : document. transferred_at_block_height ( ) ,
993+ created_at_core_block_height : document. created_at_core_block_height ( ) ,
994+ updated_at_core_block_height : document. updated_at_core_block_height ( ) ,
995+ transferred_at_core_block_height : document. transferred_at_core_block_height ( ) ,
996+ } ) ;
997+
968998 // Fetch the identity to get the correct key
969999 let identity = dash_sdk:: platform:: Identity :: fetch ( & sdk, owner_identifier)
9701000 . await
@@ -983,7 +1013,7 @@ impl WasmSdk {
9831013
9841014 // Create a transfer transition
9851015 let transition = BatchTransition :: new_document_transfer_transition_from_document (
986- document ,
1016+ transfer_document ,
9871017 document_type_ref,
9881018 recipient_identifier,
9891019 matching_key,
@@ -1089,6 +1119,26 @@ impl WasmSdk {
10891119 ) ) ) ;
10901120 }
10911121
1122+ // Get the current revision and increment it
1123+ let next_revision = Self :: get_next_revision ( & document) ?;
1124+
1125+ // Create a modified document with incremented revision for the purchase transition
1126+ let purchase_document = Document :: V0 ( DocumentV0 {
1127+ id : document. id ( ) ,
1128+ owner_id : document. owner_id ( ) ,
1129+ properties : document. properties ( ) . clone ( ) ,
1130+ revision : Some ( next_revision) ,
1131+ created_at : document. created_at ( ) ,
1132+ updated_at : document. updated_at ( ) ,
1133+ transferred_at : document. transferred_at ( ) ,
1134+ created_at_block_height : document. created_at_block_height ( ) ,
1135+ updated_at_block_height : document. updated_at_block_height ( ) ,
1136+ transferred_at_block_height : document. transferred_at_block_height ( ) ,
1137+ created_at_core_block_height : document. created_at_core_block_height ( ) ,
1138+ updated_at_core_block_height : document. updated_at_core_block_height ( ) ,
1139+ transferred_at_core_block_height : document. transferred_at_core_block_height ( ) ,
1140+ } ) ;
1141+
10921142 // Fetch buyer identity
10931143 let buyer_identity = dash_sdk:: platform:: Identity :: fetch ( & sdk, buyer_identifier)
10941144 . await
@@ -1107,7 +1157,7 @@ impl WasmSdk {
11071157
11081158 // Create document purchase transition
11091159 let transition = BatchTransition :: new_document_purchase_transition_from_document (
1110- document . into ( ) ,
1160+ purchase_document ,
11111161 document_type_ref,
11121162 buyer_identifier,
11131163 price as Credits ,
@@ -1226,25 +1276,15 @@ impl WasmSdk {
12261276 return Err ( JsValue :: from_str ( "Only the document owner can set its price" ) ) ;
12271277 }
12281278
1229- // Get existing document properties and convert to mutable map
1230- let mut properties = existing_doc. properties ( ) . clone ( ) ;
1231-
1232- // Update the price in the document properties
1233- let price_value = if price > 0 {
1234- PlatformValue :: U64 ( price)
1235- } else {
1236- PlatformValue :: Null
1237- } ;
1279+ // Get the current revision and increment it
1280+ let next_revision = Self :: get_next_revision ( & existing_doc) ?;
12381281
1239- properties. insert ( "$price" . to_string ( ) , price_value) ;
1240-
1241- // Create updated document with new properties
1242- let new_revision = existing_doc. revision ( ) . unwrap_or ( 0 ) + 1 ;
1243- let updated_doc = Document :: V0 ( DocumentV0 {
1244- id : doc_id,
1245- owner_id : owner_identifier,
1246- properties,
1247- revision : Some ( new_revision) ,
1282+ // Create a modified document with incremented revision for the price update transition
1283+ let price_update_document = Document :: V0 ( DocumentV0 {
1284+ id : existing_doc. id ( ) ,
1285+ owner_id : existing_doc. owner_id ( ) ,
1286+ properties : existing_doc. properties ( ) . clone ( ) ,
1287+ revision : Some ( next_revision) ,
12481288 created_at : existing_doc. created_at ( ) ,
12491289 updated_at : existing_doc. updated_at ( ) ,
12501290 transferred_at : existing_doc. transferred_at ( ) ,
@@ -1272,30 +1312,20 @@ impl WasmSdk {
12721312 . await
12731313 . map_err ( |e| JsValue :: from_str ( & format ! ( "Failed to fetch nonce: {}" , e) ) ) ?;
12741314
1275- // Generate entropy for the state transition
1276- let entropy_bytes = {
1277- let mut entropy = [ 0u8 ; 32 ] ;
1278- if let Some ( window) = web_sys:: window ( ) {
1279- if let Ok ( crypto) = window. crypto ( ) {
1280- let _ = crypto. get_random_values_with_u8_array ( & mut entropy) ;
1281- }
1282- }
1283- entropy
1284- } ;
1285-
1286- // Create the price update transition
1287- let transition = BatchTransition :: new_document_replacement_transition_from_document (
1288- updated_doc,
1315+ // Create the price update transition using the dedicated method
1316+ let transition = BatchTransition :: new_document_update_price_transition_from_document (
1317+ price_update_document,
12891318 document_type_ref,
1290- matching_key,
1319+ price,
1320+ & matching_key,
12911321 identity_contract_nonce,
12921322 UserFeeIncrease :: default ( ) ,
12931323 None , // token_payment_info
12941324 & signer,
12951325 sdk. version ( ) ,
12961326 None , // options
12971327 )
1298- . map_err ( |e| JsValue :: from_str ( & format ! ( "Failed to create transition: {}" , e) ) ) ?;
1328+ . map_err ( |e| JsValue :: from_str ( & format ! ( "Failed to create price update transition: {}" , e) ) ) ?;
12991329
13001330 // The transition is already signed, convert to StateTransition
13011331 let state_transition: StateTransition = transition. into ( ) ;
0 commit comments