-
Notifications
You must be signed in to change notification settings - Fork 48
/
rpcclient.py
265 lines (221 loc) · 10.6 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import requests
import time, json
import os
class RPCHost():
def __init__(self):
self._session = requests.Session()
try:
with open( os.getenv("HOME") +'/.bitcoin/bitcoin.conf') as fp:
RPCPORT="8332"
RPCHOST="localhost"
RPCSSL=False
for line in fp:
#print line
if line.split('=')[0] == "testnet" and line.split('=')[1] == "1":
RPCPORT="18332"
elif 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 = 2
hadConnectionFailures = False
while True:
try:
response = self._session.post(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():
try:
#support omnicore v0.6
return host.call("getblockchaininfo")
except:
#support omnicore v0.5
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)
def sendrawtransaction(tx):
try:
return host.call("sendrawtransaction", tx)
except Exception, e:
return e
def validateaddress(addr):
return host.call("validateaddress", addr)
def createrawtransaction(ins,outs):
return host.call("createrawtransaction",ins,outs)
def decoderawtransaction(rawtx):
return host.call("decoderawtransaction", rawtx)
def omni_decodetransaction(rawtx):
return host.call("omni_decodetransaction", rawtx)
def estimateFee(blocks=4):
try:
#support omnicore v0.6+
return host.call("estimatesmartfee", blocks)
except:
#support omnicore up to v0.5
return host.call("estimatefee", blocks)
def gettxout(txid,vout,unconfirmed=True):
return host.call("gettxout",txid,vout,unconfirmed)
## Omni Specific RPC calls
def omni_getactivations():
return host.call("omni_getactivations")
def omni_getcurrentconsensushash():
return host.call("omni_getcurrentconsensushash")
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 gettrade(txhash):
return host.call("omni_gettrade", txhash)
def getsto_MP(txid):
return host.call("getsto_MP", txid , "*")
def omni_listpendingtransactions():
return host.call("omni_listpendingtransactions")
def omni_getpayload(txid):
return host.call("omni_getpayload",txid)
def getsimplesendPayload(propertyid, amount):
return host.call("omni_createpayload_simplesend", int(propertyid), amount)
def getsendallPayload(ecosystem):
return host.call("omni_createpayload_sendall", int(ecosystem))
def getdexsellPayload(propertyidforsale, amountforsale, amountdesired, paymentwindow, minacceptfee, action):
return host.call("omni_createpayload_dexsell", int(propertyidforsale), amountforsale, amountdesired, int(paymentwindow), minacceptfee, int(action))
def getdexacceptPayload(propertyid, amount):
return host.call("omni_createpayload_dexaccept", int(propertyid), amount)
def getstoPayload(propertyid, amount):
return host.call("omni_createpayload_sto", int(propertyid), amount)
def getgrantPayload(propertyid, amount, memo):
return host.call("omni_createpayload_grant", int(propertyid), amount, memo)
def getrevokePayload(propertyid, amount, memo):
return host.call("omni_createpayload_revoke", int(propertyid), amount, memo)
def getchangeissuerPayload(propertyid):
return host.call("omni_createpayload_changeissuer", int(propertyid))
def gettradePayload(propertyidforsale, amountforsale, propertiddesired, amountdesired):
return host.call("omni_createpayload_trade", int(propertyidforsale), amountforsale, int(propertiddesired), amountdesired)
def getissuancefixedPayload(ecosystem, divisible, previousid, category,subcategory, name, url, data, amount):
return host.call("omni_createpayload_issuancefixed", int(ecosystem), int(divisible), int(previousid), category,subcategory, name, url, data, amount)
def getissuancecrowdsalePayload(ecosystem, divisible, previousid, category,subcategory, name, url, data, propertyiddesired, tokensperunit, deadline, earlybonus, issuerpercentage):
return host.call("omni_createpayload_issuancecrowdsale", int(ecosystem), int(divisible), int(previousid), category,subcategory, name, url, data, int(propertyiddesired), tokensperunit, int(deadline), int(earlybonus), int(issuerpercentage))
def getissuancemanagedPayload(ecosystem, divisible, previousid, category,subcategory, name, url, data):
return host.call("omni_createpayload_issuancemanaged", int(ecosystem), int(divisible), int(previousid), category,subcategory, name, url, data)
def getclosecrowdsalePayload(propertyid):
return host.call("omni_createpayload_closecrowdsale", int(propertyid))
def getcanceltradesbypricePayload(propertyidforsale, amountforsale, propertiddesired, amountdesired):
return host.call("omni_createpayload_canceltradesbyprice", int(propertyidforsale), amountforsale, int(propertiddesired), amountdesired)
def getcanceltradesbypairPayload(propertyidforsale, propertiddesired):
return host.call("omni_createpayload_canceltradesbypair", int(propertyidforsale), int(propertiddesired))
def getcancelalltradesPayload(ecosystem):
return host.call("omni_createpayload_cancelalltrades", int(ecosystem))
def createrawtx_opreturn(payload, rawtx=None):
return host.call("omni_createrawtx_opreturn", rawtx, payload)
def createrawtx_multisig(payload, seed, pubkey, rawtx=None):
return host.call("omni_createrawtx_multisig", rawtx, payload, seed, pubkey)
def createrawtx_input(txhash, index, rawtx=None):
return host.call("omni_createrawtx_input", rawtx, txhash, index)
def createrawtx_reference(destination, rawtx=None):
return host.call("omni_createrawtx_reference", rawtx, destination, "0.00000546")
def createrawtx_change(rawtx, previnputs, destination, fee):
return host.call("omni_createrawtx_change", rawtx, previnputs, destination, str(fee))
#bitcore calls
def getaddresstxids(address):
#Returns the txids for an address(es)
if isinstance(address,list):
payload = {"addresses": address}
else:
payload = {"addresses": [address]}
return host.call("getaddresstxids", payload)
def getaddressdeltas(address):
#Returns all changes for an address
if isinstance(address,list):
payload = {"addresses": address}
else:
payload = {"addresses": [address]}
return host.call("getaddressdeltas", payload)
def getaddressbalance(address):
#Returns the balance for an address(es)
if isinstance(address,list):
payload = {"addresses": address}
else:
payload = {"addresses": [address]}
return host.call("getaddressbalance", payload)
def getaddressutxos(address):
#Returns all unspent outputs for an address
if isinstance(address,list):
payload = {"addresses": address}
else:
payload = {"addresses": [address]}
return host.call("getaddressutxos", payload)
def getaddressmempool(address):
#Returns all mempool deltas for an address
if isinstance(address,list):
payload = {"addresses": address}
else:
payload = {"addresses": [address]}
return host.call("getaddressmempool", payload)
def getblockhashes(start,end):
#Returns array of hashes of blocks within the timestamp range provided
return host.call("getblockhashes", start, end)
def getspentinfo(txid,index):
#Returns the txid and index where an output is spent
payload = {"txid":txid,"index":index}
return host.call("getspentinfo", payload)