Skip to content

Commit

Permalink
Streamlining private key construction API in BIP32
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed May 1, 2021
1 parent 18b6bd0 commit 187eae8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
25 changes: 6 additions & 19 deletions src/util/bip32.rs
Expand Up @@ -497,14 +497,12 @@ impl ExtendedPrivKey {
hmac_engine.input(seed);
let hmac_result: Hmac<sha512::Hash> = Hmac::from_engine(hmac_engine);

let sk = secp256k1::SecretKey::from_slice(&hmac_result[..32])?;

Ok(ExtendedPrivKey {
network: network,
depth: 0,
parent_fingerprint: Default::default(),
child_number: ChildNumber::from_normal_idx(0)?,
private_key: PrivateKey::new(sk, network),
private_key: PrivateKey::from_slice(&hmac_result[..32], network)?,
chain_code: ChainCode::from(&hmac_result[32..]),
})
}
Expand Down Expand Up @@ -541,11 +539,8 @@ impl ExtendedPrivKey {

hmac_engine.input(&endian::u32_to_array_be(u32::from(i)));
let hmac_result: Hmac<sha512::Hash> = Hmac::from_engine(hmac_engine);
let mut sk = PrivateKey::new(
secp256k1::SecretKey::from_slice(&hmac_result[..32])?,
self.network
);
sk.key.add_assign(&self.private_key[..]).map_err(Error::Ecdsa)?;
let mut sk = PrivateKey::from_slice(&hmac_result[..32], self.network)?;
sk.key.add_assign(&self.private_key[..])?;

Ok(ExtendedPrivKey {
network: self.network,
Expand Down Expand Up @@ -573,18 +568,13 @@ impl ExtendedPrivKey {
return Err(Error::UnknownVersion(ver));
};

let sk = PrivateKey::new(
secp256k1::SecretKey::from_slice(&data[46..78])?,
network
);

Ok(ExtendedPrivKey {
network: network,
depth: data[4],
parent_fingerprint: Fingerprint::from(&data[5..9]),
child_number: endian::slice_to_u32_be(&data[9..13]).into(),
chain_code: ChainCode::from(&data[13..45]),
private_key: sk,
private_key: PrivateKey::from_slice(&data[46..78], network)?,
})
}

Expand Down Expand Up @@ -656,10 +646,7 @@ impl ExtendedPubKey {

let hmac_result: Hmac<sha512::Hash> = Hmac::from_engine(hmac_engine);

let private_key = PrivateKey::new(
secp256k1::SecretKey::from_slice(&hmac_result[..32])?,
self.network,
);
let private_key = PrivateKey::from_slice(&hmac_result[..32], self.network)?;
let chain_code = ChainCode::from(&hmac_result[32..]);
Ok((private_key, chain_code))
}
Expand All @@ -674,7 +661,7 @@ impl ExtendedPubKey {
) -> Result<ExtendedPubKey, Error> {
let (sk, chain_code) = self.ckd_pub_tweak(i)?;
let mut pk = self.public_key;
pk.key.add_exp_assign(secp, &sk[..]).map_err(Error::Ecdsa)?;
pk.key.add_exp_assign(secp, &sk[..])?;

Ok(ExtendedPubKey {
network: self.network,
Expand Down
8 changes: 8 additions & 0 deletions src/util/ecdsa.rs
Expand Up @@ -201,6 +201,14 @@ impl PrivateKey {
self.key[..].to_vec()
}

/// Deserialize a private key from a slice
pub fn from_slice(data: &[u8], network: Network) -> Result<PrivateKey, Error> {
Ok(PrivateKey::new(
secp256k1::SecretKey::from_slice(data)?,
network,
))
}

/// Format the private key to WIF format.
pub fn fmt_wif(&self, fmt: &mut dyn fmt::Write) -> fmt::Result {
let mut ret = [0; 34];
Expand Down

0 comments on commit 187eae8

Please sign in to comment.