Skip to content

Commit

Permalink
Onion V3 address consistency (mimblewimble#309)
Browse files Browse the repository at this point in the history
* initial addition of ov3

* move libwallet address functions into OnionV3Address type

* incorporate OnionV3 type where possible

* factor out manual dalek pubkey manipulations

* corrections due to test failures

* change json-rpc example to use ov3 for payment proof recipient address
  • Loading branch information
yeastplume authored Jan 24, 2020
1 parent 3091bd9 commit a256017
Show file tree
Hide file tree
Showing 23 changed files with 395 additions and 262 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

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

7 changes: 5 additions & 2 deletions api/src/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ use crate::keychain::{Identifier, Keychain};
use crate::libwallet::api_impl::owner_updater::{start_updater_log_thread, StatusMessage};
use crate::libwallet::api_impl::{owner, owner_updater};
use crate::libwallet::{
address, AcctPathMapping, Error, ErrorKind, InitTxArgs, IssueInvoiceTxArgs, NodeClient,
AcctPathMapping, Error, ErrorKind, InitTxArgs, IssueInvoiceTxArgs, NodeClient,
NodeHeightResult, OutputCommitMapping, PaymentProof, Slate, TxLogEntry, WalletInfo, WalletInst,
WalletLCProvider,
};
use crate::util::logger::LoggingConfig;
use crate::util::secp::key::SecretKey;
use crate::util::{from_hex, static_secp_instance, Mutex, ZeroingString};
use grin_wallet_util::OnionV3Address;
use std::convert::TryFrom;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::{channel, Sender};
use std::sync::Arc;
Expand Down Expand Up @@ -2010,7 +2012,8 @@ where
/// ```

pub fn proof_address_from_onion_v3(&self, address_v3: &str) -> Result<DalekPublicKey, Error> {
address::pubkey_from_onion_v3(address_v3)
let addr = OnionV3Address::try_from(address_v3)?;
Ok(addr.to_ed25519()?)
}

/// Returns a single, exportable [PaymentProof](../grin_wallet_libwallet/api_impl/types/struct.PaymentProof.html)
Expand Down
15 changes: 5 additions & 10 deletions api/src/owner_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ use crate::libwallet::{
OutputCommitMapping, Slate, SlateVersion, TxLogEntry, VersionedSlate, WalletInfo,
WalletLCProvider,
};
use crate::util::{from_hex, Mutex};
use crate::util::Mutex;
use crate::{Owner, OwnerRpcS};
use easy_jsonrpc_mw;
use grin_wallet_util::OnionV3Address;
use std::convert::TryFrom;
use std::sync::Arc;

/// Public definition used to generate Owner jsonrpc api.
Expand Down Expand Up @@ -1380,8 +1382,6 @@ pub fn run_doctest_owner(
use grin_wallet_libwallet::{api_impl, WalletInst};
use grin_wallet_util::grin_keychain::ExtKeychain;

use ed25519_dalek::PublicKey as DalekPublicKey;

use crate::core::global;
use crate::core::global::ChainTypes;
use grin_wallet_util::grin_util as util;
Expand Down Expand Up @@ -1510,13 +1510,8 @@ pub fn run_doctest_owner(
let w = w_lock.lc_provider().unwrap().wallet_inst().unwrap();
let proof_address = match payment_proof {
true => {
let bytes = from_hex(
"783f6528669742a990e0faf0a5fca5d5b3330e37bbb9cd5c628696d03ce4e810".to_string(),
)
.unwrap();
let mut b = [0u8; 32];
b.copy_from_slice(&bytes[0..32]);
Some(DalekPublicKey::from_bytes(&b).unwrap())
let address = "783f6528669742a990e0faf0a5fca5d5b3330e37bbb9cd5c628696d03ce4e810";
Some(OnionV3Address::try_from(address).unwrap())
}
false => None,
};
Expand Down
4 changes: 2 additions & 2 deletions api/src/owner_rpc_s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ pub trait OwnerRpcS {
"selection_strategy_is_use_all": true,
"message": "my message",
"target_slate_version": null,
"payment_proof_recipient_address": "d03c09e9c19bb74aa9ea44e0fe5ae237a9bf40bddf0941064a80913a4459c8bb",
"payment_proof_recipient_address": "pa7wkkdgs5bkteha7lykl7ff2wztgdrxxo442xdcq2lnaphe5aidd4id",
"ttl_blocks": null,
"send_args": null
}
Expand All @@ -399,7 +399,7 @@ pub trait OwnerRpcS {
"ttl_cutoff_height": null,
"num_participants": 2,
"payment_proof": {
"receiver_address": "d03c09e9c19bb74aa9ea44e0fe5ae237a9bf40bddf0941064a80913a4459c8bb",
"receiver_address": "783f6528669742a990e0faf0a5fca5d5b3330e37bbb9cd5c628696d03ce4e810",
"receiver_signature": null,
"sender_address": "32cdd63928854f8b2628b1dce4626ddcdf35d56cb7cfdf7d64cca5822b78d4d3"
},
Expand Down
41 changes: 12 additions & 29 deletions controller/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ use crate::impls::{create_sender, KeybaseAllChannels, SlateGetter as _, SlateRec
use crate::impls::{PathToSlate, SlatePutter};
use crate::keychain;
use crate::libwallet::{
self, address, InitTxArgs, IssueInvoiceTxArgs, NodeClient, PaymentProof, WalletInst,
WalletLCProvider,
self, InitTxArgs, IssueInvoiceTxArgs, NodeClient, PaymentProof, WalletInst, WalletLCProvider,
};
use crate::util::secp::key::SecretKey;
use crate::util::{to_hex, Mutex, ZeroingString};
use crate::util::{Mutex, ZeroingString};
use crate::{controller, display};
use grin_wallet_util::OnionV3Address;
use serde_json as json;
use std::fs::File;
use std::io::{Read, Write};
Expand Down Expand Up @@ -253,7 +253,7 @@ pub struct SendArgs {
pub fluff: bool,
pub max_outputs: usize,
pub target_slate_version: Option<u16>,
pub payment_proof_address: Option<String>,
pub payment_proof_address: Option<OnionV3Address>,
pub ttl_blocks: Option<u64>,
}

Expand Down Expand Up @@ -290,10 +290,6 @@ 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 @@ -303,7 +299,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,
payment_proof_recipient_address: args.payment_proof_address.clone(),
ttl_blocks: args.ttl_blocks,
send_args: None,
..Default::default()
Expand Down Expand Up @@ -897,26 +893,13 @@ where
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!("Address retrieval failed: {}", e);
error!("Backtrace: {}", e.backtrace().unwrap());
Err(e)
}
}
let addr = OnionV3Address::from_bytes(pub_key.to_bytes());
println!();
println!("Address for account - {}", g_args.account);
println!("-------------------------------------");
println!("{}", addr);
println!();
Ok(())
})?;
Ok(())
}
Expand Down
3 changes: 2 additions & 1 deletion controller/src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::util::{from_hex, static_secp_instance, to_base64, Mutex};
use failure::ResultExt;
use futures::future::{err, ok};
use futures::{Future, Stream};
use grin_wallet_util::OnionV3Address;
use hyper::header::HeaderValue;
use hyper::{Body, Request, Response, StatusCode};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -98,7 +99,7 @@ where
let tor_dir = format!("{}/tor/listener", lc.get_top_level_directory()?);
let sec_key = address::address_from_derivation_path(&k, &parent_key_id, 0)
.map_err(|e| ErrorKind::TorConfig(format!("{:?}", e).into()))?;
let onion_address = tor_config::onion_address_from_seckey(&sec_key)
let onion_address = OnionV3Address::from_private(&sec_key.0)
.map_err(|e| ErrorKind::TorConfig(format!("{:?}", e).into()))?;
warn!(
"Starting TOR Hidden Service for API listener at address {}, binding to {}",
Expand Down
23 changes: 14 additions & 9 deletions controller/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
use crate::core::core::{self, amount_to_hr_string};
use crate::core::global;
use crate::libwallet::{
address, AcctPathMapping, Error, OutputCommitMapping, OutputStatus, TxLogEntry, WalletInfo,
AcctPathMapping, Error, OutputCommitMapping, OutputStatus, TxLogEntry, WalletInfo,
};
use crate::util;
use grin_wallet_util::OnionV3Address;
use prettytable;
use std::io::prelude::Write;
use term;
Expand Down Expand Up @@ -537,8 +538,6 @@ pub fn payment_proof(tx: &TxLogEntry) -> Result<(), Error> {

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(),
Expand All @@ -556,8 +555,6 @@ pub fn payment_proof(tx: &TxLogEntry) -> Result<(), Error> {
)
};

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(),
Expand All @@ -567,14 +564,22 @@ pub fn payment_proof(tx: &TxLogEntry) -> Result<(), Error> {
None => "None".to_owned(),
};

writeln!(t, "Receiver Address: {}", receiver_address).unwrap();
writeln!(t, "Receiver Address (Onion V3): {}", receiver_onion_address).unwrap();
writeln!(
t,
"Receiver Address: {}",
OnionV3Address::from_bytes(pp.receiver_address.to_bytes())
)
.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 Address: {}",
OnionV3Address::from_bytes(pp.sender_address.to_bytes())
)
.unwrap();
writeln!(t, "Sender Signature: {}", sender_signature).unwrap();
writeln!(t, "Sender Address (Onion V3): {}", sender_onion_address).unwrap();

t.reset().unwrap();

Expand Down
8 changes: 5 additions & 3 deletions controller/tests/payment_proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ use libwallet::{InitTxArgs, Slate};
use std::thread;
use std::time::Duration;

use grin_wallet_util::OnionV3Address;

#[macro_use]
mod common;
use common::{clean_output_dir, create_wallet_proxy, setup};
Expand Down Expand Up @@ -80,7 +82,7 @@ fn payment_proofs_test_impl(test_dir: &'static str) -> Result<(), libwallet::Err
Ok(())
})?;

let address = address.unwrap();
let address = OnionV3Address::from_bytes(address.as_ref().unwrap().to_bytes());
println!("Public address is: {:?}", address);
let amount = 60_000_000_000;
let mut slate = Slate::blank(1);
Expand All @@ -93,14 +95,14 @@ fn payment_proofs_test_impl(test_dir: &'static str) -> Result<(), libwallet::Err
max_outputs: 500,
num_change_outputs: 1,
selection_strategy_is_use_all: true,
payment_proof_recipient_address: Some(address),
payment_proof_recipient_address: Some(address.clone()),
..Default::default()
};
let slate_i = sender_api.init_send_tx(m, args)?;

assert_eq!(
slate_i.payment_proof.as_ref().unwrap().receiver_address,
address
address.to_ed25519()?,
);
println!(
"Sender addr: {:?}",
Expand Down
11 changes: 11 additions & 0 deletions impls/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::keychain;
use crate::libwallet;
use crate::util::secp;
use failure::{Backtrace, Context, Fail};
use grin_wallet_util::OnionV3AddressError;
use std::env;
use std::fmt::{self, Display};

Expand All @@ -42,6 +43,10 @@ pub enum ErrorKind {
#[fail(display = "Keychain error")]
Keychain(keychain::Error),

/// Onion V3 Address Error
#[fail(display = "Onion V3 Address Error")]
OnionV3Address(OnionV3AddressError),

/// Error when formatting json
#[fail(display = "IO error")]
IO,
Expand Down Expand Up @@ -187,3 +192,9 @@ impl From<libtx::Error> for Error {
}
}
}

impl From<OnionV3AddressError> for Error {
fn from(error: OnionV3AddressError) -> Error {
Error::from(ErrorKind::OnionV3Address(error))
}
}
Loading

0 comments on commit a256017

Please sign in to comment.