Skip to content

Commit 768a185

Browse files
Added Alias mod
1 parent 549f59f commit 768a185

3 files changed

Lines changed: 96 additions & 0 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ keywords = ["waves", "blockchain"]
1313
[dependencies]
1414
base58 = "0.2.0"
1515
rand = "0.8.5"
16+
regex = "1.6.0"
1617
tiny-bip39 = "1.0.0"
1718

1819
blake2 = "0.9.2"

src/util.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
mod alias;
12
mod amount;
23

34
use curve25519_dalek::montgomery::MontgomeryPoint;
45
use ed25519_dalek::*;
56

7+
pub use alias::*;
68
pub use amount::*;
79

810
/// Signature verify function

src/util/alias.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
use regex::Regex;
2+
use std::fmt;
3+
4+
/// Regular expression for the definition of alias
5+
const REGEXP: &str = "^[-.0-9@_a-z]{4,30}$";
6+
/// Regular expression for the definition of alias with prefix
7+
const REGEXP_WITH_PREFIX: &str = "^alias:[A-Z]{1}:[-.0-9@_a-z]{4,30}$";
8+
9+
/// List of errors in processing [`Alias`]
10+
#[derive(Debug, PartialEq, Eq)]
11+
pub enum AliasError {
12+
InvalidAlias,
13+
RegexError,
14+
}
15+
16+
/// The [`Alias`] type on working with aliases in the Waves blockchain and presenting it in a format with or without the network prefix
17+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
18+
pub struct Alias(String);
19+
20+
impl Alias {
21+
/// Create an [`Alias`] from the string
22+
pub fn new(alias: &str) -> Result<Alias, AliasError> {
23+
if Self::is_valid(REGEXP, alias)? {
24+
Ok(Alias(format!("{}", alias)))
25+
} else if Self::is_valid(REGEXP_WITH_PREFIX, alias)? {
26+
let value = Self::replace_prefix(alias)?;
27+
Ok(Alias(value))
28+
} else {
29+
Err(AliasError::InvalidAlias)
30+
}
31+
}
32+
33+
/// Representing [`Alias`] as a string with a prefix
34+
pub fn to_string_with_prefix(&self, chain_id: u8) -> String {
35+
format!("alias:{}:{}", chain_id as char, self.0)
36+
}
37+
38+
/// Validation of alias using regular expressions
39+
fn is_valid(regexp: &str, alias: &str) -> Result<bool, AliasError> {
40+
match Regex::new(regexp) {
41+
Ok(r) => Ok(r.is_match(alias)),
42+
Err(_) => Err(AliasError::RegexError),
43+
}
44+
}
45+
46+
/// Removing the prefix with regular expressions
47+
fn replace_prefix(alias: &str) -> Result<String, AliasError> {
48+
match Regex::new("^alias:[A-Z]{1}:") {
49+
Ok(r) => Ok(r.replace(alias, "").to_string()),
50+
Err(_) => Err(AliasError::RegexError),
51+
}
52+
}
53+
}
54+
55+
impl fmt::Display for Alias {
56+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57+
write!(f, "{}", self.0)
58+
}
59+
}
60+
61+
#[cfg(test)]
62+
mod tests {
63+
use super::*;
64+
use crate::account::TESTNET;
65+
66+
#[test]
67+
fn test_alias() {
68+
let result = Alias::new("test");
69+
assert!(result.is_ok());
70+
71+
let alias = result.unwrap();
72+
73+
assert_eq!(alias.to_string(), "test");
74+
assert_eq!(alias.to_string_with_prefix(TESTNET), "alias:T:test");
75+
76+
let result = Alias::new("a");
77+
assert_eq!(result, Err(AliasError::InvalidAlias));
78+
79+
let result = Alias::new("3MzGEv9wnaqrYFYujAXSH5RQfHaVKNQvx3D");
80+
assert_eq!(result, Err(AliasError::InvalidAlias));
81+
}
82+
83+
#[test]
84+
fn test_alias_with_prefix() {
85+
let result = Alias::new("alias:T:test");
86+
assert!(result.is_ok());
87+
88+
let alias = result.unwrap();
89+
90+
assert_eq!(alias.to_string(), "test");
91+
assert_eq!(alias.to_string_with_prefix(TESTNET), "alias:T:test");
92+
}
93+
}

0 commit comments

Comments
 (0)