Skip to content

Commit

Permalink
feat(CAHN Demo): ♻️ WASM initialisation in another file
Browse files Browse the repository at this point in the history
  • Loading branch information
A9-dev committed Sep 5, 2024
1 parent 387cf34 commit b56d483
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import AppBar from "../../components/AppBar";
import { v4 as uuidv4 } from "uuid";
import { device_type_trust } from "@/schemas";
import TrustSubmissions from "@/app/components/TrustSubmissions";
import initializeWasm from "@/app/utils/initialiseWasm";

const Page = ({ params }) => {
const [deviceTypeData, setDeviceTypeData] = useState({
Expand Down Expand Up @@ -188,23 +189,7 @@ const Page = ({ params }) => {
}, [params.device_id, deviceTypeData.DeviceTypeId]);

useEffect(() => {
async function initializeWasm() {
const {
default: init,
gen_keys,
VerifiableCredential,
} = await import("../../wasm/vc_signing");
await init();
console.log("WASM Module initialized");
// Store functions for later use

window.gen_keys = gen_keys;
window.VerifiableCredential = VerifiableCredential;
}
// If functions aren't already stored on the window object, initialize them
// TODO: Extract initializeWasm to a separate file

if (!window.gen_keys || !window.VerifiableCredential) initializeWasm();
initializeWasm();

axios.get("http://localhost:3001/permissions/device_type").then((res) => {
setPermissionedUsers(res.data);
Expand Down
19 changes: 2 additions & 17 deletions packages/cahn_demo/client/src/app/device/[device_id]/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { v4 as uuidv4 } from "uuid";
import AppBar from "../../components/AppBar";
import { device_trust } from "@/schemas";
import TrustSubmissions from "../../components/TrustSubmissions";
import initializeWasm from "@/app/utils/initialiseWasm";

const Page = ({ params }) => {
const [deviceData, setDeviceData] = useState({
Expand Down Expand Up @@ -67,23 +68,7 @@ const Page = ({ params }) => {
}, [params.device_id, deviceData.DeviceId]);

useEffect(() => {
async function initializeWasm() {
const {
default: init,
gen_keys,
VerifiableCredential,
} = await import("../../wasm/vc_signing");
await init();
console.log("WASM Module initialized");
// Store functions for later use

window.gen_keys = gen_keys;
window.VerifiableCredential = VerifiableCredential;
}
// If functions aren't already stored on the window object, initialize them
// TODO: Extract initializeWasm to a separate file

if (!window.gen_keys || !window.VerifiableCredential) initializeWasm();
initializeWasm();

axios.get("http://localhost:3001/permissions/device/").then((res) => {
setPermissionedUsers(res.data);
Expand Down
19 changes: 2 additions & 17 deletions packages/cahn_demo/client/src/app/login/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import axios from "axios";
import { useRouter } from "next/navigation";
import CheckIcon from "@mui/icons-material/Check";
import { useEffect, useState } from "react";
import initializeWasm from "../utils/initialiseWasm";

const Page = () => {
const [email, setEmail] = useState("");
Expand All @@ -28,23 +29,7 @@ const Page = () => {
router.push("/");
}

async function initializeWasm() {
const {
default: init,
gen_keys,
VerifiableCredential,
} = await import("../wasm/vc_signing");
await init();
console.log("WASM Module initialized");

// Store functions for later use
window.gen_keys = gen_keys;
window.VerifiableCredential = VerifiableCredential;
}

// If functions aren't already stored on the window object, initialize them
// TODO: Extract initializeWasm to a separate file
if (!window.genkeys || !window.sign || !window.verify) initializeWasm();
initializeWasm();
}, []);

const handleLogin = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,7 @@ const Page = ({ params }) => {
}, [params.manufacturer_id, manufacturerData.ManufacturerId]);

useEffect(() => {
async function initializeWasm() {
const {
default: init,
gen_keys,
VerifiableCredential,
} = await import("../../wasm/vc_signing");
await init();
console.log("WASM Module initialized");
// Store functions for later use

window.gen_keys = gen_keys;
window.VerifiableCredential = VerifiableCredential;
}
// If functions aren't already stored on the window object, initialize them
// TODO: Extract initializeWasm to a separate file

if (!window.gen_keys || !window.VerifiableCredential) initializeWasm();
initializeWasm();

axios.get("http://localhost:3001/permissions/manufacturer").then((res) => {
setPermissionedUsers(res.data);
Expand Down
16 changes: 16 additions & 0 deletions packages/cahn_demo/client/src/app/utils/initialiseWasm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
async function initializeWasm() {
if (window.gen_keys && window.VerifiableCredential) return;
const {
default: init,
gen_keys,
VerifiableCredential,
} = await import("@/app/wasm/vc_signing");
await init();
console.log("WASM Module initialized");
// Store functions for later use

window.gen_keys = gen_keys;
window.VerifiableCredential = VerifiableCredential;
}

export default initializeWasm;
2 changes: 1 addition & 1 deletion packages/cahn_demo/server/output/output_db.pl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
created(1725544338.5857766).
created(1725544741.9757743).
assert(device_type(1723716151033,"TrustPhone-id","TrustPhone")).
assert(device_type(1723716151033,"VulnerableCamera-id","VulnerableCamera")).
assert(device_type(1723716151033,"EvilPhone-id","EvilPhone")).
Expand Down

0 comments on commit b56d483

Please sign in to comment.