use rand::rngs::OsRng;
use crate::{
builder::VerificationScript,
crypto::{
private_key_from_wif, wif_from_private_key, CryptoError, PublicKeyExtension,
Secp256r1PrivateKey, Secp256r1PublicKey,
},
neo_types::{ScriptHash, ScriptHashExtension},
};
#[derive(Debug, Clone)]
pub struct KeyPair {
pub private_key: Secp256r1PrivateKey,
pub public_key: Secp256r1PublicKey,
}
impl KeyPair {
pub fn new(private_key: Secp256r1PrivateKey, public_key: Secp256r1PublicKey) -> Self {
Self { private_key, public_key }
}
pub fn private_key(&self) -> Secp256r1PrivateKey {
self.private_key.clone()
}
pub fn public_key(&self) -> Secp256r1PublicKey {
self.public_key.clone()
}
pub fn from_secret_key(private_key: &Secp256r1PrivateKey) -> Self {
let public_key = private_key.clone().to_public_key();
Self::new(private_key.clone(), public_key)
}
pub fn private_key_bytes(&self) -> [u8; 32] {
self.private_key.to_raw_bytes()
}
pub fn public_key_bytes(&self) -> [u8; 64] {
let mut buf = [0u8; 64];
let vec_bytes: Vec<u8> = self.public_key.to_vec(); buf.copy_from_slice(&vec_bytes[0..64]);
buf
}
}
impl KeyPair {
pub fn new_random() -> Self {
let mut rng = OsRng; let secret_key = Secp256r1PrivateKey::random(&mut rng);
Self::from_secret_key(&secret_key)
}
pub fn from_private_key(private_key: &[u8; 32]) -> Result<Self, CryptoError> {
let secret_key = Secp256r1PrivateKey::from_bytes(private_key)?;
Ok(Self::from_secret_key(&secret_key))
}
pub fn from_wif(wif: &str) -> Result<Self, CryptoError> {
let private_key = private_key_from_wif(wif)?;
Ok(Self::from_secret_key(&private_key))
}
pub fn from_public_key(public_key: &[u8; 64]) -> Result<Self, CryptoError> {
let public_key = Secp256r1PublicKey::from_slice(public_key)?;
let secret_key = Secp256r1PrivateKey::from_bytes(&[0u8; 32]).unwrap(); Ok(Self::new(secret_key, public_key))
}
pub fn export_as_wif(&self) -> String {
wif_from_private_key(&self.private_key())
}
pub fn get_script_hash(&self) -> ScriptHash {
let vs = VerificationScript::from_public_key(&self.public_key());
vs.hash()
}
pub fn get_address(&self) -> String {
self.get_script_hash().to_address()
}
}
impl PartialEq for KeyPair {
fn eq(&self, other: &Self) -> bool {
self.private_key == other.private_key && self.public_key == other.public_key
}
}
#[cfg(test)]
mod tests {
use crate::{config::TestConstants, crypto::KeyPair, ScriptHash, ScriptHashExtension};
use hex;
#[test]
fn test_public_key_wif() {
let private_key =
hex::decode("c7134d6fd8e73d819e82755c64c93788d8db0961929e025a53363c4cc02a6962")
.unwrap();
let private_key_arr: &[u8; 32] = private_key.as_slice().try_into().unwrap();
let key_pair = KeyPair::from_private_key(private_key_arr).unwrap();
assert_eq!(
key_pair.export_as_wif(),
"L3tgppXLgdaeqSGSFw1Go3skBiy8vQAM7YMXvTHsKQtE16PBncSU"
);
}
#[test]
fn test_address() {
let private_key = hex::decode(TestConstants::DEFAULT_ACCOUNT_PRIVATE_KEY).unwrap();
let private_key_arr: &[u8; 32] = private_key.as_slice().try_into().unwrap();
let key_pair = KeyPair::from_private_key(private_key_arr).unwrap();
assert_eq!(key_pair.get_address(), TestConstants::DEFAULT_ACCOUNT_ADDRESS);
}
#[test]
fn test_script_hash() {
let private_key = hex::decode(TestConstants::DEFAULT_ACCOUNT_PRIVATE_KEY).unwrap();
let private_key_arr: &[u8; 32] = private_key.as_slice().try_into().unwrap();
let key_pair = KeyPair::from_private_key(private_key_arr).unwrap();
assert_eq!(
key_pair.get_script_hash(),
ScriptHash::from_hex(TestConstants::DEFAULT_ACCOUNT_SCRIPT_HASH).unwrap()
);
}
}