|
| 1 | +use super::SecretKey; |
| 2 | + |
| 3 | +use crate::{LwkError, XOnlyPublicKey}; |
| 4 | + |
| 5 | +use std::fmt::Display; |
| 6 | +use std::str::FromStr; |
| 7 | +use std::sync::Arc; |
| 8 | + |
| 9 | +use elements::bitcoin::secp256k1; |
| 10 | +use elements::hashes::hex::FromHex; |
| 11 | +use elements::hex::ToHex; |
| 12 | + |
| 13 | +use lwk_wollet::elements_miniscript::ToPublicKey; |
| 14 | + |
| 15 | +/// A Bitcoin ECDSA public key. |
| 16 | +/// |
| 17 | +/// See [`elements::bitcoin::PublicKey`] for more details. |
| 18 | +#[derive(uniffi::Object, PartialEq, Eq, Debug, Clone, Copy)] |
| 19 | +pub struct PublicKey { |
| 20 | + inner: elements::bitcoin::PublicKey, |
| 21 | +} |
| 22 | + |
| 23 | +impl From<elements::bitcoin::PublicKey> for PublicKey { |
| 24 | + fn from(inner: elements::bitcoin::PublicKey) -> Self { |
| 25 | + PublicKey { inner } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +impl From<PublicKey> for elements::bitcoin::PublicKey { |
| 30 | + fn from(value: PublicKey) -> Self { |
| 31 | + value.inner |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +impl From<&PublicKey> for elements::bitcoin::PublicKey { |
| 36 | + fn from(value: &PublicKey) -> Self { |
| 37 | + value.inner |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +impl AsRef<elements::bitcoin::PublicKey> for PublicKey { |
| 42 | + fn as_ref(&self) -> &elements::bitcoin::PublicKey { |
| 43 | + &self.inner |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +impl FromStr for PublicKey { |
| 48 | + type Err = LwkError; |
| 49 | + |
| 50 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 51 | + let inner = elements::bitcoin::PublicKey::from_str(s).map_err(|e| LwkError::Generic { |
| 52 | + msg: format!("Invalid public key: {e}"), |
| 53 | + })?; |
| 54 | + Ok(Self { inner }) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +impl Display for PublicKey { |
| 59 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 60 | + write!(f, "{}", self.inner) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +#[uniffi::export] |
| 65 | +impl PublicKey { |
| 66 | + /// See [`elements::bitcoin::PublicKey::from_slice`]. |
| 67 | + #[uniffi::constructor] |
| 68 | + pub fn from_bytes(bytes: &[u8]) -> Result<Arc<Self>, LwkError> { |
| 69 | + let inner = |
| 70 | + elements::bitcoin::PublicKey::from_slice(bytes).map_err(|e| LwkError::Generic { |
| 71 | + msg: format!("Invalid public key: {e}"), |
| 72 | + })?; |
| 73 | + Ok(Arc::new(PublicKey { inner })) |
| 74 | + } |
| 75 | + |
| 76 | + /// Creates a `PublicKey` from a hex string. |
| 77 | + #[uniffi::constructor] |
| 78 | + pub fn from_hex(hex: &str) -> Result<Arc<Self>, LwkError> { |
| 79 | + let bytes = Vec::<u8>::from_hex(hex).map_err(|e| LwkError::Generic { |
| 80 | + msg: format!("Invalid hex: {e}"), |
| 81 | + })?; |
| 82 | + Self::from_bytes(&bytes) |
| 83 | + } |
| 84 | + |
| 85 | + /// Derives a compressed `PublicKey` from a `SecretKey`. |
| 86 | + #[uniffi::constructor] |
| 87 | + pub fn from_secret_key(secret_key: &SecretKey) -> Arc<Self> { |
| 88 | + let secp = secp256k1::Secp256k1::new(); |
| 89 | + let sk: secp256k1::SecretKey = secret_key.into(); |
| 90 | + let inner_pk = secp256k1::PublicKey::from_secret_key(&secp, &sk); |
| 91 | + Arc::new(PublicKey { |
| 92 | + inner: elements::bitcoin::PublicKey::new(inner_pk), |
| 93 | + }) |
| 94 | + } |
| 95 | + |
| 96 | + /// See [`elements::bitcoin::PublicKey::to_bytes`]. |
| 97 | + pub fn to_bytes(&self) -> Vec<u8> { |
| 98 | + self.inner.to_bytes() |
| 99 | + } |
| 100 | + |
| 101 | + /// Returns the hex-encoded serialization. |
| 102 | + pub fn to_hex(&self) -> String { |
| 103 | + self.inner.to_bytes().to_hex() |
| 104 | + } |
| 105 | + |
| 106 | + /// See [`elements::bitcoin::PublicKey::compressed`]. |
| 107 | + pub fn is_compressed(&self) -> bool { |
| 108 | + self.inner.compressed |
| 109 | + } |
| 110 | + |
| 111 | + /// Converts to an x-only public key hex string. |
| 112 | + pub fn to_x_only_public_key(&self) -> XOnlyPublicKey { |
| 113 | + self.inner.to_x_only_pubkey().into() |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +#[cfg(feature = "simplicity")] |
| 118 | +impl PublicKey { |
| 119 | + /// Convert to simplicityhl's PublicKey type. |
| 120 | + /// |
| 121 | + /// TODO: delete when the version of elements is stabilized |
| 122 | + pub fn to_simplicityhl( |
| 123 | + &self, |
| 124 | + ) -> Result<lwk_simplicity_options::simplicityhl::elements::bitcoin::PublicKey, LwkError> { |
| 125 | + lwk_simplicity_options::simplicityhl::elements::bitcoin::PublicKey::from_slice( |
| 126 | + &self.to_bytes(), |
| 127 | + ) |
| 128 | + .map_err(|e| LwkError::Generic { |
| 129 | + msg: format!("Invalid public key: {e}"), |
| 130 | + }) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +#[cfg(test)] |
| 135 | +mod tests { |
| 136 | + use super::{PublicKey, SecretKey}; |
| 137 | + use elements::hashes::hex::FromHex; |
| 138 | + |
| 139 | + #[test] |
| 140 | + fn test_public_key_from_bytes() { |
| 141 | + let bytes = Vec::<u8>::from_hex( |
| 142 | + "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798", |
| 143 | + ) |
| 144 | + .unwrap(); |
| 145 | + let pk = PublicKey::from_bytes(&bytes).unwrap(); |
| 146 | + assert_eq!(pk.to_bytes(), bytes); |
| 147 | + assert!(pk.is_compressed()); |
| 148 | + } |
| 149 | + |
| 150 | + #[test] |
| 151 | + fn test_public_key_from_hex() { |
| 152 | + let hex = "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"; |
| 153 | + let pk = PublicKey::from_hex(hex).unwrap(); |
| 154 | + assert_eq!(pk.to_hex(), hex); |
| 155 | + } |
| 156 | + |
| 157 | + #[test] |
| 158 | + fn test_public_key_from_secret_key() { |
| 159 | + let sk = SecretKey::from_bytes(&[1u8; 32]).unwrap(); |
| 160 | + let pk = PublicKey::from_secret_key(&sk); |
| 161 | + assert_eq!(pk.to_bytes().len(), 33); |
| 162 | + assert!(pk.is_compressed()); |
| 163 | + } |
| 164 | + |
| 165 | + #[test] |
| 166 | + fn test_public_key_invalid() { |
| 167 | + assert!(PublicKey::from_bytes(&[0; 32]).is_err()); |
| 168 | + assert!(PublicKey::from_bytes(&[0; 34]).is_err()); |
| 169 | + assert!(PublicKey::from_bytes(&[0; 33]).is_err()); |
| 170 | + } |
| 171 | + |
| 172 | + #[test] |
| 173 | + fn test_public_key_roundtrip() { |
| 174 | + let hex = "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"; |
| 175 | + let pk = PublicKey::from_hex(hex).unwrap(); |
| 176 | + let pk2 = PublicKey::from_bytes(&pk.to_bytes()).unwrap(); |
| 177 | + assert_eq!(*pk, *pk2); |
| 178 | + } |
| 179 | + |
| 180 | + #[test] |
| 181 | + fn test_public_key_to_x_only() { |
| 182 | + let hex = "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"; |
| 183 | + let pk = PublicKey::from_hex(hex).unwrap(); |
| 184 | + let xonly = pk.to_x_only_public_key(); |
| 185 | + assert_eq!(xonly.to_string().len(), 64); |
| 186 | + assert_eq!( |
| 187 | + xonly.to_string(), |
| 188 | + "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798" |
| 189 | + ); |
| 190 | + } |
| 191 | + |
| 192 | + #[test] |
| 193 | + fn test_public_key_display() { |
| 194 | + let hex = "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"; |
| 195 | + let pk = PublicKey::from_hex(hex).unwrap(); |
| 196 | + assert_eq!(pk.to_string(), hex); |
| 197 | + } |
| 198 | + |
| 199 | + #[test] |
| 200 | + fn test_public_key_as_ref() { |
| 201 | + use elements::bitcoin::PublicKey as BitcoinPublicKey; |
| 202 | + let hex = "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"; |
| 203 | + let pk = PublicKey::from_hex(hex).unwrap(); |
| 204 | + let inner: &BitcoinPublicKey = (*pk).as_ref(); |
| 205 | + assert!(inner.compressed); |
| 206 | + } |
| 207 | + |
| 208 | + #[test] |
| 209 | + fn test_public_key_uncompressed() { |
| 210 | + let hex = "0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8"; |
| 211 | + let pk = PublicKey::from_hex(hex).unwrap(); |
| 212 | + assert!(!pk.is_compressed()); |
| 213 | + assert_eq!(pk.to_bytes().len(), 65); |
| 214 | + } |
| 215 | +} |
0 commit comments