6
6
use super :: Client ;
7
7
use crate :: ledger:: Ledger ;
8
8
use bitcoin:: {
9
- address:: NetworkChecked , consensus:: encode, hashes:: Hash , Address , Amount , BlockHash ,
10
- SignedAmount , Transaction , TxIn , Wtxid ,
9
+ address:: NetworkChecked , consensus:: encode, hashes:: Hash , params :: Params , Address , Amount ,
10
+ BlockHash , SignedAmount , Transaction , TxIn , Wtxid ,
11
11
} ;
12
12
use bitcoincore_rpc:: {
13
13
json:: {
@@ -86,6 +86,28 @@ impl RpcApi for Client {
86
86
) -> bitcoincore_rpc:: Result < json:: GetTransactionResult > {
87
87
let raw_tx = self . get_raw_transaction ( txid, None ) . unwrap ( ) ;
88
88
89
+ let details: Vec < GetTransactionResultDetail > = raw_tx
90
+ . output
91
+ . iter ( )
92
+ . map ( |utxo| GetTransactionResultDetail {
93
+ address : Some (
94
+ Address :: from_script (
95
+ & utxo. script_pubkey ,
96
+ Params :: new ( bitcoin:: Network :: Regtest ) ,
97
+ )
98
+ . unwrap ( )
99
+ . as_unchecked ( )
100
+ . clone ( ) ,
101
+ ) ,
102
+ category : GetTransactionResultDetailCategory :: Send ,
103
+ amount : SignedAmount :: from_sat ( utxo. value . to_sat ( ) as i64 ) ,
104
+ label : None ,
105
+ vout : 0 ,
106
+ fee : None ,
107
+ abandoned : None ,
108
+ } )
109
+ . collect ( ) ;
110
+
89
111
let res = GetTransactionResult {
90
112
info : WalletTxInfo {
91
113
confirmations : i32:: MAX ,
@@ -101,15 +123,7 @@ impl RpcApi for Client {
101
123
} ,
102
124
amount : SignedAmount :: from_sat ( raw_tx. output [ 0 ] . value . to_sat ( ) as i64 ) ,
103
125
fee : None ,
104
- details : vec ! [ GetTransactionResultDetail {
105
- address: None ,
106
- category: GetTransactionResultDetailCategory :: Send ,
107
- amount: SignedAmount :: from_sat( raw_tx. output[ 0 ] . value. to_sat( ) as i64 ) ,
108
- label: None ,
109
- vout: 0 ,
110
- fee: None ,
111
- abandoned: None ,
112
- } ] ,
126
+ details,
113
127
hex : encode:: serialize ( & raw_tx) ,
114
128
} ;
115
129
@@ -201,6 +215,41 @@ impl RpcApi for Client {
201
215
) -> bitcoincore_rpc:: Result < Amount > {
202
216
Ok ( self . ledger . calculate_balance ( ) ?)
203
217
}
218
+
219
+ fn list_unspent (
220
+ & self ,
221
+ _minconf : Option < usize > ,
222
+ _maxconf : Option < usize > ,
223
+ _addresses : Option < & [ & Address < NetworkChecked > ] > ,
224
+ _include_unsafe : Option < bool > ,
225
+ _query_options : Option < json:: ListUnspentQueryOptions > ,
226
+ ) -> bitcoincore_rpc:: Result < Vec < json:: ListUnspentResultEntry > > {
227
+ let utxos = self . ledger . get_utxos ( ) ;
228
+
229
+ Ok ( utxos
230
+ . iter ( )
231
+ . map ( |utxo| {
232
+ let tx = self . ledger . get_transaction ( utxo. txid ) . unwrap ( ) ;
233
+ let output = tx. output . get ( utxo. vout as usize ) . unwrap ( ) ;
234
+
235
+ json:: ListUnspentResultEntry {
236
+ txid : utxo. txid ,
237
+ vout : utxo. vout ,
238
+ address : None ,
239
+ label : None ,
240
+ redeem_script : None ,
241
+ witness_script : None ,
242
+ script_pub_key : output. script_pubkey . clone ( ) ,
243
+ amount : output. value ,
244
+ confirmations : 101 ,
245
+ spendable : true ,
246
+ solvable : true ,
247
+ descriptor : None ,
248
+ safe : true ,
249
+ }
250
+ } )
251
+ . collect ( ) )
252
+ }
204
253
}
205
254
206
255
#[ cfg( test) ]
0 commit comments