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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use crate::PublicKeyErrors;
use std::{borrow::Borrow, ops::Deref};
use elliptic_curve::sec1::*;
use k256::{PublicKey as PubKey, Secp256k1};
use wasm_bindgen::{prelude::*, throw_str, JsStatic};
use crate::PrivateKey;
#[wasm_bindgen]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PublicKey {
point: Vec<u8>,
is_compressed: bool,
}
impl PublicKey {
pub(crate) fn from_private_key_impl(priv_key: &PrivateKey, compress: bool) -> PublicKey {
PublicKey {
point: priv_key.get_point(compress),
is_compressed: compress,
}
}
pub(crate) fn to_hex_impl(&self) -> Result<String, PublicKeyErrors> {
let bytes = self.to_bytes_impl()?;
return Ok(hex::encode(bytes));
}
pub(crate) fn to_bytes_impl(&self) -> Result<Vec<u8>, PublicKeyErrors> {
let point: EncodedPoint<Secp256k1> = match EncodedPoint::from_bytes(&self.point.clone()) {
Ok(v) => v,
Err(e) => {
return Err(PublicKeyErrors::InvalidPoint {
error: e,
})
}
};
Ok(point.as_bytes().to_vec())
}
pub(crate) fn from_bytes_impl(bytes: &[u8], compress: bool) -> Result<PublicKey, PublicKeyErrors> {
let point: EncodedPoint<Secp256k1> = match EncodedPoint::from_bytes(bytes) {
Ok(v) => v,
Err(e) => {
return Err(PublicKeyErrors::InvalidPoint {
error: e,
})
}
};
Ok(PublicKey {
point: point.compress().as_bytes().to_vec(),
is_compressed: compress,
})
}
pub(crate) fn from_hex_impl(hex_str: String, compress: bool) -> Result<PublicKey, PublicKeyErrors> {
let point_bytes = match hex::decode(hex_str) {
Ok(v) => v,
Err(e) => {
return Err(PublicKeyErrors::ParseHex {
error: e
})
}
};
PublicKey::from_bytes_impl(&point_bytes, compress)
}
}
impl PublicKey {
fn param_is_compressed(compress: Option<bool>) -> bool {
match compress {
Some(v) => v,
None => true
}
}
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
impl PublicKey {
#[wasm_bindgen(js_name = fromHex)]
pub fn from_hex(hex_str: String, compress: Option<bool>) -> Result<PublicKey, JsValue> {
match PublicKey::from_hex_impl(hex_str, PublicKey::param_is_compressed(compress)) {
Ok(v) => Ok(v),
Err(e) => throw_str(&e.to_string()),
}
}
#[wasm_bindgen(js_name = fromBytes)]
pub fn from_bytes(bytes: &[u8], compress: Option<bool>) -> Result<PublicKey, JsValue> {
match PublicKey::from_bytes_impl(bytes, PublicKey::param_is_compressed(compress)) {
Ok(v) => Ok(v),
Err(e) => throw_str(&e.to_string()),
}
}
#[wasm_bindgen(js_name = toBytes)]
pub fn to_bytes(&self) -> Result<Vec<u8>, JsValue> {
match PublicKey::to_bytes_impl(&self) {
Ok(v) => Ok(v),
Err(e) => throw_str(&e.to_string()),
}
}
#[wasm_bindgen(js_name = toHex)]
pub fn to_hex(&self) -> Result<String, JsValue> {
match PublicKey::to_hex_impl(&self) {
Ok(v) => Ok(v),
Err(e) => throw_str(&e.to_string()),
}
}
#[wasm_bindgen(js_name = fromPrivateKey)]
pub fn from_private_key(priv_key: &PrivateKey, compress: Option<bool>) -> PublicKey {
PublicKey::from_private_key_impl(priv_key, PublicKey::param_is_compressed(compress))
}
}
#[cfg(not(target_arch = "wasm32"))]
impl PublicKey {
pub fn from_hex(hex_str: String, compress: bool) -> Result<PublicKey, PublicKeyErrors> {
PublicKey::from_hex_impl(hex_str, compress)
}
pub fn from_bytes(bytes: &[u8], compress: bool) -> Result<PublicKey, PublicKeyErrors> {
PublicKey::from_bytes_impl(bytes, compress)
}
pub fn to_bytes(&self) -> Result<Vec<u8>, PublicKeyErrors> {
PublicKey::to_bytes_impl(&self)
}
pub fn to_hex(&self) -> Result<String, PublicKeyErrors> {
PublicKey::to_hex_impl(&self)
}
pub fn from_private_key(priv_key: &PrivateKey, compress: bool) -> PublicKey {
PublicKey::from_private_key_impl(priv_key, compress)
}
}