Skip to content

Commit c7fbc61

Browse files
committed
Add getblockheader RPC call
1 parent 8095402 commit c7fbc61

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

bitcoin/rpc.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
import urlparse
4444

4545
import bitcoin
46-
from bitcoin.core import COIN, lx, b2lx, CBlock, CTransaction, COutPoint, CTxOut
46+
from bitcoin.core import COIN, lx, b2lx, CBlock, CBlockHeader, CTransaction, COutPoint, CTxOut
4747
from bitcoin.core.script import CScript
4848
from bitcoin.wallet import CBitcoinAddress, CBitcoinSecret
4949

@@ -350,6 +350,24 @@ def getbestblockhash(self):
350350
"""Return hash of best (tip) block in longest block chain."""
351351
return lx(self._call('getbestblockhash'))
352352

353+
def getblockheader(self, block_hash):
354+
"""Get block header <block_hash>
355+
356+
Raises IndexError if block_hash is not valid.
357+
"""
358+
try:
359+
block_hash = b2lx(block_hash)
360+
except TypeError:
361+
raise TypeError('%s.getblockheader(): block_hash must be bytes; got %r instance' %
362+
(self.__class__.__name__, block_hash.__class__))
363+
try:
364+
r = self._call('getblockheader', block_hash, False)
365+
except JSONRPCError as ex:
366+
raise IndexError('%s.getblockheader(): %s (%d)' %
367+
(self.__class__.__name__, ex.error['message'], ex.error['code']))
368+
return CBlockHeader.deserialize(unhexlify(r))
369+
370+
353371
def getblock(self, block_hash):
354372
"""Get block <block_hash>
355373

0 commit comments

Comments
 (0)