Compare commits
19 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b2fdc5ccf8 | ||
|
|
df6675dc09 | ||
|
|
4b98565ee2 | ||
|
|
5d7be37136 | ||
|
|
761796c94a | ||
|
|
5dc4bb6360 | ||
|
|
e31cf4773b | ||
|
|
c2656995fc | ||
|
|
b185259561 | ||
|
|
80bf744a70 | ||
|
|
0e28021b3e | ||
|
|
980fa4afd6 | ||
|
|
b20d41ace8 | ||
|
|
24d07e2cac | ||
|
|
fbffdfe65e | ||
|
|
dd13e4e422 | ||
|
|
1372898ff2 | ||
|
|
f24458248a | ||
|
|
74ac97cfe4 |
10
.github/workflows/cont_integration.yml
vendored
10
.github/workflows/cont_integration.yml
vendored
@ -41,6 +41,7 @@ jobs:
|
||||
- run: cargo check --verbose --no-default-features --features=proxy,use-openssl
|
||||
- run: cargo check --verbose --no-default-features --features=proxy,use-rustls
|
||||
- run: cargo check --verbose --no-default-features --features=proxy,use-rustls-ring
|
||||
- run: cargo check --verbose --no-default-features --features=proxy,use-rustls,use-rustls-ring
|
||||
|
||||
fmt:
|
||||
name: Rust fmt
|
||||
@ -61,13 +62,10 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
- uses: dtolnay/rust-toolchain@v1
|
||||
with:
|
||||
toolchain: 1.84.0
|
||||
toolchain: 1.90.0
|
||||
components: clippy
|
||||
- name: Rust Cache
|
||||
uses: Swatinem/rust-cache@v2.2.1
|
||||
- uses: actions-rs/clippy-check@v1
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
args: --all-features --all-targets -- -D warnings
|
||||
- run: cargo clippy --all-features --all-targets -- -D warnings
|
||||
|
||||
22
CHANGELOG.md
22
CHANGELOG.md
@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [0.24.1]
|
||||
- Default to `ring` if multiple `rustls` features are set #183
|
||||
|
||||
## [0.24.0]
|
||||
- Use default `CryptoProvider` if available, otherwise install `rustls`'s `CryptoProvider` based on features #171
|
||||
- Add a new batch method for `blockchain.transaction.get_merkle` #170
|
||||
|
||||
## [0.23.1]
|
||||
- Fix batch request to Electrum servers out of order responses #160
|
||||
@ -49,10 +55,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
- Revert "errors if expecting headers notification but not subscribed" #115
|
||||
|
||||
[0.18.0]: https://github.com/bitcoindevkit/rust-electrum-client/compare/0.17.0...0.18.0
|
||||
[0.19.0]: https://github.com/bitcoindevkit/rust-electrum-client/compare/0.18.0...v0.19.0
|
||||
[0.20.0]: https://github.com/bitcoindevkit/rust-electrum-client/compare/0.19.0...v0.20.0
|
||||
[0.21.0]: https://github.com/bitcoindevkit/rust-electrum-client/compare/0.20.0...v0.21.0
|
||||
[0.22.0]: https://github.com/bitcoindevkit/rust-electrum-client/compare/0.21.0...v0.22.0
|
||||
[0.23.0]: https://github.com/bitcoindevkit/rust-electrum-client/compare/0.22.0...v0.23.0
|
||||
[0.23.1]: https://github.com/bitcoindevkit/rust-electrum-client/compare/0.23.0...v0.23.1
|
||||
[Unreleased]: https://github.com/bitcoindevkit/rust-electrum-client/compare/0.23.1...HEAD
|
||||
[0.19.0]: https://github.com/bitcoindevkit/rust-electrum-client/compare/0.18.0...0.19.0
|
||||
[0.20.0]: https://github.com/bitcoindevkit/rust-electrum-client/compare/0.19.0...0.20.0
|
||||
[0.21.0]: https://github.com/bitcoindevkit/rust-electrum-client/compare/0.20.0...0.21.0
|
||||
[0.22.0]: https://github.com/bitcoindevkit/rust-electrum-client/compare/0.21.0...0.22.0
|
||||
[0.23.0]: https://github.com/bitcoindevkit/rust-electrum-client/compare/0.22.0...0.23.0
|
||||
[0.23.1]: https://github.com/bitcoindevkit/rust-electrum-client/compare/0.23.0...0.23.1
|
||||
[0.24.0]: https://github.com/bitcoindevkit/rust-electrum-client/compare/0.23.1...0.24.0
|
||||
[0.24.1]: https://github.com/bitcoindevkit/rust-electrum-client/compare/0.24.0...0.24.1
|
||||
[Unreleased]: https://github.com/bitcoindevkit/rust-electrum-client/compare/0.24.1...HEAD
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "electrum-client"
|
||||
version = "0.23.1"
|
||||
version = "0.24.1"
|
||||
authors = ["Alekos Filini <alekos.filini@gmail.com>"]
|
||||
license = "MIT"
|
||||
homepage = "https://github.com/bitcoindevkit/rust-electrum-client"
|
||||
|
||||
34
justfile
Normal file
34
justfile
Normal file
@ -0,0 +1,34 @@
|
||||
alias b := build
|
||||
alias c := check
|
||||
alias f := fmt
|
||||
alias t := test
|
||||
alias p := pre-push
|
||||
|
||||
_default:
|
||||
@just --list
|
||||
|
||||
# Build the project
|
||||
build:
|
||||
cargo build
|
||||
|
||||
# Check code: formatting, compilation, linting, doc comments, and commit signature
|
||||
check:
|
||||
cargo +nightly fmt --all -- --check
|
||||
cargo check --all-features --all-targets
|
||||
cargo clippy --all-features --all-targets -- -D warnings
|
||||
RUSTDOCFLAGS="-D warnings" cargo doc --all-features --no-deps
|
||||
@[ "$(git log --pretty='format:%G?' -1 HEAD)" = "N" ] && \
|
||||
echo "\n⚠️ Unsigned commit: BDK requires that commits be signed." || \
|
||||
true
|
||||
|
||||
# Format all code
|
||||
fmt:
|
||||
cargo +nightly fmt
|
||||
|
||||
# Run all tests on the workspace with all features
|
||||
test:
|
||||
cargo test --all-features -- --test-threads=1
|
||||
|
||||
# Run pre-push suite: format, check, and test
|
||||
pre-push: fmt check test
|
||||
|
||||
33
src/api.rs
33
src/api.rs
@ -145,6 +145,17 @@ where
|
||||
(**self).transaction_get_merkle(txid, height)
|
||||
}
|
||||
|
||||
fn batch_transaction_get_merkle<I>(
|
||||
&self,
|
||||
txids_and_heights: I,
|
||||
) -> Result<Vec<GetMerkleRes>, Error>
|
||||
where
|
||||
I: IntoIterator + Clone,
|
||||
I::Item: Borrow<(Txid, usize)>,
|
||||
{
|
||||
(**self).batch_transaction_get_merkle(txids_and_heights)
|
||||
}
|
||||
|
||||
fn txid_from_pos(&self, height: usize, tx_pos: usize) -> Result<Txid, Error> {
|
||||
(**self).txid_from_pos(height, tx_pos)
|
||||
}
|
||||
@ -362,6 +373,17 @@ pub trait ElectrumApi {
|
||||
/// Returns the merkle path for the transaction `txid` confirmed in the block at `height`.
|
||||
fn transaction_get_merkle(&self, txid: &Txid, height: usize) -> Result<GetMerkleRes, Error>;
|
||||
|
||||
/// Batch version of [`transaction_get_merkle`](#method.transaction_get_merkle).
|
||||
///
|
||||
/// Take a list of `(txid, height)`, for transactions with `txid` confirmed in the block at `height`.
|
||||
fn batch_transaction_get_merkle<I>(
|
||||
&self,
|
||||
txids_and_heights: I,
|
||||
) -> Result<Vec<GetMerkleRes>, Error>
|
||||
where
|
||||
I: IntoIterator + Clone,
|
||||
I::Item: Borrow<(Txid, usize)>;
|
||||
|
||||
/// Returns a transaction hash, given a block `height` and a `tx_pos` in the block.
|
||||
fn txid_from_pos(&self, height: usize, tx_pos: usize) -> Result<Txid, Error>;
|
||||
|
||||
@ -558,6 +580,17 @@ mod test {
|
||||
unreachable!()
|
||||
}
|
||||
|
||||
fn batch_transaction_get_merkle<I>(
|
||||
&self,
|
||||
_: I,
|
||||
) -> Result<Vec<crate::GetMerkleRes>, crate::Error>
|
||||
where
|
||||
I: IntoIterator + Clone,
|
||||
I::Item: std::borrow::Borrow<(bitcoin::Txid, usize)>,
|
||||
{
|
||||
unreachable!()
|
||||
}
|
||||
|
||||
fn txid_from_pos(&self, _: usize, _: usize) -> Result<bitcoin::Txid, super::Error> {
|
||||
unreachable!()
|
||||
}
|
||||
|
||||
13
src/batch.rs
13
src/batch.rs
@ -62,6 +62,17 @@ impl Batch {
|
||||
.push((String::from("blockchain.transaction.get"), params));
|
||||
}
|
||||
|
||||
/// Add one `blockchain.transaction.get_merkle` request to the batch queue
|
||||
pub fn transaction_get_merkle(&mut self, tx_hash_and_height: &(Txid, usize)) {
|
||||
let (tx_hash, height) = tx_hash_and_height;
|
||||
let params = vec![
|
||||
Param::String(format!("{:x}", tx_hash)),
|
||||
Param::Usize(*height),
|
||||
];
|
||||
self.calls
|
||||
.push((String::from("blockchain.transaction.get_merkle"), params));
|
||||
}
|
||||
|
||||
/// Add one `blockchain.estimatefee` request to the batch queue
|
||||
pub fn estimate_fee(&mut self, number: usize) {
|
||||
let params = vec![Param::Usize(number)];
|
||||
@ -77,7 +88,7 @@ impl Batch {
|
||||
}
|
||||
|
||||
/// Returns an iterator on the batch
|
||||
pub fn iter(&self) -> BatchIter {
|
||||
pub fn iter(&self) -> BatchIter<'_> {
|
||||
BatchIter {
|
||||
batch: self,
|
||||
index: 0,
|
||||
|
||||
@ -327,6 +327,22 @@ impl ElectrumApi for Client {
|
||||
impl_inner_call!(self, transaction_get_merkle, txid, height)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn batch_transaction_get_merkle<I>(
|
||||
&self,
|
||||
txids_and_heights: I,
|
||||
) -> Result<Vec<GetMerkleRes>, Error>
|
||||
where
|
||||
I: IntoIterator + Clone,
|
||||
I::Item: Borrow<(Txid, usize)>,
|
||||
{
|
||||
impl_inner_call!(
|
||||
self,
|
||||
batch_transaction_get_merkle,
|
||||
txids_and_heights.clone()
|
||||
)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn txid_from_pos(&self, height: usize, tx_pos: usize) -> Result<Txid, Error> {
|
||||
impl_inner_call!(self, txid_from_pos, height, tx_pos)
|
||||
@ -432,7 +448,9 @@ mod tests {
|
||||
let now = Instant::now();
|
||||
let client = Client::from_config(
|
||||
&endpoint,
|
||||
crate::config::ConfigBuilder::new().timeout(Some(5)).build(),
|
||||
crate::config::ConfigBuilder::new()
|
||||
.timeout(Some(Duration::from_secs(5)))
|
||||
.build(),
|
||||
);
|
||||
let elapsed = now.elapsed();
|
||||
|
||||
|
||||
@ -55,8 +55,8 @@ impl ConfigBuilder {
|
||||
}
|
||||
|
||||
/// Sets the timeout
|
||||
pub fn timeout(mut self, timeout: Option<u8>) -> Self {
|
||||
self.config.timeout = timeout.map(|t| Duration::from_secs(t as u64));
|
||||
pub fn timeout(mut self, timeout: Option<Duration>) -> Self {
|
||||
self.config.timeout = timeout;
|
||||
self
|
||||
}
|
||||
|
||||
|
||||
@ -406,6 +406,29 @@ impl RawClient<ElectrumSslStream> {
|
||||
) -> Result<Self, Error> {
|
||||
use std::convert::TryFrom;
|
||||
|
||||
if rustls::crypto::CryptoProvider::get_default().is_none() {
|
||||
// We install a crypto provider depending on the set feature.
|
||||
#[cfg(all(feature = "use-rustls", not(feature = "use-rustls-ring")))]
|
||||
rustls::crypto::CryptoProvider::install_default(
|
||||
rustls::crypto::aws_lc_rs::default_provider(),
|
||||
)
|
||||
.map_err(|_| {
|
||||
Error::CouldNotCreateConnection(rustls::Error::General(
|
||||
"Failed to install CryptoProvider".to_string(),
|
||||
))
|
||||
})?;
|
||||
|
||||
#[cfg(feature = "use-rustls-ring")]
|
||||
rustls::crypto::CryptoProvider::install_default(
|
||||
rustls::crypto::ring::default_provider(),
|
||||
)
|
||||
.map_err(|_| {
|
||||
Error::CouldNotCreateConnection(rustls::Error::General(
|
||||
"Failed to install CryptoProvider".to_string(),
|
||||
))
|
||||
})?;
|
||||
}
|
||||
|
||||
let builder = ClientConfig::builder();
|
||||
|
||||
let config = if validate_domain {
|
||||
@ -426,7 +449,7 @@ impl RawClient<ElectrumSslStream> {
|
||||
builder
|
||||
.dangerous()
|
||||
.with_custom_certificate_verifier(std::sync::Arc::new(
|
||||
#[cfg(feature = "use-rustls")]
|
||||
#[cfg(all(feature = "use-rustls", not(feature = "use-rustls-ring")))]
|
||||
danger::NoCertificateVerification::new(rustls::crypto::aws_lc_rs::default_provider()),
|
||||
#[cfg(feature = "use-rustls-ring")]
|
||||
danger::NoCertificateVerification::new(rustls::crypto::ring::default_provider()),
|
||||
@ -1102,6 +1125,17 @@ impl<T: Read + Write> ElectrumApi for RawClient<T> {
|
||||
Ok(serde_json::from_value(result)?)
|
||||
}
|
||||
|
||||
fn batch_transaction_get_merkle<I>(
|
||||
&self,
|
||||
txids_and_heights: I,
|
||||
) -> Result<Vec<GetMerkleRes>, Error>
|
||||
where
|
||||
I: IntoIterator + Clone,
|
||||
I::Item: Borrow<(Txid, usize)>,
|
||||
{
|
||||
impl_batch_call!(self, txids_and_heights, transaction_get_merkle)
|
||||
}
|
||||
|
||||
fn txid_from_pos(&self, height: usize, tx_pos: usize) -> Result<Txid, Error> {
|
||||
let params = vec![Param::Usize(height), Param::Usize(tx_pos)];
|
||||
let req = Request::new_id(
|
||||
@ -1215,7 +1249,7 @@ mod test {
|
||||
let client = RawClient::new(get_test_server(), None).unwrap();
|
||||
|
||||
let resp = client.relay_fee().unwrap();
|
||||
assert_eq!(resp, 0.00001);
|
||||
assert!(resp > 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -1448,6 +1482,98 @@ mod test {
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_batch_transaction_get_merkle() {
|
||||
use bitcoin::Txid;
|
||||
|
||||
struct TestCase {
|
||||
txid: Txid,
|
||||
block_height: usize,
|
||||
exp_pos: usize,
|
||||
exp_bytes: [u8; 32],
|
||||
}
|
||||
|
||||
let client = RawClient::new(get_test_server(), None).unwrap();
|
||||
|
||||
let test_cases: Vec<TestCase> = vec![
|
||||
TestCase {
|
||||
txid: Txid::from_str(
|
||||
"1f7ff3c407f33eabc8bec7d2cc230948f2249ec8e591bcf6f971ca9366c8788d",
|
||||
)
|
||||
.unwrap(),
|
||||
block_height: 630000,
|
||||
exp_pos: 68,
|
||||
exp_bytes: [
|
||||
34, 65, 51, 64, 49, 139, 115, 189, 185, 246, 70, 225, 168, 193, 217, 195, 47,
|
||||
66, 179, 240, 153, 24, 114, 215, 144, 196, 212, 41, 39, 155, 246, 25,
|
||||
],
|
||||
},
|
||||
TestCase {
|
||||
txid: Txid::from_str(
|
||||
"70a8639bc9b743c0610d1231103a2f8e99f4a25670946b91f16c55a5373b37d1",
|
||||
)
|
||||
.unwrap(),
|
||||
block_height: 630001,
|
||||
exp_pos: 25,
|
||||
exp_bytes: [
|
||||
169, 100, 34, 99, 168, 101, 25, 168, 184, 90, 77, 50, 151, 245, 130, 101, 193,
|
||||
229, 136, 128, 63, 110, 241, 19, 242, 59, 184, 137, 245, 249, 188, 110,
|
||||
],
|
||||
},
|
||||
TestCase {
|
||||
txid: Txid::from_str(
|
||||
"a0db149ace545beabbd87a8d6b20ffd6aa3b5a50e58add49a3d435f898c272cf",
|
||||
)
|
||||
.unwrap(),
|
||||
block_height: 840000,
|
||||
exp_pos: 0,
|
||||
exp_bytes: [
|
||||
43, 184, 95, 75, 0, 75, 230, 218, 84, 247, 102, 193, 124, 30, 133, 81, 135, 50,
|
||||
113, 18, 194, 49, 239, 47, 243, 94, 186, 208, 234, 103, 198, 158,
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
let txids_and_heights: Vec<(Txid, usize)> = test_cases
|
||||
.iter()
|
||||
.map(|case| (case.txid, case.block_height))
|
||||
.collect();
|
||||
|
||||
let resp = client
|
||||
.batch_transaction_get_merkle(&txids_and_heights)
|
||||
.unwrap();
|
||||
|
||||
for (i, (res, test_case)) in resp.iter().zip(test_cases).enumerate() {
|
||||
assert_eq!(res.block_height, test_case.block_height);
|
||||
assert_eq!(res.pos, test_case.exp_pos);
|
||||
assert_eq!(res.merkle.len(), 12);
|
||||
assert_eq!(res.merkle[0], test_case.exp_bytes);
|
||||
|
||||
// Check we can verify the merkle proof validity, but fail if we supply wrong data.
|
||||
let block_header = client.block_header(res.block_height).unwrap();
|
||||
assert!(utils::validate_merkle_proof(
|
||||
&txids_and_heights[i].0,
|
||||
&block_header.merkle_root,
|
||||
res
|
||||
));
|
||||
|
||||
let mut fail_res = res.clone();
|
||||
fail_res.pos = 13;
|
||||
assert!(!utils::validate_merkle_proof(
|
||||
&txids_and_heights[i].0,
|
||||
&block_header.merkle_root,
|
||||
&fail_res
|
||||
));
|
||||
|
||||
let fail_block_header = client.block_header(res.block_height + 1).unwrap();
|
||||
assert!(!utils::validate_merkle_proof(
|
||||
&txids_and_heights[i].0,
|
||||
&fail_block_header.merkle_root,
|
||||
res
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_txid_from_pos() {
|
||||
use bitcoin::Txid;
|
||||
|
||||
@ -18,12 +18,7 @@ fn read_response(socket: &mut TcpStream) -> io::Result<SocketAddrV4> {
|
||||
|
||||
match response.read_u8()? {
|
||||
90 => {}
|
||||
91 => {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"request rejected or failed",
|
||||
))
|
||||
}
|
||||
91 => return Err(io::Error::other("request rejected or failed")),
|
||||
92 => {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::PermissionDenied,
|
||||
|
||||
@ -37,10 +37,7 @@ fn read_addr<R: Read>(socket: &mut R) -> io::Result<TargetAddr> {
|
||||
ip, port, 0, 0,
|
||||
))))
|
||||
}
|
||||
_ => Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"unsupported address type",
|
||||
)),
|
||||
_ => Err(io::Error::other("unsupported address type")),
|
||||
}
|
||||
}
|
||||
|
||||
@ -54,35 +51,15 @@ fn read_response(socket: &mut TcpStream) -> io::Result<TargetAddr> {
|
||||
|
||||
match socket.read_u8()? {
|
||||
0 => {}
|
||||
1 => {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"general SOCKS server failure",
|
||||
))
|
||||
}
|
||||
2 => {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"connection not allowed by ruleset",
|
||||
))
|
||||
}
|
||||
3 => return Err(io::Error::new(io::ErrorKind::Other, "network unreachable")),
|
||||
4 => return Err(io::Error::new(io::ErrorKind::Other, "host unreachable")),
|
||||
5 => return Err(io::Error::new(io::ErrorKind::Other, "connection refused")),
|
||||
6 => return Err(io::Error::new(io::ErrorKind::Other, "TTL expired")),
|
||||
7 => {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"command not supported",
|
||||
))
|
||||
}
|
||||
8 => {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"address kind not supported",
|
||||
))
|
||||
}
|
||||
_ => return Err(io::Error::new(io::ErrorKind::Other, "unknown error")),
|
||||
1 => return Err(io::Error::other("general SOCKS server failure")),
|
||||
2 => return Err(io::Error::other("connection not allowed by ruleset")),
|
||||
3 => return Err(io::Error::other("network unreachable")),
|
||||
4 => return Err(io::Error::other("host unreachable")),
|
||||
5 => return Err(io::Error::other("connection refused")),
|
||||
6 => return Err(io::Error::other("TTL expired")),
|
||||
7 => return Err(io::Error::other("command not supported")),
|
||||
8 => return Err(io::Error::other("address kind not supported")),
|
||||
_ => return Err(io::Error::other("unknown error")),
|
||||
}
|
||||
|
||||
if socket.read_u8()? != 0 {
|
||||
@ -227,14 +204,11 @@ impl Socks5Stream {
|
||||
}
|
||||
|
||||
if selected_method == 0xff {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"no acceptable auth methods",
|
||||
));
|
||||
return Err(io::Error::other("no acceptable auth methods"));
|
||||
}
|
||||
|
||||
if selected_method != auth.id() && selected_method != Authentication::None.id() {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "unknown auth method"));
|
||||
return Err(io::Error::other("unknown auth method"));
|
||||
}
|
||||
|
||||
match *auth {
|
||||
|
||||
@ -13,7 +13,7 @@ use bitcoin::Txid;
|
||||
/// otherwise.
|
||||
///
|
||||
/// [`transaction_get_merkle`]: crate::ElectrumApi::transaction_get_merkle
|
||||
/// [`BlockHeader`]: bitcoin::BlockHeader
|
||||
/// [`BlockHeader`]: bitcoin::block::Header
|
||||
pub fn validate_merkle_proof(
|
||||
txid: &Txid,
|
||||
merkle_root: &TxMerkleNode,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user