@@ -24,12 +24,13 @@ extern crate serde_json;
2424
2525use std:: collections:: HashMap ;
2626
27+
2728use bitcoin:: address:: NetworkUnchecked ;
2829use bitcoin:: block:: Version ;
2930use bitcoin:: consensus:: encode;
3031use bitcoin:: hashes:: hex:: FromHex ;
3132use bitcoin:: hashes:: sha256;
32- use bitcoin:: { Address , Amount , PrivateKey , PublicKey , SignedAmount , Transaction , ScriptBuf , Script , bip158, bip32} ;
33+ use bitcoin:: { Address , Amount , PrivateKey , PublicKey , SignedAmount , Transaction , ScriptBuf , Script , bip158, bip32, Network } ;
3334use serde:: de:: Error as SerdeError ;
3435use serde:: { Deserialize , Serialize } ;
3536use std:: fmt;
@@ -515,7 +516,8 @@ pub struct GetMiningInfoResult {
515516 pub network_hash_ps : f64 ,
516517 #[ serde( rename = "pooledtx" ) ]
517518 pub pooled_tx : usize ,
518- pub chain : String ,
519+ #[ serde( deserialize_with = "deserialize_bip70_network" ) ]
520+ pub chain : Network ,
519521 pub warnings : String ,
520522}
521523
@@ -1006,8 +1008,9 @@ pub struct GetAddressInfoResult {
10061008/// Models the result of "getblockchaininfo"
10071009#[ derive( Clone , Debug , Deserialize , Serialize ) ]
10081010pub struct GetBlockchainInfoResult {
1009- /// Current network name as defined in BIP70 (main, test, regtest)
1010- pub chain : String ,
1011+ /// Current network name as defined in BIP70 (main, test, signet, regtest)
1012+ #[ serde( deserialize_with = "deserialize_bip70_network" ) ]
1013+ pub chain : Network ,
10111014 /// The current number of blocks processed in the server
10121015 pub blocks : u64 ,
10131016 /// The current number of headers we have validated
@@ -2169,6 +2172,29 @@ where
21692172 Ok ( Some ( res) )
21702173}
21712174
2175+ /// deserialize_bip70_network deserializes a Bitcoin Core network according to BIP70
2176+ /// The accepted input variants are: {"main", "test", "signet", "regtest"}
2177+ fn deserialize_bip70_network < ' de , D > ( deserializer : D ) -> Result < Network , D :: Error >
2178+ where
2179+ D : serde:: Deserializer < ' de > ,
2180+ {
2181+ struct NetworkVisitor ;
2182+ impl < ' de > serde:: de:: Visitor < ' de > for NetworkVisitor {
2183+ type Value = Network ;
2184+
2185+ fn visit_str < E : serde:: de:: Error > ( self , s : & str ) -> Result < Self :: Value , E > {
2186+ Network :: from_core_arg ( s)
2187+ . map_err ( |_| E :: invalid_value ( serde:: de:: Unexpected :: Str ( s) , & "bitcoin network encoded as a string" ) )
2188+ }
2189+
2190+ fn expecting ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
2191+ write ! ( formatter, "bitcoin network encoded as a string" )
2192+ }
2193+ }
2194+
2195+ deserializer. deserialize_str ( NetworkVisitor )
2196+ }
2197+
21722198#[ cfg( test) ]
21732199mod tests {
21742200 use super :: * ;
0 commit comments