-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_bitcoin_wallet.py
executable file
·57 lines (48 loc) · 1.89 KB
/
check_bitcoin_wallet.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
# Telegram Bot - Server and website monitoring
#
# Check for new Bitcoin transactions for a specific address
# Uses Python Bitcoinlib
#
# Call from commandline with
# $ python check_bitcoin_address.py '<address>'
#
import sys
import requests
from bitcoinlib.wallets import wallet_create_or_open
from sendmessage import sendmessage
from helpers import file_get_status_str, file_write_status
from bitcoinlib.services.services import Service
from bitcoinlib.encoding import *
from bitcoinlib.keys import Address
# Settings
timeout = 10 # seconds
message_str_add_watch = "Start monitoring new transactions for wallet with public masterkey %s"
message_str_new_tx_found = "New transactions for wallet %s with txid %s"
debug = True
# Monitor and send message
def check_wallet(public_masterkey):
latest_txid = None
try:
w = wallet_create_or_open(public_masterkey, public_masterkey)
w.scan(rescan_used=True)
txs = w.transactions()
if txs:
latest_txid = txs[-1].txid
except Exception as e:
if debug:
print("Error retrieving transactions from server: %s" % str(e))
monitor_filename = '.tbot-check-wallet-%s' % public_masterkey
status_last_txid = file_get_status_str(monitor_filename)
if debug:
print("Lastest recorded txid / found txid: %s / %s" %
(status_last_txid if status_last_txid else 'None', latest_txid if latest_txid else 'None'))
status_last_txid = None if not status_last_txid else status_last_txid
if status_last_txid != latest_txid:
if not latest_txid:
sendmessage(message_str_add_watch % public_masterkey)
else:
sendmessage(message_str_new_tx_found % (public_masterkey, latest_txid))
file_write_status(monitor_filename, "" if not latest_txid else latest_txid)
if __name__ == "__main__":
public_masterkey = sys.argv[1]
check_wallet(public_masterkey)