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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot 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.

// Polkadot 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 Polkadot.  If not, see <http://www.gnu.org/licenses/>.

use codec::{Decode, Encode};
use scale_info::TypeInfo;

use alloc::vec::Vec;
#[cfg(feature = "std")]
use sp_application_crypto::AppCrypto;
#[cfg(feature = "std")]
use sp_keystore::{Error as KeystoreError, KeystorePtr};

use sp_core::RuntimeDebug;
use sp_runtime::traits::AppVerify;

use super::{SigningContext, ValidatorId, ValidatorIndex, ValidatorSignature};

/// Signed data with signature already verified.
///
/// NOTE: This type does not have an Encode/Decode instance, as this would cancel out our
/// valid signature guarantees. If you need to encode/decode you have to convert into an
/// `UncheckedSigned` first.
///
/// `Signed` can easily be converted into `UncheckedSigned` and conversion back via `into_signed`
/// enforces a valid signature again.
#[derive(Clone, PartialEq, Eq, RuntimeDebug)]
pub struct Signed<Payload, RealPayload = Payload>(UncheckedSigned<Payload, RealPayload>);

impl<Payload, RealPayload> Signed<Payload, RealPayload> {
	/// Convert back to an unchecked type.
	pub fn into_unchecked(self) -> UncheckedSigned<Payload, RealPayload> {
		self.0
	}
}

/// Unchecked signed data, can be converted to `Signed` by checking the signature.
#[derive(Clone, PartialEq, Eq, RuntimeDebug, Encode, Decode, TypeInfo)]
pub struct UncheckedSigned<Payload, RealPayload = Payload> {
	/// The payload is part of the signed data. The rest is the signing context,
	/// which is known both at signing and at validation.
	payload: Payload,
	/// The index of the validator signing this statement.
	validator_index: ValidatorIndex,
	/// The signature by the validator of the signed payload.
	signature: ValidatorSignature,
	/// This ensures the real payload is tracked at the typesystem level.
	real_payload: core::marker::PhantomData<RealPayload>,
}

impl<Payload: EncodeAs<RealPayload>, RealPayload: Encode> Signed<Payload, RealPayload> {
	/// Used to create a `Signed` from already existing parts.
	///
	/// The signature is checked as part of the process.
	#[cfg(feature = "std")]
	pub fn new<H: Encode>(
		payload: Payload,
		validator_index: ValidatorIndex,
		signature: ValidatorSignature,
		context: &SigningContext<H>,
		key: &ValidatorId,
	) -> Option<Self> {
		let s = UncheckedSigned {
			payload,
			validator_index,
			signature,
			real_payload: std::marker::PhantomData,
		};

		s.check_signature(context, key).ok()?;

		Some(Self(s))
	}

	/// Create a new `Signed` by signing data.
	#[cfg(feature = "std")]
	pub fn sign<H: Encode>(
		keystore: &KeystorePtr,
		payload: Payload,
		context: &SigningContext<H>,
		validator_index: ValidatorIndex,
		key: &ValidatorId,
	) -> Result<Option<Self>, KeystoreError> {
		let r = UncheckedSigned::sign(keystore, payload, context, validator_index, key)?;
		Ok(r.map(Self))
	}

	/// Try to convert from `UncheckedSigned` by checking the signature.
	pub fn try_from_unchecked<H: Encode>(
		unchecked: UncheckedSigned<Payload, RealPayload>,
		context: &SigningContext<H>,
		key: &ValidatorId,
	) -> Result<Self, UncheckedSigned<Payload, RealPayload>> {
		if unchecked.check_signature(context, key).is_ok() {
			Ok(Self(unchecked))
		} else {
			Err(unchecked)
		}
	}

	/// Get a reference to data as unchecked.
	pub fn as_unchecked(&self) -> &UncheckedSigned<Payload, RealPayload> {
		&self.0
	}

	/// Immutably access the payload.
	#[inline]
	pub fn payload(&self) -> &Payload {
		&self.0.payload
	}

	/// Immutably access the validator index.
	#[inline]
	pub fn validator_index(&self) -> ValidatorIndex {
		self.0.validator_index
	}

	/// Immutably access the signature.
	#[inline]
	pub fn signature(&self) -> &ValidatorSignature {
		&self.0.signature
	}

	/// Discard signing data, get the payload
	#[inline]
	pub fn into_payload(self) -> Payload {
		self.0.payload
	}

	/// Convert `Payload` into `RealPayload`.
	pub fn convert_payload(&self) -> Signed<RealPayload>
	where
		for<'a> &'a Payload: Into<RealPayload>,
	{
		Signed(self.0.unchecked_convert_payload())
	}

	/// Convert `Payload` into some claimed `SuperPayload` if the encoding matches.
	///
	/// Succeeds if and only if the super-payload provided actually encodes as
	/// the expected payload.
	pub fn convert_to_superpayload<SuperPayload>(
		self,
		claimed: SuperPayload,
	) -> Result<Signed<SuperPayload, RealPayload>, (Self, SuperPayload)>
	where
		SuperPayload: EncodeAs<RealPayload>,
	{
		if claimed.encode_as() == self.0.payload.encode_as() {
			Ok(Signed(UncheckedSigned {
				payload: claimed,
				validator_index: self.0.validator_index,
				signature: self.0.signature,
				real_payload: core::marker::PhantomData,
			}))
		} else {
			Err((self, claimed))
		}
	}

	/// Convert `Payload` into some converted `SuperPayload` if the encoding matches.
	///
	/// This invokes the closure on the current payload, which is irreversible.
	///
	/// Succeeds if and only if the super-payload provided actually encodes as
	/// the expected payload.
	pub fn convert_to_superpayload_with<F, SuperPayload>(
		self,
		convert: F,
	) -> Result<Signed<SuperPayload, RealPayload>, SuperPayload>
	where
		F: FnOnce(Payload) -> SuperPayload,
		SuperPayload: EncodeAs<RealPayload>,
	{
		let expected_encode_as = self.0.payload.encode_as();
		let converted = convert(self.0.payload);
		if converted.encode_as() == expected_encode_as {
			Ok(Signed(UncheckedSigned {
				payload: converted,
				validator_index: self.0.validator_index,
				signature: self.0.signature,
				real_payload: core::marker::PhantomData,
			}))
		} else {
			Err(converted)
		}
	}
}

// We can't bound this on `Payload: Into<RealPayload>` because that conversion consumes
// the payload, and we don't want that. We can't bound it on `Payload: AsRef<RealPayload>`
// because there's no blanket impl of `AsRef<T> for T`. In the end, we just invent our
// own trait which does what we need: EncodeAs.
impl<Payload: EncodeAs<RealPayload>, RealPayload: Encode> UncheckedSigned<Payload, RealPayload> {
	/// Used to create a `UncheckedSigned` from already existing parts.
	///
	/// Signature is not checked here, hence `UncheckedSigned`.
	#[cfg(feature = "std")]
	pub fn new(
		payload: Payload,
		validator_index: ValidatorIndex,
		signature: ValidatorSignature,
	) -> Self {
		Self { payload, validator_index, signature, real_payload: std::marker::PhantomData }
	}

	/// Check signature and convert to `Signed` if successful.
	pub fn try_into_checked<H: Encode>(
		self,
		context: &SigningContext<H>,
		key: &ValidatorId,
	) -> Result<Signed<Payload, RealPayload>, Self> {
		Signed::try_from_unchecked(self, context, key)
	}

	/// Immutably access the payload.
	#[inline]
	pub fn unchecked_payload(&self) -> &Payload {
		&self.payload
	}

	/// Immutably access the validator index.
	#[inline]
	pub fn unchecked_validator_index(&self) -> ValidatorIndex {
		self.validator_index
	}

	/// Immutably access the signature.
	#[inline]
	pub fn unchecked_signature(&self) -> &ValidatorSignature {
		&self.signature
	}

	/// Discard signing data, get the payload
	#[inline]
	pub fn unchecked_into_payload(self) -> Payload {
		self.payload
	}

	/// Convert `Payload` into `RealPayload`.
	pub fn unchecked_convert_payload(&self) -> UncheckedSigned<RealPayload>
	where
		for<'a> &'a Payload: Into<RealPayload>,
	{
		UncheckedSigned {
			signature: self.signature.clone(),
			validator_index: self.validator_index,
			payload: (&self.payload).into(),
			real_payload: core::marker::PhantomData,
		}
	}

	fn payload_data<H: Encode>(payload: &Payload, context: &SigningContext<H>) -> Vec<u8> {
		// equivalent to (`real_payload`, context).encode()
		let mut out = payload.encode_as();
		out.extend(context.encode());
		out
	}

	/// Sign this payload with the given context and key, storing the validator index.
	#[cfg(feature = "std")]
	fn sign<H: Encode>(
		keystore: &KeystorePtr,
		payload: Payload,
		context: &SigningContext<H>,
		validator_index: ValidatorIndex,
		key: &ValidatorId,
	) -> Result<Option<Self>, KeystoreError> {
		let data = Self::payload_data(&payload, context);
		let signature =
			keystore.sr25519_sign(ValidatorId::ID, key.as_ref(), &data)?.map(|sig| Self {
				payload,
				validator_index,
				signature: sig.into(),
				real_payload: std::marker::PhantomData,
			});
		Ok(signature)
	}

	/// Validate the payload given the context and public key
	/// without creating a `Signed` type.
	pub fn check_signature<H: Encode>(
		&self,
		context: &SigningContext<H>,
		key: &ValidatorId,
	) -> Result<(), ()> {
		let data = Self::payload_data(&self.payload, context);
		if self.signature.verify(data.as_slice(), key) {
			Ok(())
		} else {
			Err(())
		}
	}

	/// Sign this payload with the given context and pair.
	#[cfg(any(feature = "runtime-benchmarks", feature = "std"))]
	pub fn benchmark_sign<H: Encode>(
		public: &super::ValidatorId,
		payload: Payload,
		context: &SigningContext<H>,
		validator_index: ValidatorIndex,
	) -> Self {
		use sp_application_crypto::RuntimeAppPublic;
		let data = Self::payload_data(&payload, context);
		let signature = public.sign(&data).unwrap();

		Self { payload, validator_index, signature, real_payload: core::marker::PhantomData }
	}

	/// Immutably access the signature.
	#[cfg(any(feature = "runtime-benchmarks", feature = "std"))]
	pub fn benchmark_signature(&self) -> ValidatorSignature {
		self.signature.clone()
	}

	/// Set the signature. Only should be used for creating testing mocks.
	#[cfg(feature = "std")]
	pub fn set_signature(&mut self, signature: ValidatorSignature) {
		self.signature = signature
	}
}

impl<Payload, RealPayload> From<Signed<Payload, RealPayload>>
	for UncheckedSigned<Payload, RealPayload>
{
	fn from(signed: Signed<Payload, RealPayload>) -> Self {
		signed.0
	}
}

/// This helper trait ensures that we can encode `Statement` as `CompactStatement`,
/// and anything as itself.
///
/// This resembles `codec::EncodeLike`, but it's distinct:
/// `EncodeLike` is a marker trait which asserts at the typesystem level that
/// one type's encoding is a valid encoding for another type. It doesn't
/// perform any type conversion when encoding.
///
/// This trait, on the other hand, provides a method which can be used to
/// simultaneously convert and encode one type as another.
pub trait EncodeAs<T> {
	/// Convert Self into T, then encode T.
	///
	/// This is useful when T is a subset of Self, reducing encoding costs;
	/// its signature also means that we do not need to clone Self in order
	/// to retain ownership, as we would if we were to do
	/// `self.clone().into().encode()`.
	fn encode_as(&self) -> Vec<u8>;
}

impl<T: Encode> EncodeAs<T> for T {
	fn encode_as(&self) -> Vec<u8> {
		self.encode()
	}
}