Skip to main content

polkadot_runtime_common/claims/
mod.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Polkadot is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.
16
17//! Pallet to process claims from Ethereum addresses.
18
19#[cfg(not(feature = "std"))]
20use alloc::{format, string::String};
21use alloc::{vec, vec::Vec};
22use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
23use core::fmt::Debug;
24use frame_support::{
25	ensure,
26	traits::{Currency, Get, IsSubType, VestingSchedule},
27	weights::Weight,
28	DefaultNoBound,
29};
30pub use pallet::*;
31use polkadot_primitives::ValidityError;
32use scale_info::TypeInfo;
33use serde::{self, Deserialize, Deserializer, Serialize, Serializer};
34use sp_io::{crypto::secp256k1_ecdsa_recover, hashing::keccak_256};
35use sp_runtime::{
36	impl_tx_ext_default,
37	traits::{
38		AsSystemOriginSigner, AsTransactionAuthorizedOrigin, CheckedSub, DispatchInfoOf,
39		Dispatchable, TransactionExtension, Zero,
40	},
41	transaction_validity::{
42		InvalidTransaction, TransactionSource, TransactionValidity, TransactionValidityError,
43		ValidTransaction,
44	},
45};
46
47type CurrencyOf<T> = <<T as Config>::VestingSchedule as VestingSchedule<
48	<T as frame_system::Config>::AccountId,
49>>::Currency;
50type BalanceOf<T> = <CurrencyOf<T> as Currency<<T as frame_system::Config>::AccountId>>::Balance;
51
52pub trait WeightInfo {
53	fn claim() -> Weight;
54	fn mint_claim() -> Weight;
55	fn claim_attest() -> Weight;
56	fn attest() -> Weight;
57	fn move_claim() -> Weight;
58	fn prevalidate_attests() -> Weight;
59}
60
61pub struct TestWeightInfo;
62impl WeightInfo for TestWeightInfo {
63	fn claim() -> Weight {
64		Weight::zero()
65	}
66	fn mint_claim() -> Weight {
67		Weight::zero()
68	}
69	fn claim_attest() -> Weight {
70		Weight::zero()
71	}
72	fn attest() -> Weight {
73		Weight::zero()
74	}
75	fn move_claim() -> Weight {
76		Weight::zero()
77	}
78	fn prevalidate_attests() -> Weight {
79		Weight::zero()
80	}
81}
82
83/// The kind of statement an account needs to make for a claim to be valid.
84#[derive(
85	Encode,
86	Decode,
87	DecodeWithMemTracking,
88	Clone,
89	Copy,
90	Eq,
91	PartialEq,
92	Debug,
93	TypeInfo,
94	Serialize,
95	Deserialize,
96	MaxEncodedLen,
97)]
98pub enum StatementKind {
99	/// Statement required to be made by non-SAFT holders.
100	Regular,
101	/// Statement required to be made by SAFT holders.
102	Saft,
103}
104
105impl StatementKind {
106	/// Convert this to the (English) statement it represents.
107	fn to_text(self) -> &'static [u8] {
108		match self {
109			StatementKind::Regular => {
110				&b"I hereby agree to the terms of the statement whose SHA-256 multihash is \
111				Qmc1XYqT6S39WNp2UeiRUrZichUWUPpGEThDE6dAb3f6Ny. (This may be found at the URL: \
112				https://statement.polkadot.network/regular.html)"[..]
113			},
114			StatementKind::Saft => {
115				&b"I hereby agree to the terms of the statement whose SHA-256 multihash is \
116				QmXEkMahfhHJPzT3RjkXiZVFi77ZeVeuxtAjhojGRNYckz. (This may be found at the URL: \
117				https://statement.polkadot.network/saft.html)"[..]
118			},
119		}
120	}
121}
122
123impl Default for StatementKind {
124	fn default() -> Self {
125		StatementKind::Regular
126	}
127}
128
129/// An Ethereum address (i.e. 20 bytes, used to represent an Ethereum account).
130///
131/// This gets serialized to the 0x-prefixed hex representation.
132#[derive(
133	Clone,
134	Copy,
135	PartialEq,
136	Eq,
137	Encode,
138	Decode,
139	DecodeWithMemTracking,
140	Default,
141	Debug,
142	TypeInfo,
143	MaxEncodedLen,
144)]
145pub struct EthereumAddress(pub [u8; 20]);
146
147impl Serialize for EthereumAddress {
148	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
149	where
150		S: Serializer,
151	{
152		let hex: String = rustc_hex::ToHex::to_hex(&self.0[..]);
153		serializer.serialize_str(&format!("0x{}", hex))
154	}
155}
156
157impl<'de> Deserialize<'de> for EthereumAddress {
158	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
159	where
160		D: Deserializer<'de>,
161	{
162		let base_string = String::deserialize(deserializer)?;
163		let offset = if base_string.starts_with("0x") { 2 } else { 0 };
164		let s = &base_string[offset..];
165		if s.len() != 40 {
166			Err(serde::de::Error::custom(
167				"Bad length of Ethereum address (should be 42 including '0x')",
168			))?;
169		}
170		let raw: Vec<u8> = rustc_hex::FromHex::from_hex(s)
171			.map_err(|e| serde::de::Error::custom(format!("{:?}", e)))?;
172		let mut r = Self::default();
173		r.0.copy_from_slice(&raw);
174		Ok(r)
175	}
176}
177
178impl AsRef<[u8]> for EthereumAddress {
179	fn as_ref(&self) -> &[u8] {
180		&self.0[..]
181	}
182}
183
184#[derive(Encode, Decode, DecodeWithMemTracking, Clone, TypeInfo, MaxEncodedLen)]
185pub struct EcdsaSignature(pub [u8; 65]);
186
187impl PartialEq for EcdsaSignature {
188	fn eq(&self, other: &Self) -> bool {
189		&self.0[..] == &other.0[..]
190	}
191}
192
193impl core::fmt::Debug for EcdsaSignature {
194	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
195		write!(f, "EcdsaSignature({:?})", &self.0[..])
196	}
197}
198
199#[frame_support::pallet]
200pub mod pallet {
201	use super::*;
202	use frame_support::pallet_prelude::*;
203	use frame_system::pallet_prelude::*;
204
205	#[pallet::pallet]
206	pub struct Pallet<T>(_);
207
208	/// Configuration trait.
209	#[pallet::config]
210	pub trait Config: frame_system::Config {
211		/// The overarching event type.
212		#[allow(deprecated)]
213		type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
214		type VestingSchedule: VestingSchedule<Self::AccountId, Moment = BlockNumberFor<Self>>;
215		#[pallet::constant]
216		type Prefix: Get<&'static [u8]>;
217		type MoveClaimOrigin: EnsureOrigin<Self::RuntimeOrigin>;
218		type WeightInfo: WeightInfo;
219	}
220
221	#[pallet::event]
222	#[pallet::generate_deposit(pub(super) fn deposit_event)]
223	pub enum Event<T: Config> {
224		/// Someone claimed some DOTs.
225		Claimed { who: T::AccountId, ethereum_address: EthereumAddress, amount: BalanceOf<T> },
226	}
227
228	#[pallet::error]
229	pub enum Error<T> {
230		/// Invalid Ethereum signature.
231		InvalidEthereumSignature,
232		/// Ethereum address has no claim.
233		SignerHasNoClaim,
234		/// Account ID sending transaction has no claim.
235		SenderHasNoClaim,
236		/// There's not enough in the pot to pay out some unvested amount. Generally implies a
237		/// logic error.
238		PotUnderflow,
239		/// A needed statement was not included.
240		InvalidStatement,
241		/// The account already has a vested balance.
242		VestedBalanceExists,
243	}
244
245	#[pallet::storage]
246	pub type Claims<T: Config> = StorageMap<_, Identity, EthereumAddress, BalanceOf<T>>;
247
248	#[pallet::storage]
249	pub type Total<T: Config> = StorageValue<_, BalanceOf<T>, ValueQuery>;
250
251	/// Vesting schedule for a claim.
252	/// First balance is the total amount that should be held for vesting.
253	/// Second balance is how much should be unlocked per block.
254	/// The block number is when the vesting should start.
255	#[pallet::storage]
256	pub type Vesting<T: Config> =
257		StorageMap<_, Identity, EthereumAddress, (BalanceOf<T>, BalanceOf<T>, BlockNumberFor<T>)>;
258
259	/// The statement kind that must be signed, if any.
260	#[pallet::storage]
261	pub type Signing<T> = StorageMap<_, Identity, EthereumAddress, StatementKind>;
262
263	/// Pre-claimed Ethereum accounts, by the Account ID that they are claimed to.
264	#[pallet::storage]
265	pub type Preclaims<T: Config> = StorageMap<_, Identity, T::AccountId, EthereumAddress>;
266
267	#[pallet::genesis_config]
268	#[derive(DefaultNoBound)]
269	pub struct GenesisConfig<T: Config> {
270		pub claims:
271			Vec<(EthereumAddress, BalanceOf<T>, Option<T::AccountId>, Option<StatementKind>)>,
272		pub vesting: Vec<(EthereumAddress, (BalanceOf<T>, BalanceOf<T>, BlockNumberFor<T>))>,
273	}
274
275	#[pallet::genesis_build]
276	impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
277		fn build(&self) {
278			// build `Claims`
279			self.claims.iter().map(|(a, b, _, _)| (*a, *b)).for_each(|(a, b)| {
280				Claims::<T>::insert(a, b);
281			});
282			// build `Total`
283			Total::<T>::put(
284				self.claims
285					.iter()
286					.fold(Zero::zero(), |acc: BalanceOf<T>, &(_, b, _, _)| acc + b),
287			);
288			// build `Vesting`
289			self.vesting.iter().for_each(|(k, v)| {
290				Vesting::<T>::insert(k, v);
291			});
292			// build `Signing`
293			self.claims
294				.iter()
295				.filter_map(|(a, _, _, s)| Some((*a, (*s)?)))
296				.for_each(|(a, s)| {
297					Signing::<T>::insert(a, s);
298				});
299			// build `Preclaims`
300			self.claims.iter().filter_map(|(a, _, i, _)| Some((i.clone()?, *a))).for_each(
301				|(i, a)| {
302					Preclaims::<T>::insert(i, a);
303				},
304			);
305		}
306	}
307
308	#[pallet::hooks]
309	impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
310
311	#[pallet::call]
312	impl<T: Config> Pallet<T> {
313		/// Make a claim to collect your DOTs.
314		///
315		/// The dispatch origin for this call must be _None_.
316		///
317		/// Unsigned Validation:
318		/// A call to claim is deemed valid if the signature provided matches
319		/// the expected signed message of:
320		///
321		/// > Ethereum Signed Message:
322		/// > (configured prefix string)(address)
323		///
324		/// and `address` matches the `dest` account.
325		///
326		/// Parameters:
327		/// - `dest`: The destination account to payout the claim.
328		/// - `ethereum_signature`: The signature of an ethereum signed message matching the format
329		///   described above.
330		///
331		/// <weight>
332		/// The weight of this call is invariant over the input parameters.
333		/// Weight includes logic to validate unsigned `claim` call.
334		///
335		/// Total Complexity: O(1)
336		/// </weight>
337		#[pallet::call_index(0)]
338		#[pallet::weight(T::WeightInfo::claim())]
339		pub fn claim(
340			origin: OriginFor<T>,
341			dest: T::AccountId,
342			ethereum_signature: EcdsaSignature,
343		) -> DispatchResult {
344			ensure_none(origin)?;
345
346			let data = dest.using_encoded(to_ascii_hex);
347			let signer = Self::eth_recover(&ethereum_signature, &data, &[][..])
348				.ok_or(Error::<T>::InvalidEthereumSignature)?;
349			ensure!(Signing::<T>::get(&signer).is_none(), Error::<T>::InvalidStatement);
350
351			Self::process_claim(signer, dest)?;
352			Ok(())
353		}
354
355		/// Mint a new claim to collect DOTs.
356		///
357		/// The dispatch origin for this call must be _Root_.
358		///
359		/// Parameters:
360		/// - `who`: The Ethereum address allowed to collect this claim.
361		/// - `value`: The number of DOTs that will be claimed.
362		/// - `vesting_schedule`: An optional vesting schedule for these DOTs.
363		///
364		/// <weight>
365		/// The weight of this call is invariant over the input parameters.
366		/// We assume worst case that both vesting and statement is being inserted.
367		///
368		/// Total Complexity: O(1)
369		/// </weight>
370		#[pallet::call_index(1)]
371		#[pallet::weight(T::WeightInfo::mint_claim())]
372		pub fn mint_claim(
373			origin: OriginFor<T>,
374			who: EthereumAddress,
375			value: BalanceOf<T>,
376			vesting_schedule: Option<(BalanceOf<T>, BalanceOf<T>, BlockNumberFor<T>)>,
377			statement: Option<StatementKind>,
378		) -> DispatchResult {
379			ensure_root(origin)?;
380
381			Total::<T>::mutate(|t| *t += value);
382			Claims::<T>::insert(who, value);
383			if let Some(vs) = vesting_schedule {
384				Vesting::<T>::insert(who, vs);
385			}
386			if let Some(s) = statement {
387				Signing::<T>::insert(who, s);
388			}
389			Ok(())
390		}
391
392		/// Make a claim to collect your DOTs by signing a statement.
393		///
394		/// The dispatch origin for this call must be _None_.
395		///
396		/// Unsigned Validation:
397		/// A call to `claim_attest` is deemed valid if the signature provided matches
398		/// the expected signed message of:
399		///
400		/// > Ethereum Signed Message:
401		/// > (configured prefix string)(address)(statement)
402		///
403		/// and `address` matches the `dest` account; the `statement` must match that which is
404		/// expected according to your purchase arrangement.
405		///
406		/// Parameters:
407		/// - `dest`: The destination account to payout the claim.
408		/// - `ethereum_signature`: The signature of an ethereum signed message matching the format
409		///   described above.
410		/// - `statement`: The identity of the statement which is being attested to in the
411		///   signature.
412		///
413		/// <weight>
414		/// The weight of this call is invariant over the input parameters.
415		/// Weight includes logic to validate unsigned `claim_attest` call.
416		///
417		/// Total Complexity: O(1)
418		/// </weight>
419		#[pallet::call_index(2)]
420		#[pallet::weight(T::WeightInfo::claim_attest())]
421		pub fn claim_attest(
422			origin: OriginFor<T>,
423			dest: T::AccountId,
424			ethereum_signature: EcdsaSignature,
425			statement: Vec<u8>,
426		) -> DispatchResult {
427			ensure_none(origin)?;
428
429			let data = dest.using_encoded(to_ascii_hex);
430			let signer = Self::eth_recover(&ethereum_signature, &data, &statement)
431				.ok_or(Error::<T>::InvalidEthereumSignature)?;
432			if let Some(s) = Signing::<T>::get(signer) {
433				ensure!(s.to_text() == &statement[..], Error::<T>::InvalidStatement);
434			}
435			Self::process_claim(signer, dest)?;
436			Ok(())
437		}
438
439		/// Attest to a statement, needed to finalize the claims process.
440		///
441		/// WARNING: Insecure unless your chain includes `PrevalidateAttests` as a
442		/// `TransactionExtension`.
443		///
444		/// Unsigned Validation:
445		/// A call to attest is deemed valid if the sender has a `Preclaim` registered
446		/// and provides a `statement` which is expected for the account.
447		///
448		/// Parameters:
449		/// - `statement`: The identity of the statement which is being attested to in the
450		///   signature.
451		///
452		/// <weight>
453		/// The weight of this call is invariant over the input parameters.
454		/// Weight includes logic to do pre-validation on `attest` call.
455		///
456		/// Total Complexity: O(1)
457		/// </weight>
458		#[pallet::call_index(3)]
459		#[pallet::weight((
460			T::WeightInfo::attest(),
461			DispatchClass::Normal,
462			Pays::No
463		))]
464		pub fn attest(origin: OriginFor<T>, statement: Vec<u8>) -> DispatchResult {
465			let who = ensure_signed(origin)?;
466			let signer = Preclaims::<T>::get(&who).ok_or(Error::<T>::SenderHasNoClaim)?;
467			if let Some(s) = Signing::<T>::get(signer) {
468				ensure!(s.to_text() == &statement[..], Error::<T>::InvalidStatement);
469			}
470			Self::process_claim(signer, who.clone())?;
471			Preclaims::<T>::remove(&who);
472			Ok(())
473		}
474
475		#[pallet::call_index(4)]
476		#[pallet::weight(T::WeightInfo::move_claim())]
477		pub fn move_claim(
478			origin: OriginFor<T>,
479			old: EthereumAddress,
480			new: EthereumAddress,
481			maybe_preclaim: Option<T::AccountId>,
482		) -> DispatchResultWithPostInfo {
483			T::MoveClaimOrigin::try_origin(origin).map(|_| ()).or_else(ensure_root)?;
484
485			Claims::<T>::take(&old).map(|c| Claims::<T>::insert(&new, c));
486			Vesting::<T>::take(&old).map(|c| Vesting::<T>::insert(&new, c));
487			Signing::<T>::take(&old).map(|c| Signing::<T>::insert(&new, c));
488			maybe_preclaim.map(|preclaim| {
489				Preclaims::<T>::mutate(&preclaim, |maybe_o| {
490					if maybe_o.as_ref().map_or(false, |o| o == &old) {
491						*maybe_o = Some(new)
492					}
493				})
494			});
495			Ok(Pays::No.into())
496		}
497	}
498
499	#[allow(deprecated)]
500	#[pallet::validate_unsigned]
501	impl<T: Config> ValidateUnsigned for Pallet<T> {
502		type Call = Call<T>;
503
504		fn validate_unsigned(_source: TransactionSource, call: &Self::Call) -> TransactionValidity {
505			const PRIORITY: u64 = 100;
506
507			let (maybe_signer, maybe_statement) = match call {
508				// <weight>
509				// The weight of this logic is included in the `claim` dispatchable.
510				// </weight>
511				Call::claim { dest: account, ethereum_signature } => {
512					let data = account.using_encoded(to_ascii_hex);
513					(Self::eth_recover(&ethereum_signature, &data, &[][..]), None)
514				},
515				// <weight>
516				// The weight of this logic is included in the `claim_attest` dispatchable.
517				// </weight>
518				Call::claim_attest { dest: account, ethereum_signature, statement } => {
519					let data = account.using_encoded(to_ascii_hex);
520					(
521						Self::eth_recover(&ethereum_signature, &data, &statement),
522						Some(statement.as_slice()),
523					)
524				},
525				_ => return Err(InvalidTransaction::Call.into()),
526			};
527
528			let signer = maybe_signer.ok_or(InvalidTransaction::Custom(
529				ValidityError::InvalidEthereumSignature.into(),
530			))?;
531
532			let e = InvalidTransaction::Custom(ValidityError::SignerHasNoClaim.into());
533			ensure!(Claims::<T>::contains_key(&signer), e);
534
535			let e = InvalidTransaction::Custom(ValidityError::InvalidStatement.into());
536			match Signing::<T>::get(signer) {
537				None => ensure!(maybe_statement.is_none(), e),
538				Some(s) => ensure!(Some(s.to_text()) == maybe_statement, e),
539			}
540
541			Ok(ValidTransaction {
542				priority: PRIORITY,
543				requires: vec![],
544				provides: vec![("claims", signer).encode()],
545				longevity: TransactionLongevity::max_value(),
546				propagate: true,
547			})
548		}
549	}
550}
551
552/// Converts the given binary data into ASCII-encoded hex. It will be twice the length.
553fn to_ascii_hex(data: &[u8]) -> Vec<u8> {
554	let mut r = Vec::with_capacity(data.len() * 2);
555	let mut push_nibble = |n| r.push(if n < 10 { b'0' + n } else { b'a' - 10 + n });
556	for &b in data.iter() {
557		push_nibble(b / 16);
558		push_nibble(b % 16);
559	}
560	r
561}
562
563impl<T: Config> Pallet<T> {
564	// Constructs the message that Ethereum RPC's `personal_sign` and `eth_sign` would sign.
565	fn ethereum_signable_message(what: &[u8], extra: &[u8]) -> Vec<u8> {
566		let prefix = T::Prefix::get();
567		let mut l = prefix.len() + what.len() + extra.len();
568		let mut rev = Vec::new();
569		while l > 0 {
570			rev.push(b'0' + (l % 10) as u8);
571			l /= 10;
572		}
573		let mut v = b"\x19Ethereum Signed Message:\n".to_vec();
574		v.extend(rev.into_iter().rev());
575		v.extend_from_slice(prefix);
576		v.extend_from_slice(what);
577		v.extend_from_slice(extra);
578		v
579	}
580
581	// Attempts to recover the Ethereum address from a message signature signed by using
582	// the Ethereum RPC's `personal_sign` and `eth_sign`.
583	fn eth_recover(s: &EcdsaSignature, what: &[u8], extra: &[u8]) -> Option<EthereumAddress> {
584		let msg = keccak_256(&Self::ethereum_signable_message(what, extra));
585		let mut res = EthereumAddress::default();
586		res.0
587			.copy_from_slice(&keccak_256(&secp256k1_ecdsa_recover(&s.0, &msg).ok()?[..])[12..]);
588		Some(res)
589	}
590
591	fn process_claim(signer: EthereumAddress, dest: T::AccountId) -> sp_runtime::DispatchResult {
592		let balance_due = Claims::<T>::get(&signer).ok_or(Error::<T>::SignerHasNoClaim)?;
593
594		let new_total =
595			Total::<T>::get().checked_sub(&balance_due).ok_or(Error::<T>::PotUnderflow)?;
596
597		let vesting = Vesting::<T>::get(&signer);
598		if vesting.is_some() && T::VestingSchedule::vesting_balance(&dest).is_some() {
599			return Err(Error::<T>::VestedBalanceExists.into());
600		}
601
602		// We first need to deposit the balance to ensure that the account exists.
603		let _ = CurrencyOf::<T>::deposit_creating(&dest, balance_due);
604
605		// Check if this claim should have a vesting schedule.
606		if let Some(vs) = vesting {
607			// This can only fail if the account already has a vesting schedule,
608			// but this is checked above.
609			T::VestingSchedule::add_vesting_schedule(&dest, vs.0, vs.1, vs.2)
610				.expect("No other vesting schedule exists, as checked above; qed");
611		}
612
613		Total::<T>::put(new_total);
614		Claims::<T>::remove(&signer);
615		Vesting::<T>::remove(&signer);
616		Signing::<T>::remove(&signer);
617
618		// Let's deposit an event to let the outside world know this happened.
619		Self::deposit_event(Event::<T>::Claimed {
620			who: dest,
621			ethereum_address: signer,
622			amount: balance_due,
623		});
624
625		Ok(())
626	}
627}
628
629/// Validate `attest` calls prior to execution. Needed to avoid a DoS attack since they are
630/// otherwise free to place on chain.
631#[derive(Encode, Decode, DecodeWithMemTracking, Clone, Eq, PartialEq, TypeInfo)]
632#[scale_info(skip_type_params(T))]
633pub struct PrevalidateAttests<T>(core::marker::PhantomData<fn(T)>);
634
635impl<T: Config> Debug for PrevalidateAttests<T>
636where
637	<T as frame_system::Config>::RuntimeCall: IsSubType<Call<T>>,
638{
639	#[cfg(feature = "std")]
640	fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
641		write!(f, "PrevalidateAttests")
642	}
643
644	#[cfg(not(feature = "std"))]
645	fn fmt(&self, _: &mut core::fmt::Formatter) -> core::fmt::Result {
646		Ok(())
647	}
648}
649
650impl<T: Config> PrevalidateAttests<T>
651where
652	<T as frame_system::Config>::RuntimeCall: IsSubType<Call<T>>,
653{
654	/// Create new `TransactionExtension` to check runtime version.
655	pub fn new() -> Self {
656		Self(core::marker::PhantomData)
657	}
658}
659
660impl<T: Config> TransactionExtension<T::RuntimeCall> for PrevalidateAttests<T>
661where
662	<T as frame_system::Config>::RuntimeCall: IsSubType<Call<T>>,
663	<<T as frame_system::Config>::RuntimeCall as Dispatchable>::RuntimeOrigin:
664		AsSystemOriginSigner<T::AccountId> + AsTransactionAuthorizedOrigin + Clone,
665{
666	const IDENTIFIER: &'static str = "PrevalidateAttests";
667	type Implicit = ();
668	type Pre = ();
669	type Val = ();
670
671	fn weight(&self, call: &T::RuntimeCall) -> Weight {
672		if let Some(Call::attest { .. }) = call.is_sub_type() {
673			T::WeightInfo::prevalidate_attests()
674		} else {
675			Weight::zero()
676		}
677	}
678
679	fn validate(
680		&self,
681		origin: <T::RuntimeCall as Dispatchable>::RuntimeOrigin,
682		call: &T::RuntimeCall,
683		_info: &DispatchInfoOf<T::RuntimeCall>,
684		_len: usize,
685		_self_implicit: Self::Implicit,
686		_inherited_implication: &impl Encode,
687		_source: TransactionSource,
688	) -> Result<
689		(ValidTransaction, Self::Val, <T::RuntimeCall as Dispatchable>::RuntimeOrigin),
690		TransactionValidityError,
691	> {
692		if let Some(Call::attest { statement: attested_statement }) = call.is_sub_type() {
693			let who = origin.as_system_origin_signer().ok_or(InvalidTransaction::BadSigner)?;
694			let signer = Preclaims::<T>::get(who)
695				.ok_or(InvalidTransaction::Custom(ValidityError::SignerHasNoClaim.into()))?;
696			if let Some(s) = Signing::<T>::get(signer) {
697				let e = InvalidTransaction::Custom(ValidityError::InvalidStatement.into());
698				ensure!(&attested_statement[..] == s.to_text(), e);
699			}
700		}
701		Ok((ValidTransaction::default(), (), origin))
702	}
703
704	impl_tx_ext_default!(T::RuntimeCall; prepare);
705}
706
707#[cfg(any(test, feature = "runtime-benchmarks"))]
708mod secp_utils {
709	use super::*;
710
711	pub fn public(secret: &libsecp256k1::SecretKey) -> libsecp256k1::PublicKey {
712		libsecp256k1::PublicKey::from_secret_key(secret)
713	}
714	pub fn eth(secret: &libsecp256k1::SecretKey) -> EthereumAddress {
715		let mut res = EthereumAddress::default();
716		res.0.copy_from_slice(&keccak_256(&public(secret).serialize()[1..65])[12..]);
717		res
718	}
719	pub fn sig<T: Config>(
720		secret: &libsecp256k1::SecretKey,
721		what: &[u8],
722		extra: &[u8],
723	) -> EcdsaSignature {
724		let msg = keccak_256(&super::Pallet::<T>::ethereum_signable_message(
725			&to_ascii_hex(what)[..],
726			extra,
727		));
728		let (sig, recovery_id) = libsecp256k1::sign(&libsecp256k1::Message::parse(&msg), secret);
729		let mut r = [0u8; 65];
730		r[0..64].copy_from_slice(&sig.serialize()[..]);
731		r[64] = recovery_id.serialize();
732		EcdsaSignature(r)
733	}
734}
735
736#[cfg(test)]
737mod mock;
738
739#[cfg(test)]
740mod tests;
741
742#[cfg(feature = "runtime-benchmarks")]
743mod benchmarking;