Skip to content

Commit 1ad8188

Browse files
author
Damien LACHAUME / PALO-IT
committed
Add --target web option to wasm-pack build and verify_certificate_chain feature
1 parent 9e6e9d6 commit 1ad8188

File tree

7 files changed

+154
-54
lines changed

7 files changed

+154
-54
lines changed

docs/website/root/manual/developer-docs/nodes/mithril-client-library-wasm.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ The Mithril client library is compatible with the following browsers:
5252
| **Firefox for Android** | `38` | 2015-05-12 | - |
5353
| **Opera Android** | `41` | 2016-10-25 | - |
5454
| **Safari on iOS** | `15.4` | 2022-03-14 | - |
55-
| **Node.Js** | `15.4.0` | 2020-12-09 | :heavy_check_mark: |
5655

5756

5857
:::warning

mithril-client-wasm/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ CARGO = cargo
1010
all: test build
1111

1212
build:
13-
wasm-pack build --release --out-name index
13+
wasm-pack build --target web --release --out-name index
1414

1515
test:
1616
wasm-pack test --headless --firefox --chrome --node --release

mithril-client-wasm/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ You will need to run the Mithril client library in a compatible browser:
6969
| **Firefox for Android** | `38` | 2015-05-12 | - |
7070
| **Opera Android** | `41` | 2016-10-25 | - |
7171
| **Safari on iOS** | `15.4` | 2022-03-14 | - |
72-
| **Node.Js** | `15.4.0` | 2020-12-09 | :heavy_check_mark: |
7372

7473

7574
Go to the `mithril-client-wasm` directory:

mithril-client-wasm/npm/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ The Mithril client library is compatible with the following browsers:
7676
| **Firefox for Android** | `38` | 2015-05-12 | - |
7777
| **Opera Android** | `41` | 2016-10-25 | - |
7878
| **Safari on iOS** | `15.4` | 2022-03-14 | - |
79-
| **Node.Js** | `15.4.0` | 2020-12-09 | :heavy_check_mark: |
8079

8180
## Getting Help
8281
First, check our [Developer documentation](https://mithril.network/doc/manual/developer-docs/nodes/mithril-client-wasm-library).

mithril-explorer/next.config.js

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,8 @@ const nextConfig = {
77
images: {
88
unoptimized: true,
99
},
10-
webpack: function (config, { isServer }) {
10+
webpack: (config) => {
1111
config.experiments = { layers: true, asyncWebAssembly: true };
12-
13-
const { join } = require('path');
14-
const { access, symlink } = require('fs/promises');
15-
config.plugins.push(
16-
new (class {
17-
apply(compiler) {
18-
compiler.hooks.afterEmit.tapPromise(
19-
'SymlinkWebpackPlugin',
20-
async (compiler) => {
21-
if (isServer) {
22-
const from = join(compiler.options.output.path, '../static');
23-
const to = join(compiler.options.output.path, 'static');
24-
25-
try {
26-
await access(from);
27-
// console.log(`${from} already exists`);
28-
return;
29-
} catch (error) {
30-
if (error.code === 'ENOENT') {
31-
// No link exists
32-
} else {
33-
throw error;
34-
}
35-
}
36-
37-
await symlink(to, from, 'junction');
38-
console.log(`created symlink ${from} -> ${to}`);
39-
}
40-
},
41-
);
42-
}
43-
})(),
44-
);
45-
4612
return config;
4713
}
4814
};
Lines changed: 148 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,159 @@
1-
import { Modal } from "react-bootstrap";
2-
import { MithrilClient } from "@mithril-dev/mithril-client-wasm";
1+
import { Modal, Spinner } from "react-bootstrap";
2+
import init, { MithrilClient } from "@mithril-dev/mithril-client-wasm";
3+
import { useEffect, useState } from "react";
4+
import { useSelector } from "react-redux";
5+
import LocalDateTime from "../LocalDateTime";
36

47
export default function VerifyCertificate({ show, onClose, certificateHash }) {
5-
let aggregatorEdpoint = "https://aggregator.testing-preview.api.mithril.network/aggregator";
6-
let genesisVerificationKey = "5b3132372c37332c3132342c3136312c362c3133372c3133312c3231332c3230372c3131372c3139382c38352c3137362c3139392c3136322c3234312c36382c3132332c3131392c3134352c31332c3233322c3234332c34392c3232392c322c3234392c3230352c3230352c33392c3233352c34345d";
8+
const currentAggregator = useSelector((state) => state.settings.selectedAggregator);
9+
const [loading, setLoading] = useState(false);
10+
const [certificateData, setCertificateData] = useState(null);
11+
const [verificationDuration, setVerificationDuration] = useState(null);
712

8-
let client = new MithrilClient(aggregatorEdpoint, genesisVerificationKey);
9-
console.log(client);
13+
useEffect(() => {
14+
let startTime;
15+
async function buildClientAndVerifyChain() {
16+
try {
17+
const client = await initializeClient();
18+
if (certificateHash !== null && certificateHash !== undefined) {
19+
startTime = performance.now();
20+
const certificate = await client.get_mithril_certificate(certificateHash);
21+
setCertificateData(certificate);
22+
await client.verify_certificate_chain(certificateHash);
1023

11-
return (
12-
<Modal show={show} onHide={onClose} size="xl" aria-labelledby="contained-modal-title-vcenter" centered>
24+
// Process duration
25+
const duration = performance.now() - startTime;
26+
const minutes = Math.floor(duration / 60000);
27+
const seconds = Math.floor((duration % 60000) / 1000);
28+
setVerificationDuration(
29+
minutes > 0 ? `${minutes} minutes and ${seconds} seconds` : `${seconds} seconds`,
30+
);
31+
}
32+
} catch (error) {
33+
console.error("Error:", error);
34+
} finally {
35+
setLoading(false);
36+
}
37+
}
38+
39+
const broadcast_channel = new BroadcastChannel("mithril-client");
40+
broadcast_channel.onmessage = eventListener;
41+
42+
if (certificateHash && show) {
43+
setLoading(true);
44+
buildClientAndVerifyChain();
45+
} else {
46+
// Reset state when the modal is closed
47+
setCertificateData(null);
48+
setLoading(false);
49+
}
50+
51+
return () => {
52+
// Cleanup: remove the previous event listener
53+
broadcast_channel.onmessage = null;
54+
};
55+
}, [show]); // eslint-disable-line react-hooks/exhaustive-deps
56+
57+
async function initializeClient() {
58+
await init();
59+
const genesisVerificationKey = await fetchGenesisVerificationKey();
60+
return new MithrilClient(currentAggregator, genesisVerificationKey);
61+
}
62+
63+
async function fetchGenesisVerificationKey() {
64+
try {
65+
let network = currentAggregator.match(/aggregator\.(.*?)\.api/);
66+
network = network && network[1] ? network[1] : null;
67+
68+
const response = await fetch(
69+
"https://raw.githubusercontent.com/input-output-hk/mithril/main/mithril-infra/configuration/" +
70+
network +
71+
"/genesis.vkey",
72+
);
73+
74+
if (!response.ok) {
75+
throw new Error(`Failed to fetch the genesis verification key. Status: ${response.status}`);
76+
}
77+
78+
const genesisVerificationKey = await response.text();
79+
return genesisVerificationKey;
80+
} catch (error) {
81+
console.error("Error fetching genesis verification key:", error.message);
82+
throw error;
83+
}
84+
}
85+
86+
function eventListener(e) {
87+
let event = e.data;
88+
if (event.type === "CertificateChainValidationStarted") {
89+
displayEventInDOM("The certificate chain validation has started...");
90+
} else if (event.type === "CertificateValidated") {
91+
displayEventInDOM(
92+
"A certificate has been validated, hash: " + event.payload.certificate_hash,
93+
);
94+
} else if (event.type === "CertificateChainValidated") {
95+
displayEventInDOM("The certificate chain is valid ✅");
96+
} else {
97+
displayEventInDOM(event);
98+
}
99+
}
100+
101+
function displayEventInDOM(message) {
102+
let eventDiv = document.createElement("div");
103+
eventDiv.innerHTML = message;
104+
let mithrilEventsDiv = document.getElementById("mithril-events");
105+
mithrilEventsDiv &&
106+
(mithrilEventsDiv.appendChild(eventDiv),
107+
(mithrilEventsDiv.scrollTop = mithrilEventsDiv.scrollHeight));
108+
}
109+
110+
return (
111+
<Modal
112+
show={show}
113+
onHide={onClose}
114+
size="xl"
115+
aria-labelledby="contained-modal-title-vcenter"
116+
centered>
13117
<Modal.Header closeButton>
14-
<Modal.Title>Verify Mithril certificate</Modal.Title>
118+
<Modal.Title>Verify certificate and certificate chain</Modal.Title>
15119
</Modal.Header>
16120
<Modal.Body>
121+
{certificateData && (
122+
<>
123+
<h4>Certificate Details</h4>
124+
<div>Certificate hash: {certificateData.hash}</div>
125+
<div style={{ display: "flex", justifyContent: "space-between" }}>
126+
<div>
127+
Sealed at: <LocalDateTime datetime={certificateData.metadata.sealed_at} />
128+
</div>
129+
{loading ? (
130+
<div style={{ display: "flex", alignItems: "center" }}>
131+
<div style={{ marginLeft: "10px", paddingRight: "10px" }}>
132+
Verifying the certificate chain...
133+
</div>
134+
<Spinner animation="border" variant="primary" />
135+
</div>
136+
) : (
137+
<div style={{ marginLeft: "10px", paddingRight: "10px" }}>
138+
Verification duration: {verificationDuration}
139+
</div>
140+
)}
141+
</div>
142+
<hr />
143+
<div>
144+
<div
145+
id="mithril-events"
146+
style={{
147+
height: "400px",
148+
maxHeight: "400px",
149+
overflowY: "auto",
150+
marginTop: "10px",
151+
}}></div>
152+
</div>
153+
</>
154+
)}
17155
</Modal.Body>
18-
<Modal.Footer>
19-
</Modal.Footer>
156+
<Modal.Footer></Modal.Footer>
20157
</Modal>
21158
);
22159
}

mithril-explorer/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@
611611
integrity sha512-fuscdXJ9G1qb7W8VdHi+IwRqij3lBkosAm4ydQtEmbY58OzHXqQhvlxqEkoz0yssNVn38bcpRWgA9PP+OGoisw==
612612

613613
"@mithril-dev/mithril-client-wasm@file:../mithril-client-wasm/pkg":
614-
version "0.1.1"
614+
version "0.1.3"
615615

616616
617617
version "14.0.4"
@@ -1743,9 +1743,9 @@ domexception@^4.0.0:
17431743
webidl-conversions "^7.0.0"
17441744

17451745
electron-to-chromium@^1.4.601:
1746-
version "1.4.616"
1747-
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.616.tgz#4bddbc2c76e1e9dbf449ecd5da3d8119826ea4fb"
1748-
integrity sha512-1n7zWYh8eS0L9Uy+GskE0lkBUNK83cXTVJI0pU3mGprFsbfSdAc15VTFbo+A+Bq4pwstmL30AVcEU3Fo463lNg==
1746+
version "1.4.617"
1747+
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.617.tgz#3b0dde6c54e5f0f26db75ce6c6ae751e5df4bf75"
1748+
integrity sha512-sYNE3QxcDS4ANW1k4S/wWYMXjCVcFSOX3Bg8jpuMFaXt/x8JCmp0R1Xe1ZXDX4WXnSRBf+GJ/3eGWicUuQq5cg==
17491749

17501750
emittery@^0.13.1:
17511751
version "0.13.1"

0 commit comments

Comments
 (0)