1#[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#[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 Regular,
101 Saft,
103}
104
105impl StatementKind {
106 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#[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 #[pallet::config]
210 pub trait Config: frame_system::Config {
211 #[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 Claimed { who: T::AccountId, ethereum_address: EthereumAddress, amount: BalanceOf<T> },
226 }
227
228 #[pallet::error]
229 pub enum Error<T> {
230 InvalidEthereumSignature,
232 SignerHasNoClaim,
234 SenderHasNoClaim,
236 PotUnderflow,
239 InvalidStatement,
241 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 #[pallet::storage]
256 pub type Vesting<T: Config> =
257 StorageMap<_, Identity, EthereumAddress, (BalanceOf<T>, BalanceOf<T>, BlockNumberFor<T>)>;
258
259 #[pallet::storage]
261 pub type Signing<T> = StorageMap<_, Identity, EthereumAddress, StatementKind>;
262
263 #[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 self.claims.iter().map(|(a, b, _, _)| (*a, *b)).for_each(|(a, b)| {
280 Claims::<T>::insert(a, b);
281 });
282 Total::<T>::put(
284 self.claims
285 .iter()
286 .fold(Zero::zero(), |acc: BalanceOf<T>, &(_, b, _, _)| acc + b),
287 );
288 self.vesting.iter().for_each(|(k, v)| {
290 Vesting::<T>::insert(k, v);
291 });
292 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 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 #[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(ðereum_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 #[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 #[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(ðereum_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 #[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 #[pallet::validate_unsigned]
500 impl<T: Config> ValidateUnsigned for Pallet<T> {
501 type Call = Call<T>;
502
503 fn validate_unsigned(_source: TransactionSource, call: &Self::Call) -> TransactionValidity {
504 const PRIORITY: u64 = 100;
505
506 let (maybe_signer, maybe_statement) = match call {
507 Call::claim { dest: account, ethereum_signature } => {
511 let data = account.using_encoded(to_ascii_hex);
512 (Self::eth_recover(ðereum_signature, &data, &[][..]), None)
513 },
514 Call::claim_attest { dest: account, ethereum_signature, statement } => {
518 let data = account.using_encoded(to_ascii_hex);
519 (
520 Self::eth_recover(ðereum_signature, &data, &statement),
521 Some(statement.as_slice()),
522 )
523 },
524 _ => return Err(InvalidTransaction::Call.into()),
525 };
526
527 let signer = maybe_signer.ok_or(InvalidTransaction::Custom(
528 ValidityError::InvalidEthereumSignature.into(),
529 ))?;
530
531 let e = InvalidTransaction::Custom(ValidityError::SignerHasNoClaim.into());
532 ensure!(Claims::<T>::contains_key(&signer), e);
533
534 let e = InvalidTransaction::Custom(ValidityError::InvalidStatement.into());
535 match Signing::<T>::get(signer) {
536 None => ensure!(maybe_statement.is_none(), e),
537 Some(s) => ensure!(Some(s.to_text()) == maybe_statement, e),
538 }
539
540 Ok(ValidTransaction {
541 priority: PRIORITY,
542 requires: vec![],
543 provides: vec![("claims", signer).encode()],
544 longevity: TransactionLongevity::max_value(),
545 propagate: true,
546 })
547 }
548 }
549}
550
551fn to_ascii_hex(data: &[u8]) -> Vec<u8> {
553 let mut r = Vec::with_capacity(data.len() * 2);
554 let mut push_nibble = |n| r.push(if n < 10 { b'0' + n } else { b'a' - 10 + n });
555 for &b in data.iter() {
556 push_nibble(b / 16);
557 push_nibble(b % 16);
558 }
559 r
560}
561
562impl<T: Config> Pallet<T> {
563 fn ethereum_signable_message(what: &[u8], extra: &[u8]) -> Vec<u8> {
565 let prefix = T::Prefix::get();
566 let mut l = prefix.len() + what.len() + extra.len();
567 let mut rev = Vec::new();
568 while l > 0 {
569 rev.push(b'0' + (l % 10) as u8);
570 l /= 10;
571 }
572 let mut v = b"\x19Ethereum Signed Message:\n".to_vec();
573 v.extend(rev.into_iter().rev());
574 v.extend_from_slice(prefix);
575 v.extend_from_slice(what);
576 v.extend_from_slice(extra);
577 v
578 }
579
580 fn eth_recover(s: &EcdsaSignature, what: &[u8], extra: &[u8]) -> Option<EthereumAddress> {
583 let msg = keccak_256(&Self::ethereum_signable_message(what, extra));
584 let mut res = EthereumAddress::default();
585 res.0
586 .copy_from_slice(&keccak_256(&secp256k1_ecdsa_recover(&s.0, &msg).ok()?[..])[12..]);
587 Some(res)
588 }
589
590 fn process_claim(signer: EthereumAddress, dest: T::AccountId) -> sp_runtime::DispatchResult {
591 let balance_due = Claims::<T>::get(&signer).ok_or(Error::<T>::SignerHasNoClaim)?;
592
593 let new_total =
594 Total::<T>::get().checked_sub(&balance_due).ok_or(Error::<T>::PotUnderflow)?;
595
596 let vesting = Vesting::<T>::get(&signer);
597 if vesting.is_some() && T::VestingSchedule::vesting_balance(&dest).is_some() {
598 return Err(Error::<T>::VestedBalanceExists.into());
599 }
600
601 let _ = CurrencyOf::<T>::deposit_creating(&dest, balance_due);
603
604 if let Some(vs) = vesting {
606 T::VestingSchedule::add_vesting_schedule(&dest, vs.0, vs.1, vs.2)
609 .expect("No other vesting schedule exists, as checked above; qed");
610 }
611
612 Total::<T>::put(new_total);
613 Claims::<T>::remove(&signer);
614 Vesting::<T>::remove(&signer);
615 Signing::<T>::remove(&signer);
616
617 Self::deposit_event(Event::<T>::Claimed {
619 who: dest,
620 ethereum_address: signer,
621 amount: balance_due,
622 });
623
624 Ok(())
625 }
626}
627
628#[derive(Encode, Decode, DecodeWithMemTracking, Clone, Eq, PartialEq, TypeInfo)]
631#[scale_info(skip_type_params(T))]
632pub struct PrevalidateAttests<T>(core::marker::PhantomData<fn(T)>);
633
634impl<T: Config> Debug for PrevalidateAttests<T>
635where
636 <T as frame_system::Config>::RuntimeCall: IsSubType<Call<T>>,
637{
638 #[cfg(feature = "std")]
639 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
640 write!(f, "PrevalidateAttests")
641 }
642
643 #[cfg(not(feature = "std"))]
644 fn fmt(&self, _: &mut core::fmt::Formatter) -> core::fmt::Result {
645 Ok(())
646 }
647}
648
649impl<T: Config> PrevalidateAttests<T>
650where
651 <T as frame_system::Config>::RuntimeCall: IsSubType<Call<T>>,
652{
653 pub fn new() -> Self {
655 Self(core::marker::PhantomData)
656 }
657}
658
659impl<T: Config> TransactionExtension<T::RuntimeCall> for PrevalidateAttests<T>
660where
661 <T as frame_system::Config>::RuntimeCall: IsSubType<Call<T>>,
662 <<T as frame_system::Config>::RuntimeCall as Dispatchable>::RuntimeOrigin:
663 AsSystemOriginSigner<T::AccountId> + AsTransactionAuthorizedOrigin + Clone,
664{
665 const IDENTIFIER: &'static str = "PrevalidateAttests";
666 type Implicit = ();
667 type Pre = ();
668 type Val = ();
669
670 fn weight(&self, call: &T::RuntimeCall) -> Weight {
671 if let Some(Call::attest { .. }) = call.is_sub_type() {
672 T::WeightInfo::prevalidate_attests()
673 } else {
674 Weight::zero()
675 }
676 }
677
678 fn validate(
679 &self,
680 origin: <T::RuntimeCall as Dispatchable>::RuntimeOrigin,
681 call: &T::RuntimeCall,
682 _info: &DispatchInfoOf<T::RuntimeCall>,
683 _len: usize,
684 _self_implicit: Self::Implicit,
685 _inherited_implication: &impl Encode,
686 _source: TransactionSource,
687 ) -> Result<
688 (ValidTransaction, Self::Val, <T::RuntimeCall as Dispatchable>::RuntimeOrigin),
689 TransactionValidityError,
690 > {
691 if let Some(Call::attest { statement: attested_statement }) = call.is_sub_type() {
692 let who = origin.as_system_origin_signer().ok_or(InvalidTransaction::BadSigner)?;
693 let signer = Preclaims::<T>::get(who)
694 .ok_or(InvalidTransaction::Custom(ValidityError::SignerHasNoClaim.into()))?;
695 if let Some(s) = Signing::<T>::get(signer) {
696 let e = InvalidTransaction::Custom(ValidityError::InvalidStatement.into());
697 ensure!(&attested_statement[..] == s.to_text(), e);
698 }
699 }
700 Ok((ValidTransaction::default(), (), origin))
701 }
702
703 impl_tx_ext_default!(T::RuntimeCall; prepare);
704}
705
706#[cfg(any(test, feature = "runtime-benchmarks"))]
707mod secp_utils {
708 use super::*;
709
710 pub fn public(secret: &libsecp256k1::SecretKey) -> libsecp256k1::PublicKey {
711 libsecp256k1::PublicKey::from_secret_key(secret)
712 }
713 pub fn eth(secret: &libsecp256k1::SecretKey) -> EthereumAddress {
714 let mut res = EthereumAddress::default();
715 res.0.copy_from_slice(&keccak_256(&public(secret).serialize()[1..65])[12..]);
716 res
717 }
718 pub fn sig<T: Config>(
719 secret: &libsecp256k1::SecretKey,
720 what: &[u8],
721 extra: &[u8],
722 ) -> EcdsaSignature {
723 let msg = keccak_256(&super::Pallet::<T>::ethereum_signable_message(
724 &to_ascii_hex(what)[..],
725 extra,
726 ));
727 let (sig, recovery_id) = libsecp256k1::sign(&libsecp256k1::Message::parse(&msg), secret);
728 let mut r = [0u8; 65];
729 r[0..64].copy_from_slice(&sig.serialize()[..]);
730 r[64] = recovery_id.serialize();
731 EcdsaSignature(r)
732 }
733}
734
735#[cfg(test)]
736mod mock;
737
738#[cfg(test)]
739mod tests;
740
741#[cfg(feature = "runtime-benchmarks")]
742mod benchmarking;