Skip to content

Commit 1288f4a

Browse files
Alenardlachaume
andcommitted
refactor: reorganize wasm example & ci test
- Move nodejs & web browser examples to the `/examples` directory - Standardize them: add a README and a MAKEFILE when missing - Rename `mithril-client-wasm/www-test` to `ci-test` - adapt ci - adapt upgrade repository dependencies devbook Co-authored-by: Damien Lachaume <[email protected]>
1 parent 56af5fc commit 1288f4a

File tree

30 files changed

+310
-200
lines changed

30 files changed

+310
-200
lines changed

.github/workflows/test-client.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,19 +300,19 @@ jobs:
300300

301301
- name: Install dependencies
302302
working-directory: mithril-client-wasm
303-
run: make www-test-install
303+
run: make ci-test-install
304304

305305
- name: Create .env file
306306
working-directory: mithril-client-wasm
307307
run: |
308-
echo "AGGREGATOR_ENDPOINT=${{ inputs.aggregator_endpoint }}" > ./www-test/.env
309-
echo "GENESIS_VERIFICATION_KEY=$(curl -s ${{ inputs.genesis_verification_key }})" >> ./www-test/.env
310-
echo "TRANSACTIONS_HASHES_TO_CERTIFY=${{ inputs.transactions_hashes_to_certify }}" >> ./www-test/.env
308+
echo "AGGREGATOR_ENDPOINT=${{ inputs.aggregator_endpoint }}" > ./ci-test/.env
309+
echo "GENESIS_VERIFICATION_KEY=$(curl -s ${{ inputs.genesis_verification_key }})" >> ./ci-test/.env
310+
echo "TRANSACTIONS_HASHES_TO_CERTIFY=${{ inputs.transactions_hashes_to_certify }}" >> ./ci-test/.env
311311
312312
- name: Start the server
313313
working-directory: mithril-client-wasm
314314
shell: bash
315-
run: make www-test-serve &
315+
run: make ci-test-serve &
316316

317317
- name: Wait for the server to be ready
318318
shell: bash

docs/devbook/upgrade-repository-dependencies/README.md

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,32 +84,61 @@ chore: upgrade doc dependencies
8484
By running 'make upgrade' command.
8585
```
8686

87-
### Upgrade `www/` and `www-test/` dependencies
87+
### Upgrade client wasm `ci-test/` dependencies
8888

8989
From the root of the repository, run:
9090

91-
[!IMPORTANT]: This command must be run before upgrading `mithril-explorer`.
91+
Upgrade the `ci-test` dependencies by running:
9292

9393
```bash
94-
cd mithril-client-wasm
95-
make upgrade-www-deps
94+
make -C mithril-client-wasm upgrade-ci-test-deps
95+
```
96+
97+
Create a dedicated commit, e.g.:
98+
99+
```bash
100+
chore: upgrade mithril client wasm 'ci-test' dependencies
101+
102+
By running 'make upgrade-ci-test-deps' command.
103+
```
104+
105+
### Upgrade client wasm examples dependencies
106+
107+
From the root of the repository, run:
108+
109+
[!IMPORTANT]: This command must be run before upgrading client wasm examples.
110+
111+
```bash
112+
make -C mithril-client-wasm build
113+
```
114+
115+
Upgrade the examples dependencies by running:
116+
117+
```bash
118+
make -C examples/client-wasm-nodejs upgrade
119+
make -C examples/client-wasm-web upgrade
96120
```
97121

98122
Create a dedicated commit, e.g.:
99123

100124
```bash
101-
chore: upgrade mithril client wasm 'www' and 'www-test' dependencies
125+
chore: upgrade mithril client wasm examples dependencies
102126

103-
By running 'make upgrade-www-deps' command.
127+
By running 'make upgrade' command in 'examples/client-wasm-nodejs' and 'examples/client-wasm-web'.
104128
```
105129

106130
### Upgrade the explorer dependencies
107131

132+
[!IMPORTANT]: This command must be run before upgrading `mithril-explorer`.
133+
134+
```bash
135+
make -C mithril-client-wasm build
136+
```
137+
108138
From the root of the repository, run:
109139

110140
```bash
111-
cd mithril-explorer
112-
make upgrade
141+
make -C mithril-explorer upgrade
113142
```
114143

115144
Create a dedicated commit, e.g.:

docs/devbook/upgrade-repository-dependencies/upgrade_dependencies.sh

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,26 @@ cargo set-version --bump patch
2323
git commit -am "chore: bump crates versions"
2424

2525
# Build mithril-client wasm (to have latest version used in the explorer)
26-
pushd mithril-client-wasm || exit
27-
make build
28-
popd || exit
26+
make -C mithril-client-wasm build
2927

3028
# Upgrade the documentation website dependencies
31-
pushd docs/website || exit
32-
make upgrade
33-
popd || exit
29+
make -C docs/website upgrade
3430
git commit -am "chore: upgrade doc dependencies
3531
3632
By running 'make upgrade' command."
3733

38-
# Upgrade www/ and www-test/ dependencies
39-
pushd mithril-client-wasm || exit
40-
make upgrade-www-deps
41-
popd || exit
34+
# Upgrade ci-test/ dependencies
35+
make -C mithril-client-wasm upgrade-ci-test-deps
36+
git commit -am "chore: upgrade mithril client wasm 'ci-test' dependencies
37+
38+
By running 'make upgrade-ci-test-deps' command."
4239

43-
git commit -am "chore: upgrade mithril client wasm 'www' and 'www-test' dependencies
40+
# Upgrade client wasm examples dependencies
41+
make -C examples/client-wasm-nodejs upgrade
42+
make -C examples/client-wasm-web upgrade
43+
git commit -am "chore: upgrade mithril client wasm examples dependencies
4444
45-
By running 'make upgrade-www-deps' command."
45+
By running 'make upgrade' command in 'examples/client-wasm-nodejs' and 'examples/client-wasm-web'."
4646

4747
# Upgrade the explorer dependencies
4848
pushd mithril-explorer || exit
@@ -55,25 +55,19 @@ By running 'make upgrade' command."
5555
# Bump Javascript packages versions
5656

5757
# Search all package.json files and bump the version
58-
# and exclude `package.json` in `node_modules` folder
59-
for package_json_file in $(find . -name package.json | grep -v "/node_modules/" | grep -v "/pkg/"); do
58+
# and exclude `package.json` in auto-generated folders
59+
for package_json_file in $(find . -name package.json | grep -v "/node_modules/" | grep -v "/pkg/" | grep -v "/dist/"); do
6060
folder="$(dirname $package_json_file)"
6161
pushd "$folder" || exit
6262
npm version patch
6363
popd || exit
6464
done
6565

66-
pushd mithril-client-wasm || exit
67-
make www-install www-test-install
68-
popd || exit
69-
70-
pushd mithril-explorer || exit
71-
make install
72-
popd || exit
73-
74-
pushd docs/website || exit
75-
make install
76-
popd || exit
66+
make -C mithril-client-wasm ci-test-install
67+
make -C examples/client-wasm-nodejs ci-test-install
68+
make -C examples/client-wasm-web ci-test-install
69+
make -C mithril-explorer install
70+
make -C docs/website install
7771

7872
git commit -am "chore: bump javascript packages versions
7973
File renamed without changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
.PHONY: clean install upgrade
2+
3+
package-lock.json:
4+
npm install
5+
6+
install: package-lock.json
7+
8+
clean:
9+
rm -rf build
10+
rm package-lock.json || true
11+
12+
upgrade: clean install
13+
npm install \
14+
prettier@latest \
15+
16+
17+
lint:
18+
npm run pretty:check
19+
20+
format:
21+
npm run pretty:write
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Mithril client wasm library example: Node.js
2+
3+
## Description
4+
5+
This example shows how to implement a Mithril client and use its features in a Node.js environment.
6+
7+
In this example, the client interacts by default with a real aggregator on the network `testing-sanchonet` to:
8+
9+
- Mithril Stake Distribution:
10+
- list the available Mithril Stake Distributions
11+
- verify the latest Mithril Stake Distribution
12+
- verify a certificate chain
13+
- compute a message
14+
- verify that the certificate signs the computed message
15+
- Cardano transactions
16+
- get proof of inclusion for a known set of Cardano transactions
17+
- verify a certificate chain
18+
- compute a message for
19+
- verify that the certificate signs the computed message
20+
21+
## Build and run the example
22+
23+
First you need to compile the Mithril client Wasm library:
24+
25+
```bash
26+
make -C ../../mithril-client-wasm build
27+
```
28+
29+
Then you can run the example:
30+
31+
```bash
32+
npm install
33+
npm run start
34+
```
35+
36+
## Links
37+
38+
- **Developer documentation**: https://mithril.network/doc/manual/develop/nodes/mithril-client-library-wasm
39+
- **NPM**: https://www.npmjs.com/package/@mithril-dev/mithril-client-wasm

mithril-client-wasm/node-test/index.js renamed to examples/client-wasm-nodejs/index.js

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ broadcast_channel.onmessage = (e) => {
1010
let event = e.data;
1111
if (event.type === "CertificateChainValidationStarted") {
1212
console.log(
13-
`The certificate chain validation has started, event_id: ${event.payload.certificate_chain_validation_id}`
13+
`The certificate chain validation has started, event_id: ${event.payload.certificate_chain_validation_id}`,
1414
);
1515
} else if (event.type === "CertificateValidated") {
1616
console.log(
17-
`A certificate has been validated, certificate_hash: ${event.payload.certificate_hash}, event_id: ${event.payload.certificate_chain_validation_id}`
17+
`A certificate has been validated, certificate_hash: ${event.payload.certificate_hash}, event_id: ${event.payload.certificate_chain_validation_id}`,
1818
);
1919
} else if (event.type === "CertificateChainValidated") {
2020
certificate_chain_validated_occurs = true;
2121
console.log(
22-
`The certificate chain is valid, event_id: ${event.payload.certificate_chain_validation_id}`
22+
`The certificate chain is valid, event_id: ${event.payload.certificate_chain_validation_id}`,
2323
);
2424
} else {
2525
console.log(event);
@@ -40,62 +40,69 @@ async function main() {
4040
http_headers: new Map([["Content-Type", "application/json"]]),
4141
// The following option activates the unstable features of the client.
4242
// Unstable features will trigger an error if this option is not set.
43-
unstable: true
43+
unstable: true,
4444
});
4545

4646
console.log(1, "Getting stake distributions list...");
4747
let mithril_stake_distributions_list = await client.list_mithril_stake_distributions();
4848
console.log(
4949
"Result",
50-
"got " + mithril_stake_distributions_list.length + " stake distributions ✔️"
50+
"got " + mithril_stake_distributions_list.length + " stake distributions ✔️",
5151
);
5252
console.log("stake distributions:", mithril_stake_distributions_list);
5353
let last_mithril_stake_distribution = mithril_stake_distributions_list[0];
5454

5555
console.log(
5656
2,
57-
"Getting last stake distribution with hash: " + last_mithril_stake_distribution.hash + "..."
57+
"Getting last stake distribution with hash: " + last_mithril_stake_distribution.hash + "...",
5858
);
5959
let last_stake_distribution = await client.get_mithril_stake_distribution(
60-
last_mithril_stake_distribution.hash
60+
last_mithril_stake_distribution.hash,
6161
);
6262
console.log("Result", "got last stake distribution ✔️");
6363
console.log("last_stake_distribution:", {
6464
epoch: last_stake_distribution.epoch,
65-
signers: last_stake_distribution.signers.map((s) => `${s.party_id} - ${s.stake} lovelace`)
65+
signers: last_stake_distribution.signers.map((s) => `${s.party_id} - ${s.stake} lovelace`),
6666
});
6767

6868
console.log(
6969
3,
7070
"Getting Mithril certificate from certificate hash: " +
71-
last_stake_distribution.certificate_hash +
72-
"..."
71+
last_stake_distribution.certificate_hash +
72+
"...",
7373
);
7474
let certificate = await client.get_mithril_certificate(last_stake_distribution.certificate_hash);
7575
console.log("Result", "got Mithril certificate ✔️");
76-
console.log("certificate:", certificate.hash, "epoch:", certificate.epoch, "signed_entity:", certificate.signed_entity_type);
76+
console.log(
77+
"certificate:",
78+
certificate.hash,
79+
"epoch:",
80+
certificate.epoch,
81+
"signed_entity:",
82+
certificate.signed_entity_type,
83+
);
7784

7885
console.log(4, "Verifying certificate chain...");
7986
let last_certificate_from_chain = await client.verify_certificate_chain(certificate.hash);
8087
await waitUntilCertificateChainValidatedOccursOrTimeout();
8188
console.log("Result", "certificate chain verified ✔️");
8289
console.log(
8390
"verify_certificate_chain OK, last_certificate_from_chain:",
84-
last_certificate_from_chain.hash
91+
last_certificate_from_chain.hash,
8592
);
8693

8794
console.log(5, "Computing the Mithril stake distribution message...");
8895
let mithril_stake_distributions_message = await client.compute_mithril_stake_distribution_message(
8996
last_stake_distribution,
90-
last_certificate_from_chain
97+
last_certificate_from_chain,
9198
);
9299
console.log("Result", "Mithril stake distribution message computed ✔️");
93100
console.log("mithril_stake_distributions_message:", mithril_stake_distributions_message);
94101

95102
console.log(6, "Validating Mithril stake distribution message...");
96103
let valid_stake_distribution_message = await client.verify_message_match_certificate(
97104
mithril_stake_distributions_message,
98-
last_certificate_from_chain
105+
last_certificate_from_chain,
99106
);
100107
console.log("Result", "Mithril stake distribution message validated ✔️");
101108
console.log("valid_stake_distribution_message:", valid_stake_distribution_message);
@@ -104,17 +111,11 @@ async function main() {
104111
const proof = await client.get_cardano_transaction_proofs([
105112
"eac09f970f47ef3ab378db9232914e146773853397e79b904f1a45123a23c21f",
106113
"81fe7a5dab42867ef309b6d7210158bf99331884ac3c3b6c7188a8c9c18d5974",
107-
"320c13f4a3e51f6f4f66fcd9007e02bf658aa4ee9a88a509028d867d3b8a8e9a"
114+
"320c13f4a3e51f6f4f66fcd9007e02bf658aa4ee9a88a509028d867d3b8a8e9a",
108115
]);
109116
console.log("Certificate hash of the returned proof", proof.certificate_hash);
110-
console.log(
111-
"Transactions hashes included in the proof:",
112-
proof.transactions_hashes
113-
);
114-
console.log(
115-
"Transactions hashes not included in the proof:",
116-
proof.non_certified_transactions
117-
);
117+
console.log("Transactions hashes included in the proof:", proof.transactions_hashes);
118+
console.log("Transactions hashes not included in the proof:", proof.non_certified_transactions);
118119

119120
console.log(9, "Verifying certificate chain...");
120121
let proof_certificate = await client.verify_certificate_chain(proof.certificate_hash);
@@ -125,19 +126,18 @@ async function main() {
125126
console.log(10, "Validating Cardano transaction proof message...");
126127
let protocol_message = await client.verify_cardano_transaction_proof_then_compute_message(
127128
proof,
128-
proof_certificate
129+
proof_certificate,
129130
);
130131
console.log("Ensure that the proof is indeed signed in the associated certificate");
131-
if ((await client.verify_message_match_certificate(protocol_message, proof_certificate)) === true) {
132+
if (
133+
(await client.verify_message_match_certificate(protocol_message, proof_certificate)) === true
134+
) {
132135
console.log("Result", "The proof is signed in the associated certificate ✔️");
133136
} else {
134137
console.log("Result", "Proof and certificate don't match ❌");
135138
}
136139
console.log("Transactions hashes certified", proof.transactions_hashes);
137-
console.log(
138-
"Transactions hashes not certified",
139-
proof.non_certified_transactions
140-
);
140+
console.log("Transactions hashes not certified", proof.non_certified_transactions);
141141

142142
process.exit(0);
143143
}

0 commit comments

Comments
 (0)