@@ -2,9 +2,11 @@ use super::broadcast::BroadcastStateTransition;
22use  super :: waitable:: Waitable ; 
33use  crate :: platform:: transition:: put_settings:: PutSettings ; 
44use  crate :: { Error ,  Sdk } ; 
5+ use  dpp:: dashcore:: secp256k1:: rand:: rngs:: StdRng ; 
6+ use  dpp:: dashcore:: secp256k1:: rand:: { Rng ,  SeedableRng } ; 
57use  dpp:: data_contract:: document_type:: accessors:: DocumentTypeV0Getters ; 
68use  dpp:: data_contract:: document_type:: DocumentType ; 
7- use  dpp:: document:: { Document ,  DocumentV0Getters } ; 
9+ use  dpp:: document:: { Document ,  DocumentV0Getters ,   DocumentV0Setters ,   INITIAL_REVISION } ; 
810use  dpp:: identity:: signer:: Signer ; 
911use  dpp:: identity:: IdentityPublicKey ; 
1012use  dpp:: state_transition:: batch_transition:: methods:: v0:: DocumentsBatchTransitionMethodsV0 ; 
@@ -22,7 +24,7 @@ pub trait PutDocument<S: Signer>: Waitable {
2224        & self , 
2325        sdk :  & Sdk , 
2426        document_type :  DocumentType , 
25-         document_state_transition_entropy :  [ u8 ;  32 ] , 
27+         document_state_transition_entropy :  Option < [ u8 ;  32 ] > , 
2628        identity_public_key :  IdentityPublicKey , 
2729        token_payment_info :  Option < TokenPaymentInfo > , 
2830        signer :  & S , 
@@ -35,7 +37,7 @@ pub trait PutDocument<S: Signer>: Waitable {
3537        & self , 
3638        sdk :  & Sdk , 
3739        document_type :  DocumentType , 
38-         document_state_transition_entropy :  [ u8 ;  32 ] , 
40+         document_state_transition_entropy :  Option < [ u8 ;  32 ] > , 
3941        identity_public_key :  IdentityPublicKey , 
4042        token_payment_info :  Option < TokenPaymentInfo > , 
4143        signer :  & S , 
@@ -49,7 +51,7 @@ impl<S: Signer> PutDocument<S> for Document {
4951        & self , 
5052        sdk :  & Sdk , 
5153        document_type :  DocumentType , 
52-         document_state_transition_entropy :  [ u8 ;  32 ] , 
54+         document_state_transition_entropy :  Option < [ u8 ;  32 ] > , 
5355        identity_public_key :  IdentityPublicKey , 
5456        token_payment_info :  Option < TokenPaymentInfo > , 
5557        signer :  & S , 
@@ -65,19 +67,48 @@ impl<S: Signer> PutDocument<S> for Document {
6567            . await ?; 
6668
6769        let  settings = settings. unwrap_or_default ( ) ; 
68- 
69-         let  transition = BatchTransition :: new_document_creation_transition_from_document ( 
70-             self . clone ( ) , 
71-             document_type. as_ref ( ) , 
72-             document_state_transition_entropy, 
73-             & identity_public_key, 
74-             new_identity_contract_nonce, 
75-             settings. user_fee_increase . unwrap_or_default ( ) , 
76-             token_payment_info, 
77-             signer, 
78-             sdk. version ( ) , 
79-             settings. state_transition_creation_options , 
80-         ) ?; 
70+         let  transition = if  self . revision ( ) . is_some ( ) 
71+             && self . revision ( ) . unwrap ( )  != INITIAL_REVISION 
72+         { 
73+             BatchTransition :: new_document_replacement_transition_from_document ( 
74+                 self . clone ( ) , 
75+                 document_type. as_ref ( ) , 
76+                 & identity_public_key, 
77+                 new_identity_contract_nonce, 
78+                 settings. user_fee_increase . unwrap_or_default ( ) , 
79+                 token_payment_info, 
80+                 signer, 
81+                 sdk. version ( ) , 
82+                 settings. state_transition_creation_options , 
83+             ) 
84+         }  else  { 
85+             let  ( document,  document_state_transition_entropy)  = document_state_transition_entropy
86+                 . map ( |entropy| ( self . clone ( ) ,  entropy) ) 
87+                 . unwrap_or_else ( || { 
88+                     let  mut  rng = StdRng :: from_entropy ( ) ; 
89+                     let  mut  document = self . clone ( ) ; 
90+                     let  entropy = rng. gen :: < [ u8 ;  32 ] > ( ) ; 
91+                     document. set_id ( Document :: generate_document_id_v0 ( 
92+                         & document_type. data_contract_id ( ) , 
93+                         & document. owner_id ( ) , 
94+                         document_type. name ( ) , 
95+                         entropy. as_slice ( ) , 
96+                     ) ) ; 
97+                     ( document,  entropy) 
98+                 } ) ; 
99+             BatchTransition :: new_document_creation_transition_from_document ( 
100+                 document, 
101+                 document_type. as_ref ( ) , 
102+                 document_state_transition_entropy, 
103+                 & identity_public_key, 
104+                 new_identity_contract_nonce, 
105+                 settings. user_fee_increase . unwrap_or_default ( ) , 
106+                 token_payment_info, 
107+                 signer, 
108+                 sdk. version ( ) , 
109+                 settings. state_transition_creation_options , 
110+             ) 
111+         } ?; 
81112
82113        // response is empty for a broadcast, result comes from the stream wait for state transition result 
83114        transition. broadcast ( sdk,  Some ( settings) ) . await ?; 
@@ -88,7 +119,7 @@ impl<S: Signer> PutDocument<S> for Document {
88119        & self , 
89120        sdk :  & Sdk , 
90121        document_type :  DocumentType , 
91-         document_state_transition_entropy :  [ u8 ;  32 ] , 
122+         document_state_transition_entropy :  Option < [ u8 ;  32 ] > , 
92123        identity_public_key :  IdentityPublicKey , 
93124        token_payment_info :  Option < TokenPaymentInfo > , 
94125        signer :  & S , 
0 commit comments