Skip to content

Commit

Permalink
improve wallet support
Browse files Browse the repository at this point in the history
  • Loading branch information
Fraccaman committed Sep 4, 2024
1 parent d02b85f commit 0a33340
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
47 changes: 35 additions & 12 deletions crates/apps_lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const WALLET_CMD: &str = "wallet";
const RELAYER_CMD: &str = "relayer";

pub mod cmds {
use super::args::CliTypes;
use super::utils::*;
use super::{
args, ArgMatches, CLIENT_CMD, NODE_CMD, RELAYER_CMD, WALLET_CMD,
Expand Down Expand Up @@ -3195,21 +3196,21 @@ pub mod cmds {
}

#[derive(Clone, Debug)]
pub struct SignOffline(pub args::SignOffline);
pub struct SignOffline(pub args::SignOffline<CliTypes>);

impl SubCmd for SignOffline {
const CMD: &'static str = "sign-offline";

fn parse(matches: &ArgMatches) -> Option<Self> {
matches
.subcommand_matches(Self::CMD)
.map(|matches| Self(args::SignOffline::parse(matches)))
matches.subcommand_matches(Self::CMD).map(|matches| {
Self(args::SignOffline::<CliTypes>::parse(matches))
})
}

fn def() -> App {
App::new(Self::CMD)
.about(wrap!("Offlne sign a transaction."))
.add_args::<args::SignOffline>()
.add_args::<args::SignOffline<CliTypes>>()
}
}

Expand Down Expand Up @@ -3468,7 +3469,7 @@ pub mod args {
DefaultFn(|| PortId::from_str("transfer").unwrap()),
);
pub const PRE_GENESIS: ArgFlag = flag("pre-genesis");
pub const PRIVATE_KEYS: ArgMulti<common::SecretKey, GlobStar> =
pub const PRIVATE_KEYS: ArgMulti<WalletKeypair, GlobStar> =
arg_multi("secret-keys");
pub const PROPOSAL_PGF_STEWARD: ArgFlag = flag("pgf-stewards");
pub const PROPOSAL_PGF_FUNDING: ArgFlag = flag("pgf-funding");
Expand Down Expand Up @@ -8063,18 +8064,18 @@ pub mod args {
}

#[derive(Clone, Debug)]
pub struct SignOffline {
pub struct SignOffline<C: NamadaTypes = SdkTypes> {
pub tx_path: PathBuf,
pub secret_keys: Vec<common::SecretKey>,
pub owner: Address,
pub secret_keys: Vec<C::Keypair>,
pub owner: C::Address,
pub output_folder_path: Option<PathBuf>,
}

impl Args for SignOffline {
impl Args for SignOffline<CliTypes> {
fn parse(matches: &ArgMatches) -> Self {
let tx_path = DATA_PATH.parse(matches);
let secret_keys = PRIVATE_KEYS.parse(matches);
let owner = RAW_ADDRESS.parse(matches);
let owner = OWNER.parse(matches);
let output_folder_path = OUTPUT_FOLDER_PATH.parse(matches);

Self {
Expand All @@ -8095,7 +8096,7 @@ pub mod args {
"The set of private keys to use to sign the transaction. The \
order matters."
)))
.arg(RAW_ADDRESS.def().help(wrap!("The owner's address.")))
.arg(OWNER.def().help(wrap!("The owner's address.")))
.arg(
OUTPUT_FOLDER_PATH
.def()
Expand All @@ -8104,6 +8105,28 @@ pub mod args {
}
}

impl CliToSdk<SignOffline<SdkTypes>> for SignOffline<CliTypes> {
type Error = std::io::Error;

fn to_sdk(
self,
ctx: &mut Context,
) -> Result<SignOffline<SdkTypes>, Self::Error> {
let chain_ctx = ctx.borrow_mut_chain_or_exit();

Ok(SignOffline::<SdkTypes> {
tx_path: self.tx_path,
secret_keys: self
.secret_keys
.iter()
.map(|key| chain_ctx.get_cached(key))
.collect(),
owner: chain_ctx.get(&self.owner),
output_folder_path: self.output_folder_path,
})
}
}

#[derive(Clone, Debug)]
pub struct DefaultBaseDir {}

Expand Down
5 changes: 4 additions & 1 deletion crates/apps_lib/src/cli/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,10 @@ impl CliApi {
utils::pk_to_tm_address(global_args, args)
}
ClientUtils::SignOffline(SignOffline(args)) => {
utils::sign_offline(global_args, args).await
let mut ctx = cli::Context::new::<IO>(global_args)
.expect("expected to construct a context");
let args = args.to_sdk(&mut ctx)?;
utils::sign_offline(args).await
}
ClientUtils::DefaultBaseDir(DefaultBaseDir(args)) => {
utils::default_base_dir(global_args, args)
Expand Down
1 change: 0 additions & 1 deletion crates/apps_lib/src/client/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,6 @@ pub async fn sign_genesis_tx(

/// Offline sign a transactions.
pub async fn sign_offline(
_global_args: args::Global,
args::SignOffline {
tx_path,
secret_keys,
Expand Down
2 changes: 1 addition & 1 deletion crates/tests/src/integration/ledger_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1632,7 +1632,7 @@ fn offline_sign() -> Result<()> {
"sign-offline",
"--data-path",
&offline_tx,
"--address",
"--owner",
&bertha_address,
"--secret-keys",
&bertha_sk,
Expand Down

0 comments on commit 0a33340

Please sign in to comment.