-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathpublic_key_binary.rs
217 lines (185 loc) · 6.33 KB
/
public_key_binary.rs
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use crate::{Error, KeyTag, PublicKey, Result};
use std::{convert::TryFrom, fmt, str::FromStr};
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PublicKeyBinary(Vec<u8>);
impl fmt::Debug for PublicKeyBinary {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let key_tag = KeyTag::try_from(self.0[0]).map_err(|_| fmt::Error)?;
f.debug_struct("PublicKeyBinary")
.field("network", &key_tag.network)
.field("type", &key_tag.key_type)
.field("address", &self.to_string())
.finish()
}
}
impl From<PublicKey> for PublicKeyBinary {
fn from(value: PublicKey) -> Self {
Self(value.to_vec())
}
}
impl From<&[u8]> for PublicKeyBinary {
fn from(value: &[u8]) -> Self {
Self(value.to_vec())
}
}
impl From<Vec<u8>> for PublicKeyBinary {
fn from(value: Vec<u8>) -> Self {
Self(value)
}
}
impl From<PublicKeyBinary> for Vec<u8> {
fn from(value: PublicKeyBinary) -> Self {
value.0
}
}
impl TryFrom<PublicKeyBinary> for PublicKey {
type Error = Error;
fn try_from(value: PublicKeyBinary) -> Result<Self> {
Self::try_from(value.0)
}
}
impl AsRef<[u8]> for PublicKeyBinary {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
impl std::str::FromStr for PublicKeyBinary {
type Err = Error;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
let mut data = bs58::decode(s).with_check(Some(0)).into_vec()?;
Ok(Self(data.split_off(1)))
}
}
impl std::fmt::Display for PublicKeyBinary {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
// allocate one extra byte for the base58 version
let mut data = vec![0u8; self.0.len() + 1];
data[1..].copy_from_slice(&self.0);
let encoded = bs58::encode(&data).with_check().into_string();
f.write_str(&encoded)
}
}
use serde::{
de::{self, Visitor},
Deserialize, Deserializer, Serialize, Serializer,
};
impl Serialize for PublicKeyBinary {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.to_string())
}
}
impl<'de> Deserialize<'de> for PublicKeyBinary {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct PublicKeyVisitor;
impl<'de> Visitor<'de> for PublicKeyVisitor {
type Value = PublicKeyBinary;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("base58 public key")
}
fn visit_str<E>(self, value: &str) -> std::result::Result<Self::Value, E>
where
E: de::Error,
{
let key = Self::Value::from_str(value)
.map_err(|_| de::Error::custom("invalid public key"))?;
Ok(key)
}
}
deserializer.deserialize_str(PublicKeyVisitor)
}
}
#[cfg(feature = "sqlx-postgres")]
mod sqlx_postgres {
use super::*;
use sqlx::{
decode::Decode,
encode::{Encode, IsNull},
error::BoxDynError,
postgres::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueRef, Postgres},
types::Type,
};
impl Type<Postgres> for PublicKeyBinary {
fn type_info() -> PgTypeInfo {
PgTypeInfo::with_name("text")
}
}
impl PgHasArrayType for PublicKeyBinary {
fn array_type_info() -> PgTypeInfo {
PgTypeInfo::with_name("_text")
}
}
impl Encode<'_, Postgres> for PublicKeyBinary {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
let address = self.to_string();
Encode::<Postgres>::encode(&address, buf)
}
fn size_hint(&self) -> usize {
25
}
}
impl<'r> Decode<'r, Postgres> for PublicKeyBinary {
fn decode(value: PgValueRef<'r>) -> std::result::Result<Self, BoxDynError> {
let value = <&str as Decode<Postgres>>::decode(value)?;
let key = Self::from_str(value)?;
Ok(key)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::PublicKey;
#[test]
fn public_key_roundtrip() {
// This is a valid b58 encoded compact key
const B58: &str = "11263KvqW3GZPAvag5sQYtBJSjb25azSTSwoi5Tza9kboaLRxcsv";
let pubkey_bin: PublicKeyBinary = B58.parse().expect("public key");
let pubkey: PublicKey = B58.parse().expect("public key");
assert_eq!(pubkey_bin.to_string(), B58.to_string());
assert_eq!(pubkey_bin, PublicKeyBinary::from(pubkey_bin.as_ref()));
assert_eq!(pubkey_bin, PublicKeyBinary::from(pubkey.clone()));
assert_eq!(
pubkey_bin,
PublicKeyBinary::from(<PublicKeyBinary as Into<Vec<u8>>>::into(pubkey_bin.clone()))
);
assert_eq!(pubkey.to_string(), pubkey_bin.to_string());
assert_eq!(pubkey, PublicKey::try_from(pubkey_bin).expect("public key"));
}
use hex_literal::hex;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
const DEFAULT_BYTES: [u8; 33] =
hex!("008f23e96ab6bbff48c8923cac831dc97111bcf33dba9f5a8539c00f9d93551af1");
fn parse_pubkey(bytes: &[u8; 33]) -> PublicKeyBinary {
PublicKeyBinary::from(bytes.to_vec())
}
// move the key so as to consume it and avoid accidentally hashing the same thing twice in tests
fn pubkey_hash(pubkey: PublicKeyBinary) -> u64 {
let mut hasher = DefaultHasher::new();
pubkey.hash(&mut hasher);
hasher.finish()
}
#[test]
fn hash_match() {
let bytes = DEFAULT_BYTES;
let public_key_one = parse_pubkey(&bytes);
let public_key_two = parse_pubkey(&bytes);
let hash_one = pubkey_hash(public_key_one);
let hash_two = pubkey_hash(public_key_two);
// hashing same input twice should always match
assert_eq!(hash_one, hash_two);
}
#[test]
fn serde() {
let orig_pub_key = parse_pubkey(&DEFAULT_BYTES);
let serialized = serde_json::to_string(&orig_pub_key).unwrap();
let deserialized = serde_json::from_str(&serialized).unwrap();
assert_eq!(orig_pub_key, deserialized);
}
}