Skip to main content

charms_lib/
lib.rs

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