-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathrpcclient.py
118 lines (97 loc) · 4.23 KB
/
rpcclient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import requests, getpass
import time, json
class RPCHost():
def __init__(self):
USER=getpass.getuser()
self._session = requests.Session()
try:
with open('/home/'+USER+'/.bitcoin/bitcoin.conf') as fp:
RPCPORT="8332"
RPCHOST="localhost"
RPCSSL=False
for line in fp:
#print line
if line.split('=')[0] == "rpcuser":
RPCUSER=line.split('=')[1].strip()
elif line.split('=')[0] == "rpcpassword":
RPCPASS=line.split('=')[1].strip()
elif line.split('=')[0] == "rpcconnect":
RPCHOST=line.split('=')[1].strip()
elif line.split('=')[0] == "rpcport":
RPCPORT=line.split('=')[1].strip()
elif line.split('=')[0] == "rpcssl":
if line.split('=')[1].strip() == "1":
RPCSSL=True
else:
RPCSSL=False
except IOError as e:
response='{"error": "Unable to load bitcoin config file. Please Notify Site Administrator"}'
return response
if RPCSSL:
self._url = "https://"+RPCUSER+":"+RPCPASS+"@"+RPCHOST+":"+RPCPORT
else:
self._url = "http://"+RPCUSER+":"+RPCPASS+"@"+RPCHOST+":"+RPCPORT
self._headers = {'content-type': 'application/json'}
def call(self, rpcMethod, *params):
payload = json.dumps({"method": rpcMethod, "params": list(params), "jsonrpc": "2.0"})
tries = 10
hadConnectionFailures = False
while True:
try:
response = self._session.get(self._url, headers=self._headers, data=payload, verify=False)
except requests.exceptions.ConnectionError:
tries -= 1
if tries == 0:
raise Exception('Failed to connect for remote procedure call.')
hadFailedConnections = True
print("Couldn't connect for remote procedure call, will sleep for ten seconds and then try again ({} more tries)".format(tries))
time.sleep(10)
else:
if hadConnectionFailures:
print('Connected for remote procedure call after retry.')
break
if not response.status_code in (200, 500):
raise Exception('RPC connection failure: ' + str(response.status_code) + ' ' + response.reason)
responseJSON = response.json()
if 'error' in responseJSON and responseJSON['error'] != None:
raise Exception('Error in ' + rpcMethod + ' RPC call: ' + str(responseJSON['error']))
#return responseJSON['result']
return responseJSON
#Define / Create RPC connection
host=RPCHost()
#Bitcoin Generic RPC calls
def getinfo():
return host.call("getinfo")
def getrawtransaction(txid):
return host.call("getrawtransaction", txid , 1)
def getblockhash(block):
return host.call("getblockhash", block)
def getblock(hash):
return host.call("getblock", hash)
## Mastercoin Specific RPC calls
def getbalance_MP(addr, propertyid):
return host.call("getbalance_MP", addr, propertyid)
def getallbalancesforaddress_MP(addr):
return host.call("getallbalancesforaddress_MP", addr)
def getallbalancesforid_MP(propertyid):
return host.call("getallbalancesforid_MP", propertyid)
def gettransaction_MP(tx):
return host.call("gettransaction_MP", tx)
def listblocktransactions_MP(height):
return host.call("listblocktransactions_MP", height)
def getproperty_MP(propertyid):
return host.call("getproperty_MP", propertyid)
def listproperties_MP():
return host.call("listproperties_MP")
def getcrowdsale_MP(propertyid):
return host.call("getcrowdsale_MP", propertyid)
def getactivecrowdsales_MP():
return host.call("getactivecrowdsales_MP")
def getactivedexsells_MP():
return host.call("getactivedexsells_MP")
def getdivisible_MP(propertyid):
return getproperty_MP(propertyid)['result']['divisible']
def getgrants_MP(propertyid):
return host.call("getgrants_MP", propertyid)
def gettradessince_MP():
return host.call("gettradessince_MP")