-
Notifications
You must be signed in to change notification settings - Fork 115
/
base.rs
266 lines (223 loc) · 6.95 KB
/
base.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//! Base functionality.
use crate::{proto, Error, ErrorReport, Result};
use eyre::WrapErr;
use serde::{de, de::Error as _, ser, Deserialize, Serialize};
use std::{fmt, str::FromStr};
use subtle_encoding::bech32;
/// Maximum allowed length (in bytes) for an address.
pub const MAX_ADDRESS_LENGTH: usize = 255;
/// Account identifiers
#[derive(Clone, Eq, PartialEq, PartialOrd, Ord)]
pub struct AccountId {
/// Account ID encoded as Bech32
bech32: String,
/// Length of the human-readable prefix of the address
hrp_length: usize,
}
impl AccountId {
/// Create an [`AccountId`] with the given human-readable prefix and
/// public key hash.
pub fn new(prefix: &str, bytes: &[u8]) -> Result<Self> {
let id = bech32::encode(prefix, &bytes);
if !prefix.chars().all(|c| matches!(c, 'a'..='z' | '0'..='9')) {
return Err(Error::AccountId { id })
.wrap_err("expected prefix to be lowercase alphanumeric characters only");
}
if matches!(bytes.len(), 1..=MAX_ADDRESS_LENGTH) {
Ok(Self {
bech32: id,
hrp_length: prefix.len(),
})
} else {
Err(Error::AccountId { id }).wrap_err_with(|| {
format!(
"account ID should be at most {} bytes long, but was {} bytes long",
MAX_ADDRESS_LENGTH,
bytes.len()
)
})
}
}
/// Get the human-readable prefix of this account.
pub fn prefix(&self) -> &str {
&self.bech32[..self.hrp_length]
}
/// Decode an account ID from Bech32 to an inner byte value.
pub fn to_bytes(&self) -> Vec<u8> {
bech32::decode(&self.bech32)
.expect("malformed Bech32 AccountId")
.1
}
}
impl AsRef<str> for AccountId {
fn as_ref(&self) -> &str {
&self.bech32
}
}
impl fmt::Debug for AccountId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("AccountId").field(&self.as_ref()).finish()
}
}
impl fmt::Display for AccountId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_ref())
}
}
impl FromStr for AccountId {
type Err = ErrorReport;
fn from_str(s: &str) -> Result<Self> {
let (hrp, bytes) = bech32::decode(s).wrap_err(format!("invalid bech32: '{}'", s))?;
Self::new(&hrp, &bytes)
}
}
impl TryFrom<AccountId> for tendermint::account::Id {
type Error = ErrorReport;
fn try_from(id: AccountId) -> Result<tendermint::account::Id> {
tendermint::account::Id::try_from(&id)
}
}
// TODO(tarcieri): non-fixed-width account ID type
impl TryFrom<&AccountId> for tendermint::account::Id {
type Error = ErrorReport;
fn try_from(id: &AccountId) -> Result<tendermint::account::Id> {
let bytes = id.to_bytes();
let len = bytes.len();
match bytes.try_into() {
Ok(bytes) => Ok(tendermint::account::Id::new(bytes)),
_ => Err(Error::AccountId {
id: id.bech32.clone(),
})
.wrap_err_with(|| format!("invalid length for account ID: {}", len)),
}
}
}
impl<'de> Deserialize<'de> for AccountId {
fn deserialize<D: de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
String::deserialize(deserializer)?
.parse()
.map_err(D::Error::custom)
}
}
impl Serialize for AccountId {
fn serialize<S: ser::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.bech32.serialize(serializer)
}
}
/// Coin defines a token with a denomination and an amount.
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct Coin {
/// Denomination
pub denom: Denom,
/// Amount
pub amount: u128,
}
impl Coin {
/// Constructor
pub fn new(amount: u128, denom: &str) -> Result<Self> {
Ok(Coin {
amount,
denom: denom.parse()?,
})
}
}
impl TryFrom<proto::cosmos::base::v1beta1::Coin> for Coin {
type Error = ErrorReport;
fn try_from(proto: proto::cosmos::base::v1beta1::Coin) -> Result<Coin> {
Coin::try_from(&proto)
}
}
impl TryFrom<&proto::cosmos::base::v1beta1::Coin> for Coin {
type Error = ErrorReport;
fn try_from(proto: &proto::cosmos::base::v1beta1::Coin) -> Result<Coin> {
Ok(Coin {
denom: proto.denom.parse()?,
amount: proto.amount.parse()?,
})
}
}
impl From<Coin> for proto::cosmos::base::v1beta1::Coin {
fn from(coin: Coin) -> proto::cosmos::base::v1beta1::Coin {
proto::cosmos::base::v1beta1::Coin::from(&coin)
}
}
impl From<&Coin> for proto::cosmos::base::v1beta1::Coin {
fn from(coin: &Coin) -> proto::cosmos::base::v1beta1::Coin {
proto::cosmos::base::v1beta1::Coin {
denom: coin.denom.to_string(),
amount: coin.amount.to_string(),
}
}
}
impl fmt::Display for Coin {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// See: https://github.com/cosmos/cosmos-sdk/blob/v0.42.4/types/coin.go#L643-L645
write!(f, "{}{}", self.amount, self.denom)
}
}
/// Denomination.
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct Denom(String);
impl AsRef<str> for Denom {
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}
impl fmt::Display for Denom {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_ref())
}
}
impl FromStr for Denom {
type Err = ErrorReport;
fn from_str(s: &str) -> Result<Self> {
// TODO(tarcieri): ensure this is the proper validation for a denom name
if s.chars()
.all(|c| matches!(c, 'A'..='Z' | 'a'..='z' | '0'..='9' | '/'))
{
Ok(Denom(s.to_owned()))
} else {
Err(Error::Denom { name: s.to_owned() }.into())
}
}
}
impl<'de> Deserialize<'de> for Denom {
fn deserialize<D: de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
String::deserialize(deserializer)?
.parse()
.map_err(D::Error::custom)
}
}
impl Serialize for Denom {
fn serialize<S: ser::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.0.serialize(serializer)
}
}
#[cfg(test)]
mod tests {
use super::{AccountId, Coin, Denom};
#[test]
fn account_id() {
"juno1cma4czt2jnydvrvz3lrc9jvcmhpjxtds95s3c6"
.parse::<AccountId>()
.unwrap();
}
#[test]
fn account_id_with_digit() {
"okp41urdh3smlstyafjtyg0d606egllhwp8kvnw0d2f"
.parse::<AccountId>()
.unwrap();
}
#[test]
fn denom_from_str() {
assert!(
"ibc/9F53D255F5320A4BE124FF20C29D46406E126CE8A09B00CA8D3CFF7905119728"
.parse::<Denom>()
.is_ok()
);
}
#[test]
fn coin() {
Coin::new(1000, "uatom").unwrap();
}
}