Skip to content

Commit

Permalink
key system upgraded to bip0039
Browse files Browse the repository at this point in the history
Signed-off-by: Francisco Hernandez Iglesias <f.hernandeziglesias@outlook.com>
  • Loading branch information
SupremoUGH committed Nov 16, 2022
1 parent a9a1ca2 commit f6a3177
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 50 deletions.
2 changes: 1 addition & 1 deletion manta-accounting/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ where
.map_err(Error::LedgerConnectionError)
}

/// Returns public receiving keys according to the `request`.
/// Returns public receiving keys.
#[inline]
pub async fn receiving_keys(&mut self) -> Result<PublicKey<C>, S::Error> {
self.signer.receiving_keys().await
Expand Down
4 changes: 2 additions & 2 deletions manta-accounting/src/wallet/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ where
request: SignRequest<C>,
) -> LocalBoxFutureResult<Result<SignResponse<C>, SignError<C>>, Self::Error>;

/// Returns public receiving keys according to the `request`.
/// Returns public receiving keys.
fn receiving_keys(&mut self) -> LocalBoxFutureResult<PublicKey<C>, Self::Error>;
}

Expand Down Expand Up @@ -1160,7 +1160,7 @@ where
result
}

/// Returns public receiving keys according to the `request`.
/// Returns public receiving keys.
#[inline]
pub fn receiving_keys(&mut self) -> PublicKey<C> {
self.state
Expand Down
5 changes: 4 additions & 1 deletion manta-pay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ arkworks = [
# Enable Download Parameters
download = ["manta-parameters/download", "std"]

# Key Features
key = ["bip32", "bip0039"]

# Enable Groth16 ZKP System
groth16 = ["ark-groth16", "ark-snark", "arkworks"]

Expand Down Expand Up @@ -82,7 +85,7 @@ std = ["manta-accounting/std", "manta-util/std"]
test = ["manta-accounting/test", "manta-crypto/test", "manta-parameters", "tempfile"]

# Wallet
wallet = ["bip32", "bip0039", "manta-crypto/getrandom", "std"]
wallet = ["key", "manta-crypto/getrandom", "std"]

# Enable WebSocket Signer Client
websocket = [
Expand Down
85 changes: 43 additions & 42 deletions manta-pay/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@
use alloc::{format, string::String};
use core::marker::PhantomData;
use manta_accounting::key::{self, AccountIndex};
use manta_crypto::rand::{CryptoRng, RngCore, Sample};
use manta_crypto::rand::{CryptoRng, RngCore};
use manta_util::{create_seal, seal, Array};

#[cfg(feature = "serde")]
use manta_util::serde::{Deserialize, Serialize, Serializer};

pub use bip32::{Error, Seed, XPrv as SecretKey};
pub use bip0039::{self, Error};
pub use bip32::{self, XPrv as SecretKey};

create_seal! {}

Expand Down Expand Up @@ -123,23 +124,26 @@ impl_coin_type!(
);

/// Seed Byte Array Type
type SeedBytes = Array<u8, { Seed::SIZE }>;
type SeedBytes = Array<u8, { bip32::Seed::SIZE }>;

/// Key Secret
#[cfg_attr(
feature = "serde",
derive(Deserialize, Serialize),
serde(crate = "manta_util::serde", deny_unknown_fields, transparent)
serde(crate = "manta_util::serde", deny_unknown_fields)
)]
#[derive(derivative::Derivative)]
#[derivative(Clone(bound = ""), Default(bound = ""))]
#[derivative(Clone(bound = ""))]
pub struct KeySecret<C>
where
C: CoinType,
{
/// Key Seed
seed: SeedBytes,

/// Mnemonic
mnemonic: Mnemonic,

/// Type Parameter Marker
__: PhantomData<C>,
}
Expand All @@ -148,26 +152,36 @@ impl<C> KeySecret<C>
where
C: CoinType,
{
/// Builds a [`KeySecret`] from raw bytes.
/// Builds a [`KeySecret`] from `seed` and `mnemonic`.
#[inline]
fn build(seed: [u8; Seed::SIZE]) -> Self {
fn new_unchecked(seed: [u8; bip32::Seed::SIZE], mnemonic: Mnemonic) -> Self {
Self {
seed: seed.into(),
mnemonic,
__: PhantomData,
}
}

/// Builds a [`KeySecret`] from a `seed`.
#[inline]
fn from_seed(seed: Seed) -> Self {
Self::build(*seed.as_bytes())
}

/// Converts a `mnemonic` phrase into a [`KeySecret`], locking it with `password`.
#[inline]
#[must_use]
pub fn new(mnemonic: Mnemonic, password: &str) -> Self {
Self::from_seed(mnemonic.to_seed(password))
Self::new_unchecked(mnemonic.to_seed(password), mnemonic)
}

/// Exposes a shared reference to the [`Mnemonic`] for `self`.
#[inline]
pub fn expose_mnemonic(&self) -> &Mnemonic {
&self.mnemonic
}

/// Samples a random [`KeySecret`] from `rng` with no password.
#[inline]
pub fn sample<R>(rng: &mut R) -> Self
where
R: CryptoRng + RngCore + ?Sized,
{
Self::new(Mnemonic::sample(rng), "")
}

/// Returns the [`SecretKey`].
Expand All @@ -190,21 +204,6 @@ pub type Account<C = Manta> = key::Account<KeySecret<C>>;
/// Vec Account type
pub type VecAccountMap<C> = Vec<Account<C>>;

impl<C> Sample for KeySecret<C>
where
C: CoinType,
{
#[inline]
fn sample<R>(_: (), rng: &mut R) -> Self
where
R: RngCore + ?Sized,
{
let mut seed = [0; Seed::SIZE];
rng.fill_bytes(&mut seed);
Self::build(seed)
}
}

/// Computes the [`BIP-0044`] path string for the given coin settings.
///
/// [`BIP-0044`]: https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
Expand Down Expand Up @@ -233,38 +232,40 @@ where
pub struct Mnemonic(
/// Underlying BIP39 Mnemonic
#[cfg_attr(feature = "serde", serde(serialize_with = "Mnemonic::serialize"))]
bip32::Mnemonic,
bip0039::Mnemonic,
);

impl Mnemonic {
/// Create a new BIP39 mnemonic phrase from the given string.
/// Create a new BIP0039 mnemonic phrase from the given string.
#[inline]
pub fn new<S>(phrase: S) -> Result<Self, Error>
where
S: AsRef<str>,
{
bip32::Mnemonic::new(phrase, Default::default()).map(Self)
pub fn new(phrase: &str) -> Result<Self, Error> {
bip0039::Mnemonic::from_phrase(phrase).map(Self)
}

/// Samples a random [`Mnemonic`] using the entropy returned from `rng`.
/// Samples a random 12 word [`Mnemonic`] using the entropy returned from `rng`.
#[inline]
pub fn sample<R>(rng: &mut R) -> Self
where
R: CryptoRng + RngCore + ?Sized,
{
Self(bip32::Mnemonic::random(rng, Default::default()))
let mut entropy: [u8; 16] = [0; 16];
rng.fill_bytes(&mut entropy);
Self(
bip0039::Mnemonic::from_entropy(entropy.to_vec())
.expect("Creating a Mnemonic from 16 bytes of entropy is not allowed to fail."),
)
}

/// Convert this mnemonic phrase into the BIP39 seed value.
/// Convert this mnemonic phrase into the BIP32 seed value.
#[inline]
pub fn to_seed(&self, password: &str) -> Seed {
pub fn to_seed(&self, password: &str) -> [u8; bip32::Seed::SIZE] {
self.0.to_seed(password)
}

/// Serializes the underlying `mnemonic` phrase.
#[cfg(feature = "serde")]
#[inline]
fn serialize<S>(mnemonic: &bip32::Mnemonic, serializer: S) -> Result<S::Ok, S::Error>
fn serialize<S>(mnemonic: &bip0039::Mnemonic, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
Expand Down Expand Up @@ -293,6 +294,6 @@ impl TryFrom<String> for Mnemonic {

#[inline]
fn try_from(string: String) -> Result<Self, Self::Error> {
Self::new(string)
Self::new(string.as_str())
}
}
4 changes: 2 additions & 2 deletions manta-pay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ pub mod util;
#[cfg_attr(doc_cfg, doc(cfg(feature = "groth16")))]
pub mod config;

#[cfg(feature = "bip32")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "bip32")))]
#[cfg(feature = "key")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "key")))]
pub mod key;

#[cfg(all(feature = "groth16", feature = "test"))]
Expand Down
4 changes: 2 additions & 2 deletions manta-pay/src/signer/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use manta_crypto::{
},
key::agreement::Derive,
merkle_tree::{self, forest::Configuration},
rand::{ChaCha20Rng, CryptoRng, Rand, RngCore},
rand::{ChaCha20Rng, CryptoRng, RngCore},
};

impl<C> DeriveAddresses for KeySecret<C>
Expand Down Expand Up @@ -78,7 +78,7 @@ where
C: CoinType,
R: CryptoRng + RngCore + ?Sized,
{
rng.gen()
KeySecret::sample(rng)
}

/// Signer UTXO Accumulator
Expand Down

0 comments on commit f6a3177

Please sign in to comment.