@@ -5,13 +5,16 @@ use crate::convert::{
55use crate :: disk:: FilesystemLogger ;
66use crate :: hex_utils;
77use base64;
8+ use bitcoin:: address:: { Address , Payload , WitnessVersion } ;
89use bitcoin:: blockdata:: constants:: WITNESS_SCALE_FACTOR ;
10+ use bitcoin:: blockdata:: script:: ScriptBuf ;
911use bitcoin:: blockdata:: transaction:: Transaction ;
1012use bitcoin:: consensus:: { encode, Decodable , Encodable } ;
1113use bitcoin:: hash_types:: { BlockHash , Txid } ;
1214use 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 } ;
1518use lightning:: chain:: chaininterface:: { BroadcasterInterface , ConfirmationTarget , FeeEstimator } ;
1619use lightning:: events:: bump_transaction:: { Utxo , WalletSource } ;
1720use lightning:: log_error;
@@ -28,6 +31,7 @@ use std::time::Duration;
2831
2932pub struct BitcoindClient {
3033 pub ( crate ) bitcoind_rpc_client : Arc < RpcClient > ,
34+ network : Network ,
3135 host : String ,
3236 port : u16 ,
3337 rpc_user : String ,
@@ -60,7 +64,7 @@ const MIN_FEERATE: u32 = 253;
6064
6165impl BitcoindClient {
6266 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 ,
6468 handle : tokio:: runtime:: Handle , logger : Arc < FilesystemLogger > ,
6569 ) -> std:: io:: Result < Self > {
6670 let http_endpoint = HttpEndpoint :: for_host ( host. clone ( ) ) . with_port ( port) ;
@@ -76,10 +80,6 @@ impl BitcoindClient {
7680 } ) ?;
7781 let mut fees: HashMap < ConfirmationTarget , AtomicU32 > = HashMap :: new ( ) ;
7882 fees. insert ( ConfirmationTarget :: OnChainSweep , AtomicU32 :: new ( 5000 ) ) ;
79- fees. insert (
80- ConfirmationTarget :: MaxAllowedNonAnchorChannelRemoteFee ,
81- AtomicU32 :: new ( 25 * 250 ) ,
82- ) ;
8383 fees. insert (
8484 ConfirmationTarget :: MinAllowedAnchorChannelRemoteFee ,
8585 AtomicU32 :: new ( MIN_FEERATE ) ,
@@ -98,6 +98,7 @@ impl BitcoindClient {
9898 port,
9999 rpc_user,
100100 rpc_password,
101+ network,
101102 fees : Arc :: new ( fees) ,
102103 handle : handle. clone ( ) ,
103104 logger,
@@ -178,9 +179,6 @@ impl BitcoindClient {
178179 fees. get ( & ConfirmationTarget :: OnChainSweep )
179180 . unwrap ( )
180181 . 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 ) ;
184182 fees. get ( & ConfirmationTarget :: MinAllowedAnchorChannelRemoteFee )
185183 . unwrap ( )
186184 . store ( mempoolmin_estimate, Ordering :: Release ) ;
@@ -264,7 +262,7 @@ impl BitcoindClient {
264262 . call_method :: < NewAddress > ( "getnewaddress" , & addr_args)
265263 . await
266264 . unwrap ( ) ;
267- Address :: from_str ( addr. 0 . as_str ( ) ) . unwrap ( )
265+ Address :: from_str ( addr. 0 . as_str ( ) ) . unwrap ( ) . require_network ( self . network ) . unwrap ( )
268266 }
269267
270268 pub async fn get_blockchain_info ( & self ) -> BlockchainInfo {
@@ -328,23 +326,38 @@ impl WalletSource for BitcoindClient {
328326 . into_iter ( )
329327 . filter_map ( |utxo| {
330328 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+ }
336342 // 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+ }
348361 _ => None ,
349362 } ,
350363 _ => None ,
@@ -353,15 +366,15 @@ impl WalletSource for BitcoindClient {
353366 . collect ( ) )
354367 }
355368
356- fn get_change_script ( & self ) -> Result < Script , ( ) > {
369+ fn get_change_script ( & self ) -> Result < ScriptBuf , ( ) > {
357370 tokio:: task:: block_in_place ( move || {
358371 Ok ( self . handle . block_on ( async move { self . get_new_address ( ) . await . script_pubkey ( ) } ) )
359372 } )
360373 }
361374
362- fn sign_tx ( & self , tx : Transaction ) -> Result < Transaction , ( ) > {
375+ fn sign_psbt ( & self , tx : PartiallySignedTransaction ) -> Result < Transaction , ( ) > {
363376 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 ( |_| ( ) ) ;
365378 let tx_hex = hex_utils:: hex_str ( & tx_bytes) ;
366379 let signed_tx = tokio:: task:: block_in_place ( move || {
367380 self . handle . block_on ( async move { self . sign_raw_transaction_with_wallet ( tx_hex) . await } )
0 commit comments