use async_trait::async_trait;
use pyde_crypto::falcon::{falcon_keygen, falcon_keygen_deterministic, falcon_sign};
use crate::error::SdkError;
use crate::tx::tx_hash;
use crate::types::{Address, FalconPubkey, FalconSecret, FalconSignature, Tx, TxHash};
#[async_trait]
pub trait Signer: Send + Sync {
fn address(&self) -> Address;
fn pubkey(&self) -> FalconPubkey;
async fn sign_hash(&self, hash: &TxHash) -> Result<FalconSignature, SdkError>;
async fn sign_tx(&self, tx: &mut Tx) -> Result<(), SdkError> {
let hash = tx_hash(tx);
let sig = self.sign_hash(&hash).await?;
tx.signature = sig;
Ok(())
}
}
pub struct LocalSigner {
address: Address,
pubkey: FalconPubkey,
secret: FalconSecret,
}
impl LocalSigner {
pub fn random() -> Result<Self, SdkError> {
let (crypto_pk, crypto_sk) =
falcon_keygen().map_err(|e| SdkError::Signing(format!("FALCON keygen failed: {e}")))?;
let pubkey: FalconPubkey = (&crypto_pk).into();
let secret: FalconSecret = (&crypto_sk).into();
let address = Address::from_pubkey(&pubkey);
Ok(Self {
address,
pubkey,
secret,
})
}
pub fn from_seed(seed: &[u8; 32]) -> Result<Self, SdkError> {
let (crypto_pk, crypto_sk) = falcon_keygen_deterministic(seed)
.map_err(|e| SdkError::Signing(format!("FALCON deterministic keygen failed: {e}")))?;
let pubkey: FalconPubkey = (&crypto_pk).into();
let secret: FalconSecret = (&crypto_sk).into();
let address = Address::from_pubkey(&pubkey);
Ok(Self {
address,
pubkey,
secret,
})
}
pub fn from_secret(secret: FalconSecret) -> Result<Self, SdkError> {
Self::from_keys(
FalconPubkey::new([0u8; crate::types::FALCON_PUBKEY_LEN]),
secret,
)
}
pub fn from_keys(pubkey: FalconPubkey, secret: FalconSecret) -> Result<Self, SdkError> {
let address = Address::from_pubkey(&pubkey);
let signer = Self {
address,
pubkey,
secret,
};
signer.validate_pair()?;
Ok(signer)
}
fn validate_pair(&self) -> Result<(), SdkError> {
if self.pubkey.as_bytes() == &[0u8; crate::types::FALCON_PUBKEY_LEN] {
return Err(SdkError::Signing(
"from_secret requires the matching pubkey — use from_keys instead".into(),
));
}
let probe = [0u8; 32];
let crypto_sk: pyde_crypto::falcon::FalconSecretKey = (&self.secret).into();
let sig = falcon_sign(&crypto_sk, &probe)
.map_err(|e| SdkError::Signing(format!("probe sign failed: {e}")))?;
let crypto_pk: pyde_crypto::falcon::FalconPublicKey = (&self.pubkey).into();
if !pyde_crypto::falcon::falcon_verify(&crypto_pk, &probe, &sig) {
return Err(SdkError::Signing("pubkey does not match secret".into()));
}
Ok(())
}
pub(crate) fn secret(&self) -> &FalconSecret {
&self.secret
}
}
#[async_trait]
impl Signer for LocalSigner {
fn address(&self) -> Address {
self.address
}
fn pubkey(&self) -> FalconPubkey {
self.pubkey
}
async fn sign_hash(&self, hash: &TxHash) -> Result<FalconSignature, SdkError> {
let crypto_sk: pyde_crypto::falcon::FalconSecretKey = (&self.secret).into();
let crypto_sig = falcon_sign(&crypto_sk, hash.as_bytes())
.map_err(|e| SdkError::Signing(format!("FALCON sign failed: {e}")))?;
Ok((&crypto_sig).into())
}
}
impl std::fmt::Debug for LocalSigner {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("LocalSigner")
.field("address", &self.address)
.field("pubkey", &self.pubkey)
.field("secret", &"<redacted>")
.finish()
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
use crate::tx::TxBuilder;
use crate::types::TxType;
#[tokio::test]
async fn random_signer_signs_and_verifies() {
let signer = LocalSigner::random().unwrap();
let hash = TxHash::new([0xCD; 32]);
let sig = signer.sign_hash(&hash).await.unwrap();
let crypto_pk: pyde_crypto::falcon::FalconPublicKey = (&signer.pubkey()).into();
let crypto_sig = pyde_crypto::falcon::FalconSignature::from_bytes(sig.as_bytes()).unwrap();
assert!(pyde_crypto::falcon::falcon_verify(
&crypto_pk,
hash.as_bytes(),
&crypto_sig
));
}
#[tokio::test]
async fn deterministic_signer_is_reproducible() {
let seed = [0x42; 32];
let a = LocalSigner::from_seed(&seed).unwrap();
let b = LocalSigner::from_seed(&seed).unwrap();
assert_eq!(a.address(), b.address());
assert_eq!(a.pubkey(), b.pubkey());
}
#[tokio::test]
async fn different_signers_have_different_addresses() {
let a = LocalSigner::random().unwrap();
let b = LocalSigner::random().unwrap();
assert_ne!(a.address(), b.address());
}
#[tokio::test]
async fn falcon_sigs_are_randomised_but_both_verify() {
let signer = LocalSigner::random().unwrap();
let hash = TxHash::new([0x77; 32]);
let s1 = signer.sign_hash(&hash).await.unwrap();
let s2 = signer.sign_hash(&hash).await.unwrap();
assert_ne!(s1, s2);
let crypto_pk: pyde_crypto::falcon::FalconPublicKey = (&signer.pubkey()).into();
for sig in [s1, s2] {
let crypto_sig =
pyde_crypto::falcon::FalconSignature::from_bytes(sig.as_bytes()).unwrap();
assert!(pyde_crypto::falcon::falcon_verify(
&crypto_pk,
hash.as_bytes(),
&crypto_sig
));
}
}
#[tokio::test]
async fn sign_tx_populates_signature_and_keeps_hash_stable() {
let signer = LocalSigner::random().unwrap();
let mut tx = TxBuilder::new()
.from(signer.address())
.transfer(Address::new([0x55; 32]), 1_000)
.build()
.unwrap();
let pre = tx_hash(&tx);
signer.sign_tx(&mut tx).await.unwrap();
let post = tx_hash(&tx);
assert_eq!(pre, post);
assert!(!tx.signature.is_empty());
assert_eq!(tx.tx_type, TxType::Standard);
}
#[tokio::test]
async fn from_keys_rejects_mismatched_pubkey() {
let a = LocalSigner::random().unwrap();
let b = LocalSigner::random().unwrap();
let mixed = LocalSigner::from_keys(a.pubkey(), b.secret().clone());
assert!(mixed.is_err());
}
#[test]
fn debug_redacts_secret() {
let signer = LocalSigner::random().unwrap();
let dbg = format!("{signer:?}");
assert!(dbg.contains("redacted"));
}
}