use bc_crypto::ECDSA_SIGNATURE_SIZE;
use bc_ur::prelude::*;
use crate::{
Digest, ECKey, ECKeyBase, ECPublicKeyBase, Error, Reference,
ReferenceProvider, Result, Signature, Verifier, tags,
};
pub const ECDSA_PUBLIC_KEY_SIZE: usize = bc_crypto::ECDSA_PUBLIC_KEY_SIZE;
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct ECPublicKey([u8; ECDSA_PUBLIC_KEY_SIZE]);
impl ECPublicKey {
pub const fn from_data(data: [u8; ECDSA_PUBLIC_KEY_SIZE]) -> Self {
Self(data)
}
pub fn data(&self) -> &[u8; ECDSA_PUBLIC_KEY_SIZE] { &self.0 }
pub fn as_bytes(&self) -> &[u8] { self.as_ref() }
}
impl ECPublicKey {
pub fn verify(
&self,
signature: &[u8; ECDSA_SIGNATURE_SIZE],
message: impl AsRef<[u8]>,
) -> bool {
bc_crypto::ecdsa_verify(&self.0, signature, message)
}
}
impl AsRef<[u8]> for ECPublicKey {
fn as_ref(&self) -> &[u8] { &self.0 }
}
impl std::fmt::Debug for ECPublicKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "ECPublicKey({})", self.hex())
}
}
impl ECKeyBase for ECPublicKey {
const KEY_SIZE: usize = bc_crypto::ECDSA_PUBLIC_KEY_SIZE;
fn from_data_ref(data: impl AsRef<[u8]>) -> Result<Self>
where
Self: Sized,
{
let data = data.as_ref();
if data.len() != ECDSA_PUBLIC_KEY_SIZE {
return Err(Error::invalid_size(
"ECDSA public key",
ECDSA_PUBLIC_KEY_SIZE,
data.len(),
));
}
let mut key = [0u8; ECDSA_PUBLIC_KEY_SIZE];
key.copy_from_slice(data);
Ok(Self(key))
}
fn data(&self) -> &[u8] { self.into() }
}
impl Verifier for ECPublicKey {
fn verify(&self, signature: &Signature, message: &dyn AsRef<[u8]>) -> bool {
match signature {
Signature::ECDSA(sig) => self.verify(sig, message),
_ => false,
}
}
}
impl ECKey for ECPublicKey {
fn public_key(&self) -> ECPublicKey { *self }
}
impl ECPublicKeyBase for ECPublicKey {
fn uncompressed_public_key(&self) -> crate::ECUncompressedPublicKey {
bc_crypto::ecdsa_decompress_public_key(&self.0).into()
}
}
impl<'a> From<&'a ECPublicKey> for &'a [u8; ECPublicKey::KEY_SIZE] {
fn from(value: &'a ECPublicKey) -> Self { &value.0 }
}
impl From<[u8; ECDSA_PUBLIC_KEY_SIZE]> for ECPublicKey {
fn from(value: [u8; ECDSA_PUBLIC_KEY_SIZE]) -> Self {
Self::from_data(value)
}
}
impl<'a> From<&'a ECPublicKey> for &'a [u8] {
fn from(value: &'a ECPublicKey) -> Self { &value.0 }
}
impl CBORTagged for ECPublicKey {
fn cbor_tags() -> Vec<Tag> {
tags_for_values(&[tags::TAG_EC_KEY, tags::TAG_EC_KEY_V1])
}
}
impl From<ECPublicKey> for CBOR {
fn from(value: ECPublicKey) -> Self { value.tagged_cbor() }
}
impl CBORTaggedEncodable for ECPublicKey {
fn untagged_cbor(&self) -> CBOR {
let mut m = Map::new();
m.insert(3, CBOR::to_byte_string(self.0));
m.into()
}
}
impl ReferenceProvider for ECPublicKey {
fn reference(&self) -> Reference {
Reference::from_digest(Digest::from_image(
self.tagged_cbor().to_cbor_data(),
))
}
}
impl std::fmt::Display for ECPublicKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "ECPublicKey({})", self.ref_hex_short())
}
}