2026-04-17 19:08:45 -04:00
|
|
|
use anyhow::Result;
|
|
|
|
|
use clap::Subcommand;
|
2026-04-24 22:10:04 -04:00
|
|
|
use keycard_wallet::{KeycardWallet, python_path};
|
2026-04-17 19:08:45 -04:00
|
|
|
use pyo3::prelude::*;
|
|
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
|
WalletCore,
|
2026-05-12 18:07:44 -04:00
|
|
|
cli::{SubcommandReturnValue, WalletSubcommand, read_mnemonic, read_pin},
|
2026-04-17 19:08:45 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// Represents generic chain CLI subcommand.
|
|
|
|
|
#[derive(Subcommand, Debug, Clone)]
|
|
|
|
|
pub enum KeycardSubcommand {
|
|
|
|
|
Available,
|
2026-05-12 18:07:44 -04:00
|
|
|
Load,
|
2026-04-17 19:08:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl WalletSubcommand for KeycardSubcommand {
|
|
|
|
|
async fn handle_subcommand(
|
|
|
|
|
self,
|
|
|
|
|
_wallet_core: &mut WalletCore,
|
|
|
|
|
) -> Result<SubcommandReturnValue> {
|
|
|
|
|
match self {
|
|
|
|
|
Self::Available => {
|
|
|
|
|
Python::with_gil(|py| {
|
|
|
|
|
python_path::add_python_path(py).expect("keycard_wallet.py not found");
|
|
|
|
|
|
2026-04-28 21:33:12 -04:00
|
|
|
let wallet = KeycardWallet::new(py)
|
|
|
|
|
.expect("`wallet::keycard::available`: invalid data received for pin");
|
|
|
|
|
let available = wallet.is_unpaired_keycard_available(py).expect(
|
|
|
|
|
"`wallet::keycard::available`: received invalid data from Keycard wrapper",
|
|
|
|
|
);
|
2026-04-17 19:08:45 -04:00
|
|
|
|
2026-04-21 18:27:14 -04:00
|
|
|
if available {
|
|
|
|
|
println!("\u{2705} Keycard is available.");
|
|
|
|
|
} else {
|
|
|
|
|
println!("\u{274c} Keycard is not available.");
|
|
|
|
|
}
|
2026-04-17 19:08:45 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Ok(SubcommandReturnValue::Empty)
|
2026-04-23 17:45:43 -04:00
|
|
|
}
|
2026-05-12 18:07:44 -04:00
|
|
|
Self::Load => {
|
2026-04-30 19:02:33 -04:00
|
|
|
let pin = read_pin()?;
|
2026-05-12 18:07:44 -04:00
|
|
|
let mnemonic = read_mnemonic()?;
|
2026-04-30 19:02:33 -04:00
|
|
|
|
2026-04-17 19:08:45 -04:00
|
|
|
Python::with_gil(|py| {
|
|
|
|
|
python_path::add_python_path(py).expect("keycard_wallet.py not found");
|
|
|
|
|
|
2026-04-28 21:33:12 -04:00
|
|
|
let wallet = KeycardWallet::new(py)
|
|
|
|
|
.expect("`wallet::keycard::load`: invalid keycard wallet provided");
|
2026-04-17 19:08:45 -04:00
|
|
|
|
2026-04-23 17:45:43 -04:00
|
|
|
let is_connected = wallet
|
2026-04-30 19:02:33 -04:00
|
|
|
.setup_communication(py, &pin)
|
2026-04-23 17:45:43 -04:00
|
|
|
.expect("Expect a Boolean.");
|
2026-04-21 18:27:14 -04:00
|
|
|
|
|
|
|
|
if is_connected {
|
|
|
|
|
println!("\u{2705} Keycard is now connected to wallet.");
|
|
|
|
|
} else {
|
|
|
|
|
println!("\u{274c} Keycard is not connected to wallet.");
|
|
|
|
|
}
|
2026-04-17 19:08:45 -04:00
|
|
|
|
2026-05-12 18:07:44 -04:00
|
|
|
drop(wallet.load_mnemonic(py, &mnemonic));
|
2026-04-17 19:08:45 -04:00
|
|
|
|
2026-04-27 18:47:02 -04:00
|
|
|
drop(wallet.disconnect(py));
|
2026-04-23 17:45:43 -04:00
|
|
|
});
|
2026-04-17 19:08:45 -04:00
|
|
|
|
2026-04-23 17:45:43 -04:00
|
|
|
Ok(SubcommandReturnValue::Empty)
|
|
|
|
|
}
|
2026-04-17 19:08:45 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|