Skip to content

Commit

Permalink
Updates build and work on substrate to evm test
Browse files Browse the repository at this point in the history
  • Loading branch information
drewstone committed Dec 4, 2023
1 parent 0be3852 commit a338851
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
15 changes: 12 additions & 3 deletions pallets/claims/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub use pallet::*;
use pallet_evm::AddressMapping;
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;
use schnorrkel::{signing_context, PublicKey, Signature};
use serde::{self, Deserialize, Serialize};
use sp_core::H160;
use sp_io::{crypto::secp256k1_ecdsa_recover, hashing::keccak_256};
Expand Down Expand Up @@ -558,8 +559,16 @@ impl<T: Config> Pallet<T> {
extra: &[u8],
) -> Option<MultiAddress> {
let msg = keccak_256(&Self::polkadotjs_signable_message(what, extra));
let res = AccountId32::new([0; 32]);
Some(MultiAddress::Native(res))
let pk: PublicKey = match addr.clone() {
MultiAddress::EVM(_) => return None,
MultiAddress::Native(a) => PublicKey::from_bytes(&a.encode()).ok()?,
};
let signature: Signature = Signature::from_bytes(&s.0.encode()).ok()?;
const SIGNING_CTX: &'static [u8] = b"substrate";
match pk.verify_simple(SIGNING_CTX, &msg, &signature) {
Ok(_) => Some(addr),
Err(_) => None,
}
}

fn process_claim(
Expand Down Expand Up @@ -616,7 +625,7 @@ impl<T: Config> Pallet<T> {
Self::eth_recover(&ethereum_signature, &data, &statement[..])
.ok_or(Error::<T>::InvalidEthereumSignature)?,
MultiAddressSignature::Native(sr25519_signature) => {
ensure!(signer.is_none(), Error::<T>::InvalidNativeAccount);
ensure!(!signer.is_none(), Error::<T>::InvalidNativeAccount);
Self::sr25519_recover(signer.unwrap(), &sr25519_signature, &data, &statement[..])
.ok_or(Error::<T>::InvalidNativeSignature)?
},
Expand Down
51 changes: 51 additions & 0 deletions pallets/claims/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -734,3 +734,54 @@ fn test_unclaimed_returned_to_destination() {
);
});
}

#[test]
fn test_claim_from_substrate_address_to_evm() {
new_test_ext().execute_with(|| {
let original_total_claims = Total::<Test>::get();
let claim_of_sub_alice = 500;
assert_ok!(ClaimsPallet::claim(
RuntimeOrigin::none(),
Some(get_multi_address_account_id(42)),
Some(sr25519_utils::sub(&alice_sr25519())),
sr25519_utils::sig::<Test>(
&alice_sr25519(),
&Some(get_multi_address_account_id(42)).encode(),
&[][..]
)
));
assert_eq!(Total::<Test>::get(), original_total_claims - claim_of_sub_alice);

// force set the expiry config
assert_ok!(ClaimsPallet::force_set_expiry_config(
RuntimeOrigin::root(),
5,
get_multi_address_account_id(100)
));

// run to after expiry block
run_to_block(7);
assert_eq!(Total::<Test>::get(), 0);
// the dest account should receive the remaining pot balance
assert_eq!(
Balances::free_balance(get_multi_address_account_id(100).to_account_id_32()),
original_total_claims - claim_of_sub_alice
);

// all further claims should fail with PotUnderflow error since the funds have been
// emptied
assert_noop!(
ClaimsPallet::claim(
RuntimeOrigin::none(),
Some(get_multi_address_account_id(42)),
None,
sr25519_utils::sig::<Test>(
&bob_sr25519(),
&Some(get_multi_address_account_id(42)).encode(),
&[][..]
)
),
Error::<Test>::PotUnderflow
);
});
}
2 changes: 2 additions & 0 deletions pallets/claims/src/utils/ethereum_address.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::*;

use serde::{Deserialize, Deserializer, Serialize, Serializer};

/// An Ethereum address (i.e. 20 bytes, used to represent an Ethereum account).
///
/// This gets serialized to the 0x-prefixed hex representation.
Expand Down

0 comments on commit a338851

Please sign in to comment.