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 StatementKind::Saft =>
114 &b"I hereby agree to the terms of the statement whose SHA-256 multihash is \
115 QmXEkMahfhHJPzT3RjkXiZVFi77ZeVeuxtAjhojGRNYckz. (This may be found at the URL: \
116 https://statement.polkadot.network/saft.html)"[..],
117 }
118 }
119}
120
121impl Default for StatementKind {
122 fn default() -> Self {
123 StatementKind::Regular
124 }
125}
126
127#[derive(
131 Clone,
132 Copy,
133 PartialEq,
134 Eq,
135 Encode,
136 Decode,
137 DecodeWithMemTracking,
138 Default,
139 Debug,
140 TypeInfo,
141 MaxEncodedLen,
142)]
143pub struct EthereumAddress(pub [u8; 20]);
144
145impl Serialize for EthereumAddress {
146 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
147 where
148 S: Serializer,
149 {
150 let hex: String = rustc_hex::ToHex::to_hex(&self.0[..]);
151 serializer.serialize_str(&format!("0x{}", hex))
152 }
153}
154
155impl<'de> Deserialize<'de> for EthereumAddress {
156 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
157 where
158 D: Deserializer<'de>,
159 {
160 let base_string = String::deserialize(deserializer)?;
161 let offset = if base_string.starts_with("0x") { 2 } else { 0 };
162 let s = &base_string[offset..];
163 if s.len() != 40 {
164 Err(serde::de::Error::custom(
165 "Bad length of Ethereum address (should be 42 including '0x')",
166 ))?;
167 }
168 let raw: Vec<u8> = rustc_hex::FromHex::from_hex(s)
169 .map_err(|e| serde::de::Error::custom(format!("{:?}", e)))?;
170 let mut r = Self::default();
171 r.0.copy_from_slice(&raw);
172 Ok(r)
173 }
174}
175
176impl AsRef<[u8]> for EthereumAddress {
177 fn as_ref(&self) -> &[u8] {
178 &self.0[..]
179 }
180}
181
182#[derive(Encode, Decode, DecodeWithMemTracking, Clone, TypeInfo, MaxEncodedLen)]
183pub struct EcdsaSignature(pub [u8; 65]);
184
185impl PartialEq for EcdsaSignature {
186 fn eq(&self, other: &Self) -> bool {
187 &self.0[..] == &other.0[..]
188 }
189}
190
191impl core::fmt::Debug for EcdsaSignature {
192 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
193 write!(f, "EcdsaSignature({:?})", &self.0[..])
194 }
195}
196
197#[frame_support::pallet]
198pub mod pallet {
199 use super::*;
200 use frame_support::pallet_prelude::*;
201 use frame_system::pallet_prelude::*;
202
203 #[pallet::pallet]
204 pub struct Pallet<T>(_);
205
206 #[pallet::config]
208 pub trait Config: frame_system::Config {
209 #[allow(deprecated)]
211 type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
212 type VestingSchedule: VestingSchedule<Self::AccountId, Moment = BlockNumberFor<Self>>;
213 #[pallet::constant]
214 type Prefix: Get<&'static [u8]>;
215 type MoveClaimOrigin: EnsureOrigin<Self::RuntimeOrigin>;
216 type WeightInfo: WeightInfo;
217 }
218
219 #[pallet::event]
220 #[pallet::generate_deposit(pub(super) fn deposit_event)]
221 pub enum Event<T: Config> {
222 Claimed { who: T::AccountId, ethereum_address: EthereumAddress, amount: BalanceOf<T> },
224 }
225
226 #[pallet::error]
227 pub enum Error<T> {
228 InvalidEthereumSignature,
230 SignerHasNoClaim,
232 SenderHasNoClaim,
234 PotUnderflow,
237 InvalidStatement,
239 VestedBalanceExists,
241 }
242
243 #[pallet::storage]
244 pub type Claims<T: Config> = StorageMap<_, Identity, EthereumAddress, BalanceOf<T>>;
245
246 #[pallet::storage]
247 pub type Total<T: Config> = StorageValue<_, BalanceOf<T>, ValueQuery>;
248
249 #[pallet::storage]
254 pub type Vesting<T: Config> =
255 StorageMap<_, Identity, EthereumAddress, (BalanceOf<T>, BalanceOf<T>, BlockNumberFor<T>)>;
256
257 #[pallet::storage]
259 pub type Signing<T> = StorageMap<_, Identity, EthereumAddress, StatementKind>;
260
261 #[pallet::storage]
263 pub type Preclaims<T: Config> = StorageMap<_, Identity, T::AccountId, EthereumAddress>;
264
265 #[pallet::genesis_config]
266 #[derive(DefaultNoBound)]
267 pub struct GenesisConfig<T: Config> {
268 pub claims:
269 Vec<(EthereumAddress, BalanceOf<T>, Option<T::AccountId>, Option<StatementKind>)>,
270 pub vesting: Vec<(EthereumAddress, (BalanceOf<T>, BalanceOf<T>, BlockNumberFor<T>))>,
271 }
272
273 #[pallet::genesis_build]
274 impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
275 fn build(&self) {
276 self.claims.iter().map(|(a, b, _, _)| (*a, *b)).for_each(|(a, b)| {
278 Claims::<T>::insert(a, b);
279 });
280 Total::<T>::put(
282 self.claims
283 .iter()
284 .fold(Zero::zero(), |acc: BalanceOf<T>, &(_, b, _, _)| acc + b),
285 );
286 self.vesting.iter().for_each(|(k, v)| {
288 Vesting::<T>::insert(k, v);
289 });
290 self.claims
292 .iter()
293 .filter_map(|(a, _, _, s)| Some((*a, (*s)?)))
294 .for_each(|(a, s)| {
295 Signing::<T>::insert(a, s);
296 });
297 self.claims.iter().filter_map(|(a, _, i, _)| Some((i.clone()?, *a))).for_each(
299 |(i, a)| {
300 Preclaims::<T>::insert(i, a);
301 },
302 );
303 }
304 }
305
306 #[pallet::hooks]
307 impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
308
309 #[pallet::call]
310 impl<T: Config> Pallet<T> {
311 #[pallet::call_index(0)]
336 #[pallet::weight(T::WeightInfo::claim())]
337 pub fn claim(
338 origin: OriginFor<T>,
339 dest: T::AccountId,
340 ethereum_signature: EcdsaSignature,
341 ) -> DispatchResult {
342 ensure_none(origin)?;
343
344 let data = dest.using_encoded(to_ascii_hex);
345 let signer = Self::eth_recover(ðereum_signature, &data, &[][..])
346 .ok_or(Error::<T>::InvalidEthereumSignature)?;
347 ensure!(Signing::<T>::get(&signer).is_none(), Error::<T>::InvalidStatement);
348
349 Self::process_claim(signer, dest)?;
350 Ok(())
351 }
352
353 #[pallet::call_index(1)]
369 #[pallet::weight(T::WeightInfo::mint_claim())]
370 pub fn mint_claim(
371 origin: OriginFor<T>,
372 who: EthereumAddress,
373 value: BalanceOf<T>,
374 vesting_schedule: Option<(BalanceOf<T>, BalanceOf<T>, BlockNumberFor<T>)>,
375 statement: Option<StatementKind>,
376 ) -> DispatchResult {
377 ensure_root(origin)?;
378
379 Total::<T>::mutate(|t| *t += value);
380 Claims::<T>::insert(who, value);
381 if let Some(vs) = vesting_schedule {
382 Vesting::<T>::insert(who, vs);
383 }
384 if let Some(s) = statement {
385 Signing::<T>::insert(who, s);
386 }
387 Ok(())
388 }
389
390 #[pallet::call_index(2)]
418 #[pallet::weight(T::WeightInfo::claim_attest())]
419 pub fn claim_attest(
420 origin: OriginFor<T>,
421 dest: T::AccountId,
422 ethereum_signature: EcdsaSignature,
423 statement: Vec<u8>,
424 ) -> DispatchResult {
425 ensure_none(origin)?;
426
427 let data = dest.using_encoded(to_ascii_hex);
428 let signer = Self::eth_recover(ðereum_signature, &data, &statement)
429 .ok_or(Error::<T>::InvalidEthereumSignature)?;
430 if let Some(s) = Signing::<T>::get(signer) {
431 ensure!(s.to_text() == &statement[..], Error::<T>::InvalidStatement);
432 }
433 Self::process_claim(signer, dest)?;
434 Ok(())
435 }
436
437 #[pallet::call_index(3)]
457 #[pallet::weight((
458 T::WeightInfo::attest(),
459 DispatchClass::Normal,
460 Pays::No
461 ))]
462 pub fn attest(origin: OriginFor<T>, statement: Vec<u8>) -> DispatchResult {
463 let who = ensure_signed(origin)?;
464 let signer = Preclaims::<T>::get(&who).ok_or(Error::<T>::SenderHasNoClaim)?;
465 if let Some(s) = Signing::<T>::get(signer) {
466 ensure!(s.to_text() == &statement[..], Error::<T>::InvalidStatement);
467 }
468 Self::process_claim(signer, who.clone())?;
469 Preclaims::<T>::remove(&who);
470 Ok(())
471 }
472
473 #[pallet::call_index(4)]
474 #[pallet::weight(T::WeightInfo::move_claim())]
475 pub fn move_claim(
476 origin: OriginFor<T>,
477 old: EthereumAddress,
478 new: EthereumAddress,
479 maybe_preclaim: Option<T::AccountId>,
480 ) -> DispatchResultWithPostInfo {
481 T::MoveClaimOrigin::try_origin(origin).map(|_| ()).or_else(ensure_root)?;
482
483 Claims::<T>::take(&old).map(|c| Claims::<T>::insert(&new, c));
484 Vesting::<T>::take(&old).map(|c| Vesting::<T>::insert(&new, c));
485 Signing::<T>::take(&old).map(|c| Signing::<T>::insert(&new, c));
486 maybe_preclaim.map(|preclaim| {
487 Preclaims::<T>::mutate(&preclaim, |maybe_o| {
488 if maybe_o.as_ref().map_or(false, |o| o == &old) {
489 *maybe_o = Some(new)
490 }
491 })
492 });
493 Ok(Pays::No.into())
494 }
495 }
496
497 #[pallet::validate_unsigned]
498 impl<T: Config> ValidateUnsigned for Pallet<T> {
499 type Call = Call<T>;
500
501 fn validate_unsigned(_source: TransactionSource, call: &Self::Call) -> TransactionValidity {
502 const PRIORITY: u64 = 100;
503
504 let (maybe_signer, maybe_statement) = match call {
505 Call::claim { dest: account, ethereum_signature } => {
509 let data = account.using_encoded(to_ascii_hex);
510 (Self::eth_recover(ðereum_signature, &data, &[][..]), None)
511 },
512 Call::claim_attest { dest: account, ethereum_signature, statement } => {
516 let data = account.using_encoded(to_ascii_hex);
517 (
518 Self::eth_recover(ðereum_signature, &data, &statement),
519 Some(statement.as_slice()),
520 )
521 },
522 _ => return Err(InvalidTransaction::Call.into()),
523 };
524
525 let signer = maybe_signer.ok_or(InvalidTransaction::Custom(
526 ValidityError::InvalidEthereumSignature.into(),
527 ))?;
528
529 let e = InvalidTransaction::Custom(ValidityError::SignerHasNoClaim.into());
530 ensure!(Claims::<T>::contains_key(&signer), e);
531
532 let e = InvalidTransaction::Custom(ValidityError::InvalidStatement.into());
533 match Signing::<T>::get(signer) {
534 None => ensure!(maybe_statement.is_none(), e),
535 Some(s) => ensure!(Some(s.to_text()) == maybe_statement, e),
536 }
537
538 Ok(ValidTransaction {
539 priority: PRIORITY,
540 requires: vec![],
541 provides: vec![("claims", signer).encode()],
542 longevity: TransactionLongevity::max_value(),
543 propagate: true,
544 })
545 }
546 }
547}
548
549fn to_ascii_hex(data: &[u8]) -> Vec<u8> {
551 let mut r = Vec::with_capacity(data.len() * 2);
552 let mut push_nibble = |n| r.push(if n < 10 { b'0' + n } else { b'a' - 10 + n });
553 for &b in data.iter() {
554 push_nibble(b / 16);
555 push_nibble(b % 16);
556 }
557 r
558}
559
560impl<T: Config> Pallet<T> {
561 fn ethereum_signable_message(what: &[u8], extra: &[u8]) -> Vec<u8> {
563 let prefix = T::Prefix::get();
564 let mut l = prefix.len() + what.len() + extra.len();
565 let mut rev = Vec::new();
566 while l > 0 {
567 rev.push(b'0' + (l % 10) as u8);
568 l /= 10;
569 }
570 let mut v = b"\x19Ethereum Signed Message:\n".to_vec();
571 v.extend(rev.into_iter().rev());
572 v.extend_from_slice(prefix);
573 v.extend_from_slice(what);
574 v.extend_from_slice(extra);
575 v
576 }
577
578 fn eth_recover(s: &EcdsaSignature, what: &[u8], extra: &[u8]) -> Option<EthereumAddress> {
581 let msg = keccak_256(&Self::ethereum_signable_message(what, extra));
582 let mut res = EthereumAddress::default();
583 res.0
584 .copy_from_slice(&keccak_256(&secp256k1_ecdsa_recover(&s.0, &msg).ok()?[..])[12..]);
585 Some(res)
586 }
587
588 fn process_claim(signer: EthereumAddress, dest: T::AccountId) -> sp_runtime::DispatchResult {
589 let balance_due = Claims::<T>::get(&signer).ok_or(Error::<T>::SignerHasNoClaim)?;
590
591 let new_total =
592 Total::<T>::get().checked_sub(&balance_due).ok_or(Error::<T>::PotUnderflow)?;
593
594 let vesting = Vesting::<T>::get(&signer);
595 if vesting.is_some() && T::VestingSchedule::vesting_balance(&dest).is_some() {
596 return Err(Error::<T>::VestedBalanceExists.into())
597 }
598
599 let _ = CurrencyOf::<T>::deposit_creating(&dest, balance_due);
601
602 if let Some(vs) = vesting {
604 T::VestingSchedule::add_vesting_schedule(&dest, vs.0, vs.1, vs.2)
607 .expect("No other vesting schedule exists, as checked above; qed");
608 }
609
610 Total::<T>::put(new_total);
611 Claims::<T>::remove(&signer);
612 Vesting::<T>::remove(&signer);
613 Signing::<T>::remove(&signer);
614
615 Self::deposit_event(Event::<T>::Claimed {
617 who: dest,
618 ethereum_address: signer,
619 amount: balance_due,
620 });
621
622 Ok(())
623 }
624}
625
626#[derive(Encode, Decode, DecodeWithMemTracking, Clone, Eq, PartialEq, TypeInfo)]
629#[scale_info(skip_type_params(T))]
630pub struct PrevalidateAttests<T>(core::marker::PhantomData<fn(T)>);
631
632impl<T: Config> Debug for PrevalidateAttests<T>
633where
634 <T as frame_system::Config>::RuntimeCall: IsSubType<Call<T>>,
635{
636 #[cfg(feature = "std")]
637 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
638 write!(f, "PrevalidateAttests")
639 }
640
641 #[cfg(not(feature = "std"))]
642 fn fmt(&self, _: &mut core::fmt::Formatter) -> core::fmt::Result {
643 Ok(())
644 }
645}
646
647impl<T: Config> PrevalidateAttests<T>
648where
649 <T as frame_system::Config>::RuntimeCall: IsSubType<Call<T>>,
650{
651 pub fn new() -> Self {
653 Self(core::marker::PhantomData)
654 }
655}
656
657impl<T: Config> TransactionExtension<T::RuntimeCall> for PrevalidateAttests<T>
658where
659 <T as frame_system::Config>::RuntimeCall: IsSubType<Call<T>>,
660 <<T as frame_system::Config>::RuntimeCall as Dispatchable>::RuntimeOrigin:
661 AsSystemOriginSigner<T::AccountId> + AsTransactionAuthorizedOrigin + Clone,
662{
663 const IDENTIFIER: &'static str = "PrevalidateAttests";
664 type Implicit = ();
665 type Pre = ();
666 type Val = ();
667
668 fn weight(&self, call: &T::RuntimeCall) -> Weight {
669 if let Some(Call::attest { .. }) = call.is_sub_type() {
670 T::WeightInfo::prevalidate_attests()
671 } else {
672 Weight::zero()
673 }
674 }
675
676 fn validate(
677 &self,
678 origin: <T::RuntimeCall as Dispatchable>::RuntimeOrigin,
679 call: &T::RuntimeCall,
680 _info: &DispatchInfoOf<T::RuntimeCall>,
681 _len: usize,
682 _self_implicit: Self::Implicit,
683 _inherited_implication: &impl Encode,
684 _source: TransactionSource,
685 ) -> Result<
686 (ValidTransaction, Self::Val, <T::RuntimeCall as Dispatchable>::RuntimeOrigin),
687 TransactionValidityError,
688 > {
689 if let Some(Call::attest { statement: attested_statement }) = call.is_sub_type() {
690 let who = origin.as_system_origin_signer().ok_or(InvalidTransaction::BadSigner)?;
691 let signer = Preclaims::<T>::get(who)
692 .ok_or(InvalidTransaction::Custom(ValidityError::SignerHasNoClaim.into()))?;
693 if let Some(s) = Signing::<T>::get(signer) {
694 let e = InvalidTransaction::Custom(ValidityError::InvalidStatement.into());
695 ensure!(&attested_statement[..] == s.to_text(), e);
696 }
697 }
698 Ok((ValidTransaction::default(), (), origin))
699 }
700
701 impl_tx_ext_default!(T::RuntimeCall; prepare);
702}
703
704#[cfg(any(test, feature = "runtime-benchmarks"))]
705mod secp_utils {
706 use super::*;
707
708 pub fn public(secret: &libsecp256k1::SecretKey) -> libsecp256k1::PublicKey {
709 libsecp256k1::PublicKey::from_secret_key(secret)
710 }
711 pub fn eth(secret: &libsecp256k1::SecretKey) -> EthereumAddress {
712 let mut res = EthereumAddress::default();
713 res.0.copy_from_slice(&keccak_256(&public(secret).serialize()[1..65])[12..]);
714 res
715 }
716 pub fn sig<T: Config>(
717 secret: &libsecp256k1::SecretKey,
718 what: &[u8],
719 extra: &[u8],
720 ) -> EcdsaSignature {
721 let msg = keccak_256(&super::Pallet::<T>::ethereum_signable_message(
722 &to_ascii_hex(what)[..],
723 extra,
724 ));
725 let (sig, recovery_id) = libsecp256k1::sign(&libsecp256k1::Message::parse(&msg), secret);
726 let mut r = [0u8; 65];
727 r[0..64].copy_from_slice(&sig.serialize()[..]);
728 r[64] = recovery_id.serialize();
729 EcdsaSignature(r)
730 }
731}
732
733#[cfg(test)]
734mod mock;
735
736#[cfg(test)]
737mod tests;
738
739#[cfg(feature = "runtime-benchmarks")]
740mod benchmarking;