Skip to content

Commit

Permalink
bugfix: Fix claims pallet (#394)
Browse files Browse the repository at this point in the history
* Fix claims pallet

* Add claims test account to the testnet

* DRY

* fix: correct the way to wrap the message (#396)

---------

Co-authored-by: Trung-Tin Pham <[email protected]>
  • Loading branch information
shekohex and AtelyPham authored Jan 4, 2024
1 parent a2acee0 commit e795313
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 89 deletions.
1 change: 0 additions & 1 deletion node/src/chainspec/mainnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ pub fn local_mainnet_config(chain_id: u64) -> Result<ChainSpec, String> {
mainnet::get_edgeware_genesis_balance_distribution(),
mainnet::get_leaderboard_balance_distribution(),
mainnet::get_substrate_balance_distribution(),
mainnet::get_local_balance_distribution(),
]),
// Genesis investor / team distribution (pallet-balances + pallet-vesting)
combine_distributions(vec![
Expand Down
22 changes: 18 additions & 4 deletions node/src/chainspec/testnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,12 @@ pub fn local_testnet_config(chain_id: u64) -> Result<ChainSpec, String> {
get_account_id_from_seed::<sr25519::Public>("Eve"),
],
chain_id,
vec![],
vec![],
combine_distributions(vec![
develop::get_evm_balance_distribution(),
testnet::get_evm_balance_distribution(),
]),
testnet::get_substrate_balance_distribution(),
develop::get_local_claims(),
true,
)
},
Expand Down Expand Up @@ -199,6 +203,7 @@ pub fn tangle_testnet_config(chain_id: u64) -> Result<ChainSpec, String> {
testnet::get_evm_balance_distribution(),
]),
testnet::get_substrate_balance_distribution(),
vec![],
true,
)
},
Expand Down Expand Up @@ -228,6 +233,7 @@ fn testnet_genesis(
chain_id: u64,
genesis_evm_distribution: Vec<(H160, fp_evm::GenesisAccount)>,
genesis_substrate_distribution: Vec<(AccountId, Balance)>,
claims: Vec<(MultiAddress, Balance)>,
_enable_println: bool,
) -> RuntimeGenesisConfig {
const ENDOWMENT: Balance = 10_000_000 * UNIT;
Expand All @@ -242,10 +248,13 @@ fn testnet_genesis(
.collect();

let num_endowed_accounts = endowed_accounts.len();
let claims: Vec<(MultiAddress, Balance, Option<StatementKind>)> = endowed_accounts

let claims_list: Vec<(MultiAddress, Balance, Option<StatementKind>)> = endowed_accounts
.iter()
.map(|x| (MultiAddress::Native(x.clone()), ENDOWMENT, Some(StatementKind::Regular)))
.chain(claims.clone().into_iter().map(|(a, b)| (a, b, Some(StatementKind::Regular))))
.collect();

let vesting_claims: Vec<(
MultiAddress,
BoundedVec<(Balance, Balance, BlockNumber), MaxVestingSchedules>,
Expand All @@ -256,6 +265,11 @@ fn testnet_genesis(
bounded_vec.try_push((ENDOWMENT, ENDOWMENT, 0)).unwrap();
(MultiAddress::Native(x.clone()), bounded_vec)
})
.chain(claims.into_iter().map(|(a, b)| {
let mut bounded_vec = BoundedVec::new();
bounded_vec.try_push((b, b, 0)).unwrap();
(a, bounded_vec)
}))
.collect();

RuntimeGenesisConfig {
Expand Down Expand Up @@ -338,7 +352,7 @@ fn testnet_genesis(
phantom: PhantomData,
},
claims: ClaimsConfig {
claims,
claims: claims_list,
vesting: vesting_claims,
expiry: Some((
200u64,
Expand Down
42 changes: 42 additions & 0 deletions node/src/distributions/develop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,49 @@
use std::str::FromStr;

use fp_evm::GenesisAccount;
use pallet_airdrop_claims::MultiAddress;
use sp_core::{H160, U256};
use sp_runtime::AccountId32;
use tangle_primitives::Balance;

pub fn get_local_claims() -> Vec<(MultiAddress, Balance)> {
vec![
// Test account with a simple menmonic
// Mnemonic: "test test test test test test test test test test test junk"
// Path: m/44'/60'/0'/0/0
// Private Key: 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
(
MultiAddress::EVM(
H160::from_str("f39Fd6e51aad88F6F4ce6aB8827279cffFb92266")
.expect("internal H160 is valid; qed")
.into(),
),
1_000_000_000_000_000_000_000_000u128,
),
(
MultiAddress::EVM(
H160::from_str("2DFA35bd8C59C38FB3eC4e71b0106160E130A40E")
.expect("internal H160 is valid; qed")
.into(),
),
1_000_000_000_000_000_000_000_000u128,
),
(
MultiAddress::Native(
AccountId32::from_str("5EbkKKTdRJzP1j3aM3S7q178du6tW7ZVWK9Dtjx9CbTFEpGf")
.expect("internal AccountId32 is valid; qed"),
),
1_000_000_000_000_000_000_000_000u128,
),
(
MultiAddress::Native(
AccountId32::from_str("5DLXgUoVVeCZKHduaVhkH4RvLcyG1GdQwLqYLd4aFuYX1qve")
.expect("internal AccountId32 is valid; qed"),
),
1_000_000_000_000_000_000_000_000u128,
),
]
}

pub fn get_evm_balance_distribution() -> Vec<(H160, GenesisAccount)> {
vec![
Expand Down
39 changes: 0 additions & 39 deletions node/src/distributions/mainnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,45 +165,6 @@ pub fn get_leaderboard_balance_distribution() -> DistributionResult {
)
}

/// Used for testing purposes
///
/// DO NOT USE IN MAINNET
pub fn get_local_balance_distribution() -> DistributionResult {
let list = vec![
// Test account with a simple menmonic
// Mnemonic: "test test test test test test test test test test test junk"
// Path: m/44'/60'/0'/0/0
// Private Key: 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
H160::from_str("f39Fd6e51aad88F6F4ce6aB8827279cffFb92266")
.expect("internal H160 is valid; qed"),
// Test account with a simple menmonic
// Mnemonic: "test test test test test test test test test test test junk"
// Path: m/44'/60'/0'/0/1
// Private Key: 0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d
H160::from_str("70997970C51812dc3A010C7d01b50e0d17dc79C8")
.expect("internal H160 is valid; qed"),
// H160 address of Alice dev account
// Derived from SS58 (42 prefix) address
// SS58: 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY
// hex: 0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d
// Using the full hex key, truncating to the first 20 bytes (the first 40 hex
// chars)
H160::from_str("d43593c715fdd31c61141abd04a99fd6822c8558")
.expect("internal H160 is valid; qed"),
];
let endowment = ONE_PERCENT_TOTAL_SUPPLY / list.len() as u128;
let local_list: Vec<(MultiAddress, u128)> = list
.into_iter()
.map(|address| (MultiAddress::EVM(EthereumAddress(address.0)), endowment))
.collect();
get_distribution_for(
local_list,
Some(StatementKind::Regular),
ONE_MONTH_BLOCKS,
TWO_YEARS_BLOCKS,
)
}

pub fn get_substrate_balance_distribution() -> DistributionResult {
let arr = get_edgeware_snapshot_list()
.into_iter()
Expand Down
41 changes: 32 additions & 9 deletions pallets/claims/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,7 @@ pub mod pallet {
signature: MultiAddressSignature,
) -> DispatchResult {
ensure_none(origin)?;

let data = dest.using_encoded(to_ascii_hex);
let data = dest.as_ref().map(Self::encode_multi_address).unwrap_or_default();
let signer = Self::get_signer_multi_address(signer.clone(), signature, data, vec![])?;
ensure!(Signing::<T>::get(&signer).is_none(), Error::<T>::InvalidStatement);
Self::process_claim(signer, dest)?;
Expand Down Expand Up @@ -407,8 +406,7 @@ pub mod pallet {
statement: Vec<u8>,
) -> DispatchResult {
ensure_none(origin)?;

let data = dest.using_encoded(to_ascii_hex);
let data = dest.as_ref().map(Self::encode_multi_address).unwrap_or_default();
let signer =
Self::get_signer_multi_address(signer.clone(), signature, data, statement.clone())?;
if let Some(s) = Signing::<T>::get(signer.clone()) {
Expand Down Expand Up @@ -460,7 +458,7 @@ pub mod pallet {
// The weight of this logic is included in the `claim` dispatchable.
// </weight>
Call::claim { dest: account, signer, signature } => {
let data = account.using_encoded(to_ascii_hex);
let data = account.as_ref().map(Self::encode_multi_address).unwrap_or_default();
match Self::get_signer_multi_address(
signer.clone(),
signature.clone(),
Expand All @@ -475,7 +473,7 @@ pub mod pallet {
// The weight of this logic is included in the `claim_attest` dispatchable.
// </weight>
Call::claim_attest { dest: account, signer, signature, statement } => {
let data = account.using_encoded(to_ascii_hex);
let data = account.as_ref().map(Self::encode_multi_address).unwrap_or_default();
match Self::get_signer_multi_address(
signer.clone(),
signature.clone(),
Expand Down Expand Up @@ -525,7 +523,13 @@ fn to_ascii_hex(data: &[u8]) -> Vec<u8> {
}

impl<T: Config> Pallet<T> {
// Constructs the message that Ethereum RPC's `personal_sign` and `eth_sign` would sign.
fn encode_multi_address(multi_address: &MultiAddress) -> Vec<u8> {
match multi_address {
MultiAddress::EVM(ref address) => address.using_encoded(to_ascii_hex),
MultiAddress::Native(ref address) => address.using_encoded(to_ascii_hex),
}
}
/// Constructs the message that Ethereum RPC's `personal_sign` and `eth_sign` would sign.
fn ethereum_signable_message(what: &[u8], extra: &[u8]) -> Vec<u8> {
let prefix = T::Prefix::get();
let mut l = prefix.len() + what.len() + extra.len();
Expand Down Expand Up @@ -554,8 +558,10 @@ impl<T: Config> Pallet<T> {

// Constructs the message that PolkadotJS would sign.
fn polkadotjs_signable_message(what: &[u8], extra: &[u8]) -> Vec<u8> {
let mut v = Vec::new();
let prefix = T::Prefix::get();
let mut v = prefix.to_vec();

v.extend_from_slice(prefix);
v.extend_from_slice(what);
v.extend_from_slice(extra);
v
Expand All @@ -570,6 +576,7 @@ impl<T: Config> Pallet<T> {
extra: &[u8],
) -> Option<MultiAddress> {
let msg = keccak_256(&Self::polkadotjs_signable_message(what, extra));

let public: Public = match addr.clone() {
MultiAddress::EVM(_) => return None,
MultiAddress::Native(a) => {
Expand All @@ -578,9 +585,25 @@ impl<T: Config> Pallet<T> {
Public(bytes)
},
};

match sr25519_verify(&s.0, &msg, &public) {
true => Some(addr),
false => None,
false => {
// If the signature verification fails, we try to wrap the hashed msg in a
// `<Bytes></Bytes>` tag and try again.
let polkadotjs_prefix = b"<Bytes>";
let polkadotjs_suffix = b"</Bytes>";

let mut wrapped_msg = Vec::new();
wrapped_msg.extend_from_slice(polkadotjs_prefix);
wrapped_msg.extend_from_slice(&msg);
wrapped_msg.extend_from_slice(polkadotjs_suffix);

match sr25519_verify(&s.0, &wrapped_msg, &public) {
true => Some(addr),
false => None,
}
},
}
}

Expand Down
Loading

0 comments on commit e795313

Please sign in to comment.