@@ -5,13 +5,16 @@ use crate::convert::{
5
5
use crate :: disk:: FilesystemLogger ;
6
6
use crate :: hex_utils;
7
7
use base64;
8
+ use bitcoin:: address:: { Address , Payload , WitnessVersion } ;
8
9
use bitcoin:: blockdata:: constants:: WITNESS_SCALE_FACTOR ;
10
+ use bitcoin:: blockdata:: script:: ScriptBuf ;
9
11
use bitcoin:: blockdata:: transaction:: Transaction ;
10
12
use bitcoin:: consensus:: { encode, Decodable , Encodable } ;
11
13
use bitcoin:: hash_types:: { BlockHash , Txid } ;
12
14
use bitcoin:: hashes:: Hash ;
13
- use bitcoin:: util:: address:: { Address , Payload , WitnessVersion } ;
14
- use bitcoin:: { OutPoint , Script , TxOut , WPubkeyHash , XOnlyPublicKey } ;
15
+ use bitcoin:: key:: XOnlyPublicKey ;
16
+ use bitcoin:: psbt:: PartiallySignedTransaction ;
17
+ use bitcoin:: { Network , OutPoint , TxOut , WPubkeyHash } ;
15
18
use lightning:: chain:: chaininterface:: { BroadcasterInterface , ConfirmationTarget , FeeEstimator } ;
16
19
use lightning:: events:: bump_transaction:: { Utxo , WalletSource } ;
17
20
use lightning:: log_error;
@@ -28,6 +31,7 @@ use std::time::Duration;
28
31
29
32
pub struct BitcoindClient {
30
33
pub ( crate ) bitcoind_rpc_client : Arc < RpcClient > ,
34
+ network : Network ,
31
35
host : String ,
32
36
port : u16 ,
33
37
rpc_user : String ,
@@ -60,7 +64,7 @@ const MIN_FEERATE: u32 = 253;
60
64
61
65
impl BitcoindClient {
62
66
pub ( crate ) async fn new (
63
- host : String , port : u16 , rpc_user : String , rpc_password : String ,
67
+ host : String , port : u16 , rpc_user : String , rpc_password : String , network : Network ,
64
68
handle : tokio:: runtime:: Handle , logger : Arc < FilesystemLogger > ,
65
69
) -> std:: io:: Result < Self > {
66
70
let http_endpoint = HttpEndpoint :: for_host ( host. clone ( ) ) . with_port ( port) ;
@@ -76,10 +80,6 @@ impl BitcoindClient {
76
80
} ) ?;
77
81
let mut fees: HashMap < ConfirmationTarget , AtomicU32 > = HashMap :: new ( ) ;
78
82
fees. insert ( ConfirmationTarget :: OnChainSweep , AtomicU32 :: new ( 5000 ) ) ;
79
- fees. insert (
80
- ConfirmationTarget :: MaxAllowedNonAnchorChannelRemoteFee ,
81
- AtomicU32 :: new ( 25 * 250 ) ,
82
- ) ;
83
83
fees. insert (
84
84
ConfirmationTarget :: MinAllowedAnchorChannelRemoteFee ,
85
85
AtomicU32 :: new ( MIN_FEERATE ) ,
@@ -98,6 +98,7 @@ impl BitcoindClient {
98
98
port,
99
99
rpc_user,
100
100
rpc_password,
101
+ network,
101
102
fees : Arc :: new ( fees) ,
102
103
handle : handle. clone ( ) ,
103
104
logger,
@@ -178,9 +179,6 @@ impl BitcoindClient {
178
179
fees. get ( & ConfirmationTarget :: OnChainSweep )
179
180
. unwrap ( )
180
181
. store ( high_prio_estimate, Ordering :: Release ) ;
181
- fees. get ( & ConfirmationTarget :: MaxAllowedNonAnchorChannelRemoteFee )
182
- . unwrap ( )
183
- . store ( std:: cmp:: max ( 25 * 250 , high_prio_estimate * 10 ) , Ordering :: Release ) ;
184
182
fees. get ( & ConfirmationTarget :: MinAllowedAnchorChannelRemoteFee )
185
183
. unwrap ( )
186
184
. store ( mempoolmin_estimate, Ordering :: Release ) ;
@@ -264,7 +262,7 @@ impl BitcoindClient {
264
262
. call_method :: < NewAddress > ( "getnewaddress" , & addr_args)
265
263
. await
266
264
. unwrap ( ) ;
267
- Address :: from_str ( addr. 0 . as_str ( ) ) . unwrap ( )
265
+ Address :: from_str ( addr. 0 . as_str ( ) ) . unwrap ( ) . require_network ( self . network ) . unwrap ( )
268
266
}
269
267
270
268
pub async fn get_blockchain_info ( & self ) -> BlockchainInfo {
@@ -328,23 +326,38 @@ impl WalletSource for BitcoindClient {
328
326
. into_iter ( )
329
327
. filter_map ( |utxo| {
330
328
let outpoint = OutPoint { txid : utxo. txid , vout : utxo. vout } ;
331
- match utxo. address . payload {
332
- Payload :: WitnessProgram { version, ref program } => match version {
333
- WitnessVersion :: V0 => WPubkeyHash :: from_slice ( program)
334
- . map ( |wpkh| Utxo :: new_v0_p2wpkh ( outpoint, utxo. amount , & wpkh) )
335
- . ok ( ) ,
329
+ match utxo. address . payload . clone ( ) {
330
+ Payload :: WitnessProgram ( wp) => match wp. version ( ) {
331
+ WitnessVersion :: V0 => {
332
+ bitcoin:: hashes:: hash160:: Hash :: from_slice ( wp. program ( ) . as_bytes ( ) )
333
+ . map ( |hash| {
334
+ Utxo :: new_v0_p2wpkh (
335
+ outpoint,
336
+ utxo. amount ,
337
+ & WPubkeyHash :: from_raw_hash ( hash) ,
338
+ )
339
+ } )
340
+ . ok ( )
341
+ }
336
342
// TODO: Add `Utxo::new_v1_p2tr` upstream.
337
- WitnessVersion :: V1 => XOnlyPublicKey :: from_slice ( program)
338
- . map ( |_| Utxo {
339
- outpoint,
340
- output : TxOut {
341
- value : utxo. amount ,
342
- script_pubkey : Script :: new_witness_program ( version, program) ,
343
- } ,
344
- satisfaction_weight : 1 /* empty script_sig */ * WITNESS_SCALE_FACTOR as u64 +
345
- 1 /* witness items */ + 1 /* schnorr sig len */ + 64 , /* schnorr sig */
346
- } )
347
- . ok ( ) ,
343
+ WitnessVersion :: V1 => {
344
+ bitcoin:: hashes:: hash160:: Hash :: from_slice ( wp. program ( ) . as_bytes ( ) )
345
+ . map ( |wp_hash| {
346
+ let _pubkey =
347
+ XOnlyPublicKey :: from_slice ( wp_hash. as_byte_array ( ) )
348
+ . expect ( "Invalid pubkey length" ) ;
349
+ Utxo {
350
+ outpoint,
351
+ output : TxOut {
352
+ value : utxo. amount ,
353
+ script_pubkey : ScriptBuf :: new_witness_program ( & wp) ,
354
+ } ,
355
+ satisfaction_weight : 1 /* empty script_sig */ * WITNESS_SCALE_FACTOR as u64 +
356
+ 1 /* witness items */ + 1 /* schnorr sig len */ + 64 , /* schnorr sig */
357
+ }
358
+ } )
359
+ . ok ( )
360
+ }
348
361
_ => None ,
349
362
} ,
350
363
_ => None ,
@@ -353,15 +366,15 @@ impl WalletSource for BitcoindClient {
353
366
. collect ( ) )
354
367
}
355
368
356
- fn get_change_script ( & self ) -> Result < Script , ( ) > {
369
+ fn get_change_script ( & self ) -> Result < ScriptBuf , ( ) > {
357
370
tokio:: task:: block_in_place ( move || {
358
371
Ok ( self . handle . block_on ( async move { self . get_new_address ( ) . await . script_pubkey ( ) } ) )
359
372
} )
360
373
}
361
374
362
- fn sign_tx ( & self , tx : Transaction ) -> Result < Transaction , ( ) > {
375
+ fn sign_psbt ( & self , tx : PartiallySignedTransaction ) -> Result < Transaction , ( ) > {
363
376
let mut tx_bytes = Vec :: new ( ) ;
364
- let _ = tx. consensus_encode ( & mut tx_bytes) . map_err ( |_| ( ) ) ;
377
+ let _ = tx. unsigned_tx . consensus_encode ( & mut tx_bytes) . map_err ( |_| ( ) ) ;
365
378
let tx_hex = hex_utils:: hex_str ( & tx_bytes) ;
366
379
let signed_tx = tokio:: task:: block_in_place ( move || {
367
380
self . handle . block_on ( async move { self . sign_raw_transaction_with_wallet ( tx_hex) . await } )
0 commit comments