claims.rs 18.8 KB
Newer Older
Shawn Tabrizi's avatar
Shawn Tabrizi committed
1
// Copyright 2017-2020 Parity Technologies (UK) Ltd.
2
// This file is part of Polkadot.
Gav Wood's avatar
Gav Wood committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

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

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

//! Module to process claims from Ethereum addresses.

use rstd::prelude::*;
20
use sp_io::{hashing::keccak_256, crypto::secp256k1_ecdsa_recover};
21
22
use frame_support::{decl_event, decl_storage, decl_module, decl_error};
use frame_support::weights::SimpleDispatchInfo;
Gavin Wood's avatar
Gavin Wood committed
23
use frame_support::traits::{Currency, Get, VestingSchedule};
Gavin Wood's avatar
Gavin Wood committed
24
use system::{ensure_root, ensure_none};
25
use codec::{Encode, Decode};
Gav Wood's avatar
Gav Wood committed
26
#[cfg(feature = "std")]
27
use serde::{self, Serialize, Deserialize, Serializer, Deserializer};
Gavin Wood's avatar
Gavin Wood committed
28
use sp_runtime::traits::{Zero, CheckedSub};
29
use sp_runtime::{
30
	RuntimeDebug, transaction_validity::{
Gavin Wood's avatar
Gavin Wood committed
31
32
		TransactionLongevity, TransactionValidity, ValidTransaction, InvalidTransaction
	},
33
};
Gavin Wood's avatar
Gavin Wood committed
34
use primitives::ValidityError;
35
36
use system;

Gavin Wood's avatar
Gavin Wood committed
37
38
type CurrencyOf<T> = <<T as Trait>::VestingSchedule as VestingSchedule<<T as system::Trait>::AccountId>>::Currency;
type BalanceOf<T> = <CurrencyOf<T> as Currency<<T as system::Trait>::AccountId>>::Balance;
Gav Wood's avatar
Gav Wood committed
39
40

/// Configuration trait.
41
pub trait Trait: system::Trait {
Gav Wood's avatar
Gav Wood committed
42
43
	/// The overarching event type.
	type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
Gavin Wood's avatar
Gavin Wood committed
44
	type VestingSchedule: VestingSchedule<Self::AccountId, Moment=Self::BlockNumber>;
45
	type Prefix: Get<&'static [u8]>;
Gav Wood's avatar
Gav Wood committed
46
47
}

48
49
50
/// An Ethereum address (i.e. 20 bytes, used to represent an Ethereum account).
///
/// This gets serialized to the 0x-prefixed hex representation.
Gavin Wood's avatar
Gavin Wood committed
51
#[derive(Clone, Copy, PartialEq, Eq, Encode, Decode, Default, RuntimeDebug)]
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
pub struct EthereumAddress([u8; 20]);

#[cfg(feature = "std")]
impl Serialize for EthereumAddress {
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
		let hex: String = rustc_hex::ToHex::to_hex(&self.0[..]);
		serializer.serialize_str(&format!("0x{}", hex))
	}
}

#[cfg(feature = "std")]
impl<'de> Deserialize<'de> for EthereumAddress {
	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
		let base_string = String::deserialize(deserializer)?;
		let offset = if base_string.starts_with("0x") { 2 } else { 0 };
		let s = &base_string[offset..];
		if s.len() != 40 {
			Err(serde::de::Error::custom("Bad length of Ethereum address (should be 42 including '0x')"))?;
		}
		let raw: Vec<u8> = rustc_hex::FromHex::from_hex(s)
			.map_err(|e| serde::de::Error::custom(format!("{:?}", e)))?;
		let mut r = Self::default();
		r.0.copy_from_slice(&raw);
		Ok(r)
	}
}
78

79
80
#[derive(Encode, Decode, Clone)]
pub struct EcdsaSignature(pub [u8; 65]);
81

82
83
84
impl PartialEq for EcdsaSignature {
	fn eq(&self, other: &Self) -> bool {
		&self.0[..] == &other.0[..]
85
86
	}
}
Gav Wood's avatar
Gav Wood committed
87

88
89
90
91
impl rstd::fmt::Debug for EcdsaSignature {
	fn fmt(&self, f: &mut rstd::fmt::Formatter<'_>) -> rstd::fmt::Result {
		write!(f, "EcdsaSignature({:?})", &self.0[..])
	}
92
93
}

Gav Wood's avatar
Gav Wood committed
94
95
decl_event!(
	pub enum Event<T> where
96
97
		Balance = BalanceOf<T>,
		AccountId = <T as system::Trait>::AccountId
Gav Wood's avatar
Gav Wood committed
98
99
	{
		/// Someone claimed some DOTs.
100
		Claimed(AccountId, EthereumAddress, Balance),
Gav Wood's avatar
Gav Wood committed
101
102
103
	}
);

104
105
106
107
108
109
decl_error! {
	pub enum Error for Module<T: Trait> {
		/// Invalid Ethereum signature.
		InvalidEthereumSignature,
		/// Ethereum address has no claim.
		SignerHasNoClaim,
Gavin Wood's avatar
Gavin Wood committed
110
111
112
113
114
		/// The destination is already vesting and cannot be the target of a further claim.
		DestinationVesting,
		/// There's not enough in the pot to pay out some unvested amount. Generally implies a logic
		/// error.
		PotUnderflow,
115
116
117
	}
}

Gav Wood's avatar
Gav Wood committed
118
119
120
121
122
123
124
decl_storage! {
	// A macro for the Storage trait, and its implementation, for this module.
	// This allows for type-safe usage of the Substrate storage database, so you can
	// keep things around between blocks.
	trait Store for Module<T: Trait> as Claims {
		Claims get(claims) build(|config: &GenesisConfig<T>| {
			config.claims.iter().map(|(a, b)| (a.clone(), b.clone())).collect::<Vec<_>>()
Gavin Wood's avatar
Gavin Wood committed
125
		}): map hasher(blake2_256) EthereumAddress => Option<BalanceOf<T>>;
Gav Wood's avatar
Gav Wood committed
126
		Total get(total) build(|config: &GenesisConfig<T>| {
127
128
			config.claims.iter().fold(Zero::zero(), |acc: BalanceOf<T>, &(_, n)| acc + n)
		}): BalanceOf<T>;
129
130
131
132
		/// Vesting schedule for a claim.
		/// First balance is the total amount that should be held for vesting.
		/// Second balance is how much should be unlocked per block.
		/// The block number is when the vesting should start.
Gavin Wood's avatar
Gavin Wood committed
133
134
135
		Vesting get(vesting) config():
			map hasher(blake2_256) EthereumAddress
			=> Option<(BalanceOf<T>, BalanceOf<T>, T::BlockNumber)>;
Gav Wood's avatar
Gav Wood committed
136
137
	}
	add_extra_genesis {
138
		config(claims): Vec<(EthereumAddress, BalanceOf<T>)>;
Gav Wood's avatar
Gav Wood committed
139
140
141
142
143
	}
}

decl_module! {
	pub struct Module<T: Trait> for enum Call where origin: T::Origin {
144
145
		type Error = Error<T>;

146
147
148
		/// The Prefix that is used in signed Ethereum messages for this network
		const Prefix: &[u8] = T::Prefix::get();

Gav Wood's avatar
Gav Wood committed
149
		/// Deposit one of this module's events by using the default implementation.
thiolliere's avatar
thiolliere committed
150
		fn deposit_event() = default;
Gav Wood's avatar
Gav Wood committed
151
152

		/// Make a claim.
153
		#[weight = SimpleDispatchInfo::FixedNormal(1_000_000)]
154
155
		fn claim(origin, dest: T::AccountId, ethereum_signature: EcdsaSignature) {
			ensure_none(origin)?;
156

157
158
			let data = dest.using_encoded(to_ascii_hex);
			let signer = Self::eth_recover(&ethereum_signature, &data)
159
				.ok_or(Error::<T>::InvalidEthereumSignature)?;
160

161
			let balance_due = <Claims<T>>::get(&signer)
162
				.ok_or(Error::<T>::SignerHasNoClaim)?;
163

164
			let new_total = Self::total().checked_sub(&balance_due).ok_or(Error::<T>::PotUnderflow)?;
Gavin Wood's avatar
Gavin Wood committed
165

166
			// Check if this claim should have a vesting schedule.
167
168
169
170
171
			if let Some(vs) = <Vesting<T>>::get(&signer) {
				// If this fails, destination account already has a vesting schedule
				// applied to it, and this claim should not be processed.
				T::VestingSchedule::add_vesting_schedule(&dest, vs.0, vs.1, vs.2)
					.map_err(|_| Error::<T>::DestinationVesting)?;
172
173
			}

174
175
			CurrencyOf::<T>::deposit_creating(&dest, balance_due);
			<Total<T>>::put(new_total);
176
177
178
			<Claims<T>>::remove(&signer);
			<Vesting<T>>::remove(&signer);

Gav Wood's avatar
Gav Wood committed
179
			// Let's deposit an event to let the outside world know this happened.
180
181
			Self::deposit_event(RawEvent::Claimed(dest, signer, balance_due));
		}
Gavin Wood's avatar
Gavin Wood committed
182
183

		/// Add a new claim, if you are root.
Gavin Wood's avatar
Gavin Wood committed
184
		#[weight = SimpleDispatchInfo::FixedNormal(30_000)]
185
186
187
188
189
		fn mint_claim(origin,
			who: EthereumAddress,
			value: BalanceOf<T>,
			vesting_schedule: Option<(BalanceOf<T>, BalanceOf<T>, T::BlockNumber)>,
		) {
Gavin Wood's avatar
Gavin Wood committed
190
191
192
193
			ensure_root(origin)?;

			<Total<T>>::mutate(|t| *t += value);
			<Claims<T>>::insert(who, value);
194
195
196
			if let Some(vs) = vesting_schedule {
				<Vesting<T>>::insert(who, vs);
			}
Gavin Wood's avatar
Gavin Wood committed
197
		}
198
199
200
	}
}

201
202
203
204
205
206
207
208
209
210
211
/// Converts the given binary data into ASCII-encoded hex. It will be twice the length.
fn to_ascii_hex(data: &[u8]) -> Vec<u8> {
	let mut r = Vec::with_capacity(data.len() * 2);
	let mut push_nibble = |n| r.push(if n < 10 { b'0' + n } else { b'a' - 10 + n });
	for &b in data.iter() {
		push_nibble(b / 16);
		push_nibble(b % 16);
	}
	r
}

212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
impl<T: Trait> Module<T> {
	// Constructs the message that Ethereum RPC's `personal_sign` and `eth_sign` would sign.
	fn ethereum_signable_message(what: &[u8]) -> Vec<u8> {
		let prefix = T::Prefix::get();
		let mut l = prefix.len() + what.len();
		let mut rev = Vec::new();
		while l > 0 {
			rev.push(b'0' + (l % 10) as u8);
			l /= 10;
		}
		let mut v = b"\x19Ethereum Signed Message:\n".to_vec();
		v.extend(rev.into_iter().rev());
		v.extend_from_slice(&prefix[..]);
		v.extend_from_slice(what);
		v
	}

	// Attempts to recover the Ethereum address from a message signature signed by using
	// the Ethereum RPC's `personal_sign` and `eth_sign`.
	fn eth_recover(s: &EcdsaSignature, what: &[u8]) -> Option<EthereumAddress> {
		let msg = keccak_256(&Self::ethereum_signable_message(what));
		let mut res = EthereumAddress::default();
234
		res.0.copy_from_slice(&keccak_256(&secp256k1_ecdsa_recover(&s.0, &msg).ok()?[..])[12..]);
235
236
237
238
		Some(res)
	}
}

Kian Paimani's avatar
Kian Paimani committed
239
#[allow(deprecated)] // Allow `ValidateUnsigned`
240
impl<T: Trait> sp_runtime::traits::ValidateUnsigned for Module<T> {
241
242
243
244
245
246
247
	type Call = Call<T>;

	fn validate_unsigned(call: &Self::Call) -> TransactionValidity {
		const PRIORITY: u64 = 100;

		match call {
			Call::claim(account, ethereum_signature) => {
248
249
250
251
				let data = account.using_encoded(to_ascii_hex);
				let maybe_signer = Self::eth_recover(&ethereum_signature, &data);
				let signer = if let Some(s) = maybe_signer {
					s
252
				} else {
Gavin Wood's avatar
Gavin Wood committed
253
254
255
					return InvalidTransaction::Custom(
						ValidityError::InvalidEthereumSignature.into(),
					).into();
256
257
				};

258
				if !<Claims<T>>::contains_key(&signer) {
Gavin Wood's avatar
Gavin Wood committed
259
260
261
					return Err(InvalidTransaction::Custom(
						ValidityError::SignerHasNoClaim.into(),
					).into());
262
263
				}

Gavin Wood's avatar
Gavin Wood committed
264
				Ok(ValidTransaction {
265
266
					priority: PRIORITY,
					requires: vec![],
267
					provides: vec![("claims", signer).encode()],
268
					longevity: TransactionLongevity::max_value(),
269
					propagate: true,
270
				})
271
			}
Gavin Wood's avatar
Gavin Wood committed
272
			_ => Err(InvalidTransaction::Call.into()),
Gav Wood's avatar
Gav Wood committed
273
274
275
276
277
278
279
280
		}
	}
}

#[cfg(test)]
mod tests {
	use secp256k1;
	use tiny_keccak::keccak256;
281
	use hex_literal::hex;
Gav Wood's avatar
Gav Wood committed
282
283
	use super::*;

284
	use sp_core::H256;
285
	use codec::Encode;
Gav Wood's avatar
Gav Wood committed
286
	// The testing primitives are very useful for avoiding having to work with signatures
Black3HDF's avatar
Black3HDF committed
287
	// or public keys. `u64` is used as the `AccountId` and no `Signature`s are required.
Gavin Wood's avatar
Gavin Wood committed
288
289
290
291
	use sp_runtime::{Perbill, traits::{BlakeTwo256, IdentityLookup, Identity}, testing::Header};
	use frame_support::{
		impl_outer_origin, assert_ok, assert_err, assert_noop, parameter_types
	};
Gav Wood's avatar
Gav Wood committed
292
293
294
295
296
297
298
299
300
301
	use balances;

	impl_outer_origin! {
		pub enum Origin for Test {}
	}
	// For testing the module, we construct most of a mock runtime. This means
	// first constructing a configuration type (`Test`) which `impl`s each of the
	// configuration traits of modules we want to use.
	#[derive(Clone, Eq, PartialEq)]
	pub struct Test;
302
	parameter_types! {
303
		pub const BlockHashCount: u32 = 250;
304
305
306
		pub const MaximumBlockWeight: u32 = 4 * 1024 * 1024;
		pub const MaximumBlockLength: u32 = 4 * 1024 * 1024;
		pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);
307
	}
Gav Wood's avatar
Gav Wood committed
308
309
	impl system::Trait for Test {
		type Origin = Origin;
310
		type Call = ();
Gav Wood's avatar
Gav Wood committed
311
312
313
314
315
316
317
318
		type Index = u64;
		type BlockNumber = u64;
		type Hash = H256;
		type Hashing = BlakeTwo256;
		type AccountId = u64;
		type Lookup = IdentityLookup<u64>;
		type Header = Header;
		type Event = ();
319
		type BlockHashCount = BlockHashCount;
320
321
		type MaximumBlockWeight = MaximumBlockWeight;
		type MaximumBlockLength = MaximumBlockLength;
Gavin Wood's avatar
Gavin Wood committed
322
		type AvailableBlockRatio = AvailableBlockRatio;
323
		type Version = ();
324
		type ModuleToIndex = ();
325
326
		type AccountData = balances::AccountData<u64>;
		type OnNewAccount = ();
327
		type OnKilledAccount = Balances;
Gav Wood's avatar
Gav Wood committed
328
	}
Gavin Wood's avatar
Gavin Wood committed
329
330

	parameter_types! {
Shawn Tabrizi's avatar
Shawn Tabrizi committed
331
		pub const ExistentialDeposit: u64 = 1;
Gavin Wood's avatar
Gavin Wood committed
332
333
334
		pub const CreationFee: u64 = 0;
	}

Gav Wood's avatar
Gav Wood committed
335
336
	impl balances::Trait for Test {
		type Balance = u64;
Gavin Wood's avatar
Gavin Wood committed
337
		type Event = ();
338
		type DustRemoval = ();
Gavin Wood's avatar
Gavin Wood committed
339
		type ExistentialDeposit = ExistentialDeposit;
340
		type AccountStore = System;
Gav Wood's avatar
Gav Wood committed
341
	}
342

Gavin Wood's avatar
Gavin Wood committed
343
344
345
346
347
348
	impl vesting::Trait for Test {
		type Event = ();
		type Currency = Balances;
		type BlockNumberToBalance = Identity;
	}

349
	parameter_types!{
350
		pub const Prefix: &'static [u8] = b"Pay RUSTs to the TEST account:";
351
352
	}

Gav Wood's avatar
Gav Wood committed
353
354
	impl Trait for Test {
		type Event = ();
Gavin Wood's avatar
Gavin Wood committed
355
		type VestingSchedule = Vesting;
356
		type Prefix = Prefix;
Gav Wood's avatar
Gav Wood committed
357
	}
358
	type System = system::Module<Test>;
Gav Wood's avatar
Gav Wood committed
359
	type Balances = balances::Module<Test>;
Gavin Wood's avatar
Gavin Wood committed
360
	type Vesting = vesting::Module<Test>;
Gav Wood's avatar
Gav Wood committed
361
362
	type Claims = Module<Test>;

Gavin Wood's avatar
Gavin Wood committed
363
	fn alice() -> secp256k1::SecretKey {
Gav Wood's avatar
Gav Wood committed
364
365
		secp256k1::SecretKey::parse(&keccak256(b"Alice")).unwrap()
	}
Gavin Wood's avatar
Gavin Wood committed
366
367
368
369
370
	fn bob() -> secp256k1::SecretKey {
		secp256k1::SecretKey::parse(&keccak256(b"Bob")).unwrap()
	}
	fn public(secret: &secp256k1::SecretKey) -> secp256k1::PublicKey {
		secp256k1::PublicKey::from_secret_key(secret)
Gav Wood's avatar
Gav Wood committed
371
	}
Gavin Wood's avatar
Gavin Wood committed
372
	fn eth(secret: &secp256k1::SecretKey) -> EthereumAddress {
Gav Wood's avatar
Gav Wood committed
373
		let mut res = EthereumAddress::default();
Gavin Wood's avatar
Gavin Wood committed
374
		res.0.copy_from_slice(&keccak256(&public(secret).serialize()[1..65])[12..]);
Gav Wood's avatar
Gav Wood committed
375
376
		res
	}
Gavin Wood's avatar
Gavin Wood committed
377
	fn sig(secret: &secp256k1::SecretKey, what: &[u8]) -> EcdsaSignature {
378
		let msg = keccak256(&Claims::ethereum_signable_message(&to_ascii_hex(what)[..]));
Gavin Wood's avatar
Gavin Wood committed
379
		let (sig, recovery_id) = secp256k1::sign(&secp256k1::Message::parse(&msg), secret);
380
381
382
383
		let mut r = [0u8; 65];
		r[0..64].copy_from_slice(&sig.serialize()[..]);
		r[64] = recovery_id.serialize();
		EcdsaSignature(r)
Gav Wood's avatar
Gav Wood committed
384
385
386
387
	}

	// This function basically just builds a genesis storage key/value store according to
	// our desired mockup.
388
	fn new_test_ext() -> sp_io::TestExternalities {
389
		let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
Gav Wood's avatar
Gav Wood committed
390
		// We use default for brevity, but you can configure as desired if needed.
391
392
		balances::GenesisConfig::<Test>::default().assimilate_storage(&mut t).unwrap();
		GenesisConfig::<Test>{
Gavin Wood's avatar
Gavin Wood committed
393
			claims: vec![(eth(&alice()), 100)],
394
			vesting: vec![(eth(&alice()), (50, 10, 1))],
395
		}.assimilate_storage(&mut t).unwrap();
Gav Wood's avatar
Gav Wood committed
396
397
398
399
400
		t.into()
	}

	#[test]
	fn basic_setup_works() {
401
		new_test_ext().execute_with(|| {
Gav Wood's avatar
Gav Wood committed
402
			assert_eq!(Claims::total(), 100);
Gavin Wood's avatar
Gavin Wood committed
403
			assert_eq!(Claims::claims(&eth(&alice())), Some(100));
thiolliere's avatar
thiolliere committed
404
			assert_eq!(Claims::claims(&EthereumAddress::default()), None);
405
			assert_eq!(Claims::vesting(&eth(&alice())), Some((50, 10, 1)));
Gav Wood's avatar
Gav Wood committed
406
407
408
		});
	}

409
410
411
412
413
414
415
416
417
	#[test]
	fn serde_works() {
		let x = EthereumAddress(hex!["0123456789abcdef0123456789abcdef01234567"]);
		let y = serde_json::to_string(&x).unwrap();
		assert_eq!(y, "\"0x0123456789abcdef0123456789abcdef01234567\"");
		let z: EthereumAddress = serde_json::from_str(&y).unwrap();
		assert_eq!(x, z);
	}

Gav Wood's avatar
Gav Wood committed
418
419
	#[test]
	fn claiming_works() {
420
		new_test_ext().execute_with(|| {
421
			assert_eq!(Balances::free_balance(42), 0);
Gavin Wood's avatar
Gavin Wood committed
422
			assert_ok!(Claims::claim(Origin::NONE, 42, sig(&alice(), &42u64.encode())));
Gav Wood's avatar
Gav Wood committed
423
			assert_eq!(Balances::free_balance(&42), 100);
424
			assert_eq!(Vesting::vesting_balance(&42), Some(50));
425
			assert_eq!(Claims::total(), 0);
Gav Wood's avatar
Gav Wood committed
426
427
428
		});
	}

Gavin Wood's avatar
Gavin Wood committed
429
430
431
	#[test]
	fn add_claim_works() {
		new_test_ext().execute_with(|| {
432
433
			assert_noop!(
				Claims::mint_claim(Origin::signed(42), eth(&bob()), 200, None),
434
				sp_runtime::traits::BadOrigin,
435
			);
436
			assert_eq!(Balances::free_balance(42), 0);
437
438
			assert_noop!(
				Claims::claim(Origin::NONE, 69, sig(&bob(), &69u64.encode())),
439
				Error::<Test>::SignerHasNoClaim,
440
441
			);
			assert_ok!(Claims::mint_claim(Origin::ROOT, eth(&bob()), 200, None));
442
			assert_eq!(Claims::total(), 300);
443
			assert_ok!(Claims::claim(Origin::NONE, 69, sig(&bob(), &69u64.encode())));
444
			assert_eq!(Balances::free_balance(&69), 200);
445
			assert_eq!(Vesting::vesting_balance(&69), None);
446
			assert_eq!(Claims::total(), 100);
447
448
449
450
451
452
453
454
		});
	}

	#[test]
	fn add_claim_with_vesting_works() {
		new_test_ext().execute_with(|| {
			assert_noop!(
				Claims::mint_claim(Origin::signed(42), eth(&bob()), 200, Some((50, 10, 1))),
455
				sp_runtime::traits::BadOrigin,
456
			);
Gavin Wood's avatar
Gavin Wood committed
457
			assert_eq!(Balances::free_balance(42), 0);
Gavin Wood's avatar
Gavin Wood committed
458
459
			assert_noop!(
				Claims::claim(Origin::NONE, 69, sig(&bob(), &69u64.encode())),
460
				Error::<Test>::SignerHasNoClaim
Gavin Wood's avatar
Gavin Wood committed
461
			);
462
			assert_ok!(Claims::mint_claim(Origin::ROOT, eth(&bob()), 200, Some((50, 10, 1))));
Gavin Wood's avatar
Gavin Wood committed
463
			assert_ok!(Claims::claim(Origin::NONE, 69, sig(&bob(), &69u64.encode())));
464
			assert_eq!(Balances::free_balance(&69), 200);
465
			assert_eq!(Vesting::vesting_balance(&69), Some(50));
Gavin Wood's avatar
Gavin Wood committed
466
467
468
		});
	}

469
470
	#[test]
	fn origin_signed_claiming_fail() {
471
		new_test_ext().execute_with(|| {
Gavin Wood's avatar
Gavin Wood committed
472
			assert_eq!(Balances::free_balance(42), 0);
Gavin Wood's avatar
Gavin Wood committed
473
			assert_err!(
Gavin Wood's avatar
Gavin Wood committed
474
				Claims::claim(Origin::signed(42), 42, sig(&alice(), &42u64.encode())),
475
				sp_runtime::traits::BadOrigin,
Gavin Wood's avatar
Gavin Wood committed
476
			);
477
478
479
		});
	}

Gav Wood's avatar
Gav Wood committed
480
481
	#[test]
	fn double_claiming_doesnt_work() {
482
		new_test_ext().execute_with(|| {
Gavin Wood's avatar
Gavin Wood committed
483
			assert_eq!(Balances::free_balance(42), 0);
Gavin Wood's avatar
Gavin Wood committed
484
			assert_ok!(Claims::claim(Origin::NONE, 42, sig(&alice(), &42u64.encode())));
485
486
487
488
			assert_noop!(
				Claims::claim(Origin::NONE, 42, sig(&alice(), &42u64.encode())),
				Error::<Test>::SignerHasNoClaim
			);
Gav Wood's avatar
Gav Wood committed
489
490
491
		});
	}

492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
	#[test]
	fn claiming_while_vested_doesnt_work() {
		new_test_ext().execute_with(|| {
			assert_eq!(Claims::total(), 100);
			// A user is already vested
			assert_ok!(<Test as Trait>::VestingSchedule::add_vesting_schedule(&69, 1000, 100, 10));
			CurrencyOf::<Test>::make_free_balance_be(&69, 1000);
			assert_eq!(Balances::free_balance(69), 1000);
			assert_ok!(Claims::mint_claim(Origin::ROOT, eth(&bob()), 200, Some((50, 10, 1))));
			// New total
			assert_eq!(Claims::total(), 300);

			// They should not be able to claim
			assert_noop!(
				Claims::claim(Origin::NONE, 69, sig(&bob(), &69u64.encode())),
				Error::<Test>::DestinationVesting
			);
			// Everything should be unchanged
			assert_eq!(Claims::total(), 300);
			assert_eq!(Balances::free_balance(69), 1000);
512
			assert_eq!(Vesting::vesting_balance(&69), Some(1000));
513
514
515
		});
	}

Gav Wood's avatar
Gav Wood committed
516
517
	#[test]
	fn non_sender_sig_doesnt_work() {
518
		new_test_ext().execute_with(|| {
Gavin Wood's avatar
Gavin Wood committed
519
			assert_eq!(Balances::free_balance(42), 0);
520
521
522
523
			assert_noop!(
				Claims::claim(Origin::NONE, 42, sig(&alice(), &69u64.encode())),
				Error::<Test>::SignerHasNoClaim
			);
Gav Wood's avatar
Gav Wood committed
524
525
526
527
528
		});
	}

	#[test]
	fn non_claimant_doesnt_work() {
529
		new_test_ext().execute_with(|| {
Gavin Wood's avatar
Gavin Wood committed
530
			assert_eq!(Balances::free_balance(42), 0);
531
532
533
534
			assert_noop!(
				Claims::claim(Origin::NONE, 42, sig(&bob(), &69u64.encode())),
				Error::<Test>::SignerHasNoClaim
			);
Gav Wood's avatar
Gav Wood committed
535
536
537
538
539
		});
	}

	#[test]
	fn real_eth_sig_works() {
540
		new_test_ext().execute_with(|| {
541
542
			// "Pay RUSTs to the TEST account:2a00000000000000"
			let sig = hex!["444023e89b67e67c0562ed0305d252a5dd12b2af5ac51d6d3cb69a0b486bc4b3191401802dc29d26d586221f7256cd3329fe82174bdf659baea149a40e1c495d1c"];
543
			let sig = EcdsaSignature(sig);
544
			let who = 42u64.using_encoded(to_ascii_hex);
545
			let signer = Claims::eth_recover(&sig, &who).unwrap();
546
			assert_eq!(signer.0, hex!["6d31165d5d932d571f3b44695653b46dcc327e84"]);
547
		});
Gav Wood's avatar
Gav Wood committed
548
	}
549
550
551

	#[test]
	fn validate_unsigned_works() {
Kian Paimani's avatar
Kian Paimani committed
552
		#![allow(deprecated)] // Allow `ValidateUnsigned`
553
		use sp_runtime::traits::ValidateUnsigned;
Kian Paimani's avatar
Kian Paimani committed
554

555
		new_test_ext().execute_with(|| {
556
			assert_eq!(
Gavin Wood's avatar
Gavin Wood committed
557
				<Module<Test>>::validate_unsigned(&Call::claim(1, sig(&alice(), &1u64.encode()))),
Gavin Wood's avatar
Gavin Wood committed
558
				Ok(ValidTransaction {
559
560
					priority: 100,
					requires: vec![],
Gavin Wood's avatar
Gavin Wood committed
561
					provides: vec![("claims", eth(&alice())).encode()],
562
					longevity: TransactionLongevity::max_value(),
563
					propagate: true,
564
				})
565
566
			);
			assert_eq!(
567
				<Module<Test>>::validate_unsigned(&Call::claim(0, EcdsaSignature([0; 65]))),
Gavin Wood's avatar
Gavin Wood committed
568
				InvalidTransaction::Custom(ValidityError::InvalidEthereumSignature.into()).into(),
569
570
			);
			assert_eq!(
Gavin Wood's avatar
Gavin Wood committed
571
				<Module<Test>>::validate_unsigned(&Call::claim(1, sig(&bob(), &1u64.encode()))),
Gavin Wood's avatar
Gavin Wood committed
572
				InvalidTransaction::Custom(ValidityError::SignerHasNoClaim.into()).into(),
573
574
			);
			assert_eq!(
Gavin Wood's avatar
Gavin Wood committed
575
				<Module<Test>>::validate_unsigned(&Call::claim(0, sig(&bob(), &1u64.encode()))),
Gavin Wood's avatar
Gavin Wood committed
576
				InvalidTransaction::Custom(ValidityError::SignerHasNoClaim.into()).into(),
577
578
579
			);
		});
	}
Gav Wood's avatar
Gav Wood committed
580
}