/
validator_signer.rs
252 lines (213 loc) · 7.49 KB
/
validator_signer.rs
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
use std::path::Path;
use std::sync::Arc;
use borsh::BorshSerialize;
use near_crypto::{InMemorySigner, KeyType, PublicKey, Signature, Signer};
use crate::block::{Approval, BlockHeader, BlockHeaderInnerLite, BlockHeaderInnerRest};
use crate::challenge::ChallengeBody;
use crate::hash::{hash, CryptoHash};
use crate::network::{AnnounceAccount, PeerId};
use crate::sharding::{ChunkHash, ShardChunkHeaderInner};
use crate::telemetry::TelemetryInfo;
use crate::types::{AccountId, BlockHeight, EpochId};
/// Validator signer that is used to sign blocks and approvals.
pub trait ValidatorSigner: Sync + Send {
/// Account id of the given validator.
fn validator_id(&self) -> &AccountId;
/// Public key that identifies this validator.
fn public_key(&self) -> PublicKey;
/// Serializes telemetry info to JSON and signs it, returning JSON with "signature" field.
fn sign_telemetry(&self, info: &TelemetryInfo) -> serde_json::Value;
/// Signs given parts of the header.
fn sign_block_header_parts(
&self,
prev_hash: CryptoHash,
inner_lite: &BlockHeaderInnerLite,
inner_rest: &BlockHeaderInnerRest,
) -> (CryptoHash, Signature);
/// Signs given inner of the chunk header.
fn sign_chunk_header_inner(
&self,
chunk_header_inner: &ShardChunkHeaderInner,
) -> (ChunkHash, Signature);
/// Signs approval of given parent hash and reference hash.
fn sign_approval(
&self,
parent_hash: &CryptoHash,
reference_hash: &Option<CryptoHash>,
target_height: BlockHeight,
is_endorsement: bool,
) -> Signature;
/// Signs challenge body.
fn sign_challenge(&self, challenge_body: &ChallengeBody) -> (CryptoHash, Signature);
/// Signs account announce.
fn sign_account_announce(
&self,
account_id: &AccountId,
peer_id: &PeerId,
epoch_id: &EpochId,
) -> Signature;
fn compute_vrf_with_proof(
&self,
data: &[u8],
) -> (near_crypto::vrf::Value, near_crypto::vrf::Proof);
/// Used by test infrastructure, only implement if make sense for testing otherwise raise `unimplemented`.
fn write_to_file(&self, path: &Path);
}
/// Test-only signer that "signs" everything with 0s.
/// Don't use in any production or code that requires signature verification.
#[derive(Default)]
pub struct EmptyValidatorSigner {
account_id: AccountId,
}
impl ValidatorSigner for EmptyValidatorSigner {
fn validator_id(&self) -> &AccountId {
&self.account_id
}
fn public_key(&self) -> PublicKey {
PublicKey::empty(KeyType::ED25519)
}
fn sign_telemetry(&self, _info: &TelemetryInfo) -> serde_json::Value {
serde_json::Value::default()
}
fn sign_block_header_parts(
&self,
prev_hash: CryptoHash,
inner_lite: &BlockHeaderInnerLite,
inner_rest: &BlockHeaderInnerRest,
) -> (CryptoHash, Signature) {
let hash = BlockHeader::compute_hash(prev_hash, inner_lite, inner_rest);
(hash, Signature::default())
}
fn sign_chunk_header_inner(
&self,
chunk_header_inner: &ShardChunkHeaderInner,
) -> (ChunkHash, Signature) {
let hash = ChunkHash(hash(&chunk_header_inner.try_to_vec().expect("Failed to serialize")));
(hash, Signature::default())
}
fn sign_approval(
&self,
_parent_hash: &CryptoHash,
_reference_hash: &Option<CryptoHash>,
_target_height: BlockHeight,
_is_endorsement: bool,
) -> Signature {
Signature::default()
}
fn sign_challenge(&self, challenge_body: &ChallengeBody) -> (CryptoHash, Signature) {
let hash = hash(&challenge_body.try_to_vec().expect("Failed to serialize"));
(hash, Signature::default())
}
fn sign_account_announce(
&self,
_account_id: &String,
_peer_id: &PeerId,
_epoch_id: &EpochId,
) -> Signature {
Signature::default()
}
fn compute_vrf_with_proof(
&self,
_data: &[u8],
) -> (near_crypto::vrf::Value, near_crypto::vrf::Proof) {
unimplemented!()
}
fn write_to_file(&self, _path: &Path) {
unimplemented!()
}
}
/// Signer that keeps secret key in memory and signs locally.
#[derive(Clone)]
pub struct InMemoryValidatorSigner {
account_id: AccountId,
signer: Arc<dyn Signer>,
}
impl InMemoryValidatorSigner {
pub fn from_random(account_id: AccountId, key_type: KeyType) -> Self {
Self {
account_id: account_id.clone(),
signer: Arc::new(InMemorySigner::from_random(account_id, key_type)),
}
}
pub fn from_seed(account_id: &str, key_type: KeyType, seed: &str) -> Self {
Self {
account_id: account_id.to_string(),
signer: Arc::new(InMemorySigner::from_seed(account_id, key_type, seed)),
}
}
pub fn public_key(&self) -> PublicKey {
self.signer.public_key()
}
pub fn from_file(path: &Path) -> Self {
let signer = InMemorySigner::from_file(path);
Self { account_id: signer.account_id.clone(), signer: Arc::new(signer) }
}
}
impl ValidatorSigner for InMemoryValidatorSigner {
fn validator_id(&self) -> &AccountId {
&self.account_id
}
fn public_key(&self) -> PublicKey {
self.signer.public_key()
}
fn sign_telemetry(&self, info: &TelemetryInfo) -> serde_json::Value {
let mut value = serde_json::to_value(info).expect("Telemetry must serialize to JSON");
let content = serde_json::to_string(&value).expect("Telemetry must serialize to JSON");
value["signature"] = format!("{}", self.signer.sign(content.as_bytes())).into();
value
}
fn sign_block_header_parts(
&self,
prev_hash: CryptoHash,
inner_lite: &BlockHeaderInnerLite,
inner_rest: &BlockHeaderInnerRest,
) -> (CryptoHash, Signature) {
let hash = BlockHeader::compute_hash(prev_hash, inner_lite, inner_rest);
(hash, self.signer.sign(hash.as_ref()))
}
fn sign_chunk_header_inner(
&self,
chunk_header_inner: &ShardChunkHeaderInner,
) -> (ChunkHash, Signature) {
let hash = ChunkHash(hash(&chunk_header_inner.try_to_vec().expect("Failed to serialize")));
let signature = self.signer.sign(hash.as_ref());
(hash, signature)
}
fn sign_approval(
&self,
parent_hash: &CryptoHash,
reference_hash: &Option<CryptoHash>,
target_height: BlockHeight,
is_endorsement: bool,
) -> Signature {
self.signer.sign(&Approval::get_data_for_sig(
parent_hash,
reference_hash,
target_height,
is_endorsement,
))
}
fn sign_challenge(&self, challenge_body: &ChallengeBody) -> (CryptoHash, Signature) {
let hash = hash(&challenge_body.try_to_vec().expect("Failed to serialize"));
let signature = self.signer.sign(hash.as_ref());
(hash, signature)
}
fn sign_account_announce(
&self,
account_id: &AccountId,
peer_id: &PeerId,
epoch_id: &EpochId,
) -> Signature {
let hash = AnnounceAccount::build_header_hash(&account_id, &peer_id, epoch_id);
self.signer.sign(hash.as_ref())
}
fn compute_vrf_with_proof(
&self,
data: &[u8],
) -> (near_crypto::vrf::Value, near_crypto::vrf::Proof) {
self.signer.compute_vrf_with_proof(data)
}
fn write_to_file(&self, path: &Path) {
self.signer.write_to_file(path);
}
}