-
Notifications
You must be signed in to change notification settings - Fork 2
/
monero_wallet_rpc.py
246 lines (187 loc) · 9.11 KB
/
monero_wallet_rpc.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
import logging
import requests
import json
from requests.auth import HTTPDigestAuth
import os
import binascii
class MoneroWalletRpc:
def __init__(self, rpc_url:str, user=None, pwd=None):
self.rpc_url = rpc_url
self.user = user
self.password = pwd
self.headers = {'content-type': 'application/json'}
self.update = {"jsonrpc": "2.0", "id": "0"}
self.rpc_input = None
def post_to_monero_wallet_rpc(self, method: str, params=None):
if params is not None:
rpc_input = json.dumps({"jsonrpc": "2.0", "id": "0",
"method": method, "params": params})
else:
rpc_input = json.dumps({"jsonrpc": "2.0", "id": "0",
"method": method})
logging.info(json.dumps(json.loads(rpc_input), indent=4))
response = requests.post(
self.rpc_url,
data=rpc_input,
headers=self.headers,
auth=HTTPDigestAuth(self.user, self.password)
)
logging.info(json.dumps(response.json(), indent=4))
return response.json()
def refresh(self):
return self.post_to_monero_wallet_rpc("refresh")
def prepare_multisig(self):
return self.post_to_monero_wallet_rpc("prepare_multisig")
def exchange_multisig_keys(self, *, multisig_info):
params = {"multisig_info": multisig_info}
return self.post_to_monero_wallet_rpc("exchange_multisig_keys", params)
def make_multisig(self, *, multisig_info, threshold: int, password: str = None):
params = {
"multisig_info": multisig_info,
"threshold": threshold}
if password:
params["password"] = password
return self.post_to_monero_wallet_rpc("make_multisig", params)
def finalize_multisig(self, *, multisig_info, password: str = None):
params = {
"multisig_info": multisig_info}
if password:
params["password"] = password
return self.post_to_monero_wallet_rpc("finalize_multisig", params)
def is_multisig(self):
return self.post_to_monero_wallet_rpc("is_multisig")
def sign_multisig(self, tx_data_hex=None):
params = {"tx_data_hex" : tx_data_hex}
return self.post_to_monero_wallet_rpc("sign_multisig", params=params)
def submit_multisig(self, tx_data_hex=None):
params = {"tx_data_hex" : tx_data_hex}
return self.post_to_monero_wallet_rpc("submit_multisig", params=params)
def export_multisig_info(self):
return self.post_to_monero_wallet_rpc("export_multisig_info")
def import_multisig_info(self, info=None):
params = {"info": info}
return self.post_to_monero_wallet_rpc("import_multisig_info", params=params)
def get_balance(self):
return self.post_to_monero_wallet_rpc("getbalance")
def get_address(self):
return self.post_to_monero_wallet_rpc("getaddress")
def get_height(self):
return self.post_to_monero_wallet_rpc("getheight")
def sweep_dust(self):
return self.post_to_monero_wallet_rpc("sweep_dust")
def get_payments(self, payment_id):
params = {"wallet_address": payment_id}
return self.post_to_monero_wallet_rpc("payment_id", params)
def export_key_images(self):
return self.post_to_monero_wallet_rpc("export_key_images")
def import_key_images(self, keys:dict):
dict_key = {}
list_keysig = []
for key_image, sig in keys:
dict_key.update({"key_image": key_image,"signature": sig})
list_keysig.append(dict_key)
dict_key.clear()
params = {"signed_key_images": list_keysig}
return self.post_to_monero_wallet_rpc("import_key_images", params)
def rescan_spent(self):
return self.post_to_monero_wallet_rpc("rescan_spent")
def start_mining(self,thread_count:int, background_mining:bool, ignore_battery:bool):
if background_mining is True:
background_mining_str = "true"
else:
background_mining_str = "false"
if ignore_battery is True:
ignore_battery_str = "true"
else:
ignore_battery_str = "false"
params = {"threads_count": thread_count, "do_background_mining": background_mining_str, "ignore_battery": ignore_battery_str}
return self.post_to_monero_wallet_rpc("start_mining", params)
def stop_mining(self):
return self.post_to_monero_wallet_rpc("stop_mining")
def stop_wallet(self):
return self.post_to_monero_wallet_rpc("stop_wallet")
def get_languages(self):
return self.post_to_monero_wallet_rpc("get_languages")
#You need to have set the argument "–wallet-dir" when
def create_wallet(self, *, wallet_name:str, password: str, language:str = "English"):
params = {"filename": wallet_name, "password": password, "language": language}
return self.post_to_monero_wallet_rpc("create_wallet", params)
#You need to have set the argument "–wallet-dir" when
def open_wallet(self, *, wallet_name:str, password:str=""):
params = {"filename": wallet_name, "password": password}
return self.post_to_monero_wallet_rpc("open_wallet", params)
def close_wallet(self):
return self.post_to_monero_wallet_rpc("close_wallet")
def delete_address_book(self, address_index: int):
params = {"index": address_index}
return self.post_to_monero_wallet_rpc("delete_address_book", params)
def add_address_book(self, address: str, payment_id: str=None, description: str=None):
params = {"address": address}
if payment_id is not None:
params.update({"payment_id":payment_id})
if description is not None:
params.update({"description":description})
return self.post_to_monero_wallet_rpc("add_address_book", params)
def sign(self, data: str):
params = {"data": data}
return self.post_to_monero_wallet_rpc("sign", params)
def verify(self, data: str, address: str, signature:str):
params = {"data": data, "address": address, "signature": signature}
return self.post_to_monero_wallet_rpc("verify", params=params)
def make_uri_payment(self, address:str, amount:int,
payment_id:str, tx_description:str, recipient_name:str ):
params = {"address": address, "amount": amount, "payment_id": payment_id,
"tx_description" : tx_description, "recipient_name": recipient_name}
return self.post_to_monero_wallet_rpc("make_uri", params)
def transfer(self, transactions, mixin=7, payment_id=None):
# standard json header
headers = self.headers
recipients = []
for address, amount in transactions.items():
int_amount = int(self.get_amount(amount))
assert amount == float(self.get_money(str(int_amount))), "Amount conversion failed"
recipients.append({"address": address, "amount": int_amount})
params = {"destinations": recipients,
"mixin": mixin}
if payment_id:
params["payment_id"] = payment_id
return self.post_to_monero_wallet_rpc("transfer", params)
def get_amount(self, amount):
"""encode amount (float number) to the cryptonote format. Hope its correct.
Based on C++ code:
https://github.com/monero-project/bitmonero/blob/master/src/cryptonote_core/cryptonote_format_utils.cpp#L211
"""
cryptonote_display_decimal_point = 12
str_amount = str(amount)
fraction_size = 0
if '.' in str_amount:
point_index = str_amount.index('.')
fraction_size = len(str_amount) - point_index - 1
while fraction_size < cryptonote_display_decimal_point and '0' == str_amount[-1]:
str_amount = str_amount[:-1]
fraction_size = fraction_size - 1
if cryptonote_display_decimal_point < fraction_size:
return False
str_amount = str_amount[:point_index] + str_amount[point_index+1:]
if not str_amount:
return False
if fraction_size < cryptonote_display_decimal_point:
str_amount = str_amount + '0'*(cryptonote_display_decimal_point - fraction_size)
return str_amount
def get_money(self, amount):
"""decode cryptonote amount format to user friendly format. Hope its correct.
Based on C++ code:
https://github.com/monero-project/bitmonero/blob/master/src/cryptonote_core/cryptonote_format_utils.cpp#L751
"""
cryptonote_display_decimal_point = 12
s = amount
if len(s) < cryptonote_display_decimal_point + 1:
# add some trailing zeros, if needed, to have constant width
s = '0' * (cryptonote_display_decimal_point + 1 - len(s)) + s
idx = len(s) - cryptonote_display_decimal_point
s = s[0:idx] + "." + s[idx:]
return s
def get_payment_id(self):
random_32_bytes = os.urandom(32)
payment_id = "".join(map(chr, binascii.hexlify(random_32_bytes)))
return payment_id