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

Add option to finalize and post transactions separately (for air-gap wallets) #255

Merged
merged 2 commits into from
Nov 18, 2019
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
60 changes: 48 additions & 12 deletions controller/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,8 @@ where
pub struct FinalizeArgs {
pub input: String,
pub fluff: bool,
pub nopost: bool,
pub dest: Option<String>,
}

pub fn finalize<'a, L, C, K>(
Expand Down Expand Up @@ -462,19 +464,27 @@ where
})?;
}

controller::owner_single_use(wallet.clone(), keychain_mask, |api, m| {
let result = api.post_tx(m, &slate.tx, args.fluff);
match result {
Ok(_) => {
info!("Transaction sent successfully, check the wallet again for confirmation.");
Ok(())
}
Err(e) => {
error!("Tx not sent: {}", e);
Err(e)
if !args.nopost {
controller::owner_single_use(wallet.clone(), keychain_mask, |api, m| {
let result = api.post_tx(m, &slate.tx, args.fluff);
match result {
Ok(_) => {
info!(
"Transaction sent successfully, check the wallet again for confirmation."
);
Ok(())
}
Err(e) => {
error!("Tx not sent: {}", e);
Err(e)
}
}
}
})?;
})?;
}

if args.dest.is_some() {
PathToSlate((&args.dest.unwrap()).into()).put_tx(&slate)?;
}

Ok(())
}
Expand Down Expand Up @@ -720,6 +730,32 @@ where
Ok(())
}

/// Post
pub struct PostArgs {
pub input: String,
pub fluff: bool,
}

pub fn post<'a, L, C, K>(
wallet: Arc<Mutex<Box<dyn WalletInst<'a, L, C, K>>>>,
keychain_mask: Option<&SecretKey>,
args: PostArgs,
) -> Result<(), Error>
where
L: WalletLCProvider<'a, C, K>,
C: NodeClient + 'a,
K: keychain::Keychain + 'a,
{
let slate = PathToSlate((&args.input).into()).get_tx()?;

controller::owner_single_use(wallet.clone(), keychain_mask, |api, m| {
api.post_tx(m, &slate.tx, args.fluff)?;
info!("Posted transaction");
return Ok(());
})?;
Ok(())
}

/// Repost
pub struct RepostArgs {
pub id: u32,
Expand Down
21 changes: 21 additions & 0 deletions src/bin/grin-wallet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,15 @@ subcommands:
help: Fluff the transaction (ignore Dandelion relay protocol)
short: f
long: fluff
- nopost:
help: Do not post the transaction.
short: n
long: nopost
- dest:
help: Specify file to save the finalized slate.
short: d
long: dest
takes_value: true
- invoice:
about: Initialize an invoice transaction.
args:
Expand Down Expand Up @@ -259,6 +268,18 @@ subcommands:
short: t
long: txid
takes_value: true
- post:
about: Posts a finalized transaction to the chain
args:
- input:
help: File name of the transaction to post
short: i
long: input
takes_value: true
- fluff:
help: Fluff the transaction (ignore Dandelion relay protocol)
short: f
long: fluff
- repost:
about: Reposts a stored, completed but unconfirmed transaction to the chain, or dumps it to a file
args:
Expand Down
23 changes: 23 additions & 0 deletions src/cmd/wallet_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,15 +559,24 @@ pub fn parse_receive_args(receive_args: &ArgMatches) -> Result<command::ReceiveA

pub fn parse_finalize_args(args: &ArgMatches) -> Result<command::FinalizeArgs, ParseError> {
let fluff = args.is_present("fluff");
let nopost = args.is_present("nopost");
let tx_file = parse_required(args, "input")?;

if !Path::new(&tx_file).is_file() {
let msg = format!("File {} not found.", tx_file);
return Err(ParseError::ArgumentError(msg));
}

let dest_file = match args.is_present("dest") {
true => Some(args.value_of("dest").unwrap().to_owned()),
false => None,
};

Ok(command::FinalizeArgs {
input: tx_file.to_owned(),
fluff: fluff,
nopost: nopost,
dest: dest_file.to_owned(),
})
}

Expand Down Expand Up @@ -738,6 +747,16 @@ pub fn parse_txs_args(args: &ArgMatches) -> Result<command::TxsArgs, ParseError>
})
}

pub fn parse_post_args(args: &ArgMatches) -> Result<command::PostArgs, ParseError> {
let tx_file = parse_required(args, "input")?;
let fluff = args.is_present("fluff");

Ok(command::PostArgs {
input: tx_file.to_owned(),
fluff: fluff,
})
}

pub fn parse_repost_args(args: &ArgMatches) -> Result<command::RepostArgs, ParseError> {
let tx_id = match args.value_of("id") {
None => None,
Expand Down Expand Up @@ -1012,6 +1031,10 @@ where
wallet_config.dark_background_color_scheme.unwrap_or(true),
)
}
("post", Some(args)) => {
let a = arg_parse!(parse_post_args(&args));
command::post(wallet, km, a)
}
("repost", Some(args)) => {
let a = arg_parse!(parse_repost_args(&args));
command::repost(wallet, km, a)
Expand Down