feat: add examples
This commit is contained in:
1
__enclave/virt_enclave/.gitignore
vendored
Normal file
1
__enclave/virt_enclave/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
platform_signing_key.json
|
||||||
9
__enclave/virt_enclave/examples/create_signing_key.rs
Normal file
9
__enclave/virt_enclave/examples/create_signing_key.rs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
use virt_enclave::sig::SigningKeyPair;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let signing_key_pair = SigningKeyPair::new();
|
||||||
|
match signing_key_pair.write_to_file("platform_signing_key.json") {
|
||||||
|
Err(_) => println!("Write platform signing key failed!"),
|
||||||
|
Ok(_) => println!("Write platform signing key successed!"),
|
||||||
|
}
|
||||||
|
}
|
||||||
34
__enclave/virt_enclave/examples/sign_file.rs
Normal file
34
__enclave/virt_enclave/examples/sign_file.rs
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
use std::io::Read;
|
||||||
|
use std::fs::File;
|
||||||
|
use ring::digest;
|
||||||
|
use virt_enclave::sig::*;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut args = std::env::args();
|
||||||
|
args.next();
|
||||||
|
let signing_key_pair = match SigningKeyPair::read_from_file("platform_signing_key.json") {
|
||||||
|
Err(_) => { println!("Read file failed!"); return; },
|
||||||
|
Ok(k) => k,
|
||||||
|
};
|
||||||
|
let f = match args.next() {
|
||||||
|
None => { println!("File not assigned!"); return; },
|
||||||
|
Some(f) => f,
|
||||||
|
};
|
||||||
|
let mut file = match File::open(&f) {
|
||||||
|
Err(_) => { println!("Open file failed: {}", f); return; }
|
||||||
|
Ok(f) => f,
|
||||||
|
};
|
||||||
|
let mut buf = vec![];
|
||||||
|
let _len = match file.read_to_end(&mut buf) {
|
||||||
|
Err(_) => { println!("Read file failed: {}", f); return; }
|
||||||
|
Ok(c) => c,
|
||||||
|
};
|
||||||
|
let d = digest::digest(&digest::SHA256, &buf);
|
||||||
|
let digest_hex = hex::encode(&d);
|
||||||
|
let mut signed_message = SignedMessage::new(d.as_ref().to_vec(), None);
|
||||||
|
signed_message.sign(&signing_key_pair);
|
||||||
|
|
||||||
|
println!("File : {}", f);
|
||||||
|
println!("Hex : {}", digest_hex);
|
||||||
|
println!("Signed: {}", serde_json::to_string(&signed_message).unwrap());
|
||||||
|
}
|
||||||
3
__enclave/virt_enclave/src/lib.rs
Normal file
3
__enclave/virt_enclave/src/lib.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
pub mod qjs;
|
||||||
|
pub mod sig;
|
||||||
|
pub mod rpc;
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
mod qjs;
|
include!("lib.rs");
|
||||||
mod sig;
|
|
||||||
|
|
||||||
pub use qjs::*;
|
pub use qjs::*;
|
||||||
pub use sig::*; // TODO
|
pub use sig::*; // TODO
|
||||||
|
pub use rpc::*;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let context = QuickJSContext::new().unwrap();
|
let context = QuickJSContext::new().unwrap();
|
||||||
|
|||||||
@@ -4,6 +4,14 @@ use quick_js::console::Level;
|
|||||||
use quick_js::console::ConsoleBackend;
|
use quick_js::console::ConsoleBackend;
|
||||||
use serde::{ Serialize, Deserialize };
|
use serde::{ Serialize, Deserialize };
|
||||||
use rust_util::XResult;
|
use rust_util::XResult;
|
||||||
|
use crate::sig::*;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct QuickJSPack {
|
||||||
|
hash: String, // hash in hex == HEX(SHA256(self.script))
|
||||||
|
script: String,
|
||||||
|
sig: SignedMessage, // sig.msg = SHA256(self.script)
|
||||||
|
}
|
||||||
|
|
||||||
pub struct QuickJSContext {
|
pub struct QuickJSContext {
|
||||||
context: Context,
|
context: Context,
|
||||||
|
|||||||
17
__enclave/virt_enclave/src/rpc.rs
Normal file
17
__enclave/virt_enclave/src/rpc.rs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
use serde::{ Serialize, Deserialize };
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct InvokeRequest {
|
||||||
|
hash: String,
|
||||||
|
method: String,
|
||||||
|
params: String, // JSON, empty: []
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct InvokeResponse {
|
||||||
|
request_id: String,
|
||||||
|
hash: String,
|
||||||
|
method: String,
|
||||||
|
result: String,
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user