use crate::ecc::keccak256;
use crate::ecc::PublicKey;
use crate::ecc::PublicKeyAddress;
use crate::ecc::SecretKey;
use crate::error::Result;
pub fn sign_raw(sec: SecretKey, msg: &[u8]) -> [u8; 65] {
sign(sec, &hash(msg))
}
pub fn sign(sec: SecretKey, hash: &[u8; 32]) -> [u8; 65] {
sec.sign_hash(hash)
}
pub fn hash(msg: &[u8]) -> [u8; 32] {
keccak256(msg)
}
pub fn recover(msg: &[u8], sig: impl AsRef<[u8]>) -> Result<PublicKey> {
let sig_byte: [u8; 65] = sig.as_ref().try_into()?;
crate::ecc::recover(msg, sig_byte)
}
pub fn verify(msg: &[u8], address: &PublicKeyAddress, sig: impl AsRef<[u8]>) -> bool {
if let Ok(p) = recover(msg, sig) {
p.address() == *address
} else {
false
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::ecc::SecretKey;
#[test]
fn test_default_sign() {
let key =
SecretKey::try_from("65860affb4b570dba06db294aa7c676f68e04a5bf2721243ad3cbc05a79c68c0")
.unwrap();
let msg = "hello";
let h = self::hash(msg.as_bytes());
let sig = self::sign(key, &h);
assert_eq!(sig, key.sign(msg));
}
}