|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use super::error::DecodingError; |
|
|
use asn1_der::typed::{DerDecodable, Sequence}; |
|
|
use core::cmp; |
|
|
use core::fmt; |
|
|
use core::hash; |
|
|
use libsecp256k1::{Message, Signature}; |
|
|
use sha2::{Digest as ShaDigestTrait, Sha256}; |
|
|
use zeroize::Zeroize; |
|
|
|
|
|
|
|
|
#[derive(Clone)] |
|
|
pub struct Keypair { |
|
|
secret: SecretKey, |
|
|
public: PublicKey, |
|
|
} |
|
|
|
|
|
impl Keypair { |
|
|
|
|
|
#[cfg(feature = "rand")] |
|
|
pub fn generate() -> Keypair { |
|
|
Keypair::from(SecretKey::generate()) |
|
|
} |
|
|
|
|
|
|
|
|
pub fn public(&self) -> &PublicKey { |
|
|
&self.public |
|
|
} |
|
|
|
|
|
|
|
|
pub fn secret(&self) -> &SecretKey { |
|
|
&self.secret |
|
|
} |
|
|
} |
|
|
|
|
|
impl fmt::Debug for Keypair { |
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
|
|
f.debug_struct("Keypair") |
|
|
.field("public", &self.public) |
|
|
.finish() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
impl From<SecretKey> for Keypair { |
|
|
fn from(secret: SecretKey) -> Keypair { |
|
|
let public = PublicKey(libsecp256k1::PublicKey::from_secret_key(&secret.0)); |
|
|
Keypair { secret, public } |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
impl From<Keypair> for SecretKey { |
|
|
fn from(kp: Keypair) -> SecretKey { |
|
|
kp.secret |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
#[derive(Clone)] |
|
|
pub struct SecretKey(libsecp256k1::SecretKey); |
|
|
|
|
|
impl fmt::Debug for SecretKey { |
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
|
|
write!(f, "SecretKey") |
|
|
} |
|
|
} |
|
|
|
|
|
impl SecretKey { |
|
|
|
|
|
#[cfg(feature = "rand")] |
|
|
pub fn generate() -> SecretKey { |
|
|
SecretKey(libsecp256k1::SecretKey::random(&mut rand::thread_rng())) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn try_from_bytes(mut sk: impl AsMut<[u8]>) -> Result<SecretKey, DecodingError> { |
|
|
let sk_bytes = sk.as_mut(); |
|
|
let secret = libsecp256k1::SecretKey::parse_slice(&*sk_bytes) |
|
|
.map_err(|e| DecodingError::failed_to_parse("parse secp256k1 secret key", e))?; |
|
|
sk_bytes.zeroize(); |
|
|
Ok(SecretKey(secret)) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn from_der(mut der: impl AsMut<[u8]>) -> Result<SecretKey, DecodingError> { |
|
|
|
|
|
let der_obj = der.as_mut(); |
|
|
|
|
|
let mut sk_bytes = Sequence::decode(der_obj) |
|
|
.and_then(|seq| seq.get(1)) |
|
|
.and_then(Vec::load) |
|
|
.map_err(|e| DecodingError::failed_to_parse("secp256k1 SecretKey bytes", e))?; |
|
|
|
|
|
let sk = SecretKey::try_from_bytes(&mut sk_bytes)?; |
|
|
sk_bytes.zeroize(); |
|
|
der_obj.zeroize(); |
|
|
Ok(sk) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn sign(&self, msg: &[u8]) -> Vec<u8> { |
|
|
let generic_array = Sha256::digest(msg); |
|
|
|
|
|
|
|
|
let mut array = [0u8; 32]; |
|
|
array.copy_from_slice(generic_array.as_slice()); |
|
|
|
|
|
let message = Message::parse(&array); |
|
|
|
|
|
libsecp256k1::sign(&message, &self.0) |
|
|
.0 |
|
|
.serialize_der() |
|
|
.as_ref() |
|
|
.into() |
|
|
} |
|
|
|
|
|
|
|
|
pub fn to_bytes(&self) -> [u8; 32] { |
|
|
self.0.serialize() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
#[derive(Eq, Clone)] |
|
|
pub struct PublicKey(libsecp256k1::PublicKey); |
|
|
|
|
|
impl fmt::Debug for PublicKey { |
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
|
|
f.write_str("PublicKey(compressed): ")?; |
|
|
for byte in &self.to_bytes() { |
|
|
write!(f, "{byte:x}")?; |
|
|
} |
|
|
Ok(()) |
|
|
} |
|
|
} |
|
|
|
|
|
impl cmp::PartialEq for PublicKey { |
|
|
fn eq(&self, other: &Self) -> bool { |
|
|
self.to_bytes().eq(&other.to_bytes()) |
|
|
} |
|
|
} |
|
|
|
|
|
impl hash::Hash for PublicKey { |
|
|
fn hash<H: hash::Hasher>(&self, state: &mut H) { |
|
|
self.to_bytes().hash(state); |
|
|
} |
|
|
} |
|
|
|
|
|
impl cmp::PartialOrd for PublicKey { |
|
|
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { |
|
|
Some(self.cmp(other)) |
|
|
} |
|
|
} |
|
|
|
|
|
impl cmp::Ord for PublicKey { |
|
|
fn cmp(&self, other: &Self) -> cmp::Ordering { |
|
|
self.to_bytes().cmp(&other.to_bytes()) |
|
|
} |
|
|
} |
|
|
|
|
|
impl PublicKey { |
|
|
|
|
|
pub fn verify(&self, msg: &[u8], sig: &[u8]) -> bool { |
|
|
self.verify_hash(Sha256::digest(msg).as_ref(), sig) |
|
|
} |
|
|
|
|
|
|
|
|
pub fn verify_hash(&self, msg: &[u8], sig: &[u8]) -> bool { |
|
|
Message::parse_slice(msg) |
|
|
.and_then(|m| Signature::parse_der(sig).map(|s| libsecp256k1::verify(&m, &s, &self.0))) |
|
|
.unwrap_or(false) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn to_bytes(&self) -> [u8; 33] { |
|
|
self.0.serialize_compressed() |
|
|
} |
|
|
|
|
|
|
|
|
pub fn to_bytes_uncompressed(&self) -> [u8; 65] { |
|
|
self.0.serialize() |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn try_from_bytes(k: &[u8]) -> Result<PublicKey, DecodingError> { |
|
|
libsecp256k1::PublicKey::parse_slice(k, Some(libsecp256k1::PublicKeyFormat::Compressed)) |
|
|
.map_err(|e| DecodingError::failed_to_parse("secp256k1 public key", e)) |
|
|
.map(PublicKey) |
|
|
} |
|
|
} |
|
|
|
|
|
#[cfg(test)] |
|
|
mod tests { |
|
|
use super::*; |
|
|
|
|
|
#[test] |
|
|
#[cfg(feature = "rand")] |
|
|
fn secp256k1_secret_from_bytes() { |
|
|
let sk1 = SecretKey::generate(); |
|
|
let mut sk_bytes = [0; 32]; |
|
|
sk_bytes.copy_from_slice(&sk1.0.serialize()[..]); |
|
|
let sk2 = SecretKey::try_from_bytes(&mut sk_bytes).unwrap(); |
|
|
assert_eq!(sk1.0.serialize(), sk2.0.serialize()); |
|
|
assert_eq!(sk_bytes, [0; 32]); |
|
|
} |
|
|
} |
|
|
|