#ledger-hardware #ledger #hardware-wallet #simulator #hardware #wallet #speculos

bin+lib ledger-sim

Rust 的 Ledger Speculos 模拟器包装器

1 个不稳定版本

0.1.0 2023年6月16日

#12#ledger-hardware

Apache-2.0

28KB
579 代码行

通过本地安装或 Docker 镜像执行 Speculos 的 Rust 包装器,以简化与 ledger 应用程序的 CI/CD。

提供 Docker 和 Local 执行的驱动程序,并通过 Generic 抽象来支持运行时驱动程序选择。

示例

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(())
}

依赖项

~30–45MB
~590K SLoC