From 8d115c057c8abceb2695471e56ffb414d73581fc Mon Sep 17 00:00:00 2001 From: Zhen Lu Date: Tue, 26 Sep 2023 15:34:41 -0700 Subject: [PATCH] Add wasm function --- src/remote_signing.rs | 45 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/remote_signing.rs b/src/remote_signing.rs index 6710d51..81c46c6 100644 --- a/src/remote_signing.rs +++ b/src/remote_signing.rs @@ -81,3 +81,48 @@ pub fn handle_remote_signing_webhook_event( .expect("serde value to json should not fail"), }) } + + +#[wasm_bindgen] +extern "C" { + pub type JsValidation; + + #[wasm_bindgen(structural, method)] + pub fn validate(this: &JsValidation, request: String) -> bool; +} + +unsafe impl Send for JsValidation {} +unsafe impl Sync for JsValidation {} + +pub struct WasmValidator { + js_validation: JsValidation, +} + +impl WasmValidator { + pub fn new(js_validation: JsValidation) -> Self { + Self { js_validation } + } +} + +impl Validation for WasmValidator { + fn should_sign(&self, webhook: String) -> bool { + self.js_validation.validate(webhook) + } +} + +pub fn wasm_handle_remote_signing_webhook_event( + webhook_data: Vec, + webhook_signature: String, + webhook_secret: String, + master_seed_bytes: Vec, + validation: JsValidation, +) -> Result { + let validator = WasmValidator::new(validation); + handle_remote_signing_webhook_event( + webhook_data, + webhook_signature, + webhook_secret, + master_seed_bytes, + Box::new(validator), + ) +}