Skip to content

Commit

Permalink
Updates to support XORed wallet keys/Tokens (#2982)
Browse files Browse the repository at this point in the history
* updates to support xored tokens in wallet

* rustfmt
  • Loading branch information
yeastplume authored Aug 2, 2019
1 parent f79d05b commit 705fcbb
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
45 changes: 45 additions & 0 deletions core/src/libtx/secp_ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,48 @@ pub mod option_sig_serde {

}

/// Serializes an Option<secp::SecretKey> to and from hex
pub mod option_seckey_serde {
use crate::serde::{Deserialize, Deserializer, Serializer};
use crate::util::{from_hex, secp, static_secp_instance, to_hex};
use serde::de::Error;

///
pub fn serialize<S>(
key: &Option<secp::key::SecretKey>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match key {
Some(key) => serializer.serialize_str(&to_hex(key.0.to_vec())),
None => serializer.serialize_none(),
}
}

///
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<secp::key::SecretKey>, D::Error>
where
D: Deserializer<'de>,
{
let static_secp = static_secp_instance();
let static_secp = static_secp.lock();
Option::<String>::deserialize(deserializer).and_then(|res| match res {
Some(string) => from_hex(string.to_string())
.map_err(|err| Error::custom(err.to_string()))
.and_then(|bytes: Vec<u8>| {
let mut b = [0u8; 32];
b.copy_from_slice(&bytes[0..32]);
secp::key::SecretKey::from_slice(&static_secp, &b)
.map(|val| Some(val))
.map_err(|err| Error::custom(err.to_string()))
}),
None => Ok(None),
})
}
}

/// Serializes a secp::Signature to and from hex
pub mod sig_serde {
use crate::serde::{Deserialize, Deserializer, Serializer};
Expand Down Expand Up @@ -286,6 +328,8 @@ mod test {

#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
struct SerTest {
#[serde(with = "option_seckey_serde")]
pub opt_skey: Option<SecretKey>,
#[serde(with = "pubkey_serde")]
pub pub_key: PublicKey,
#[serde(with = "option_sig_serde")]
Expand All @@ -308,6 +352,7 @@ mod test {
let msg = Message::from_slice(&msg).unwrap();
let sig = aggsig::sign_single(&secp, &msg, &sk, None, None).unwrap();
SerTest {
opt_skey: Some(sk.clone()),
pub_key: PublicKey::from_secret_key(&secp, &sk).unwrap(),
opt_sig: Some(sig.clone()),
sig: sig.clone(),
Expand Down
7 changes: 7 additions & 0 deletions keychain/src/keychain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ impl Keychain for ExtKeychain {
Ok(keychain)
}

fn mask_master_key(&mut self, mask: &SecretKey) -> Result<(), Error> {
for i in 0..secp::constants::SECRET_KEY_SIZE {
self.master.secret_key.0[i] ^= mask.0[i];
}
Ok(())
}

/// For testing - probably not a good idea to use outside of tests.
fn from_random_seed(is_floo: bool) -> Result<ExtKeychain, Error> {
let seed: String = thread_rng().sample_iter(&Alphanumeric).take(16).collect();
Expand Down
3 changes: 3 additions & 0 deletions keychain/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,9 @@ pub trait Keychain: Sync + Send + Clone {
/// Generates a keychain from a randomly generated seed. Mostly used for tests.
fn from_random_seed(is_floo: bool) -> Result<Self, Error>;

/// XOR masks the keychain's master key against another key
fn mask_master_key(&mut self, mask: &SecretKey) -> Result<(), Error>;

/// Root identifier for that keychain
fn root_key_id() -> Identifier;

Expand Down

0 comments on commit 705fcbb

Please sign in to comment.