Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove MultiSignature #127

Merged
merged 13 commits into from
Dec 18, 2020
4 changes: 3 additions & 1 deletion polkadot-js/alphanet-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,7 @@
"CallErrorAsFatal(ExitError)",
"Other(Cow<'static, str>)"
]
}
},
"EthereumSignature": "[u8; 65]",
"ExtrinsicSignature": "EthereumSignature"
}
4 changes: 3 additions & 1 deletion polkadot-js/standalone-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,7 @@
"CallErrorAsFatal(ExitError)",
"Other(Cow<'static, str>)"
]
}
},
"EthereumSignature": "[u8; 65]",
"ExtrinsicSignature": "EthereumSignature"
}
88 changes: 85 additions & 3 deletions runtime/account/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,91 @@
// You should have received a copy of the GNU General Public License
// along with Moonbeam. If not, see <http://www.gnu.org/licenses/>.

//! The Ethereum Signature implementation.
//!
//! It includes the Verify and IdentifyAccount traits for the AccountId20

#![cfg_attr(not(feature = "std"), no_std)]

mod signer;
use sp_core::{H160, H256, ecdsa};
use codec::{Decode, Encode};
use sha3::{Digest, Keccak256};

#[cfg(feature = "std")]
pub use serde::{Serialize, Deserialize, de::DeserializeOwned};

#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[derive(Eq, PartialEq, Clone, Encode, Decode, sp_core::RuntimeDebug)]
pub struct EthereumSignature(ecdsa::Signature);

impl From<ecdsa::Signature> for EthereumSignature {
fn from(x: ecdsa::Signature) -> Self {
EthereumSignature(x)
}
}

impl sp_runtime::traits::Verify for EthereumSignature {
type Signer = EthereumSigner;
fn verify<L: sp_runtime::traits::Lazy<[u8]>>(
&self,
mut msg: L,
signer: &H160
) -> bool {
let mut m = [0u8; 32];
m.copy_from_slice(Keccak256::digest(msg.get()).as_slice());
match sp_io::crypto::secp256k1_ecdsa_recover(self.0.as_ref(), &m) {
Ok(pubkey) => {
// TODO This conversion could use a comment. Why H256 first, then H160?
H160::from(H256::from_slice(Keccak256::digest(&pubkey).as_slice())) ==
*signer
},
Err(sp_io::EcdsaVerifyError::BadRS) => {
log::error!(target: "evm", "Error recovering: Incorrect value of R or S");
false
},
Err(sp_io::EcdsaVerifyError::BadV) => {
log::error!(target: "evm", "Error recovering: Incorrect value of V");
false
},
Err(sp_io::EcdsaVerifyError::BadSignature) => {
log::error!(target: "evm", "Error recovering: Invalid signature");
false
}
}
}
}


/// Public key for an Ethereum / H160 compatible account
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Encode, Decode, sp_core::RuntimeDebug)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub struct EthereumSigner ([u8; 20]);

impl sp_runtime::traits::IdentifyAccount for EthereumSigner {
type AccountId = H160;
fn into_account(self) -> H160 {
self.0.into()
}
}

impl From<[u8; 20]> for EthereumSigner {
fn from(x: [u8; 20]) -> Self {
EthereumSigner(x)
}
}


impl From<ecdsa::Public> for EthereumSigner {
fn from(x: ecdsa::Public) -> Self {
let mut m = [0u8; 20];
m.copy_from_slice(&x.as_ref()[13..33]);
EthereumSigner(m)
}
}

pub use self::signer::EthereumSignature;
pub use self::signer::MultiSignature;
#[cfg(feature = "std")]
impl std::fmt::Display for EthereumSigner {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(fmt, "ethereum signature: {:?}", H160::from_slice(&self.0))
}
}
120 changes: 0 additions & 120 deletions runtime/account/src/signer.rs

This file was deleted.

2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub use sp_runtime::{Perbill, Permill};
pub type BlockNumber = u32;

/// Alias to 512-bit hash when used in the context of a transaction signature on the chain.
pub type Signature = account::MultiSignature;
pub type Signature = account::EthereumSignature;

/// Some way of identifying an account on the chain. We intentionally make it equivalent
/// to the public key of our transaction signing scheme.
Expand Down
6 changes: 3 additions & 3 deletions runtime/src/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ macro_rules! runtime_parachain {
spec_name: create_runtime_str!("moonbase-alphanet"),
impl_name: create_runtime_str!("moonbase-alphanet"),
authoring_version: 3,
spec_version: 5,
impl_version: 1,
spec_version: 6,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
transaction_version: 2,
};

impl cumulus_parachain_upgrade::Config for Runtime {
Expand Down
6 changes: 3 additions & 3 deletions runtime/src/standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ macro_rules! runtime_standalone {
spec_name: create_runtime_str!("moonbeam-standalone"),
impl_name: create_runtime_str!("moonbeam-standalone"),
authoring_version: 3,
spec_version: 5,
impl_version: 1,
spec_version: 6,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
transaction_version: 2,
};

impl pallet_aura::Config for Runtime {
Expand Down