@@ -59,7 +59,7 @@ pub struct MithrilClient {
59
59
impl MithrilClient {
60
60
/// Constructor for wasm client
61
61
#[ wasm_bindgen( constructor) ]
62
- pub async fn new ( aggregator_endpoint : & str , genesis_verification_key : & str ) -> MithrilClient {
62
+ pub fn new ( aggregator_endpoint : & str , genesis_verification_key : & str ) -> MithrilClient {
63
63
let feedback_receiver = Arc :: new ( JSBroadcastChannelFeedbackReceiver :: new ( "mithril-client" ) ) ;
64
64
let client = ClientBuilder :: aggregator ( aggregator_endpoint, genesis_verification_key)
65
65
. add_feedback_receiver ( feedback_receiver)
@@ -76,12 +76,16 @@ impl MithrilClient {
76
76
. snapshot ( )
77
77
. get ( digest)
78
78
. await
79
- . map_err ( |err| format ! ( "{err:?}" ) ) ?;
79
+ . map_err ( |err| format ! ( "{err:?}" ) ) ?
80
+ . ok_or ( JsValue :: from_str ( & format ! (
81
+ "No snapshot found for digest: '{digest}'"
82
+ ) ) ) ?;
80
83
81
84
Ok ( serde_wasm_bindgen:: to_value ( & result) ?)
82
85
}
83
86
84
87
/// Call the client to get the list of available snapshots
88
+ #[ wasm_bindgen]
85
89
pub async fn list_snapshots ( & self ) -> WasmResult {
86
90
let result = self
87
91
. client
@@ -94,18 +98,23 @@ impl MithrilClient {
94
98
}
95
99
96
100
/// Call the client to get a mithril stake distribution from a hash
101
+ #[ wasm_bindgen]
97
102
pub async fn get_mithril_stake_distribution ( & self , hash : & str ) -> WasmResult {
98
103
let result = self
99
104
. client
100
105
. mithril_stake_distribution ( )
101
106
. get ( hash)
102
107
. await
103
- . map_err ( |err| format ! ( "{err:?}" ) ) ?;
108
+ . map_err ( |err| format ! ( "{err:?}" ) ) ?
109
+ . ok_or ( JsValue :: from_str ( & format ! (
110
+ "No mithril stake distribution found for hash: '{hash}'"
111
+ ) ) ) ?;
104
112
105
113
Ok ( serde_wasm_bindgen:: to_value ( & result) ?)
106
114
}
107
115
108
116
/// Call the client for the list of available mithril stake distributions
117
+ #[ wasm_bindgen]
109
118
pub async fn list_mithril_stake_distributions ( & self ) -> WasmResult {
110
119
let result = self
111
120
. client
@@ -118,6 +127,7 @@ impl MithrilClient {
118
127
}
119
128
120
129
/// Call the client to compute a mithril stake distribution message
130
+ #[ wasm_bindgen]
121
131
pub async fn compute_mithril_stake_distribution_message (
122
132
& self ,
123
133
stake_distribution : JsValue ,
@@ -132,6 +142,7 @@ impl MithrilClient {
132
142
}
133
143
134
144
/// Call the client to verify a mithril stake distribution message
145
+ #[ wasm_bindgen]
135
146
pub async fn verify_message_match_certificate (
136
147
& self ,
137
148
message : JsValue ,
@@ -145,18 +156,23 @@ impl MithrilClient {
145
156
}
146
157
147
158
/// Call the client to get a mithril certificate from a certificate hash
159
+ #[ wasm_bindgen]
148
160
pub async fn get_mithril_certificate ( & self , hash : & str ) -> WasmResult {
149
161
let result = self
150
162
. client
151
163
. certificate ( )
152
164
. get ( hash)
153
165
. await
154
- . map_err ( |err| format ! ( "{err:?}" ) ) ?;
166
+ . map_err ( |err| format ! ( "{err:?}" ) ) ?
167
+ . ok_or ( JsValue :: from_str ( & format ! (
168
+ "No certificate found for hash: '{hash}'"
169
+ ) ) ) ?;
155
170
156
171
Ok ( serde_wasm_bindgen:: to_value ( & result) ?)
157
172
}
158
173
159
174
/// Call the client for the list of available mithril certificates
175
+ #[ wasm_bindgen]
160
176
pub async fn list_mithril_certificates ( & self ) -> WasmResult {
161
177
let result = self
162
178
. client
@@ -169,6 +185,7 @@ impl MithrilClient {
169
185
}
170
186
171
187
/// Call the client to verify the certificate chain from a certificate hash
188
+ #[ wasm_bindgen]
172
189
pub async fn verify_certificate_chain ( & self , hash : & str ) -> WasmResult {
173
190
let result = self
174
191
. client
@@ -184,19 +201,235 @@ impl MithrilClient {
184
201
#[ cfg( test) ]
185
202
mod tests {
186
203
use super :: * ;
204
+ use mithril_client:: {
205
+ common:: ProtocolMessage , MithrilCertificateListItem , MithrilStakeDistribution ,
206
+ MithrilStakeDistributionListItem , Snapshot , SnapshotListItem ,
207
+ } ;
187
208
use wasm_bindgen_test:: * ;
188
209
210
+ const AGGREGATOR_ENDPOINT : & str =
211
+ "https://aggregator.testing-preview.api.mithril.network/aggregator" ;
212
+ const GENESIS_VERIFICATION_KEY : & str = "5b33322c3235332c3138362c3230312c3137372c31312c3131372c3133352c3138372c3136372c3138312c3138382c32322c35392c3230362c3130352c3233312c3135302c3231352c33302c37382c3231322c37362c31362c3235322c3138302c37322c3133342c3133372c3234372c3136312c36385d" ;
213
+
189
214
wasm_bindgen_test_configure ! ( run_in_browser) ;
190
215
#[ wasm_bindgen_test]
191
- async fn certificate_get_list ( ) {
192
- let wasm_client = MithrilClient :: new (
193
- "https://aggregator.testing-preview.api.mithril.network/aggregator" ,
194
- "5b33322c3235332c3138362c3230312c3137372c31312c3131372c3133352c3138372c3136372c3138312c3138382c32322c35392c3230362c3130352c3233312c3135302c3231352c33302c37382c3231322c37362c31362c3235322c3138302c37322c3133342c3133372c3234372c3136312c36385d" ,
195
- ) . await ;
216
+ async fn list_snapshots_should_return_value_convertible_in_rust_type ( ) {
217
+ let client = MithrilClient :: new ( AGGREGATOR_ENDPOINT , GENESIS_VERIFICATION_KEY ) ;
218
+
219
+ let snapshots_list_js_value = client
220
+ . list_snapshots ( )
221
+ . await
222
+ . expect ( "list_snapshots should not fail" ) ;
223
+ let snapshots_list =
224
+ serde_wasm_bindgen:: from_value :: < Vec < SnapshotListItem > > ( snapshots_list_js_value)
225
+ . expect ( "conversion should not fail" ) ;
226
+
227
+ assert_eq ! ( snapshots_list. len( ) , 20 ) ;
228
+ }
229
+
230
+ #[ wasm_bindgen_test]
231
+ async fn get_snapshot_should_return_value_convertible_in_rust_type ( ) {
232
+ let client = MithrilClient :: new ( AGGREGATOR_ENDPOINT , GENESIS_VERIFICATION_KEY ) ;
233
+ let snapshots_list_js_value = client. list_snapshots ( ) . await . unwrap ( ) ;
234
+ let snapshots_list =
235
+ serde_wasm_bindgen:: from_value :: < Vec < SnapshotListItem > > ( snapshots_list_js_value)
236
+ . unwrap ( ) ;
237
+ let last_digest = & snapshots_list. first ( ) . unwrap ( ) . digest ;
238
+
239
+ let snapshot_js_value = client
240
+ . get_snapshot ( last_digest)
241
+ . await
242
+ . expect ( "get_snapshot should not fail" ) ;
243
+ let snapshot = serde_wasm_bindgen:: from_value :: < Snapshot > ( snapshot_js_value)
244
+ . expect ( "conversion should not fail" ) ;
245
+
246
+ assert_eq ! ( snapshot. digest, last_digest. to_string( ) ) ;
247
+ }
248
+
249
+ #[ wasm_bindgen_test]
250
+ async fn get_snapshot_should_fail_with_unknown_digest ( ) {
251
+ let client = MithrilClient :: new ( AGGREGATOR_ENDPOINT , GENESIS_VERIFICATION_KEY ) ;
252
+
253
+ client
254
+ . get_snapshot ( "whatever" )
255
+ . await
256
+ . expect_err ( "get_snapshot should fail" ) ;
257
+ }
258
+
259
+ #[ wasm_bindgen_test]
260
+ async fn list_mithril_stake_distributions_should_return_value_convertible_in_rust_type ( ) {
261
+ let wasm_client = MithrilClient :: new ( AGGREGATOR_ENDPOINT , GENESIS_VERIFICATION_KEY ) ;
196
262
197
- wasm_client
263
+ let msd_list_js_value = wasm_client
198
264
. list_mithril_stake_distributions ( )
199
265
. await
200
- . expect ( "should not fail" ) ;
266
+ . expect ( "list_mithril_stake_distributions should not fail" ) ;
267
+ let msd_list = serde_wasm_bindgen:: from_value :: < Vec < MithrilStakeDistributionListItem > > (
268
+ msd_list_js_value,
269
+ )
270
+ . expect ( "conversion should not fail" ) ;
271
+
272
+ assert_eq ! ( msd_list. len( ) , 20 ) ;
273
+ }
274
+
275
+ #[ wasm_bindgen_test]
276
+ async fn get_mithril_stake_distribution_should_return_value_convertible_in_rust_type ( ) {
277
+ let client = MithrilClient :: new ( AGGREGATOR_ENDPOINT , GENESIS_VERIFICATION_KEY ) ;
278
+ let msd_list_js_value = client. list_mithril_stake_distributions ( ) . await . unwrap ( ) ;
279
+ let msd_list = serde_wasm_bindgen:: from_value :: < Vec < MithrilStakeDistributionListItem > > (
280
+ msd_list_js_value,
281
+ )
282
+ . unwrap ( ) ;
283
+ let last_hash = & msd_list. first ( ) . unwrap ( ) . hash ;
284
+
285
+ let msd_js_value = client
286
+ . get_mithril_stake_distribution ( last_hash)
287
+ . await
288
+ . expect ( "get_mithril_stake_distribution should not fail" ) ;
289
+ let msd = serde_wasm_bindgen:: from_value :: < MithrilStakeDistribution > ( msd_js_value)
290
+ . expect ( "conversion should not fail" ) ;
291
+
292
+ assert_eq ! ( msd. hash, last_hash. to_string( ) ) ;
293
+ }
294
+
295
+ #[ wasm_bindgen_test]
296
+ async fn get_mithril_stake_distribution_should_fail_with_unknown_hash ( ) {
297
+ let client = MithrilClient :: new ( AGGREGATOR_ENDPOINT , GENESIS_VERIFICATION_KEY ) ;
298
+
299
+ client
300
+ . get_mithril_stake_distribution ( "whatever" )
301
+ . await
302
+ . expect_err ( "get_mithril_stake_distribution should fail" ) ;
303
+ }
304
+
305
+ #[ wasm_bindgen_test]
306
+ async fn list_mithril_certificates_should_return_value_convertible_in_rust_type ( ) {
307
+ let client = MithrilClient :: new ( AGGREGATOR_ENDPOINT , GENESIS_VERIFICATION_KEY ) ;
308
+
309
+ let certificates_list_js_value = client
310
+ . list_mithril_certificates ( )
311
+ . await
312
+ . expect ( "list_mithril_certificates should not fail" ) ;
313
+ let certificates_list = serde_wasm_bindgen:: from_value :: < Vec < MithrilCertificateListItem > > (
314
+ certificates_list_js_value,
315
+ )
316
+ . expect ( "conversion should not fail" ) ;
317
+
318
+ assert_eq ! ( certificates_list. len( ) , 20 ) ;
319
+ }
320
+
321
+ #[ wasm_bindgen_test]
322
+ async fn get_mithril_certificate_should_return_value_convertible_in_rust_type ( ) {
323
+ let client = MithrilClient :: new ( AGGREGATOR_ENDPOINT , GENESIS_VERIFICATION_KEY ) ;
324
+ let certificates_list_js_value = client. list_mithril_certificates ( ) . await . unwrap ( ) ;
325
+ let certificates_list = serde_wasm_bindgen:: from_value :: < Vec < MithrilCertificateListItem > > (
326
+ certificates_list_js_value,
327
+ )
328
+ . unwrap ( ) ;
329
+ let last_hash = & certificates_list. first ( ) . unwrap ( ) . hash ;
330
+
331
+ let certificate_js_value = client
332
+ . get_mithril_certificate ( last_hash)
333
+ . await
334
+ . expect ( "get_mithril_certificate should not fail" ) ;
335
+ let certificate =
336
+ serde_wasm_bindgen:: from_value :: < MithrilCertificate > ( certificate_js_value)
337
+ . expect ( "conversion should not fail" ) ;
338
+
339
+ assert_eq ! ( certificate. hash, last_hash. to_string( ) ) ;
340
+ }
341
+
342
+ #[ wasm_bindgen_test]
343
+ async fn get_mithril_certificate_should_fail_with_unknown_hash ( ) {
344
+ let client = MithrilClient :: new ( AGGREGATOR_ENDPOINT , GENESIS_VERIFICATION_KEY ) ;
345
+
346
+ client
347
+ . get_mithril_certificate ( "whatever" )
348
+ . await
349
+ . expect_err ( "get_mithril_certificate should fail" ) ;
350
+ }
351
+
352
+ #[ wasm_bindgen_test]
353
+ async fn compute_mithril_stake_distribution_message_should_return_value_convertible_in_rust_type (
354
+ ) {
355
+ let client = MithrilClient :: new ( AGGREGATOR_ENDPOINT , GENESIS_VERIFICATION_KEY ) ;
356
+ let msd_list_js_value = client. list_mithril_stake_distributions ( ) . await . unwrap ( ) ;
357
+ let msd_list = serde_wasm_bindgen:: from_value :: < Vec < MithrilStakeDistributionListItem > > (
358
+ msd_list_js_value,
359
+ )
360
+ . unwrap ( ) ;
361
+ let last_hash = & msd_list. first ( ) . unwrap ( ) . hash ;
362
+ let msd_js_value = client
363
+ . get_mithril_stake_distribution ( last_hash)
364
+ . await
365
+ . unwrap ( ) ;
366
+
367
+ let message_js_value = client
368
+ . compute_mithril_stake_distribution_message ( msd_js_value)
369
+ . await
370
+ . expect ( "compute_mithril_stake_distribution_message should not fail" ) ;
371
+ serde_wasm_bindgen:: from_value :: < ProtocolMessage > ( message_js_value)
372
+ . expect ( "conversion should not fail" ) ;
373
+ }
374
+
375
+ // This test is commented for now as it's execution takes too long with a real testnet aggregator
376
+ // Timeout in tests is 20s
377
+ /*
378
+ #[wasm_bindgen_test]
379
+ async fn verify_certificate_chain_should_return_value_convertible_in_rust_type() {
380
+ let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
381
+ let msd_list_js_value = client.list_mithril_stake_distributions().await.unwrap();
382
+ let msd_list = serde_wasm_bindgen::from_value::<Vec<MithrilStakeDistributionListItem>>(
383
+ msd_list_js_value,
384
+ )
385
+ .unwrap();
386
+ let last_hash = &msd_list.first().unwrap().hash;
387
+ let msd_js_value = client
388
+ .get_mithril_stake_distribution(last_hash)
389
+ .await
390
+ .unwrap();
391
+ let msd = serde_wasm_bindgen::from_value::<MithrilStakeDistribution>(msd_js_value).unwrap();
392
+
393
+ let certificate_js_value = client
394
+ .verify_certificate_chain(&msd.certificate_hash)
395
+ .await
396
+ .expect("verify_certificate_chain should not fail");
397
+ serde_wasm_bindgen::from_value::<MithrilCertificate>(certificate_js_value)
398
+ .expect("conversion should not fail");
399
+ }
400
+ */
401
+
402
+ // This test is commented for now as it's execution takes too long with a real testnet aggregator
403
+ // Timeout in tests is 20s
404
+ /*
405
+ #[wasm_bindgen_test]
406
+ async fn verify_message_match_certificate_should_return_true() {
407
+ let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
408
+ let msd_list_js_value = client.list_mithril_stake_distributions().await.unwrap();
409
+ let msd_list = serde_wasm_bindgen::from_value::<Vec<MithrilStakeDistributionListItem>>(
410
+ msd_list_js_value,
411
+ )
412
+ .unwrap();
413
+ let last_hash = &msd_list.first().unwrap().hash;
414
+ let msd_js_value = client
415
+ .get_mithril_stake_distribution(last_hash)
416
+ .await
417
+ .unwrap();
418
+ let msd = serde_wasm_bindgen::from_value::<MithrilStakeDistribution>(msd_js_value.clone())
419
+ .unwrap();
420
+ let last_certificate_js_value = client
421
+ .verify_certificate_chain(&msd.certificate_hash)
422
+ .await
423
+ .unwrap();
424
+ let message_js_value = client
425
+ .compute_mithril_stake_distribution_message(msd_js_value)
426
+ .await
427
+ .unwrap();
428
+
429
+ client
430
+ .verify_message_match_certificate(message_js_value, last_certificate_js_value)
431
+ .await
432
+ .expect("verify_message_match_certificate should not fail");
201
433
}
434
+ */
202
435
}
0 commit comments