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

Move device init to command level #41

Merged
merged 1 commit into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cmd/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct Cmd {

impl Cmd {
pub fn run(&self, device: &Device) -> Result {
device.init()?;
let keypair = device.get_keypair(false)?;
let duration = bench_sign(&keypair, self.iterations)?;
let rate = self.iterations as f64 / duration.as_secs_f64();
Expand Down
1 change: 1 addition & 0 deletions src/cmd/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub struct Cmd {}

impl Cmd {
pub fn run(&self, device: &Device) -> Result {
device.init()?;
let config = device.get_config()?;
print_json(&config)
}
Expand Down
1 change: 1 addition & 0 deletions src/cmd/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub struct Cmd {}

impl Cmd {
pub fn run(&self, device: &Device) -> Result {
device.init()?;
let info = device.get_info()?;
print_json(&info)
}
Expand Down
1 change: 1 addition & 0 deletions src/cmd/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Cmd {

impl Cmd {
pub fn run(&self, device: &Device) -> Result {
device.init()?;
let keypair = device.get_keypair(self.generate)?;
print_keypair(&keypair)
}
Expand Down
1 change: 1 addition & 0 deletions src/cmd/provision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub struct Cmd {}

impl Cmd {
pub fn run(&self, device: &Device) -> Result {
device.init()?;
let keypair = device.provision()?;
print_keypair(&keypair)
}
Expand Down
1 change: 1 addition & 0 deletions src/cmd/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct Cmd {}

impl Cmd {
pub fn run(&self, device: &Device) -> Result {
device.init()?;
let tests = device.get_tests();
let results: Vec<(String, TestResult)> = tests
.iter()
Expand Down
6 changes: 1 addition & 5 deletions src/device/ecc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Device {
})
}

fn init_device(&self) -> Result {
pub fn init(&self) -> Result {
// Initialize the global instance if not already initialized
Ok(ecc608::init(
&self.path.to_string_lossy(),
Expand All @@ -72,7 +72,6 @@ impl Device {
}

pub fn get_info(&self) -> Result<Info> {
self.init_device()?;
let info = with_ecc(|ecc: &mut Ecc| {
ecc.get_info()
.and_then(|info| ecc.get_serial().map(|serial| Info { info, serial }))
Expand All @@ -81,7 +80,6 @@ impl Device {
}

pub fn get_keypair(&self, create: bool) -> Result<Keypair> {
self.init_device()?;
let keypair: Keypair = with_ecc(|ecc| {
if create {
generate_compact_key_in_slot(ecc, self.slot)
Expand All @@ -93,7 +91,6 @@ impl Device {
}

pub fn provision(&self) -> Result<Keypair> {
self.init_device()?;
let slot_config = ecc608::SlotConfig::default();
let key_config = ecc608::KeyConfig::default();
for slot in 0..=ecc608::MAX_SLOT {
Expand All @@ -107,7 +104,6 @@ impl Device {
}

pub fn get_config(&self) -> Result<Config> {
self.init_device()?;
let slot_config = with_ecc(|ecc| ecc.get_slot_config(self.slot))?;
let key_config = with_ecc(|ecc| ecc.get_key_config(self.slot))?;
let zones = [ecc608::Zone::Config, ecc608::Zone::Data]
Expand Down
8 changes: 8 additions & 0 deletions src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ impl DeviceArgs {
}

impl Device {
pub fn init(&self) -> Result {
match self {
#[cfg(feature = "ecc608")]
Self::Ecc(device) => device.init(),
_ => Ok(()),
}
}

pub fn get_info(&self) -> Result<Info> {
let info = match self {
#[cfg(feature = "ecc608")]
Expand Down