Skip to content

Commit 453ba89

Browse files
author
Damien LACHAUME / PALO-IT
committed
Add get_cardano_transaction_proofs & compute_cardano_transaction_proof_message in client_wasm
1 parent 24187ce commit 453ba89

File tree

2 files changed

+82
-7
lines changed

2 files changed

+82
-7
lines changed

mithril-client-wasm/src/client_wasm.rs

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use async_trait::async_trait;
2-
use mithril_client::{
3-
feedback::{FeedbackReceiver, MithrilEvent},
4-
Client, ClientBuilder, MessageBuilder, MithrilCertificate,
5-
};
62
use serde::Serialize;
73
use std::sync::Arc;
84
use wasm_bindgen::prelude::*;
95

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;
1112

1213
#[wasm_bindgen]
1314
struct JSBroadcastChannelFeedbackReceiver {
@@ -55,7 +56,7 @@ impl From<MithrilEvent> for MithrilEventWasm {
5556
pub struct MithrilClient {
5657
client: Client,
5758

58-
/// Unstable fonctions
59+
/// Unstable functions
5960
pub unstable: MithrilUnstableClient,
6061
}
6162

@@ -247,17 +248,62 @@ impl MithrilUnstableClient {
247248

248249
Ok(serde_wasm_bindgen::to_value(&result)?)
249250
}
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+
}
250295
}
251296

252297
#[cfg(test)]
253298
mod tests {
254299
use super::*;
255300
use crate::test_data;
301+
use wasm_bindgen_test::*;
302+
256303
use mithril_client::{
257304
common::ProtocolMessage, CardanoTransactionCommitment, MithrilCertificateListItem,
258305
MithrilStakeDistribution, MithrilStakeDistributionListItem, Snapshot, SnapshotListItem,
259306
};
260-
use wasm_bindgen_test::*;
261307

262308
const GENESIS_VERIFICATION_KEY: &str = "5b33322c3235332c3138362c3230312c3137372c31312c3131372c3133352c3138372c3136372c3138312c3138382c32322c35392c3230362c3130352c3233312c3135302c3231352c33302c37382c3231322c37362c31362c3235322c3138302c37322c3133342c3133372c3234372c3136312c36385d";
263309
const FAKE_AGGREGATOR_IP: &str = "127.0.0.1";
@@ -487,4 +533,31 @@ mod tests {
487533
.await
488534
.expect_err("get_cardano_transaction_set should fail");
489535
}
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+
}
490563
}

mithril-client-wasm/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ pub use client_wasm::MithrilClient;
66

77
#[cfg(test)]
88
mod test_data;
9+
10+
pub(crate) type WasmResult = Result<wasm_bindgen::JsValue, wasm_bindgen::JsValue>;

0 commit comments

Comments
 (0)