-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwallet_functions.py
353 lines (273 loc) · 12.3 KB
/
wallet_functions.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import os
import csv
import time
import json
import gzip
import psutil
import base64
import qrcode
import random
import requests
import threading
import subprocess
from lxml import html
import monero_usd_price
import PySimpleGUI as sg
from datetime import datetime, timezone
import platform
import clipboard
import config as cfg
# CHECK FUNCTIONS ( Return True/False ) ################################################################################
def check_if_monero_wallet_address_is_valid_format(wallet_address):
# Check if the wallet address starts with the number 4
if wallet_address[0] != "4":
return False
# Check if the wallet address is exactly 95 or 106 characters long
if len(wallet_address) not in [95, 106]:
return False
# Check if the wallet address contains only valid characters
valid_chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
for char in wallet_address:
if char not in valid_chars:
return False
# If it passed all these checks
return True
def check_if_wallet_exists(daemon_rpc_url):
if not os.path.isfile(f"{cfg.wallet_name}.keys") or not os.path.isfile(cfg.wallet_name):
# If either file doesn't exist
start_block_height = get_current_block_height(daemon_rpc_url=daemon_rpc_url)
create_wallet(wallet_name=cfg.wallet_name)
return start_block_height
else:
# If both files exist, do nothing
print('Wallet exists already.')
return None
def check_if_node_works(node):
url = f'http://{node}/json_rpc'
headers = {'Content-Type': 'application/json'}
payload = {
'jsonrpc': '2.0',
'id': '0',
'method': 'get_info',
'params': {}
}
try:
response = requests.post(url, data=json.dumps(payload), headers=headers)
response.raise_for_status()
result = response.json()
if 'result' in result and 'status' in result['result'] and result['result']['status'] == 'OK':
return True
else:
return False
except requests.exceptions.RequestException as e:
print(e)
return False
def check_if_payment_id_is_valid(payment_id):
if len(payment_id) != 16:
return False
valid_chars = set('0123456789abcdef')
for char in payment_id:
if char not in valid_chars:
return False
# If it passed all these checks
return True
# WALLET "RETURN SOMETHING" FUNCTIONS ##################################################################################
def make_integrated_address(payment_id, merchant_public_wallet_address):
headers = {'Content-Type': 'application/json'}
data = {
"jsonrpc": "2.0",
"id": "0",
"method": "make_integrated_address",
"params": {
"standard_address": merchant_public_wallet_address,
"payment_id": payment_id
}
}
response = requests.post(f"{cfg.local_rpc_url}", headers=headers, data=json.dumps(data))
result = response.json()
if 'error' in result:
print('Error:', result['error']['message'])
else:
integrated_address = result['result']['integrated_address']
return integrated_address
def get_current_block_height(daemon_rpc_url):
# Set up the JSON-RPC request
headers = {'content-type': 'application/json'}
data = {
"jsonrpc": "2.0",
"id": "0",
"method": "get_info"
}
# Send the JSON-RPC request to the daemon
response = requests.post(daemon_rpc_url, data=json.dumps(data), headers=headers)
# Parse the response to get the block height
if response.status_code == 200:
response_data = response.json()
block_height = response_data["result"]["height"]
print(f'Block Height: {block_height}')
return block_height
else:
return None
def get_wallet_balance():
headers = {"content-type": "application/json"}
payload = {
"jsonrpc": "2.0",
"id": "0",
"method": "get_balance"
}
try:
# get balance
response = requests.post(cfg.local_rpc_url, headers=headers, data=json.dumps(payload), auth=(cfg.rpc_username, cfg.rpc_password))
response.raise_for_status()
result = response.json().get("result")
if result is None:
raise ValueError("Failed to get wallet balance")
xmr_balance = monero_usd_price.calculate_monero_from_atomic_units(atomic_units=result["balance"])
xmr_unlocked_balance = monero_usd_price.calculate_monero_from_atomic_units(atomic_units=result["unlocked_balance"])
#print(cfg.xmr_unlocked_balance)
try:
usd_balance = format(monero_usd_price.calculate_usd_from_monero(monero_amount=float(xmr_balance), print_price_to_console=False, monero_price=cfg.current_monero_price), ".2f")
except:
usd_balance = '---.--'
#print(usd_balance)
return xmr_balance, usd_balance, xmr_unlocked_balance
except Exception as e:
print(f'get_wallet_balance error: {e}')
return '--.------------', '---.--'
def get_wallet_balance_in_xmr_minus_amount(amount_in_usd=1):
# This returns the UNLOCKED monero amount in the wallet minus a USD amount. Default $1. (or 0 if there is less than $1 in the wallet)
# Get our current balances. Some extra is needed for transaction fees. Less than $0.01, but leaving $1 to future-proof.
cfg.wallet_balance_xmr, cfg.wallet_balance_usd, cfg.xmr_unlocked_balance = get_wallet_balance()
# Get amount of Monero that is worth the same USD amount.
monero_amount_worth_amount_in_usd = monero_usd_price.calculate_monero_from_usd(usd_amount=amount_in_usd, print_price_to_console=False, monero_price=cfg.current_monero_price)
wallet_balance_minus_amount_usd = cfg.xmr_unlocked_balance - monero_amount_worth_amount_in_usd
#print(wallet_balance_minus_amount_usd)
if wallet_balance_minus_amount_usd > 0:
return wallet_balance_minus_amount_usd
else:
return 0
def get_wallet_address():
headers = {"content-type": "application/json"}
payload = {
"jsonrpc": "2.0",
"id": "0",
"method": "get_address"
}
response = requests.post(cfg.local_rpc_url, headers=headers, data=json.dumps(payload), auth=(cfg.rpc_username, cfg.rpc_password))
response.raise_for_status()
result = response.json().get("result")
if result is None:
raise ValueError("Failed to get wallet address")
address = result["address"]
print(address)
return address
def get_all_transactions():
headers = {"content-type": "application/json"}
payload = {
"jsonrpc": "2.0",
"id": "0",
"method": "get_transfers",
"params": {
"in": True,
"out": True,
"pending": True,
"failed": True,
}
}
response = requests.post(cfg.local_rpc_url, headers=headers, data=json.dumps(payload), auth=(cfg.rpc_username, cfg.rpc_password))
response_data = response.json()
#print('WE GOT:')
#print(response_data)
if "error" in response_data:
raise ValueError(f"RPC Error {response_data['error']['code']}: {response_data['error']['message']}")
result_data = response_data.get("result", {})
all_transfers = []
# Iterate over each direction and aggregate the transactions
for direction in ["in", "out", "pending", "failed"]:
transactions = result_data.get(direction, [])
all_transfers.extend(transactions)
return all_transfers
def filter_transactions(transfers, direction):
"""
Filter transactions based on the specified direction.
Parameters:
- transfers (list of dict): List of transactions.
- direction (str): The direction to filter by. Options are "in", "out", "pending", "failed"
Returns:
- list of dict: Filtered transactions.
"""
# Use a list comprehension to filter the transactions based on direction
filtered_transfers = [transaction for transaction in transfers if transaction.get("type") == direction]
return filtered_transfers
# WALLET "DO SOMETHING" FUNCTIONS ######################################################################################
def send_monero(destination_address, amount, payment_id=None):
# this needs to measure in atomic units, not xmr, so this converts it.
amount = monero_usd_price.calculate_atomic_units_from_monero(monero_amount=amount)
if check_if_monero_wallet_address_is_valid_format(wallet_address=destination_address):
print('Address is valid. Trying to send Monero')
# Changes the wallet address to use an integrated wallet address ONLY if a payment id was specified.
if payment_id:
# generate the integrated address to pay (an address with the payment ID baked into it)
destination_address = make_integrated_address(payment_id=payment_id, merchant_public_wallet_address=destination_address)
headers = {"content-type": "application/json"}
payload = {
"jsonrpc": "2.0",
"id": "0",
"method": "transfer",
"params": {
"destinations": [{"amount": amount, "address": destination_address}],
"priority": 1,
"get_tx_key": True
}
}
response = requests.post(cfg.local_rpc_url, headers=headers, data=json.dumps(payload), auth=(cfg.rpc_username, cfg.rpc_password))
response.raise_for_status()
result = response.json().get("result")
print('Trying to send Monero')
#if result is None:
# print('Failed to send Monero transaction')
response_data = response.json()
if "result" in response_data:
print('Monero transaction successful.')
elif "error" in response_data:
print(f"Error: {response_data['error']['message']} (Code: {response_data['error']['code']})")
else:
print('Unexpected response format.')
else:
print('Wallet is not a valid monero wallet address.')
# FUNCTIONS TO FIX #####################################################################################################
# SHOULD UPDATE THIS TO USE RPC BUT IT IS WORKING, SO I HAVE NOT MESSED WITH IT
def create_wallet(wallet_name): # Using CLI Wallet
# Remove existing wallet if present
try:
os.remove(cfg.wallet_name)
except:
pass
try:
os.remove(f'{cfg.wallet_name}.keys')
except:
pass
command = f"{cfg.monero_wallet_cli_path} --generate-new-wallet {os.path.join(cfg.wallet_file_path, cfg.wallet_name)} --mnemonic-language English --command exit"
process = subprocess.Popen(command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
# Sending two newline characters, pressing 'Enter' twice
process.stdin.write('\n')
process.stdin.write('\n')
process.stdin.flush()
# Getting the output and error messages
stdout, stderr = process.communicate()
#print(stdout)
#print(stderr)
worked_check = process.returncode
if worked_check == 0:
output_text = stdout
wallet_address = output_text.split('Generated new wallet: ')[1].split('View key: ')[0].strip()
view_key = output_text.split('View key: ')[1].split('*********************')[0].strip()
seed = output_text.split(' of your immediate control.')[1].split('********')[0].strip().replace('\n', '')
print(f'wallet_address: {wallet_address}')
print(f'view_key: {view_key}')
print(f'seed: {seed}')
with open(file=f'{cfg.wallet_name}_seed.txt', mode='a', encoding='utf-8') as f:
f.write(f'Wallet Address:\n{wallet_address}\nView Key:\n{view_key}\nSeed:\n{seed}\n\nThe above wallet should not be your main source of funds. This is ONLY to be a side account for recording and auto-forwarding payments. If anyone gets access to this seed, they can steal all your funds. Please use responsibly.\n\n\n\n')
return seed, wallet_address, view_key
else:
print(stderr)