Skip to content

Commit 8a664db

Browse files
noib3LeoComandini
authored andcommitted
rust: update electrum-client to 0.13.0
To properly handle some invalid proxy values. Co-authored-by: noib3 <[email protected]>
1 parent bd4a5b7 commit 8a664db

File tree

7 files changed

+64
-14
lines changed

7 files changed

+64
-14
lines changed

subprojects/gdk_rust/Cargo.lock

Lines changed: 16 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

subprojects/gdk_rust/gdk_electrum/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ testing = [ "gdk-common/testing" ]
1010
[dependencies]
1111
base64 = "0.13"
1212
block-modes = "0.8.0"
13-
electrum-client = { version = "0.12.0", default-features = false, features = [ "proxy", "use-openssl" ] }
13+
electrum-client = { version = "0.13.0", default-features = false, features = [ "proxy", "use-openssl" ] }
1414
gdk-common = { path = "../gdk_common" }
1515
gdk-registry = { path = "../gdk_registry" }
1616
gdk-pin-client = { path = "../gdk_pin_client" }

subprojects/gdk_rust/gdk_electrum/src/interface.rs

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
22

33
use crate::error::*;
44

5-
use electrum_client::{Client, ConfigBuilder};
5+
use electrum_client::{Client, ConfigBuilder, Socks5Config};
66
use gdk_common::network::NETWORK_REQUEST_TIMEOUT;
77
use std::str::FromStr;
88

@@ -17,13 +17,15 @@ impl ElectrumUrl {
1717
let mut config = ConfigBuilder::new();
1818

1919
// TODO: add support for socks5 credentials?
20-
config = config.socks5(
21-
proxy.filter(|p| !p.trim().is_empty()).map(|p| electrum_client::Socks5Config::new(p)),
22-
)?;
20+
if let Some(proxy) = proxy {
21+
if !proxy.trim().is_empty() {
22+
config = config.socks5(Some(Socks5Config::new(proxy)));
23+
}
24+
}
2325

2426
let timeout = timeout.unwrap_or(NETWORK_REQUEST_TIMEOUT.as_secs() as u8);
2527

26-
config = config.timeout(Some(timeout))?;
28+
config = config.timeout(Some(timeout));
2729

2830
let (url, config) = match self {
2931
ElectrumUrl::Tls(url, validate) => {
@@ -85,6 +87,8 @@ mod test {
8587
use gdk_common::scripts::p2shwpkh_script_sig;
8688
use std::str::FromStr;
8789

90+
use super::*;
91+
8892
fn p2pkh_hex(pk: &str) -> (PublicKey, Script) {
8993
let pk = Vec::<u8>::from_hex(pk).unwrap();
9094
let pk = PublicKey::from_slice(pk.as_slice()).unwrap();
@@ -191,4 +195,38 @@ mod test {
191195
let script_sig = p2shwpkh_script_sig(&public_key);
192196
assert_eq!(tx.input[0].script_sig, script_sig);
193197
}
198+
199+
/// Tests that passing an invalid proxy to `ElectrumUrl::build_client()`
200+
/// immediately results in an error.
201+
#[test]
202+
fn invalid_proxy() {
203+
let url = ElectrumUrl::Plaintext(String::new());
204+
205+
let invalid_proxy = "socks5://3.4.5.6";
206+
207+
assert!(matches!(
208+
url.build_client(Some(invalid_proxy), None),
209+
210+
Err(Error::ClientError(electrum_client::Error::IOError(err)))
211+
if err.kind() == std::io::ErrorKind::InvalidInput,
212+
));
213+
}
214+
215+
#[test]
216+
fn valid_proxy() {
217+
let url = ElectrumUrl::Plaintext(String::new());
218+
219+
// `build_client()` can still return an error, here we're just checking
220+
// that the error it returns (if any) is not caused by an incorrectly
221+
// formatted proxy.
222+
223+
for valid_proxy in ["127.0.0.1:9050", "socks5://127.0.0.1:9050"] {
224+
assert!(!matches!(
225+
url.build_client(Some(valid_proxy), None),
226+
227+
Err(Error::ClientError(electrum_client::Error::IOError(err)))
228+
if err.kind() == std::io::ErrorKind::InvalidInput,
229+
));
230+
}
231+
}
194232
}

subprojects/gdk_rust/gdk_rust/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ android_logger = "0.8.6"
2424
[dev-dependencies]
2525
gdk-test = { path = "../gdk_test" }
2626
tempfile = "3.2.0"
27-
electrum-client = { version = "0.12.0", default-features = false, features = [ "proxy", "use-openssl" ] }
27+
electrum-client = { version = "0.13.0", default-features = false, features = [ "proxy", "use-openssl" ] }
2828
bitcoin = { version = "0.29", features = [ "serde" ] }
2929
elements = { version = "0.20", features = ["serde"] }
3030
# TODO: remove "legacy" feature when upgrading electrs binaries

subprojects/gdk_rust/gdk_rust/tests/integration/integration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::thread;
44
use std::time::{Duration, Instant};
55

66
use electrsd::bitcoind::bitcoincore_rpc::RpcApi;
7-
use electrum_client::ElectrumApi;
7+
use electrsd::electrum_client::ElectrumApi;
88
use gdk_common::bitcoin::util::bip32::DerivationPath;
99
use gdk_common::bitcoin::Witness;
1010
use gdk_common::log::info;

subprojects/gdk_rust/gdk_test/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ edition = "2021"
55

66
[dependencies]
77
electrsd = { version = "0.22.0", features = [ "legacy" ] }
8-
electrum-client = { version = "0.12.0", default-features = false, features = [ "proxy", "use-openssl" ] }
8+
electrum-client = { version = "0.13.0", default-features = false, features = [ "proxy", "use-openssl" ] }
99
env_logger = "0.10.0"
1010
gdk-common = { path = "../gdk_common" }
1111
gdk-electrum = { path = "../gdk_electrum", features = ["testing"] }

subprojects/gdk_rust/gdk_test/src/test_session.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::thread;
33
use std::time::Duration;
44

55
use electrsd::bitcoind::bitcoincore_rpc::RpcApi;
6-
use electrum_client::ElectrumApi;
6+
use electrsd::electrum_client::ElectrumApi;
77
use gdk_common::bitcoin::hashes::hex::FromHex;
88
use gdk_common::bitcoin::secp256k1::SecretKey;
99
use gdk_common::bitcoin::Amount;

0 commit comments

Comments
 (0)