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

Slate binary/base64 encoding #112

Closed
wants to merge 16 commits into from
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 34 additions & 14 deletions controller/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ use crate::config::WalletConfig;
use crate::error::{Error, ErrorKind};
use crate::impls::{
instantiate_wallet, FileWalletCommAdapter, HTTPWalletCommAdapter, KeybaseWalletCommAdapter,
LMDBBackend, NullWalletCommAdapter,
LMDBBackend, NullWalletCommAdapter, StdioWalletCommAdapter,
};
use crate::impls::{HTTPNodeClient, WalletSeed};
use crate::libwallet::{InitTxArgs, IssueInvoiceTxArgs, NodeClient, WalletInst};
use crate::libwallet::{InitTxArgs, IssueInvoiceTxArgs, NodeClient, Slate, WalletInst};
use crate::{controller, display};

/// Arguments common to all wallet commands
Expand Down Expand Up @@ -296,6 +296,7 @@ pub fn send(
"file" => FileWalletCommAdapter::new(),
"keybase" => KeybaseWalletCommAdapter::new(),
"self" => NullWalletCommAdapter::new(),
"string" => StdioWalletCommAdapter::new(),
_ => NullWalletCommAdapter::new(),
};
if adapter.supports_sync() {
Expand Down Expand Up @@ -337,6 +338,7 @@ pub fn send(

/// Receive command argument
pub struct ReceiveArgs {
pub method: String,
pub input: String,
pub message: Option<String>,
}
Expand All @@ -346,7 +348,11 @@ pub fn receive(
g_args: &GlobalArgs,
args: ReceiveArgs,
) -> Result<(), Error> {
let adapter = FileWalletCommAdapter::new();
let adapter = match args.method.as_str() {
"file" => FileWalletCommAdapter::new(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our plan is to deprecate "file" then, right? I can't think of any reason we would want file support when we have something as simple as a string.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand, why would we want to deprecate the ability to save a slate as a file?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fully replaces every use case for file send/receive. One of our many usability issues right now is the fact that there are so many different ways to send/receive grin. Certain exchanges only support certain methods, and the same goes for wallets. It may sound counter-intuitive, but the way we make grin more usable is to support fewer transfer methods, not more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still not with you here. Are you suggesting that anyone using the command line wallet should have to cut and paste strings into the command line instead of being allowed to specify a file name, and everyone creating a new transaction for email send (for instance) would either have to cut/paste or pipe output into a file?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, seems easy enough. Regardless, I'm not as worried about the 'file' method as having a separate serialization format for files. If we're going to serialize slates to base64 for files too, then I'm ok with keeping it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can keep the file as is for now and have it output a hex or b64 file after the hard fork

"string" => StdioWalletCommAdapter::new(),
_ => NullWalletCommAdapter::new(),
};
let mut slate = adapter.receive_tx_async(&args.input)?;
controller::foreign_single_use(wallet, |api| {
if let Err(e) = api.verify_slate_messages(&slate) {
Expand All @@ -358,15 +364,18 @@ pub fn receive(
})?;
let send_tx = format!("{}.response", args.input);
adapter.send_tx_async(&send_tx, &slate)?;
info!(
"Response file {}.response generated, and can be sent back to the transaction originator.",
args.input
);
if args.method.as_str() == "file" {
info!(
"Response file {}.response generated, sending it back to the transaction originator.",
args.input
);
}
Ok(())
}

/// Finalize command args
pub struct FinalizeArgs {
pub method: String,
pub input: String,
pub fluff: bool,
}
Expand All @@ -375,7 +384,11 @@ pub fn finalize(
wallet: Arc<Mutex<WalletInst<impl NodeClient + 'static, keychain::ExtKeychain>>>,
args: FinalizeArgs,
) -> Result<(), Error> {
let adapter = FileWalletCommAdapter::new();
let adapter = match args.method.as_str() {
"file" => FileWalletCommAdapter::new(),
"string" => StdioWalletCommAdapter::new(),
_ => NullWalletCommAdapter::new(),
};
let mut slate = adapter.receive_tx_async(&args.input)?;
// Rather than duplicating the entire command, we'll just
// try to determine what kind of finalization this is
Expand Down Expand Up @@ -436,6 +449,7 @@ pub fn finalize(
pub struct IssueInvoiceArgs {
/// output file
pub dest: String,
pub method: String,
/// issue invoice tx args
pub issue_args: IssueInvoiceTxArgs,
}
Expand All @@ -446,9 +460,14 @@ pub fn issue_invoice_tx(
) -> Result<(), Error> {
controller::owner_single_use(wallet.clone(), |api| {
let slate = api.issue_invoice_tx(args.issue_args)?;
let mut tx_file = File::create(args.dest.clone())?;
tx_file.write_all(json::to_string(&slate).unwrap().as_bytes())?;
tx_file.sync_all()?;
let adapter = match args.method.as_str() {
"file" => FileWalletCommAdapter::new(),
"string" => StdioWalletCommAdapter::new(),
_ => NullWalletCommAdapter::new(),
};

adapter.send_tx_async(args.dest.as_str(), &slate)?;

Ok(())
})?;
Ok(())
Expand All @@ -462,7 +481,8 @@ pub struct ProcessInvoiceArgs {
pub method: String,
pub dest: String,
pub max_outputs: usize,
pub input: String,
pub target_slate_version: Option<u16>,
pub slate: Slate,
pub estimate_selection_strategies: bool,
}

Expand All @@ -472,8 +492,7 @@ pub fn process_invoice(
args: ProcessInvoiceArgs,
dark_scheme: bool,
) -> Result<(), Error> {
let adapter = FileWalletCommAdapter::new();
let slate = adapter.receive_tx_async(&args.input)?;
let slate = args.slate.clone();
controller::owner_single_use(wallet.clone(), |api| {
if args.estimate_selection_strategies {
let strategies = vec!["smallest", "all"]
Expand Down Expand Up @@ -530,6 +549,7 @@ pub fn process_invoice(
"http" => HTTPWalletCommAdapter::new(),
"file" => FileWalletCommAdapter::new(),
"self" => NullWalletCommAdapter::new(),
"string" => StdioWalletCommAdapter::new(),
_ => NullWalletCommAdapter::new(),
};
if adapter.supports_sync() {
Expand Down
1 change: 1 addition & 0 deletions impls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ tokio-core = "0.1"
tokio-retry = "0.1"
uuid = { version = "0.7", features = ["serde", "v4"] }
chrono = { version = "0.4.4", features = ["serde"] }
base64 = "0.9"

grin_wallet_util = { path = "../util", version = "2.0.0-beta.1" }

Expand Down
3 changes: 3 additions & 0 deletions impls/src/adapters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ mod file;
mod http;
mod keybase;
mod null;
mod stdio;

pub use self::file::FileWalletCommAdapter;
pub use self::http::HTTPWalletCommAdapter;
pub use self::keybase::KeybaseWalletCommAdapter;
pub use self::null::NullWalletCommAdapter;
pub use self::stdio::StdioWalletCommAdapter;

use crate::config::WalletConfig;
use crate::libwallet::{Error, Slate};
use std::collections::HashMap;
extern crate base64;

/// Encapsulate wallet to wallet communication functions
pub trait WalletCommAdapter {
Expand Down
78 changes: 78 additions & 0 deletions impls/src/adapters/stdio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2018 The Grin Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/// Standard Input/Output 'plugin' implementation
use std::io::{stdin, Read};

use crate::base64;
use crate::config::WalletConfig;
use crate::libwallet::{Error, ErrorKind, Slate};
use crate::WalletCommAdapter;
use std::collections::HashMap;

#[derive(Clone)]
pub struct StdioWalletCommAdapter {}

impl StdioWalletCommAdapter {
/// Create
pub fn new() -> Box<dyn WalletCommAdapter> {
Box::new(StdioWalletCommAdapter {})
}
}

impl WalletCommAdapter for StdioWalletCommAdapter {
fn supports_sync(&self) -> bool {
false
}

fn send_tx_sync(&self, _dest: &str, _slate: &Slate) -> Result<Slate, Error> {
unimplemented!();
}

fn send_tx_async(&self, _dest: &str, slate: &Slate) -> Result<(), Error> {
let bytes = slate.to_bytes()?;
println!("{}", base64::encode(&bytes));
Ok(())
}

fn receive_tx_async(&self, params: &str) -> Result<Slate, Error> {
let params = params.trim();
// if user passed the string as input decode that, else
// read from stdin
let b64string = match params {
"" => {
let mut stream = stdin();
let mut content = String::new();
stream.read_to_string(&mut content)?;
content
}
_ => params.to_owned(),
};

let bytes = base64::decode(b64string.as_bytes()).map_err(|_| ErrorKind::SlateDeser)?;
let slate = Slate::from_bytes(&bytes)?;
Ok(slate)
}

fn listen(
&self,
_params: HashMap<String, String>,
_config: WalletConfig,
_passphrase: &str,
_account: &str,
_node_api_secret: Option<String>,
) -> Result<(), Error> {
unimplemented!();
}
}
3 changes: 2 additions & 1 deletion impls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use grin_wallet_util::grin_core as core;
use grin_wallet_util::grin_keychain as keychain;
use grin_wallet_util::grin_store as store;
use grin_wallet_util::grin_util as util;
extern crate base64;
extern crate grin_wallet_config as config;

mod adapters;
Expand All @@ -40,7 +41,7 @@ pub mod test_framework;

pub use crate::adapters::{
FileWalletCommAdapter, HTTPWalletCommAdapter, KeybaseWalletCommAdapter, NullWalletCommAdapter,
WalletCommAdapter,
StdioWalletCommAdapter, WalletCommAdapter,
};
pub use crate::backends::{wallet_db_exists, LMDBBackend};
pub use crate::error::{Error, ErrorKind};
Expand Down
2 changes: 1 addition & 1 deletion libwallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ extern crate serde_derive;
extern crate log;
#[macro_use]
extern crate lazy_static;

extern crate strum;

#[macro_use]
extern crate strum_macros;

Expand Down
Loading