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
|
2020-11-26 04:45:52 +01:00
|
|
|
self.node = node
|
2020-11-24 07:26:02 +01:00
|
|
|
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
|
|
|
}
|
2020-11-26 04:45:52 +01:00
|
|
|
balance(script: $pubkey) {
|
|
|
|
amount
|
2020-11-27 03:21:42 +01:00
|
|
|
base
|
2020-11-26 04:45:52 +01:00
|
|
|
}
|
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):
|
2020-11-27 03:21:42 +01:00
|
|
|
trans = []
|
2020-11-24 07:26:02 +01:00
|
|
|
i = 0
|
2020-11-27 03:21:42 +01:00
|
|
|
|
|
|
|
for sens in 'received','sent','receiving','sending':
|
|
|
|
res = self.historyDoc['transactionsHistory'][sens]
|
|
|
|
for bloc in res:
|
|
|
|
output = bloc['outputs'][0]
|
2020-11-27 05:19:28 +01:00
|
|
|
outPubkey = output.split("SIG(")[1].replace(')','')
|
|
|
|
if sens in ('received','receiving') or self.pubkey != outPubkey:
|
|
|
|
trans.append(i)
|
|
|
|
trans[i] = []
|
|
|
|
trans[i].append(sens)
|
|
|
|
if sens in ('receiving','sending'):
|
|
|
|
trans[i].append(int(time.time()))
|
|
|
|
else:
|
|
|
|
trans[i].append(bloc['writtenTime'])
|
|
|
|
if sens in ('sent','sending'):
|
|
|
|
trans[i].append(outPubkey)
|
|
|
|
else:
|
|
|
|
trans[i].append(bloc['issuers'][0])
|
|
|
|
amount = int(output.split(':')[0])
|
|
|
|
base = int(output.split(':')[1])
|
|
|
|
trans[i].append(amount*pow(10,base)/100)
|
|
|
|
trans[i].append(bloc['comment'])
|
|
|
|
i += 1
|
2020-11-25 05:06:57 +01:00
|
|
|
|
2020-11-24 07:26:02 +01:00
|
|
|
trans.sort(key=lambda x: x[1])
|
|
|
|
|
2020-11-26 04:45:52 +01:00
|
|
|
# Get balance
|
2020-11-27 03:21:42 +01:00
|
|
|
balance = self.historyDoc['balance']['amount']
|
|
|
|
baseBalance = self.historyDoc['balance']['base']
|
|
|
|
balance = balance*pow(10,baseBalance)/100
|
2020-11-26 04:45:52 +01:00
|
|
|
|
2020-11-24 07:26:02 +01:00
|
|
|
# Get terminal size
|
|
|
|
rows = int(os.popen('stty size', 'r').read().split()[1])
|
|
|
|
|
|
|
|
print('-'.center(rows, '-'))
|
2020-11-26 04:45:52 +01:00
|
|
|
print("\033[1m{: <20} | {: <45} | {: <7} | {: <30}\033[0m".format(" Date"," De / À (clé publique)","Montant (Ḡ1)","Commentaire"))
|
2020-11-27 03:21:42 +01:00
|
|
|
for t in trans:
|
|
|
|
if t[0] == "received":
|
2020-11-25 05:06:57 +01:00
|
|
|
color = "green"
|
2020-11-27 03:21:42 +01:00
|
|
|
elif t[0] == "receiving":
|
2020-11-25 05:06:57 +01:00
|
|
|
color = "yellow"
|
2020-11-27 03:21:42 +01:00
|
|
|
elif t[0] == "sending":
|
2020-11-25 05:06:57 +01:00
|
|
|
color = "red"
|
|
|
|
else: color = "blue"
|
|
|
|
|
2020-11-27 03:21:42 +01:00
|
|
|
date = datetime.fromtimestamp(t[1]).strftime("%d/%m/%Y à %H:%M")
|
2020-11-24 07:26:02 +01:00
|
|
|
print('-'.center(rows, '-'))
|
2020-11-27 03:21:42 +01:00
|
|
|
print(colored("| {: <18} | {: <45} | {: <12} | {: <30}".format(date, *t[2:]), color))
|
2020-11-26 04:45:52 +01:00
|
|
|
print('-'.center(rows, '-'))
|
|
|
|
print('\033[1mSolde du compte: {0} Ḡ1\033[0m'.format(balance).center(rows, ' '))
|
2020-11-25 05:06:57 +01:00
|
|
|
print('-'.center(rows, '-'))
|
2020-11-26 04:45:52 +01:00
|
|
|
print(colored('Reçus', 'green'), '-', colored('En cours de réception', 'yellow'), '-', colored('Envoyé', 'blue'), '-', colored("En cours d'envoi", 'red'))
|
2020-11-24 07:26:02 +01:00
|
|
|
|
|
|
|
def history(self):
|
|
|
|
self.sendDoc()
|
|
|
|
self.parseHistory()
|
|
|
|
|
2020-11-25 05:06:57 +01:00
|
|
|
|