-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathverify_js.rs
68 lines (60 loc) · 1.97 KB
/
verify_js.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use wasm_bindgen::prelude::*;
use crate::{G1Pubkey, InvalidPoint, Pubkey, VerificationError};
struct VerifyWebError(pub String);
impl From<hex::FromHexError> for VerifyWebError {
fn from(source: hex::FromHexError) -> Self {
Self(source.to_string())
}
}
impl From<InvalidPoint> for VerifyWebError {
fn from(source: InvalidPoint) -> Self {
Self(source.to_string())
}
}
impl From<VerificationError> for VerifyWebError {
fn from(source: VerificationError) -> Self {
Self(source.to_string())
}
}
impl From<VerifyWebError> for JsValue {
fn from(source: VerifyWebError) -> JsValue {
JsValue::from_str(&source.0)
}
}
/// This is the entry point from JavaScript.
///
/// The argument types are chosen such that the JS binding is simple
/// (u32 can be expressed as number, u64 cannot; strings are easier than binary data).
///
/// The result type is translated to an exception in case of an error
/// and too a boolean value in case of success.
#[wasm_bindgen]
#[allow(dead_code)] // exported via wasm_bindgen
pub fn verify_beacon(
pk_hex: &str,
round: u32,
previous_signature_hex: &str,
signature_hex: &str,
) -> Result<bool, JsValue> {
Ok(verify_beacon_impl(
pk_hex,
round,
previous_signature_hex,
signature_hex,
)?)
}
/// Like verify_beacon but with the structured error type needed to translate between lower level errors and JsValue.
/// If you cn show me how to translate from hex::FromHexError to JsValue without this intermediate function,
/// I'd be happy to learn how.
fn verify_beacon_impl(
pk_hex: &str,
round: u32,
previous_signature_hex: &str,
signature_hex: &str,
) -> Result<bool, VerifyWebError> {
let pk = G1Pubkey::from_variable(&hex::decode(pk_hex)?)?;
let previous_signature = hex::decode(previous_signature_hex)?;
let signature = hex::decode(signature_hex)?;
let result = pk.verify(round.into(), &previous_signature, &signature)?;
Ok(result)
}