4343 import urlparse
4444
4545import bitcoin
46- from bitcoin .core import COIN , lx , b2lx , CBlock , CBlockHeader , CTransaction , COutPoint , CTxOut
46+ from bitcoin .core import COIN , x , lx , b2lx , CBlock , CBlockHeader , CTransaction , COutPoint , CTxOut
4747from bitcoin .core .script import CScript
4848from bitcoin .wallet import CBitcoinAddress , CBitcoinSecret
4949
@@ -318,9 +318,13 @@ def getbestblockhash(self):
318318 """Return hash of best (tip) block in longest block chain."""
319319 return lx (self ._call ('getbestblockhash' ))
320320
321- def getblockheader (self , block_hash ):
321+ def getblockheader (self , block_hash , verbose = False ):
322322 """Get block header <block_hash>
323323
324+ verbose - If true a dict is returned with the values returned by
325+ getblockheader that are not in the block header itself
326+ (height, nextblockhash, etc.)
327+
324328 Raises IndexError if block_hash is not valid.
325329 """
326330 try :
@@ -329,11 +333,22 @@ def getblockheader(self, block_hash):
329333 raise TypeError ('%s.getblockheader(): block_hash must be bytes; got %r instance' %
330334 (self .__class__ .__name__ , block_hash .__class__ ))
331335 try :
332- r = self ._call ('getblockheader' , block_hash , False )
336+ r = self ._call ('getblockheader' , block_hash , verbose )
333337 except JSONRPCError as ex :
334338 raise IndexError ('%s.getblockheader(): %s (%d)' %
335339 (self .__class__ .__name__ , ex .error ['message' ], ex .error ['code' ]))
336- return CBlockHeader .deserialize (unhexlify (r ))
340+
341+ if verbose :
342+ nextblockhash = None
343+ if 'nextblockhash' in r :
344+ nextblockhash = lx (r ['nextblockhash' ])
345+ return {'confirmations' :r ['confirmations' ],
346+ 'height' :r ['height' ],
347+ 'mediantime' :r ['mediantime' ],
348+ 'nextblockhash' :nextblockhash ,
349+ 'chainwork' :x (r ['chainwork' ])}
350+ else :
351+ return CBlockHeader .deserialize (unhexlify (r ))
337352
338353
339354 def getblock (self , block_hash ):
0 commit comments