Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@ readme = "README.md"

[dependencies]
aes = "0.7.4"
bs58 = "0.4.0"
rand = "0.8.4"
ripemd160 = "0.9.1"
bs58 = { version = "0.4.0", default-features = false, features = ["alloc"] }
rand = { version = "0.8.4", default-features = false, features = ["alloc"] }
ripemd160 = { version = "0.9.0", default-features = false }
scrypt = { version = "0.7.0", default-features = false }
secp256k1 = "0.20.3"
sha2 = "0.9.5"
unicode-normalization = "0.1.19"
secp256k1 = { version = "0.20.0", default-features = false, features = ["alloc"] }
digest = { version = "0.10.7", default-features = false }
sha2 = { version = "0.10.8", default-features = false }
unicode-normalization = { version = "0.1.19", default-features = false }

[features]
default = ["std"]
std = ["rand/std", "rand/std_rng", "sha2/std", "unicode-normalization/std", "digest/std"]
53 changes: 43 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,40 @@
//! });
//! ```

#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(not(feature = "std"))]
extern crate alloc;

#[cfg(not(feature = "std"))]
use alloc::{
string::String,
vec::Vec,
vec
};

#[cfg(feature = "std")]
extern crate std;

#[cfg(feature = "std")]
use std::{
string::String,
vec,
vec::Vec,
};

use aes::Aes256;
use aes::cipher::{
BlockDecrypt,
BlockEncrypt,
generic_array::GenericArray,
NewBlockCipher
};
use rand::RngCore;
use digest::Digest;
use rand::{RngCore};
use ripemd160::Ripemd160;
use scrypt::Params;
use secp256k1::{Secp256k1, SecretKey, PublicKey};
use sha2::Digest;
use unicode_normalization::UnicodeNormalization;

/// Number of base58 characters on every encrypted private key.
Expand Down Expand Up @@ -255,6 +277,9 @@ pub enum Error {
WifKey,
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}

/// Internal Functions to manipulate an arbitrary number of bytes [u8].
trait BytesManipulation {
/// Encode informed data in base 58 check.
Expand Down Expand Up @@ -627,7 +652,10 @@ pub trait Generate {
/// .decrypt("\u{03d2}\u{0301}\u{0000}\u{010400}\u{01f4a9}").is_ok()
/// );
/// ```
fn generate(&self, compress: bool) -> Result<String, Error>;
#[cfg(feature = "std")]
fn generate<R: RngCore>(&self, compress: bool) -> Result<String, Error>;

fn generate_rng<R: RngCore>(&self, compress: bool, rng: &mut R) -> Result<String, Error>;
}

/// Internal trait to manipulate private keys (32 bytes).
Expand Down Expand Up @@ -683,14 +711,15 @@ impl BytesManipulation for [u8] {
#[inline]
fn hash160(&self) -> [u8; 20] {
let mut result = [0x00; 20];
use ripemd160::Digest;
result[..].copy_from_slice(&Ripemd160::digest(&sha2::Sha256::digest(self)));
result
}

#[inline]
fn hash256(&self) -> [u8; 32] {
let mut result = [0x00; 32];
result[..].copy_from_slice(&sha2::Sha256::digest(&sha2::Sha256::digest(self)));
result[..].copy_from_slice(&sha2::Sha256::digest(sha2::Sha256::digest(self)));
result
}

Expand Down Expand Up @@ -777,13 +806,17 @@ impl Encrypt for [u8; 32] {
}

impl Generate for str {
#[inline]
fn generate(&self, compress: bool) -> Result<String, Error> {
#[cfg(feature = "std")]
fn generate<R: RngCore>(&self, compress: bool) -> Result<String, Error> {
self.generate_rng(compress, &mut rand::thread_rng())
}

fn generate_rng<R: RngCore>(&self, compress: bool, rng: &mut R) -> Result<String, Error> {
let mut owner_salt = [0x00; 8];
let mut pass_factor = [0x00; 32];
let mut seed_b = [0x00; 24];

rand::thread_rng().fill_bytes(&mut owner_salt);
rng.fill_bytes(&mut owner_salt);

scrypt::scrypt(
self.nfc().collect::<String>().as_bytes(),
Expand All @@ -796,7 +829,7 @@ impl Generate for str {

let mut pass_point_mul = PublicKey::from_slice(&pass_point).map_err(|_| Error::PubKey)?;

rand::thread_rng().fill_bytes(&mut seed_b);
rng.fill_bytes(&mut seed_b);

let factor_b = seed_b.hash256();

Expand Down Expand Up @@ -1157,7 +1190,7 @@ mod tests {
Err(Error::WifKey)
);
assert_eq!(
"KzkcmnPaJd7mqT47Rnk9XMGRfW2wfo7ar2M2o6Yoe6Rdgbg2bHM9".replace("d", "b").decode_wif(),
"KzkcmnPaJd7mqT47Rnk9XMGRfW2wfo7ar2M2o6Yoe6Rdgbg2bHM9".replace('d', "b").decode_wif(),
Err(Error::Checksum)
);
assert_eq!(["a"; 51].concat().decode_wif(), Err(Error::WifKey));
Expand All @@ -1174,7 +1207,7 @@ mod tests {
}
assert!(TV_ENCRYPTED[1].decrypt("Satoshi").is_ok());
assert_eq!(TV_ENCRYPTED[1].decrypt("wrong"), Err(Error::Pass));
assert_eq!(TV_ENCRYPTED[1].replace("X", "x").decrypt("Satoshi"), Err(Error::Checksum));
assert_eq!(TV_ENCRYPTED[1].replace('X', "x").decrypt("Satoshi"), Err(Error::Checksum));
assert_eq!(TV_ENCRYPTED[1][1..].decrypt("Satoshi"), Err(Error::EncKey));
}

Expand Down