mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-09 19:01:08 +00:00
Tests for ed25519 verify.
This commit is contained in:
@@ -273,4 +273,19 @@ mod tests {
|
||||
FromHex::from_hex("ecd0e108a98e192af1d2c25055f4e3bed784b5c877204e73219a5203251feaab").unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ed25519_verify_should_work() {
|
||||
let mut ext = TestExternalities::default();
|
||||
let test_code = include_bytes!("../../wasm-runtime/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
|
||||
let key = ed25519::Pair::from_seed(&tiny_keccak::keccak256(b"test"));
|
||||
let sig = key.sign(b"all ok!");
|
||||
let mut calldata = vec![];
|
||||
calldata.extend_from_slice(key.public().as_ref());
|
||||
calldata.extend_from_slice(sig.as_ref());
|
||||
assert_eq!(
|
||||
WasmExecutor.call(&mut ext, &test_code[..], "test_ed25519_verify", &CallData(calldata)).unwrap(),
|
||||
vec![1]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,6 +48,18 @@ impl Signature {
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<[u8; 64]> for Signature {
|
||||
fn as_ref(&self) -> &[u8; 64] {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<[u8]> for Signature {
|
||||
fn as_ref(&self) -> &[u8] {
|
||||
&self.0[..]
|
||||
}
|
||||
}
|
||||
|
||||
impl Public {
|
||||
pub fn from(data: [u8; 32]) -> Self {
|
||||
Public(data)
|
||||
@@ -59,6 +71,18 @@ impl Public {
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<[u8; 32]> for Public {
|
||||
fn as_ref(&self) -> &[u8; 32] {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<[u8]> for Public {
|
||||
fn as_ref(&self) -> &[u8] {
|
||||
&self.0[..]
|
||||
}
|
||||
}
|
||||
|
||||
impl Pair {
|
||||
/// Generate new secure (random) key pair.
|
||||
pub fn new() -> Pair {
|
||||
|
||||
@@ -91,13 +91,10 @@ 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 fn ed25519_verify(sig: &[u8], msg: &[u8], pubkey: &[u8]) -> bool {
|
||||
sig.len() != 64 || pubkey.len() != 32 || unsafe {
|
||||
ext_ed25519_verify(&msg[0], msg.len() as u32, &sig[0], &pubkey[0])
|
||||
} == 0
|
||||
}
|
||||
|
||||
pub trait Printable {
|
||||
|
||||
BIN
Binary file not shown.
Binary file not shown.
@@ -8,12 +8,21 @@ use alloc::vec::Vec;
|
||||
|
||||
#[macro_use]
|
||||
extern crate runtime_support;
|
||||
use runtime_support::{set_storage, storage, print, keccak256};
|
||||
use runtime_support::{set_storage, storage, print, keccak256, ed25519_verify};
|
||||
|
||||
fn test_keccak256(input: Vec<u8>) -> Vec<u8> {
|
||||
keccak256(&input).to_vec()
|
||||
}
|
||||
|
||||
fn test_ed25519_verify(input: Vec<u8>) -> Vec<u8> {
|
||||
let sig = &input[0..64];
|
||||
let pubkey = &input[64..96];
|
||||
let msg = b"all ok!";
|
||||
let mut r = Vec::new();
|
||||
r.push(ed25519_verify(sig, &msg[..], pubkey) as u8);
|
||||
r
|
||||
}
|
||||
|
||||
fn test_data_in(input: Vec<u8>) -> Vec<u8> {
|
||||
print(b"set_storage" as &[u8]);
|
||||
set_storage(b"input", &input);
|
||||
@@ -29,4 +38,4 @@ fn test_data_in(input: Vec<u8>) -> Vec<u8> {
|
||||
}
|
||||
|
||||
|
||||
impl_stubs!(test_data_in, test_keccak256);
|
||||
impl_stubs!(test_data_in, test_keccak256, test_ed25519_verify);
|
||||
|
||||
Reference in New Issue
Block a user