2 Implements simple interface to bitcoind's RPC.
5 import simplejson as json
7 from twisted.internet import defer
8 from twisted.web import client
11 log = stratum.logger.get_logger('bitcoin_rpc')
13 class BitcoinRPC(object):
15 def __init__(self, host, port, username, password):
16 self.bitcoin_url = 'http://%s:%d' % (host, port)
17 self.credentials = base64.b64encode("%s:%s" % (username, password))
19 'Content-Type': 'text/json',
20 'Authorization': 'Basic %s' % self.credentials,
23 def _call_raw(self, data):
24 return client.getPage(
31 def _call(self, method, params):
32 return self._call_raw(json.dumps({
39 @defer.inlineCallbacks
40 def submitblock(self, block_hex):
41 resp = (yield self._call('submitblock', [block_hex,]))
42 if json.loads(resp)['result'] == None:
43 defer.returnValue(True)
45 defer.returnValue(False)
47 @defer.inlineCallbacks
49 resp = (yield self._call('getinfo', []))
50 defer.returnValue(json.loads(resp)['result'])
52 @defer.inlineCallbacks
53 def getblocktemplate(self):
54 resp = (yield self._call('getblocktemplate', []))
55 defer.returnValue(json.loads(resp)['result'])
57 @defer.inlineCallbacks
59 resp = (yield self._call('getwork', []))
61 defer.returnValue(json.loads(resp)['result']['data'][8:72])
62 except Exception as e:
63 log.exception("Cannot decode prevhash %s" % str(e))
66 @defer.inlineCallbacks
67 def validateaddress(self, address):
68 resp = (yield self._call('validateaddress', [address,]))
69 defer.returnValue(json.loads(resp)['result'])