charms_lib/
lib.rs

1use charms_client::{NormalizedSpell, tx::Tx};
2use wasm_bindgen::{JsValue, prelude::wasm_bindgen};
3
4/// Verification key for the current `charms-spell-checker` binary
5/// (and the current protocol version).
6pub const SPELL_VK: &str = "0x0041d9843ec25ba04797a0ce29af364389f7eda9f7126ef39390c357432ad9aa";
7
8#[wasm_bindgen(js_name = "extractAndVerifySpell")]
9pub fn extract_and_verify_spell_js(tx: JsValue, mock: bool) -> Result<JsValue, JsValue> {
10    let tx: Tx = serde_wasm_bindgen::from_value(tx)?;
11    let norm_spell = extract_and_verify_spell(&tx, mock)?;
12    let value = serde_wasm_bindgen::to_value(&norm_spell)?;
13    Ok(value)
14}
15
16pub fn extract_and_verify_spell(tx: &Tx, mock: bool) -> Result<NormalizedSpell, String> {
17    let norm_spell = charms_client::tx::extract_and_verify_spell(SPELL_VK, tx, mock)
18        .map_err(|e| e.to_string())?;
19    Ok(norm_spell)
20}
21
22#[cfg(test)]
23mod tests {
24    use super::*;
25
26    #[test]
27    fn test_extract_and_verify_spell() {
28        let tx_json = include_str!("../test/bitcoin-tx.json");
29        let tx: Tx = serde_json::from_str(tx_json).unwrap();
30        let norm_spell = extract_and_verify_spell(&tx, true).unwrap();
31        println!("{}", serde_json::to_string_pretty(&norm_spell).unwrap());
32    }
33}