Skip to content

Commit

Permalink
Update ecdsa-authority spec (#1022)
Browse files Browse the repository at this point in the history
* Fix ecdsa-authority test

* Use `to_be_bytes`

---------

Co-authored-by: Xavier Lau <xavier@inv.cafe>
  • Loading branch information
hujw77 and AurevoirXavier authored Mar 16, 2023
1 parent 478f126 commit 3c07f7b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
4 changes: 2 additions & 2 deletions pallet/ecdsa-authority/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ pub mod pallet {
),
}
};
let message = Sign::eth_signable_message(
let message = Sign::signable_message(
T::ChainId::get(),
T::Version::get().spec_name.as_ref(),
&ethabi::encode(&[
Expand Down Expand Up @@ -547,7 +547,7 @@ pub mod pallet {
message_root,
nonce: <Nonce<T>>::get(),
};
let message = Sign::eth_signable_message(
let message = Sign::signable_message(
T::ChainId::get(),
T::Version::get().spec_name.as_ref(),
&ethabi::encode(&[
Expand Down
33 changes: 20 additions & 13 deletions pallet/ecdsa-authority/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,16 @@ impl Sign {
hashing::keccak_256(data)
}

pub(crate) fn eth_signable_message(chain_id: u64, spec_name: &[u8], data: &[u8]) -> Hash {
// \x19\x01 + keccack256(ChainIDSpecName::ecdsa-authority) + struct_hash
pub fn domain_separator(chain_id: u64, spec_name: &[u8]) -> [u8; 32] {
Self::hash(&[&chain_id.to_be_bytes(), spec_name, b"::ecdsa-authority"].concat())
}

// \x19\x01 + keccack256(ChainIDSpecName::ecdsa-authority) + struct_hash
pub(crate) fn signable_message(chain_id: u64, spec_name: &[u8], data: &[u8]) -> Hash {
Hash(Self::hash(
&[
b"\x19\x01".as_slice(),
&Self::hash(&[&chain_id.to_le_bytes(), spec_name, b"::ecdsa-authority"].concat()),
&Self::domain_separator(chain_id, spec_name),
&Self::hash(data),
]
.concat(),
Expand Down Expand Up @@ -102,20 +106,23 @@ pub struct Commitment {
}

#[test]
fn eth_signable_message() {
fn signable_message() {
assert_eq!(
array_bytes::bytes2hex("0x", Sign::eth_signable_message(46, b"Darwinia", &[0; 32])),
"0xb492857010088b0dff298645e9105549d088aab7bcb20cf5a3d0bc17dce91045"
array_bytes::bytes2hex("0x", Sign::domain_separator(46, b"Darwinia").as_ref()),
"0xc494742e979bd6ab2dca4950fddd8809e1502ab8ef7b8d749364ec32cb6e1b3e"
);
assert_eq!(
array_bytes::bytes2hex("0x", Sign::hash(b"46Darwinia::ecdsa-authority")),
"0xf8a76f5ceeff36d74ff99c4efc0077bcc334721f17d1d5f17cfca78455967e1e"
array_bytes::bytes2hex("0x", Sign::domain_separator(43, b"Pangolin2").as_ref()),
"0xe97c73e46305f3bca2279f002665725cd29e465c6624e83a135f7b2e6b1a8134"
);

let data = array_bytes::hex2bytes_unchecked("0x30a82982a8d5050d1c83bbea574aea301a4d317840a8c4734a308ffaa6a63bc8cb76085b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000100000000000000000000000068898db1012808808c903f390909c52d9f7067490000000000000000000000004cdc1dbbd754ea539f1ffaea91f1b6c4b8dd14bd");
assert_eq!(
array_bytes::bytes2hex("0x", Sign::eth_signable_message(45, b"Pangoro", &data)),
"0x4bddffe492f1091c1902d1952fc4673b12915f4b22822c6c84eacad574f11f2e"
array_bytes::bytes2hex("0x", Sign::signable_message(46, b"Darwinia", &[0; 32])),
"0xe52c7ebc7e478b623a16cc38469eca4aa1255bed6cd2599e529080d27ecaed32"
);
assert_eq!(
array_bytes::bytes2hex("0x", Sign::signable_message(45, b"Pangoro", &array_bytes::hex2bytes_unchecked("0x30a82982a8d5050d1c83bbea574aea301a4d317840a8c4734a308ffaa6a63bc8cb76085b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000100000000000000000000000068898db1012808808c903f390909c52d9f7067490000000000000000000000004cdc1dbbd754ea539f1ffaea91f1b6c4b8dd14bd"))),
"0x9688cfb794c97094994409ed1c7c7caad076ae53ec13af8a370b55654f7bcb36"
);

let operation = Operation::SwapMembers {
Expand All @@ -134,7 +141,7 @@ fn eth_signable_message() {
ethabi::Token::Uint(0.into()),
]);
assert_eq!(
array_bytes::bytes2hex("0x", Sign::eth_signable_message(45, b"Pangoro", &encoded)),
"0xe328aa10278425238407d49104ac5a55fd68e7f378b327c902d4d5035cfcfedf"
array_bytes::bytes2hex("0x", Sign::signable_message(45, b"Pangoro", &encoded)),
"0x2ca922116daa8c7fec2b58362b58764e83f3b24e906fb50f58e3e1a2e208fb77"
);
}
20 changes: 10 additions & 10 deletions pallet/ecdsa-authority/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn add_authority() {
assert_eq!(EcdsaAuthority::next_authorities(), vec![a_0]);
assert_eq!(EcdsaAuthority::nonce(), 0);
let message = array_bytes::hex_n_into_unchecked(
"0x5c883184c9c53c59857253454df1b4813e8b3fb28648beb85555d58d1e801e14",
"0x5dcc31dcd194f2ccb42e13ed80001e37492f796d6d62514525fcf66de6f955c8",
);
assert_eq!(
EcdsaAuthority::authorities_change_to_sign(),
Expand Down Expand Up @@ -123,7 +123,7 @@ fn remove_authority() {
assert_eq!(EcdsaAuthority::next_authorities(), vec![a_2]);
assert_eq!(EcdsaAuthority::nonce(), 0);
let message = array_bytes::hex_n_into_unchecked(
"0x76139aa9d1c7b35fc744b10444898ee5703e3f77406b926f903006436b7930c7",
"0xb59076c5054bc451c964b47af005b7b807b3501c36ef4d4375cb39637baea13b",
);
assert_eq!(
EcdsaAuthority::authorities_change_to_sign(),
Expand Down Expand Up @@ -182,7 +182,7 @@ fn swap_authority() {
assert_eq!(EcdsaAuthority::next_authorities(), vec![a_2]);
assert_eq!(EcdsaAuthority::nonce(), 0);
let message = array_bytes::hex_n_into_unchecked(
"0x30effc17a3fcf9b3079168c2c2be54b6d9fbdfd7077c9d844ec241dd70dd0507",
"0x0f9863685b4ef59a98fc26a063dad4713698af2d10af5f2ea921fed3f39fac71",
);
assert_eq!(
EcdsaAuthority::authorities_change_to_sign(),
Expand Down Expand Up @@ -231,7 +231,7 @@ fn sync_interval_and_max_pending_period() {
});
run_to_block(<Runtime as Config>::SyncInterval::get());
let message = array_bytes::hex_n_into_unchecked(
"0x742776a31e49b3f5a2a15a6781eb99f96e8116bfc67aae652a08b9b1235146d2",
"0x7eba5c34eb163661830babd9d52b674f80812b4cde832429635352eb6f9225af",
);
assert_eq!(
EcdsaAuthority::new_message_root_to_sign(),
Expand Down Expand Up @@ -268,7 +268,7 @@ fn sync_interval_and_max_pending_period() {
);
run_to_block(offset + <<Runtime as Config>::MaxPendingPeriod as Get<u64>>::get());
let message = array_bytes::hex_n_into_unchecked(
"0xafd9fc3dc135079be23746b4beb27255e5d3b4c5f3d05db766af751c0ed97920",
"0x3e5c445233cc9d281c4fde6ffc5d1c57701d932afba5e6cea07f9b1e88d41fc6",
);
assert_eq!(
EcdsaAuthority::new_message_root_to_sign(),
Expand Down Expand Up @@ -318,7 +318,7 @@ fn submit_authorities_change_signature() {
assert_ok!(EcdsaAuthority::add_authority(RuntimeOrigin::root(), a_3));
let operation = Operation::AddMember { new: a_3 };
let message = array_bytes::hex_n_into_unchecked(
"0x3ad89c7824d6e83c180482c888a0af99baa95ce17a39285d6f943df5d95e7759",
"0x7c2560e894619daa9e7369148a97b05d16e1c439c2467b08f64af578aba9cb4a",
);
assert_eq!(
EcdsaAuthority::authorities_change_to_sign(),
Expand Down Expand Up @@ -373,7 +373,7 @@ fn submit_authorities_change_signature() {
},
Event::CollectingNewMessageRootSignatures {
message: array_bytes::hex_n_into_unchecked(
"0xe7bded73843f446f46b42ee0e0cc435f4f66fbcedf36c635c437a4d63bb44696"
"0x1a8ed5724cc495c64b46b43c079e82e299aaac24f79deae23bbfea88e2e1abdc"
)
}
]
Expand All @@ -399,7 +399,7 @@ fn submit_new_message_root_signature() {

run_to_block(<<Runtime as Config>::SyncInterval as Get<u64>>::get());
let message = array_bytes::hex_n_into_unchecked(
"0x742776a31e49b3f5a2a15a6781eb99f96e8116bfc67aae652a08b9b1235146d2",
"0x7eba5c34eb163661830babd9d52b674f80812b4cde832429635352eb6f9225af",
);
assert_eq!(
EcdsaAuthority::new_message_root_to_sign(),
Expand Down Expand Up @@ -485,7 +485,7 @@ fn tx_fee() {
(2..<Runtime as Config>::SyncInterval::get()).for_each(|n| run_to_block(n as _));
run_to_block(<<Runtime as Config>::SyncInterval as Get<u64>>::get());
let message = array_bytes::hex_n_into_unchecked(
"0x742776a31e49b3f5a2a15a6781eb99f96e8116bfc67aae652a08b9b1235146d2",
"0x7eba5c34eb163661830babd9d52b674f80812b4cde832429635352eb6f9225af",
);

// Free for first-correct signature.
Expand All @@ -508,7 +508,7 @@ fn tx_fee() {

assert_ok!(EcdsaAuthority::remove_authority(RuntimeOrigin::root(), a_1));
let message = array_bytes::hex_n_into_unchecked(
"0x24956af4b0842e1caec63782602c5a94089ba7c8ab8bd12d4243bb1a893b8af0",
"0x9c9af6df8ad32bce1fe3e8e4a1c638843786b2cc7f7932ff4d3f2de7b29b2632",
);

// Free for first-correct signature.
Expand Down

0 comments on commit 3c07f7b

Please sign in to comment.