cfx_crypto/
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// Copyright 2020 Parity Technologies
18//
19// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
20// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
21// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
22// option. This file may not be copied, modified, or distributed
23// except according to those terms.
24
25pub mod crypto;
26
27use lazy_static::lazy_static;
28
29lazy_static! {
30    pub static ref SECP256K1: secp256k1::Secp256k1 =
31        secp256k1::Secp256k1::new();
32}
33
34/// Trait for secret key types used in cryptographic operations.
35pub trait SecretKey: AsRef<[u8]> {
36    /// Create a secret key from an unsafe slice (may not be validated).
37    /// Returns an error if the slice is invalid.
38    fn from_unsafe_slice(bytes: &[u8]) -> Result<Self, crypto::Error>
39    where Self: Sized;
40}
41
42/// Trait for keypair types that can generate random keypairs.
43pub trait KeyPair {
44    type Secret: SecretKey;
45    type Public: AsRef<[u8]>;
46
47    /// Get the secret key.
48    fn secret(&self) -> &Self::Secret;
49    /// Get the public key.
50    fn public(&self) -> &Self::Public;
51}
52
53/// Trait for generating random keypairs.
54pub trait RandomKeyPairGenerator {
55    type KeyPair: KeyPair;
56    type Error;
57
58    /// Generate a random keypair.
59    fn generate(&mut self) -> Result<Self::KeyPair, Self::Error>;
60}