diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/account.rs | 6 | ||||
| -rw-r--r-- | src/hdk.rs | 11 | ||||
| -rw-r--r-- | src/mnemonic.rs | 3 | ||||
| -rw-r--r-- | src/mnemonic/wordlist.rs | 16 |
4 files changed, 16 insertions, 20 deletions
diff --git a/src/account.rs b/src/account.rs index e1da1ae..f862853 100644 --- a/src/account.rs +++ b/src/account.rs @@ -20,7 +20,7 @@ pub struct PrivateKey(SecretKey); impl PrivateKey { /// Creates a private key from a secret. pub fn new(secret: impl AsRef<[u8]>) -> Result<Self> { - let key = SecretKey::from_be_bytes(secret.as_ref())?; + let key = SecretKey::from_slice(secret.as_ref())?; Ok(PrivateKey(key)) } @@ -47,7 +47,7 @@ impl PrivateKey { /// Returns the private key's 32 byte secret. pub fn secret(&self) -> [u8; 32] { - self.0.to_be_bytes().into() + self.0.to_bytes().into() } /// Generate a signature for the specified message. @@ -59,7 +59,7 @@ impl PrivateKey { pub fn try_sign(&self, message: [u8; 32]) -> Result<Signature> { let (signature, recovery_id) = SigningKey::from(&self.0) .as_nonzero_scalar() - .try_sign_prehashed_rfc6979::<Sha256>(message.into(), b"")?; + .try_sign_prehashed_rfc6979::<Sha256>(&message.into(), b"")?; Ok(Signature(signature, recovery_id.unwrap())) } } @@ -32,13 +32,13 @@ fn derive_slice(seed: &[u8], path: &Path) -> Result<PrivateKey> { for (i, component) in path.components().enumerate() { let (secret, chain_code) = extended_key.split_at(32); - let secret = SecretKey::from_be_bytes(secret)?; + let secret = SecretKey::from_slice(secret)?; let mut hmac: Hmac<Sha512> = Hmac::<Sha512>::new_from_slice(chain_code)?; let value = match component { Component::Hardened(value) => { hmac.update(&[0]); - hmac.update(&secret.to_be_bytes()); + hmac.update(&secret.to_bytes()); value | HARDENED } Component::Normal(value) => { @@ -50,10 +50,11 @@ fn derive_slice(seed: &[u8], path: &Path) -> Result<PrivateKey> { let mut child_key = hmac.finalize().into_bytes(); - let child_secret = SecretKey::from_be_bytes(&child_key[..32]) + let child_secret = SecretKey::from_slice(&child_key[..32]) .with_context(|| format!("path '{path}' component #{i} yields invalid child key"))?; - let next_secret = SecretKey::new(*child_secret.as_scalar_core() + *secret.as_scalar_core()); - child_key[..32].copy_from_slice(&next_secret.to_be_bytes()); + let next_secret = + SecretKey::new(*child_secret.as_scalar_primitive() + *secret.as_scalar_primitive()); + child_key[..32].copy_from_slice(&next_secret.to_bytes()); extended_key = child_key } diff --git a/src/mnemonic.rs b/src/mnemonic.rs index 432c88a..bd9e108 100644 --- a/src/mnemonic.rs +++ b/src/mnemonic.rs @@ -156,7 +156,8 @@ impl Mnemonic { salt.nfkd().to_string().as_bytes(), ROUNDS, &mut buf, - ); + ) + .expect("HMAC can be initialized with any key length"); Seed(buf) } diff --git a/src/mnemonic/wordlist.rs b/src/mnemonic/wordlist.rs index 702f745..0e472ee 100644 --- a/src/mnemonic/wordlist.rs +++ b/src/mnemonic/wordlist.rs @@ -1,7 +1,7 @@ //! Module containing the embedded BIP-0039 word lists. use crate::mnemonic::Language; -use std::{mem::MaybeUninit, sync::Once}; +use std::sync::OnceLock; /// A parsed word list. pub struct Wordlist<'a>(Vec<&'a str>); @@ -52,16 +52,10 @@ macro_rules! match_language { )*) => {$( match $lang { Language::$l => { - static mut WORDLIST: MaybeUninit<Wordlist> = MaybeUninit::uninit(); - static INIT: Once = Once::new(); - INIT.call_once(|| { - unsafe { - WORDLIST.as_mut_ptr().write(Wordlist::parse( - include_str!(concat!("wordlist/", $f)), - )) - } - }); - unsafe { &*WORDLIST.as_ptr() } + static WORDLIST: OnceLock<Wordlist> = OnceLock::new(); + WORDLIST.get_or_init(|| { + Wordlist::parse(include_str!(concat!("wordlist/", $f))) + }) } } )*}; |
