Implement bump_transaction::WalletSource for BitcoindClient
authorWilmer Paulino <wilmer@wilmerpaulino.com>
Mon, 14 Aug 2023 16:00:21 +0000 (09:00 -0700)
committerWilmer Paulino <wilmer@wilmerpaulino.com>
Mon, 14 Aug 2023 16:07:35 +0000 (09:07 -0700)
`BitcoindClient` will be used as the view into the onchain wallet from
which we'll source UTXOs.

src/bitcoind_client.rs
src/convert.rs

index 228f27a2318406b1a2380b73ba78669eb2bd55b8..a093e6d2eb7c6d1d85868b64c069181bada19759 100644 (file)
@@ -1,13 +1,19 @@
 use crate::convert::{
-       BlockchainInfo, FeeResponse, FundedTx, MempoolMinFeeResponse, NewAddress, RawTx, SignedTx,
+       BlockchainInfo, FeeResponse, FundedTx, ListUnspentResponse, MempoolMinFeeResponse, NewAddress,
+       RawTx, SignedTx,
 };
 use crate::disk::FilesystemLogger;
+use crate::hex_utils;
 use base64;
+use bitcoin::blockdata::constants::WITNESS_SCALE_FACTOR;
 use bitcoin::blockdata::transaction::Transaction;
-use bitcoin::consensus::encode;
+use bitcoin::consensus::{encode, Decodable, Encodable};
 use bitcoin::hash_types::{BlockHash, Txid};
-use bitcoin::util::address::Address;
+use bitcoin::hashes::Hash;
+use bitcoin::util::address::{Address, Payload, WitnessVersion};
+use bitcoin::{OutPoint, Script, TxOut, WPubkeyHash, XOnlyPublicKey};
 use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
+use lightning::events::bump_transaction::{Utxo, WalletSource};
 use lightning::log_error;
 use lightning::routing::utxo::{UtxoLookup, UtxoResult};
 use lightning::util::logger::Logger;
@@ -250,6 +256,13 @@ impl BitcoindClient {
                        .await
                        .unwrap()
        }
+
+       pub async fn list_unspent(&self) -> ListUnspentResponse {
+               self.bitcoind_rpc_client
+                       .call_method::<ListUnspentResponse>("listunspent", &vec![])
+                       .await
+                       .unwrap()
+       }
 }
 
 impl FeeEstimator for BitcoindClient {
@@ -308,3 +321,55 @@ impl UtxoLookup for BitcoindClient {
                todo!();
        }
 }
+
+impl WalletSource for BitcoindClient {
+       fn list_confirmed_utxos(&self) -> Result<Vec<Utxo>, ()> {
+               let utxos = tokio::task::block_in_place(move || {
+                       self.handle.block_on(async move { self.list_unspent().await }).0
+               });
+               Ok(utxos
+                       .into_iter()
+                       .filter_map(|utxo| {
+                               let outpoint = OutPoint { txid: utxo.txid, vout: utxo.vout };
+                               match utxo.address.payload {
+                                       Payload::WitnessProgram { version, ref program } => match version {
+                                               WitnessVersion::V0 => WPubkeyHash::from_slice(program)
+                                                       .map(|wpkh| Utxo::new_v0_p2wpkh(outpoint, utxo.amount, &wpkh))
+                                                       .ok(),
+                                               // TODO: Add `Utxo::new_v1_p2tr` upstream.
+                                               WitnessVersion::V1 => XOnlyPublicKey::from_slice(program)
+                                                       .map(|_| Utxo {
+                                                               outpoint,
+                                                               output: TxOut {
+                                                                       value: utxo.amount,
+                                                                       script_pubkey: Script::new_witness_program(version, program),
+                                                               },
+                                                               satisfaction_weight: 1 /* empty script_sig */ * WITNESS_SCALE_FACTOR as u64 +
+                                                                       1 /* witness items */ + 1 /* schnorr sig len */ + 64, /* schnorr sig */
+                                                       })
+                                                       .ok(),
+                                               _ => None,
+                                       },
+                                       _ => None,
+                               }
+                       })
+                       .collect())
+       }
+
+       fn get_change_script(&self) -> Result<Script, ()> {
+               tokio::task::block_in_place(move || {
+                       Ok(self.handle.block_on(async move { self.get_new_address().await.script_pubkey() }))
+               })
+       }
+
+       fn sign_tx(&self, tx: Transaction) -> Result<Transaction, ()> {
+               let mut tx_bytes = Vec::new();
+               let _ = tx.consensus_encode(&mut tx_bytes).map_err(|_| ());
+               let tx_hex = hex_utils::hex_str(&tx_bytes);
+               let signed_tx = tokio::task::block_in_place(move || {
+                       self.handle.block_on(async move { self.sign_raw_transaction_with_wallet(tx_hex).await })
+               });
+               let signed_tx_bytes = hex_utils::to_vec(&signed_tx.hex).ok_or(())?;
+               Transaction::consensus_decode(&mut signed_tx_bytes.as_slice()).map_err(|_| ())
+       }
+}
index 84b033e3a0820fc911f5d4dec42a32f0f8ba05df..7f1bf108828753df82a0a754e82c22bc332a8664 100644 (file)
@@ -1,7 +1,8 @@
 use bitcoin::hashes::hex::FromHex;
-use bitcoin::BlockHash;
+use bitcoin::{Address, BlockHash, Txid};
 use lightning_block_sync::http::JsonResponse;
 use std::convert::TryInto;
+use std::str::FromStr;
 
 pub struct FundedTx {
        pub changepos: i64,
@@ -116,3 +117,33 @@ impl TryInto<BlockchainInfo> for JsonResponse {
                })
        }
 }
+
+pub struct ListUnspentUtxo {
+       pub txid: Txid,
+       pub vout: u32,
+       pub amount: u64,
+       pub address: Address,
+}
+
+pub struct ListUnspentResponse(pub Vec<ListUnspentUtxo>);
+
+impl TryInto<ListUnspentResponse> for JsonResponse {
+       type Error = std::io::Error;
+       fn try_into(self) -> Result<ListUnspentResponse, Self::Error> {
+               let utxos = self
+                       .0
+                       .as_array()
+                       .unwrap()
+                       .iter()
+                       .map(|utxo| ListUnspentUtxo {
+                               txid: Txid::from_str(&utxo["txid"].as_str().unwrap().to_string()).unwrap(),
+                               vout: utxo["vout"].as_u64().unwrap() as u32,
+                               amount: bitcoin::Amount::from_btc(utxo["amount"].as_f64().unwrap())
+                                       .unwrap()
+                                       .to_sat(),
+                               address: Address::from_str(&utxo["address"].as_str().unwrap().to_string()).unwrap(),
+                       })
+                       .collect();
+               Ok(ListUnspentResponse(utxos))
+       }
+}