1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use ed25519::signature::Signer;
use pea_core::{types, util};
use serde::{Deserialize, Serialize};
use serde_big_array::BigArray;
use std::error::Error;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Stake {
pub public_key: types::PublicKeyBytes,
pub amount: types::Amount,
pub deposit: bool, pub fee: types::Amount,
pub timestamp: types::Timestamp,
#[serde(with = "BigArray")]
pub signature: types::SignatureBytes,
}
impl Stake {
pub fn new(deposit: bool, amount: types::Amount, fee: types::Amount) -> Stake {
Stake {
public_key: [0; 32],
amount,
deposit,
fee,
timestamp: util::timestamp(),
signature: [0; 64],
}
}
pub fn hash(&self) -> types::Hash {
util::hash(&bincode::serialize(&Header::from(self)).unwrap())
}
pub fn sign(&mut self, keypair: &types::Keypair) {
self.public_key = keypair.public.to_bytes();
self.signature = keypair.sign(&self.hash()).to_bytes();
}
pub fn verify(&self) -> Result<(), Box<dyn Error>> {
let public_key = types::PublicKey::from_bytes(&self.public_key)?;
let signature = types::Signature::from_bytes(&self.signature)?;
Ok(public_key.verify_strict(&self.hash(), &signature)?)
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Header {
pub public_key: types::PublicKeyBytes,
pub amount: types::Amount,
pub fee: types::Amount,
pub timestamp: types::Timestamp,
}
impl Header {
pub fn from(stake: &Stake) -> Header {
Header {
public_key: stake.public_key,
amount: stake.amount,
fee: stake.fee,
timestamp: stake.timestamp,
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Compressed {
pub public_key: types::PublicKeyBytes,
pub amount: types::CompressedAmount,
pub fee: types::CompressedAmount,
pub deposit: bool,
pub timestamp: types::Timestamp,
#[serde(with = "BigArray")]
pub signature: types::SignatureBytes,
}