Add signature module
Because things other than transactions can be signed.
This commit is contained in:
@ -4,7 +4,8 @@
|
|||||||
|
|
||||||
use log::{Entry, Sha256Hash};
|
use log::{Entry, Sha256Hash};
|
||||||
use event::Event;
|
use event::Event;
|
||||||
use transaction::{get_pubkey, sign_transaction_data, PublicKey, Signature, Transaction};
|
use transaction::{sign_transaction_data, Transaction};
|
||||||
|
use signature::{get_pubkey, PublicKey, Signature};
|
||||||
use genesis::Genesis;
|
use genesis::Genesis;
|
||||||
use historian::{reserve_signature, Historian};
|
use historian::{reserve_signature, Historian};
|
||||||
use ring::signature::Ed25519KeyPair;
|
use ring::signature::Ed25519KeyPair;
|
||||||
@ -157,7 +158,7 @@ impl Accountant {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use transaction::{generate_keypair, get_pubkey};
|
use signature::{generate_keypair, get_pubkey};
|
||||||
use logger::ExitReason;
|
use logger::ExitReason;
|
||||||
use genesis::Creator;
|
use genesis::Creator;
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
use std::io;
|
use std::io;
|
||||||
use accountant::Accountant;
|
use accountant::Accountant;
|
||||||
use transaction::{PublicKey, Transaction};
|
use transaction::Transaction;
|
||||||
|
use signature::PublicKey;
|
||||||
use log::{Entry, Sha256Hash};
|
use log::{Entry, Sha256Hash};
|
||||||
use std::net::UdpSocket;
|
use std::net::UdpSocket;
|
||||||
use bincode::{deserialize, serialize};
|
use bincode::{deserialize, serialize};
|
||||||
|
@ -5,7 +5,8 @@
|
|||||||
use std::net::UdpSocket;
|
use std::net::UdpSocket;
|
||||||
use std::io;
|
use std::io;
|
||||||
use bincode::{deserialize, serialize};
|
use bincode::{deserialize, serialize};
|
||||||
use transaction::{get_pubkey, sign_transaction_data, PublicKey, Signature, Transaction};
|
use transaction::{sign_transaction_data, Transaction};
|
||||||
|
use signature::{get_pubkey, PublicKey, Signature};
|
||||||
use log::{Entry, Sha256Hash};
|
use log::{Entry, Sha256Hash};
|
||||||
use ring::signature::Ed25519KeyPair;
|
use ring::signature::Ed25519KeyPair;
|
||||||
use accountant_skel::{Request, Response};
|
use accountant_skel::{Request, Response};
|
||||||
|
@ -3,7 +3,8 @@ extern crate silk;
|
|||||||
|
|
||||||
use silk::accountant_stub::AccountantStub;
|
use silk::accountant_stub::AccountantStub;
|
||||||
use silk::event::Event;
|
use silk::event::Event;
|
||||||
use silk::transaction::{generate_keypair, get_pubkey, sign_transaction_data, Transaction};
|
use silk::signature::{generate_keypair, get_pubkey};
|
||||||
|
use silk::transaction::{sign_transaction_data, Transaction};
|
||||||
use silk::genesis::Genesis;
|
use silk::genesis::Genesis;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
use std::net::UdpSocket;
|
use std::net::UdpSocket;
|
||||||
|
@ -2,7 +2,8 @@ extern crate silk;
|
|||||||
|
|
||||||
use silk::historian::Historian;
|
use silk::historian::Historian;
|
||||||
use silk::log::{verify_slice, Entry, Sha256Hash};
|
use silk::log::{verify_slice, Entry, Sha256Hash};
|
||||||
use silk::transaction::{generate_keypair, get_pubkey, sign_claim_data};
|
use silk::signature::{generate_keypair, get_pubkey};
|
||||||
|
use silk::transaction::sign_claim_data;
|
||||||
use silk::event::Event;
|
use silk::event::Event;
|
||||||
use std::thread::sleep;
|
use std::thread::sleep;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
@ -2,7 +2,7 @@ extern crate serde_json;
|
|||||||
extern crate silk;
|
extern crate silk;
|
||||||
|
|
||||||
use silk::genesis::{Creator, Genesis};
|
use silk::genesis::{Creator, Genesis};
|
||||||
use silk::transaction::{generate_keypair, get_pubkey};
|
use silk::signature::{generate_keypair, get_pubkey};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let alice = Creator {
|
let alice = Creator {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
//! The `event` crate provides the data structures for log events.
|
//! The `event` crate provides the data structures for log events.
|
||||||
|
|
||||||
use transaction::{PublicKey, Signature, Transaction};
|
use signature::{PublicKey, Signature};
|
||||||
|
use transaction::Transaction;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use log::Sha256Hash;
|
use log::Sha256Hash;
|
||||||
|
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
//! A library for generating the chain's genesis block.
|
//! A library for generating the chain's genesis block.
|
||||||
|
|
||||||
use event::Event;
|
use event::Event;
|
||||||
use transaction::{generate_keypair, get_pubkey, sign_transaction_data, PublicKey, Transaction};
|
use transaction::{sign_transaction_data, Transaction};
|
||||||
|
use signature::{generate_keypair, get_pubkey, PublicKey};
|
||||||
use log::{create_entries, hash, Entry, Sha256Hash};
|
use log::{create_entries, hash, Entry, Sha256Hash};
|
||||||
use ring::rand::SystemRandom;
|
use ring::rand::SystemRandom;
|
||||||
use ring::signature::Ed25519KeyPair;
|
use ring::signature::Ed25519KeyPair;
|
||||||
|
@ -7,7 +7,7 @@ use std::sync::mpsc::{sync_channel, Receiver, SyncSender};
|
|||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
use log::{hash, Entry, Sha256Hash};
|
use log::{hash, Entry, Sha256Hash};
|
||||||
use logger::{ExitReason, Logger};
|
use logger::{ExitReason, Logger};
|
||||||
use transaction::Signature;
|
use signature::Signature;
|
||||||
use event::Event;
|
use event::Event;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
@ -70,6 +70,7 @@ mod tests {
|
|||||||
use log::*;
|
use log::*;
|
||||||
use event::*;
|
use event::*;
|
||||||
use transaction::*;
|
use transaction::*;
|
||||||
|
use signature::*;
|
||||||
use std::thread::sleep;
|
use std::thread::sleep;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ pub mod log;
|
|||||||
pub mod logger;
|
pub mod logger;
|
||||||
pub mod event;
|
pub mod event;
|
||||||
pub mod transaction;
|
pub mod transaction;
|
||||||
|
pub mod signature;
|
||||||
pub mod genesis;
|
pub mod genesis;
|
||||||
pub mod historian;
|
pub mod historian;
|
||||||
pub mod accountant;
|
pub mod accountant;
|
||||||
|
@ -169,8 +169,8 @@ pub fn next_ticks(start_hash: &Sha256Hash, num_hashes: u64, len: usize) -> Vec<E
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use transaction::{generate_keypair, get_pubkey, sign_claim_data, sign_transaction_data,
|
use signature::{generate_keypair, get_pubkey};
|
||||||
Transaction};
|
use transaction::{sign_claim_data, sign_transaction_data, Transaction};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_event_verify() {
|
fn test_event_verify() {
|
||||||
|
@ -80,6 +80,7 @@ mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
use log::*;
|
use log::*;
|
||||||
use event::*;
|
use event::*;
|
||||||
|
use signature::*;
|
||||||
use transaction::*;
|
use transaction::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
30
src/signature.rs
Normal file
30
src/signature.rs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
//! The `signature` crate provides functionality for public and private keys
|
||||||
|
|
||||||
|
use generic_array::GenericArray;
|
||||||
|
use generic_array::typenum::{U32, U64};
|
||||||
|
use ring::signature::Ed25519KeyPair;
|
||||||
|
use ring::{rand, signature};
|
||||||
|
use untrusted;
|
||||||
|
|
||||||
|
pub type PublicKey = GenericArray<u8, U32>;
|
||||||
|
pub type Signature = GenericArray<u8, U64>;
|
||||||
|
|
||||||
|
/// Return a new ED25519 keypair
|
||||||
|
pub fn generate_keypair() -> Ed25519KeyPair {
|
||||||
|
let rng = rand::SystemRandom::new();
|
||||||
|
let pkcs8_bytes = signature::Ed25519KeyPair::generate_pkcs8(&rng).unwrap();
|
||||||
|
signature::Ed25519KeyPair::from_pkcs8(untrusted::Input::from(&pkcs8_bytes)).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return the public key for the given keypair
|
||||||
|
pub fn get_pubkey(keypair: &Ed25519KeyPair) -> PublicKey {
|
||||||
|
GenericArray::clone_from_slice(keypair.public_key_bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Verify a signed message with the given public key.
|
||||||
|
pub fn verify_signature(peer_public_key_bytes: &[u8], msg_bytes: &[u8], sig_bytes: &[u8]) -> bool {
|
||||||
|
let peer_public_key = untrusted::Input::from(peer_public_key_bytes);
|
||||||
|
let msg = untrusted::Input::from(msg_bytes);
|
||||||
|
let sig = untrusted::Input::from(sig_bytes);
|
||||||
|
signature::verify(&signature::ED25519, peer_public_key, msg, sig).is_ok()
|
||||||
|
}
|
@ -1,17 +1,11 @@
|
|||||||
//! The `transaction` crate provides functionality for creating log transactions.
|
//! The `transaction` crate provides functionality for creating log transactions.
|
||||||
|
|
||||||
use generic_array::GenericArray;
|
use signature::{get_pubkey, verify_signature, PublicKey, Signature};
|
||||||
use generic_array::typenum::{U32, U64};
|
|
||||||
use ring::signature::Ed25519KeyPair;
|
use ring::signature::Ed25519KeyPair;
|
||||||
use ring::{rand, signature};
|
|
||||||
use untrusted;
|
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use bincode::serialize;
|
use bincode::serialize;
|
||||||
use log::Sha256Hash;
|
use log::Sha256Hash;
|
||||||
|
|
||||||
pub type PublicKey = GenericArray<u8, U32>;
|
|
||||||
pub type Signature = GenericArray<u8, U64>;
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
||||||
pub struct Transaction<T> {
|
pub struct Transaction<T> {
|
||||||
pub from: PublicKey,
|
pub from: PublicKey,
|
||||||
@ -38,22 +32,9 @@ impl<T: Serialize> Transaction<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return a new ED25519 keypair
|
|
||||||
pub fn generate_keypair() -> Ed25519KeyPair {
|
|
||||||
let rng = rand::SystemRandom::new();
|
|
||||||
let pkcs8_bytes = signature::Ed25519KeyPair::generate_pkcs8(&rng).unwrap();
|
|
||||||
signature::Ed25519KeyPair::from_pkcs8(untrusted::Input::from(&pkcs8_bytes)).unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Return the public key for the given keypair
|
|
||||||
pub fn get_pubkey(keypair: &Ed25519KeyPair) -> PublicKey {
|
|
||||||
GenericArray::clone_from_slice(keypair.public_key_bytes())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Return a signature for the given data using the private key from the given keypair.
|
|
||||||
fn sign_serialized<T: Serialize>(data: &T, keypair: &Ed25519KeyPair) -> Signature {
|
fn sign_serialized<T: Serialize>(data: &T, keypair: &Ed25519KeyPair) -> Signature {
|
||||||
let serialized = serialize(data).unwrap();
|
let serialized = serialize(data).unwrap();
|
||||||
GenericArray::clone_from_slice(keypair.sign(&serialized).as_ref())
|
Signature::clone_from_slice(keypair.sign(&serialized).as_ref())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return a signature for the given transaction data using the private key from the given keypair.
|
/// Return a signature for the given transaction data using the private key from the given keypair.
|
||||||
@ -76,14 +57,6 @@ pub fn sign_claim_data<T: Serialize>(
|
|||||||
sign_transaction_data(data, keypair, &get_pubkey(keypair), last_id)
|
sign_transaction_data(data, keypair, &get_pubkey(keypair), last_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Verify a signed message with the given public key.
|
|
||||||
pub fn verify_signature(peer_public_key_bytes: &[u8], msg_bytes: &[u8], sig_bytes: &[u8]) -> bool {
|
|
||||||
let peer_public_key = untrusted::Input::from(peer_public_key_bytes);
|
|
||||||
let msg = untrusted::Input::from(msg_bytes);
|
|
||||||
let sig = untrusted::Input::from(sig_bytes);
|
|
||||||
signature::verify(&signature::ED25519, peer_public_key, msg, sig).is_ok()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
Reference in New Issue
Block a user