@@ -85,6 +85,8 @@ pub enum Error {
85
85
ExcessiveScriptSize ,
86
86
/// Script is not a p2pkh, p2sh or witness program.
87
87
UnrecognizedScript ,
88
+ /// Address type is either invalid or not supported in rust-bitcoin.
89
+ UnknownAddressType ( String ) ,
88
90
}
89
91
90
92
impl fmt:: Display for Error {
@@ -101,7 +103,8 @@ impl fmt::Display for Error {
101
103
Error :: InvalidSegwitV0ProgramLength ( l) => write ! ( f, "a v0 witness program must be either of length 20 or 32 bytes: length={}" , l) ,
102
104
Error :: UncompressedPubkey => write ! ( f, "an uncompressed pubkey was used where it is not allowed" ) ,
103
105
Error :: ExcessiveScriptSize => write ! ( f, "script size exceed 520 bytes" ) ,
104
- Error :: UnrecognizedScript => write ! ( f, "script is not a p2pkh, p2sh or witness program" )
106
+ Error :: UnrecognizedScript => write ! ( f, "script is not a p2pkh, p2sh or witness program" ) ,
107
+ Error :: UnknownAddressType ( ref s) => write ! ( f, "unknown address type: '{}' is either invalid or not supported in rust-bitcoin" , s) ,
105
108
}
106
109
}
107
110
}
@@ -124,7 +127,8 @@ impl std::error::Error for Error {
124
127
| InvalidSegwitV0ProgramLength ( _)
125
128
| UncompressedPubkey
126
129
| ExcessiveScriptSize
127
- | UnrecognizedScript => None ,
130
+ | UnrecognizedScript
131
+ | UnknownAddressType ( _) => None ,
128
132
}
129
133
}
130
134
}
@@ -172,15 +176,15 @@ impl fmt::Display for AddressType {
172
176
}
173
177
174
178
impl FromStr for AddressType {
175
- type Err = ( ) ;
179
+ type Err = Error ;
176
180
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
177
181
match s {
178
182
"p2pkh" => Ok ( AddressType :: P2pkh ) ,
179
183
"p2sh" => Ok ( AddressType :: P2sh ) ,
180
184
"p2wpkh" => Ok ( AddressType :: P2wpkh ) ,
181
185
"p2wsh" => Ok ( AddressType :: P2wsh ) ,
182
186
"p2tr" => Ok ( AddressType :: P2tr ) ,
183
- _ => Err ( ( ) ) ,
187
+ _ => Err ( Error :: UnknownAddressType ( s . to_owned ( ) ) ) ,
184
188
}
185
189
}
186
190
}
@@ -1510,4 +1514,17 @@ mod tests {
1510
1514
assert_eq ! ( Address :: from_script( & bad_p2wsh, Network :: Bitcoin ) , expected) ;
1511
1515
assert_eq ! ( Address :: from_script( & invalid_segwitv0_script, Network :: Bitcoin ) , Err ( Error :: InvalidSegwitV0ProgramLength ( 17 ) ) ) ;
1512
1516
}
1517
+
1518
+ #[ test]
1519
+ fn valid_address_parses_correctly ( ) {
1520
+ let addr = AddressType :: from_str ( "p2tr" ) . expect ( "false negative while parsing address" ) ;
1521
+ assert_eq ! ( addr, AddressType :: P2tr ) ;
1522
+ }
1523
+
1524
+ #[ test]
1525
+ fn invalid_address_parses_error ( ) {
1526
+ let got = AddressType :: from_str ( "invalid" ) ;
1527
+ let want = Err ( Error :: UnknownAddressType ( "invalid" . to_string ( ) ) ) ;
1528
+ assert_eq ! ( got, want) ;
1529
+ }
1513
1530
}
0 commit comments