-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Phala-Network/wasm-support
Add wasm support for both web and node environment
- Loading branch information
Showing
12 changed files
with
200 additions
and
9 deletions.
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
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(); | ||
|