Merge pull request #592 from LNP-BP/feat/ecdsa-key-creation
Constructors for compressed and uncompressed ECDSA keys
This commit is contained in:
commit
d0fb626baf
|
@ -27,10 +27,7 @@
|
||||||
//!
|
//!
|
||||||
//! // Generate random key pair
|
//! // Generate random key pair
|
||||||
//! let s = Secp256k1::new();
|
//! let s = Secp256k1::new();
|
||||||
//! let public_key = ecdsa::PublicKey {
|
//! let public_key = ecdsa::PublicKey::new(s.generate_keypair(&mut thread_rng()).1);
|
||||||
//! compressed: true,
|
|
||||||
//! key: s.generate_keypair(&mut thread_rng()).1,
|
|
||||||
//! };
|
|
||||||
//!
|
//!
|
||||||
//! // Generate pay-to-pubkey-hash address
|
//! // Generate pay-to-pubkey-hash address
|
||||||
//! let address = Address::p2pkh(&public_key, Network::Bitcoin);
|
//! let address = Address::p2pkh(&public_key, Network::Bitcoin);
|
||||||
|
|
|
@ -502,13 +502,7 @@ impl ExtendedPrivKey {
|
||||||
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 {
|
private_key: PrivateKey::from_slice(&hmac_result[..32], network)?,
|
||||||
compressed: true,
|
|
||||||
network: network,
|
|
||||||
key: secp256k1::SecretKey::from_slice(
|
|
||||||
&hmac_result[..32]
|
|
||||||
).map_err(Error::Ecdsa)?,
|
|
||||||
},
|
|
||||||
chain_code: ChainCode::from(&hmac_result[32..]),
|
chain_code: ChainCode::from(&hmac_result[32..]),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -545,12 +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 {
|
let mut sk = PrivateKey::from_slice(&hmac_result[..32], self.network)?;
|
||||||
compressed: true,
|
sk.key.add_assign(&self.private_key[..])?;
|
||||||
network: self.network,
|
|
||||||
key: secp256k1::SecretKey::from_slice(&hmac_result[..32]).map_err(Error::Ecdsa)?,
|
|
||||||
};
|
|
||||||
sk.key.add_assign(&self.private_key[..]).map_err(Error::Ecdsa)?;
|
|
||||||
|
|
||||||
Ok(ExtendedPrivKey {
|
Ok(ExtendedPrivKey {
|
||||||
network: self.network,
|
network: self.network,
|
||||||
|
@ -584,13 +574,7 @@ impl ExtendedPrivKey {
|
||||||
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: PrivateKey {
|
private_key: PrivateKey::from_slice(&data[46..78], network)?,
|
||||||
compressed: true,
|
|
||||||
network: network,
|
|
||||||
key: secp256k1::SecretKey::from_slice(
|
|
||||||
&data[46..78]
|
|
||||||
).map_err(Error::Ecdsa)?,
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -662,11 +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 {
|
let private_key = PrivateKey::from_slice(&hmac_result[..32], self.network)?;
|
||||||
compressed: true,
|
|
||||||
network: self.network,
|
|
||||||
key: secp256k1::SecretKey::from_slice(&hmac_result[..32])?,
|
|
||||||
};
|
|
||||||
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))
|
||||||
}
|
}
|
||||||
|
@ -681,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,
|
||||||
|
|
|
@ -329,25 +329,13 @@ mod tests {
|
||||||
let (sk2, pk2) = secp.generate_keypair(&mut thread_rng());
|
let (sk2, pk2) = secp.generate_keypair(&mut thread_rng());
|
||||||
let (sk3, pk3) = secp.generate_keypair(&mut thread_rng());
|
let (sk3, pk3) = secp.generate_keypair(&mut thread_rng());
|
||||||
|
|
||||||
let sk1 = PrivateKey {
|
let sk1 = PrivateKey::new(sk1, Network::Bitcoin);
|
||||||
key: sk1,
|
let sk2 = PrivateKey::new_uncompressed(sk2, Network::Bitcoin);
|
||||||
compressed: true,
|
let sk3 = PrivateKey::new(sk3, Network::Bitcoin);
|
||||||
network: Network::Bitcoin,
|
|
||||||
};
|
|
||||||
let sk2 = PrivateKey {
|
|
||||||
key: sk2,
|
|
||||||
compressed: false,
|
|
||||||
network: Network::Bitcoin,
|
|
||||||
};
|
|
||||||
let sk3 = PrivateKey {
|
|
||||||
key: sk3,
|
|
||||||
compressed: true,
|
|
||||||
network: Network::Bitcoin,
|
|
||||||
};
|
|
||||||
let pks = [
|
let pks = [
|
||||||
PublicKey { key: pk1, compressed: true },
|
PublicKey::new(pk1),
|
||||||
PublicKey { key: pk2, compressed: false },
|
PublicKey::new_uncompressed(pk2),
|
||||||
PublicKey { key: pk3, compressed: true },
|
PublicKey::new(pk3),
|
||||||
];
|
];
|
||||||
let contract = b"if bottle mt dont remembr drink wont pay";
|
let contract = b"if bottle mt dont remembr drink wont pay";
|
||||||
|
|
||||||
|
|
|
@ -37,6 +37,23 @@ pub struct PublicKey {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PublicKey {
|
impl PublicKey {
|
||||||
|
/// Constructs compressed ECDSA public key from the provided generic Secp256k1 public key
|
||||||
|
pub fn new(key: secp256k1::PublicKey) -> PublicKey {
|
||||||
|
PublicKey {
|
||||||
|
compressed: true,
|
||||||
|
key: key,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Constructs uncompressed (legacy) ECDSA public key from the provided generic Secp256k1
|
||||||
|
/// public key
|
||||||
|
pub fn new_uncompressed(key: secp256k1::PublicKey) -> PublicKey {
|
||||||
|
PublicKey {
|
||||||
|
compressed: false,
|
||||||
|
key: key,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns bitcoin 160-bit hash of the public key
|
/// Returns bitcoin 160-bit hash of the public key
|
||||||
pub fn pubkey_hash(&self) -> PubkeyHash {
|
pub fn pubkey_hash(&self) -> PubkeyHash {
|
||||||
if self.compressed {
|
if self.compressed {
|
||||||
|
@ -151,6 +168,26 @@ pub struct PrivateKey {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PrivateKey {
|
impl PrivateKey {
|
||||||
|
/// Constructs compressed ECDSA private key from the provided generic Secp256k1 private key
|
||||||
|
/// and the specified network
|
||||||
|
pub fn new(key: secp256k1::SecretKey, network: Network) -> PrivateKey {
|
||||||
|
PrivateKey {
|
||||||
|
compressed: true,
|
||||||
|
network: network,
|
||||||
|
key: key,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Constructs uncompressed (legacy) ECDSA private key from the provided generic Secp256k1
|
||||||
|
/// private key and the specified network
|
||||||
|
pub fn new_uncompressed(key: secp256k1::SecretKey, network: Network) -> PrivateKey {
|
||||||
|
PrivateKey {
|
||||||
|
compressed: false,
|
||||||
|
network: network,
|
||||||
|
key: key,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Creates a public key from this private key
|
/// Creates a public key from this private key
|
||||||
pub fn public_key<C: secp256k1::Signing>(&self, secp: &Secp256k1<C>) -> PublicKey {
|
pub fn public_key<C: secp256k1::Signing>(&self, secp: &Secp256k1<C>) -> PublicKey {
|
||||||
PublicKey {
|
PublicKey {
|
||||||
|
@ -164,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