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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use super::*;

/// Multicipher signature
#[wasm_bindgen(js_name = Signature)]
#[derive(Clone, Debug)]
pub struct JsMSignature {
    inner: MSignature,
}

#[wasm_bindgen(js_class = Signature)]
impl JsMSignature {
    /// Parses a string into a {@link Signature}.
    #[wasm_bindgen(constructor)]
    pub fn new(sign_str: &str) -> Result<JsMSignature, JsValue> {
        let inner: MSignature = sign_str.parse().map_err_to_js()?;
        Ok(Self { inner })
    }

    /// Converts a {@link SecpSignature} into a multicipher {@link Signature}.
    #[wasm_bindgen(js_name = fromSecp)]
    pub fn from_secp(secp: &JsSecpSignature) -> Self {
        let inner = MSignature::from(secp.inner().clone());
        Self { inner }
    }

    /// All multicipher signatures start with this prefix
    #[wasm_bindgen]
    pub fn prefix() -> String {
        MSignature::PREFIX.to_string()
    }

    /// Converts a {@link Signature} into a string.
    // Note that Clippy complains if you call these methods to_string. But implementing Display is not enough to get a toString in JS.
    #[wasm_bindgen(js_name=toString)]
    pub fn stringify(&self) -> String {
        self.inner.to_string()
    }
}

impl From<MSignature> for JsMSignature {
    fn from(inner: MSignature) -> Self {
        Self { inner }
    }
}

impl Wraps<MSignature> for JsMSignature {
    fn inner(&self) -> &MSignature {
        &self.inner
    }
}

/// Secp256k1 signature
#[wasm_bindgen(js_name = SecpSignature)]
#[derive(Clone, Debug)]
pub struct JsSecpSignature {
    inner: SecpSignature,
}

#[wasm_bindgen(js_class = SecpSignature)]
impl JsSecpSignature {
    /// Deserializes an ASN.1 DER encoded buffer into a {@link SepcSignature}
    #[wasm_bindgen(js_name = fromDer)]
    pub fn from_der(bytes: &[u8]) -> Result<JsSecpSignature, JsValue> {
        let inner = SecpSignature::from_der(bytes).map_err_to_js()?;
        Ok(Self { inner })
    }

    /// Serializes a {@link SepcSignature} into an ASN.1 DER encoded buffer
    #[wasm_bindgen(js_name = toDer)]
    pub fn to_der(&self) -> Vec<u8> {
        self.inner.to_der()
    }

    /// Converts a {@link SecpSignature} into a string.
    // Note that Clippy complains if you call these methods to_string. But implementing Display is not enough to get a toString in JS.
    #[wasm_bindgen(js_name=toString)]
    pub fn stringify(&self) -> String {
        self.inner.to_string()
    }
}

impl From<SecpSignature> for JsSecpSignature {
    fn from(inner: SecpSignature) -> Self {
        Self { inner }
    }
}

impl Wraps<SecpSignature> for JsSecpSignature {
    fn inner(&self) -> &SecpSignature {
        &self.inner
    }
}