cfxkey/
lib.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
17// #![warn(missing_docs)]
18
19mod brain;
20mod brain_prefix;
21mod error;
22mod extended;
23mod keypair;
24mod password;
25mod prefix;
26mod random;
27mod secret;
28mod signature;
29
30pub mod brain_recover;
31pub mod crypto;
32pub mod math;
33
34use cfx_crypto::{
35    KeyPair as CryptoKeyPair,
36    RandomKeyPairGenerator as CryptoRandomKeyPairGenerator,
37};
38
39use lazy_static::lazy_static;
40pub use parity_wordlist::Error as WordlistError;
41
42pub use self::{
43    brain::Brain,
44    brain_prefix::BrainPrefix,
45    error::Error,
46    extended::{
47        Derivation, DerivationError, ExtendedKeyPair, ExtendedPublic,
48        ExtendedSecret,
49    },
50    keypair::{is_compatible_public, public_to_address, KeyPair},
51    math::public_is_valid,
52    password::Password,
53    prefix::Prefix,
54    random::Random,
55    secret::Secret,
56    signature::{recover, sign, verify_address, verify_public, Signature},
57    KeyPairGenerator as Generator,
58};
59
60use cfx_types::H256;
61
62pub use cfx_types::{Address, Public};
63pub type Message = H256;
64
65lazy_static! {
66    pub static ref SECP256K1: secp256k1::Secp256k1 =
67        secp256k1::Secp256k1::new();
68}
69
70/// Uninstantiatable error type for infallible generators.
71#[derive(Debug)]
72pub enum Void {}
73
74/// Generates new keypair.
75pub trait KeyPairGenerator {
76    type Error;
77
78    /// Should be called to generate new keypair.
79    fn generate(&mut self) -> Result<KeyPair, Self::Error>;
80}
81
82// Implement crypto traits for our types
83// Note: CryptoSecretKey is implemented in secret.rs to avoid conflicts
84// Note: We can't implement CryptoPublicKey for Public directly due to orphan
85// rules, so we'll use a wrapper approach in the crypto compatibility layer
86
87impl CryptoKeyPair for KeyPair {
88    type Public = Public;
89    type Secret = Secret;
90
91    fn secret(&self) -> &Self::Secret { self.secret() }
92
93    fn public(&self) -> &Self::Public { self.public() }
94}
95
96impl CryptoRandomKeyPairGenerator for Random {
97    type Error = std::io::Error;
98    type KeyPair = KeyPair;
99
100    fn generate(&mut self) -> Result<Self::KeyPair, Self::Error> {
101        KeyPairGenerator::generate(self)
102    }
103}