@@ -162,25 +162,8 @@ impl TryInto<(BlockHash, Option<u32>)> for JsonResponse {
162
162
impl TryInto < Txid > for JsonResponse {
163
163
type Error = std:: io:: Error ;
164
164
fn try_into ( self ) -> std:: io:: Result < Txid > {
165
- match self . 0 . as_str ( ) {
166
- None => Err ( std:: io:: Error :: new (
167
- std:: io:: ErrorKind :: InvalidData ,
168
- "expected JSON string" ,
169
- ) ) ,
170
- Some ( hex_data) => match Vec :: < u8 > :: from_hex ( hex_data) {
171
- Err ( _) => Err ( std:: io:: Error :: new (
172
- std:: io:: ErrorKind :: InvalidData ,
173
- "invalid hex data" ,
174
- ) ) ,
175
- Ok ( txid_data) => match encode:: deserialize ( & txid_data) {
176
- Err ( _) => Err ( std:: io:: Error :: new (
177
- std:: io:: ErrorKind :: InvalidData ,
178
- "invalid txid" ,
179
- ) ) ,
180
- Ok ( txid) => Ok ( txid) ,
181
- } ,
182
- } ,
183
- }
165
+ let hex_data = self . 0 . as_str ( ) . ok_or ( Self :: Error :: new ( std:: io:: ErrorKind :: InvalidData , "expected JSON string" ) ) ?;
166
+ Txid :: from_str ( hex_data) . map_err ( |err|Self :: Error :: new ( std:: io:: ErrorKind :: InvalidData , err. to_string ( ) ) )
184
167
}
185
168
}
186
169
@@ -622,7 +605,7 @@ pub(crate) mod tests {
622
605
match TryInto :: < Txid > :: try_into ( response) {
623
606
Err ( e) => {
624
607
assert_eq ! ( e. kind( ) , std:: io:: ErrorKind :: InvalidData ) ;
625
- assert_eq ! ( e. get_ref( ) . unwrap( ) . to_string( ) , "invalid hex data " ) ;
608
+ assert_eq ! ( e. get_ref( ) . unwrap( ) . to_string( ) , "bad hex string length 6 (expected 64) " ) ;
626
609
}
627
610
Ok ( _) => panic ! ( "Expected error" ) ,
628
611
}
@@ -634,7 +617,7 @@ pub(crate) mod tests {
634
617
match TryInto :: < Txid > :: try_into ( response) {
635
618
Err ( e) => {
636
619
assert_eq ! ( e. kind( ) , std:: io:: ErrorKind :: InvalidData ) ;
637
- assert_eq ! ( e. get_ref( ) . unwrap( ) . to_string( ) , "invalid txid " ) ;
620
+ assert_eq ! ( e. get_ref( ) . unwrap( ) . to_string( ) , "bad hex string length 4 (expected 64) " ) ;
638
621
}
639
622
Ok ( _) => panic ! ( "Expected error" ) ,
640
623
}
@@ -650,6 +633,20 @@ pub(crate) mod tests {
650
633
}
651
634
}
652
635
636
+ #[ test]
637
+ fn into_txid_from_bitcoind_rpc_json_response ( ) {
638
+ let mut rpc_response = serde_json:: json!(
639
+ { "error" : "" , "id" : "770" , "result" : "7934f775149929a8b742487129a7c3a535dfb612f0b726cc67bc10bc2628f906" }
640
+
641
+ ) ;
642
+ let r: std:: io:: Result < Txid > = JsonResponse ( rpc_response. get_mut ( "result" ) . unwrap ( ) . take ( ) )
643
+ . try_into ( ) ;
644
+ assert_eq ! (
645
+ r. unwrap( ) . to_string( ) ,
646
+ "7934f775149929a8b742487129a7c3a535dfb612f0b726cc67bc10bc2628f906"
647
+ ) ;
648
+ }
649
+
653
650
// TryInto<Transaction> can be used in two ways, first with plain hex response where data is
654
651
// the hex encoded transaction (e.g. as a result of getrawtransaction) or as a JSON object
655
652
// where the hex encoded transaction can be found in the hex field of the object (if present)
0 commit comments