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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use pea_core::*;
use pea_key::Key;
use serde::{Deserialize, Serialize};
use serde_big_array::BigArray;
use sha2::{Digest, Sha256};
use std::error::Error;
pub trait Stake {
fn get_timestamp(&self) -> u32;
fn get_deposit(&self) -> bool;
fn get_fee_bytes(&self) -> AmountBytes;
fn hash(&self) -> Hash;
fn hash_input(&self) -> [u8; 9];
}
impl Stake for StakeA {
fn get_timestamp(&self) -> u32 {
self.timestamp
}
fn get_deposit(&self) -> bool {
self.deposit
}
fn get_fee_bytes(&self) -> AmountBytes {
pea_int::to_be_bytes(self.fee)
}
fn hash(&self) -> Hash {
hash(self)
}
fn hash_input(&self) -> [u8; 9] {
hash_input(self)
}
}
impl Stake for StakeB {
fn get_timestamp(&self) -> u32 {
self.timestamp
}
fn get_deposit(&self) -> bool {
self.deposit
}
fn get_fee_bytes(&self) -> AmountBytes {
self.fee
}
fn hash(&self) -> Hash {
hash(self)
}
fn hash_input(&self) -> [u8; 9] {
hash_input(self)
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct StakeA {
pub fee: u128,
pub deposit: bool,
pub timestamp: u32,
#[serde(with = "BigArray")]
pub signature: SignatureBytes,
pub input_address: AddressBytes,
pub hash: Hash,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct StakeB {
pub fee: AmountBytes,
pub deposit: bool,
pub timestamp: u32,
#[serde(with = "BigArray")]
pub signature: SignatureBytes,
}
impl StakeA {
pub fn b(&self) -> StakeB {
StakeB {
fee: pea_int::to_be_bytes(self.fee),
deposit: self.deposit,
timestamp: self.timestamp,
signature: self.signature,
}
}
pub fn hash(&self) -> Hash {
hash(self)
}
pub fn sign(deposit: bool, fee: u128, timestamp: u32, key: &Key) -> Result<StakeA, Box<dyn Error>> {
let mut stake_a = StakeA {
fee: pea_int::floor(fee),
deposit,
timestamp,
signature: [0; 64],
input_address: [0; 20],
hash: [0; 32],
};
stake_a.hash = stake_a.hash();
stake_a.signature = key.sign(&stake_a.hash)?;
stake_a.input_address = key.address_bytes();
Ok(stake_a)
}
}
impl StakeB {
pub fn a(&self, input_address: Option<AddressBytes>) -> Result<StakeA, Box<dyn Error>> {
let input_address = match input_address {
Some(x) => x,
None => self.input_address()?,
};
Ok(StakeA {
fee: pea_int::from_be_slice(&self.fee),
deposit: self.deposit,
timestamp: self.timestamp,
signature: self.signature,
input_address,
hash: self.hash(),
})
}
pub fn hash(&self) -> Hash {
hash(self)
}
fn input_address(&self) -> Result<AddressBytes, Box<dyn Error>> {
Ok(Key::address(&self.input_public_key()?))
}
fn input_public_key(&self) -> Result<PublicKeyBytes, Box<dyn Error>> {
Ok(Key::recover(&self.hash(), &self.signature)?)
}
}
fn hash<T: Stake>(stake: &T) -> Hash {
let mut hasher = Sha256::new();
hasher.update(&stake.hash_input());
hasher.finalize().into()
}
fn hash_input<T: Stake>(stake: &T) -> [u8; 9] {
let mut bytes = [0; 9];
bytes[0..4].copy_from_slice(&stake.get_timestamp().to_be_bytes());
bytes[4..8].copy_from_slice(&stake.get_fee_bytes());
bytes[8] = if stake.get_deposit() { 1 } else { 0 };
bytes
}
impl Default for StakeA {
fn default() -> Self {
StakeA {
fee: 0,
deposit: false,
timestamp: 0,
signature: [0; 64],
input_address: [0; 20],
hash: [0; 32],
}
}
}
impl Default for StakeB {
fn default() -> Self {
StakeB {
fee: [0; AMOUNT_BYTES],
deposit: false,
timestamp: 0,
signature: [0; 64],
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_hash() {
assert_eq!(
StakeB::default().hash(),
[
62, 112, 119, 253, 47, 102, 214, 137, 224, 206, 230, 167, 207, 91, 55, 191, 45, 202, 124, 151, 154, 243, 86, 208, 163, 28, 188, 92, 133, 96,
92, 125
]
);
}
}