Download Evaluate/projects/libp2p/rust/identity/src/lib.rs from SYSUSELab/RustRepoTrans: direct link, hf CLI and curl.
- Browser
- Download file 4.62 kB
-
https://huggingface.co/datasets/SYSUSELab/RustRepoTrans/resolve/main/Evaluate/projects/libp2p/rust/identity/src/lib.rs
- Command line
-
hf download hf://datasets/SYSUSELab/RustRepoTrans/Evaluate/projects/libp2p/rust/identity/src/lib.rs
-
curl -L -o lib.rs https://huggingface.co/datasets/SYSUSELab/RustRepoTrans/resolve/main/Evaluate/projects/libp2p/rust/identity/src/lib.rs
4.62 kB
| // Copyright 2019 Parity Technologies (UK) Ltd. | |
| // Copyright 2023 Protocol Labs. | |
| // | |
| // Permission is hereby granted, free of charge, to any person obtaining a | |
| // copy of this software and associated documentation files (the "Software"), | |
| // to deal in the Software without restriction, including without limitation | |
| // the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
| // and/or sell copies of the Software, and to permit persons to whom the | |
| // Software is furnished to do so, subject to the following conditions: | |
| // | |
| // The above copyright notice and this permission notice shall be included in | |
| // all copies or substantial portions of the Software. | |
| // | |
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
| // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
| // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
| // DEALINGS IN THE SOFTWARE. | |
| //! A node's network identity keys. | |
| //! | |
| //! Such identity keys can be randomly generated on every startup, | |
| //! but using already existing, fixed keys is usually required. | |
| //! Though libp2p uses other crates (e.g. `ed25519_dalek`) internally, | |
| //! such details are not exposed as part of libp2p's public interface | |
| //! to keep them easily upgradable or replaceable (e.g. to `ed25519_zebra`). | |
| //! Consequently, keys of external ed25519 or secp256k1 crates cannot be | |
| //! directly converted into libp2p network identities. | |
| //! Instead, loading fixed keys must use the standard, thus more portable | |
| //! binary representation of the specific key type | |
| //! (e.g. [ed25519 binary format](https://datatracker.ietf.org/doc/html/rfc8032#section-5.1.5)). | |
| //! All key types have functions to enable conversion to/from their binary representations. | |
| mod proto { | |
| include!("generated/mod.rs"); | |
| pub(crate) use self::keys_proto::*; | |
| } | |
| pub mod ecdsa; | |
| pub mod ed25519; | |
| pub mod rsa; | |
| pub mod secp256k1; | |
| mod error; | |
| mod keypair; | |
| mod peer_id; | |
| impl zeroize::Zeroize for proto::PrivateKey { | |
| fn zeroize(&mut self) { | |
| self.Data.zeroize(); | |
| } | |
| } | |
| impl From<&PublicKey> for proto::PublicKey { | |
| fn from(key: &PublicKey) -> Self { | |
| match &key.publickey { | |
| keypair::PublicKeyInner::Ed25519(key) => proto::PublicKey { | |
| Type: proto::KeyType::Ed25519, | |
| Data: key.to_bytes().to_vec(), | |
| }, | |
| keypair::PublicKeyInner::Rsa(key) => proto::PublicKey { | |
| Type: proto::KeyType::RSA, | |
| Data: key.encode_x509(), | |
| }, | |
| keypair::PublicKeyInner::Secp256k1(key) => proto::PublicKey { | |
| Type: proto::KeyType::Secp256k1, | |
| Data: key.to_bytes().to_vec(), | |
| }, | |
| keypair::PublicKeyInner::Ecdsa(key) => proto::PublicKey { | |
| Type: proto::KeyType::ECDSA, | |
| Data: key.encode_der(), | |
| }, | |
| } | |
| } | |
| } | |
| pub use error::{DecodingError, OtherVariantError, SigningError}; | |
| pub use keypair::{Keypair, PublicKey}; | |
| pub use peer_id::{ParseError, PeerId}; | |
| /// The type of key a `KeyPair` is holding. | |
| pub enum KeyType { | |
| Ed25519, | |
| RSA, | |
| Secp256k1, | |
| Ecdsa, | |
| } | |
| impl std::fmt::Display for KeyType { | |
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | |
| match self { | |
| KeyType::Ed25519 => f.write_str("Ed25519"), | |
| KeyType::RSA => f.write_str("RSA"), | |
| KeyType::Secp256k1 => f.write_str("Secp256k1"), | |
| KeyType::Ecdsa => f.write_str("Ecdsa"), | |
| } | |
| } | |
| } | |