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
use std::convert::TryFrom;

use chain_impl_mockchain::certificate::VotePlanId as VotePlanIdLib;
use wasm_bindgen::prelude::*;

// TODO add VotePlan certificate

#[derive(Clone, Debug, PartialEq, Eq)]
#[wasm_bindgen]
pub struct VotePlanId(pub(crate) VotePlanIdLib);

#[wasm_bindgen]
impl VotePlanId {
    pub fn from_hex(hex_data: String) -> Result<VotePlanId, JsValue> {
        Ok(VotePlanId(
            VotePlanIdLib::try_from(
                hex::decode(hex_data)
                    .map_err(|e| JsValue::from(e.to_string()))?
                    .as_slice(),
            )
            .map_err(|e| JsValue::from(e.to_string()))?,
        ))
    }
    pub fn from_bytes(bytes: &[u8]) -> Result<VotePlanId, JsValue> {
        Ok(VotePlanId(
            VotePlanIdLib::try_from(bytes).map_err(|e| JsValue::from(e.to_string()))?,
        ))
    }

    pub fn to_bytes(&self) -> Result<Box<[u8]>, JsValue> {
        Ok(self.0.as_ref().into())
    }
}