@@ -18,7 +18,7 @@ use js_sys::{Error, Uint8Array};
1818use secrecy:: ExposeSecret ;
1919use wasm_bindgen:: prelude:: { wasm_bindgen, JsValue } ;
2020use wasm_bindgen:: JsCast ;
21- use wasm_bindgen_derive:: TryFromJsValue ;
21+ use wasm_bindgen_derive:: { try_from_js_array , try_from_js_option , TryFromJsValue } ;
2222
2323use crate as umbral_pre;
2424use crate :: { DefaultDeserialize , DefaultSerialize } ;
@@ -45,45 +45,6 @@ fn map_js_err<T: fmt::Display>(err: T) -> Error {
4545 Error :: new ( & format ! ( "{err}" ) )
4646}
4747
48- /// Tries to convert an optional value (either `null` or a `#[wasm_bindgen]` marked structure)
49- /// from `JsValue` to the Rust type.
50- // TODO (#25): This is necessary since wasm-bindgen does not support
51- // having a parameter of `Option<&T>`, and using `Option<T>` consumes the argument
52- // (see https://github.com/rustwasm/wasm-bindgen/issues/2370).
53- fn try_from_js_option < ' a , T > ( value : & ' a JsValue ) -> Result < Option < T > , Error >
54- where
55- T : TryFrom < & ' a JsValue > ,
56- <T as TryFrom < & ' a JsValue > >:: Error : fmt:: Display ,
57- {
58- let typed_value = if value. is_null ( ) {
59- None
60- } else {
61- Some ( T :: try_from ( value) . map_err ( map_js_err) ?)
62- } ;
63- Ok ( typed_value)
64- }
65-
66- /// Tries to convert a JS array from `JsValue` to a vector of Rust type elements.
67- // TODO (#23): This is necessary since wasm-bindgen does not support
68- // having a parameter of `Vec<&T>`
69- // (see https://github.com/rustwasm/wasm-bindgen/issues/111).
70- fn try_from_js_array < T > ( value : & JsValue ) -> Result < Vec < T > , Error >
71- where
72- for < ' a > T : TryFrom < & ' a JsValue > ,
73- for < ' a > <T as TryFrom < & ' a JsValue > >:: Error : fmt:: Display ,
74- {
75- let array: & js_sys:: Array = value
76- . dyn_ref ( )
77- . ok_or_else ( || Error :: new ( "Got a non-array argument where an array was expected" ) ) ?;
78- let length: usize = array. length ( ) . try_into ( ) . map_err ( map_js_err) ?;
79- let mut result = Vec :: < T > :: with_capacity ( length) ;
80- for js in array. iter ( ) {
81- let typed_elem = T :: try_from ( & js) . map_err ( map_js_err) ?;
82- result. push ( typed_elem) ;
83- }
84- Ok ( result)
85- }
86-
8748#[ wasm_bindgen]
8849#[ derive( derive_more:: AsRef ) ]
8950pub struct SecretKey ( umbral_pre:: SecretKey ) ;
@@ -473,7 +434,7 @@ pub fn decrypt_reencrypted(
473434 // TODO (#23): using a custom type since `wasm_bindgen` currently does not support
474435 // Vec<CustomStruct> as a parameter.
475436 // Will probably be fixed along with https://github.com/rustwasm/wasm-bindgen/issues/111
476- let typed_vcfrags = try_from_js_array :: < VerifiedCapsuleFrag > ( vcfrags. as_ref ( ) ) ?;
437+ let typed_vcfrags = try_from_js_array :: < VerifiedCapsuleFrag > ( vcfrags) . map_err ( map_js_err ) ?;
477438 let backend_vcfrags = typed_vcfrags. into_iter ( ) . map ( |vcfrag| vcfrag. 0 ) ;
478439 umbral_pre:: decrypt_reencrypted (
479440 & receiving_sk. 0 ,
@@ -499,8 +460,10 @@ impl KeyFrag {
499460 delegating_pk : & OptionPublicKey ,
500461 receiving_pk : & OptionPublicKey ,
501462 ) -> Result < VerifiedKeyFrag , Error > {
502- let typed_delegating_pk = try_from_js_option :: < PublicKey > ( delegating_pk. as_ref ( ) ) ?;
503- let typed_receiving_pk = try_from_js_option :: < PublicKey > ( receiving_pk. as_ref ( ) ) ?;
463+ let typed_delegating_pk =
464+ try_from_js_option :: < PublicKey > ( delegating_pk) . map_err ( map_js_err) ?;
465+ let typed_receiving_pk =
466+ try_from_js_option :: < PublicKey > ( receiving_pk) . map_err ( map_js_err) ?;
504467
505468 self . 0
506469 . verify (
0 commit comments