From 2e4446bb8703bdbf775f0efcdf8338733e16e862 Mon Sep 17 00:00:00 2001 From: Tommy Volk Date: Fri, 8 Nov 2024 13:45:04 -0600 Subject: [PATCH] chore(deps): bump electrum-client to v0.21 --- Cargo.lock | 31 ++------- Cargo.toml | 5 +- fedimint-bitcoind/src/electrum.rs | 45 ++++-------- fedimint-core/src/bitcoin_migration.rs | 96 -------------------------- 4 files changed, 23 insertions(+), 154 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 639384374d5..21277488a13 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1954,20 +1954,18 @@ checksum = "11157ac094ffbdde99aa67b23417ebdd801842852b500e395a45a9c0aac03e4a" [[package]] name = "electrum-client" -version = "0.18.0" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6bc133f1c8d829d254f013f946653cbeb2b08674b960146361d1e9b67733ad19" +checksum = "7a0bd443023f9f5c4b7153053721939accc7113cbdf810a024434eed454b3db1" dependencies = [ - "bitcoin 0.30.2", - "bitcoin-private", + "bitcoin 0.32.4", "byteorder", "libc", "log", - "rustls 0.21.11", + "rustls 0.23.7", "serde", "serde_json", - "webpki", - "webpki-roots 0.22.6", + "webpki-roots 0.25.4", "winapi", ] @@ -8824,25 +8822,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "webpki" -version = "0.22.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed63aea5ce73d0ff405984102c42de94fc55a6b75765d621c65262469b3c9b53" -dependencies = [ - "ring 0.17.8", - "untrusted 0.9.0", -] - -[[package]] -name = "webpki-roots" -version = "0.22.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6c71e40d7d2c34a5106301fb632274ca37242cd0c9d3e64dbece371a40a2d87" -dependencies = [ - "webpki", -] - [[package]] name = "webpki-roots" version = "0.25.4" diff --git a/Cargo.toml b/Cargo.toml index 94ad1944c20..25ad1bcaf93 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -102,7 +102,10 @@ clap = { version = "4.5.20", features = ["derive", "env"] } cln-rpc = "0.2.0" criterion = "0.5.1" devimint = { path = "./devimint", version = "=0.5.0-alpha" } -electrum-client = { version = "0.18.0", features = ["use-rustls"] } +electrum-client = { version = "0.21.0", default-features = false, features = [ + "proxy", + "use-rustls-ring", +] } erased-serde = "0.4" esplora-client = { version = "0.10.0", default-features = false, features = [ "async-https-rustls", diff --git a/fedimint-bitcoind/src/electrum.rs b/fedimint-bitcoind/src/electrum.rs index 10c2de1a59a..aaafa06b574 100644 --- a/fedimint-bitcoind/src/electrum.rs +++ b/fedimint-bitcoind/src/electrum.rs @@ -4,10 +4,6 @@ use anyhow::{anyhow as format_err, bail}; use bitcoin::{BlockHash, Network, ScriptBuf, Transaction, Txid}; use electrum_client::ElectrumApi; use electrum_client::Error::Protocol; -use fedimint_core::bitcoin_migration::{ - bitcoin30_to_bitcoin32_block_hash, bitcoin30_to_bitcoin32_tx, - bitcoin32_to_bitcoin30_script_buf, bitcoin32_to_bitcoin30_txid, -}; use fedimint_core::envs::BitcoinRpcConfig; use fedimint_core::runtime::block_in_place; use fedimint_core::task::TaskHandle; @@ -75,13 +71,11 @@ impl IBitcoindRpc for ElectrumClient { async fn get_block_hash(&self, height: u64) -> anyhow::Result { let height = usize::try_from(height)?; let result = block_in_place(|| self.client.block_headers(height, 1))?; - Ok(bitcoin30_to_bitcoin32_block_hash( - &result - .headers - .first() - .ok_or_else(|| format_err!("empty block headers response"))? - .block_hash(), - )) + Ok(result + .headers + .first() + .ok_or_else(|| format_err!("empty block headers response"))? + .block_hash()) } async fn get_fee_rate(&self, confirmation_target: u16) -> anyhow::Result> { @@ -107,11 +101,8 @@ impl IBitcoindRpc for ElectrumClient { } async fn get_tx_block_height(&self, txid: &Txid) -> anyhow::Result> { - let tx = block_in_place(|| { - self.client - .transaction_get(&bitcoin32_to_bitcoin30_txid(txid)) - }) - .map_err(|error| info!(?error, "Unable to get raw transaction")); + let tx = block_in_place(|| self.client.transaction_get(txid)) + .map_err(|error| info!(?error, "Unable to get raw transaction")); match tx.ok() { None => Ok(None), Some(tx) => { @@ -132,11 +123,8 @@ impl IBitcoindRpc for ElectrumClient { block_hash: &BlockHash, block_height: u64, ) -> anyhow::Result { - let tx = block_in_place(|| { - self.client - .transaction_get(&bitcoin32_to_bitcoin30_txid(txid)) - }) - .map_err(|error| info!(?error, "Unable to get raw transaction")); + let tx = block_in_place(|| self.client.transaction_get(txid)) + .map_err(|error| info!(?error, "Unable to get raw transaction")); match tx.ok() { None => Ok(false), @@ -149,10 +137,8 @@ impl IBitcoindRpc for ElectrumClient { match block_in_place(|| self.client.script_get_history(&output.script_pubkey))? .iter() - .find(|tx| { - tx.tx_hash == bitcoin32_to_bitcoin30_txid(txid) - && tx.height as u64 == block_height - }) { + .find(|tx| &tx.tx_hash == txid && tx.height as u64 == block_height) + { Some(tx) => { let sanity_block_hash = self.get_block_hash(tx.height as u64).await?; anyhow::ensure!( @@ -178,14 +164,11 @@ impl IBitcoindRpc for ElectrumClient { script: &ScriptBuf, ) -> anyhow::Result> { let mut results = vec![]; - let transactions = block_in_place(|| { - self.client - .script_get_history(&bitcoin32_to_bitcoin30_script_buf(script)) - })?; + let transactions = block_in_place(|| self.client.script_get_history(script))?; for history in transactions { - results.push(bitcoin30_to_bitcoin32_tx(&block_in_place(|| { + results.push(block_in_place(|| { self.client.transaction_get(&history.tx_hash) - })?)); + })?); } Ok(results) } diff --git a/fedimint-core/src/bitcoin_migration.rs b/fedimint-core/src/bitcoin_migration.rs index 0bc7644487a..9938e33e006 100644 --- a/fedimint-core/src/bitcoin_migration.rs +++ b/fedimint-core/src/bitcoin_migration.rs @@ -1,7 +1,5 @@ use std::str::FromStr; -use bitcoin::consensus::Decodable; -use bitcoin30::consensus::Encodable; use bitcoin30::hashes::Hash; pub fn bitcoin29_to_bitcoin32_psbt( @@ -80,77 +78,6 @@ pub fn bitcoin32_to_bitcoin30_address(address: &bitcoin::Address) -> bitcoin30:: .assume_checked() } -fn bitcoin30_to_bitcoin32_witness(witness: &bitcoin30::Witness) -> bitcoin::Witness { - let mut bytes = vec![]; - witness - .consensus_encode(&mut bytes) - .expect("Failed to consensus-encode bitcoin30 witness"); - let mut cursor = bitcoin::io::Cursor::new(bytes); - bitcoin::Witness::consensus_decode(&mut cursor) - .expect("Failed to convert bitcoin30 witness to bitcoin32 witness") -} - -fn bitcoin30_to_bitcoin32_txin(txin: &bitcoin30::TxIn) -> bitcoin::TxIn { - bitcoin::TxIn { - previous_output: bitcoin30_to_bitcoin32_outpoint(&txin.previous_output), - script_sig: bitcoin30_to_bitcoin32_script_buf(&txin.script_sig), - sequence: bitcoin::Sequence(txin.sequence.0), - witness: bitcoin30_to_bitcoin32_witness(&txin.witness), - } -} - -fn bitcoin30_to_bitcoin32_txout(txout: &bitcoin30::TxOut) -> bitcoin::TxOut { - bitcoin::TxOut { - value: bitcoin::Amount::from_sat(txout.value), - script_pubkey: bitcoin30_to_bitcoin32_script_buf(&txout.script_pubkey), - } -} - -fn bitcoin30_to_bitcoin32_locktime( - locktime: bitcoin30::blockdata::locktime::absolute::LockTime, -) -> bitcoin::blockdata::locktime::absolute::LockTime { - match locktime { - bitcoin30::blockdata::locktime::absolute::LockTime::Blocks(height) => { - bitcoin::blockdata::locktime::absolute::LockTime::Blocks( - bitcoin::blockdata::locktime::absolute::Height::from_consensus( - height.to_consensus_u32(), - ) - .expect("Failed to convert bitcoin30 block height locktime to bitcoin32 block height locktime"), - ) - } - bitcoin30::blockdata::locktime::absolute::LockTime::Seconds(time) => { - bitcoin::blockdata::locktime::absolute::LockTime::Seconds( - bitcoin::blockdata::locktime::absolute::Time::from_consensus(time.to_consensus_u32()).expect("Failed to convert bitcoin30 timestamp locktime to bitcoin32 timestamp locktime"), - ) - } - } -} - -pub fn bitcoin30_to_bitcoin32_tx(tx: &bitcoin30::Transaction) -> bitcoin::Transaction { - bitcoin::Transaction { - version: bitcoin::blockdata::transaction::Version(tx.version), - lock_time: bitcoin30_to_bitcoin32_locktime(tx.lock_time), - input: tx.input.iter().map(bitcoin30_to_bitcoin32_txin).collect(), - output: tx.output.iter().map(bitcoin30_to_bitcoin32_txout).collect(), - } -} - -fn bitcoin30_to_bitcoin32_script_buf(script: &bitcoin30::ScriptBuf) -> bitcoin::ScriptBuf { - bitcoin::ScriptBuf::from(script.as_bytes().to_vec()) -} - -pub fn bitcoin32_to_bitcoin30_script_buf(script: &bitcoin::ScriptBuf) -> bitcoin30::ScriptBuf { - bitcoin30::ScriptBuf::from(script.as_bytes().to_vec()) -} - -pub fn bitcoin30_to_bitcoin32_block_hash( - hash: &bitcoin30::block::BlockHash, -) -> bitcoin::block::BlockHash { - bitcoin::block::BlockHash::from_raw_hash(bitcoin30_to_bitcoin32_sha256d_hash( - &hash.to_raw_hash(), - )) -} - pub fn bitcoin32_to_bitcoin30_block_hash( hash: &bitcoin::block::BlockHash, ) -> bitcoin30::block::BlockHash { @@ -188,23 +115,6 @@ pub fn bitcoin32_to_bitcoin30_network(network: &bitcoin::Network) -> bitcoin30:: } } -fn bitcoin30_to_bitcoin32_txid(txid: &bitcoin30::Txid) -> bitcoin::Txid { - bitcoin::Txid::from_str(&txid.to_string()) - .expect("Failed to convert bitcoin30 txid to bitcoin32 txid") -} - -pub fn bitcoin32_to_bitcoin30_txid(txid: &bitcoin::Txid) -> bitcoin30::Txid { - bitcoin30::Txid::from_str(&txid.to_string()) - .expect("Failed to convert bitcoin32 txid to bitcoin30 txid") -} - -fn bitcoin30_to_bitcoin32_outpoint(outpoint: &bitcoin30::OutPoint) -> bitcoin::OutPoint { - bitcoin::OutPoint { - txid: bitcoin30_to_bitcoin32_txid(&outpoint.txid), - vout: outpoint.vout, - } -} - pub fn bitcoin30_to_bitcoin32_payment_preimage( preimage: &lightning::ln::PaymentPreimage, ) -> lightning_types::payment::PaymentPreimage { @@ -223,12 +133,6 @@ pub fn bitcoin32_to_bitcoin30_sha256_hash( bitcoin30::hashes::sha256::Hash::from_slice(hash.as_ref()).expect("Invalid hash length") } -fn bitcoin30_to_bitcoin32_sha256d_hash( - hash: &bitcoin30::hashes::sha256d::Hash, -) -> bitcoin::hashes::sha256d::Hash { - *bitcoin::hashes::sha256d::Hash::from_bytes_ref(hash.as_ref()) -} - fn bitcoin32_to_bitcoin30_sha256d_hash( hash: &bitcoin::hashes::sha256d::Hash, ) -> bitcoin30::hashes::sha256d::Hash {