1 unstable release

0.1.0 Jun 16, 2023

#11 in #ledger-hardware

Apache-2.0

28KB
579 lines

Rust wrapper for executing Speculos via local install or docker image, provided to simplify CI/CD with ledger applications.

Drivers are provided for Docker and Local execution, with a Generic abstraction to support runtime driver selection.

Examples:

use ledger_sim::{GenericDriver, DriverMode, Driver, Model, Options};
use ledger_lib::{Device, transport::{Transport, TcpTransport, TcpInfo}, DEFAULT_TIMEOUT};
use ledger_proto::apdus::{AppInfoReq, AppInfoResp};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Setup driver for speculos connection
    let driver = GenericDriver::new(DriverMode::Docker)?;

    // Launch speculos with the provided app
    let opts = Options {
        model: Model::NanoX,
        apdu_port: Some(1237),
        ..Default::default()
    };
    let mut handle = driver.run("ledger-app", opts).await?;

    // Setup TCP APDU transport to speculos
    let mut transport = TcpTransport::new()?;
    let mut device = transport.connect(TcpInfo::default()).await?;

    // Fetch app info via transport
    let mut buff = [0u8; 256];
    let info = device.request::<AppInfoResp>(AppInfoReq{}, &mut buff, DEFAULT_TIMEOUT).await?;

    // Await simulator exit or exit signal
    tokio::select!(
        // Await simulator task completion
        _ = driver.wait(&mut handle) => {
            debug!("Complete!");
        }
        // Exit on ctrl + c
        _ = tokio::signal::ctrl_c() => {
            debug!("Exit!");
            driver.exit(handle).await?;
        },
    );

    Ok(())
}

Dependencies

~22–37MB
~575K SLoC