|
| 1 | +use crate::bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey}; |
| 2 | +use crate::bitcoin::PrivateKey; |
| 3 | +use crate::error::WasmUtxoError; |
| 4 | +use wasm_bindgen::prelude::*; |
| 5 | + |
| 6 | +// Internal enum to hold either public-only or private+public keys |
| 7 | +#[derive(Debug, Clone)] |
| 8 | +enum ECPairKey { |
| 9 | + PublicOnly(PublicKey), |
| 10 | + Private { |
| 11 | + secret_key: SecretKey, |
| 12 | + public_key: PublicKey, |
| 13 | + }, |
| 14 | +} |
| 15 | + |
| 16 | +impl ECPairKey { |
| 17 | + fn public_key(&self) -> PublicKey { |
| 18 | + match self { |
| 19 | + ECPairKey::PublicOnly(pk) => *pk, |
| 20 | + ECPairKey::Private { public_key, .. } => *public_key, |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + fn secret_key(&self) -> Option<SecretKey> { |
| 25 | + match self { |
| 26 | + ECPairKey::PublicOnly(_) => None, |
| 27 | + ECPairKey::Private { secret_key, .. } => Some(*secret_key), |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/// WASM wrapper for elliptic curve key pairs (always uses compressed keys) |
| 33 | +#[wasm_bindgen] |
| 34 | +#[derive(Debug, Clone)] |
| 35 | +pub struct WasmECPair { |
| 36 | + key: ECPairKey, |
| 37 | +} |
| 38 | + |
| 39 | +impl WasmECPair { |
| 40 | + /// Get the public key as a secp256k1::PublicKey (for internal Rust use) |
| 41 | + pub(crate) fn get_public_key(&self) -> PublicKey { |
| 42 | + self.key.public_key() |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +#[wasm_bindgen] |
| 47 | +impl WasmECPair { |
| 48 | + /// Create an ECPair from a private key (always uses compressed keys) |
| 49 | + #[wasm_bindgen] |
| 50 | + pub fn from_private_key(private_key: &[u8]) -> Result<WasmECPair, WasmUtxoError> { |
| 51 | + if private_key.len() != 32 { |
| 52 | + return Err(WasmUtxoError::new("Private key must be 32 bytes")); |
| 53 | + } |
| 54 | + |
| 55 | + let secret_key = SecretKey::from_slice(private_key) |
| 56 | + .map_err(|e| WasmUtxoError::new(&format!("Invalid private key: {}", e)))?; |
| 57 | + |
| 58 | + let secp = Secp256k1::new(); |
| 59 | + let public_key = PublicKey::from_secret_key(&secp, &secret_key); |
| 60 | + |
| 61 | + Ok(WasmECPair { |
| 62 | + key: ECPairKey::Private { |
| 63 | + secret_key, |
| 64 | + public_key, |
| 65 | + }, |
| 66 | + }) |
| 67 | + } |
| 68 | + |
| 69 | + /// Create an ECPair from a public key (always uses compressed keys) |
| 70 | + #[wasm_bindgen] |
| 71 | + pub fn from_public_key(public_key: &[u8]) -> Result<WasmECPair, WasmUtxoError> { |
| 72 | + let public_key = PublicKey::from_slice(public_key) |
| 73 | + .map_err(|e| WasmUtxoError::new(&format!("Invalid public key: {}", e)))?; |
| 74 | + |
| 75 | + Ok(WasmECPair { |
| 76 | + key: ECPairKey::PublicOnly(public_key), |
| 77 | + }) |
| 78 | + } |
| 79 | + |
| 80 | + fn from_wif_with_network_check( |
| 81 | + wif_string: &str, |
| 82 | + expected_network: Option<crate::bitcoin::NetworkKind>, |
| 83 | + ) -> Result<WasmECPair, WasmUtxoError> { |
| 84 | + let private_key = PrivateKey::from_wif(wif_string) |
| 85 | + .map_err(|e| WasmUtxoError::new(&format!("Invalid WIF: {}", e)))?; |
| 86 | + |
| 87 | + if let Some(expected) = expected_network { |
| 88 | + if private_key.network != expected { |
| 89 | + let network_name = match expected { |
| 90 | + crate::bitcoin::NetworkKind::Main => "mainnet", |
| 91 | + crate::bitcoin::NetworkKind::Test => "testnet", |
| 92 | + }; |
| 93 | + return Err(WasmUtxoError::new(&format!( |
| 94 | + "Expected {} WIF", |
| 95 | + network_name |
| 96 | + ))); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + let secp = Secp256k1::new(); |
| 101 | + let secret_key = private_key.inner; |
| 102 | + let public_key = PublicKey::from_secret_key(&secp, &secret_key); |
| 103 | + |
| 104 | + Ok(WasmECPair { |
| 105 | + key: ECPairKey::Private { |
| 106 | + secret_key, |
| 107 | + public_key, |
| 108 | + }, |
| 109 | + }) |
| 110 | + } |
| 111 | + |
| 112 | + /// Create an ECPair from a WIF string (auto-detects network) |
| 113 | + #[wasm_bindgen] |
| 114 | + pub fn from_wif(wif_string: &str) -> Result<WasmECPair, WasmUtxoError> { |
| 115 | + Self::from_wif_with_network_check(wif_string, None) |
| 116 | + } |
| 117 | + |
| 118 | + /// Create an ECPair from a mainnet WIF string |
| 119 | + #[wasm_bindgen] |
| 120 | + pub fn from_wif_mainnet(wif_string: &str) -> Result<WasmECPair, WasmUtxoError> { |
| 121 | + use crate::bitcoin::NetworkKind; |
| 122 | + Self::from_wif_with_network_check(wif_string, Some(NetworkKind::Main)) |
| 123 | + } |
| 124 | + |
| 125 | + /// Create an ECPair from a testnet WIF string |
| 126 | + #[wasm_bindgen] |
| 127 | + pub fn from_wif_testnet(wif_string: &str) -> Result<WasmECPair, WasmUtxoError> { |
| 128 | + use crate::bitcoin::NetworkKind; |
| 129 | + Self::from_wif_with_network_check(wif_string, Some(NetworkKind::Test)) |
| 130 | + } |
| 131 | + |
| 132 | + /// Get the private key as a Uint8Array (if available) |
| 133 | + #[wasm_bindgen(getter)] |
| 134 | + pub fn private_key(&self) -> Option<js_sys::Uint8Array> { |
| 135 | + self.key |
| 136 | + .secret_key() |
| 137 | + .map(|sk| js_sys::Uint8Array::from(&sk.secret_bytes()[..])) |
| 138 | + } |
| 139 | + |
| 140 | + /// Get the compressed public key as a Uint8Array (always 33 bytes) |
| 141 | + #[wasm_bindgen(getter)] |
| 142 | + pub fn public_key(&self) -> js_sys::Uint8Array { |
| 143 | + let pk = self.key.public_key(); |
| 144 | + let bytes = pk.serialize(); |
| 145 | + js_sys::Uint8Array::from(&bytes[..]) |
| 146 | + } |
| 147 | + |
| 148 | + /// Convert to WIF string (mainnet) |
| 149 | + #[wasm_bindgen] |
| 150 | + pub fn to_wif(&self) -> Result<String, WasmUtxoError> { |
| 151 | + self.to_wif_mainnet() |
| 152 | + } |
| 153 | + |
| 154 | + /// Convert to mainnet WIF string |
| 155 | + #[wasm_bindgen] |
| 156 | + pub fn to_wif_mainnet(&self) -> Result<String, WasmUtxoError> { |
| 157 | + use crate::bitcoin::NetworkKind; |
| 158 | + self.to_wif_with_network(NetworkKind::Main) |
| 159 | + } |
| 160 | + |
| 161 | + /// Convert to testnet WIF string |
| 162 | + #[wasm_bindgen] |
| 163 | + pub fn to_wif_testnet(&self) -> Result<String, WasmUtxoError> { |
| 164 | + use crate::bitcoin::NetworkKind; |
| 165 | + self.to_wif_with_network(NetworkKind::Test) |
| 166 | + } |
| 167 | + |
| 168 | + fn to_wif_with_network( |
| 169 | + &self, |
| 170 | + network: crate::bitcoin::NetworkKind, |
| 171 | + ) -> Result<String, WasmUtxoError> { |
| 172 | + let secret_key = self |
| 173 | + .key |
| 174 | + .secret_key() |
| 175 | + .ok_or_else(|| WasmUtxoError::new("Cannot get WIF from public key"))?; |
| 176 | + |
| 177 | + let private_key = PrivateKey::new(secret_key, network); |
| 178 | + Ok(private_key.to_wif()) |
| 179 | + } |
| 180 | +} |
0 commit comments