Change paylib to class format

This commit is contained in:
poka 2020-11-17 06:06:45 +01:00
parent 4cedf50a8e
commit de6372cdd8
3 changed files with 105 additions and 101 deletions

View File

@ -1,8 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from tkinter import * from tkinter import Tk, Label, Entry, Button
sys.path.insert(1, 'libs') from libs.paylib import Transaction
from paylib import *
window = Tk() window = Tk()
@ -12,7 +11,6 @@ title.pack()
# Recipient input # Recipient input
recipientEntry = Entry() recipientEntry = Entry()
# xRecipient = Entry(window, textvariable=recipient, width=30)
recipientEntry.pack() recipientEntry.pack()
@ -29,13 +27,11 @@ def ProceedPaiement():
amount = int(AmountEntry.get()) amount = int(AmountEntry.get())
comment = str(commentEntry.get()) comment = str(commentEntry.get())
print("Paiement en cours ... " + recipient) print("Paiement en cours ... " + recipient)
returnGen = sendGenDoc(recipient, amount, comment)
checkTXDoc(returnGen, recipient, amount, comment)
returnSigned = signDoc(returnGen)
sendTXDoc(returnSigned)
# del recipient, amount, comment, returnGen, returnSigned trans = Transaction(recipient, amount, comment)
recipient = amount = comment = returnGen = returnSigned = None trans.send()
recipient = amount = comment = None
sendButton = Button(window, text="Envoyer", command=ProceedPaiement) sendButton = Button(window, text="Envoyer", command=ProceedPaiement)
sendButton.pack() sendButton.pack()

View File

@ -17,9 +17,16 @@ if not (dunikey):
# Define Duniter GVA node # Define Duniter GVA node
transport = AIOHTTPTransport(url=node) transport = AIOHTTPTransport(url=node)
client = Client(transport=transport, fetch_schema_from_transport=True) client = Client(transport=transport, fetch_schema_from_transport=True)
issuer = get_privkey(dunikey, "pubsec").pubkey
def sendGenDoc(recipient, amount, comment=''):
class Transaction():
def __init__(self, recipient, amount, comment=''):
self.recipient = recipient
self.amount = amount
self.comment = comment
self.issuer = get_privkey(dunikey, "pubsec").pubkey
def genDoc(self):
# TODO: Check args # TODO: Check args
# Build TX generation document # Build TX generation document
@ -35,25 +42,25 @@ def sendGenDoc(recipient, amount, comment=''):
""" """
) )
paramsBuild = { paramsBuild = {
"recipient": recipient, "recipient": self.recipient,
"issuer": issuer, "issuer": self.issuer,
"amount": amount, "amount": self.amount,
"comment": comment "comment": self.comment
} }
# Send TX document # Send TX document
try: try:
txDoc = str(client.execute(queryBuild, variable_values=paramsBuild))[13:-3].replace('\\n','\n') self.txDoc = str(client.execute(queryBuild, variable_values=paramsBuild))[13:-3].replace('\\n','\n')
return txDoc return self.txDoc
except Exception as e: except Exception as e:
message = ast.literal_eval(str(e))["message"] message = ast.literal_eval(str(e))["message"]
sys.stderr.write("Echec de la génération du document:\n" + message + "\n") sys.stderr.write("Echec de la génération du document:\n" + message + "\n")
sys.exit(1) sys.exit(1)
# Check document # Check document
def checkTXDoc(txDoc, recipient, amount, comment=''): def checkTXDoc(self):
docList = txDoc.splitlines() docList = self.txDoc.splitlines()
for i, line in enumerate(docList): for i, line in enumerate(docList):
if re.search("Issuers:", line): if re.search("Issuers:", line):
issuerRaw = docList[(i + 1) % len(docList)] issuerRaw = docList[(i + 1) % len(docList)]
@ -64,24 +71,23 @@ def checkTXDoc(txDoc, recipient, amount, comment=''):
if re.search("Comment:", line): if re.search("Comment:", line):
commentRaw = line.split(': ', 1)[1] commentRaw = line.split(': ', 1)[1]
if issuerRaw != issuer or int(outAmount) != amount or outPubkey != recipient or commentRaw != comment: if issuerRaw != self.issuer or int(outAmount) != self.amount or outPubkey != self.recipient or commentRaw != self.comment:
sys.stderr.write(colored("Le document généré est corrompu !\nNe fait plus confiance au noeud " + node + "\n", 'red')) sys.stderr.write(colored("Le document généré est corrompu !\nNe fait plus confiance au noeud " + node + "\n", 'red'))
sys.stderr.write(colored(issuerRaw + " envoi " + outAmount + " vers " + outPubkey + " with comment: " + commentRaw + "\n", "yellow")) sys.stderr.write(colored(issuerRaw + " envoi " + outAmount + " vers " + outPubkey + " with comment: " + commentRaw + "\n", "yellow"))
sys.exit(1) sys.exit(1)
else: else:
print("Le document généré est conforme.") print("Le document généré est conforme.")
return True return self.txDoc
def signDoc(txDoc): def signDoc(self):
# Sign TX document # Sign TX document
signature = fmt["64"](sign(txDoc.encode(), get_privkey(dunikey, "pubsec"))[:-len(txDoc.encode())]) signature = fmt["64"](sign(self.txDoc.encode(), get_privkey(dunikey, "pubsec"))[:-len(self.txDoc.encode())])
signedDoc = txDoc + signature.decode() self.signedDoc = self.txDoc + signature.decode()
return self.signedDoc
return signedDoc
def sendTXDoc(signedDoc): def sendTXDoc(self):
# Build TX document # Build TX document
querySign = gql( querySign = gql(
""" """
@ -96,7 +102,7 @@ def sendTXDoc(signedDoc):
""" """
) )
paramsSign = { paramsSign = {
"signedDoc": signedDoc "signedDoc": self.signedDoc
} }
# Send TX Signed document # Send TX Signed document
@ -108,3 +114,9 @@ def sendTXDoc(signedDoc):
sys.stderr.write("Echec de la transaction:\n" + message + "\n") sys.stderr.write("Echec de la transaction:\n" + message + "\n")
sys.exit(1) sys.exit(1)
def send(self):
self.genDoc()
self.checkTXDoc()
self.signDoc()
self.sendTXDoc()

16
pay.py
View File

@ -1,7 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import sys import sys
from libs.paylib import sendGenDoc, checkTXDoc, signDoc, sendTXDoc from libs.paylibClass import Transaction
# Get args # Get args
try: try:
@ -9,17 +9,13 @@ try:
amount = int(sys.argv[2]) amount = int(sys.argv[2])
if len(sys.argv) > 3: if len(sys.argv) > 3:
comment = sys.argv[3] comment = sys.argv[3]
else:
comment = ""
except Exception as e: except Exception as e:
print("Please enter the recipient's public and the amount\n" + str(e)) print("Please enter the recipient's public and the amount\n" + str(e))
sys.exit(1) sys.exit(1)
# Execute workflow # Create transaction and send it
## Generate TX document from server and check it trans = Transaction(recipient, amount, comment)
returnGen = sendGenDoc(recipient, amount, comment='') trans.send()
docIsOK = checkTXDoc(returnGen, recipient, amount, comment='')
# Confirm TX document is ok, sign and send it
if docIsOK:
returnSigned = signDoc(returnGen)
sendTXDoc(returnSigned)