waves_rust/model/account/
address.rs1use crate::error::{Error, Result};
2use crate::model::account::PublicKey;
3use crate::model::ByteString;
4use crate::util::{Base58, Crypto, JsonDeserializer};
5use serde_json::Value;
6use std::fmt;
7use std::hash::Hash;
8
9#[derive(Eq, PartialEq, Clone, Hash)]
10pub struct Address {
11 bytes: Vec<u8>,
12}
13
14impl fmt::Debug for Address {
15 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16 write!(f, "Address {{ {} }}", self.encoded())
17 }
18}
19
20impl std::str::FromStr for Address {
21 type Err = Error;
22
23 fn from_str(base58string: &str) -> Result<Address> {
24 Ok(Address {
25 bytes: Base58::decode(base58string)?,
26 })
27 }
28}
29
30impl Address {
31 pub fn from_public_key(chain_id: u8, public_key: &PublicKey) -> Result<Address> {
32 Ok(Address {
33 bytes: Crypto::get_address(
34 &chain_id,
35 &Crypto::get_public_key_hash(&public_key.bytes())?,
36 )?,
37 })
38 }
39
40 pub fn from_string(address: &str) -> Result<Address> {
41 Ok(Address {
42 bytes: Base58::decode(address)?,
43 })
44 }
45
46 pub fn chain_id(&self) -> u8 {
47 self.bytes[1]
48 }
49
50 pub fn public_key_hash(&self) -> Vec<u8> {
51 self.bytes[2..22].to_vec()
52 }
53}
54
55impl ByteString for Address {
56 fn bytes(&self) -> Vec<u8> {
57 self.bytes.clone()
58 }
59
60 fn encoded(&self) -> String {
61 Base58::encode(&self.bytes, false)
62 }
63
64 fn encoded_with_prefix(&self) -> String {
65 Base58::encode(&self.bytes, true)
66 }
67}
68
69impl TryFrom<&Value> for Address {
70 type Error = Error;
71
72 fn try_from(value: &Value) -> Result<Self> {
73 let string = JsonDeserializer::safe_to_string(value)?;
74 Address::from_string(&string)
75 }
76}
77
78#[cfg(test)]
79mod tests {
80 use crate::error::Result;
81 use crate::model::account::{Address, PrivateKey};
82 use crate::model::{ByteString, ChainId};
83 use serde_json::Value;
84 use std::borrow::Borrow;
85 use std::str::FromStr;
86
87 #[test]
88 fn test_address_from_public_key() {
89 let seed_phrase = "blame vacant regret company chase trip grant funny brisk innocent";
90 let expected_address = "3Ms87NGAAaPWZux233TB9A3TXps4LDkyJWN";
91
92 let private_key =
93 PrivateKey::from_seed(seed_phrase, 0).expect("failed to get private key from seed");
94 let public_key = private_key.public_key();
95 let address = public_key
96 .address(ChainId::TESTNET.byte())
97 .expect("failed to get address from public key")
98 .encoded();
99
100 assert_eq!(address, expected_address)
101 }
102
103 #[test]
104 fn test_address_from_string() {
105 let expected_address = "3MtQQX9NwYH5URGGcS2e6ptEgV7wTFesaRW";
106 let address =
107 Address::from_string(expected_address).expect("failed to get address from string");
108 assert_eq!(expected_address, address.encoded())
109 }
110
111 #[test]
112 fn test_address_std_from_str() {
113 let expected_address = "3MtQQX9NwYH5URGGcS2e6ptEgV7wTFesaRW";
114 let address =
115 Address::from_str(expected_address).expect("failed to get address from string");
116 assert_eq!(expected_address, address.encoded())
117 }
118
119 #[test]
120 fn test_address_from_json() -> Result<()> {
121 let expected_address = "3MtQQX9NwYH5URGGcS2e6ptEgV7wTFesaRW";
122 let address: Address = Value::String("3MtQQX9NwYH5URGGcS2e6ptEgV7wTFesaRW".to_owned())
123 .borrow()
124 .try_into()?;
125 assert_eq!(expected_address, address.encoded());
126 Ok(())
127 }
128
129 #[test]
130 fn test_byte_string_for_address() -> Result<()> {
131 let address = Address::from_string("3MtQQX9NwYH5URGGcS2e6ptEgV7wTFesaRW")?;
132 let expected_bytes: Vec<u8> = vec![
133 1, 84, 49, 59, 204, 61, 157, 141, 148, 218, 122, 51, 43, 12, 171, 81, 190, 13, 80, 46,
134 88, 199, 218, 79, 208, 145,
135 ];
136 let expected_encoded = "3MtQQX9NwYH5URGGcS2e6ptEgV7wTFesaRW";
137 let expected_encoded_with_prefix = "base58:3MtQQX9NwYH5URGGcS2e6ptEgV7wTFesaRW";
138 assert_eq!(expected_bytes, address.bytes());
139 assert_eq!(expected_encoded, address.encoded());
140 assert_eq!(expected_encoded_with_prefix, address.encoded_with_prefix());
141 Ok(())
142 }
143}