2020-11-24 07:26:02 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2020-11-25 05:06:57 +01:00
|
|
|
import sys, re, os.path, json, ast, time
|
2020-11-24 07:26:02 +01:00
|
|
|
from datetime import datetime
|
|
|
|
from termcolor import colored
|
2020-11-24 07:41:23 +01:00
|
|
|
from lib.natools import fmt, sign, get_privkey
|
2020-11-24 07:26:02 +01:00
|
|
|
from gql import gql, Client
|
|
|
|
from gql.transport.aiohttp import AIOHTTPTransport
|
|
|
|
|
|
|
|
PUBKEY_REGEX = "(?![OIl])[1-9A-Za-z]{42,45}"
|
|
|
|
|
|
|
|
class History:
|
|
|
|
|
|
|
|
def __init__(self, dunikey, node, pubkey, useMempool=False):
|
|
|
|
self.dunikey = dunikey
|
|
|
|
self.pubkey = pubkey if pubkey else get_privkey(dunikey, "pubsec").pubkey
|
|
|
|
self.useMempool = useMempool
|
|
|
|
if not re.match(PUBKEY_REGEX, self.pubkey) or len(self.pubkey) > 45:
|
|
|
|
sys.stderr.write("La clé publique n'est pas au bon format.\n")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# Define Duniter GVA node
|
|
|
|
transport = AIOHTTPTransport(url=node)
|
|
|
|
self.client = Client(transport=transport, fetch_schema_from_transport=True)
|
|
|
|
|
|
|
|
def sendDoc(self):
|
|
|
|
# Build TX generation document
|
|
|
|
queryBuild = gql(
|
|
|
|
"""
|
|
|
|
query ($pubkey: String!){
|
|
|
|
transactionsHistory(pubkey: $pubkey) {
|
|
|
|
received {
|
|
|
|
writtenTime
|
|
|
|
issuers
|
|
|
|
outputs
|
|
|
|
comment
|
|
|
|
}
|
|
|
|
sent {
|
|
|
|
writtenTime
|
|
|
|
issuers
|
|
|
|
outputs
|
|
|
|
comment
|
|
|
|
}
|
2020-11-25 05:06:57 +01:00
|
|
|
receiving {
|
|
|
|
issuers
|
|
|
|
outputs
|
|
|
|
comment
|
|
|
|
}
|
|
|
|
sending {
|
|
|
|
issuers
|
|
|
|
outputs
|
|
|
|
comment
|
|
|
|
}
|
2020-11-24 07:26:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
paramsBuild = {
|
|
|
|
"pubkey": self.pubkey
|
|
|
|
}
|
|
|
|
|
|
|
|
# Send TX document
|
|
|
|
try:
|
|
|
|
self.historyDoc = self.client.execute(queryBuild, variable_values=paramsBuild)
|
|
|
|
except Exception as e:
|
|
|
|
message = ast.literal_eval(str(e))["message"]
|
|
|
|
sys.stderr.write("Echec de récupération de l'historique:\n" + message + "\n")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
def parseHistory(self):
|
|
|
|
|
|
|
|
res = self.historyDoc['transactionsHistory']['received']
|
|
|
|
transIn=[[0 for x in range(0)] for y in range(len(res))]
|
|
|
|
for i, bloc in enumerate(res):
|
|
|
|
for output in bloc['outputs']:
|
|
|
|
if re.search(self.pubkey, output):
|
|
|
|
transIn[i].append("IN")
|
|
|
|
transIn[i].append(bloc['writtenTime'])
|
|
|
|
transIn[i].append(bloc['issuers'][0])
|
|
|
|
transIn[i].append(int(output.split(':')[0])/100)
|
|
|
|
transIn[i].append(bloc['comment'])
|
|
|
|
|
|
|
|
|
|
|
|
res = self.historyDoc['transactionsHistory']['sent']
|
|
|
|
transOut=[[0 for x in range(0)] for y in range(len(res))]
|
|
|
|
i = 0
|
|
|
|
for bloc in res:
|
|
|
|
for output in bloc['outputs']:
|
|
|
|
if not re.search(self.pubkey, output):
|
|
|
|
transOut[i].append("OUT")
|
|
|
|
transOut[i].append(bloc['writtenTime'])
|
|
|
|
transOut[i].append(output.split("SIG(")[1].replace(')',''))
|
|
|
|
transOut[i].append(int(output.split(':')[0])/100)
|
|
|
|
transOut[i].append(bloc['comment'])
|
|
|
|
i += 1
|
|
|
|
|
2020-11-25 05:06:57 +01:00
|
|
|
res = self.historyDoc['transactionsHistory']['receiving']
|
|
|
|
transInMempool=[[0 for x in range(0)] for y in range(len(res))]
|
|
|
|
for i, bloc in enumerate(res):
|
|
|
|
for output in bloc['outputs']:
|
|
|
|
if re.search(self.pubkey, output):
|
|
|
|
transInMempool[i].append("INM")
|
|
|
|
transInMempool[i].append(int(time.time()))
|
|
|
|
transInMempool[i].append(bloc['issuers'][0])
|
|
|
|
transInMempool[i].append(int(output.split(':')[0])/100)
|
|
|
|
transInMempool[i].append(bloc['comment'])
|
|
|
|
|
|
|
|
res = self.historyDoc['transactionsHistory']['sending']
|
|
|
|
transOutMempool=[[0 for x in range(0)] for y in range(len(res))]
|
|
|
|
i = 0
|
|
|
|
for bloc in res:
|
|
|
|
for output in bloc['outputs']:
|
|
|
|
if not re.search(self.pubkey, output):
|
|
|
|
transOutMempool[i].append("OUTM")
|
|
|
|
transOutMempool[i].append(int(time.time()))
|
|
|
|
transOutMempool[i].append(output.split("SIG(")[1].replace(')',''))
|
|
|
|
transOutMempool[i].append(int(output.split(':')[0])/100)
|
|
|
|
transOutMempool[i].append(bloc['comment'])
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
trans = transIn + transOut + transInMempool + transOutMempool
|
2020-11-24 07:26:02 +01:00
|
|
|
trans = list(filter(None, trans))
|
|
|
|
trans.sort(key=lambda x: x[1])
|
|
|
|
|
|
|
|
# Get terminal size
|
|
|
|
rows = int(os.popen('stty size', 'r').read().split()[1])
|
|
|
|
|
|
|
|
print('-'.center(rows, '-'))
|
2020-11-25 05:06:57 +01:00
|
|
|
print("{: <20} | {: <45} | {: <7} | {: <30}".format(" Date"," De / À (clé publique)","Montant","Commentaire"))
|
2020-11-24 07:26:02 +01:00
|
|
|
for i in trans:
|
2020-11-25 05:06:57 +01:00
|
|
|
if i[0] == "IN":
|
|
|
|
color = "green"
|
|
|
|
elif i[0] == "INM":
|
|
|
|
color = "yellow"
|
|
|
|
elif i[0] == "OUTM":
|
|
|
|
color = "red"
|
|
|
|
else: color = "blue"
|
|
|
|
|
2020-11-24 07:26:02 +01:00
|
|
|
date = datetime.fromtimestamp(i[1]).strftime("%d/%m/%Y à %H:%M")
|
|
|
|
print('-'.center(rows, '-'))
|
|
|
|
print(colored("{: <20} | {: <45} | {: <7} | {: <30}".format(date, *i[2:]), color))
|
2020-11-25 05:06:57 +01:00
|
|
|
print('-'.center(rows, '-'))
|
2020-11-24 07:26:02 +01:00
|
|
|
|
|
|
|
def history(self):
|
|
|
|
self.sendDoc()
|
|
|
|
self.parseHistory()
|
|
|
|
|
2020-11-25 05:06:57 +01:00
|
|
|
|