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 cfx_types::H256;
40use lazy_static::lazy_static;
41use secp256k1::SecretKey;
42
43pub use cfx_types::{Address, Public};
44pub use parity_wordlist::Error as WordlistError;
45pub type Message = H256;
46
47pub use self::{
48    brain::Brain,
49    brain_prefix::BrainPrefix,
50    error::Error,
51    extended::{
52        Derivation, DerivationError, ExtendedKeyPair, ExtendedPublic,
53        ExtendedSecret,
54    },
55    keypair::{is_compatible_public, public_to_address, KeyPair},
56    math::public_is_valid,
57    password::Password,
58    prefix::Prefix,
59    random::Random,
60    secret::Secret,
61    signature::{recover, sign, verify_address, verify_public, Signature},
62    KeyPairGenerator as Generator,
63};
64
65lazy_static! {
66    /// The scalar `1` as a `SecretKey`.
67    pub(crate) static ref ONE_KEY: SecretKey = SecretKey::from_slice(&[
68        0, 0, 0, 0, 0, 0, 0, 0,
69        0, 0, 0, 0, 0, 0, 0, 0,
70        0, 0, 0, 0, 0, 0, 0, 0,
71        0, 0, 0, 0, 0, 0, 0, 1,
72    ]).expect("1 is a valid secret key");
73
74    /// The scalar `n - 1` (i.e. `-1 mod n`) as a `SecretKey`.
75    pub(crate) static ref MINUS_ONE_KEY: SecretKey = ONE_KEY.negate();
76}
77
78/// Uninstantiatable error type for infallible generators.
79#[derive(Debug)]
80pub enum Void {}
81
82/// Generates new keypair.
83pub trait KeyPairGenerator {
84    type Error;
85
86    /// Should be called to generate new keypair.
87    fn generate(&mut self) -> Result<KeyPair, Self::Error>;
88}
89
90// Implement crypto traits for our types
91// Note: CryptoSecretKey is implemented in secret.rs to avoid conflicts
92// Note: We can't implement CryptoPublicKey for Public directly due to orphan
93// rules, so we'll use a wrapper approach in the crypto compatibility layer
94
95impl CryptoKeyPair for KeyPair {
96    type Public = Public;
97    type Secret = Secret;
98
99    fn secret(&self) -> &Self::Secret { self.secret() }
100
101    fn public(&self) -> &Self::Public { self.public() }
102}
103
104impl CryptoRandomKeyPairGenerator for Random {
105    type Error = std::io::Error;
106    type KeyPair = KeyPair;
107
108    fn generate(&mut self) -> Result<Self::KeyPair, Self::Error> {
109        KeyPairGenerator::generate(self)
110    }
111}