Sign Claim's 'to' field
Otherwise, the accountant will treat deposits of the same amount as duplicates.
This commit is contained in:
parent
a0410c4677
commit
0d0fee1ca1
|
@ -69,9 +69,9 @@ impl Accountant {
|
|||
}
|
||||
|
||||
pub fn deposit(self: &mut Self, n: u64, keypair: &Ed25519KeyPair) -> Result<Signature> {
|
||||
use event::{get_pubkey, sign_serialized};
|
||||
use event::{get_pubkey, sign_claim_data};
|
||||
let key = get_pubkey(keypair);
|
||||
let sig = sign_serialized(&n, keypair);
|
||||
let sig = sign_claim_data(&n, keypair);
|
||||
self.deposit_signed(key, n, sig).map(|_| sig)
|
||||
}
|
||||
|
||||
|
|
|
@ -34,9 +34,9 @@ impl AccountantStub {
|
|||
}
|
||||
|
||||
pub fn deposit(self: &mut Self, n: u64, keypair: &Ed25519KeyPair) -> io::Result<Signature> {
|
||||
use event::{get_pubkey, sign_serialized};
|
||||
use event::{get_pubkey, sign_claim_data};
|
||||
let key = get_pubkey(keypair);
|
||||
let sig = sign_serialized(&n, keypair);
|
||||
let sig = sign_claim_data(&n, keypair);
|
||||
self.deposit_signed(key, n, sig).map(|_| sig)
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ extern crate silk;
|
|||
|
||||
use silk::historian::Historian;
|
||||
use silk::log::{verify_slice, Entry, Sha256Hash};
|
||||
use silk::event::{generate_keypair, get_pubkey, sign_serialized, Event};
|
||||
use silk::event::{generate_keypair, get_pubkey, sign_claim_data, Event};
|
||||
use std::thread::sleep;
|
||||
use std::time::Duration;
|
||||
use std::sync::mpsc::SendError;
|
||||
|
@ -11,7 +11,7 @@ fn create_log(hist: &Historian<Sha256Hash>) -> Result<(), SendError<Event<Sha256
|
|||
sleep(Duration::from_millis(15));
|
||||
let data = Sha256Hash::default();
|
||||
let keypair = generate_keypair();
|
||||
let event0 = Event::new_claim(get_pubkey(&keypair), data, sign_serialized(&data, &keypair));
|
||||
let event0 = Event::new_claim(get_pubkey(&keypair), data, sign_claim_data(&data, &keypair));
|
||||
hist.sender.send(event0)?;
|
||||
sleep(Duration::from_millis(10));
|
||||
Ok(())
|
||||
|
|
39
src/event.rs
39
src/event.rs
|
@ -44,7 +44,14 @@ pub enum Event<T> {
|
|||
|
||||
impl<T> Event<T> {
|
||||
pub fn new_claim(to: PublicKey, data: T, sig: Signature) -> Self {
|
||||
// TODO: Change this to a Transaction
|
||||
Event::Claim { to, data, sig }
|
||||
//Event::Transaction {
|
||||
// from: None,
|
||||
// to,
|
||||
// data,
|
||||
// sig,
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -63,7 +70,7 @@ pub fn get_pubkey(keypair: &Ed25519KeyPair) -> PublicKey {
|
|||
}
|
||||
|
||||
/// Return a signature for the given data using the private key from the given keypair.
|
||||
pub fn sign_serialized<T: Serialize>(data: &T, keypair: &Ed25519KeyPair) -> Signature {
|
||||
fn sign_serialized<T: Serialize>(data: &T, keypair: &Ed25519KeyPair) -> Signature {
|
||||
use bincode::serialize;
|
||||
let serialized = serialize(data).unwrap();
|
||||
GenericArray::clone_from_slice(keypair.sign(&serialized).as_ref())
|
||||
|
@ -78,6 +85,11 @@ pub fn sign_transaction_data<T: Serialize>(
|
|||
sign_serialized(&(data, to), keypair)
|
||||
}
|
||||
|
||||
/// Return a signature for the given data using the private key from the given keypair.
|
||||
pub fn sign_claim_data<T: Serialize>(data: &T, keypair: &Ed25519KeyPair) -> Signature {
|
||||
sign_transaction_data(data, keypair, &get_pubkey(keypair))
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
use untrusted;
|
||||
|
@ -99,8 +111,8 @@ pub fn get_signature<T>(event: &Event<T>) -> Option<Signature> {
|
|||
pub fn verify_event<T: Serialize>(event: &Event<T>) -> bool {
|
||||
use bincode::serialize;
|
||||
if let Event::Claim { to, ref data, sig } = *event {
|
||||
let mut claim_data = serialize(&data).unwrap();
|
||||
if !verify_signature(&to, &claim_data, &sig) {
|
||||
let sign_data = serialize(&(&data, &to)).unwrap();
|
||||
if !verify_signature(&to, &sign_data, &sig) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -118,3 +130,24 @@ pub fn verify_event<T: Serialize>(event: &Event<T>) -> bool {
|
|||
}
|
||||
true
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use bincode::{deserialize, serialize};
|
||||
use log::{hash, Sha256Hash};
|
||||
|
||||
#[test]
|
||||
fn test_serialize_claim() {
|
||||
//let claim0 = Event::new_claim(Default::default(), 0u8, Default::default());
|
||||
let claim0 = Event::Transaction {
|
||||
from: None,
|
||||
to: Default::default(),
|
||||
data: hash(b"hi"),
|
||||
sig: Default::default(),
|
||||
};
|
||||
let buf = serialize(&claim0).unwrap();
|
||||
let claim1: Event<Sha256Hash> = deserialize(&buf).unwrap();
|
||||
assert_eq!(claim1, claim0);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -214,7 +214,7 @@ mod tests {
|
|||
#[test]
|
||||
fn test_bad_event_signature() {
|
||||
let keypair = generate_keypair();
|
||||
let sig = sign_serialized(&hash(b"hello, world"), &keypair);
|
||||
let sig = sign_claim_data(&hash(b"hello, world"), &keypair);
|
||||
let event0 = Event::new_claim(get_pubkey(&keypair), hash(b"goodbye cruel world"), sig);
|
||||
let mut sigs = HashSet::new();
|
||||
assert!(!verify_event_and_reserve_signature(&mut sigs, &event0));
|
||||
|
@ -226,7 +226,7 @@ mod tests {
|
|||
let keypair = generate_keypair();
|
||||
let to = get_pubkey(&keypair);
|
||||
let data = &hash(b"hello, world");
|
||||
let sig = sign_serialized(data, &keypair);
|
||||
let sig = sign_claim_data(data, &keypair);
|
||||
let event0 = Event::new_claim(to, data, sig);
|
||||
let mut sigs = HashSet::new();
|
||||
assert!(verify_event_and_reserve_signature(&mut sigs, &event0));
|
||||
|
|
|
@ -211,8 +211,8 @@ mod tests {
|
|||
|
||||
// First, verify entries
|
||||
let keypair = generate_keypair();
|
||||
let event0 = Event::new_claim(get_pubkey(&keypair), zero, sign_serialized(&zero, &keypair));
|
||||
let event1 = Event::new_claim(get_pubkey(&keypair), one, sign_serialized(&one, &keypair));
|
||||
let event0 = Event::new_claim(get_pubkey(&keypair), zero, sign_claim_data(&zero, &keypair));
|
||||
let event1 = Event::new_claim(get_pubkey(&keypair), one, sign_claim_data(&one, &keypair));
|
||||
let events = vec![event0, event1];
|
||||
let mut entries = create_entries(&zero, 0, events);
|
||||
assert!(verify_slice(&entries, &zero));
|
||||
|
@ -229,7 +229,7 @@ mod tests {
|
|||
fn test_claim() {
|
||||
let keypair = generate_keypair();
|
||||
let data = hash(b"hello, world");
|
||||
let event0 = Event::new_claim(get_pubkey(&keypair), data, sign_serialized(&data, &keypair));
|
||||
let event0 = Event::new_claim(get_pubkey(&keypair), data, sign_claim_data(&data, &keypair));
|
||||
let zero = Sha256Hash::default();
|
||||
let entries = create_entries(&zero, 0, vec![event0]);
|
||||
assert!(verify_slice(&entries, &zero));
|
||||
|
@ -241,7 +241,7 @@ mod tests {
|
|||
let event0 = Event::new_claim(
|
||||
get_pubkey(&keypair),
|
||||
hash(b"goodbye cruel world"),
|
||||
sign_serialized(&hash(b"hello, world"), &keypair),
|
||||
sign_claim_data(&hash(b"hello, world"), &keypair),
|
||||
);
|
||||
let zero = Sha256Hash::default();
|
||||
let entries = create_entries(&zero, 0, vec![event0]);
|
||||
|
|
Loading…
Reference in New Issue