@@ -146,3 +146,94 @@ impl OnionMessageContents for DNSResolverMessage {
146
146
}
147
147
}
148
148
}
149
+
150
+ /// A struct containing the two parts of a BIP 353 Human Readable Name - the user and domain parts.
151
+ ///
152
+ /// The `user` and `domain` parts, together, cannot exceed 232 bytes in length, and both must be
153
+ /// non-empty.
154
+ ///
155
+ /// To protect against [Homograph Attacks], both parts of a Human Readable Name must be plain
156
+ /// ASCII.
157
+ ///
158
+ /// [Homograph Attacks]: https://en.wikipedia.org/wiki/IDN_homograph_attack
159
+ #[ derive( Clone , Debug , Hash , PartialEq , Eq ) ]
160
+ pub struct HumanReadableName {
161
+ // TODO Remove the heap allocations given the whole data can't be more than 256 bytes.
162
+ user : String ,
163
+ domain : String ,
164
+ }
165
+
166
+ impl HumanReadableName {
167
+ /// Constructs a new [`HumanReadableName`] from the `user` and `domain` parts. See the
168
+ /// struct-level documentation for more on the requirements on each.
169
+ pub fn new ( user : String , domain : String ) -> Result < HumanReadableName , ( ) > {
170
+ const REQUIRED_EXTRA_LEN : usize = ".user._bitcoin-payment." . len ( ) + 1 ;
171
+ if user. len ( ) + domain. len ( ) + REQUIRED_EXTRA_LEN > 255 {
172
+ return Err ( ( ) ) ;
173
+ }
174
+ if user. is_empty ( ) || domain. is_empty ( ) {
175
+ return Err ( ( ) ) ;
176
+ }
177
+ if !user. is_ascii ( ) || !domain. is_ascii ( ) {
178
+ return Err ( ( ) ) ;
179
+ }
180
+ Ok ( HumanReadableName { user, domain } )
181
+ }
182
+
183
+ /// Constructs a new [`HumanReadableName`] from the standard encoding - `user`@`domain`.
184
+ ///
185
+ /// If `user` includes the standard BIP 353 ₿ prefix it is automatically removed as required by
186
+ /// BIP 353.
187
+ pub fn from_encoded ( encoded : & str ) -> Result < HumanReadableName , ( ) > {
188
+ if let Some ( ( user, domain) ) = encoded. strip_prefix ( '₿' ) . unwrap_or ( encoded) . split_once ( "@" )
189
+ {
190
+ Self :: new ( user. to_string ( ) , domain. to_string ( ) )
191
+ } else {
192
+ Err ( ( ) )
193
+ }
194
+ }
195
+
196
+ /// Gets the `user` part of this Human Readable Name
197
+ pub fn user ( & self ) -> & str {
198
+ & self . user
199
+ }
200
+
201
+ /// Gets the `domain` part of this Human Readable Name
202
+ pub fn domain ( & self ) -> & str {
203
+ & self . domain
204
+ }
205
+ }
206
+
207
+ // Serialized per the requirements for inclusion in a BOLT 12 `invoice_request`
208
+ impl Writeable for HumanReadableName {
209
+ fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , io:: Error > {
210
+ ( self . user . len ( ) as u8 ) . write ( writer) ?;
211
+ writer. write_all ( & self . user . as_bytes ( ) ) ?;
212
+ ( self . domain . len ( ) as u8 ) . write ( writer) ?;
213
+ writer. write_all ( & self . domain . as_bytes ( ) )
214
+ }
215
+ }
216
+
217
+ impl Readable for HumanReadableName {
218
+ fn read < R : io:: Read > ( reader : & mut R ) -> Result < Self , DecodeError > {
219
+ let mut read_bytes = [ 0 ; 255 ] ;
220
+
221
+ let user_len: u8 = Readable :: read ( reader) ?;
222
+ reader. read_exact ( & mut read_bytes[ ..user_len as usize ] ) ?;
223
+ let user_bytes: Vec < u8 > = read_bytes[ ..user_len as usize ] . into ( ) ;
224
+ let user = match String :: from_utf8 ( user_bytes) {
225
+ Ok ( user) => user,
226
+ Err ( _) => return Err ( DecodeError :: InvalidValue ) ,
227
+ } ;
228
+
229
+ let domain_len: u8 = Readable :: read ( reader) ?;
230
+ reader. read_exact ( & mut read_bytes[ ..domain_len as usize ] ) ?;
231
+ let domain_bytes: Vec < u8 > = read_bytes[ ..domain_len as usize ] . into ( ) ;
232
+ let domain = match String :: from_utf8 ( domain_bytes) {
233
+ Ok ( domain) => domain,
234
+ Err ( _) => return Err ( DecodeError :: InvalidValue ) ,
235
+ } ;
236
+
237
+ HumanReadableName :: new ( user, domain) . map_err ( |( ) | DecodeError :: InvalidValue )
238
+ }
239
+ }
0 commit comments