diff --git a/Cargo.lock b/Cargo.lock index c13c49a45..0e1ae127f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2316,7 +2316,6 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-core", - "sp-io", "sp-keyring", "sp-runtime", "sp-std", diff --git a/pallet/account-migration/Cargo.toml b/pallet/account-migration/Cargo.toml index db1b78f78..3ad71d51a 100644 --- a/pallet/account-migration/Cargo.toml +++ b/pallet/account-migration/Cargo.toml @@ -26,7 +26,6 @@ pallet-assets = { default-features = false, git = "https://github.com/parityte pallet-balances = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" } pallet-vesting = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" } sp-core = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" } -sp-io = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" } sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" } sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" } @@ -52,7 +51,6 @@ std = [ "pallet-balances/std", "pallet-vesting/std", "sp-core/std", - "sp-io/std", "sp-runtime/std", "sp-std/std", ] diff --git a/pallet/account-migration/src/lib.rs b/pallet/account-migration/src/lib.rs index b44464923..dccfd7988 100644 --- a/pallet/account-migration/src/lib.rs +++ b/pallet/account-migration/src/lib.rs @@ -61,20 +61,18 @@ use frame_system::{pallet_prelude::*, AccountInfo, RawOrigin}; use pallet_balances::AccountData; use pallet_vesting::VestingInfo; use sp_core::sr25519::{Public, Signature}; -use sp_io::hashing; use sp_runtime::{ traits::{IdentityLookup, Verify}, AccountId32, }; use sp_std::prelude::*; -type Message = [u8; 32]; -const KTON_ID: u64 = 1026; - #[frame_support::pallet] pub mod pallet { use super::*; + const KTON_ID: u64 = 1026; + #[pallet::pallet] pub struct Pallet(PhantomData); @@ -94,9 +92,6 @@ pub mod pallet { { /// Override the [`frame_system::Config::RuntimeEvent`]. type RuntimeEvent: From + IsType<::RuntimeEvent>; - /// Chain's ID, which is used for constructing the message. (follow EIP-712 SPEC) - #[pallet::constant] - type ChainId: Get; } #[allow(missing_docs)] @@ -263,11 +258,7 @@ pub mod pallet { return InvalidTransaction::Custom(E_ACCOUNT_ALREADY_EXISTED).into(); } - let message = sr25519_signable_message( - T::ChainId::get(), - T::Version::get().spec_name.as_ref(), - to, - ); + let message = sr25519_signable_message(T::Version::get().spec_name.as_ref(), to); if verify_sr25519_signature(from, &message, signature) { ValidTransaction::with_tag_prefix("account-migration") @@ -284,25 +275,22 @@ pub mod pallet { } pub use pallet::*; -fn sr25519_signable_message( - chain_id: u64, - spec_name: &[u8], - account_id_20: &AccountId20, -) -> Message { - hashing::blake2_256( - &[ - &hashing::blake2_256( - &[&chain_id.to_le_bytes(), spec_name, b"::account-migration"].concat(), - ), - account_id_20.0.as_slice(), - ] - .concat(), - ) +fn sr25519_signable_message(spec_name: &[u8], account_id_20: &AccountId20) -> Vec { + [ + b"I authorize the migration to ", + account_id_20.0.as_slice(), + b", an unused address on ", + spec_name, + b". Sign this message to authorize using the Substrate key associated with the account on ", + &spec_name[..spec_name.len() - 1], + b" that you wish to migrate.", + ] + .concat() } fn verify_sr25519_signature( public_key: &AccountId32, - message: &Message, + message: &[u8], signature: &Signature, ) -> bool { // Actually, `&[u8]` is `[u8; 32]` here. @@ -313,5 +301,5 @@ fn verify_sr25519_signature( return false; }; - signature.verify(message.as_slice(), public_key) + signature.verify(message, public_key) } diff --git a/pallet/account-migration/src/tests.rs b/pallet/account-migration/src/tests.rs index be89f89e3..849820ca8 100644 --- a/pallet/account-migration/src/tests.rs +++ b/pallet/account-migration/src/tests.rs @@ -23,35 +23,24 @@ use sp_keyring::sr25519::Keyring; #[test] fn sr25519_signable_message_should_work() { - [(46_u64, b"Darwinia2".as_slice()), (44, b"Crab2"), (43, b"Pangolin2")] - .iter() - .zip([ - [ - 75, 134, 66, 181, 153, 10, 7, 244, 225, 154, 100, 68, 239, 19, 129, 51, 181, 78, - 66, 254, 167, 54, 211, 20, 171, 68, 160, 46, 216, 98, 9, 44, - ], - [ - 171, 8, 180, 157, 214, 41, 236, 80, 127, 218, 216, 136, 239, 56, 153, 31, 128, 168, - 154, 112, 70, 245, 19, 68, 53, 29, 49, 95, 238, 209, 238, 129, - ], - [ - 251, 70, 107, 65, 22, 164, 1, 85, 114, 150, 161, 208, 235, 131, 15, 111, 154, 207, - 193, 216, 110, 54, 58, 177, 15, 99, 104, 179, 13, 30, 55, 205, - ], - ]) - .for_each(|((chain_id, spec_name), message)| { - assert_eq!( - sr25519_signable_message(*chain_id, spec_name, &Default::default()), - message - ); - }); + ["Darwinia2", "Crab2", "Pangolin2"].iter().for_each(|s| { + assert_eq!( + sr25519_signable_message(s.as_bytes(), &Default::default()), + format!( + "I authorize the migration to {}, an unused address on {}. Sign this message to authorize using the Substrate key associated with the account on {} that you wish to migrate.", + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", + s, + &s[..s.len() - 1], + ).as_bytes() + ); + }); } #[test] fn verify_sr25519_signature_should_work() { Keyring::iter().enumerate().for_each(|(i, from)| { let to = [i as _; 20]; - let message = sr25519_signable_message(46, b"Darwinia2", &to.into()); + let message = sr25519_signable_message(b"Darwinia2", &to.into()); let signature = from.sign(&message); assert!(verify_sr25519_signature(&from.public().0.into(), &message, &signature)); diff --git a/runtime/crab/src/pallets/account_migration.rs b/runtime/crab/src/pallets/account_migration.rs index 1797da2ec..5e230b464 100644 --- a/runtime/crab/src/pallets/account_migration.rs +++ b/runtime/crab/src/pallets/account_migration.rs @@ -20,6 +20,5 @@ use crate::*; impl darwinia_account_migration::Config for Runtime { - type ChainId = ::ChainId; type RuntimeEvent = RuntimeEvent; } diff --git a/runtime/darwinia/src/pallets/account_migration.rs b/runtime/darwinia/src/pallets/account_migration.rs index 1797da2ec..5e230b464 100644 --- a/runtime/darwinia/src/pallets/account_migration.rs +++ b/runtime/darwinia/src/pallets/account_migration.rs @@ -20,6 +20,5 @@ use crate::*; impl darwinia_account_migration::Config for Runtime { - type ChainId = ::ChainId; type RuntimeEvent = RuntimeEvent; } diff --git a/runtime/pangolin/src/pallets/account_migration.rs b/runtime/pangolin/src/pallets/account_migration.rs index 6eeca3e9c..5e230b464 100644 --- a/runtime/pangolin/src/pallets/account_migration.rs +++ b/runtime/pangolin/src/pallets/account_migration.rs @@ -20,6 +20,5 @@ use crate::*; impl darwinia_account_migration::Config for Runtime { - type ChainId = ::ChainId; type RuntimeEvent = RuntimeEvent; }