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};
#[derive(Clone, PartialEq, Eq, RuntimeDebug)]
pub struct Signed<Payload, RealPayload = Payload>(UncheckedSigned<Payload, RealPayload>);
impl<Payload, RealPayload> Signed<Payload, RealPayload> {
pub fn into_unchecked(self) -> UncheckedSigned<Payload, RealPayload> {
self.0
}
}
#[derive(Clone, PartialEq, Eq, RuntimeDebug, Encode, Decode, TypeInfo)]
pub struct UncheckedSigned<Payload, RealPayload = Payload> {
payload: Payload,
validator_index: ValidatorIndex,
signature: ValidatorSignature,
real_payload: core::marker::PhantomData<RealPayload>,
}
impl<Payload: EncodeAs<RealPayload>, RealPayload: Encode> Signed<Payload, RealPayload> {
#[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))
}
#[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))
}
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)
}
}
pub fn as_unchecked(&self) -> &UncheckedSigned<Payload, RealPayload> {
&self.0
}
#[inline]
pub fn payload(&self) -> &Payload {
&self.0.payload
}
#[inline]
pub fn validator_index(&self) -> ValidatorIndex {
self.0.validator_index
}
#[inline]
pub fn signature(&self) -> &ValidatorSignature {
&self.0.signature
}
#[inline]
pub fn into_payload(self) -> Payload {
self.0.payload
}
pub fn convert_payload(&self) -> Signed<RealPayload>
where
for<'a> &'a Payload: Into<RealPayload>,
{
Signed(self.0.unchecked_convert_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))
}
}
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)
}
}
}
impl<Payload: EncodeAs<RealPayload>, RealPayload: Encode> UncheckedSigned<Payload, RealPayload> {
#[cfg(feature = "std")]
pub fn new(
payload: Payload,
validator_index: ValidatorIndex,
signature: ValidatorSignature,
) -> Self {
Self { payload, validator_index, signature, real_payload: std::marker::PhantomData }
}
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)
}
#[inline]
pub fn unchecked_payload(&self) -> &Payload {
&self.payload
}
#[inline]
pub fn unchecked_validator_index(&self) -> ValidatorIndex {
self.validator_index
}
#[inline]
pub fn unchecked_signature(&self) -> &ValidatorSignature {
&self.signature
}
#[inline]
pub fn unchecked_into_payload(self) -> Payload {
self.payload
}
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> {
let mut out = payload.encode_as();
out.extend(context.encode());
out
}
#[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)
}
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(())
}
}
#[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 }
}
#[cfg(any(feature = "runtime-benchmarks", feature = "std"))]
pub fn benchmark_signature(&self) -> ValidatorSignature {
self.signature.clone()
}
#[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
}
}
pub trait EncodeAs<T> {
fn encode_as(&self) -> Vec<u8>;
}
impl<T: Encode> EncodeAs<T> for T {
fn encode_as(&self) -> Vec<u8> {
self.encode()
}
}