Skip to content

Commit

Permalink
Slatepack Pt. 3 - The Packening (SlatepackAddress implementation) (#413)
Browse files Browse the repository at this point in the history
* addition of SlatepackAddress type

* complete conversion of ed25519 keys to x25519 and isolate conversion within libwallet

* refactor packing/unpacking of slates logic into libwallet and out of slate adapters

* println
  • Loading branch information
yeastplume authored May 26, 2020
1 parent 038dafc commit 03cb109
Show file tree
Hide file tree
Showing 14 changed files with 519 additions and 248 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion controller/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ grin_wallet_libwallet = { path = "../libwallet", version = "4.0.0-alpha.1" }
grin_wallet_config = { path = "../config", version = "4.0.0-alpha.1" }

[dev-dependencies]
x25519-dalek = "0.6"
ed25519-dalek = "1.0.0-pre.1"
118 changes: 53 additions & 65 deletions controller/tests/slatepack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@ use grin_wallet_util::grin_core as core;
use grin_wallet_util::OnionV3Address;

use impls::test_framework::{self, LocalWalletClient};
use impls::{
PathToSlatepack, PathToSlatepackArmored, SlateGetter as _, SlatePutter as _, SlatepackArgs,
};
use impls::{PathToSlatepack, SlatePutter as _};
use std::sync::atomic::Ordering;
use std::thread;
use std::time::Duration;

use grin_wallet_libwallet::{InitTxArgs, IssueInvoiceTxArgs, Slate, Slatepack};
use grin_wallet_libwallet::{
InitTxArgs, IssueInvoiceTxArgs, Slate, Slatepack, SlatepackAddress, Slatepacker,
SlatepackerArgs,
};

use x25519_dalek::PublicKey as xDalekPublicKey;
use x25519_dalek::StaticSecret;
use ed25519_dalek::PublicKey as edDalekPublicKey;
use ed25519_dalek::SecretKey as edDalekSecretKey;

#[macro_use]
mod common;
Expand All @@ -43,54 +44,37 @@ fn output_slatepack(
file: &str,
armored: bool,
use_bin: bool,
sender: Option<xDalekPublicKey>,
recipients: Vec<xDalekPublicKey>,
sender: Option<SlatepackAddress>,
recipients: Vec<SlatepackAddress>,
) -> Result<(), libwallet::Error> {
let packer = Slatepacker::new(SlatepackerArgs {
sender,
recipients,
dec_key: None,
});
let mut file = file.into();
if armored {
let file = format!("{}.armored", file);
let args = SlatepackArgs {
pathbuf: file.into(),
sender,
recipients,
dec_key: None,
};
PathToSlatepackArmored::new(args).put_tx(&slate, use_bin)
} else {
let args = SlatepackArgs {
pathbuf: file.into(),
sender,
recipients,
dec_key: None,
};
PathToSlatepack::new(args).put_tx(&slate, use_bin)
file = format!("{}.armored", file);
}
PathToSlatepack::new(file.into(), &packer, armored).put_tx(&slate, use_bin)
}

fn slate_from_packed(
file: &str,
armored: bool,
dec_key: Option<&StaticSecret>,
dec_key: Option<&edDalekSecretKey>,
) -> Result<(Slatepack, Slate), libwallet::Error> {
let packer = Slatepacker::new(SlatepackerArgs {
sender: None,
recipients: vec![],
dec_key,
});
let mut file = file.into();
if armored {
let file = format!("{}.armored", file);
let args = SlatepackArgs {
pathbuf: file.into(),
sender: None,
recipients: vec![],
dec_key,
};
let pts = PathToSlatepackArmored::new(args);
Ok((pts.get_slatepack()?, pts.get_tx()?.0))
} else {
let args = SlatepackArgs {
pathbuf: file.into(),
sender: None,
recipients: vec![],
dec_key,
};
let pts = PathToSlatepack::new(args);
Ok((pts.get_slatepack()?, pts.get_tx()?.0))
file = format!("{}.armored", file);
}
let slatepack = PathToSlatepack::new(file.into(), &packer, armored).get_slatepack()?;
Ok((slatepack.clone(), packer.get_slate(&slatepack)?))
}

/// self send impl
Expand Down Expand Up @@ -165,34 +149,38 @@ fn slatepack_exchange_test_impl(

let (recipients_1, dec_key_1, sender_1) = match use_encryption {
true => {
let mut rec_address = xDalekPublicKey::from([0u8; 32]);
let mut sec_key = StaticSecret::from([0u8; 32]);
let mut rec_address = SlatepackAddress::random();
let mut sec_key = edDalekSecretKey::from_bytes(&[0u8; 32]).unwrap();
wallet::controller::owner_single_use(Some(wallet1.clone()), mask1, None, |api, m| {
let ed25519_sec_key = api.get_secret_key(m, 0)?;
let mut b = [0u8; 32];
b.copy_from_slice(&ed25519_sec_key.as_ref()[0..32]);
sec_key = StaticSecret::from(b);
rec_address = xDalekPublicKey::from(&sec_key);
sec_key = api.get_secret_key(m, 0)?;
let pub_key = edDalekPublicKey::from(&sec_key);
rec_address = SlatepackAddress::new(&pub_key);
Ok(())
})?;
(vec![rec_address], Some(sec_key), Some(rec_address.clone()))
(
vec![rec_address.clone()],
Some(sec_key),
Some(rec_address.clone()),
)
}
false => (vec![], None, None),
};

let (recipients_2, dec_key_2, sender_2) = match use_encryption {
true => {
let mut rec_address = xDalekPublicKey::from([0u8; 32]);
let mut sec_key = StaticSecret::from([0u8; 32]);
let mut rec_address = SlatepackAddress::random();
let mut sec_key = edDalekSecretKey::from_bytes(&[0u8; 32]).unwrap();
wallet::controller::owner_single_use(Some(wallet2.clone()), mask2, None, |api, m| {
let ed25519_sec_key = api.get_secret_key(m, 0)?;
let mut b = [0u8; 32];
b.copy_from_slice(&ed25519_sec_key.as_ref()[0..32]);
sec_key = StaticSecret::from(b);
rec_address = xDalekPublicKey::from(&sec_key);
sec_key = api.get_secret_key(m, 0)?;
let pub_key = edDalekPublicKey::from(&sec_key);
rec_address = SlatepackAddress::new(&pub_key);
Ok(())
})?;
(vec![rec_address], Some(sec_key), Some(rec_address.clone()))
(
vec![rec_address.clone()],
Some(sec_key),
Some(rec_address.clone()),
)
}
false => (vec![], None, None),
};
Expand Down Expand Up @@ -232,7 +220,7 @@ fn slatepack_exchange_test_impl(
&send_file,
use_armored,
use_bin,
sender_1,
sender_1.clone(),
recipients_2.clone(),
)?;
api.tx_lock_outputs(m, &slate)?;
Expand All @@ -257,8 +245,8 @@ fn slatepack_exchange_test_impl(
use_armored,
use_bin,
// re-encrypt for sender!
sender_2,
match slatepack.sender {
sender_2.clone(),
match slatepack.sender.clone() {
Some(s) => vec![s.clone()],
None => vec![],
},
Expand Down Expand Up @@ -326,7 +314,7 @@ fn slatepack_exchange_test_impl(
&send_file,
use_armored,
use_bin,
sender_2,
sender_2.clone(),
recipients_1.clone(),
)?;
Ok(())
Expand All @@ -352,8 +340,8 @@ fn slatepack_exchange_test_impl(
&receive_file,
use_armored,
use_bin,
sender_1,
match slatepack.sender {
sender_1.clone(),
match slatepack.sender.clone() {
Some(s) => vec![s.clone()],
None => vec![],
},
Expand Down
2 changes: 1 addition & 1 deletion impls/src/adapters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod slatepack;
pub use self::file::PathToSlate;
pub use self::http::{HttpSlateSender, SchemeNotHttp};
pub use self::keybase::{KeybaseAllChannels, KeybaseChannel};
pub use self::slatepack::{PathToSlatepack, PathToSlatepackArmored, SlatepackArgs};
pub use self::slatepack::PathToSlatepack;

use crate::config::{TorConfig, WalletConfig};
use crate::libwallet::{Error, ErrorKind, Slate};
Expand Down
Loading

0 comments on commit 03cb109

Please sign in to comment.