referrerpolicy=no-referrer-when-downgrade
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// This file is part of Substrate.
//
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// If you read this, you are very thorough, congratulations.

//! Signature-related code

pub use libp2p::identity::SigningError;

/// Public key.
pub enum PublicKey {
	/// Litep2p public key.
	Libp2p(libp2p::identity::PublicKey),

	/// Libp2p public key.
	Litep2p(litep2p::crypto::PublicKey),
}

impl PublicKey {
	/// Protobuf-encode [`PublicKey`].
	pub fn encode_protobuf(&self) -> Vec<u8> {
		match self {
			Self::Libp2p(public) => public.encode_protobuf(),
			Self::Litep2p(public) => public.to_protobuf_encoding(),
		}
	}

	/// Get `PeerId` of the [`PublicKey`].
	pub fn to_peer_id(&self) -> sc_network_types::PeerId {
		match self {
			Self::Libp2p(public) => public.to_peer_id().into(),
			Self::Litep2p(public) => public.to_peer_id().into(),
		}
	}
}

/// Keypair.
pub enum Keypair {
	/// Litep2p keypair.
	Libp2p(libp2p::identity::Keypair),

	/// Libp2p keypair.
	Litep2p(litep2p::crypto::ed25519::Keypair),
}

impl Keypair {
	/// Generate ed25519 keypair.
	pub fn generate_ed25519() -> Self {
		Keypair::Litep2p(litep2p::crypto::ed25519::Keypair::generate())
	}

	/// Get [`Keypair`]'s public key.
	pub fn public(&self) -> PublicKey {
		match self {
			Keypair::Libp2p(keypair) => PublicKey::Libp2p(keypair.public()),
			Keypair::Litep2p(keypair) => PublicKey::Litep2p(keypair.public().into()),
		}
	}
}

/// A result of signing a message with a network identity. Since `PeerId` is potentially a hash of a
/// `PublicKey`, you need to reveal the `PublicKey` next to the signature, so the verifier can check
/// if the signature was made by the entity that controls a given `PeerId`.
pub struct Signature {
	/// The public key derived from the network identity that signed the message.
	pub public_key: PublicKey,

	/// The actual signature made for the message signed.
	pub bytes: Vec<u8>,
}

impl Signature {
	/// Create new [`Signature`].
	pub fn new(public_key: PublicKey, bytes: Vec<u8>) -> Self {
		Self { public_key, bytes }
	}

	/// Create a signature for a message with a given network identity.
	pub fn sign_message(
		message: impl AsRef<[u8]>,
		keypair: &Keypair,
	) -> Result<Self, SigningError> {
		match keypair {
			Keypair::Libp2p(keypair) => {
				let public_key = keypair.public();
				let bytes = keypair.sign(message.as_ref())?;

				Ok(Signature { public_key: PublicKey::Libp2p(public_key), bytes })
			},
			Keypair::Litep2p(keypair) => {
				let public_key = keypair.public();
				let bytes = keypair.sign(message.as_ref());

				Ok(Signature { public_key: PublicKey::Litep2p(public_key.into()), bytes })
			},
		}
	}
}