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
fn has_character_count(s: &str, char_count: u64) -> bool {
s.chars().count() as u64 == char_count
}
fn is_p2pkh(address: &String) -> bool {
let p2pkh_leading_symbols = vec![
"1", "m", "n", ];
let has_p2pkh_leading_symbol = p2pkh_leading_symbols
.iter()
.any(|leading_symbol| address.starts_with(leading_symbol));
let has_correct_char_count =
has_character_count(address, 33) || has_character_count(address, 34);
has_correct_char_count && has_p2pkh_leading_symbol
}
pub fn is_p2sh(address: &String) -> bool {
let p2sh_leading_symbols = vec![
"2", "3", ];
let has_p2sh_leading_symbol = p2sh_leading_symbols
.iter()
.any(|leading_symbol| address.starts_with(leading_symbol));
let has_correct_char_count =
has_character_count(address, 34)
|| has_character_count(address, 35);
has_correct_char_count && has_p2sh_leading_symbol
}
fn could_be_p2sh_p2wpkh(address: &String) -> bool {
is_p2sh(address)
}
fn could_be_p2sh_p2wsh(address: &String) -> bool {
is_p2sh(address)
}
fn is_p2wpkh(address: &String) -> bool {
let p2wpkh_leading_symbols = vec![
"bc1q", "tb1q", ];
let has_p2wpkh_leading_symbol = p2wpkh_leading_symbols
.iter()
.any(|leading_symbol| address.starts_with(leading_symbol));
has_character_count(address, 42) && has_p2wpkh_leading_symbol
}
fn is_p2wsh(address: &String) -> bool {
let p2wsh_leading_symbols = vec![
"bc1q", "tb1q", ];
let has_p2wsh_leading_symbol = p2wsh_leading_symbols
.iter()
.any(|leading_symbol| address.starts_with(leading_symbol));
has_character_count(address, 62) && has_p2wsh_leading_symbol
}
fn is_p2tr(address: &String) -> bool {
let p2tr_leading_symbols = vec![
"bc1p", "tb1p", ];
let has_p2tr_leading_symbol = p2tr_leading_symbols
.iter()
.any(|leading_symbol| address.starts_with(leading_symbol));
has_character_count(address, 62) && has_p2tr_leading_symbol
}
pub fn is_legacy(address: &String) -> bool {
is_p2pkh(address)
}
pub fn is_nested_segwit(address: &String) -> bool {
is_p2sh(address)
}
pub fn is_wrapped_segwit(address: &String) -> bool {
is_nested_segwit(address)
}
pub fn is_segwit_native(address: &String) -> bool {
is_p2wpkh(address) || is_p2wsh(address)
}
pub fn is_segwit_v0(address: &String) -> bool {
is_segwit_native(address) || is_p2sh(address)
}
pub fn is_segwit_v1(address: &String) -> bool {
is_taproot(address)
}
pub fn is_taproot(address: &String) -> bool {
is_p2tr(address)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn is_legacy_works() {
let legacy_address = "1J9uwBYepTm5737RtzkSEePTevGgDGLP5S".to_string();
let nested_segwit_address = "37u4L57bLqZ8NL9bs1GNX2x52KxviDfvPp".to_string();
let native_segwit_address = "bc1qfvmj8jse4r7203mrchfyt24sjcpna3s2y35ylp".to_string();
let taproot_address =
"bc1p8denc9m4sqe9hluasrvxkkdqgkydrk5ctxre5nkk4qwdvefn0sdsc6eqxe".to_string();
assert_eq!(is_legacy(&legacy_address), true);
assert_eq!(is_legacy(&nested_segwit_address), false);
assert_eq!(is_legacy(&native_segwit_address), false);
assert_eq!(is_legacy(&taproot_address), false);
assert_eq!(is_nested_segwit(&nested_segwit_address), true);
assert_eq!(is_nested_segwit(&legacy_address), false);
assert_eq!(is_nested_segwit(&native_segwit_address), false);
assert_eq!(is_nested_segwit(&taproot_address), false);
assert_eq!(is_segwit_native(&native_segwit_address), true);
assert_eq!(is_segwit_native(&legacy_address), false);
assert_eq!(is_segwit_native(&nested_segwit_address), false);
assert_eq!(is_segwit_native(&taproot_address), false);
assert_eq!(is_segwit_v0(&nested_segwit_address), true);
assert_eq!(is_segwit_v0(&native_segwit_address), true);
assert_eq!(is_segwit_v0(&taproot_address), false);
assert_eq!(is_segwit_v1(&nested_segwit_address), false);
assert_eq!(is_segwit_v1(&native_segwit_address), false);
assert_eq!(is_segwit_v1(&taproot_address), true);
assert_eq!(is_taproot(&taproot_address), true);
assert_eq!(is_taproot(&legacy_address), false);
assert_eq!(is_taproot(&nested_segwit_address), false);
assert_eq!(is_taproot(&native_segwit_address), false);
}
}