Skip to content

Commit

Permalink
Human readable sign message (#195)
Browse files Browse the repository at this point in the history
* Human readable sign message

* Update spec
  • Loading branch information
AurevoirXavier committed Jan 11, 2023
1 parent 756fcaf commit db36081
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 57 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions pallet/account-migration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }

Expand All @@ -52,7 +51,6 @@ std = [
"pallet-balances/std",
"pallet-vesting/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
]
44 changes: 16 additions & 28 deletions pallet/account-migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(PhantomData<T>);

Expand All @@ -94,9 +92,6 @@ pub mod pallet {
{
/// Override the [`frame_system::Config::RuntimeEvent`].
type RuntimeEvent: From<Event> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// Chain's ID, which is used for constructing the message. (follow EIP-712 SPEC)
#[pallet::constant]
type ChainId: Get<u64>;
}

#[allow(missing_docs)]
Expand Down Expand Up @@ -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")
Expand All @@ -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<u8> {
[
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.
Expand All @@ -313,5 +301,5 @@ fn verify_sr25519_signature(
return false;
};

signature.verify(message.as_slice(), public_key)
signature.verify(message, public_key)
}
35 changes: 12 additions & 23 deletions pallet/account-migration/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
1 change: 0 additions & 1 deletion runtime/crab/src/pallets/account_migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@
use crate::*;

impl darwinia_account_migration::Config for Runtime {
type ChainId = <Runtime as pallet_evm::Config>::ChainId;
type RuntimeEvent = RuntimeEvent;
}
1 change: 0 additions & 1 deletion runtime/darwinia/src/pallets/account_migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@
use crate::*;

impl darwinia_account_migration::Config for Runtime {
type ChainId = <Runtime as pallet_evm::Config>::ChainId;
type RuntimeEvent = RuntimeEvent;
}
1 change: 0 additions & 1 deletion runtime/pangolin/src/pallets/account_migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@
use crate::*;

impl darwinia_account_migration::Config for Runtime {
type ChainId = <Self as pallet_evm::Config>::ChainId;
type RuntimeEvent = RuntimeEvent;
}

0 comments on commit db36081

Please sign in to comment.