cfxkey/random.rs
1// Copyright 2015-2019 Parity Technologies (UK) Ltd.
2// This file is part of Parity Ethereum.
3
4// Parity Ethereum is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Parity Ethereum is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
16
17use super::{KeyPair, KeyPairGenerator, SECP256K1};
18use rand_07::rngs::OsRng;
19
20/// Randomly generates new keypair, instantiating the RNG each time.
21pub struct Random;
22
23impl KeyPairGenerator for Random {
24 type Error = ::std::io::Error;
25
26 fn generate(&mut self) -> Result<KeyPair, Self::Error> {
27 match OsRng.generate() {
28 Ok(pair) => Ok(pair),
29 Err(void) => match void {}, // LLVM unreachable
30 }
31 }
32}
33
34impl KeyPairGenerator for OsRng {
35 type Error = crate::Void;
36
37 fn generate(&mut self) -> Result<KeyPair, Self::Error> {
38 let (sec, publ) = SECP256K1
39 .generate_keypair(self)
40 .expect("context always created with full capabilities; qed");
41
42 Ok(KeyPair::from_keypair(sec, publ))
43 }
44}