Skip to content

Commit

Permalink
Improve file-based tx send to not use private file (mimblewimble#1421)
Browse files Browse the repository at this point in the history
Instead of generating a private file on send that needs to be
provided again in finalize, the private context information is
saved in the wallet db.

Also move internal Context to bona fide libwallet type
  • Loading branch information
ignopeverell authored Aug 28, 2018
1 parent a58558b commit 4048a30
Show file tree
Hide file tree
Showing 14 changed files with 190 additions and 159 deletions.
1 change: 0 additions & 1 deletion src/bin/cmd/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

/// Grin client commands processing
use std::net::SocketAddr;

use clap::ArgMatches;
Expand Down
7 changes: 1 addition & 6 deletions src/bin/cmd/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,7 @@ pub fn wallet_command(wallet_args: &ArgMatches, global_config: GlobalConfig) {
let tx_file = send_args
.value_of("input")
.expect("Receiver's transaction file required");
let priv_file = send_args
.value_of("private")
.expect("Private transaction file required");
let slate = api
.file_finalize_tx(priv_file, tx_file)
.expect("Finalize failed");
let slate = api.file_finalize_tx(tx_file).expect("Finalize failed");

let result = api.post_tx(&slate, fluff);
match result {
Expand Down
7 changes: 1 addition & 6 deletions src/bin/grin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ fn main() {
.default_value("1")
.takes_value(true))
.arg(Arg::with_name("dest")
.help("Send the transaction to the provided server")
.help("Send the transaction to the provided server (start with http://) or save as file.")
.short("d")
.long("dest")
.takes_value(true))
Expand All @@ -232,11 +232,6 @@ fn main() {
.short("i")
.long("input")
.takes_value(true))
.arg(Arg::with_name("private")
.help("Private transaction file previously generated by send.")
.short("p")
.long("private")
.takes_value(true))
.arg(Arg::with_name("fluff")
.help("Fluff the transaction (ignore Dandelion relay protocol)")
.short("f")
Expand Down
17 changes: 8 additions & 9 deletions src/bin/tui/peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,14 @@ pub struct TUIPeerView;

impl TUIStatusListener for TUIPeerView {
fn create() -> Box<View> {
let table_view =
TableView::<PeerStats, PeerColumn>::new()
.column(PeerColumn::Address, "Address", |c| c.width_percent(20))
.column(PeerColumn::State, "State", |c| c.width_percent(20))
.column(PeerColumn::Direction, "Direction", |c| c.width_percent(20))
.column(PeerColumn::TotalDifficulty, "Total Difficulty", |c| {
c.width_percent(20)
})
.column(PeerColumn::Version, "Version", |c| c.width_percent(20));
let table_view = TableView::<PeerStats, PeerColumn>::new()
.column(PeerColumn::Address, "Address", |c| c.width_percent(20))
.column(PeerColumn::State, "State", |c| c.width_percent(20))
.column(PeerColumn::Direction, "Direction", |c| c.width_percent(20))
.column(PeerColumn::TotalDifficulty, "Total Difficulty", |c| {
c.width_percent(20)
})
.column(PeerColumn::Version, "Version", |c| c.width_percent(20));
let peer_status_view = BoxView::with_full_screen(
LinearLayout::new(Orientation::Vertical)
.child(
Expand Down
11 changes: 6 additions & 5 deletions src/bin/tui/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,8 @@ impl<T: TableViewItem<H>, H: Eq + Hash + Copy + Clone + 'static> TableView<T, H>
fn column_select(&mut self) {
let next = self.active_column();
let column = self.columns[next].column;
let current = self.columns
let current = self
.columns
.iter()
.position(|c| c.order != Ordering::Equal)
.unwrap_or(0);
Expand Down Expand Up @@ -736,10 +737,10 @@ impl<T: TableViewItem<H> + 'static, H: Eq + Hash + Copy + Clone + 'static> View
let column_count = self.columns.len();

// Split up all columns into sized / unsized groups
let (mut sized, mut usized): (Vec<&mut TableColumn<H>>, Vec<&mut TableColumn<H>>) =
self.columns
.iter_mut()
.partition(|c| c.requested_width.is_some());
let (mut sized, mut usized): (Vec<&mut TableColumn<H>>, Vec<&mut TableColumn<H>>) = self
.columns
.iter_mut()
.partition(|c| c.requested_width.is_some());

// Subtract one for the separators between our columns (that's column_count - 1)
let mut available_width = size.x.saturating_sub(column_count.saturating_sub(1) * 3);
Expand Down
18 changes: 17 additions & 1 deletion wallet/src/file_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use error::{Error, ErrorKind};
use libwallet;

use libwallet::types::{
OutputData, TxLogEntry, WalletBackend, WalletClient, WalletDetails, WalletOutputBatch,
Context, OutputData, TxLogEntry, WalletBackend, WalletClient, WalletDetails, WalletOutputBatch,
};

use types::{WalletConfig, WalletSeed};
Expand Down Expand Up @@ -121,6 +121,18 @@ where
unimplemented!()
}

fn save_private_context(
&mut self,
_slate_id: &[u8],
_ctx: &Context,
) -> Result<(), libwallet::Error> {
unimplemented!()
}

fn delete_private_context(&mut self, _slate_id: &[u8]) -> Result<(), libwallet::Error> {
unimplemented!()
}

fn commit(&self) -> Result<(), libwallet::Error> {
let mut data_file = File::create(self.data_file_path.clone())
.context(libwallet::ErrorKind::CallbackImpl("Could not create"))?;
Expand Down Expand Up @@ -239,6 +251,10 @@ where
Box::new(self.tx_log.iter().cloned())
}

fn get_private_context(&mut self, _slate_id: &[u8]) -> Result<Context, libwallet::Error> {
unimplemented!()
}

fn get(&self, id: &Identifier) -> Result<OutputData, libwallet::Error> {
self.outputs
.get(&id.to_hex())
Expand Down
24 changes: 14 additions & 10 deletions wallet/src/libwallet/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use core::core::hash::Hashed;
use core::ser;
use keychain::Keychain;
use libtx::slate::Slate;
use libwallet::internal::{selection, sigcontext, tx, updater};
use libwallet::internal::{selection, tx, updater};
use libwallet::types::{
BlockFees, CbData, OutputData, TxLogEntry, TxWrapper, WalletBackend, WalletClient, WalletInfo,
};
Expand Down Expand Up @@ -207,9 +207,12 @@ where
let mut pub_tx = File::create(dest)?;
pub_tx.write_all(json::to_string(&slate).unwrap().as_bytes())?;
pub_tx.sync_all()?;
let mut priv_tx = File::create(dest.to_owned() + ".private")?;
priv_tx.write_all(json::to_string(&context).unwrap().as_bytes())?;
priv_tx.sync_all()?;

{
let mut batch = w.batch()?;
batch.save_private_context(slate.id.as_bytes(), &context)?;
batch.commit()?;
}

// lock our inputs
lock_fn(&mut **w)?;
Expand Down Expand Up @@ -259,23 +262,24 @@ where
/// propagation.
pub fn file_finalize_tx(
&mut self,
private_tx_file: &str,
receiver_file: &str,
) -> Result<Slate, Error> {

let mut pub_tx_f = File::open(receiver_file)?;
let mut content = String::new();
pub_tx_f.read_to_string(&mut content)?;
let mut slate: Slate = json::from_str(&content).map_err(|_| ErrorKind::Format)?;

let mut priv_tx_f = File::open(private_tx_file)?;
let mut content = String::new();
priv_tx_f.read_to_string(&mut content)?;
let context: sigcontext::Context = json::from_str(&content).map_err(|_| ErrorKind::Format)?;

let mut w = self.wallet.lock().unwrap();
w.open_with_credentials()?;

let context = w.get_private_context(slate.id.as_bytes())?;
tx::complete_tx(&mut **w, &mut slate, &context)?;
{
let mut batch = w.batch()?;
batch.delete_private_context(slate.id.as_bytes())?;
batch.commit()?;
}

w.close()?;
Ok(slate)
Expand Down
1 change: 0 additions & 1 deletion wallet/src/libwallet/internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,5 @@
pub mod keys;
pub mod restore;
pub mod selection;
pub mod sigcontext;
pub mod tx;
pub mod updater;
17 changes: 5 additions & 12 deletions wallet/src/libwallet/internal/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use keychain::{Identifier, Keychain};
use libtx::{build, slate::Slate, tx_fee};
use libwallet::error::{Error, ErrorKind};
use libwallet::internal::{keys, sigcontext};
use libwallet::internal::keys;
use libwallet::types::*;

use util::LOGGER;
Expand All @@ -37,14 +37,7 @@ pub fn build_send_tx_slate<T: ?Sized, C, K>(
max_outputs: usize,
change_outputs: usize,
selection_strategy_is_use_all: bool,
) -> Result<
(
Slate,
sigcontext::Context,
impl FnOnce(&mut T) -> Result<(), Error>,
),
Error,
>
) -> Result<(Slate, Context, impl FnOnce(&mut T) -> Result<(), Error>), Error>
where
T: WalletBackend<C, K>,
C: WalletClient,
Expand Down Expand Up @@ -74,7 +67,7 @@ where
let blinding = slate.add_transaction_elements(&keychain, elems)?;

// Create our own private context
let mut context = sigcontext::Context::new(
let mut context = Context::new(
wallet.keychain().secp(),
blinding.secret_key(&keychain.secp()).unwrap(),
);
Expand Down Expand Up @@ -149,7 +142,7 @@ pub fn build_recipient_output_with_slate<T: ?Sized, C, K>(
) -> Result<
(
Identifier,
sigcontext::Context,
Context,
impl FnOnce(&mut T) -> Result<(), Error>,
),
Error,
Expand All @@ -173,7 +166,7 @@ where
slate.add_transaction_elements(&keychain, vec![build::output(amount, key_id.clone())])?;

// Add blinding sum to our context
let mut context = sigcontext::Context::new(
let mut context = Context::new(
keychain.secp(),
blinding
.secret_key(wallet.keychain().clone().secp())
Expand Down
84 changes: 0 additions & 84 deletions wallet/src/libwallet/internal/sigcontext.rs

This file was deleted.

15 changes: 4 additions & 11 deletions wallet/src/libwallet/internal/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use core::core::Transaction;
use keychain::{Identifier, Keychain};
use libtx::slate::Slate;
use libtx::{build, tx_fee};
use libwallet::internal::{selection, sigcontext, updater};
use libwallet::types::{TxLogEntryType, WalletBackend, WalletClient};
use libwallet::internal::{selection, updater};
use libwallet::types::{Context, TxLogEntryType, WalletBackend, WalletClient};
use libwallet::{Error, ErrorKind};
use util::LOGGER;

Expand Down Expand Up @@ -61,14 +61,7 @@ pub fn create_send_tx<T: ?Sized, C, K>(
max_outputs: usize,
num_change_outputs: usize,
selection_strategy_is_use_all: bool,
) -> Result<
(
Slate,
sigcontext::Context,
impl FnOnce(&mut T) -> Result<(), Error>,
),
Error,
>
) -> Result<(Slate, Context, impl FnOnce(&mut T) -> Result<(), Error>), Error>
where
T: WalletBackend<C, K>,
C: WalletClient,
Expand Down Expand Up @@ -117,7 +110,7 @@ where
pub fn complete_tx<T: ?Sized, C, K>(
wallet: &mut T,
slate: &mut Slate,
context: &sigcontext::Context,
context: &Context,
) -> Result<(), Error>
where
T: WalletBackend<C, K>,
Expand Down
Loading

0 comments on commit 4048a30

Please sign in to comment.