diff --git a/substrate/native-runtime/support/src/lib.rs b/substrate/native-runtime/support/src/lib.rs index a2fe13eb10..7f2de2d103 100644 --- a/substrate/native-runtime/support/src/lib.rs +++ b/substrate/native-runtime/support/src/lib.rs @@ -3,6 +3,9 @@ extern crate environmental; extern crate polkadot_state_machine; extern crate tiny_keccak; +use std::fmt; +use primitives::ed25519; + pub use std::vec::Vec; pub use std::rc::Rc; pub use std::cell::RefCell; @@ -11,7 +14,6 @@ pub use std::slice; pub use std::mem::{size_of, transmute, swap, uninitialized}; pub use polkadot_state_machine::Externalities; -use std::fmt; // TODO: use the real error, not NoError. @@ -63,9 +65,9 @@ pub fn storage_into(_key: &[u8]) -> Option { }).unwrap_or(None) } -pub fn set_storage(_key: &[u8], _value: &[u8]) { +pub fn set_storage(key: &[u8], value: &[u8]) { ext::with(|holder| - holder.ext.set_storage(_key.to_vec(), _value.to_vec()) + holder.ext.set_storage(key.to_vec(), value.to_vec()) ); } @@ -79,6 +81,11 @@ pub fn chain_id() -> u64 { /// Conduct a Keccak-256 hash of the given data. pub use tiny_keccak::keccak256; +/// Verify a ed25519 signature. +pub fn ed25519_verify(sig: &[u8; 64], msg: &[u8], pubkey: &[u8; 32]) -> bool { + ed25519::verify(&sig[..], msg, &pubkey[..]) +} + /// Execute the given closure with global function available whose functionality routes into the /// externalities `ext`. Forwards the value that the closure returns. pub fn with_externalities R>(ext: &mut Externalities, f: F) -> R { diff --git a/substrate/primitives/src/ed25519.rs b/substrate/primitives/src/ed25519.rs index 576d1a409a..58556b6669 100644 --- a/substrate/primitives/src/ed25519.rs +++ b/substrate/primitives/src/ed25519.rs @@ -15,6 +15,17 @@ impl<'a> ::std::fmt::Display for HexDisplay<'a> { } } +pub fn verify(sig: &[u8], message: &[u8], public: &[u8]) -> bool { + let public_key = untrusted::Input::from(public); + let msg = untrusted::Input::from(message); + let sig = untrusted::Input::from(sig); + + match signature::verify(&signature::ED25519, public_key, msg, sig) { + Ok(_) => true, + _ => false, + } +} + /// A public key. #[derive(PartialEq, Clone, Debug)] pub struct Public ([u8; 32]); diff --git a/substrate/wasm-runtime/support/src/lib.rs b/substrate/wasm-runtime/support/src/lib.rs index ae3feb14c9..9f75504513 100644 --- a/substrate/wasm-runtime/support/src/lib.rs +++ b/substrate/wasm-runtime/support/src/lib.rs @@ -30,6 +30,7 @@ extern "C" { fn ext_get_storage_into(key_data: *const u8, key_len: u32, value_data: *mut u8, value_len: u32) -> u32; fn ext_chain_id() -> u64; fn ext_keccak256(data: *const u8, len: u32, out: *mut u8); + fn ext_ed25519_verify(msg_data: *const u8, msg_len: u32, sig_data: *const u8, pubkey_data: *const u8) -> u32; } pub fn storage(key: &[u8]) -> Vec { @@ -89,6 +90,16 @@ pub fn keccak256(data: &[u8]) -> [u8; 32] { } } +/// Verify a ed25519 signature. +pub fn ed25519_verify(sig: &[u8; 64], msg: &[u8], pubkey: &[u8; 32]) -> bool { + unsafe { + match ext_ed25519_verify(&msg[0], msg.len() as u32, &sig[0], &pubkey[0]) { + 0 => false, + _ => true, + } + } +} + pub trait Printable { fn print(self); }