Streamlining private key construction API in BIP32
This commit is contained in:
parent
18b6bd0d15
commit
187eae8a13
|
@ -497,14 +497,12 @@ impl ExtendedPrivKey {
|
||||||
hmac_engine.input(seed);
|
hmac_engine.input(seed);
|
||||||
let hmac_result: Hmac<sha512::Hash> = Hmac::from_engine(hmac_engine);
|
let hmac_result: Hmac<sha512::Hash> = Hmac::from_engine(hmac_engine);
|
||||||
|
|
||||||
let sk = secp256k1::SecretKey::from_slice(&hmac_result[..32])?;
|
|
||||||
|
|
||||||
Ok(ExtendedPrivKey {
|
Ok(ExtendedPrivKey {
|
||||||
network: network,
|
network: network,
|
||||||
depth: 0,
|
depth: 0,
|
||||||
parent_fingerprint: Default::default(),
|
parent_fingerprint: Default::default(),
|
||||||
child_number: ChildNumber::from_normal_idx(0)?,
|
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..]),
|
chain_code: ChainCode::from(&hmac_result[32..]),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -541,11 +539,8 @@ impl ExtendedPrivKey {
|
||||||
|
|
||||||
hmac_engine.input(&endian::u32_to_array_be(u32::from(i)));
|
hmac_engine.input(&endian::u32_to_array_be(u32::from(i)));
|
||||||
let hmac_result: Hmac<sha512::Hash> = Hmac::from_engine(hmac_engine);
|
let hmac_result: Hmac<sha512::Hash> = Hmac::from_engine(hmac_engine);
|
||||||
let mut sk = PrivateKey::new(
|
let mut sk = PrivateKey::from_slice(&hmac_result[..32], self.network)?;
|
||||||
secp256k1::SecretKey::from_slice(&hmac_result[..32])?,
|
sk.key.add_assign(&self.private_key[..])?;
|
||||||
self.network
|
|
||||||
);
|
|
||||||
sk.key.add_assign(&self.private_key[..]).map_err(Error::Ecdsa)?;
|
|
||||||
|
|
||||||
Ok(ExtendedPrivKey {
|
Ok(ExtendedPrivKey {
|
||||||
network: self.network,
|
network: self.network,
|
||||||
|
@ -573,18 +568,13 @@ impl ExtendedPrivKey {
|
||||||
return Err(Error::UnknownVersion(ver));
|
return Err(Error::UnknownVersion(ver));
|
||||||
};
|
};
|
||||||
|
|
||||||
let sk = PrivateKey::new(
|
|
||||||
secp256k1::SecretKey::from_slice(&data[46..78])?,
|
|
||||||
network
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(ExtendedPrivKey {
|
Ok(ExtendedPrivKey {
|
||||||
network: network,
|
network: network,
|
||||||
depth: data[4],
|
depth: data[4],
|
||||||
parent_fingerprint: Fingerprint::from(&data[5..9]),
|
parent_fingerprint: Fingerprint::from(&data[5..9]),
|
||||||
child_number: endian::slice_to_u32_be(&data[9..13]).into(),
|
child_number: endian::slice_to_u32_be(&data[9..13]).into(),
|
||||||
chain_code: ChainCode::from(&data[13..45]),
|
chain_code: ChainCode::from(&data[13..45]),
|
||||||
private_key: sk,
|
private_key: PrivateKey::from_slice(&data[46..78], network)?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -656,10 +646,7 @@ impl ExtendedPubKey {
|
||||||
|
|
||||||
let hmac_result: Hmac<sha512::Hash> = Hmac::from_engine(hmac_engine);
|
let hmac_result: Hmac<sha512::Hash> = Hmac::from_engine(hmac_engine);
|
||||||
|
|
||||||
let private_key = PrivateKey::new(
|
let private_key = PrivateKey::from_slice(&hmac_result[..32], self.network)?;
|
||||||
secp256k1::SecretKey::from_slice(&hmac_result[..32])?,
|
|
||||||
self.network,
|
|
||||||
);
|
|
||||||
let chain_code = ChainCode::from(&hmac_result[32..]);
|
let chain_code = ChainCode::from(&hmac_result[32..]);
|
||||||
Ok((private_key, chain_code))
|
Ok((private_key, chain_code))
|
||||||
}
|
}
|
||||||
|
@ -674,7 +661,7 @@ impl ExtendedPubKey {
|
||||||
) -> Result<ExtendedPubKey, Error> {
|
) -> Result<ExtendedPubKey, Error> {
|
||||||
let (sk, chain_code) = self.ckd_pub_tweak(i)?;
|
let (sk, chain_code) = self.ckd_pub_tweak(i)?;
|
||||||
let mut pk = self.public_key;
|
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 {
|
Ok(ExtendedPubKey {
|
||||||
network: self.network,
|
network: self.network,
|
||||||
|
|
|
@ -201,6 +201,14 @@ impl PrivateKey {
|
||||||
self.key[..].to_vec()
|
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.
|
/// Format the private key to WIF format.
|
||||||
pub fn fmt_wif(&self, fmt: &mut dyn fmt::Write) -> fmt::Result {
|
pub fn fmt_wif(&self, fmt: &mut dyn fmt::Write) -> fmt::Result {
|
||||||
let mut ret = [0; 34];
|
let mut ret = [0; 34];
|
||||||
|
|
Loading…
Reference in New Issue