iop_keyvault_wasm/
pk.rs

1use super::*;
2
3/// Multicipher public key
4///
5/// A public key (also called shared key or pk in some literature) is that part
6/// of an asymmetric keypair which can be used to verify the authenticity of the
7/// sender of a message or to encrypt a message that can only be decrypted by a
8/// single recipient. In both cases this other party owns the {@link PrivateKey}
9/// part of the keypair and never shares it with anyone else.
10#[wasm_bindgen(js_name = PublicKey)]
11#[derive(Clone, Debug)]
12pub struct JsMPublicKey {
13    inner: MPublicKey,
14}
15
16#[wasm_bindgen(js_class = PublicKey)]
17impl JsMPublicKey {
18    /// Parses a string into a {@link PublicKey}.
19    #[wasm_bindgen(constructor)]
20    pub fn new(pub_key_str: &str) -> Result<JsMPublicKey, JsValue> {
21        let inner: MPublicKey = pub_key_str.parse().map_err_to_js()?;
22        Ok(Self { inner })
23    }
24
25    /// Converts a {@link SecpPublicKey} into a multicipher {@link PublicKey}.
26    #[wasm_bindgen(js_name = fromSecp)]
27    pub fn from_secp(pk: &JsSecpPublicKey) -> Self {
28        let inner = MPublicKey::from(pk.inner().clone());
29        Self { inner }
30    }
31
32    /// All multicipher public keys start with this prefix
33    #[wasm_bindgen]
34    pub fn prefix() -> String {
35        MPublicKey::PREFIX.to_string()
36    }
37
38    /// Calculates the key id (also called fingerprint or address in some
39    /// literature) of the public key.
40    #[wasm_bindgen(js_name = keyId)]
41    pub fn key_id(&self) -> JsMKeyId {
42        JsMKeyId::from(self.inner.key_id())
43    }
44
45    /// Validates if `key_id` belongs to this public key
46    ///
47    /// We do not yet have multiple versions of key ids for the same multicipher
48    /// public key, so for now this comparison is trivial. But when we introduce
49    /// newer versions, we need to take the version of the `key_id` argument
50    /// into account and calculate that possibly older version from `self`.
51    #[wasm_bindgen(js_name = validateId)]
52    pub fn validate_id(&self, key_id: &JsMKeyId) -> bool {
53        self.inner.validate_id(key_id.inner())
54    }
55
56    /// This method can be used to verify if a given signature for a message was
57    /// made using the private key that belongs to this public key.
58    ///
59    /// @see PrivateKey.sign
60    #[wasm_bindgen(js_name = validateEcdsa)]
61    pub fn validate_ecdsa(&self, data: &[u8], signature: &JsMSignature) -> bool {
62        self.inner.verify(data, signature.inner())
63    }
64
65    /// Converts a {@link PublicKey} into a string.
66    // Note that Clippy complains if you call these methods to_string. But implementing Display is not enough to get a toString in JS.
67    #[wasm_bindgen(js_name=toString)]
68    pub fn stringify(&self) -> String {
69        self.inner.to_string()
70    }
71}
72
73impl From<MPublicKey> for JsMPublicKey {
74    fn from(inner: MPublicKey) -> Self {
75        Self { inner }
76    }
77}
78
79impl Wraps<MPublicKey> for JsMPublicKey {
80    fn inner(&self) -> &MPublicKey {
81        &self.inner
82    }
83}
84
85/// Secp256k1 public key
86#[wasm_bindgen(js_name = SecpPublicKey)]
87#[derive(Clone, Debug)]
88pub struct JsSecpPublicKey {
89    inner: SecpPublicKey,
90}
91
92#[wasm_bindgen(js_class = SecpPublicKey)]
93impl JsSecpPublicKey {
94    /// Parses a string into a {@link SecpPublicKey}.
95    #[wasm_bindgen(constructor)]
96    pub fn new(key: &str) -> Result<JsSecpPublicKey, JsValue> {
97        let inner: SecpPublicKey = key.parse().map_err_to_js()?;
98        Ok(Self { inner })
99    }
100
101    /// Calculates the key id (also called fingerprint or address in some
102    /// literature) of the public key.
103    #[wasm_bindgen(js_name = keyId)]
104    pub fn key_id(&self) -> JsSecpKeyId {
105        JsSecpKeyId::from(self.inner.key_id())
106    }
107
108    /// Calculates the key id of the public key the non-standard way ark.io and
109    /// therefore Hydra uses.
110    ///
111    /// Regular bitcoin-based chains use the ripemd160 hash of the sha2-256 hash
112    /// of the public key, but ARK only uses ripemd160.
113    #[wasm_bindgen(js_name = arkKeyId)]
114    pub fn ark_key_id(&self) -> JsSecpKeyId {
115        JsSecpKeyId::from(self.inner.ark_key_id())
116    }
117
118    /// Validates if `key_id` belongs to this public key
119    #[wasm_bindgen(js_name = validateId)]
120    pub fn validate_id(&self, key_id: &JsSecpKeyId) -> bool {
121        self.inner.validate_id(key_id.inner())
122    }
123
124    /// Validates if `key_id` belongs to this public key if it was generated
125    /// the ark.io way.
126    #[wasm_bindgen(js_name = validateArkId)]
127    pub fn validate_ark_id(&self, key_id: &JsSecpKeyId) -> bool {
128        self.inner.validate_ark_id(key_id.inner())
129    }
130
131    /// This method can be used to verify if a given signature for a message was
132    /// made using the private key that belongs to this public key.
133    #[wasm_bindgen(js_name = validateEcdsa)]
134    pub fn validate_ecdsa(&self, data: &[u8], signature: &JsSecpSignature) -> bool {
135        self.inner.verify(data, signature.inner())
136    }
137
138    /// Converts a {@link SecpPublicKey} into a string.
139    // Note that Clippy complains if you call these methods to_string. But implementing Display is not enough to get a toString in JS.
140    #[wasm_bindgen(js_name=toString)]
141    pub fn stringify(&self) -> String {
142        self.inner.to_string()
143    }
144}
145
146impl From<SecpPublicKey> for JsSecpPublicKey {
147    fn from(inner: SecpPublicKey) -> Self {
148        Self { inner }
149    }
150}
151
152impl Wraps<SecpPublicKey> for JsSecpPublicKey {
153    fn inner(&self) -> &SecpPublicKey {
154        &self.inner
155    }
156}