diff --git a/substrate/executor/src/wasm_executor.rs b/substrate/executor/src/wasm_executor.rs index 8597656c51..719343f8fa 100644 --- a/substrate/executor/src/wasm_executor.rs +++ b/substrate/executor/src/wasm_executor.rs @@ -27,6 +27,7 @@ use error::{Error, ErrorKind, Result}; use wasm_utils::{MemoryInstance, UserDefinedElements, AddModuleWithoutFullDependentInstance}; use tiny_keccak; +use primitives::ed25519; struct Heap { end: u32, @@ -148,6 +149,24 @@ impl_function_executor!(this: FunctionExecutor<'e, E>, [0; 32] }; let _ = this.memory.set(out, &result); + }, + ext_ed25519_verify(msg_data: *const u8, msg_len: u32, sig_data: *const u8, pubkey_data: *const u8) -> u32 => { + (||{ + let mut sig = [0u8; 64]; + if let Err(_) = this.memory.get_into(sig_data, &mut sig[..]) { + return 0; + }; + let mut pubkey = [0u8; 32]; + if let Err(_) = this.memory.get_into(pubkey_data, &mut pubkey[..]) { + return 0; + }; + + if let Ok(msg) = this.memory.get(msg_data, msg_len as usize) { + if ed25519::Signature::from(sig).verify(&msg, &ed25519::Public::from(pubkey)) { 1 } else { 0 } + } else { + 0 + } + })() } => <'e, E: Externalities + 'e> ); diff --git a/substrate/primitives/src/ed25519.rs b/substrate/primitives/src/ed25519.rs index 676f8ef266..576d1a409a 100644 --- a/substrate/primitives/src/ed25519.rs +++ b/substrate/primitives/src/ed25519.rs @@ -26,6 +26,28 @@ pub struct Pair(signature::Ed25519KeyPair); #[derive(Clone)] pub struct Signature ([u8; 64]); +impl Signature { + pub fn from(data: [u8; 64]) -> Self { + Signature(data) + } + pub fn from_slice(data: &[u8]) -> Self { + let mut r = [0u8; 64]; + r.copy_from_slice(data); + Signature(r) + } +} + +impl Public { + pub fn from(data: [u8; 32]) -> Self { + Public(data) + } + pub fn from_slice(data: &[u8]) -> Self { + let mut r = [0u8; 32]; + r.copy_from_slice(data); + Public(r) + } +} + impl Pair { /// Generate new secure (random) key pair. pub fn new() -> Pair {