rings-core 0.20.0

Chord DHT implementation with ICE
Documentation
//! Default method signing using secp256k1 ECDSA.

use crate::ecc::keccak256;
use crate::ecc::PublicKey;
use crate::ecc::PublicKeyAddress;
use crate::ecc::SecretKey;
use crate::error::Result;

/// sign function passing raw message parameter.
pub fn sign_raw(sec: SecretKey, msg: &[u8]) -> [u8; 65] {
    sign(sec, &hash(msg))
}

/// sign function with `hash` data.
pub fn sign(sec: SecretKey, hash: &[u8; 32]) -> [u8; 65] {
    sec.sign_hash(hash)
}

/// generate hash data from message.
pub fn hash(msg: &[u8]) -> [u8; 32] {
    keccak256(msg)
}

/// recover public key from message and signature.
pub fn recover(msg: &[u8], sig: impl AsRef<[u8]>) -> Result<PublicKey<33>> {
    let sig_byte: [u8; 65] = sig.as_ref().try_into()?;
    crate::ecc::recover(msg, sig_byte)
}

/// verify signature with message and address.
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_secp256k1;