sc_network/service/
signature.rs1pub use libp2p::identity::SigningError;
24
25pub enum PublicKey {
27 Libp2p(libp2p::identity::PublicKey),
29
30 Litep2p(litep2p::crypto::PublicKey),
32}
33
34impl PublicKey {
35 pub fn encode_protobuf(&self) -> Vec<u8> {
37 match self {
38 Self::Libp2p(public) => public.encode_protobuf(),
39 Self::Litep2p(public) => public.to_protobuf_encoding(),
40 }
41 }
42
43 pub fn to_peer_id(&self) -> sc_network_types::PeerId {
45 match self {
46 Self::Libp2p(public) => public.to_peer_id().into(),
47 Self::Litep2p(public) => public.to_peer_id().into(),
48 }
49 }
50}
51
52pub enum Keypair {
54 Libp2p(libp2p::identity::Keypair),
56
57 Litep2p(litep2p::crypto::ed25519::Keypair),
59}
60
61impl Keypair {
62 pub fn generate_ed25519() -> Self {
64 Keypair::Litep2p(litep2p::crypto::ed25519::Keypair::generate())
65 }
66
67 pub fn public(&self) -> PublicKey {
69 match self {
70 Keypair::Libp2p(keypair) => PublicKey::Libp2p(keypair.public()),
71 Keypair::Litep2p(keypair) => PublicKey::Litep2p(keypair.public().into()),
72 }
73 }
74}
75
76pub struct Signature {
80 pub public_key: PublicKey,
82
83 pub bytes: Vec<u8>,
85}
86
87impl Signature {
88 pub fn new(public_key: PublicKey, bytes: Vec<u8>) -> Self {
90 Self { public_key, bytes }
91 }
92
93 pub fn sign_message(
95 message: impl AsRef<[u8]>,
96 keypair: &Keypair,
97 ) -> Result<Self, SigningError> {
98 match keypair {
99 Keypair::Libp2p(keypair) => {
100 let public_key = keypair.public();
101 let bytes = keypair.sign(message.as_ref())?;
102
103 Ok(Signature { public_key: PublicKey::Libp2p(public_key), bytes })
104 },
105 Keypair::Litep2p(keypair) => {
106 let public_key = keypair.public();
107 let bytes = keypair.sign(message.as_ref());
108
109 Ok(Signature { public_key: PublicKey::Litep2p(public_key.into()), bytes })
110 },
111 }
112 }
113}