Skip to content

Commit 3032869

Browse files
author
Damien LACHAUME / PALO-IT
committed
Enhance VerifyCertificate component
- Move function that handles calculation of processing time - Move function that retrieves aggregator network from its url - Rename `init` to `initMithrilClient`
1 parent 0916fd7 commit 3032869

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

mithril-explorer/src/components/VerifyCertificate/index.js

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { Modal, Spinner } from "react-bootstrap";
2-
import init, { MithrilClient } from "@mithril-dev/mithril-client-wasm";
2+
import initMithrilClient, { MithrilClient } from "@mithril-dev/mithril-client-wasm";
33
import { useEffect, useState } from "react";
44
import { useSelector } from "react-redux";
55
import LocalDateTime from "../LocalDateTime";
6+
import { formatProcessDuration, computeAggregatorNetworkFromUrl } from "../../utils";
67

78
export default function VerifyCertificate({ show, onClose, certificateHash }) {
89
const currentAggregator = useSelector((state) => state.settings.selectedAggregator);
@@ -11,23 +12,15 @@ export default function VerifyCertificate({ show, onClose, certificateHash }) {
1112
const [verificationDuration, setVerificationDuration] = useState(null);
1213

1314
useEffect(() => {
14-
let startTime;
1515
async function buildClientAndVerifyChain() {
1616
try {
1717
const client = await initializeClient();
1818
if (certificateHash !== null && certificateHash !== undefined) {
19-
startTime = performance.now();
19+
let startTime = performance.now();
2020
const certificate = await client.get_mithril_certificate(certificateHash);
2121
setCertificateData(certificate);
2222
await client.verify_certificate_chain(certificateHash);
23-
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-
);
23+
setVerificationDuration(formatProcessDuration(startTime));
3124
}
3225
} catch (error) {
3326
console.error("Error:", error);
@@ -55,16 +48,14 @@ export default function VerifyCertificate({ show, onClose, certificateHash }) {
5548
}, [show]); // eslint-disable-line react-hooks/exhaustive-deps
5649

5750
async function initializeClient() {
58-
await init();
51+
await initMithrilClient();
5952
const genesisVerificationKey = await fetchGenesisVerificationKey();
6053
return new MithrilClient(currentAggregator, genesisVerificationKey);
6154
}
6255

6356
async function fetchGenesisVerificationKey() {
6457
try {
65-
let network = currentAggregator.match(/aggregator\.(.*?)\.api/);
66-
network = network && network[1] ? network[1] : null;
67-
58+
const network = computeAggregatorNetworkFromUrl(currentAggregator);
6859
const response = await fetch(
6960
"https://raw.githubusercontent.com/input-output-hk/mithril/main/mithril-infra/configuration/" +
7061
network +
@@ -89,10 +80,10 @@ export default function VerifyCertificate({ show, onClose, certificateHash }) {
8980
displayEventInDOM("The certificate chain validation has started...");
9081
} else if (event.type === "CertificateValidated") {
9182
displayEventInDOM(
92-
"A certificate has been validated, hash: " + event.payload.certificate_hash,
83+
"A certificate has been validated, hash: <strong>" + event.payload.certificate_hash + "<strong>",
9384
);
9485
} else if (event.type === "CertificateChainValidated") {
95-
displayEventInDOM("The certificate chain is valid ✅");
86+
displayEventInDOM("<strong>The certificate chain is valid ✅<strong>");
9687
} else {
9788
displayEventInDOM(event);
9889
}
@@ -115,7 +106,7 @@ export default function VerifyCertificate({ show, onClose, certificateHash }) {
115106
aria-labelledby="contained-modal-title-vcenter"
116107
centered>
117108
<Modal.Header closeButton>
118-
<Modal.Title>Verify certificate and certificate chain</Modal.Title>
109+
<Modal.Title>Verify certificate</Modal.Title>
119110
</Modal.Header>
120111
<Modal.Body>
121112
{certificateData && (

mithril-explorer/src/utils.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,20 @@ function getCExplorerUrlForPool(network, partyId) {
7878
return url;
7979
}
8080

81+
function formatProcessDuration(startTime) {
82+
const duration = performance.now() - startTime;
83+
const minutes = Math.floor(duration / 60000);
84+
const seconds = Math.floor((duration % 60000) / 1000);
85+
return(
86+
minutes > 0 ? `${minutes} minutes and ${seconds} seconds` : `${seconds} seconds`
87+
);
88+
}
89+
90+
function computeAggregatorNetworkFromUrl(aggregatorUrl) {
91+
const network = aggregatorUrl.match(/aggregator\.(.*?)\.api/);
92+
return(network && network[1] ? network[1] : null);
93+
}
94+
8195
module.exports = {
8296
checkUrl,
8397
formatStake,
@@ -86,4 +100,6 @@ module.exports = {
86100
formatBytes,
87101
formatPartyId,
88102
getCExplorerUrlForPool,
103+
formatProcessDuration,
104+
computeAggregatorNetworkFromUrl,
89105
};

0 commit comments

Comments
 (0)