Skip to content

Commit

Permalink
Proof of Payment Command Line (mimblewimble#260)
Browse files Browse the repository at this point in the history
* refactor address generation code into libwallet, bool to flag whether to include proof, add sender address in init_send_tx

* rustfmt

* require payment proof addr as part of init_tx

* rustfmt

* store payment proof on sender transaction side

* rustfmt

* change sig to ed25519 sig

* rustfmt

* add message creation and signature

* rustfmt

* add payment proof verification function

* rustfmt

* validate proof on sender side, store proof

* rustfmt

* fix json tests

* fixes and updates to tests

* added API functions for converting and retrieving proof addresses

* rustfmt

* add payment proof to init_send_tx example

* rustfmt

* incorrect comment

* add commands for requesting payment proofs

* rustfmt

* wire up payment proofs into command line

* rustfmt

* add address command

* rustfmt

* added tor sending from owner api

* rustfmt
  • Loading branch information
yeastplume authored Nov 28, 2019
1 parent 7293ca9 commit 9fd1d49
Show file tree
Hide file tree
Showing 11 changed files with 279 additions and 28 deletions.
21 changes: 19 additions & 2 deletions api/src/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ where
/// Holds all update and status messages returned by the
/// updater process
updater_messages: Arc<Mutex<Vec<StatusMessage>>>,
/// Optional TOR configuration, holding address of sender and
/// data directory
tor_config: Mutex<Option<TorConfig>>,
}

impl<L, C, K> Owner<L, C, K>
Expand Down Expand Up @@ -176,9 +179,23 @@ where
updater_running,
status_tx: Mutex::new(Some(tx)),
updater_messages,
tor_config: Mutex::new(None),
}
}

/// Set the TOR configuration for this instance of the OwnerAPI, used during
/// `init_send_tx` when send args are present and a TOR address is specified
///
/// # Arguments
/// * `tor_config` - The optional [TorConfig](#) to use
/// # Returns
/// * Nothing

pub fn set_tor_config(&self, tor_config: Option<TorConfig>) {
let mut lock = self.tor_config.lock();
*lock = tor_config;
}

/// Returns a list of accounts stored in the wallet (i.e. mappings between
/// user-specified labels and BIP32 derivation paths.
/// # Arguments
Expand Down Expand Up @@ -636,8 +653,8 @@ where
.into());
}
};
//TODO: no TOR just now via this method, to keep compatibility for now
let comm_adapter = create_sender(&sa.method, &sa.dest, None)
let tor_config_lock = self.tor_config.lock();
let comm_adapter = create_sender(&sa.method, &sa.dest, tor_config_lock.clone())
.map_err(|e| ErrorKind::GenericError(format!("{}", e)))?;
slate = comm_adapter.send_tx(&slate)?;
self.tx_lock_outputs(keychain_mask, &slate, 0)?;
Expand Down
38 changes: 38 additions & 0 deletions api/src/owner_rpc_s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1849,6 +1849,39 @@ pub trait OwnerRpcS {
*/

fn proof_address_from_onion_v3(&self, address_v3: String) -> Result<PubAddress, ErrorKind>;

/**
Networked version of [Owner::set_tor_config](struct.Owner.html#method.set_tor_config).
```
# grin_wallet_api::doctest_helper_json_rpc_owner_assert_response!(
# r#"
{
"jsonrpc": "2.0",
"method": "set_tor_config",
"params": {
"tor_config": {
"use_tor_listener": true,
"socks_proxy_addr": "127.0.0.1:59050",
"send_config_dir": "."
}
},
"id": 1
}
# "#
# ,
# r#"
{
"id": 1,
"jsonrpc": "2.0",
"result": {
"Ok": null
}
}
# "#
# , true, 0, false, false, false);
```
*/
fn set_tor_config(&self, tor_config: Option<TorConfig>) -> Result<(), ErrorKind>;
}

impl<L, C, K> OwnerRpcS for Owner<L, C, K>
Expand Down Expand Up @@ -2173,4 +2206,9 @@ where
Owner::proof_address_from_onion_v3(self, &address_v3).map_err(|e| e.kind())?;
Ok(PubAddress { address })
}

fn set_tor_config(&self, tor_config: Option<TorConfig>) -> Result<(), ErrorKind> {
Owner::set_tor_config(self, tor_config);
Ok(())
}
}
53 changes: 51 additions & 2 deletions controller/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ use crate::error::{Error, ErrorKind};
use crate::impls::{create_sender, KeybaseAllChannels, SlateGetter as _, SlateReceiver as _};
use crate::impls::{PathToSlate, SlatePutter};
use crate::keychain;
use crate::libwallet::{InitTxArgs, IssueInvoiceTxArgs, NodeClient, WalletInst, WalletLCProvider};
use crate::libwallet::{
address, InitTxArgs, IssueInvoiceTxArgs, NodeClient, WalletInst, WalletLCProvider,
};
use crate::util::secp::key::SecretKey;
use crate::util::{Mutex, ZeroingString};
use crate::util::{to_hex, Mutex, ZeroingString};
use crate::{controller, display};
use serde_json as json;
use std::fs::File;
Expand Down Expand Up @@ -173,6 +175,7 @@ pub fn owner_api<L, C, K>(
wallet: Arc<Mutex<Box<dyn WalletInst<'static, L, C, K>>>>,
keychain_mask: Option<SecretKey>,
config: &WalletConfig,
tor_config: &TorConfig,
g_args: &GlobalArgs,
) -> Result<(), Error>
where
Expand All @@ -190,6 +193,7 @@ where
g_args.api_secret.clone(),
g_args.tls_conf.clone(),
config.owner_api_include_foreign.clone(),
Some(tor_config.clone()),
);
if let Err(e) = res {
return Err(ErrorKind::LibWallet(e.kind(), e.cause_string()).into());
Expand Down Expand Up @@ -254,6 +258,7 @@ pub struct SendArgs {
pub fluff: bool,
pub max_outputs: usize,
pub target_slate_version: Option<u16>,
pub payment_proof_address: Option<String>,
}

pub fn send<L, C, K>(
Expand Down Expand Up @@ -289,6 +294,10 @@ where
.collect();
display::estimate(args.amount, strategies, dark_scheme);
} else {
let payment_proof_recipient_address = match args.payment_proof_address {
Some(ref p) => Some(address::ed25519_parse_pubkey(p)?),
None => None,
};
let init_args = InitTxArgs {
src_acct_name: None,
amount: args.amount,
Expand All @@ -298,6 +307,7 @@ where
selection_strategy_is_use_all: args.selection_strategy == "all",
message: args.message.clone(),
target_slate_version: args.target_slate_version,
payment_proof_recipient_address,
send_args: None,
..Default::default()
};
Expand Down Expand Up @@ -722,6 +732,7 @@ where
// should only be one here, but just in case
for tx in txs {
display::tx_messages(&tx, dark_scheme)?;
display::payment_proof(&tx)?;
}
}

Expand Down Expand Up @@ -874,3 +885,41 @@ where
})?;
Ok(())
}

/// Payment Proof Address
pub fn address<L, C, K>(
wallet: Arc<Mutex<Box<dyn WalletInst<'static, L, C, K>>>>,
g_args: &GlobalArgs,
keychain_mask: Option<&SecretKey>,
) -> Result<(), Error>
where
L: WalletLCProvider<'static, C, K> + 'static,
C: NodeClient + 'static,
K: keychain::Keychain + 'static,
{
controller::owner_single_use(wallet.clone(), keychain_mask, |api, m| {
// Just address at derivation index 0 for now
let pub_key = api.get_public_proof_address(m, 0)?;
let result = address::onion_v3_from_pubkey(&pub_key);
match result {
Ok(a) => {
println!();
println!("Public Proof Address for account - {}", g_args.account);
println!("-------------------------------------");
println!("{}", to_hex(pub_key.as_bytes().to_vec()));
println!();
println!("TOR Onion V3 Address for account - {}", g_args.account);
println!("-------------------------------------");
println!("{}", a);
println!();
Ok(())
}
Err(e) => {
error!("Addres retrieval failed: {}", e);
error!("Backtrace: {}", e.backtrace().unwrap());
Err(e)
}
}
})?;
Ok(())
}
15 changes: 12 additions & 3 deletions controller/src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//! Controller for wallet.. instantiates and handles listeners (or single-run
//! invocations) as needed.
use crate::api::{self, ApiServer, BasicAuthMiddleware, ResponseFuture, Router, TLSConfig};
use crate::config::TorConfig;
use crate::keychain::Keychain;
use crate::libwallet::{
address, Error, ErrorKind, NodeClient, NodeVersionInfo, Slate, WalletInst, WalletLCProvider,
Expand Down Expand Up @@ -168,6 +169,7 @@ pub fn owner_listener<L, C, K>(
api_secret: Option<String>,
tls_config: Option<TLSConfig>,
owner_api_include_foreign: Option<bool>,
tor_config: Option<TorConfig>,
) -> Result<(), Error>
where
L: WalletLCProvider<'static, C, K> + 'static,
Expand All @@ -191,8 +193,12 @@ where
}

let api_handler_v2 = OwnerAPIHandlerV2::new(wallet.clone());
let api_handler_v3 =
OwnerAPIHandlerV3::new(wallet.clone(), keychain_mask.clone(), running_foreign);
let api_handler_v3 = OwnerAPIHandlerV3::new(
wallet.clone(),
keychain_mask.clone(),
tor_config,
running_foreign,
);

router
.add_route("/v2/owner", Arc::new(api_handler_v2))
Expand Down Expand Up @@ -593,9 +599,12 @@ where
pub fn new(
wallet: Arc<Mutex<Box<dyn WalletInst<'static, L, C, K> + 'static>>>,
keychain_mask: Arc<Mutex<Option<SecretKey>>>,
tor_config: Option<TorConfig>,
running_foreign: bool,
) -> OwnerAPIHandlerV3<L, C, K> {
let owner_api = Arc::new(Owner::new(wallet.clone()));
let owner_api = Owner::new(wallet.clone());
owner_api.set_tor_config(tor_config);
let owner_api = Arc::new(owner_api);
OwnerAPIHandlerV3 {
wallet,
owner_api,
Expand Down
80 changes: 79 additions & 1 deletion controller/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use crate::core::core::{self, amount_to_hr_string};
use crate::core::global;
use crate::libwallet::{
AcctPathMapping, Error, OutputCommitMapping, OutputStatus, TxLogEntry, WalletInfo,
address, AcctPathMapping, Error, OutputCommitMapping, OutputStatus, TxLogEntry, WalletInfo,
};
use crate::util;
use prettytable;
Expand Down Expand Up @@ -160,6 +160,7 @@ pub fn txs(
bMG->"Amount \nDebited",
bMG->"Fee",
bMG->"Net \nDifference",
bMG->"Payment \nProof",
bMG->"Kernel",
bMG->"Tx \nData",
]);
Expand Down Expand Up @@ -201,6 +202,10 @@ pub fn txs(
Some(e) => util::to_hex(e.0.to_vec()),
None => "None".to_owned(),
};
let payment_proof = match t.payment_proof {
Some(_) => "Yes".to_owned(),
None => "None".to_owned(),
};
if dark_background_color_scheme {
table.add_row(row![
bFC->id,
Expand All @@ -215,6 +220,7 @@ pub fn txs(
bFR->amount_debited_str,
bFR->fee,
bFY->net_diff,
bfG->payment_proof,
bFB->kernel_excess,
bFb->tx_data,
]);
Expand All @@ -233,6 +239,7 @@ pub fn txs(
bFD->amount_debited_str,
bFD->fee,
bFG->net_diff,
bfG->payment_proof,
bFB->kernel_excess,
bFB->tx_data,
]);
Expand All @@ -250,6 +257,7 @@ pub fn txs(
bFD->amount_debited_str,
bFD->fee,
bFG->net_diff,
bfG->payment_proof,
bFB->kernel_excess,
bFB->tx_data,
]);
Expand Down Expand Up @@ -498,3 +506,73 @@ pub fn tx_messages(tx: &TxLogEntry, dark_background_color_scheme: bool) -> Resul

Ok(())
}

/// Display individual Payment Proof
pub fn payment_proof(tx: &TxLogEntry) -> Result<(), Error> {
let title = format!("Payment Proof - Transaction '{}'", tx.id,);
println!();
if term::stdout().is_none() {
println!("Could not open terminal");
return Ok(());
}
let mut t = term::stdout().unwrap();
t.fg(term::color::MAGENTA).unwrap();
writeln!(t, "{}", title).unwrap();
t.reset().unwrap();

let pp = match &tx.payment_proof {
None => {
writeln!(t, "{}", "None").unwrap();
t.reset().unwrap();
return Ok(());
}
Some(p) => p.clone(),
};

t.fg(term::color::WHITE).unwrap();
writeln!(t).unwrap();
let receiver_address = util::to_hex(pp.receiver_address.to_bytes().to_vec());
let receiver_onion_address = address::onion_v3_from_pubkey(&pp.receiver_address)?;
let receiver_signature = match pp.receiver_signature {
Some(s) => util::to_hex(s.to_bytes().to_vec()),
None => "None".to_owned(),
};
let fee = match tx.fee {
Some(f) => f,
None => 0,
};
let amount = if tx.amount_credited >= tx.amount_debited {
core::amount_to_hr_string(tx.amount_credited - tx.amount_debited, true)
} else {
format!(
"{}",
core::amount_to_hr_string(tx.amount_debited - tx.amount_credited - fee, true)
)
};

let sender_address = util::to_hex(pp.sender_address.to_bytes().to_vec());
let sender_onion_address = address::onion_v3_from_pubkey(&pp.sender_address)?;
let sender_signature = match pp.sender_signature {
Some(s) => util::to_hex(s.to_bytes().to_vec()),
None => "None".to_owned(),
};
let kernel_excess = match tx.kernel_excess {
Some(e) => util::to_hex(e.0.to_vec()),
None => "None".to_owned(),
};

writeln!(t, "Receiver Address: {}", receiver_address).unwrap();
writeln!(t, "Receiver Address (Onion V3): {}", receiver_onion_address).unwrap();
writeln!(t, "Receiver Signature: {}", receiver_signature).unwrap();
writeln!(t, "Amount: {}", amount).unwrap();
writeln!(t, "Kernel Excess: {}", kernel_excess).unwrap();
writeln!(t, "Sender Address: {}", sender_address).unwrap();
writeln!(t, "Sender Signature: {}", sender_signature).unwrap();
writeln!(t, "Sender Address (Onion V3): {}", sender_onion_address).unwrap();

t.reset().unwrap();

println!();

Ok(())
}
Loading

0 comments on commit 9fd1d49

Please sign in to comment.