|
| 1 | +import { MithrilClient } from "@mithril-dev/mithril-client-wasm"; |
| 2 | + |
| 3 | +let aggregator_endpoint = "https://aggregator.testing-sanchonet.api.mithril.network/aggregator"; |
| 4 | +let genesis_verification_key = |
| 5 | + "5b3132372c37332c3132342c3136312c362c3133372c3133312c3231332c3230372c3131372c3139382c38352c3137362c3139392c3136322c3234312c36382c3132332c3131392c3134352c31332c3233322c3234332c34392c3232392c322c3234392c3230352c3230352c33392c3233352c34345d"; |
| 6 | +let certificate_chain_validated_occurs = false; |
| 7 | + |
| 8 | +const broadcast_channel = new BroadcastChannel("mithril-client"); |
| 9 | +broadcast_channel.onmessage = (e) => { |
| 10 | + let event = e.data; |
| 11 | + if (event.type === "CertificateChainValidationStarted") { |
| 12 | + console.log( |
| 13 | + `The certificate chain validation has started, event_id: ${event.payload.certificate_chain_validation_id}` |
| 14 | + ); |
| 15 | + } else if (event.type === "CertificateValidated") { |
| 16 | + console.log( |
| 17 | + `A certificate has been validated, certificate_hash: ${event.payload.certificate_hash}, event_id: ${event.payload.certificate_chain_validation_id}` |
| 18 | + ); |
| 19 | + } else if (event.type === "CertificateChainValidated") { |
| 20 | + certificate_chain_validated_occurs = true; |
| 21 | + console.log( |
| 22 | + `The certificate chain is valid, event_id: ${event.payload.certificate_chain_validation_id}` |
| 23 | + ); |
| 24 | + } else { |
| 25 | + console.log(event); |
| 26 | + } |
| 27 | +}; |
| 28 | + |
| 29 | +async function main() { |
| 30 | + async function waitUntilCertificateChainValidatedOccursOrTimeout() { |
| 31 | + for (let i = 0; i < 100 && !certificate_chain_validated_occurs; i++) { |
| 32 | + await new Promise((r) => setTimeout(r, 100)); |
| 33 | + } |
| 34 | + certificate_chain_validated_occurs = false; |
| 35 | + } |
| 36 | + |
| 37 | + let client = new MithrilClient(aggregator_endpoint, genesis_verification_key, { |
| 38 | + // The following header is set as an example. |
| 39 | + // It's used to demonstrate how to add headers. |
| 40 | + http_headers: new Map([["Content-Type", "application/json"]]), |
| 41 | + // The following option activates the unstable features of the client. |
| 42 | + // Unstable features will trigger an error if this option is not set. |
| 43 | + unstable: true |
| 44 | + }); |
| 45 | + |
| 46 | + console.log(1, "Getting stake distributions list..."); |
| 47 | + let mithril_stake_distributions_list = await client.list_mithril_stake_distributions(); |
| 48 | + console.log( |
| 49 | + "Result", |
| 50 | + "got " + mithril_stake_distributions_list.length + " stake distributions ✔️" |
| 51 | + ); |
| 52 | + console.log("stake distributions:", mithril_stake_distributions_list); |
| 53 | + let last_mithril_stake_distribution = mithril_stake_distributions_list[0]; |
| 54 | + |
| 55 | + console.log( |
| 56 | + 2, |
| 57 | + "Getting last stake distribution with hash: " + last_mithril_stake_distribution.hash + "..." |
| 58 | + ); |
| 59 | + let last_stake_distribution = await client.get_mithril_stake_distribution( |
| 60 | + last_mithril_stake_distribution.hash |
| 61 | + ); |
| 62 | + console.log("Result", "got last stake distribution ✔️"); |
| 63 | + console.log("last_stake_distribution:", { |
| 64 | + epoch: last_stake_distribution.epoch, |
| 65 | + signers: last_stake_distribution.signers.map((s) => `${s.party_id} - ${s.stake} lovelace`) |
| 66 | + }); |
| 67 | + |
| 68 | + console.log( |
| 69 | + 3, |
| 70 | + "Getting Mithril certificate from certificate hash: " + |
| 71 | + last_stake_distribution.certificate_hash + |
| 72 | + "..." |
| 73 | + ); |
| 74 | + let certificate = await client.get_mithril_certificate(last_stake_distribution.certificate_hash); |
| 75 | + console.log("Result", "got Mithril certificate ✔️"); |
| 76 | + console.log("certificate:", certificate.hash, "epoch:", certificate.epoch, "signed_entity:", certificate.signed_entity_type); |
| 77 | + |
| 78 | + console.log(4, "Verifying certificate chain..."); |
| 79 | + let last_certificate_from_chain = await client.verify_certificate_chain(certificate.hash); |
| 80 | + await waitUntilCertificateChainValidatedOccursOrTimeout(); |
| 81 | + console.log("Result", "certificate chain verified ✔️"); |
| 82 | + console.log( |
| 83 | + "verify_certificate_chain OK, last_certificate_from_chain:", |
| 84 | + last_certificate_from_chain.hash |
| 85 | + ); |
| 86 | + |
| 87 | + console.log(5, "Computing the Mithril stake distribution message..."); |
| 88 | + let mithril_stake_distributions_message = await client.compute_mithril_stake_distribution_message( |
| 89 | + last_stake_distribution, |
| 90 | + last_certificate_from_chain |
| 91 | + ); |
| 92 | + console.log("Result", "Mithril stake distribution message computed ✔️"); |
| 93 | + console.log("mithril_stake_distributions_message:", mithril_stake_distributions_message); |
| 94 | + |
| 95 | + console.log(6, "Validating Mithril stake distribution message..."); |
| 96 | + let valid_stake_distribution_message = await client.verify_message_match_certificate( |
| 97 | + mithril_stake_distributions_message, |
| 98 | + last_certificate_from_chain |
| 99 | + ); |
| 100 | + console.log("Result", "Mithril stake distribution message validated ✔️"); |
| 101 | + console.log("valid_stake_distribution_message:", valid_stake_distribution_message); |
| 102 | + |
| 103 | + console.log(7, "Getting transaction proof..."); |
| 104 | + const proof = await client.get_cardano_transaction_proofs([ |
| 105 | + "eac09f970f47ef3ab378db9232914e146773853397e79b904f1a45123a23c21f", |
| 106 | + "81fe7a5dab42867ef309b6d7210158bf99331884ac3c3b6c7188a8c9c18d5974", |
| 107 | + "320c13f4a3e51f6f4f66fcd9007e02bf658aa4ee9a88a509028d867d3b8a8e9a" |
| 108 | + ]); |
| 109 | + 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 | + ); |
| 118 | + |
| 119 | + console.log(9, "Verifying certificate chain..."); |
| 120 | + let proof_certificate = await client.verify_certificate_chain(proof.certificate_hash); |
| 121 | + await waitUntilCertificateChainValidatedOccursOrTimeout(); |
| 122 | + console.log("Result", "certificate chain verified ✔️"); |
| 123 | + console.log("verify_certificate_chain OK, last_certificate_from_chain:", proof_certificate?.hash); |
| 124 | + |
| 125 | + console.log(10, "Validating Cardano transaction proof message..."); |
| 126 | + let protocol_message = await client.verify_cardano_transaction_proof_then_compute_message( |
| 127 | + proof, |
| 128 | + proof_certificate |
| 129 | + ); |
| 130 | + 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 | + console.log("Result", "The proof is signed in the associated certificate ✔️"); |
| 133 | + } else { |
| 134 | + console.log("Result", "Proof and certificate don't match ❌"); |
| 135 | + } |
| 136 | + console.log("Transactions hashes certified", proof.transactions_hashes); |
| 137 | + console.log( |
| 138 | + "Transactions hashes not certified", |
| 139 | + proof.non_certified_transactions |
| 140 | + ); |
| 141 | + |
| 142 | + process.exit(0); |
| 143 | +} |
| 144 | + |
| 145 | +await main(); |
0 commit comments