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
use crypto::publickey::{public_to_address, recover, Signature};
use ethereum_types::{H256, U64};
use hash::keccak;
use jsonrpc_core::Result;
use v1::{
helpers::{dispatch::eth_data_hash, errors},
types::{Bytes, RecoveredAccount},
};
pub fn verify_signature(
is_prefixed: bool,
message: Bytes,
r: H256,
s: H256,
v: U64,
chain_id: Option<u64>,
) -> Result<RecoveredAccount> {
let hash = if is_prefixed {
eth_data_hash(message.0)
} else {
keccak(message.0)
};
let v = v.as_u64();
let is_valid_for_current_chain = match (chain_id, v) {
(None, v) if v == 0 || v == 1 => true,
(Some(chain_id), v) if v >= 35 => (v - 35) / 2 == chain_id,
_ => false,
};
let v = if v >= 35 { (v - 1) % 2 } else { v };
let signature = Signature::from_rsv(&r, &s, v as u8);
let public_key = recover(&signature, &hash).map_err(errors::encryption)?;
let address = public_to_address(&public_key);
Ok(RecoveredAccount {
address,
public_key,
is_valid_for_current_chain,
})
}
#[cfg(test)]
mod tests {
use super::*;
use crypto::publickey::{self, Generator};
use ethereum_types::{H160, U64};
pub fn add_chain_replay_protection(v: u64, chain_id: Option<u64>) -> u64 {
v + if let Some(n) = chain_id {
35 + n * 2
} else {
0
}
}
struct TestCase {
should_prefix: bool,
signing_chain_id: Option<u64>,
rpc_chain_id: Option<u64>,
is_valid_for_current_chain: bool,
}
fn sign(
should_prefix: bool,
data: Vec<u8>,
signing_chain_id: Option<u64>,
) -> (H160, [u8; 32], [u8; 32], U64) {
let hash = if should_prefix {
eth_data_hash(data)
} else {
keccak(data)
};
let account = publickey::Random.generate();
let address = account.address();
let sig = publickey::sign(account.secret(), &hash).unwrap();
let (r, s, v) = (sig.r(), sig.s(), sig.v());
let v = add_chain_replay_protection(v as u64, signing_chain_id);
let (r_buf, s_buf) = {
let (mut r_buf, mut s_buf) = ([0u8; 32], [0u8; 32]);
r_buf.copy_from_slice(r);
s_buf.copy_from_slice(s);
(r_buf, s_buf)
};
(address.into(), r_buf, s_buf, v.into())
}
fn run_test(test_case: TestCase) {
let TestCase {
should_prefix,
signing_chain_id,
rpc_chain_id,
is_valid_for_current_chain,
} = test_case;
let data = vec![5u8];
let (address, r, s, v) = sign(should_prefix, data.clone(), signing_chain_id);
let account = verify_signature(
should_prefix,
data.into(),
r.into(),
s.into(),
v,
rpc_chain_id,
)
.unwrap();
assert_eq!(account.address, address.into());
assert_eq!(
account.is_valid_for_current_chain,
is_valid_for_current_chain
)
}
#[test]
fn test_verify_signature_prefixed_mainnet() {
run_test(TestCase {
should_prefix: true,
signing_chain_id: Some(1),
rpc_chain_id: Some(1),
is_valid_for_current_chain: true,
})
}
#[test]
fn test_verify_signature_not_prefixed_mainnet() {
run_test(TestCase {
should_prefix: false,
signing_chain_id: Some(1),
rpc_chain_id: Some(1),
is_valid_for_current_chain: true,
})
}
#[test]
fn test_verify_signature_incompatible_chain_id() {
run_test(TestCase {
should_prefix: false,
signing_chain_id: Some(65),
rpc_chain_id: Some(1),
is_valid_for_current_chain: false,
});
run_test(TestCase {
should_prefix: true,
signing_chain_id: Some(65),
rpc_chain_id: Some(1),
is_valid_for_current_chain: false,
});
}
#[test]
fn test_verify_signature_no_signing_chain_id() {
run_test(TestCase {
should_prefix: false,
signing_chain_id: None,
rpc_chain_id: Some(1),
is_valid_for_current_chain: false,
});
run_test(TestCase {
should_prefix: true,
signing_chain_id: None,
rpc_chain_id: Some(1),
is_valid_for_current_chain: false,
});
}
#[test]
fn test_verify_signature_no_rpc_chain_id() {
run_test(TestCase {
should_prefix: false,
signing_chain_id: Some(1),
rpc_chain_id: None,
is_valid_for_current_chain: false,
});
run_test(TestCase {
should_prefix: true,
signing_chain_id: Some(1),
rpc_chain_id: None,
is_valid_for_current_chain: false,
});
}
#[test]
fn test_verify_signature_no_chain_replay_protection() {
run_test(TestCase {
should_prefix: false,
signing_chain_id: None,
rpc_chain_id: None,
is_valid_for_current_chain: true,
});
run_test(TestCase {
should_prefix: true,
signing_chain_id: None,
rpc_chain_id: None,
is_valid_for_current_chain: true,
});
}
}