fluence_keypair/
lib.rs

1/*
2 * Copyright 2020 Fluence Labs Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#![recursion_limit = "512"]
18#![warn(rust_2018_idioms)]
19#![deny(
20    dead_code,
21    nonstandard_style,
22    unused_imports,
23    unused_mut,
24    unused_variables,
25    unused_unsafe,
26    unreachable_patterns
27)]
28
29mod ed25519;
30pub mod error;
31pub mod key_pair;
32pub mod public_key;
33#[cfg(not(target_arch = "wasm32"))]
34mod rsa;
35mod secp256k1;
36pub mod signature;
37
38pub use crate::public_key::PublicKey;
39pub use crate::signature::Signature;
40pub use key_pair::KeyFormat;
41pub use key_pair::KeyPair;
42
43pub mod peerid_serializer {
44    use libp2p_identity::PeerId;
45    use serde::{Deserialize, Deserializer, Serialize, Serializer};
46    use std::str::FromStr;
47
48    pub fn serialize<S>(value: &PeerId, serializer: S) -> Result<S::Ok, S::Error>
49    where
50        S: Serializer,
51    {
52        value.to_base58().serialize(serializer)
53    }
54
55    pub fn deserialize<'de, D>(deserializer: D) -> Result<PeerId, D::Error>
56    where
57        D: Deserializer<'de>,
58    {
59        let str = String::deserialize(deserializer)?;
60        PeerId::from_str(&str).map_err(|e| {
61            serde::de::Error::custom(format!("peer id deserialization failed for {e:?}"))
62        })
63    }
64}