|
1 | 1 | use async_trait::async_trait;
|
2 |
| -use mithril_client::{ |
3 |
| - feedback::{FeedbackReceiver, MithrilEvent}, |
4 |
| - Client, ClientBuilder, MessageBuilder, MithrilCertificate, |
5 |
| -}; |
6 | 2 | use serde::Serialize;
|
7 | 3 | use std::sync::Arc;
|
8 | 4 | use wasm_bindgen::prelude::*;
|
9 | 5 |
|
10 |
| -type WasmResult = Result<JsValue, JsValue>; |
| 6 | +use mithril_client::{ |
| 7 | + feedback::{FeedbackReceiver, MithrilEvent}, |
| 8 | + CardanoTransactionsProofs, Client, ClientBuilder, MessageBuilder, MithrilCertificate, |
| 9 | +}; |
| 10 | + |
| 11 | +use crate::WasmResult; |
11 | 12 |
|
12 | 13 | #[wasm_bindgen]
|
13 | 14 | struct JSBroadcastChannelFeedbackReceiver {
|
@@ -55,7 +56,7 @@ impl From<MithrilEvent> for MithrilEventWasm {
|
55 | 56 | pub struct MithrilClient {
|
56 | 57 | client: Client,
|
57 | 58 |
|
58 |
| - /// Unstable fonctions |
| 59 | + /// Unstable functions |
59 | 60 | pub unstable: MithrilUnstableClient,
|
60 | 61 | }
|
61 | 62 |
|
@@ -247,17 +248,62 @@ impl MithrilUnstableClient {
|
247 | 248 |
|
248 | 249 | Ok(serde_wasm_bindgen::to_value(&result)?)
|
249 | 250 | }
|
| 251 | + |
| 252 | + /// Call the client to get a Cardano transactions proofs |
| 253 | + #[wasm_bindgen] |
| 254 | + pub async fn get_cardano_transaction_proofs(&self, ctx_hashes: Box<[JsValue]>) -> WasmResult { |
| 255 | + let hashes = ctx_hashes |
| 256 | + .iter() |
| 257 | + .map(|h| { |
| 258 | + h.as_string().ok_or(JsValue::from_str(&format!( |
| 259 | + "All transaction hashes must be strings: '{h:?}'" |
| 260 | + ))) |
| 261 | + }) |
| 262 | + .collect::<Result<Vec<String>, JsValue>>() |
| 263 | + .map_err(|err| format!("{err:?}"))?; |
| 264 | + |
| 265 | + let result = self |
| 266 | + .client |
| 267 | + .cardano_transaction_proof() |
| 268 | + .get_proofs(&hashes) |
| 269 | + .await |
| 270 | + .map_err(|err| format!("{err:?}"))?; |
| 271 | + |
| 272 | + Ok(serde_wasm_bindgen::to_value(&result)?) |
| 273 | + } |
| 274 | + |
| 275 | + /// Call the client to compute a cardano transaction proof message |
| 276 | + #[wasm_bindgen] |
| 277 | + pub async fn compute_cardano_transaction_proof_message( |
| 278 | + &self, |
| 279 | + cardano_transaction_proof: JsValue, |
| 280 | + certificate: JsValue, |
| 281 | + ) -> WasmResult { |
| 282 | + let certificate: MithrilCertificate = |
| 283 | + serde_wasm_bindgen::from_value(certificate).map_err(|err| format!("{err:?}"))?; |
| 284 | + let cardano_transaction_proof: CardanoTransactionsProofs = |
| 285 | + serde_wasm_bindgen::from_value(cardano_transaction_proof) |
| 286 | + .map_err(|err| format!("{err:?}"))?; |
| 287 | + let verified_proof = cardano_transaction_proof |
| 288 | + .verify() |
| 289 | + .map_err(|err| format!("{err:?}"))?; |
| 290 | + let result = MessageBuilder::new() |
| 291 | + .compute_cardano_transactions_proofs_message(&certificate, &verified_proof); |
| 292 | + |
| 293 | + Ok(serde_wasm_bindgen::to_value(&result)?) |
| 294 | + } |
250 | 295 | }
|
251 | 296 |
|
252 | 297 | #[cfg(test)]
|
253 | 298 | mod tests {
|
254 | 299 | use super::*;
|
255 | 300 | use crate::test_data;
|
| 301 | + use wasm_bindgen_test::*; |
| 302 | + |
256 | 303 | use mithril_client::{
|
257 | 304 | common::ProtocolMessage, CardanoTransactionCommitment, MithrilCertificateListItem,
|
258 | 305 | MithrilStakeDistribution, MithrilStakeDistributionListItem, Snapshot, SnapshotListItem,
|
259 | 306 | };
|
260 |
| - use wasm_bindgen_test::*; |
261 | 307 |
|
262 | 308 | const GENESIS_VERIFICATION_KEY: &str = "5b33322c3235332c3138362c3230312c3137372c31312c3131372c3133352c3138372c3136372c3138312c3138382c32322c35392c3230362c3130352c3233312c3135302c3231352c33302c37382c3231322c37362c31362c3235322c3138302c37322c3133342c3133372c3234372c3136312c36385d";
|
263 | 309 | const FAKE_AGGREGATOR_IP: &str = "127.0.0.1";
|
@@ -487,4 +533,31 @@ mod tests {
|
487 | 533 | .await
|
488 | 534 | .expect_err("get_cardano_transaction_set should fail");
|
489 | 535 | }
|
| 536 | + |
| 537 | + #[wasm_bindgen_test] |
| 538 | + async fn get_cardano_transaction_proofs_should_return_value_convertible_in_rust_type() { |
| 539 | + let tx_hash = test_data::proof_transaction_hashes()[0]; |
| 540 | + let ctx_hashes = Box::new([JsValue::from(tx_hash)]); |
| 541 | + let client = get_mithril_client(); |
| 542 | + |
| 543 | + let tx_proof = client |
| 544 | + .unstable |
| 545 | + .get_cardano_transaction_proofs(ctx_hashes) |
| 546 | + .await |
| 547 | + .expect("get_verified_cardano_transaction_proofs should not fail"); |
| 548 | + let cardano_tx_proof = |
| 549 | + serde_wasm_bindgen::from_value::<CardanoTransactionsProofs>(tx_proof.clone()) |
| 550 | + .expect("conversion should not fail"); |
| 551 | + |
| 552 | + let certificate = client |
| 553 | + .get_mithril_certificate(&cardano_tx_proof.certificate_hash) |
| 554 | + .await |
| 555 | + .unwrap(); |
| 556 | + |
| 557 | + client |
| 558 | + .unstable |
| 559 | + .compute_cardano_transaction_proof_message(tx_proof, certificate) |
| 560 | + .await |
| 561 | + .expect("Compute tx proof message for matching cert failed"); |
| 562 | + } |
490 | 563 | }
|
0 commit comments