-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add wasm support for both web and node environment #1
Merged
Merged
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ef91b59
build web and node target
tolak 30fa161
fix Makefile target name
tolak 6b15004
add wasm_bindgen trait to js api interface
0xshawn 6c62c0d
add serde-wasm-bindgen
0xshawn 1440fab
add js_verify
tolak 5f2f3ec
compile ring to wasm js to avoid env being imported in wasm
tolak 4712c43
add serde_bytes to wasm exported struct
0xshawn fc0ab40
fix quote collateral decoding
tolak 9f87a7f
test js_verify api in node env
tolak 24ecb26
test js_verify api in web env
tolak bc3903d
rename js filename in readme
0xshawn c339752
Merge remote-tracking branch 'origin/master' into wasm-support
0xshawn d3f0e9d
make wasm related denpendencies optional and add them to js feature
0xshawn b6f7c5e
fix cargo check errors
0xshawn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
WASM_PACK = wasm-pack | ||
INSTALL_TOOL = cargo install wasm-pack | ||
BUILD_WEB = $(WASM_PACK) build --release --target web --out-dir pkg/web --out-name dcap-qvl-web -- --features=js | ||
BUILD_NODE = $(WASM_PACK) build --release --target nodejs --out-dir pkg/node --out-name dcap-qvl-node -- --features=js | ||
|
||
all: install_wasm_tool build_web_pkg build_node_pkg | ||
|
||
install_wasm_tool: | ||
@echo "Installing wasm-pack if not already installed..." | ||
@if ! command -v $(WASM_PACK) &> /dev/null; then \ | ||
echo "wasm-pack not found, installing..."; \ | ||
$(INSTALL_TOOL); \ | ||
else \ | ||
echo "wasm-pack is already installed."; \ | ||
fi | ||
|
||
build_web_pkg: install_wasm_tool | ||
@echo "Building for web browsers..." | ||
$(BUILD_WEB) | ||
|
||
build_node_pkg: install_wasm_tool | ||
@echo "Building for Node.js..." | ||
$(BUILD_NODE) | ||
|
||
clean: | ||
@echo "Cleaning up..." | ||
rm -rf pkg | ||
|
||
.PHONY: all install_wasm_tool build_web_pkg build_node_pkg clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -11,14 +11,38 @@ use crate::{ | |||||||
utils::{self, encode_as_der, extract_certs, verify_certificate_chain}, | ||||||||
}; | ||||||||
use crate::{Error, QuoteCollateralV3}; | ||||||||
use serde::{Deserialize, Serialize}; | ||||||||
use wasm_bindgen::prelude::*; | ||||||||
|
||||||||
#[derive(Debug, Clone)] | ||||||||
#[derive(Debug, Clone, Deserialize, Serialize)] | ||||||||
pub struct VerifiedReport { | ||||||||
pub status: String, | ||||||||
pub advisory_ids: Vec<String>, | ||||||||
pub report: Report, | ||||||||
} | ||||||||
|
||||||||
#[wasm_bindgen] | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
pub fn js_verify( | ||||||||
raw_quote: JsValue, | ||||||||
quote_collateral: JsValue, | ||||||||
now: u64, | ||||||||
) -> Result<JsValue, JsValue> { | ||||||||
let raw_quote: Vec<u8> = serde_wasm_bindgen::from_value(raw_quote) | ||||||||
.map_err(|_| JsValue::from_str("Failed to decode raw_quote"))?; | ||||||||
let quote_collateral_bytes: Vec<u8> = serde_wasm_bindgen::from_value(quote_collateral) | ||||||||
.map_err(|_| JsValue::from_str("Failed to decode quote_collateral"))?; | ||||||||
let quote_collateral = QuoteCollateralV3::decode(&mut quote_collateral_bytes.as_slice()) | ||||||||
.map_err(|_| JsValue::from_str("Failed to decode quote_collateral_bytes"))?; | ||||||||
|
||||||||
let verified_report = verify(&raw_quote, "e_collateral, now).map_err(|e| { | ||||||||
serde_wasm_bindgen::to_value(&e) | ||||||||
.unwrap_or_else(|_| JsValue::from_str("Failed to encode Error")) | ||||||||
})?; | ||||||||
|
||||||||
serde_wasm_bindgen::to_value(&verified_report) | ||||||||
.map_err(|_| JsValue::from_str("Failed to encode verified_report")) | ||||||||
} | ||||||||
|
||||||||
/// Verify a quote | ||||||||
/// | ||||||||
/// # Arguments | ||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pkg | ||
sample |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Test the JS bindings | ||
|
||
## Verify Quote with Node | ||
|
||
``` | ||
cd tests/js | ||
node verify_quote_node.js | ||
``` | ||
|
||
## Verify Quote with Web | ||
|
||
``` | ||
cd tests/js | ||
ln -sf ../../pkg pkg | ||
ln -sf ../../sample sample | ||
python3 -m http.server 8000 | ||
``` | ||
|
||
Open http://localhost:8000/index.html in browser, and check the console for the result. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Verify Quote</title> | ||
</head> | ||
<body> | ||
<h1>Verify Quote</h1> | ||
<script type="module" src="./verify_quote_web.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const { js_verify } = require('../../pkg/node/dcap-qvl-node'); | ||
|
||
// Function to read a file as a Uint8Array | ||
function readFileAsUint8Array(filePath) { | ||
const data = fs.readFileSync(filePath); | ||
return new Uint8Array(data); | ||
} | ||
|
||
// Paths to your sample files | ||
const rawQuotePath = path.join(__dirname, '../../sample', 'tdx_quote'); | ||
const quoteCollateralPath = path.join(__dirname, '../../sample', 'tdx_quote_collateral'); | ||
|
||
// Read the files | ||
const rawQuote = readFileAsUint8Array(rawQuotePath); | ||
const quoteCollateral = readFileAsUint8Array(quoteCollateralPath); | ||
|
||
// Current timestamp | ||
// TCBInfoExpired when using current timestamp, pick the time from verify_quote.rs | ||
// const now = BigInt(Math.floor(Date.now() / 1000)); | ||
const now = BigInt(1725258675); | ||
|
||
try { | ||
// Call the js_verify function | ||
const result = js_verify(rawQuote, quoteCollateral, now); | ||
console.log('Verification Result:', result); | ||
} catch (error) { | ||
console.error('Verification failed:', error); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import init, { js_verify } from '/pkg/web/dcap-qvl-web.js'; | ||
|
||
// Function to fetch a file as a Uint8Array | ||
async function fetchFileAsUint8Array(url) { | ||
const response = await fetch(url); | ||
const data = await response.arrayBuffer(); | ||
return new Uint8Array(data); | ||
} | ||
|
||
// URLs to your sample files | ||
const rawQuoteUrl = '/sample/tdx_quote'; | ||
const quoteCollateralUrl = '/sample/tdx_quote_collateral'; | ||
|
||
// Load the files | ||
async function loadFilesAndVerify() { | ||
try { | ||
// Initialize the WASM module | ||
await init('/pkg/web/dcap-qvl-web_bg.wasm'); | ||
|
||
const rawQuote = await fetchFileAsUint8Array(rawQuoteUrl); | ||
const quoteCollateral = await fetchFileAsUint8Array(quoteCollateralUrl); | ||
|
||
// Current timestamp | ||
const now = BigInt(1725258675); | ||
|
||
// Call the js_verify function | ||
const result = js_verify(rawQuote, quoteCollateral, now); | ||
console.log('Verification Result:', result); | ||
} catch (error) { | ||
console.error('Verification failed:', error); | ||
} | ||
} | ||
|
||
// Execute the verification | ||
loadFilesAndVerify(); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make them optional and enable them by the feature
js
.