2023-09-01 04:10:56 +00:00
|
|
|
use crate::private_key::PrivateKeyBytes;
|
|
|
|
|
|
|
|
use digest::Digest;
|
|
|
|
use ripemd::Ripemd160;
|
|
|
|
use sha2::Sha256;
|
|
|
|
use thiserror::Error;
|
|
|
|
|
2023-09-01 04:57:05 +00:00
|
|
|
pub(crate) type PublicKeyBytes = [u8; 33];
|
2023-09-01 04:10:56 +00:00
|
|
|
|
2023-09-01 04:49:35 +00:00
|
|
|
/// Functions required to use an `ExtendedPublicKey`.
|
2023-09-01 04:10:56 +00:00
|
|
|
pub trait PublicKey: Sized {
|
2023-09-01 04:49:35 +00:00
|
|
|
/// The error returned by [`PublicKey::derive_child()`].
|
2023-09-01 04:10:56 +00:00
|
|
|
type Err: std::error::Error;
|
|
|
|
|
2023-09-06 15:21:47 +00:00
|
|
|
/*
|
|
|
|
* This may not be doable given ed25519 public keys must be derived from the private key.
|
2023-09-01 04:49:35 +00:00
|
|
|
/// Create a Self from bytes.
|
2023-09-01 04:10:56 +00:00
|
|
|
fn from_bytes(b: &PublicKeyBytes) -> Self;
|
2023-09-06 15:21:47 +00:00
|
|
|
*/
|
2023-09-01 04:49:35 +00:00
|
|
|
|
2023-09-06 15:21:47 +00:00
|
|
|
/// Convert a &Self to bytes.
|
2023-09-01 04:10:56 +00:00
|
|
|
fn to_bytes(&self) -> PublicKeyBytes;
|
|
|
|
|
|
|
|
/// Derive a child [`PublicKey`] with given `PrivateKeyBytes`.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// An error may be returned if:
|
|
|
|
/// * A nonzero `other` is provided.
|
|
|
|
/// * An error specific to the given algorithm was encountered.
|
|
|
|
fn derive_child(&self, other: PrivateKeyBytes) -> Result<Self, Self::Err>;
|
|
|
|
|
2023-09-01 04:49:35 +00:00
|
|
|
/// Create a BIP-0032/SLIP-0010 fingerprint from the public key.
|
2023-09-01 04:10:56 +00:00
|
|
|
fn fingerprint(&self) -> [u8; 4] {
|
|
|
|
let hash = Sha256::new().chain_update(self.to_bytes()).finalize();
|
|
|
|
let hash = Ripemd160::new().chain_update(hash).finalize();
|
|
|
|
// Note: Safety assured by type returned from Ripemd160
|
2023-11-05 06:29:10 +00:00
|
|
|
hash[..4]
|
|
|
|
.try_into()
|
|
|
|
.expect("Ripemd160 returned too little data")
|
2023-09-01 04:10:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-01 04:49:35 +00:00
|
|
|
/// Errors associated with creating and arithmetic on public keys. This specific error is only
|
|
|
|
/// intended to be used by the implementations in this crate.
|
2023-09-01 04:10:56 +00:00
|
|
|
#[derive(Clone, Debug, Error)]
|
|
|
|
pub enum PublicKeyError {
|
2023-09-01 04:49:35 +00:00
|
|
|
/// For the given algorithm, the private key must be nonzero.
|
2023-09-01 04:10:56 +00:00
|
|
|
#[error("The provided public key must be nonzero, but is not")]
|
|
|
|
NonZero,
|
|
|
|
|
2023-09-06 15:21:47 +00:00
|
|
|
/// Public key derivation is unsupported for this algorithm.
|
|
|
|
#[error("Public key derivation is unsupported for this algorithm")]
|
|
|
|
DerivationUnsupported,
|
2023-09-01 04:10:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "secp256k1")]
|
|
|
|
use k256::{
|
|
|
|
elliptic_curve::{group::prime::PrimeCurveAffine, sec1::ToEncodedPoint},
|
|
|
|
AffinePoint, NonZeroScalar,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[cfg(feature = "secp256k1")]
|
|
|
|
impl PublicKey for k256::PublicKey {
|
|
|
|
type Err = PublicKeyError;
|
|
|
|
|
2023-09-06 15:21:47 +00:00
|
|
|
/*
|
2023-09-01 04:10:56 +00:00
|
|
|
fn from_bytes(b: &PublicKeyBytes) -> Self {
|
|
|
|
Self::from_sec1_bytes(b).expect("Invalid public key bytes")
|
|
|
|
}
|
2023-09-06 15:21:47 +00:00
|
|
|
*/
|
2023-09-01 04:10:56 +00:00
|
|
|
|
|
|
|
fn to_bytes(&self) -> PublicKeyBytes {
|
2023-09-26 02:16:33 +00:00
|
|
|
let mut result = [0u8; 33];
|
|
|
|
result[..].copy_from_slice(self.to_encoded_point(true).as_bytes());
|
|
|
|
result
|
2023-09-01 04:10:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn derive_child(&self, other: PrivateKeyBytes) -> Result<Self, Self::Err> {
|
|
|
|
if other.iter().all(|n| n == &0) {
|
|
|
|
return Err(PublicKeyError::NonZero);
|
|
|
|
}
|
|
|
|
// Checked: See above
|
|
|
|
let scalar = Option::<NonZeroScalar>::from(NonZeroScalar::from_repr(other.into()))
|
|
|
|
.expect("Should have been able to get a NonZeroScalar");
|
|
|
|
|
|
|
|
let point = self.to_projective() + (AffinePoint::generator() * *scalar);
|
2023-09-30 07:19:37 +00:00
|
|
|
Ok(Self::from_affine(point.into()).expect("Could not from_affine after scalar arithmetic"))
|
2023-09-01 04:10:56 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-06 15:21:47 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
use ed25519_dalek::VerifyingKey;
|
|
|
|
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
impl PublicKey for VerifyingKey {
|
|
|
|
type Err = PublicKeyError;
|
|
|
|
|
|
|
|
/*
|
|
|
|
fn from_bytes(b: &PublicKeyBytes) -> Self {
|
|
|
|
Self::from_bytes(b).expect("Invalid public key bytes")
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
fn to_bytes(&self) -> PublicKeyBytes {
|
|
|
|
let mut result = [0u8; 33];
|
|
|
|
result[1..33].copy_from_slice(&self.to_bytes()[..]);
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
fn derive_child(&self, _other: PrivateKeyBytes) -> Result<Self, Self::Err> {
|
|
|
|
Err(Self::Err::DerivationUnsupported)
|
|
|
|
}
|
|
|
|
}
|