diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2022-05-03 07:59:24 +0200 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2022-05-03 07:59:24 +0200 |
| commit | 2cf5f650b8fdbd1992f3d65ad6e102087e74f896 (patch) | |
| tree | 7c501fcd82b33a6203cb6b55c4820d698c1d83e5 /src/hdk.rs | |
| parent | 1c13a451bc2e56ce8531d5da683a3c7aa6d4d887 (diff) | |
Use Rust Secp256k1 implementation from RustCrypto project (#92)
This PR ports `hdwallet` to use the `k256` crate - a native Rust implementation of secp256k1 elliptic curve and ECDSA signing.
The motivation for this change is that we can just use the `wasm32-wasi` Rust target without requiring a WASI C toolchain! Notice that the CI no longer requires installing the WASI SDK in order to build the WASI target.
Diffstat (limited to 'src/hdk.rs')
| -rw-r--r-- | src/hdk.rs | 32 |
1 files changed, 10 insertions, 22 deletions
@@ -6,7 +6,7 @@ pub use self::path::{Component, Path}; use crate::account::PrivateKey; use anyhow::{Context as _, Result}; use hmac::{Hmac, Mac as _}; -use secp256k1::{PublicKey, Secp256k1, SecretKey, Signing}; +use k256::{elliptic_curve::sec1::ToEncodedPoint as _, SecretKey}; use sha2::Sha512; /// A value indicating a path component is hardened. @@ -30,34 +30,31 @@ fn derive_slice(seed: &[u8], path: &Path) -> Result<PrivateKey> { hmac.finalize().into_bytes() }; - let secp = Secp256k1::signing_only(); for (i, component) in path.components().enumerate() { let (secret, chain_code) = extended_key.split_at(32); + let secret = SecretKey::from_be_bytes(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); + hmac.update(&secret.to_be_bytes()); value | HARDENED } Component::Normal(value) => { - hmac.update(&public_key(&secp, secret)?); + hmac.update(secret.public_key().to_encoded_point(true).as_bytes()); value } }; hmac.update(&value.to_be_bytes()); let mut child_key = hmac.finalize().into_bytes(); - let child_secret = SecretKey::from_slice(&child_key[..32]) - .and_then(|mut key| { - key.add_assign(secret)?; - Ok(key) - }) - .with_context(|| { - format!("path '{}' component #{} yields invalid child key", path, i) - })?; - child_key[..32].copy_from_slice(child_secret.as_ref()); + + let child_secret = SecretKey::from_be_bytes(&child_key[..32]).with_context(|| { + format!("path '{}' component #{} yields invalid child key", path, i) + })?; + 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()); extended_key = child_key } @@ -65,15 +62,6 @@ fn derive_slice(seed: &[u8], path: &Path) -> Result<PrivateKey> { PrivateKey::new(&extended_key[..32]) } -fn public_key<C>(secp: &Secp256k1<C>, secret: &[u8]) -> Result<[u8; 33]> -where - C: Signing, -{ - let secret_key = SecretKey::from_slice(secret)?; - let public_key = PublicKey::from_secret_key(secp, &secret_key); - Ok(public_key.serialize()) -} - #[cfg(test)] mod tests { use super::*; |
