#![cfg(feature = "full")]
use {
crate::pubkey::Pubkey,
std::borrow::{Borrow, Cow},
};
pub use {
crate::signer::{keypair::*, null_signer::*, presigner::*, *},
solana_signature::{ParseSignatureError, Signature, SIGNATURE_BYTES},
};
pub trait Signable {
fn sign(&mut self, keypair: &Keypair) {
let signature = keypair.sign_message(self.signable_data().borrow());
self.set_signature(signature);
}
fn verify(&self) -> bool {
self.get_signature()
.verify(self.pubkey().as_ref(), self.signable_data().borrow())
}
fn pubkey(&self) -> Pubkey;
fn signable_data(&self) -> Cow<[u8]>;
fn get_signature(&self) -> Signature;
fn set_signature(&mut self, signature: Signature);
}
#[cfg(test)]
mod tests {
use {super::*, solana_sdk::signer::keypair::Keypair};
#[test]
fn test_signature_fromstr() {
let signature = Keypair::new().sign_message(&[0u8]);
let mut signature_base58_str = bs58::encode(signature).into_string();
assert_eq!(signature_base58_str.parse::<Signature>(), Ok(signature));
signature_base58_str.push_str(&bs58::encode(<[u8; 64]>::from(signature)).into_string());
assert_eq!(
signature_base58_str.parse::<Signature>(),
Err(ParseSignatureError::WrongSize)
);
signature_base58_str.truncate(signature_base58_str.len() / 2);
assert_eq!(signature_base58_str.parse::<Signature>(), Ok(signature));
signature_base58_str.truncate(signature_base58_str.len() / 2);
assert_eq!(
signature_base58_str.parse::<Signature>(),
Err(ParseSignatureError::WrongSize)
);
let mut signature_base58_str = bs58::encode(<[u8; 64]>::from(signature)).into_string();
assert_eq!(signature_base58_str.parse::<Signature>(), Ok(signature));
signature_base58_str.replace_range(..1, "I");
assert_eq!(
signature_base58_str.parse::<Signature>(),
Err(ParseSignatureError::Invalid)
);
let mut too_long = bs58::encode(&[255u8; SIGNATURE_BYTES]).into_string();
too_long.push('1');
assert_eq!(
too_long.parse::<Signature>(),
Err(ParseSignatureError::WrongSize)
);
}
}