2018-11-16 08:04:46 -08:00
|
|
|
//! The `signature` module provides functionality for public, and private keys.
|
|
|
|
|
2018-12-14 20:39:10 -08:00
|
|
|
use crate::pubkey::Pubkey;
|
2018-11-16 08:04:46 -08:00
|
|
|
use bs58;
|
2019-04-18 14:37:20 -06:00
|
|
|
use ed25519_dalek;
|
2018-11-16 08:04:46 -08:00
|
|
|
use generic_array::typenum::U64;
|
|
|
|
use generic_array::GenericArray;
|
2019-04-18 14:37:20 -06:00
|
|
|
use rand::rngs::OsRng;
|
2018-11-16 08:04:46 -08:00
|
|
|
use serde_json;
|
|
|
|
use std::error;
|
|
|
|
use std::fmt;
|
2018-12-12 13:13:18 -08:00
|
|
|
use std::fs::{self, File};
|
|
|
|
use std::io::Write;
|
|
|
|
use std::path::Path;
|
2018-11-16 08:04:46 -08:00
|
|
|
|
2019-04-18 14:37:20 -06:00
|
|
|
pub type Keypair = ed25519_dalek::Keypair;
|
2018-11-16 08:04:46 -08:00
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
|
|
|
pub struct Signature(GenericArray<u8, U64>);
|
|
|
|
|
|
|
|
impl Signature {
|
|
|
|
pub fn new(signature_slice: &[u8]) -> Self {
|
2019-03-17 19:48:12 -07:00
|
|
|
Self(GenericArray::clone_from_slice(&signature_slice))
|
2018-11-16 08:04:46 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn verify(&self, pubkey_bytes: &[u8], message_bytes: &[u8]) -> bool {
|
2019-04-18 14:37:20 -06:00
|
|
|
let pubkey = ed25519_dalek::PublicKey::from_bytes(pubkey_bytes);
|
|
|
|
let signature = ed25519_dalek::Signature::from_bytes(self.0.as_slice());
|
|
|
|
if pubkey.is_err() || signature.is_err() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
pubkey
|
|
|
|
.unwrap()
|
|
|
|
.verify(message_bytes, &signature.unwrap())
|
|
|
|
.is_ok()
|
2018-11-16 08:04:46 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-01 12:00:30 -08:00
|
|
|
pub trait Signable {
|
|
|
|
fn sign(&mut self, keypair: &Keypair) {
|
|
|
|
let data = self.signable_data();
|
2019-01-25 23:41:20 -07:00
|
|
|
self.set_signature(keypair.sign_message(&data));
|
2018-12-01 12:00:30 -08:00
|
|
|
}
|
|
|
|
fn verify(&self) -> bool {
|
|
|
|
self.get_signature()
|
|
|
|
.verify(&self.pubkey().as_ref(), &self.signable_data())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pubkey(&self) -> Pubkey;
|
|
|
|
fn signable_data(&self) -> Vec<u8>;
|
|
|
|
fn get_signature(&self) -> Signature;
|
|
|
|
fn set_signature(&mut self, signature: Signature);
|
|
|
|
}
|
|
|
|
|
2018-11-16 08:04:46 -08:00
|
|
|
impl AsRef<[u8]> for Signature {
|
|
|
|
fn as_ref(&self) -> &[u8] {
|
|
|
|
&self.0[..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for Signature {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", bs58::encode(self.0).into_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Signature {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", bs58::encode(self.0).into_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait KeypairUtil {
|
|
|
|
fn new() -> Self;
|
|
|
|
fn pubkey(&self) -> Pubkey;
|
2019-01-25 23:41:20 -07:00
|
|
|
fn sign_message(&self, message: &[u8]) -> Signature;
|
2018-11-16 08:04:46 -08:00
|
|
|
}
|
|
|
|
|
2019-04-18 14:37:20 -06:00
|
|
|
impl KeypairUtil for Keypair {
|
2018-11-16 08:04:46 -08:00
|
|
|
/// Return a new ED25519 keypair
|
|
|
|
fn new() -> Self {
|
2019-04-18 14:37:20 -06:00
|
|
|
let mut rng = OsRng::new().unwrap();
|
|
|
|
Keypair::generate(&mut rng)
|
2018-11-16 08:04:46 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the public key for the given keypair
|
|
|
|
fn pubkey(&self) -> Pubkey {
|
2019-04-18 14:37:20 -06:00
|
|
|
Pubkey::new(&self.public.as_ref())
|
2018-11-16 08:04:46 -08:00
|
|
|
}
|
2019-01-25 23:41:20 -07:00
|
|
|
|
|
|
|
fn sign_message(&self, message: &[u8]) -> Signature {
|
2019-04-18 14:37:20 -06:00
|
|
|
Signature::new(&self.sign(message).to_bytes())
|
2019-01-25 23:41:20 -07:00
|
|
|
}
|
2018-11-16 08:04:46 -08:00
|
|
|
}
|
|
|
|
|
2019-04-18 11:47:34 -06:00
|
|
|
pub fn read_keypair(path: &str) -> Result<Keypair, Box<error::Error>> {
|
2019-04-18 14:37:20 -06:00
|
|
|
let file = File::open(path.to_string())?;
|
|
|
|
let bytes: Vec<u8> = serde_json::from_reader(file)?;
|
|
|
|
let keypair = Keypair::from_bytes(&bytes).unwrap();
|
2018-11-16 08:04:46 -08:00
|
|
|
Ok(keypair)
|
|
|
|
}
|
2018-12-12 13:13:18 -08:00
|
|
|
|
|
|
|
pub fn gen_keypair_file(outfile: String) -> Result<String, Box<error::Error>> {
|
2019-04-18 14:37:20 -06:00
|
|
|
let keypair_bytes = Keypair::new().to_bytes();
|
|
|
|
let serialized = serde_json::to_string(&keypair_bytes.to_vec())?;
|
2018-12-12 13:13:18 -08:00
|
|
|
|
|
|
|
if outfile != "-" {
|
|
|
|
if let Some(outdir) = Path::new(&outfile).parent() {
|
|
|
|
fs::create_dir_all(outdir)?;
|
|
|
|
}
|
|
|
|
let mut f = File::create(outfile)?;
|
|
|
|
f.write_all(&serialized.clone().into_bytes())?;
|
|
|
|
}
|
|
|
|
Ok(serialized)
|
|
|
|
}
|
2019-04-18 14:37:20 -06:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use std::mem;
|
|
|
|
|
|
|
|
fn tmp_file_path(name: &str) -> String {
|
|
|
|
use std::env;
|
|
|
|
let out_dir = env::var("OUT_DIR").unwrap_or_else(|_| "target".to_string());
|
|
|
|
let keypair = Keypair::new();
|
|
|
|
|
|
|
|
format!("{}/tmp/{}-{}", out_dir, name, keypair.pubkey()).to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_gen_keypair_file() {
|
|
|
|
let outfile = tmp_file_path("test_gen_keypair_file.json");
|
|
|
|
let serialized_keypair = gen_keypair_file(outfile.to_string()).unwrap();
|
|
|
|
let keypair_vec: Vec<u8> = serde_json::from_str(&serialized_keypair).unwrap();
|
|
|
|
assert!(Path::new(&outfile).exists());
|
|
|
|
assert_eq!(
|
|
|
|
keypair_vec,
|
|
|
|
read_keypair(&outfile).unwrap().to_bytes().to_vec()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
read_keypair(&outfile).unwrap().pubkey().as_ref().len(),
|
|
|
|
mem::size_of::<Pubkey>()
|
|
|
|
);
|
|
|
|
fs::remove_file(&outfile).unwrap();
|
|
|
|
assert!(!Path::new(&outfile).exists());
|
|
|
|
}
|
|
|
|
}
|