mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-06 16:08:08 +00:00
7e2714f843
* Rebuild runtime * Remove invalid value from chainspec (#68) * service: use grandpa block import for locally sealed aura blocks (#85) * bump version to v0.3.1 * Update lock file. * limit number of transactions when building blocks (#91) * Update to latest Substrate * Bump to 0.3.2 * Actually bump. * v0.3.2 (#98) * bump substrate version * fix polkadot-collator * point to alexander-backports of substrate * bump version * cli: fix node shutdown (#100) * update to latest substrate, change to v0.3.4 * update to latest substrate, bump version to 0.3.5 * v0.3.6 * try to build on every v0.3 commit and update alexander-backports * bump to v0.3.7 * bump to 0.3.8 * Bump to 0.3.9: network and pruning improvements * Bump to 0.3.10: reduce network bandwidth usage * Use libp2p-kad 0.3.2 (#122) * Bump libp2p-identify to 0.3.1 (#123) * Bump to 0.3.12 (#127) * Update Substrate again (#128) * update substrate and bump version to v0.3.13 * bump version to v0.3.14: fix --reserved-nodes * add a manually curated grandpa module (#136) * updating v0.3 to use substrate v0.10 (#146) * updating to latest substrate v0.10 * better handling of outer poll * nit * fix tests * remove comment * reduce indentation * use self.poll * bring oneshot into scope * spaces * wrap * remove match * wrap * Update primitives/Cargo.toml Co-Authored-By: gterzian <2792687+gterzian@users.noreply.github.com> * Update runtime/wasm/Cargo.toml Co-Authored-By: gterzian <2792687+gterzian@users.noreply.github.com> * Update runtime/wasm/Cargo.toml Co-Authored-By: gterzian <2792687+gterzian@users.noreply.github.com> * Update test-parachains/adder/collator/src/main.rs Co-Authored-By: gterzian <2792687+gterzian@users.noreply.github.com> * indent * add paranthese * config: fix wrong ip for alexander bootnode (#161) * fix curated-grandpa and rebuild wasm (#162) * [v0.3] Integrates new gossip system into Polkadot (#166) * new gossip validation in network * integrate new gossip into service * network: guard validation network future under exit signal (#168) * bump version to v0.3.15: substrate v0.10 * [v0.3] update to substrate master (#175) * update to substrate master * fix test * service: fix telemetry endpoints on alexander chainspec (#169) (#178) * Update v0.3 to latest Substrate master (#177) * update substrate v0.3 to latest master * bump spec version * update to latest master: remove fees module * update runtime blobs * bump version to 0.3.16 * replace sr25519 accountid with anysigner * bump version to v0.3.17 * Some PoC-3 GRANDPA tweaks (#181) * call on_finalise after triggering curated_grandpa change * make grandpa rounds shorter for faster finalization * use authorities when calculating duty roster (#185) * [v0.3] Update to substrate master (#183) * update to latest substrate master * bump version to 0.3.18 * update to latest substrate master * bump spec version * update runtime wasm blobs * remove current_offline_slash from chain spec * update to substrate master: bump version to v0.3.19 (#188) * update to substrate master: bump version to v0.3.19 libp2p network improvements * network: replace NodeIndex with PeerId * network: fix tests * polkadot v0.3.20 (#190) * update to substrate master: bump version to 0.3.20 * runtime: add offchain worker trait * runtime: rebuild wasm blobs * bump spec version (#191) * Fix compilation * Update version to 0.4.0 * Switch to use `polkadot-master` branch from substrate * Remove unused struct * Remove `grandpa::SyncedAuthorities` from `OnSessionChange`
290 lines
9.5 KiB
Rust
290 lines
9.5 KiB
Rust
// Copyright 2017-2018 Parity Technologies (UK) Ltd.
|
|
// This file is part of Substrate.
|
|
|
|
// 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::*;
|
|
use sr_io::{keccak_256, secp256k1_ecdsa_recover};
|
|
use srml_support::{StorageValue, StorageMap};
|
|
use srml_support::traits::Currency;
|
|
use system::ensure_signed;
|
|
use codec::Encode;
|
|
#[cfg(feature = "std")]
|
|
use sr_primitives::traits::Zero;
|
|
use system;
|
|
|
|
type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as system::Trait>::AccountId>>::Balance;
|
|
|
|
/// Configuration trait.
|
|
pub trait Trait: system::Trait {
|
|
/// The overarching event type.
|
|
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
|
|
type Currency: Currency<Self::AccountId>;
|
|
}
|
|
|
|
type EthereumAddress = [u8; 20];
|
|
|
|
// This is a bit of a workaround until codec supports [u8; 65] directly.
|
|
#[derive(Encode, Decode, Clone, PartialEq)]
|
|
#[cfg_attr(feature = "std", derive(Debug))]
|
|
pub struct EcdsaSignature([u8; 32], [u8; 32], i8);
|
|
|
|
impl EcdsaSignature {
|
|
pub fn to_blob(&self) -> [u8; 65] {
|
|
let mut r = [0u8; 65];
|
|
r[0..32].copy_from_slice(&self.0[..]);
|
|
r[32..64].copy_from_slice(&self.1[..]);
|
|
r[64] = self.2 as u8;
|
|
r
|
|
}
|
|
pub fn from_blob(blob: &[u8; 65]) -> Self {
|
|
let mut r = Self([0u8; 32], [0u8; 32], 0);
|
|
r.0[..].copy_from_slice(&blob[0..32]);
|
|
r.1[..].copy_from_slice(&blob[32..64]);
|
|
r.2 = blob[64] as i8;
|
|
r
|
|
}
|
|
}
|
|
|
|
decl_event!(
|
|
pub enum Event<T> where
|
|
B = BalanceOf<T>,
|
|
A = <T as system::Trait>::AccountId
|
|
{
|
|
/// Someone claimed some DOTs.
|
|
Claimed(A, EthereumAddress, B),
|
|
}
|
|
);
|
|
|
|
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<_>>()
|
|
}): map EthereumAddress => Option<BalanceOf<T>>;
|
|
Total get(total) build(|config: &GenesisConfig<T>| {
|
|
config.claims.iter().fold(Zero::zero(), |acc: BalanceOf<T>, &(_, n)| acc + n)
|
|
}): BalanceOf<T>;
|
|
}
|
|
add_extra_genesis {
|
|
config(claims): Vec<(EthereumAddress, BalanceOf<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 = b"Pay DOTs to the Polkadot account:";
|
|
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(ðereum_signable_message(what));
|
|
let mut res = EthereumAddress::default();
|
|
res.copy_from_slice(&keccak_256(&secp256k1_ecdsa_recover(&s.to_blob(), &msg).ok()?[..])[12..]);
|
|
Some(res)
|
|
}
|
|
|
|
decl_module! {
|
|
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
|
|
/// Deposit one of this module's events by using the default implementation.
|
|
fn deposit_event<T>() = default;
|
|
|
|
/// Make a claim.
|
|
fn claim(origin, ethereum_signature: EcdsaSignature) {
|
|
// This is a public call, so we ensure that the origin is some signed account.
|
|
let sender = ensure_signed(origin)?;
|
|
|
|
let signer = sender.using_encoded(|data|
|
|
eth_recover(ðereum_signature, data)
|
|
).ok_or("Invalid Ethereum signature")?;
|
|
|
|
let balance_due = <Claims<T>>::take(&signer)
|
|
.ok_or("Ethereum address has no claim")?;
|
|
|
|
<Total<T>>::mutate(|t| if *t < balance_due {
|
|
panic!("Logic error: Pot less than the total of claims!")
|
|
} else {
|
|
*t -= balance_due
|
|
});
|
|
|
|
T::Currency::deposit_creating(&sender, balance_due);
|
|
|
|
// Let's deposit an event to let the outside world know this happened.
|
|
Self::deposit_event(RawEvent::Claimed(sender, signer, balance_due));
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use secp256k1;
|
|
use tiny_keccak::keccak256;
|
|
use super::*;
|
|
|
|
use sr_io::{self as runtime_io, with_externalities};
|
|
use substrate_primitives::{H256, Blake2Hasher};
|
|
use codec::{Decode, Encode};
|
|
// The testing primitives are very useful for avoiding having to work with signatures
|
|
// or public keys. `u64` is used as the `AccountId` and no `Signature`s are requried.
|
|
use sr_primitives::{
|
|
BuildStorage, traits::{BlakeTwo256, IdentityLookup}, testing::{Digest, DigestItem, Header}
|
|
};
|
|
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;
|
|
impl system::Trait for Test {
|
|
type Origin = Origin;
|
|
type Index = u64;
|
|
type BlockNumber = u64;
|
|
type Hash = H256;
|
|
type Hashing = BlakeTwo256;
|
|
type Digest = Digest;
|
|
type AccountId = u64;
|
|
type Lookup = IdentityLookup<u64>;
|
|
type Header = Header;
|
|
type Event = ();
|
|
type Log = DigestItem;
|
|
}
|
|
impl balances::Trait for Test {
|
|
type Balance = u64;
|
|
type OnFreeBalanceZero = ();
|
|
type OnNewAccount = ();
|
|
type Event = ();
|
|
type TransactionPayment = ();
|
|
type DustRemoval = ();
|
|
type TransferPayment = ();
|
|
}
|
|
impl Trait for Test {
|
|
type Event = ();
|
|
type Currency = Balances;
|
|
}
|
|
type Balances = balances::Module<Test>;
|
|
type Claims = Module<Test>;
|
|
|
|
fn alice_secret() -> secp256k1::SecretKey {
|
|
secp256k1::SecretKey::parse(&keccak256(b"Alice")).unwrap()
|
|
}
|
|
fn alice_public() -> secp256k1::PublicKey {
|
|
secp256k1::PublicKey::from_secret_key(&alice_secret())
|
|
}
|
|
fn alice_eth() -> EthereumAddress {
|
|
let mut res = EthereumAddress::default();
|
|
res.copy_from_slice(&keccak256(&alice_public().serialize()[1..65])[12..]);
|
|
res
|
|
}
|
|
fn alice_sig(what: &[u8]) -> EcdsaSignature {
|
|
let msg = keccak256(ðereum_signable_message(what));
|
|
let (sig, recovery_id) = secp256k1::sign(&secp256k1::Message::parse(&msg), &alice_secret()).unwrap();
|
|
let sig: ([u8; 32], [u8; 32]) = Decode::decode(&mut &sig.serialize()[..]).unwrap();
|
|
EcdsaSignature(sig.0, sig.1, recovery_id.serialize() as i8)
|
|
}
|
|
fn bob_secret() -> secp256k1::SecretKey {
|
|
secp256k1::SecretKey::parse(&keccak256(b"Bob")).unwrap()
|
|
}
|
|
fn bob_sig(what: &[u8]) -> EcdsaSignature {
|
|
let msg = keccak256(ðereum_signable_message(what));
|
|
let (sig, recovery_id) = secp256k1::sign(&secp256k1::Message::parse(&msg), &bob_secret()).unwrap();
|
|
let sig: ([u8; 32], [u8; 32]) = Decode::decode(&mut &sig.serialize()[..]).unwrap();
|
|
EcdsaSignature(sig.0, sig.1, recovery_id.serialize() as i8)
|
|
}
|
|
|
|
// This function basically just builds a genesis storage key/value store according to
|
|
// our desired mockup.
|
|
fn new_test_ext() -> sr_io::TestExternalities<Blake2Hasher> {
|
|
let mut t = system::GenesisConfig::<Test>::default().build_storage().unwrap().0;
|
|
// We use default for brevity, but you can configure as desired if needed.
|
|
t.extend(balances::GenesisConfig::<Test>::default().build_storage().unwrap().0);
|
|
t.extend(GenesisConfig::<Test>{
|
|
claims: vec![(alice_eth(), 100)],
|
|
}.build_storage().unwrap().0);
|
|
t.into()
|
|
}
|
|
|
|
#[test]
|
|
fn basic_setup_works() {
|
|
with_externalities(&mut new_test_ext(), || {
|
|
assert_eq!(Claims::total(), 100);
|
|
assert_eq!(Claims::claims(&alice_eth()), Some(100));
|
|
assert_eq!(Claims::claims(&[0; 20]), None);
|
|
});
|
|
}
|
|
|
|
#[test]
|
|
fn claiming_works() {
|
|
with_externalities(&mut new_test_ext(), || {
|
|
assert_eq!(Balances::free_balance(&42), 0);
|
|
assert_ok!(Claims::claim(Origin::signed(42), alice_sig(&42u64.encode())));
|
|
assert_eq!(Balances::free_balance(&42), 100);
|
|
});
|
|
}
|
|
|
|
#[test]
|
|
fn double_claiming_doesnt_work() {
|
|
with_externalities(&mut new_test_ext(), || {
|
|
assert_eq!(Balances::free_balance(&42), 0);
|
|
assert_ok!(Claims::claim(Origin::signed(42), alice_sig(&42u64.encode())));
|
|
assert_noop!(Claims::claim(Origin::signed(42), alice_sig(&42u64.encode())), "Ethereum address has no claim");
|
|
});
|
|
}
|
|
|
|
#[test]
|
|
fn non_sender_sig_doesnt_work() {
|
|
with_externalities(&mut new_test_ext(), || {
|
|
assert_eq!(Balances::free_balance(&42), 0);
|
|
assert_noop!(Claims::claim(Origin::signed(42), alice_sig(&69u64.encode())), "Ethereum address has no claim");
|
|
});
|
|
}
|
|
|
|
#[test]
|
|
fn non_claimant_doesnt_work() {
|
|
with_externalities(&mut new_test_ext(), || {
|
|
assert_eq!(Balances::free_balance(&42), 0);
|
|
assert_noop!(Claims::claim(Origin::signed(42), bob_sig(&69u64.encode())), "Ethereum address has no claim");
|
|
});
|
|
}
|
|
|
|
#[test]
|
|
fn real_eth_sig_works() {
|
|
let sig = hex!["7505f2880114da51b3f5d535f8687953c0ab9af4ab81e592eaebebf53b728d2b6dfd9b5bcd70fee412b1f31360e7c2774009305cb84fc50c1d0ff8034dfa5fff1c"];
|
|
let sig = EcdsaSignature::from_blob(&sig);
|
|
let who = 42u64.encode();
|
|
let signer = eth_recover(&sig, &who).unwrap();
|
|
assert_eq!(signer, hex!["DF67EC7EAe23D2459694685257b6FC59d1BAA1FE"]);
|
|
}
|
|
}
|