Skip to content

Commit

Permalink
light-client-js: use serde-wasm-bindgen
Browse files Browse the repository at this point in the history
To remove deprecation warnings and do the right thing,
use serde-wasm-bindgen utilities for serializing and deserializing
Rust types to and from JsValue.
  • Loading branch information
mzabaluev committed Dec 7, 2022
1 parent 7c85ce2 commit 84b9cc6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
1 change: 1 addition & 0 deletions light-client-js/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ serde_json = { version = "1.0", default-features = false }
tendermint = { version = "0.27.0", default-features = false, path = "../tendermint" }
tendermint-light-client-verifier = { version = "0.27.0", default-features = false, path = "../light-client-verifier" }
wasm-bindgen = { version = "0.2.63", default-features = false, features = [ "serde-serialize" ] }
serde-wasm-bindgen = { version = "0.4.5", default-features = false }

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
Expand Down
58 changes: 31 additions & 27 deletions light-client-js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,46 +25,50 @@ use wasm_bindgen::{prelude::*, JsValue};
/// Check whether a given untrusted block can be trusted.
#[wasm_bindgen]
pub fn verify(untrusted: &JsValue, trusted: &JsValue, options: &JsValue, now: &JsValue) -> JsValue {
let result = deserialize_params(untrusted, trusted, options, now).map(
|(untrusted, trusted, options, now)| {
let verifier = ProdVerifier::default();
verifier.verify(
untrusted.as_untrusted_state(),
trusted.as_trusted_state(),
&options,
now,
)
},
);
JsValue::from_serde(&result).unwrap()
let result = deserialize_params(
untrusted.clone(),
trusted.clone(),
options.clone(),
now.clone(),
)
.map(|(untrusted, trusted, options, now)| {
let verifier = ProdVerifier::default();
verifier.verify(
untrusted.as_untrusted_state(),
trusted.as_trusted_state(),
&options,
now,
)
});
serde_wasm_bindgen::to_value(&result).unwrap()
}

fn deserialize_params(
untrusted: &JsValue,
trusted: &JsValue,
options: &JsValue,
now: &JsValue,
untrusted: JsValue,
trusted: JsValue,
options: JsValue,
now: JsValue,
) -> Result<(LightBlock, LightBlock, Options, Time), Error> {
let untrusted = untrusted.into_serde().map_err(|e| Error::Serialization {
param: "untrusted".to_string(),
msg: e.to_string(),
})?;
let untrusted =
serde_wasm_bindgen::from_value(untrusted).map_err(|e| Error::Serialization {
param: "untrusted".into(),
msg: e.to_string(),
})?;

let trusted = trusted.into_serde().map_err(|e| Error::Serialization {
param: "trusted".to_string(),
let trusted = serde_wasm_bindgen::from_value(trusted).map_err(|e| Error::Serialization {
param: "trusted".into(),
msg: e.to_string(),
})?;

let options = options
.into_serde::<JsOptions>()
let options = serde_wasm_bindgen::from_value::<JsOptions>(options)
.map(Into::into)
.map_err(|e| Error::Serialization {
param: "options".to_string(),
param: "options".into(),
msg: e.to_string(),
})?;

let now = now.into_serde().map_err(|e| Error::Serialization {
param: "now".to_string(),
let now = serde_wasm_bindgen::from_value(now).map_err(|e| Error::Serialization {
param: "now".into(),
msg: e.to_string(),
})?;

Expand Down

0 comments on commit 84b9cc6

Please sign in to comment.