-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwallet_functions.py
298 lines (231 loc) · 10.9 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
import os
import json
import requests
import subprocess
import monero_usd_price
import monerorequest
from datetime import datetime
import config as cfg
# CHECK FUNCTIONS ######################################################################################################
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
# ACTION 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 monerorequest.Check.wallet(wallet_address=destination_address, allow_standard=True, allow_integrated_address=True, allow_subaddress=True):
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,
#"ring_size": 11,
"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('Sent Monero')
if result is None:
print('Failed to send Monero transaction')
else:
print('Wallet is not a valid monero wallet address.')
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 create_wallet(wallet_name=cfg.wallet_name): # Using CLI Wallet
# Remove existing wallet if present
try:
os.remove(wallet_name)
except:
pass
try:
os.remove(f'{wallet_name}.keys')
except:
pass
command = f"{cfg.monero_wallet_cli_path} --generate-new-wallet {os.path.join(cfg.wallet_file_path, 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
cfg.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: {cfg.wallet_address}')
print(f'view_key: {view_key}')
print(f'seed: {seed}')
with open(file=f'{wallet_name}_seed.txt', mode='a', encoding='utf-8') as f:
f.write(f'Wallet Address:\n{cfg.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 paying monthly subscriptions. If anyone gets access to this seed, they can steal all your funds. Please use responsibly.\n\n\n\n')
return seed, cfg.wallet_address, view_key
else:
print(stderr)
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"])
cfg.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(float(xmr_balance)), ".2f")
except:
usd_balance = '---.--'
#print(usd_balance)
return xmr_balance, usd_balance, cfg.xmr_unlocked_balance
except Exception as e:
print(f'get_wallet_balance error: {e}')
return '--.------------', '---.--'
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 determine_if_a_payment_is_due(subscription):
try: # Get all outgoing transfers from the wallet
headers = {"Content-Type": "application/json"}
data = {
"jsonrpc": "2.0",
"id": "0",
"method": "get_transfers",
"params": {"out": True},
}
# Get outgoing payments
response = requests.post(cfg.local_rpc_url, headers=headers, data=json.dumps(data))
transfers = response.json()["result"]["out"]
except Exception as e:
print(f"Error querying Monero RPC: {e}")
return False, ''
#print(f"Transfers: {transfers}")
# Check each of them
for t in transfers:
if 'destinations' in t and 'payment_id' in t and 'timestamp' in t: # if it has all the fields we are checking
payment_id = t['payment_id']
dest_address = t['destinations'][0]['address'] # we are reading the addresses. DO NOT convert to integrated
transaction_date = t['timestamp']
transaction_date = datetime.fromtimestamp(transaction_date)
#print(f'\nFOUND: {payment_id}, {dest_address}, {transaction_date}\n')
if payment_id == subscription["payment_id"] and dest_address == make_integrated_address(payment_id=payment_id, merchant_public_wallet_address=subscription["sellers_wallet"]):
# Check the date. See if it happened this billing cycle.
days_left = check_date_for_how_many_days_until_payment_needed(transaction_date, subscription["days_per_billing_cycle"])
if days_left > 0: # renew when subscription expires
print(f'Found a payment on {transaction_date}. No payment is due.')
return False, transaction_date # It was this billing cycle. Payment is NOT due.
# if today's date is before the subscription start date
subscription_start_date = datetime.strptime(subscription["start_date"], "%Y-%m-%d")
if datetime.now() <= subscription_start_date:
return False, subscription_start_date
# If we made it here without finding a payment this month, a payment is due.
print('Did not find a payment. A payment is due.')
return True, ''
def check_date_for_how_many_days_until_payment_needed(date, number_of_days):
# Returns the number of days left.
# if subscription start date is in the future
if datetime.now() <= date:
number_of_days = 0
# Calculate the time difference in hours
hours_difference = (datetime.now() - date).total_seconds() / (60 * 60)
# Calculate the hours left
hours_left = (number_of_days * 24) - hours_difference
# Calculate the days left
days_left = hours_left / 24
# print(f'Days Left: {days_left}')
# print(f'Hours Left: {hours_left}')
return days_left