Adopting new ECDSA key constructors throughout the library

This commit is contained in:
Dr Maxim Orlovsky 2021-05-01 13:34:31 +02:00
parent ceacc7d85f
commit 18b6bd0d15
No known key found for this signature in database
GPG Key ID: FFC0250947E5C6F7
3 changed files with 24 additions and 46 deletions

View File

@ -27,10 +27,7 @@
//!
//! // Generate random key pair
//! let s = Secp256k1::new();
//! let public_key = ecdsa::PublicKey {
//! compressed: true,
//! key: s.generate_keypair(&mut thread_rng()).1,
//! };
//! let public_key = ecdsa::PublicKey::new(s.generate_keypair(&mut thread_rng()).1);
//!
//! // Generate pay-to-pubkey-hash address
//! let address = Address::p2pkh(&public_key, Network::Bitcoin);

View File

@ -497,18 +497,14 @@ 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 {
compressed: true,
network: network,
key: secp256k1::SecretKey::from_slice(
&hmac_result[..32]
).map_err(Error::Ecdsa)?,
},
private_key: PrivateKey::new(sk, network),
chain_code: ChainCode::from(&hmac_result[32..]),
})
}
@ -545,11 +541,10 @@ 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 {
compressed: true,
network: self.network,
key: secp256k1::SecretKey::from_slice(&hmac_result[..32]).map_err(Error::Ecdsa)?,
};
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)?;
Ok(ExtendedPrivKey {
@ -578,19 +573,18 @@ 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: PrivateKey {
compressed: true,
network: network,
key: secp256k1::SecretKey::from_slice(
&data[46..78]
).map_err(Error::Ecdsa)?,
},
private_key: sk,
})
}
@ -662,11 +656,10 @@ impl ExtendedPubKey {
let hmac_result: Hmac<sha512::Hash> = Hmac::from_engine(hmac_engine);
let private_key = PrivateKey {
compressed: true,
network: self.network,
key: secp256k1::SecretKey::from_slice(&hmac_result[..32])?,
};
let private_key = PrivateKey::new(
secp256k1::SecretKey::from_slice(&hmac_result[..32])?,
self.network,
);
let chain_code = ChainCode::from(&hmac_result[32..]);
Ok((private_key, chain_code))
}

View File

@ -329,25 +329,13 @@ mod tests {
let (sk2, pk2) = secp.generate_keypair(&mut thread_rng());
let (sk3, pk3) = secp.generate_keypair(&mut thread_rng());
let sk1 = PrivateKey {
key: sk1,
compressed: true,
network: Network::Bitcoin,
};
let sk2 = PrivateKey {
key: sk2,
compressed: false,
network: Network::Bitcoin,
};
let sk3 = PrivateKey {
key: sk3,
compressed: true,
network: Network::Bitcoin,
};
let sk1 = PrivateKey::new(sk1, Network::Bitcoin);
let sk2 = PrivateKey::new_uncompressed(sk2, Network::Bitcoin);
let sk3 = PrivateKey::new(sk3, Network::Bitcoin);
let pks = [
PublicKey { key: pk1, compressed: true },
PublicKey { key: pk2, compressed: false },
PublicKey { key: pk3, compressed: true },
PublicKey::new(pk1),
PublicKey::new_uncompressed(pk2),
PublicKey::new(pk3),
];
let contract = b"if bottle mt dont remembr drink wont pay";