feat: add validation rule example
add validation rule rust example
This commit is contained in:
parent
664d5c4bcd
commit
574251e003
|
@ -0,0 +1,16 @@
|
|||
[package]
|
||||
name = "contract_demo"
|
||||
version = "0.1.0"
|
||||
authors = ["xialiwei <xialiwei@hyperchain.cn>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[lib]
|
||||
path = "src/lib.rs"
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
protobuf = "2.10.1"
|
||||
sha2 = "0.8.1"
|
||||
x509-parser = "0.6.0"
|
||||
pem = "0.7.0"
|
|
@ -0,0 +1,35 @@
|
|||
// extern crate protobuf;
|
||||
// extern crate sha2;
|
||||
|
||||
// use crate::crypto::ecdsa;
|
||||
// use crate::model::transaction;
|
||||
// use sha2::{Digest, Sha256};
|
||||
|
||||
pub fn verify(proof: &[u8], validator: &[u8]) -> bool {
|
||||
// let cap =
|
||||
// protobuf::parse_from_bytes::<transaction::ChaincodeActionPayload>(proof).expect("error");
|
||||
// let cap_act = cap.action.unwrap();
|
||||
// let prp = protobuf::parse_from_bytes::<transaction::ProposalResponsePayload>(
|
||||
// &cap_act.proposal_response_payload,
|
||||
// )
|
||||
// .expect("error");
|
||||
// println!("{:?}", prp);
|
||||
|
||||
// let endorsers = cap_act.endorsements;
|
||||
// println!("{:?}", endorsers[0]);
|
||||
|
||||
// let mut digest = Sha256::new();
|
||||
// let mut payload = cap_act.proposal_response_payload.to_owned();
|
||||
// payload.extend(&endorsers[0].endorser);
|
||||
// digest.input(&payload);
|
||||
// let digest_byte = digest.result();
|
||||
// println!("{:?}", digest_byte);
|
||||
|
||||
// return ecdsa::verify(
|
||||
// &endorsers[0].signature,
|
||||
// &digest_byte,
|
||||
// &validator,
|
||||
// ecdsa::EcdsaAlgorithmn::P256,
|
||||
// );
|
||||
return true
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
pub mod contract;
|
|
@ -0,0 +1,34 @@
|
|||
#![allow(unused_assignments)]
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::os::raw::{c_char, c_void};
|
||||
|
||||
extern "C" {
|
||||
fn ecdsa_verify(sig_ptr: i64, digest_ptr: i64, pubkey_ptr: i64, opt: i32) -> i32;
|
||||
}
|
||||
|
||||
pub enum EcdsaAlgorithmn {
|
||||
P256 = 1,
|
||||
Secp256k1 = 2,
|
||||
}
|
||||
|
||||
pub fn verify(signature: &[u8], digest: &[u8], pubkey: &[u8], opt: EcdsaAlgorithmn) -> bool {
|
||||
let mut ecdsa_opt = 0;
|
||||
match opt {
|
||||
EcdsaAlgorithmn::P256 => ecdsa_opt = 1,
|
||||
EcdsaAlgorithmn::Secp256k1 => ecdsa_opt = 2,
|
||||
}
|
||||
let res = unsafe {
|
||||
ecdsa_verify(
|
||||
signature.as_ptr() as i64,
|
||||
digest.as_ptr() as i64,
|
||||
pubkey.as_ptr() as i64,
|
||||
ecdsa_opt,
|
||||
)
|
||||
};
|
||||
if res == 1 {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
// return true;
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
pub mod ecdsa;
|
|
@ -0,0 +1,29 @@
|
|||
use std::ffi::CStr;
|
||||
use std::os::raw::{c_char, c_void};
|
||||
|
||||
pub mod app;
|
||||
pub mod crypto;
|
||||
pub mod memory;
|
||||
pub mod model;
|
||||
|
||||
use app::contract;
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn allocate(size: usize) -> *mut c_void {
|
||||
return memory::allocate(size);
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn deallocate(pointer: *mut c_void, capacity: usize) {
|
||||
return memory::deallocate(pointer, capacity);
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn start_verify(proof_ptr: *mut c_char, validator_ptr: *mut c_char) -> i32 {
|
||||
let proof = unsafe { CStr::from_ptr(proof_ptr).to_bytes() };
|
||||
let validator = unsafe { CStr::from_ptr(validator_ptr).to_bytes() };
|
||||
let res = contract::verify(proof, validator);
|
||||
|
||||
return res as i32;
|
||||
// 1
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
use std::mem;
|
||||
use std::os::raw::c_void;
|
||||
|
||||
pub fn allocate(size: usize) -> *mut c_void {
|
||||
let mut buffer = Vec::with_capacity(size);
|
||||
let pointer = buffer.as_mut_ptr();
|
||||
mem::forget(buffer);
|
||||
|
||||
pointer as *mut c_void
|
||||
}
|
||||
|
||||
pub fn deallocate(pointer: *mut c_void, capacity: usize) {
|
||||
unsafe {
|
||||
let _ = Vec::from_raw_parts(pointer, 0, capacity);
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
pub mod transaction;
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue