@@ -12,6 +12,58 @@ use super::{
1212 } ,
1313} ;
1414
15+ ///
16+ /// ###### Example:
17+ ///
18+ /// You can create a Bitcoin transaction directly in your NEAR contract or Rust client
19+ /// or you can do it from a JSON.
20+ ///
21+ /// ```rust
22+ /// // The first case would be as follows:
23+ /// let omni_tx = BitcoinTransaction {
24+ /// version: Version::One,
25+ /// lock_time: LockTime::from_height(1000000).unwrap(),
26+ /// input: vec![TxIn {
27+ /// previous_output: OutPoint {
28+ /// txid: Txid(Hash::all_zeros()),
29+ /// vout: 0,
30+ /// },
31+ /// script_sig: ScriptBuf::default(),
32+ /// sequence: Sequence::default(),
33+ /// witness: Witness::default(),
34+ /// }],
35+ /// output: vec![TxOut {
36+ /// value: Amount::from_sat(10000),
37+ /// script_pubkey: ScriptBuf::default(),
38+ /// }],
39+ /// };
40+ ///
41+ /// // If you prefer to do it from a JSON:
42+ /// let json_value = r#"
43+ /// {
44+ /// "version": "1",
45+ /// "lock_time": "0",
46+ /// "input": [{
47+ /// "previous_output": {
48+ /// "txid": "bc25cc0dddd0a202c21e66521a692c0586330a9a9dcc38ccd9b4d2093037f31a",
49+ /// "vout": 0
50+ /// },
51+ /// "script_sig": [],
52+ /// "sequence": 4294967295,
53+ /// "witness": []
54+ /// }],s
55+ /// "output": [{
56+ /// "value": 1,
57+ /// "script_pubkey": "76a9148356ecd5f1761e60c144dc2f4de6bf7d8be7690688ad"
58+ /// },
59+ /// {
60+ /// "value": 2649,
61+ /// "script_pubkey": "76a9148356ecd5f1761e60c144dc2f4de6bf7d8be7690688ac"
62+ /// }]
63+ /// }
64+ /// "#;
65+ /// let tx = BitcoinTransaction::from_json(json_value).unwrap();
66+ ///
1567#[ derive(
1668 Debug ,
1769 Clone ,
@@ -48,7 +100,7 @@ fn sha256d(data: &[u8]) -> Vec<u8> {
48100}
49101
50102impl BitcoinTransaction {
51- // Common
103+ /// Encode the transaction into a vector of bytes
52104 pub fn serialize ( & self ) -> Vec < u8 > {
53105 let mut buffer = Vec :: new ( ) ;
54106
@@ -57,7 +109,7 @@ impl BitcoinTransaction {
57109 buffer
58110 }
59111
60- // Legacy
112+ /// Encode a legacy transaction into a vector of bytes
61113 pub fn build_for_signing_legacy ( & self , sighash_type : EcdsaSighashType ) -> Vec < u8 > {
62114 let mut buffer = Vec :: new ( ) ;
63115
@@ -69,6 +121,7 @@ impl BitcoinTransaction {
69121 buffer
70122 }
71123
124+ /// Attach a script sig to the transaction
72125 pub fn build_with_script_sig (
73126 & mut self ,
74127 input_index : usize ,
@@ -90,7 +143,7 @@ impl BitcoinTransaction {
90143 buffer
91144 }
92145
93- // Segwit
146+ /// Encode the transaction for signing in SegWit format
94147 pub fn build_for_signing_segwit (
95148 & self ,
96149 sighash_type : EcdsaSighashType ,
@@ -112,6 +165,7 @@ impl BitcoinTransaction {
112165 buffer
113166 }
114167
168+ /// Function to attach a witness to the transaction
115169 pub fn build_with_witness (
116170 & mut self ,
117171 input_index : usize ,
@@ -206,6 +260,7 @@ impl BitcoinTransaction {
206260 self . input . is_empty ( )
207261 }
208262
263+ /// Serialise a JSON representation of the transaction into a BitcoinTransaction struct
209264 pub fn from_json ( json : & str ) -> Result < Self , near_sdk:: serde_json:: Error > {
210265 let tx: Self = near_sdk:: serde_json:: from_str ( json) ?;
211266 Ok ( tx)
@@ -437,7 +492,6 @@ mod tests {
437492 } ;
438493
439494 let serialized = omni_tx. build_for_signing_legacy ( OmniSighashType :: All ) ;
440- println ! ( "serialized BTC Omni: {:?}" , serialized) ;
441495
442496 assert_eq ! ( buffer. len( ) , serialized. len( ) ) ;
443497 assert_eq ! ( buffer, serialized) ;
0 commit comments