@@ -24,12 +24,13 @@ extern crate serde_json;
24
24
25
25
use std:: collections:: HashMap ;
26
26
27
+
27
28
use bitcoin:: address:: NetworkUnchecked ;
28
29
use bitcoin:: block:: Version ;
29
30
use bitcoin:: consensus:: encode;
30
31
use bitcoin:: hashes:: hex:: FromHex ;
31
32
use 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 } ;
33
34
use serde:: de:: Error as SerdeError ;
34
35
use serde:: { Deserialize , Serialize } ;
35
36
use std:: fmt;
@@ -515,7 +516,8 @@ pub struct GetMiningInfoResult {
515
516
pub network_hash_ps : f64 ,
516
517
#[ serde( rename = "pooledtx" ) ]
517
518
pub pooled_tx : usize ,
518
- pub chain : String ,
519
+ #[ serde( deserialize_with = "deserialize_bip70_network" ) ]
520
+ pub chain : Network ,
519
521
pub warnings : String ,
520
522
}
521
523
@@ -1006,8 +1008,9 @@ pub struct GetAddressInfoResult {
1006
1008
/// Models the result of "getblockchaininfo"
1007
1009
#[ derive( Clone , Debug , Deserialize , Serialize ) ]
1008
1010
pub 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 ,
1011
1014
/// The current number of blocks processed in the server
1012
1015
pub blocks : u64 ,
1013
1016
/// The current number of headers we have validated
@@ -2169,6 +2172,29 @@ where
2169
2172
Ok ( Some ( res) )
2170
2173
}
2171
2174
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
+
2172
2198
#[ cfg( test) ]
2173
2199
mod tests {
2174
2200
use super :: * ;
0 commit comments