mirror of
https://github.com/mimblewimble/grin.git
synced 2025-02-01 17:01:09 +03:00
Updates to support XORed wallet keys/Tokens (#2982)
* updates to support xored tokens in wallet * rustfmt
This commit is contained in:
parent
f79d05ba53
commit
705fcbb1a6
3 changed files with 55 additions and 0 deletions
|
@ -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
|
/// Serializes a secp::Signature to and from hex
|
||||||
pub mod sig_serde {
|
pub mod sig_serde {
|
||||||
use crate::serde::{Deserialize, Deserializer, Serializer};
|
use crate::serde::{Deserialize, Deserializer, Serializer};
|
||||||
|
@ -286,6 +328,8 @@ mod test {
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
|
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
|
||||||
struct SerTest {
|
struct SerTest {
|
||||||
|
#[serde(with = "option_seckey_serde")]
|
||||||
|
pub opt_skey: Option<SecretKey>,
|
||||||
#[serde(with = "pubkey_serde")]
|
#[serde(with = "pubkey_serde")]
|
||||||
pub pub_key: PublicKey,
|
pub pub_key: PublicKey,
|
||||||
#[serde(with = "option_sig_serde")]
|
#[serde(with = "option_sig_serde")]
|
||||||
|
@ -308,6 +352,7 @@ mod test {
|
||||||
let msg = Message::from_slice(&msg).unwrap();
|
let msg = Message::from_slice(&msg).unwrap();
|
||||||
let sig = aggsig::sign_single(&secp, &msg, &sk, None, None).unwrap();
|
let sig = aggsig::sign_single(&secp, &msg, &sk, None, None).unwrap();
|
||||||
SerTest {
|
SerTest {
|
||||||
|
opt_skey: Some(sk.clone()),
|
||||||
pub_key: PublicKey::from_secret_key(&secp, &sk).unwrap(),
|
pub_key: PublicKey::from_secret_key(&secp, &sk).unwrap(),
|
||||||
opt_sig: Some(sig.clone()),
|
opt_sig: Some(sig.clone()),
|
||||||
sig: sig.clone(),
|
sig: sig.clone(),
|
||||||
|
|
|
@ -69,6 +69,13 @@ impl Keychain for ExtKeychain {
|
||||||
Ok(keychain)
|
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.
|
/// For testing - probably not a good idea to use outside of tests.
|
||||||
fn from_random_seed(is_floo: bool) -> Result<ExtKeychain, Error> {
|
fn from_random_seed(is_floo: bool) -> Result<ExtKeychain, Error> {
|
||||||
let seed: String = thread_rng().sample_iter(&Alphanumeric).take(16).collect();
|
let seed: String = thread_rng().sample_iter(&Alphanumeric).take(16).collect();
|
||||||
|
|
|
@ -467,6 +467,9 @@ pub trait Keychain: Sync + Send + Clone {
|
||||||
/// Generates a keychain from a randomly generated seed. Mostly used for tests.
|
/// Generates a keychain from a randomly generated seed. Mostly used for tests.
|
||||||
fn from_random_seed(is_floo: bool) -> Result<Self, Error>;
|
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
|
/// Root identifier for that keychain
|
||||||
fn root_key_id() -> Identifier;
|
fn root_key_id() -> Identifier;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue