@@ -38,6 +38,22 @@ impl RpcApi for Client {
38
38
) ;
39
39
}
40
40
41
+ fn send_raw_transaction < R : bitcoincore_rpc:: RawTx > (
42
+ & self ,
43
+ tx : R ,
44
+ ) -> bitcoincore_rpc:: Result < bitcoin:: Txid > {
45
+ let tx: Transaction = encode:: deserialize_hex ( & tx. raw_hex ( ) ) . unwrap ( ) ;
46
+
47
+ if let Err ( e) = self . ledger . check_transaction ( tx. clone ( ) ) {
48
+ return Err ( bitcoincore_rpc:: Error :: Io ( std:: io:: Error :: other ( format ! (
49
+ "{e}"
50
+ ) ) ) ) ;
51
+ }
52
+
53
+ self . ledger . add_transaction_unconditionally ( tx. clone ( ) ) ;
54
+
55
+ Ok ( tx. compute_txid ( ) )
56
+ }
41
57
fn get_raw_transaction (
42
58
& self ,
43
59
txid : & bitcoin:: Txid ,
@@ -67,22 +83,6 @@ impl RpcApi for Client {
67
83
blocktime : None ,
68
84
} )
69
85
}
70
- fn send_raw_transaction < R : bitcoincore_rpc:: RawTx > (
71
- & self ,
72
- tx : R ,
73
- ) -> bitcoincore_rpc:: Result < bitcoin:: Txid > {
74
- let tx: Transaction = encode:: deserialize_hex ( & tx. raw_hex ( ) ) . unwrap ( ) ;
75
-
76
- self . ledger . add_transaction_unconditionally ( tx. clone ( ) ) ;
77
-
78
- if !self . ledger . check_transaction ( tx. clone ( ) ) {
79
- return Err ( bitcoincore_rpc:: Error :: Io ( std:: io:: Error :: other (
80
- "Transaction not valid." ,
81
- ) ) ) ;
82
- }
83
-
84
- Ok ( tx. compute_txid ( ) )
85
- }
86
86
87
87
fn get_transaction (
88
88
& self ,
@@ -207,19 +207,24 @@ mod tests {
207
207
fn raw_transaction ( ) {
208
208
let rpc = Client :: new ( "" , bitcoincore_rpc:: Auth :: None ) . unwrap ( ) ;
209
209
210
+ // Get some BTC.
211
+ let address = rpc. get_new_address ( None , None ) . unwrap ( ) ;
212
+ rpc. generate_to_address ( 101 , address. assume_checked_ref ( ) )
213
+ . unwrap ( ) ;
214
+ let prev_tx = rpc. ledger . _get_transactions ( ) . get ( 0 ) . unwrap ( ) . to_owned ( ) ;
215
+
210
216
// Insert raw transactions to Bitcoin.
211
217
let txin = TxIn {
212
218
previous_output : OutPoint {
213
- txid : Txid :: from_byte_array ( [ 0x45 ; 32 ] ) ,
219
+ txid : prev_tx . compute_txid ( ) ,
214
220
vout : 0 ,
215
221
} ,
216
- sequence : bitcoin:: transaction:: Sequence :: ENABLE_RBF_NO_LOCKTIME ,
217
- script_sig : ScriptBuf :: default ( ) ,
218
- witness : Witness :: new ( ) ,
222
+ ..Default :: default ( )
219
223
} ;
224
+ println ! ( "----- {:?}" , txin. clone( ) ) ;
220
225
let txout = TxOut {
221
226
value : Amount :: from_sat ( 0x1F ) ,
222
- script_pubkey : test_common :: get_temp_address ( ) . script_pubkey ( ) ,
227
+ script_pubkey : address . assume_checked ( ) . script_pubkey ( ) ,
223
228
} ;
224
229
let inserted_tx1 = test_common:: create_transaction ( vec ! [ txin] , vec ! [ txout] ) ;
225
230
rpc. send_raw_transaction ( & inserted_tx1) . unwrap ( ) ;
0 commit comments