Skip to content
Snippets Groups Projects
Commit ec78b724 authored by Gav's avatar Gav
Browse files

Introduce Ed25519 verify as an external.

parent 6b1153d9
Branches
No related merge requests found
......@@ -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<T: Sized>(_key: &[u8]) -> Option<T> {
}).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, F: FnOnce() -> R>(ext: &mut Externalities<Error=NoError>, f: F) -> R {
......
......@@ -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]);
......
......@@ -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<u8> {
......@@ -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);
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment