Skip to content

Commit e178b63

Browse files
committed
unzip windows bitcoin binaries
1 parent 325499d commit e178b63

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ env_logger = "0.8"
2121
[build-dependencies]
2222
bitcoin_hashes = "0.10"
2323
tar = "0.4"
24+
zip = "0.6"
2425

2526
# allows to keep MSRV 1.41.1
2627
ureq = "1.0"

build.rs

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use bitcoin_hashes::{sha256, Hash};
22
use flate2::read::GzDecoder;
33
use std::fs::File;
4-
use std::io::{BufRead, BufReader, Read};
4+
use std::io::{self, BufRead, BufReader, Cursor, Read};
55
use std::path::Path;
66
use std::str::FromStr;
77
use tar::Archive;
@@ -43,7 +43,7 @@ fn download_filename() -> String {
4343

4444
#[cfg(all(target_os = "windows", target_arch = "x86_64"))]
4545
fn download_filename() -> String {
46-
format!("bitcoin-{}-win64-unsigned.tar.gz", &VERSION)
46+
format!("bitcoin-{}-win64.zip", &VERSION)
4747
}
4848

4949
fn get_expected_sha256(filename: &str) -> sha256::Hash {
@@ -82,7 +82,7 @@ fn main() {
8282
let download_filename = download_filename();
8383
let expected_hash = get_expected_sha256(&download_filename);
8484
let out_dir = std::env::var_os("OUT_DIR").unwrap();
85-
let bitcoin_exe_home = Path::new(&out_dir).join("bitcoin");
85+
let mut bitcoin_exe_home = Path::new(&out_dir).join("bitcoin");
8686
if !bitcoin_exe_home.exists() {
8787
std::fs::create_dir(&bitcoin_exe_home).unwrap();
8888
}
@@ -101,23 +101,46 @@ fn main() {
101101
"https://bitcoincore.org/bin/bitcoin-core-{}/{}",
102102
VERSION, download_filename
103103
);
104+
println!("url:{}", url);
104105
let mut downloaded_bytes = Vec::new();
105106
let resp = ureq::get(&url).call();
106-
assert_eq!(resp.status(), 200);
107+
assert_eq!(resp.status(), 200, "url {} didn't return 200", url);
107108

108109
let _size = resp
109110
.into_reader()
110111
.read_to_end(&mut downloaded_bytes)
111112
.unwrap();
112113
let downloaded_hash = sha256::Hash::hash(&downloaded_bytes);
113114
assert_eq!(expected_hash, downloaded_hash);
114-
let d = GzDecoder::new(&downloaded_bytes[..]);
115115

116-
let mut archive = Archive::new(d);
117-
for mut entry in archive.entries().unwrap().flatten() {
118-
if let Ok(file) = entry.path() {
119-
if file.ends_with("bitcoind") {
120-
entry.unpack_in(&bitcoin_exe_home).unwrap();
116+
if download_filename.ends_with(".tar.gz") {
117+
let d = GzDecoder::new(&downloaded_bytes[..]);
118+
119+
let mut archive = Archive::new(d);
120+
for mut entry in archive.entries().unwrap().flatten() {
121+
if let Ok(file) = entry.path() {
122+
if file.ends_with("bitcoind") {
123+
entry.unpack_in(&bitcoin_exe_home).unwrap();
124+
}
125+
}
126+
}
127+
} else if download_filename.ends_with(".zip") {
128+
let cursor = Cursor::new(downloaded_bytes);
129+
let mut archive = zip::ZipArchive::new(cursor).unwrap();
130+
for i in 0..zip::ZipArchive::len(&archive) {
131+
let mut file = archive.by_index(i).unwrap();
132+
let outpath = match file.enclosed_name() {
133+
Some(path) => path.to_owned(),
134+
None => continue,
135+
};
136+
137+
if outpath.file_name().map(|s| s.to_str()) == Some(Some("bitcoind.exe")) {
138+
bitcoin_exe_home.push(outpath);
139+
std::fs::create_dir_all(&bitcoin_exe_home).unwrap();
140+
println!("{:?}", bitcoin_exe_home);
141+
let mut outfile = std::fs::File::create(&bitcoin_exe_home).unwrap();
142+
io::copy(&mut file, &mut outfile).unwrap();
143+
break;
121144
}
122145
}
123146
}

src/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,11 +416,17 @@ impl From<bitcoincore_rpc::Error> for Error {
416416

417417
/// Provide the bitcoind executable path if a version feature has been specified
418418
pub fn downloaded_exe_path() -> Result<String, Error> {
419+
let ext = if cfg!(target_os = "windows") {
420+
".exe"
421+
} else {
422+
""
423+
};
419424
if versions::HAS_FEATURE {
420425
Ok(format!(
421-
"{}/bitcoin/bitcoin-{}/bin/bitcoind",
426+
"{}/bitcoin/bitcoin-{}/bin/bitcoind{}",
422427
env!("OUT_DIR"),
423-
versions::VERSION
428+
versions::VERSION,
429+
ext
424430
))
425431
} else {
426432
Err(Error::NoFeature)

0 commit comments

Comments
 (0)