@@ -53,7 +53,6 @@ pub use sp_core::H256;
5353use std:: {
5454 convert:: TryInto ,
5555 future:: Future ,
56- str:: FromStr ,
5756 sync:: Arc ,
5857 time:: { Duration , Instant } ,
5958} ;
@@ -101,8 +100,6 @@ const RANDOMIZATION_FACTOR: f64 = 0.25;
101100const DERIVATION_KEY_LABEL : & str = "derivation-key" ;
102101const DEPOSIT_LABEL : & str = "deposit" ;
103102
104- const SWEEP_ADDRESS : & str = "sweep-address" ;
105-
106103fn get_exponential_backoff ( ) -> ExponentialBackoff {
107104 ExponentialBackoff {
108105 current_interval : INITIAL_INTERVAL ,
@@ -150,8 +147,6 @@ pub trait BitcoinCoreApi {
150147
151148 fn list_transactions ( & self , max_count : Option < usize > ) -> Result < Vec < json:: ListTransactionResult > , Error > ;
152149
153- fn list_addresses ( & self ) -> Result < Vec < Address > , Error > ;
154-
155150 async fn get_raw_tx ( & self , txid : & Txid , block_hash : & BlockHash ) -> Result < Vec < u8 > , Error > ;
156151
157152 async fn get_transaction ( & self , txid : & Txid , block_hash : Option < BlockHash > ) -> Result < Transaction , Error > ;
@@ -162,15 +157,11 @@ pub trait BitcoinCoreApi {
162157
163158 async fn get_new_address ( & self ) -> Result < Address , Error > ;
164159
165- async fn get_new_sweep_address ( & self ) -> Result < Address , Error > ;
166-
167- async fn get_last_sweep_height ( & self ) -> Result < Option < u32 > , Error > ;
168-
169160 async fn get_new_public_key ( & self ) -> Result < PublicKey , Error > ;
170161
171- fn dump_private_key ( & self , address : & Address ) -> Result < PrivateKey , Error > ;
162+ fn dump_derivation_key ( & self , public_key : & PublicKey ) -> Result < PrivateKey , Error > ;
172163
173- fn import_private_key ( & self , private_key : & PrivateKey , is_derivation_key : bool ) -> Result < ( ) , Error > ;
164+ fn import_derivation_key ( & self , private_key : & PrivateKey ) -> Result < ( ) , Error > ;
174165
175166 async fn add_new_deposit_key ( & self , public_key : PublicKey , secret_key : Vec < u8 > ) -> Result < ( ) , Error > ;
176167
@@ -213,8 +204,6 @@ pub trait BitcoinCoreApi {
213204 num_confirmations : u32 ,
214205 ) -> Result < TransactionMetadata , Error > ;
215206
216- async fn sweep_funds ( & self , address : Address ) -> Result < Txid , Error > ;
217-
218207 async fn create_or_load_wallet ( & self ) -> Result < ( ) , Error > ;
219208
220209 async fn rescan_blockchain ( & self , start_height : usize , end_height : usize ) -> Result < ( ) , Error > ;
@@ -370,7 +359,7 @@ impl BitcoinCoreBuilder {
370359
371360#[ derive( Clone ) ]
372361pub struct BitcoinCore {
373- pub rpc : Arc < Client > ,
362+ rpc : Arc < Client > ,
374363 wallet_name : Option < String > ,
375364 network : Network ,
376365 transaction_creation_lock : Arc < Mutex < ( ) > > ,
@@ -482,6 +471,7 @@ impl BitcoinCore {
482471 signed_funded_raw_tx. complete,
483472 signed_funded_raw_tx. errors
484473 ) ;
474+
485475 return Err ( Error :: TransactionSigningError ) ;
486476 }
487477
@@ -724,27 +714,6 @@ impl BitcoinCoreApi for BitcoinCore {
724714 . list_transactions ( None , max_count. or ( Some ( DEFAULT_MAX_TX_COUNT ) ) , None , None ) ?)
725715 }
726716
727- // TODO: remove this once the wallet migration has completed
728- fn list_addresses ( & self ) -> Result < Vec < Address > , Error > {
729- // Lists groups of addresses which have had their common ownership
730- // made public by common use as inputs or as the resulting change
731- // in past transactions
732- let groupings: Vec < Vec < Vec < serde_json:: Value > > > = self . rpc . call ( "listaddressgroupings" , & [ ] ) ?;
733- let addresses = groupings
734- . into_iter ( )
735- . flatten ( )
736- . filter_map ( |group| {
737- group
738- . get ( 0 )
739- . and_then ( |v| v. as_str ( ) )
740- . map ( Address :: from_str) ?
741- . and_then ( |x| x. require_network ( self . network ) )
742- . ok ( )
743- } )
744- . collect :: < Vec < _ > > ( ) ;
745- Ok ( addresses)
746- }
747-
748717 /// Get the raw transaction identified by `Txid` and stored
749718 /// in the specified block.
750719 ///
@@ -799,26 +768,6 @@ impl BitcoinCoreApi for BitcoinCore {
799768 . require_network ( self . network ) ?)
800769 }
801770
802- async fn get_new_sweep_address ( & self ) -> Result < Address , Error > {
803- Ok ( self
804- . rpc
805- . get_new_address ( Some ( SWEEP_ADDRESS ) , Some ( AddressType :: Bech32 ) ) ?
806- . require_network ( self . network ) ?)
807- }
808-
809- async fn get_last_sweep_height ( & self ) -> Result < Option < u32 > , Error > {
810- Ok ( self
811- . rpc
812- . list_transactions ( Some ( SWEEP_ADDRESS ) , Some ( DEFAULT_MAX_TX_COUNT ) , None , None ) ?
813- . into_iter ( )
814- // we want to return None if there is no sweep tx for full nodes or new
815- // pruned nodes and we should return an error if any tx is still in the mempool
816- . map ( |tx| tx. info . blockheight . ok_or ( Error :: ConfirmationError ) )
817- . collect :: < Result < Vec < _ > , _ > > ( ) ?
818- . into_iter ( )
819- . min ( ) )
820- }
821-
822771 /// Gets a new public key for an address in the wallet
823772 async fn get_new_public_key ( & self ) -> Result < PublicKey , Error > {
824773 let address = self
@@ -830,16 +779,15 @@ impl BitcoinCoreApi for BitcoinCore {
830779 Ok ( public_key)
831780 }
832781
833- fn dump_private_key ( & self , address : & Address ) -> Result < PrivateKey , Error > {
834- Ok ( self . rpc . dump_private_key ( address) ?)
782+ fn dump_derivation_key ( & self , public_key : & PublicKey ) -> Result < PrivateKey , Error > {
783+ let address = Address :: p2wpkh ( public_key, self . network ) . map_err ( ConversionError :: from) ?;
784+ Ok ( self . rpc . dump_private_key ( & address) ?)
835785 }
836786
837- fn import_private_key ( & self , private_key : & PrivateKey , is_derivation_key : bool ) -> Result < ( ) , Error > {
838- Ok ( self . rpc . import_private_key (
839- private_key,
840- is_derivation_key. then_some ( DERIVATION_KEY_LABEL ) ,
841- Some ( false ) ,
842- ) ?)
787+ fn import_derivation_key ( & self , private_key : & PrivateKey ) -> Result < ( ) , Error > {
788+ Ok ( self
789+ . rpc
790+ . import_private_key ( private_key, Some ( DERIVATION_KEY_LABEL ) , Some ( false ) ) ?)
843791 }
844792
845793 /// Derive and import the private key for the master public key and public secret
@@ -1063,66 +1011,6 @@ impl BitcoinCoreApi for BitcoinCore {
10631011 . await ?)
10641012 }
10651013
1066- async fn sweep_funds ( & self , address : Address ) -> Result < Txid , Error > {
1067- let unspent = self . rpc . list_unspent ( Some ( 0 ) , None , None , None , None ) ?;
1068-
1069- let mut amount = Amount :: ZERO ;
1070- let mut utxos = Vec :: < json:: CreateRawTransactionInput > :: new ( ) ;
1071-
1072- for entry in unspent {
1073- if self . electrs_client . is_tx_output_spent ( & entry. txid , entry. vout ) . await ? {
1074- log:: info!( "{}:{} already spent" , entry. txid, entry. vout) ;
1075- // skip if already spent
1076- continue ;
1077- }
1078- amount += entry. amount ;
1079- utxos. push ( json:: CreateRawTransactionInput {
1080- txid : entry. txid ,
1081- vout : entry. vout ,
1082- sequence : None ,
1083- } )
1084- }
1085-
1086- log:: info!( "Sweeping {} from {} utxos" , amount, utxos. len( ) ) ;
1087- let mut outputs = serde_json:: Map :: < String , serde_json:: Value > :: new ( ) ;
1088- outputs. insert ( address. to_string ( ) , serde_json:: Value :: from ( amount. to_btc ( ) ) ) ;
1089-
1090- let args = [
1091- serde_json:: to_value :: < & [ json:: CreateRawTransactionInput ] > ( & utxos) ?,
1092- serde_json:: to_value ( outputs) ?,
1093- serde_json:: to_value ( 0i64 ) ?, /* locktime - default 0: see https://developer.bitcoin.org/reference/rpc/createrawtransaction.html */
1094- serde_json:: to_value ( true ) ?, // BIP125-replaceable, aka Replace By Fee (RBF)
1095- ] ;
1096- let raw_tx: String = self . rpc . call ( "createrawtransaction" , & args) ?;
1097-
1098- let funding_opts = FundRawTransactionOptions {
1099- fee_rate : None ,
1100- add_inputs : Some ( false ) ,
1101- subtract_fee_from_outputs : Some ( vec ! [ 0 ] ) ,
1102- ..Default :: default ( )
1103- } ;
1104- let funded_raw_tx = self . rpc . fund_raw_transaction ( raw_tx, Some ( & funding_opts) , None ) ?;
1105-
1106- let signed_funded_raw_tx =
1107- self . rpc
1108- . sign_raw_transaction_with_wallet ( & funded_raw_tx. transaction ( ) ?, None , None ) ?;
1109-
1110- if signed_funded_raw_tx. errors . is_some ( ) {
1111- log:: warn!(
1112- "Received bitcoin funding errors (complete={}): {:?}" ,
1113- signed_funded_raw_tx. complete,
1114- signed_funded_raw_tx. errors
1115- ) ;
1116- return Err ( Error :: TransactionSigningError ) ;
1117- }
1118-
1119- let transaction = signed_funded_raw_tx. transaction ( ) ?;
1120- let txid = self . rpc . send_raw_transaction ( & transaction) ?;
1121- log:: info!( "Sent sweep tx: {txid}" ) ;
1122-
1123- Ok ( txid)
1124- }
1125-
11261014 /// Create or load a wallet on Bitcoin Core.
11271015 async fn create_or_load_wallet ( & self ) -> Result < ( ) , Error > {
11281016 let wallet_name = if let Some ( ref wallet_name) = self . wallet_name {
@@ -1185,7 +1073,7 @@ impl BitcoinCoreApi for BitcoinCore {
11851073 // filter to only import
11861074 // a) payments in the blockchain (not in mempool), and
11871075 // b) payments TO the address (as bitcoin core will already know about transactions spending FROM it)
1188- let confirmed_payments_to = all_transactions. iter ( ) . filter ( |tx| {
1076+ let confirmed_payments_to = all_transactions. into_iter ( ) . filter ( |tx| {
11891077 if let Some ( status) = & tx. status {
11901078 if !status. confirmed {
11911079 return false ;
0 commit comments