, key: &ValidatorId) -> Result<(), ()> {
let data = Self::payload_data(&self.payload, context);
if self.signature.verify(data.as_slice(), key) { Ok(()) } else { Err(()) }
}
/// Immutably access the payload.
#[inline]
pub fn payload(&self) -> &Payload {
&self.payload
}
/// Immutably access the validator index.
#[inline]
pub fn validator_index(&self) -> ValidatorIndex {
self.validator_index
}
/// Immutably access the signature.
#[inline]
pub fn signature(&self) -> &ValidatorSignature {
&self.signature
}
/// Discard signing data, get the payload
// Note: can't `impl From> for P` because the orphan rule exception doesn't
// handle this case yet. Likewise can't `impl Into
for Signed
` because it might
// potentially conflict with the global blanket impl, even though it currently doesn't.
#[inline]
pub fn into_payload(self) -> Payload {
self.payload
}
}
/// Custom validity errors used in Polkadot while validating transactions.
#[repr(u8)]
pub enum ValidityError {
/// The Ethereum signature is invalid.
InvalidEthereumSignature = 0,
/// The signer has no claim.
SignerHasNoClaim = 1,
/// No permission to execute the call.
NoPermission = 2,
/// An invalid statement was made for a claim.
InvalidStatement = 3,
}
impl From for u8 {
fn from(err: ValidityError) -> Self {
err as u8
}
}
/// App-specific crypto used for reporting equivocation/misbehavior in BABE,
/// GRANDPA and Parachains, described in the white paper as the fisherman role.
/// Any rewards for misbehavior reporting will be paid out to this account.
pub mod fisherman {
use super::{Signature, Verify};
use primitives::crypto::KeyTypeId;
/// Key type for the reporting module. Used for reporting BABE, GRANDPA
/// and Parachain equivocations.
pub const KEY_TYPE: KeyTypeId = KeyTypeId(*b"fish");
mod app {
use application_crypto::{app_crypto, sr25519};
app_crypto!(sr25519, super::KEY_TYPE);
}
/// Identity of the equivocation/misbehavior reporter.
pub type FishermanId = app::Public;
/// An `AppCrypto` type to allow submitting signed transactions using the fisherman
/// application key as signer.
pub struct FishermanAppCrypto;
impl frame_system::offchain::AppCrypto<::Signer, Signature> for FishermanAppCrypto {
type RuntimeAppPublic = FishermanId;
type GenericSignature = primitives::sr25519::Signature;
type GenericPublic = primitives::sr25519::Public;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn balance_bigger_than_usize() {
let zero_b: Balance = 0;
let zero_u: usize = 0;
assert!(zero_b.leading_zeros() >= zero_u.leading_zeros());
}
#[test]
fn collator_signature_payload_is_valid() {
// if this fails, collator signature verification code has to be updated.
let h = Hash::default();
assert_eq!(h.as_ref().len(), 32);
let _payload = collator_signature_payload(
&Hash::from([1; 32]),
&5u32.into(),
&Hash::from([2; 32]),
);
}
}