Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Separate interface from HWI calls and add HWI binary support #105

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
19 changes: 15 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,21 @@ jobs:
run: rustup set profile minimal
- name: Update toolchain
run: rustup update
- name: Test
run: cargo test --features ${{ matrix.features }}
- name: Wipe
run: cargo test test_wipe_device -- --ignored
- name: Download and Install HWI
run: ./get_hwi.sh
- name: Test python
run: cargo test --features ${{ matrix.features }} test_python -- --test-threads=1
- name: Restart simulator
if: matrix.emulator.name == 'ledger'
run: docker restart simulator
- name: Test binary
run: cargo test --features ${{ matrix.features }} test_binary -- --test-threads=1
- name: Wipe python
run: cargo test test_wipe_device_pyhton -- --ignored --test-threads=1
- name: Restart simulator
run: docker restart simulator
- name: Wipe binary
run: cargo test test_wipe_device_binary -- --ignored --test-threads=1
test-readme-examples:
runs-on: ubuntu-22.04
steps:
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ tags
venv/

backup*

# Binary downloaded for tests
hwi-binary/
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,17 @@ pip install -r requirements.txt
use bitcoin::Network;
use bitcoin::bip32::DerivationPath;
use hwi::error::Error;
use hwi::HWIClient;
use hwi::types::HWIClient;
use hwi::implementations::python_implementation::PythonHWIImplementation;
use std::str::FromStr;

fn main() -> Result<(), Error> {
let mut devices = HWIClient::enumerate()?;
let mut devices = HWIClient::<PythonHWIImplementation>::enumerate()?;
if devices.is_empty() {
panic!("No devices found!");
}
let first_device = devices.remove(0)?;
let client = HWIClient::get_client(&first_device, true, Network::Bitcoin.into())?;
let client = HWIClient::<PythonHWIImplementation>::get_client(&first_device, true, Network::Bitcoin.into())?;
let derivation_path = DerivationPath::from_str("m/44'/1'/0'/0/0").unwrap();
let s = client.sign_message("I love BDK wallet", &derivation_path)?;
println!("{:?}", s.signature);
Expand Down
58 changes: 58 additions & 0 deletions get_hwi.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/bash

set -e

# Function to detect OS and architecture
detect_platform() {
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)

case $OS in
linux)
case $ARCH in
x86_64) PLATFORM="linux-x86_64" ;;
aarch64) PLATFORM="linux-aarch64" ;;
*) echo "Unsupported Linux architecture: $ARCH"; exit 1 ;;
esac
;;
darwin)
case $ARCH in
x86_64) PLATFORM="mac-x86_64" ;;
arm64) PLATFORM="mac-arm64" ;;
*) echo "Unsupported macOS architecture: $ARCH"; exit 1 ;;
esac
;;
*) echo "Unsupported OS: $OS"; exit 1 ;;
esac

echo $PLATFORM
}

# Detect platform
PLATFORM=$(detect_platform)

# Set HWI version
HWI_VERSION="2.3.1"

# Set download URL
DOWNLOAD_URL="https://github.com/bitcoin-core/HWI/releases/download/${HWI_VERSION}/hwi-${HWI_VERSION}-${PLATFORM}.tar.gz"

# Set output directory
OUTPUT_DIR="hwi-binary"

# Create output directory if it doesn't exist
mkdir -p $OUTPUT_DIR
chmod 755 $OUTPUT_DIR

# Download and extract HWI
echo "Downloading HWI for $PLATFORM..."
curl -L $DOWNLOAD_URL -o hwi.tar.gz
echo "Download completed. Extracting..."
chmod 644 hwi.tar.gz
tar xzvf hwi.tar.gz -C $OUTPUT_DIR
rm hwi.tar.gz

# Make the binary executable
chmod +x $OUTPUT_DIR/hwi

echo "HWI binary downloaded and extracted to $OUTPUT_DIR/hwi"
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ pub enum Error {
Io(std::io::Error),
Hwi(String, Option<ErrorCode>),
Python(pyo3::PyErr),
NotImplemented,
}

impl fmt::Display for Error {
Expand All @@ -90,6 +91,7 @@ impl fmt::Display for Error {
Io(_) => f.write_str("I/O error"),
Hwi(ref s, ref code) => write!(f, "HWI error: {}, ({:?})", s, code),
Python(_) => f.write_str("python error"),
NotImplemented => f.write_str("not implemented"),
}
}
}
Expand All @@ -104,6 +106,7 @@ impl std::error::Error for Error {
Io(ref e) => Some(e),
Hwi(_, _) => None,
Python(ref e) => Some(e),
NotImplemented => None,
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/implementations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod binary_implementation;
pub mod python_implementation;
Loading
Loading