1
// Copyright (C) Parity Technologies (UK) Ltd.
2
// This file is part of Polkadot.
3

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

            
17
//! Pallet to process claims from Ethereum addresses.
18

            
19
#[cfg(not(feature = "std"))]
20
use alloc::{format, string::String};
21
use alloc::{vec, vec::Vec};
22
use codec::{Decode, Encode};
23
use core::fmt::Debug;
24
use frame_support::{
25
	ensure,
26
	traits::{Currency, Get, IsSubType, VestingSchedule},
27
	weights::Weight,
28
	DefaultNoBound,
29
};
30
pub use pallet::*;
31
use polkadot_primitives::ValidityError;
32
use scale_info::TypeInfo;
33
use serde::{self, Deserialize, Deserializer, Serialize, Serializer};
34
use sp_io::{crypto::secp256k1_ecdsa_recover, hashing::keccak_256};
35
use sp_runtime::{
36
	traits::{CheckedSub, DispatchInfoOf, SignedExtension, Zero},
37
	transaction_validity::{
38
		InvalidTransaction, TransactionValidity, TransactionValidityError, ValidTransaction,
39
	},
40
	RuntimeDebug,
41
};
42

            
43
type CurrencyOf<T> = <<T as Config>::VestingSchedule as VestingSchedule<
44
	<T as frame_system::Config>::AccountId,
45
>>::Currency;
46
type BalanceOf<T> = <CurrencyOf<T> as Currency<<T as frame_system::Config>::AccountId>>::Balance;
47

            
48
pub trait WeightInfo {
49
	fn claim() -> Weight;
50
	fn mint_claim() -> Weight;
51
	fn claim_attest() -> Weight;
52
	fn attest() -> Weight;
53
	fn move_claim() -> Weight;
54
}
55

            
56
pub struct TestWeightInfo;
57
impl WeightInfo for TestWeightInfo {
58
	fn claim() -> Weight {
59
		Weight::zero()
60
	}
61
	fn mint_claim() -> Weight {
62
		Weight::zero()
63
	}
64
	fn claim_attest() -> Weight {
65
		Weight::zero()
66
	}
67
	fn attest() -> Weight {
68
		Weight::zero()
69
	}
70
	fn move_claim() -> Weight {
71
		Weight::zero()
72
	}
73
}
74

            
75
/// The kind of statement an account needs to make for a claim to be valid.
76
#[derive(
77
	Encode, Decode, Clone, Copy, Eq, PartialEq, RuntimeDebug, TypeInfo, Serialize, Deserialize,
78
)]
79
pub enum StatementKind {
80
	/// Statement required to be made by non-SAFT holders.
81
	Regular,
82
	/// Statement required to be made by SAFT holders.
83
	Saft,
84
}
85

            
86
impl StatementKind {
87
	/// Convert this to the (English) statement it represents.
88
	fn to_text(self) -> &'static [u8] {
89
		match self {
90
			StatementKind::Regular =>
91
				&b"I hereby agree to the terms of the statement whose SHA-256 multihash is \
92
				Qmc1XYqT6S39WNp2UeiRUrZichUWUPpGEThDE6dAb3f6Ny. (This may be found at the URL: \
93
				https://statement.polkadot.network/regular.html)"[..],
94
			StatementKind::Saft =>
95
				&b"I hereby agree to the terms of the statement whose SHA-256 multihash is \
96
				QmXEkMahfhHJPzT3RjkXiZVFi77ZeVeuxtAjhojGRNYckz. (This may be found at the URL: \
97
				https://statement.polkadot.network/saft.html)"[..],
98
		}
99
	}
100
}
101

            
102
impl Default for StatementKind {
103
	fn default() -> Self {
104
		StatementKind::Regular
105
	}
106
}
107

            
108
/// An Ethereum address (i.e. 20 bytes, used to represent an Ethereum account).
109
///
110
/// This gets serialized to the 0x-prefixed hex representation.
111
#[derive(Clone, Copy, PartialEq, Eq, Encode, Decode, Default, RuntimeDebug, TypeInfo)]
112
pub struct EthereumAddress([u8; 20]);
113

            
114
impl Serialize for EthereumAddress {
115
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
116
	where
117
		S: Serializer,
118
	{
119
		let hex: String = rustc_hex::ToHex::to_hex(&self.0[..]);
120
		serializer.serialize_str(&format!("0x{}", hex))
121
	}
122
}
123

            
124
impl<'de> Deserialize<'de> for EthereumAddress {
125
	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
126
	where
127
		D: Deserializer<'de>,
128
	{
129
		let base_string = String::deserialize(deserializer)?;
130
		let offset = if base_string.starts_with("0x") { 2 } else { 0 };
131
		let s = &base_string[offset..];
132
		if s.len() != 40 {
133
			Err(serde::de::Error::custom(
134
				"Bad length of Ethereum address (should be 42 including '0x')",
135
			))?;
136
		}
137
		let raw: Vec<u8> = rustc_hex::FromHex::from_hex(s)
138
			.map_err(|e| serde::de::Error::custom(format!("{:?}", e)))?;
139
		let mut r = Self::default();
140
		r.0.copy_from_slice(&raw);
141
		Ok(r)
142
	}
143
}
144

            
145
#[derive(Encode, Decode, Clone, TypeInfo)]
146
pub struct EcdsaSignature(pub [u8; 65]);
147

            
148
impl PartialEq for EcdsaSignature {
149
	fn eq(&self, other: &Self) -> bool {
150
		&self.0[..] == &other.0[..]
151
	}
152
}
153

            
154
impl core::fmt::Debug for EcdsaSignature {
155
	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
156
		write!(f, "EcdsaSignature({:?})", &self.0[..])
157
	}
158
}
159

            
160
#[frame_support::pallet]
161
pub mod pallet {
162
	use super::*;
163
	use frame_support::pallet_prelude::*;
164
	use frame_system::pallet_prelude::*;
165

            
166
	#[pallet::pallet]
167
	#[pallet::without_storage_info]
168
	pub struct Pallet<T>(_);
169

            
170
	/// Configuration trait.
171
	#[pallet::config]
172
	pub trait Config: frame_system::Config {
173
		/// The overarching event type.
174
		type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
175
		type VestingSchedule: VestingSchedule<Self::AccountId, Moment = BlockNumberFor<Self>>;
176
		#[pallet::constant]
177
		type Prefix: Get<&'static [u8]>;
178
		type MoveClaimOrigin: EnsureOrigin<Self::RuntimeOrigin>;
179
		type WeightInfo: WeightInfo;
180
	}
181

            
182
	#[pallet::event]
183
	#[pallet::generate_deposit(pub(super) fn deposit_event)]
184
	pub enum Event<T: Config> {
185
		/// Someone claimed some DOTs.
186
		Claimed { who: T::AccountId, ethereum_address: EthereumAddress, amount: BalanceOf<T> },
187
	}
188

            
189
	#[pallet::error]
190
	pub enum Error<T> {
191
		/// Invalid Ethereum signature.
192
		InvalidEthereumSignature,
193
		/// Ethereum address has no claim.
194
		SignerHasNoClaim,
195
		/// Account ID sending transaction has no claim.
196
		SenderHasNoClaim,
197
		/// There's not enough in the pot to pay out some unvested amount. Generally implies a
198
		/// logic error.
199
		PotUnderflow,
200
		/// A needed statement was not included.
201
		InvalidStatement,
202
		/// The account already has a vested balance.
203
		VestedBalanceExists,
204
	}
205

            
206
	#[pallet::storage]
207
	pub type Claims<T: Config> = StorageMap<_, Identity, EthereumAddress, BalanceOf<T>>;
208

            
209
	#[pallet::storage]
210
	pub type Total<T: Config> = StorageValue<_, BalanceOf<T>, ValueQuery>;
211

            
212
	/// Vesting schedule for a claim.
213
	/// First balance is the total amount that should be held for vesting.
214
	/// Second balance is how much should be unlocked per block.
215
	/// The block number is when the vesting should start.
216
	#[pallet::storage]
217
	pub type Vesting<T: Config> =
218
		StorageMap<_, Identity, EthereumAddress, (BalanceOf<T>, BalanceOf<T>, BlockNumberFor<T>)>;
219

            
220
	/// The statement kind that must be signed, if any.
221
	#[pallet::storage]
222
	pub(super) type Signing<T> = StorageMap<_, Identity, EthereumAddress, StatementKind>;
223

            
224
	/// Pre-claimed Ethereum accounts, by the Account ID that they are claimed to.
225
	#[pallet::storage]
226
	pub(super) type Preclaims<T: Config> = StorageMap<_, Identity, T::AccountId, EthereumAddress>;
227

            
228
	#[pallet::genesis_config]
229
	#[derive(DefaultNoBound)]
230
	pub struct GenesisConfig<T: Config> {
231
		pub claims:
232
			Vec<(EthereumAddress, BalanceOf<T>, Option<T::AccountId>, Option<StatementKind>)>,
233
		pub vesting: Vec<(EthereumAddress, (BalanceOf<T>, BalanceOf<T>, BlockNumberFor<T>))>,
234
	}
235

            
236
	#[pallet::genesis_build]
237
	impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
238
		fn build(&self) {
239
			// build `Claims`
240
			self.claims.iter().map(|(a, b, _, _)| (*a, *b)).for_each(|(a, b)| {
241
				Claims::<T>::insert(a, b);
242
			});
243
			// build `Total`
244
			Total::<T>::put(
245
				self.claims
246
					.iter()
247
					.fold(Zero::zero(), |acc: BalanceOf<T>, &(_, b, _, _)| acc + b),
248
			);
249
			// build `Vesting`
250
			self.vesting.iter().for_each(|(k, v)| {
251
				Vesting::<T>::insert(k, v);
252
			});
253
			// build `Signing`
254
			self.claims
255
				.iter()
256
				.filter_map(|(a, _, _, s)| Some((*a, (*s)?)))
257
				.for_each(|(a, s)| {
258
					Signing::<T>::insert(a, s);
259
				});
260
			// build `Preclaims`
261
			self.claims.iter().filter_map(|(a, _, i, _)| Some((i.clone()?, *a))).for_each(
262
				|(i, a)| {
263
					Preclaims::<T>::insert(i, a);
264
				},
265
			);
266
		}
267
	}
268

            
269
	#[pallet::hooks]
270
	impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
271

            
272
	#[pallet::call]
273
	impl<T: Config> Pallet<T> {
274
		/// Make a claim to collect your DOTs.
275
		///
276
		/// The dispatch origin for this call must be _None_.
277
		///
278
		/// Unsigned Validation:
279
		/// A call to claim is deemed valid if the signature provided matches
280
		/// the expected signed message of:
281
		///
282
		/// > Ethereum Signed Message:
283
		/// > (configured prefix string)(address)
284
		///
285
		/// and `address` matches the `dest` account.
286
		///
287
		/// Parameters:
288
		/// - `dest`: The destination account to payout the claim.
289
		/// - `ethereum_signature`: The signature of an ethereum signed message matching the format
290
		///   described above.
291
		///
292
		/// <weight>
293
		/// The weight of this call is invariant over the input parameters.
294
		/// Weight includes logic to validate unsigned `claim` call.
295
		///
296
		/// Total Complexity: O(1)
297
		/// </weight>
298
		#[pallet::call_index(0)]
299
		#[pallet::weight(T::WeightInfo::claim())]
300
		pub fn claim(
301
			origin: OriginFor<T>,
302
			dest: T::AccountId,
303
			ethereum_signature: EcdsaSignature,
304
		) -> DispatchResult {
305
			ensure_none(origin)?;
306

            
307
			let data = dest.using_encoded(to_ascii_hex);
308
			let signer = Self::eth_recover(&ethereum_signature, &data, &[][..])
309
				.ok_or(Error::<T>::InvalidEthereumSignature)?;
310
			ensure!(Signing::<T>::get(&signer).is_none(), Error::<T>::InvalidStatement);
311

            
312
			Self::process_claim(signer, dest)?;
313
			Ok(())
314
		}
315

            
316
		/// Mint a new claim to collect DOTs.
317
		///
318
		/// The dispatch origin for this call must be _Root_.
319
		///
320
		/// Parameters:
321
		/// - `who`: The Ethereum address allowed to collect this claim.
322
		/// - `value`: The number of DOTs that will be claimed.
323
		/// - `vesting_schedule`: An optional vesting schedule for these DOTs.
324
		///
325
		/// <weight>
326
		/// The weight of this call is invariant over the input parameters.
327
		/// We assume worst case that both vesting and statement is being inserted.
328
		///
329
		/// Total Complexity: O(1)
330
		/// </weight>
331
		#[pallet::call_index(1)]
332
		#[pallet::weight(T::WeightInfo::mint_claim())]
333
		pub fn mint_claim(
334
			origin: OriginFor<T>,
335
			who: EthereumAddress,
336
			value: BalanceOf<T>,
337
			vesting_schedule: Option<(BalanceOf<T>, BalanceOf<T>, BlockNumberFor<T>)>,
338
			statement: Option<StatementKind>,
339
		) -> DispatchResult {
340
			ensure_root(origin)?;
341

            
342
			Total::<T>::mutate(|t| *t += value);
343
			Claims::<T>::insert(who, value);
344
			if let Some(vs) = vesting_schedule {
345
				Vesting::<T>::insert(who, vs);
346
			}
347
			if let Some(s) = statement {
348
				Signing::<T>::insert(who, s);
349
			}
350
			Ok(())
351
		}
352

            
353
		/// Make a claim to collect your DOTs by signing a statement.
354
		///
355
		/// The dispatch origin for this call must be _None_.
356
		///
357
		/// Unsigned Validation:
358
		/// A call to `claim_attest` is deemed valid if the signature provided matches
359
		/// the expected signed message of:
360
		///
361
		/// > Ethereum Signed Message:
362
		/// > (configured prefix string)(address)(statement)
363
		///
364
		/// and `address` matches the `dest` account; the `statement` must match that which is
365
		/// expected according to your purchase arrangement.
366
		///
367
		/// Parameters:
368
		/// - `dest`: The destination account to payout the claim.
369
		/// - `ethereum_signature`: The signature of an ethereum signed message matching the format
370
		///   described above.
371
		/// - `statement`: The identity of the statement which is being attested to in the
372
		///   signature.
373
		///
374
		/// <weight>
375
		/// The weight of this call is invariant over the input parameters.
376
		/// Weight includes logic to validate unsigned `claim_attest` call.
377
		///
378
		/// Total Complexity: O(1)
379
		/// </weight>
380
		#[pallet::call_index(2)]
381
		#[pallet::weight(T::WeightInfo::claim_attest())]
382
		pub fn claim_attest(
383
			origin: OriginFor<T>,
384
			dest: T::AccountId,
385
			ethereum_signature: EcdsaSignature,
386
			statement: Vec<u8>,
387
		) -> DispatchResult {
388
			ensure_none(origin)?;
389

            
390
			let data = dest.using_encoded(to_ascii_hex);
391
			let signer = Self::eth_recover(&ethereum_signature, &data, &statement)
392
				.ok_or(Error::<T>::InvalidEthereumSignature)?;
393
			if let Some(s) = Signing::<T>::get(signer) {
394
				ensure!(s.to_text() == &statement[..], Error::<T>::InvalidStatement);
395
			}
396
			Self::process_claim(signer, dest)?;
397
			Ok(())
398
		}
399

            
400
		/// Attest to a statement, needed to finalize the claims process.
401
		///
402
		/// WARNING: Insecure unless your chain includes `PrevalidateAttests` as a
403
		/// `SignedExtension`.
404
		///
405
		/// Unsigned Validation:
406
		/// A call to attest is deemed valid if the sender has a `Preclaim` registered
407
		/// and provides a `statement` which is expected for the account.
408
		///
409
		/// Parameters:
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 do pre-validation on `attest` call.
416
		///
417
		/// Total Complexity: O(1)
418
		/// </weight>
419
		#[pallet::call_index(3)]
420
		#[pallet::weight((
421
			T::WeightInfo::attest(),
422
			DispatchClass::Normal,
423
			Pays::No
424
		))]
425
		pub fn attest(origin: OriginFor<T>, statement: Vec<u8>) -> DispatchResult {
426
			let who = ensure_signed(origin)?;
427
			let signer = Preclaims::<T>::get(&who).ok_or(Error::<T>::SenderHasNoClaim)?;
428
			if let Some(s) = Signing::<T>::get(signer) {
429
				ensure!(s.to_text() == &statement[..], Error::<T>::InvalidStatement);
430
			}
431
			Self::process_claim(signer, who.clone())?;
432
			Preclaims::<T>::remove(&who);
433
			Ok(())
434
		}
435

            
436
		#[pallet::call_index(4)]
437
		#[pallet::weight(T::WeightInfo::move_claim())]
438
		pub fn move_claim(
439
			origin: OriginFor<T>,
440
			old: EthereumAddress,
441
			new: EthereumAddress,
442
			maybe_preclaim: Option<T::AccountId>,
443
		) -> DispatchResultWithPostInfo {
444
			T::MoveClaimOrigin::try_origin(origin).map(|_| ()).or_else(ensure_root)?;
445

            
446
			Claims::<T>::take(&old).map(|c| Claims::<T>::insert(&new, c));
447
			Vesting::<T>::take(&old).map(|c| Vesting::<T>::insert(&new, c));
448
			Signing::<T>::take(&old).map(|c| Signing::<T>::insert(&new, c));
449
			maybe_preclaim.map(|preclaim| {
450
				Preclaims::<T>::mutate(&preclaim, |maybe_o| {
451
					if maybe_o.as_ref().map_or(false, |o| o == &old) {
452
						*maybe_o = Some(new)
453
					}
454
				})
455
			});
456
			Ok(Pays::No.into())
457
		}
458
	}
459

            
460
	#[pallet::validate_unsigned]
461
	impl<T: Config> ValidateUnsigned for Pallet<T> {
462
		type Call = Call<T>;
463

            
464
		fn validate_unsigned(_source: TransactionSource, call: &Self::Call) -> TransactionValidity {
465
			const PRIORITY: u64 = 100;
466

            
467
			let (maybe_signer, maybe_statement) = match call {
468
				// <weight>
469
				// The weight of this logic is included in the `claim` dispatchable.
470
				// </weight>
471
				Call::claim { dest: account, ethereum_signature } => {
472
					let data = account.using_encoded(to_ascii_hex);
473
					(Self::eth_recover(&ethereum_signature, &data, &[][..]), None)
474
				},
475
				// <weight>
476
				// The weight of this logic is included in the `claim_attest` dispatchable.
477
				// </weight>
478
				Call::claim_attest { dest: account, ethereum_signature, statement } => {
479
					let data = account.using_encoded(to_ascii_hex);
480
					(
481
						Self::eth_recover(&ethereum_signature, &data, &statement),
482
						Some(statement.as_slice()),
483
					)
484
				},
485
				_ => return Err(InvalidTransaction::Call.into()),
486
			};
487

            
488
			let signer = maybe_signer.ok_or(InvalidTransaction::Custom(
489
				ValidityError::InvalidEthereumSignature.into(),
490
			))?;
491

            
492
			let e = InvalidTransaction::Custom(ValidityError::SignerHasNoClaim.into());
493
			ensure!(Claims::<T>::contains_key(&signer), e);
494

            
495
			let e = InvalidTransaction::Custom(ValidityError::InvalidStatement.into());
496
			match Signing::<T>::get(signer) {
497
				None => ensure!(maybe_statement.is_none(), e),
498
				Some(s) => ensure!(Some(s.to_text()) == maybe_statement, e),
499
			}
500

            
501
			Ok(ValidTransaction {
502
				priority: PRIORITY,
503
				requires: vec![],
504
				provides: vec![("claims", signer).encode()],
505
				longevity: TransactionLongevity::max_value(),
506
				propagate: true,
507
			})
508
		}
509
	}
510
}
511

            
512
/// Converts the given binary data into ASCII-encoded hex. It will be twice the length.
513
fn to_ascii_hex(data: &[u8]) -> Vec<u8> {
514
	let mut r = Vec::with_capacity(data.len() * 2);
515
	let mut push_nibble = |n| r.push(if n < 10 { b'0' + n } else { b'a' - 10 + n });
516
	for &b in data.iter() {
517
		push_nibble(b / 16);
518
		push_nibble(b % 16);
519
	}
520
	r
521
}
522

            
523
impl<T: Config> Pallet<T> {
524
	// Constructs the message that Ethereum RPC's `personal_sign` and `eth_sign` would sign.
525
	fn ethereum_signable_message(what: &[u8], extra: &[u8]) -> Vec<u8> {
526
		let prefix = T::Prefix::get();
527
		let mut l = prefix.len() + what.len() + extra.len();
528
		let mut rev = Vec::new();
529
		while l > 0 {
530
			rev.push(b'0' + (l % 10) as u8);
531
			l /= 10;
532
		}
533
		let mut v = b"\x19Ethereum Signed Message:\n".to_vec();
534
		v.extend(rev.into_iter().rev());
535
		v.extend_from_slice(prefix);
536
		v.extend_from_slice(what);
537
		v.extend_from_slice(extra);
538
		v
539
	}
540

            
541
	// Attempts to recover the Ethereum address from a message signature signed by using
542
	// the Ethereum RPC's `personal_sign` and `eth_sign`.
543
	fn eth_recover(s: &EcdsaSignature, what: &[u8], extra: &[u8]) -> Option<EthereumAddress> {
544
		let msg = keccak_256(&Self::ethereum_signable_message(what, extra));
545
		let mut res = EthereumAddress::default();
546
		res.0
547
			.copy_from_slice(&keccak_256(&secp256k1_ecdsa_recover(&s.0, &msg).ok()?[..])[12..]);
548
		Some(res)
549
	}
550

            
551
	fn process_claim(signer: EthereumAddress, dest: T::AccountId) -> sp_runtime::DispatchResult {
552
		let balance_due = Claims::<T>::get(&signer).ok_or(Error::<T>::SignerHasNoClaim)?;
553

            
554
		let new_total =
555
			Total::<T>::get().checked_sub(&balance_due).ok_or(Error::<T>::PotUnderflow)?;
556

            
557
		let vesting = Vesting::<T>::get(&signer);
558
		if vesting.is_some() && T::VestingSchedule::vesting_balance(&dest).is_some() {
559
			return Err(Error::<T>::VestedBalanceExists.into())
560
		}
561

            
562
		// We first need to deposit the balance to ensure that the account exists.
563
		let _ = CurrencyOf::<T>::deposit_creating(&dest, balance_due);
564

            
565
		// Check if this claim should have a vesting schedule.
566
		if let Some(vs) = vesting {
567
			// This can only fail if the account already has a vesting schedule,
568
			// but this is checked above.
569
			T::VestingSchedule::add_vesting_schedule(&dest, vs.0, vs.1, vs.2)
570
				.expect("No other vesting schedule exists, as checked above; qed");
571
		}
572

            
573
		Total::<T>::put(new_total);
574
		Claims::<T>::remove(&signer);
575
		Vesting::<T>::remove(&signer);
576
		Signing::<T>::remove(&signer);
577

            
578
		// Let's deposit an event to let the outside world know this happened.
579
		Self::deposit_event(Event::<T>::Claimed {
580
			who: dest,
581
			ethereum_address: signer,
582
			amount: balance_due,
583
		});
584

            
585
		Ok(())
586
	}
587
}
588

            
589
/// Validate `attest` calls prior to execution. Needed to avoid a DoS attack since they are
590
/// otherwise free to place on chain.
591
#[derive(Encode, Decode, Clone, Eq, PartialEq, TypeInfo)]
592
#[scale_info(skip_type_params(T))]
593
pub struct PrevalidateAttests<T>(core::marker::PhantomData<fn(T)>);
594

            
595
impl<T: Config> Debug for PrevalidateAttests<T>
596
where
597
	<T as frame_system::Config>::RuntimeCall: IsSubType<Call<T>>,
598
{
599
	#[cfg(feature = "std")]
600
	fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
601
		write!(f, "PrevalidateAttests")
602
	}
603

            
604
	#[cfg(not(feature = "std"))]
605
	fn fmt(&self, _: &mut core::fmt::Formatter) -> core::fmt::Result {
606
		Ok(())
607
	}
608
}
609

            
610
impl<T: Config> PrevalidateAttests<T>
611
where
612
	<T as frame_system::Config>::RuntimeCall: IsSubType<Call<T>>,
613
{
614
	/// Create new `SignedExtension` to check runtime version.
615
	pub fn new() -> Self {
616
		Self(core::marker::PhantomData)
617
	}
618
}
619

            
620
impl<T: Config> SignedExtension for PrevalidateAttests<T>
621
where
622
	<T as frame_system::Config>::RuntimeCall: IsSubType<Call<T>>,
623
{
624
	type AccountId = T::AccountId;
625
	type Call = <T as frame_system::Config>::RuntimeCall;
626
	type AdditionalSigned = ();
627
	type Pre = ();
628
	const IDENTIFIER: &'static str = "PrevalidateAttests";
629

            
630
	fn additional_signed(&self) -> Result<Self::AdditionalSigned, TransactionValidityError> {
631
		Ok(())
632
	}
633

            
634
	fn pre_dispatch(
635
		self,
636
		who: &Self::AccountId,
637
		call: &Self::Call,
638
		info: &DispatchInfoOf<Self::Call>,
639
		len: usize,
640
	) -> Result<Self::Pre, TransactionValidityError> {
641
		self.validate(who, call, info, len).map(|_| ())
642
	}
643

            
644
	// <weight>
645
	// The weight of this logic is included in the `attest` dispatchable.
646
	// </weight>
647
	fn validate(
648
		&self,
649
		who: &Self::AccountId,
650
		call: &Self::Call,
651
		_info: &DispatchInfoOf<Self::Call>,
652
		_len: usize,
653
	) -> TransactionValidity {
654
		if let Some(local_call) = call.is_sub_type() {
655
			if let Call::attest { statement: attested_statement } = local_call {
656
				let signer = Preclaims::<T>::get(who)
657
					.ok_or(InvalidTransaction::Custom(ValidityError::SignerHasNoClaim.into()))?;
658
				if let Some(s) = Signing::<T>::get(signer) {
659
					let e = InvalidTransaction::Custom(ValidityError::InvalidStatement.into());
660
					ensure!(&attested_statement[..] == s.to_text(), e);
661
				}
662
			}
663
		}
664
		Ok(ValidTransaction::default())
665
	}
666
}
667

            
668
#[cfg(any(test, feature = "runtime-benchmarks"))]
669
mod secp_utils {
670
	use super::*;
671

            
672
	pub fn public(secret: &libsecp256k1::SecretKey) -> libsecp256k1::PublicKey {
673
		libsecp256k1::PublicKey::from_secret_key(secret)
674
	}
675
	pub fn eth(secret: &libsecp256k1::SecretKey) -> EthereumAddress {
676
		let mut res = EthereumAddress::default();
677
		res.0.copy_from_slice(&keccak_256(&public(secret).serialize()[1..65])[12..]);
678
		res
679
	}
680
	pub fn sig<T: Config>(
681
		secret: &libsecp256k1::SecretKey,
682
		what: &[u8],
683
		extra: &[u8],
684
	) -> EcdsaSignature {
685
		let msg = keccak_256(&super::Pallet::<T>::ethereum_signable_message(
686
			&to_ascii_hex(what)[..],
687
			extra,
688
		));
689
		let (sig, recovery_id) = libsecp256k1::sign(&libsecp256k1::Message::parse(&msg), secret);
690
		let mut r = [0u8; 65];
691
		r[0..64].copy_from_slice(&sig.serialize()[..]);
692
		r[64] = recovery_id.serialize();
693
		EcdsaSignature(r)
694
	}
695
}
696

            
697
#[cfg(test)]
698
mod tests {
699
	use super::*;
700
	use hex_literal::hex;
701
	use secp_utils::*;
702

            
703
	use codec::Encode;
704
	// The testing primitives are very useful for avoiding having to work with signatures
705
	// or public keys. `u64` is used as the `AccountId` and no `Signature`s are required.
706
	use crate::claims;
707
	use claims::Call as ClaimsCall;
708
	use frame_support::{
709
		assert_err, assert_noop, assert_ok, derive_impl,
710
		dispatch::{GetDispatchInfo, Pays},
711
		ord_parameter_types, parameter_types,
712
		traits::{ExistenceRequirement, WithdrawReasons},
713
	};
714
	use pallet_balances;
715
	use sp_runtime::{
716
		traits::Identity, transaction_validity::TransactionLongevity, BuildStorage,
717
		DispatchError::BadOrigin, TokenError,
718
	};
719

            
720
	type Block = frame_system::mocking::MockBlock<Test>;
721

            
722
	frame_support::construct_runtime!(
723
		pub enum Test
724
		{
725
			System: frame_system,
726
			Balances: pallet_balances,
727
			Vesting: pallet_vesting,
728
			Claims: claims,
729
		}
730
	);
731

            
732
	#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
733
	impl frame_system::Config for Test {
734
		type RuntimeOrigin = RuntimeOrigin;
735
		type RuntimeCall = RuntimeCall;
736
		type Block = Block;
737
		type RuntimeEvent = RuntimeEvent;
738
		type AccountData = pallet_balances::AccountData<u64>;
739
		type MaxConsumers = frame_support::traits::ConstU32<16>;
740
	}
741

            
742
	#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
743
	impl pallet_balances::Config for Test {
744
		type AccountStore = System;
745
	}
746

            
747
	parameter_types! {
748
		pub const MinVestedTransfer: u64 = 1;
749
		pub UnvestedFundsAllowedWithdrawReasons: WithdrawReasons =
750
			WithdrawReasons::except(WithdrawReasons::TRANSFER | WithdrawReasons::RESERVE);
751
	}
752

            
753
	impl pallet_vesting::Config for Test {
754
		type RuntimeEvent = RuntimeEvent;
755
		type Currency = Balances;
756
		type BlockNumberToBalance = Identity;
757
		type MinVestedTransfer = MinVestedTransfer;
758
		type WeightInfo = ();
759
		type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
760
		type BlockNumberProvider = System;
761
		const MAX_VESTING_SCHEDULES: u32 = 28;
762
	}
763

            
764
	parameter_types! {
765
		pub Prefix: &'static [u8] = b"Pay RUSTs to the TEST account:";
766
	}
767
	ord_parameter_types! {
768
		pub const Six: u64 = 6;
769
	}
770

            
771
	impl Config for Test {
772
		type RuntimeEvent = RuntimeEvent;
773
		type VestingSchedule = Vesting;
774
		type Prefix = Prefix;
775
		type MoveClaimOrigin = frame_system::EnsureSignedBy<Six, u64>;
776
		type WeightInfo = TestWeightInfo;
777
	}
778

            
779
	fn alice() -> libsecp256k1::SecretKey {
780
		libsecp256k1::SecretKey::parse(&keccak_256(b"Alice")).unwrap()
781
	}
782
	fn bob() -> libsecp256k1::SecretKey {
783
		libsecp256k1::SecretKey::parse(&keccak_256(b"Bob")).unwrap()
784
	}
785
	fn dave() -> libsecp256k1::SecretKey {
786
		libsecp256k1::SecretKey::parse(&keccak_256(b"Dave")).unwrap()
787
	}
788
	fn eve() -> libsecp256k1::SecretKey {
789
		libsecp256k1::SecretKey::parse(&keccak_256(b"Eve")).unwrap()
790
	}
791
	fn frank() -> libsecp256k1::SecretKey {
792
		libsecp256k1::SecretKey::parse(&keccak_256(b"Frank")).unwrap()
793
	}
794

            
795
	// This function basically just builds a genesis storage key/value store according to
796
	// our desired mockup.
797
	pub fn new_test_ext() -> sp_io::TestExternalities {
798
		let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
799
		// We use default for brevity, but you can configure as desired if needed.
800
		pallet_balances::GenesisConfig::<Test>::default()
801
			.assimilate_storage(&mut t)
802
			.unwrap();
803
		claims::GenesisConfig::<Test> {
804
			claims: vec![
805
				(eth(&alice()), 100, None, None),
806
				(eth(&dave()), 200, None, Some(StatementKind::Regular)),
807
				(eth(&eve()), 300, Some(42), Some(StatementKind::Saft)),
808
				(eth(&frank()), 400, Some(43), None),
809
			],
810
			vesting: vec![(eth(&alice()), (50, 10, 1))],
811
		}
812
		.assimilate_storage(&mut t)
813
		.unwrap();
814
		t.into()
815
	}
816

            
817
	fn total_claims() -> u64 {
818
		100 + 200 + 300 + 400
819
	}
820

            
821
	#[test]
822
	fn basic_setup_works() {
823
		new_test_ext().execute_with(|| {
824
			assert_eq!(claims::Total::<Test>::get(), total_claims());
825
			assert_eq!(claims::Claims::<Test>::get(&eth(&alice())), Some(100));
826
			assert_eq!(claims::Claims::<Test>::get(&eth(&dave())), Some(200));
827
			assert_eq!(claims::Claims::<Test>::get(&eth(&eve())), Some(300));
828
			assert_eq!(claims::Claims::<Test>::get(&eth(&frank())), Some(400));
829
			assert_eq!(claims::Claims::<Test>::get(&EthereumAddress::default()), None);
830
			assert_eq!(claims::Vesting::<Test>::get(&eth(&alice())), Some((50, 10, 1)));
831
		});
832
	}
833

            
834
	#[test]
835
	fn serde_works() {
836
		let x = EthereumAddress(hex!["0123456789abcdef0123456789abcdef01234567"]);
837
		let y = serde_json::to_string(&x).unwrap();
838
		assert_eq!(y, "\"0x0123456789abcdef0123456789abcdef01234567\"");
839
		let z: EthereumAddress = serde_json::from_str(&y).unwrap();
840
		assert_eq!(x, z);
841
	}
842

            
843
	#[test]
844
	fn claiming_works() {
845
		new_test_ext().execute_with(|| {
846
			assert_eq!(Balances::free_balance(42), 0);
847
			assert_ok!(Claims::claim(
848
				RuntimeOrigin::none(),
849
				42,
850
				sig::<Test>(&alice(), &42u64.encode(), &[][..])
851
			));
852
			assert_eq!(Balances::free_balance(&42), 100);
853
			assert_eq!(Vesting::vesting_balance(&42), Some(50));
854
			assert_eq!(claims::Total::<Test>::get(), total_claims() - 100);
855
		});
856
	}
857

            
858
	#[test]
859
	fn basic_claim_moving_works() {
860
		new_test_ext().execute_with(|| {
861
			assert_eq!(Balances::free_balance(42), 0);
862
			assert_noop!(
863
				Claims::move_claim(RuntimeOrigin::signed(1), eth(&alice()), eth(&bob()), None),
864
				BadOrigin
865
			);
866
			assert_ok!(Claims::move_claim(
867
				RuntimeOrigin::signed(6),
868
				eth(&alice()),
869
				eth(&bob()),
870
				None
871
			));
872
			assert_noop!(
873
				Claims::claim(
874
					RuntimeOrigin::none(),
875
					42,
876
					sig::<Test>(&alice(), &42u64.encode(), &[][..])
877
				),
878
				Error::<Test>::SignerHasNoClaim
879
			);
880
			assert_ok!(Claims::claim(
881
				RuntimeOrigin::none(),
882
				42,
883
				sig::<Test>(&bob(), &42u64.encode(), &[][..])
884
			));
885
			assert_eq!(Balances::free_balance(&42), 100);
886
			assert_eq!(Vesting::vesting_balance(&42), Some(50));
887
			assert_eq!(claims::Total::<Test>::get(), total_claims() - 100);
888
		});
889
	}
890

            
891
	#[test]
892
	fn claim_attest_moving_works() {
893
		new_test_ext().execute_with(|| {
894
			assert_ok!(Claims::move_claim(
895
				RuntimeOrigin::signed(6),
896
				eth(&dave()),
897
				eth(&bob()),
898
				None
899
			));
900
			let s = sig::<Test>(&bob(), &42u64.encode(), StatementKind::Regular.to_text());
901
			assert_ok!(Claims::claim_attest(
902
				RuntimeOrigin::none(),
903
				42,
904
				s,
905
				StatementKind::Regular.to_text().to_vec()
906
			));
907
			assert_eq!(Balances::free_balance(&42), 200);
908
		});
909
	}
910

            
911
	#[test]
912
	fn attest_moving_works() {
913
		new_test_ext().execute_with(|| {
914
			assert_ok!(Claims::move_claim(
915
				RuntimeOrigin::signed(6),
916
				eth(&eve()),
917
				eth(&bob()),
918
				Some(42)
919
			));
920
			assert_ok!(Claims::attest(
921
				RuntimeOrigin::signed(42),
922
				StatementKind::Saft.to_text().to_vec()
923
			));
924
			assert_eq!(Balances::free_balance(&42), 300);
925
		});
926
	}
927

            
928
	#[test]
929
	fn claiming_does_not_bypass_signing() {
930
		new_test_ext().execute_with(|| {
931
			assert_ok!(Claims::claim(
932
				RuntimeOrigin::none(),
933
				42,
934
				sig::<Test>(&alice(), &42u64.encode(), &[][..])
935
			));
936
			assert_noop!(
937
				Claims::claim(
938
					RuntimeOrigin::none(),
939
					42,
940
					sig::<Test>(&dave(), &42u64.encode(), &[][..])
941
				),
942
				Error::<Test>::InvalidStatement,
943
			);
944
			assert_noop!(
945
				Claims::claim(
946
					RuntimeOrigin::none(),
947
					42,
948
					sig::<Test>(&eve(), &42u64.encode(), &[][..])
949
				),
950
				Error::<Test>::InvalidStatement,
951
			);
952
			assert_ok!(Claims::claim(
953
				RuntimeOrigin::none(),
954
				42,
955
				sig::<Test>(&frank(), &42u64.encode(), &[][..])
956
			));
957
		});
958
	}
959

            
960
	#[test]
961
	fn attest_claiming_works() {
962
		new_test_ext().execute_with(|| {
963
			assert_eq!(Balances::free_balance(42), 0);
964
			let s = sig::<Test>(&dave(), &42u64.encode(), StatementKind::Saft.to_text());
965
			let r = Claims::claim_attest(
966
				RuntimeOrigin::none(),
967
				42,
968
				s.clone(),
969
				StatementKind::Saft.to_text().to_vec(),
970
			);
971
			assert_noop!(r, Error::<Test>::InvalidStatement);
972

            
973
			let r = Claims::claim_attest(
974
				RuntimeOrigin::none(),
975
				42,
976
				s,
977
				StatementKind::Regular.to_text().to_vec(),
978
			);
979
			assert_noop!(r, Error::<Test>::SignerHasNoClaim);
980
			// ^^^ we use ecdsa_recover, so an invalid signature just results in a random signer id
981
			// being recovered, which realistically will never have a claim.
982

            
983
			let s = sig::<Test>(&dave(), &42u64.encode(), StatementKind::Regular.to_text());
984
			assert_ok!(Claims::claim_attest(
985
				RuntimeOrigin::none(),
986
				42,
987
				s,
988
				StatementKind::Regular.to_text().to_vec()
989
			));
990
			assert_eq!(Balances::free_balance(&42), 200);
991
			assert_eq!(claims::Total::<Test>::get(), total_claims() - 200);
992

            
993
			let s = sig::<Test>(&dave(), &42u64.encode(), StatementKind::Regular.to_text());
994
			let r = Claims::claim_attest(
995
				RuntimeOrigin::none(),
996
				42,
997
				s,
998
				StatementKind::Regular.to_text().to_vec(),
999
			);
			assert_noop!(r, Error::<Test>::SignerHasNoClaim);
		});
	}
	#[test]
	fn attesting_works() {
		new_test_ext().execute_with(|| {
			assert_eq!(Balances::free_balance(42), 0);
			assert_noop!(
				Claims::attest(RuntimeOrigin::signed(69), StatementKind::Saft.to_text().to_vec()),
				Error::<Test>::SenderHasNoClaim
			);
			assert_noop!(
				Claims::attest(
					RuntimeOrigin::signed(42),
					StatementKind::Regular.to_text().to_vec()
				),
				Error::<Test>::InvalidStatement
			);
			assert_ok!(Claims::attest(
				RuntimeOrigin::signed(42),
				StatementKind::Saft.to_text().to_vec()
			));
			assert_eq!(Balances::free_balance(&42), 300);
			assert_eq!(claims::Total::<Test>::get(), total_claims() - 300);
		});
	}
	#[test]
	fn claim_cannot_clobber_preclaim() {
		new_test_ext().execute_with(|| {
			assert_eq!(Balances::free_balance(42), 0);
			// Alice's claim is 100
			assert_ok!(Claims::claim(
				RuntimeOrigin::none(),
				42,
				sig::<Test>(&alice(), &42u64.encode(), &[][..])
			));
			assert_eq!(Balances::free_balance(&42), 100);
			// Eve's claim is 300 through Account 42
			assert_ok!(Claims::attest(
				RuntimeOrigin::signed(42),
				StatementKind::Saft.to_text().to_vec()
			));
			assert_eq!(Balances::free_balance(&42), 100 + 300);
			assert_eq!(claims::Total::<Test>::get(), total_claims() - 400);
		});
	}
	#[test]
	fn valid_attest_transactions_are_free() {
		new_test_ext().execute_with(|| {
			let p = PrevalidateAttests::<Test>::new();
			let c = RuntimeCall::Claims(ClaimsCall::attest {
				statement: StatementKind::Saft.to_text().to_vec(),
			});
			let di = c.get_dispatch_info();
			assert_eq!(di.pays_fee, Pays::No);
			let r = p.validate(&42, &c, &di, 20);
			assert_eq!(r, TransactionValidity::Ok(ValidTransaction::default()));
		});
	}
	#[test]
	fn invalid_attest_transactions_are_recognized() {
		new_test_ext().execute_with(|| {
			let p = PrevalidateAttests::<Test>::new();
			let c = RuntimeCall::Claims(ClaimsCall::attest {
				statement: StatementKind::Regular.to_text().to_vec(),
			});
			let di = c.get_dispatch_info();
			let r = p.validate(&42, &c, &di, 20);
			assert!(r.is_err());
			let c = RuntimeCall::Claims(ClaimsCall::attest {
				statement: StatementKind::Saft.to_text().to_vec(),
			});
			let di = c.get_dispatch_info();
			let r = p.validate(&69, &c, &di, 20);
			assert!(r.is_err());
		});
	}
	#[test]
	fn cannot_bypass_attest_claiming() {
		new_test_ext().execute_with(|| {
			assert_eq!(Balances::free_balance(42), 0);
			let s = sig::<Test>(&dave(), &42u64.encode(), &[]);
			let r = Claims::claim(RuntimeOrigin::none(), 42, s.clone());
			assert_noop!(r, Error::<Test>::InvalidStatement);
		});
	}
	#[test]
	fn add_claim_works() {
		new_test_ext().execute_with(|| {
			assert_noop!(
				Claims::mint_claim(RuntimeOrigin::signed(42), eth(&bob()), 200, None, None),
				sp_runtime::traits::BadOrigin,
			);
			assert_eq!(Balances::free_balance(42), 0);
			assert_noop!(
				Claims::claim(
					RuntimeOrigin::none(),
					69,
					sig::<Test>(&bob(), &69u64.encode(), &[][..])
				),
				Error::<Test>::SignerHasNoClaim,
			);
			assert_ok!(Claims::mint_claim(RuntimeOrigin::root(), eth(&bob()), 200, None, None));
			assert_eq!(claims::Total::<Test>::get(), total_claims() + 200);
			assert_ok!(Claims::claim(
				RuntimeOrigin::none(),
				69,
				sig::<Test>(&bob(), &69u64.encode(), &[][..])
			));
			assert_eq!(Balances::free_balance(&69), 200);
			assert_eq!(Vesting::vesting_balance(&69), None);
			assert_eq!(claims::Total::<Test>::get(), total_claims());
		});
	}
	#[test]
	fn add_claim_with_vesting_works() {
		new_test_ext().execute_with(|| {
			assert_noop!(
				Claims::mint_claim(
					RuntimeOrigin::signed(42),
					eth(&bob()),
					200,
					Some((50, 10, 1)),
					None
				),
				sp_runtime::traits::BadOrigin,
			);
			assert_eq!(Balances::free_balance(42), 0);
			assert_noop!(
				Claims::claim(
					RuntimeOrigin::none(),
					69,
					sig::<Test>(&bob(), &69u64.encode(), &[][..])
				),
				Error::<Test>::SignerHasNoClaim,
			);
			assert_ok!(Claims::mint_claim(
				RuntimeOrigin::root(),
				eth(&bob()),
				200,
				Some((50, 10, 1)),
				None
			));
			assert_ok!(Claims::claim(
				RuntimeOrigin::none(),
				69,
				sig::<Test>(&bob(), &69u64.encode(), &[][..])
			));
			assert_eq!(Balances::free_balance(&69), 200);
			assert_eq!(Vesting::vesting_balance(&69), Some(50));
			// Make sure we can not transfer the vested balance.
			assert_err!(
				<Balances as Currency<_>>::transfer(
					&69,
					&80,
					180,
					ExistenceRequirement::AllowDeath
				),
				TokenError::Frozen,
			);
		});
	}
	#[test]
	fn add_claim_with_statement_works() {
		new_test_ext().execute_with(|| {
			assert_noop!(
				Claims::mint_claim(
					RuntimeOrigin::signed(42),
					eth(&bob()),
					200,
					None,
					Some(StatementKind::Regular)
				),
				sp_runtime::traits::BadOrigin,
			);
			assert_eq!(Balances::free_balance(42), 0);
			let signature = sig::<Test>(&bob(), &69u64.encode(), StatementKind::Regular.to_text());
			assert_noop!(
				Claims::claim_attest(
					RuntimeOrigin::none(),
					69,
					signature.clone(),
					StatementKind::Regular.to_text().to_vec()
				),
				Error::<Test>::SignerHasNoClaim
			);
			assert_ok!(Claims::mint_claim(
				RuntimeOrigin::root(),
				eth(&bob()),
				200,
				None,
				Some(StatementKind::Regular)
			));
			assert_noop!(
				Claims::claim_attest(RuntimeOrigin::none(), 69, signature.clone(), vec![],),
				Error::<Test>::SignerHasNoClaim
			);
			assert_ok!(Claims::claim_attest(
				RuntimeOrigin::none(),
				69,
				signature.clone(),
				StatementKind::Regular.to_text().to_vec()
			));
			assert_eq!(Balances::free_balance(&69), 200);
		});
	}
	#[test]
	fn origin_signed_claiming_fail() {
		new_test_ext().execute_with(|| {
			assert_eq!(Balances::free_balance(42), 0);
			assert_err!(
				Claims::claim(
					RuntimeOrigin::signed(42),
					42,
					sig::<Test>(&alice(), &42u64.encode(), &[][..])
				),
				sp_runtime::traits::BadOrigin,
			);
		});
	}
	#[test]
	fn double_claiming_doesnt_work() {
		new_test_ext().execute_with(|| {
			assert_eq!(Balances::free_balance(42), 0);
			assert_ok!(Claims::claim(
				RuntimeOrigin::none(),
				42,
				sig::<Test>(&alice(), &42u64.encode(), &[][..])
			));
			assert_noop!(
				Claims::claim(
					RuntimeOrigin::none(),
					42,
					sig::<Test>(&alice(), &42u64.encode(), &[][..])
				),
				Error::<Test>::SignerHasNoClaim
			);
		});
	}
	#[test]
	fn claiming_while_vested_doesnt_work() {
		new_test_ext().execute_with(|| {
			CurrencyOf::<Test>::make_free_balance_be(&69, total_claims());
			assert_eq!(Balances::free_balance(69), total_claims());
			// A user is already vested
			assert_ok!(<Test as Config>::VestingSchedule::add_vesting_schedule(
				&69,
				total_claims(),
				100,
				10
			));
			assert_ok!(Claims::mint_claim(
				RuntimeOrigin::root(),
				eth(&bob()),
				200,
				Some((50, 10, 1)),
				None
			));
			// New total
			assert_eq!(claims::Total::<Test>::get(), total_claims() + 200);
			// They should not be able to claim
			assert_noop!(
				Claims::claim(
					RuntimeOrigin::none(),
					69,
					sig::<Test>(&bob(), &69u64.encode(), &[][..])
				),
				Error::<Test>::VestedBalanceExists,
			);
		});
	}
	#[test]
	fn non_sender_sig_doesnt_work() {
		new_test_ext().execute_with(|| {
			assert_eq!(Balances::free_balance(42), 0);
			assert_noop!(
				Claims::claim(
					RuntimeOrigin::none(),
					42,
					sig::<Test>(&alice(), &69u64.encode(), &[][..])
				),
				Error::<Test>::SignerHasNoClaim
			);
		});
	}
	#[test]
	fn non_claimant_doesnt_work() {
		new_test_ext().execute_with(|| {
			assert_eq!(Balances::free_balance(42), 0);
			assert_noop!(
				Claims::claim(
					RuntimeOrigin::none(),
					42,
					sig::<Test>(&bob(), &69u64.encode(), &[][..])
				),
				Error::<Test>::SignerHasNoClaim
			);
		});
	}
	#[test]
	fn real_eth_sig_works() {
		new_test_ext().execute_with(|| {
			// "Pay RUSTs to the TEST account:2a00000000000000"
			let sig = hex!["444023e89b67e67c0562ed0305d252a5dd12b2af5ac51d6d3cb69a0b486bc4b3191401802dc29d26d586221f7256cd3329fe82174bdf659baea149a40e1c495d1c"];
			let sig = EcdsaSignature(sig);
			let who = 42u64.using_encoded(to_ascii_hex);
			let signer = Claims::eth_recover(&sig, &who, &[][..]).unwrap();
			assert_eq!(signer.0, hex!["6d31165d5d932d571f3b44695653b46dcc327e84"]);
		});
	}
	#[test]
	fn validate_unsigned_works() {
		use sp_runtime::traits::ValidateUnsigned;
		let source = sp_runtime::transaction_validity::TransactionSource::External;
		new_test_ext().execute_with(|| {
			assert_eq!(
				Pallet::<Test>::validate_unsigned(
					source,
					&ClaimsCall::claim {
						dest: 1,
						ethereum_signature: sig::<Test>(&alice(), &1u64.encode(), &[][..])
					}
				),
				Ok(ValidTransaction {
					priority: 100,
					requires: vec![],
					provides: vec![("claims", eth(&alice())).encode()],
					longevity: TransactionLongevity::max_value(),
					propagate: true,
				})
			);
			assert_eq!(
				Pallet::<Test>::validate_unsigned(
					source,
					&ClaimsCall::claim { dest: 0, ethereum_signature: EcdsaSignature([0; 65]) }
				),
				InvalidTransaction::Custom(ValidityError::InvalidEthereumSignature.into()).into(),
			);
			assert_eq!(
				Pallet::<Test>::validate_unsigned(
					source,
					&ClaimsCall::claim {
						dest: 1,
						ethereum_signature: sig::<Test>(&bob(), &1u64.encode(), &[][..])
					}
				),
				InvalidTransaction::Custom(ValidityError::SignerHasNoClaim.into()).into(),
			);
			let s = sig::<Test>(&dave(), &1u64.encode(), StatementKind::Regular.to_text());
			let call = ClaimsCall::claim_attest {
				dest: 1,
				ethereum_signature: s,
				statement: StatementKind::Regular.to_text().to_vec(),
			};
			assert_eq!(
				Pallet::<Test>::validate_unsigned(source, &call),
				Ok(ValidTransaction {
					priority: 100,
					requires: vec![],
					provides: vec![("claims", eth(&dave())).encode()],
					longevity: TransactionLongevity::max_value(),
					propagate: true,
				})
			);
			assert_eq!(
				Pallet::<Test>::validate_unsigned(
					source,
					&ClaimsCall::claim_attest {
						dest: 1,
						ethereum_signature: EcdsaSignature([0; 65]),
						statement: StatementKind::Regular.to_text().to_vec()
					}
				),
				InvalidTransaction::Custom(ValidityError::InvalidEthereumSignature.into()).into(),
			);
			let s = sig::<Test>(&bob(), &1u64.encode(), StatementKind::Regular.to_text());
			let call = ClaimsCall::claim_attest {
				dest: 1,
				ethereum_signature: s,
				statement: StatementKind::Regular.to_text().to_vec(),
			};
			assert_eq!(
				Pallet::<Test>::validate_unsigned(source, &call),
				InvalidTransaction::Custom(ValidityError::SignerHasNoClaim.into()).into(),
			);
			let s = sig::<Test>(&dave(), &1u64.encode(), StatementKind::Saft.to_text());
			let call = ClaimsCall::claim_attest {
				dest: 1,
				ethereum_signature: s,
				statement: StatementKind::Regular.to_text().to_vec(),
			};
			assert_eq!(
				Pallet::<Test>::validate_unsigned(source, &call),
				InvalidTransaction::Custom(ValidityError::SignerHasNoClaim.into()).into(),
			);
			let s = sig::<Test>(&dave(), &1u64.encode(), StatementKind::Saft.to_text());
			let call = ClaimsCall::claim_attest {
				dest: 1,
				ethereum_signature: s,
				statement: StatementKind::Saft.to_text().to_vec(),
			};
			assert_eq!(
				Pallet::<Test>::validate_unsigned(source, &call),
				InvalidTransaction::Custom(ValidityError::InvalidStatement.into()).into(),
			);
		});
	}
}
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking {
	use super::*;
	use crate::claims::Call;
	use frame_benchmarking::{account, benchmarks};
	use frame_support::traits::UnfilteredDispatchable;
	use frame_system::RawOrigin;
	use secp_utils::*;
	use sp_runtime::{traits::ValidateUnsigned, DispatchResult};
	const SEED: u32 = 0;
	const MAX_CLAIMS: u32 = 10_000;
	const VALUE: u32 = 1_000_000;
	fn create_claim<T: Config>(input: u32) -> DispatchResult {
		let secret_key = libsecp256k1::SecretKey::parse(&keccak_256(&input.encode())).unwrap();
		let eth_address = eth(&secret_key);
		let vesting = Some((100_000u32.into(), 1_000u32.into(), 100u32.into()));
		super::Pallet::<T>::mint_claim(
			RawOrigin::Root.into(),
			eth_address,
			VALUE.into(),
			vesting,
			None,
		)?;
		Ok(())
	}
	fn create_claim_attest<T: Config>(input: u32) -> DispatchResult {
		let secret_key = libsecp256k1::SecretKey::parse(&keccak_256(&input.encode())).unwrap();
		let eth_address = eth(&secret_key);
		let vesting = Some((100_000u32.into(), 1_000u32.into(), 100u32.into()));
		super::Pallet::<T>::mint_claim(
			RawOrigin::Root.into(),
			eth_address,
			VALUE.into(),
			vesting,
			Some(Default::default()),
		)?;
		Ok(())
	}
	benchmarks! {
		// Benchmark `claim` including `validate_unsigned` logic.
		claim {
			let c = MAX_CLAIMS;
			for i in 0 .. c / 2 {
				create_claim::<T>(c)?;
				create_claim_attest::<T>(u32::MAX - c)?;
			}
			let secret_key = libsecp256k1::SecretKey::parse(&keccak_256(&c.encode())).unwrap();
			let eth_address = eth(&secret_key);
			let account: T::AccountId = account("user", c, SEED);
			let vesting = Some((100_000u32.into(), 1_000u32.into(), 100u32.into()));
			let signature = sig::<T>(&secret_key, &account.encode(), &[][..]);
			super::Pallet::<T>::mint_claim(RawOrigin::Root.into(), eth_address, VALUE.into(), vesting, None)?;
			assert_eq!(Claims::<T>::get(eth_address), Some(VALUE.into()));
			let source = sp_runtime::transaction_validity::TransactionSource::External;
			let call_enc = Call::<T>::claim {
				dest: account.clone(),
				ethereum_signature: signature.clone()
			}.encode();
		}: {
			let call = <Call<T> as Decode>::decode(&mut &*call_enc)
				.expect("call is encoded above, encoding must be correct");
			super::Pallet::<T>::validate_unsigned(source, &call).map_err(|e| -> &'static str { e.into() })?;
			call.dispatch_bypass_filter(RawOrigin::None.into())?;
		}
		verify {
			assert_eq!(Claims::<T>::get(eth_address), None);
		}
		// Benchmark `mint_claim` when there already exists `c` claims in storage.
		mint_claim {
			let c = MAX_CLAIMS;
			for i in 0 .. c / 2 {
				create_claim::<T>(c)?;
				create_claim_attest::<T>(u32::MAX - c)?;
			}
			let eth_address = account("eth_address", 0, SEED);
			let vesting = Some((100_000u32.into(), 1_000u32.into(), 100u32.into()));
			let statement = StatementKind::Regular;
		}: _(RawOrigin::Root, eth_address, VALUE.into(), vesting, Some(statement))
		verify {
			assert_eq!(Claims::<T>::get(eth_address), Some(VALUE.into()));
		}
		// Benchmark `claim_attest` including `validate_unsigned` logic.
		claim_attest {
			let c = MAX_CLAIMS;
			for i in 0 .. c / 2 {
				create_claim::<T>(c)?;
				create_claim_attest::<T>(u32::MAX - c)?;
			}
			// Crate signature
			let attest_c = u32::MAX - c;
			let secret_key = libsecp256k1::SecretKey::parse(&keccak_256(&attest_c.encode())).unwrap();
			let eth_address = eth(&secret_key);
			let account: T::AccountId = account("user", c, SEED);
			let vesting = Some((100_000u32.into(), 1_000u32.into(), 100u32.into()));
			let statement = StatementKind::Regular;
			let signature = sig::<T>(&secret_key, &account.encode(), statement.to_text());
			super::Pallet::<T>::mint_claim(RawOrigin::Root.into(), eth_address, VALUE.into(), vesting, Some(statement))?;
			assert_eq!(Claims::<T>::get(eth_address), Some(VALUE.into()));
			let call_enc = Call::<T>::claim_attest {
				dest: account.clone(),
				ethereum_signature: signature.clone(),
				statement: StatementKind::Regular.to_text().to_vec()
			}.encode();
			let source = sp_runtime::transaction_validity::TransactionSource::External;
		}: {
			let call = <Call<T> as Decode>::decode(&mut &*call_enc)
				.expect("call is encoded above, encoding must be correct");
			super::Pallet::<T>::validate_unsigned(source, &call).map_err(|e| -> &'static str { e.into() })?;
			call.dispatch_bypass_filter(RawOrigin::None.into())?;
		}
		verify {
			assert_eq!(Claims::<T>::get(eth_address), None);
		}
		// Benchmark `attest` including prevalidate logic.
		attest {
			let c = MAX_CLAIMS;
			for i in 0 .. c / 2 {
				create_claim::<T>(c)?;
				create_claim_attest::<T>(u32::MAX - c)?;
			}
			let attest_c = u32::MAX - c;
			let secret_key = libsecp256k1::SecretKey::parse(&keccak_256(&attest_c.encode())).unwrap();
			let eth_address = eth(&secret_key);
			let account: T::AccountId = account("user", c, SEED);
			let vesting = Some((100_000u32.into(), 1_000u32.into(), 100u32.into()));
			let statement = StatementKind::Regular;
			let signature = sig::<T>(&secret_key, &account.encode(), statement.to_text());
			super::Pallet::<T>::mint_claim(RawOrigin::Root.into(), eth_address, VALUE.into(), vesting, Some(statement))?;
			Preclaims::<T>::insert(&account, eth_address);
			assert_eq!(Claims::<T>::get(eth_address), Some(VALUE.into()));
			let call = super::Call::<T>::attest { statement: StatementKind::Regular.to_text().to_vec() };
			// We have to copy the validate statement here because of trait issues... :(
			let validate = |who: &T::AccountId, call: &super::Call<T>| -> DispatchResult {
				if let Call::attest{ statement: attested_statement } = call {
					let signer = Preclaims::<T>::get(who).ok_or("signer has no claim")?;
					if let Some(s) = Signing::<T>::get(signer) {
						ensure!(&attested_statement[..] == s.to_text(), "invalid statement");
					}
				}
				Ok(())
			};
			let call_enc = call.encode();
		}: {
			let call = <Call<T> as Decode>::decode(&mut &*call_enc)
				.expect("call is encoded above, encoding must be correct");
			validate(&account, &call)?;
			call.dispatch_bypass_filter(RawOrigin::Signed(account).into())?;
		}
		verify {
			assert_eq!(Claims::<T>::get(eth_address), None);
		}
		move_claim {
			let c = MAX_CLAIMS;
			for i in 0 .. c / 2 {
				create_claim::<T>(c)?;
				create_claim_attest::<T>(u32::MAX - c)?;
			}
			let attest_c = u32::MAX - c;
			let secret_key = libsecp256k1::SecretKey::parse(&keccak_256(&attest_c.encode())).unwrap();
			let eth_address = eth(&secret_key);
			let new_secret_key = libsecp256k1::SecretKey::parse(&keccak_256(&(u32::MAX/2).encode())).unwrap();
			let new_eth_address = eth(&new_secret_key);
			let account: T::AccountId = account("user", c, SEED);
			Preclaims::<T>::insert(&account, eth_address);
			assert!(Claims::<T>::contains_key(eth_address));
			assert!(!Claims::<T>::contains_key(new_eth_address));
		}: _(RawOrigin::Root, eth_address, new_eth_address, Some(account))
		verify {
			assert!(!Claims::<T>::contains_key(eth_address));
			assert!(Claims::<T>::contains_key(new_eth_address));
		}
		// Benchmark the time it takes to do `repeat` number of keccak256 hashes
		#[extra]
		keccak256 {
			let i in 0 .. 10_000;
			let bytes = (i).encode();
		}: {
			for index in 0 .. i {
				let _hash = keccak_256(&bytes);
			}
		}
		// Benchmark the time it takes to do `repeat` number of `eth_recover`
		#[extra]
		eth_recover {
			let i in 0 .. 1_000;
			// Crate signature
			let secret_key = libsecp256k1::SecretKey::parse(&keccak_256(&i.encode())).unwrap();
			let account: T::AccountId = account("user", i, SEED);
			let signature = sig::<T>(&secret_key, &account.encode(), &[][..]);
			let data = account.using_encoded(to_ascii_hex);
			let extra = StatementKind::default().to_text();
		}: {
			for _ in 0 .. i {
				assert!(super::Pallet::<T>::eth_recover(&signature, &data, extra).is_some());
			}
		}
		impl_benchmark_test_suite!(
			Pallet,
			crate::claims::tests::new_test_ext(),
			crate::claims::tests::Test,
		);
	}
}