Add signature module
Because things other than transactions can be signed.
This commit is contained in:
parent
5d0356f74b
commit
624c151ca2
|
@ -4,7 +4,8 @@
|
|||
|
||||
use log::{Entry, Sha256Hash};
|
||||
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 historian::{reserve_signature, Historian};
|
||||
use ring::signature::Ed25519KeyPair;
|
||||
|
@ -157,7 +158,7 @@ impl Accountant {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use transaction::{generate_keypair, get_pubkey};
|
||||
use signature::{generate_keypair, get_pubkey};
|
||||
use logger::ExitReason;
|
||||
use genesis::Creator;
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use std::io;
|
||||
use accountant::Accountant;
|
||||
use transaction::{PublicKey, Transaction};
|
||||
use transaction::Transaction;
|
||||
use signature::PublicKey;
|
||||
use log::{Entry, Sha256Hash};
|
||||
use std::net::UdpSocket;
|
||||
use bincode::{deserialize, serialize};
|
||||
|
|
|
@ -5,7 +5,8 @@
|
|||
use std::net::UdpSocket;
|
||||
use std::io;
|
||||
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 ring::signature::Ed25519KeyPair;
|
||||
use accountant_skel::{Request, Response};
|
||||
|
|
|
@ -3,7 +3,8 @@ extern crate silk;
|
|||
|
||||
use silk::accountant_stub::AccountantStub;
|
||||
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 std::time::Instant;
|
||||
use std::net::UdpSocket;
|
||||
|
|
|
@ -2,7 +2,8 @@ extern crate silk;
|
|||
|
||||
use silk::historian::Historian;
|
||||
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 std::thread::sleep;
|
||||
use std::time::Duration;
|
||||
|
|
|
@ -2,7 +2,7 @@ extern crate serde_json;
|
|||
extern crate silk;
|
||||
|
||||
use silk::genesis::{Creator, Genesis};
|
||||
use silk::transaction::{generate_keypair, get_pubkey};
|
||||
use silk::signature::{generate_keypair, get_pubkey};
|
||||
|
||||
fn main() {
|
||||
let alice = Creator {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
//! 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 log::Sha256Hash;
|
||||
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
//! A library for generating the chain's genesis block.
|
||||
|
||||
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 ring::rand::SystemRandom;
|
||||
use ring::signature::Ed25519KeyPair;
|
||||
|
|
|
@ -7,7 +7,7 @@ use std::sync::mpsc::{sync_channel, Receiver, SyncSender};
|
|||
use std::time::Instant;
|
||||
use log::{hash, Entry, Sha256Hash};
|
||||
use logger::{ExitReason, Logger};
|
||||
use transaction::Signature;
|
||||
use signature::Signature;
|
||||
use event::Event;
|
||||
use serde::Serialize;
|
||||
use std::fmt::Debug;
|
||||
|
@ -70,6 +70,7 @@ mod tests {
|
|||
use log::*;
|
||||
use event::*;
|
||||
use transaction::*;
|
||||
use signature::*;
|
||||
use std::thread::sleep;
|
||||
use std::time::Duration;
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ pub mod log;
|
|||
pub mod logger;
|
||||
pub mod event;
|
||||
pub mod transaction;
|
||||
pub mod signature;
|
||||
pub mod genesis;
|
||||
pub mod historian;
|
||||
pub mod accountant;
|
||||
|
|
|
@ -169,8 +169,8 @@ pub fn next_ticks(start_hash: &Sha256Hash, num_hashes: u64, len: usize) -> Vec<E
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use transaction::{generate_keypair, get_pubkey, sign_claim_data, sign_transaction_data,
|
||||
Transaction};
|
||||
use signature::{generate_keypair, get_pubkey};
|
||||
use transaction::{sign_claim_data, sign_transaction_data, Transaction};
|
||||
|
||||
#[test]
|
||||
fn test_event_verify() {
|
||||
|
|
|
@ -80,6 +80,7 @@ mod tests {
|
|||
use super::*;
|
||||
use log::*;
|
||||
use event::*;
|
||||
use signature::*;
|
||||
use transaction::*;
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -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.
|
||||
|
||||
use generic_array::GenericArray;
|
||||
use generic_array::typenum::{U32, U64};
|
||||
use signature::{get_pubkey, verify_signature, PublicKey, Signature};
|
||||
use ring::signature::Ed25519KeyPair;
|
||||
use ring::{rand, signature};
|
||||
use untrusted;
|
||||
use serde::Serialize;
|
||||
use bincode::serialize;
|
||||
use log::Sha256Hash;
|
||||
|
||||
pub type PublicKey = GenericArray<u8, U32>;
|
||||
pub type Signature = GenericArray<u8, U64>;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
||||
pub struct Transaction<T> {
|
||||
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 {
|
||||
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.
|
||||
|
@ -76,14 +57,6 @@ pub fn sign_claim_data<T: Serialize>(
|
|||
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)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
Loading…
Reference in New Issue