use async_trait::async_trait;
use crate::error::{Result, ZeraError};
#[async_trait]
pub trait ZeraSigner: Send + Sync {
fn public_key(&self) -> &str;
async fn sign(&self, data: &[u8]) -> Result<Vec<u8>>;
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KeyPairSigner {
public_key: String,
private_key: String,
}
impl KeyPairSigner {
pub fn new(public_key: impl Into<String>, private_key: impl Into<String>) -> Result<Self> {
let public_key = public_key.into();
let private_key = private_key.into();
if public_key.is_empty() {
return Err(ZeraError::InvalidInput("publicKey is required".to_string()));
}
if private_key.is_empty() {
return Err(ZeraError::InvalidInput(
"privateKey is required".to_string(),
));
}
Ok(Self {
public_key,
private_key,
})
}
}
#[async_trait]
impl ZeraSigner for KeyPairSigner {
fn public_key(&self) -> &str {
&self.public_key
}
async fn sign(&self, data: &[u8]) -> Result<Vec<u8>> {
sign_transaction_data(data, &self.private_key, &self.public_key)
}
}
pub fn sign_transaction_data(
data: &[u8],
private_key_base58: &str,
public_key_identifier: &str,
) -> Result<Vec<u8>> {
crate::crypto::signature::sign_transaction_data(data, private_key_base58, public_key_identifier)
}
pub fn create_transaction_hash(data: &[u8]) -> Vec<u8> {
crate::crypto::signature::create_transaction_hash(data)
}