From de6372cdd88dbe797b8e46db42522336afdf813a Mon Sep 17 00:00:00 2001 From: poka Date: Tue, 17 Nov 2020 06:06:45 +0100 Subject: [PATCH] Change paylib to class format --- gui-tkinter.py | 16 ++--- libs/paylib.py | 174 ++++++++++++++++++++++++++----------------------- pay.py | 16 ++--- 3 files changed, 105 insertions(+), 101 deletions(-) diff --git a/gui-tkinter.py b/gui-tkinter.py index 67ffcd8..3860e7c 100755 --- a/gui-tkinter.py +++ b/gui-tkinter.py @@ -1,8 +1,7 @@ #!/usr/bin/env python3 -from tkinter import * -sys.path.insert(1, 'libs') -from paylib import * +from tkinter import Tk, Label, Entry, Button +from libs.paylib import Transaction window = Tk() @@ -12,7 +11,6 @@ title.pack() # Recipient input recipientEntry = Entry() -# xRecipient = Entry(window, textvariable=recipient, width=30) recipientEntry.pack() @@ -29,13 +27,11 @@ def ProceedPaiement(): amount = int(AmountEntry.get()) comment = str(commentEntry.get()) 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 - recipient = amount = comment = returnGen = returnSigned = None + trans = Transaction(recipient, amount, comment) + trans.send() + + recipient = amount = comment = None sendButton = Button(window, text="Envoyer", command=ProceedPaiement) sendButton.pack() diff --git a/libs/paylib.py b/libs/paylib.py index 8f522b0..03cc779 100644 --- a/libs/paylib.py +++ b/libs/paylib.py @@ -17,94 +17,106 @@ if not (dunikey): # Define Duniter GVA node transport = AIOHTTPTransport(url=node) client = Client(transport=transport, fetch_schema_from_transport=True) -issuer = get_privkey(dunikey, "pubsec").pubkey - -def sendGenDoc(recipient, amount, comment=''): - # TODO: Check args - - # Build TX generation document - queryBuild = gql( - """ - query ($recipient: String!, $issuer: String!, $amount: Int!, $comment: String!){ genTxs( - amount: $amount - comment: $comment - issuer: $issuer - recipient: $recipient - ) - } - """ - ) - paramsBuild = { - "recipient": recipient, - "issuer": issuer, - "amount": amount, - "comment": comment - } - - # Send TX document - try: - txDoc = str(client.execute(queryBuild, variable_values=paramsBuild))[13:-3].replace('\\n','\n') - return txDoc - except Exception as e: - message = ast.literal_eval(str(e))["message"] - sys.stderr.write("Echec de la génération du document:\n" + message + "\n") - sys.exit(1) -# Check document -def checkTXDoc(txDoc, recipient, amount, comment=''): - docList = txDoc.splitlines() - for i, line in enumerate(docList): - if re.search("Issuers:", line): - issuerRaw = docList[(i + 1) % len(docList)] - if re.search("Outputs:", line): - outputRaw = docList[(i + 1) % len(docList)].split(":") - outAmount = outputRaw[0] - outPubkey = outputRaw[2].split("SIG(")[1].replace(')','') - if re.search("Comment:", line): - commentRaw = line.split(': ', 1)[1] +class Transaction(): + def __init__(self, recipient, amount, comment=''): + self.recipient = recipient + self.amount = amount + self.comment = comment + self.issuer = get_privkey(dunikey, "pubsec").pubkey - if issuerRaw != issuer or int(outAmount) != amount or outPubkey != recipient or commentRaw != 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(issuerRaw + " envoi " + outAmount + " vers " + outPubkey + " with comment: " + commentRaw + "\n", "yellow")) - sys.exit(1) - else: - print("Le document généré est conforme.") - return True + def genDoc(self): + # TODO: Check args + + # Build TX generation document + queryBuild = gql( + """ + query ($recipient: String!, $issuer: String!, $amount: Int!, $comment: String!){ genTxs( + amount: $amount + comment: $comment + issuer: $issuer + recipient: $recipient + ) + } + """ + ) + paramsBuild = { + "recipient": self.recipient, + "issuer": self.issuer, + "amount": self.amount, + "comment": self.comment + } + + # Send TX document + try: + self.txDoc = str(client.execute(queryBuild, variable_values=paramsBuild))[13:-3].replace('\\n','\n') + return self.txDoc + except Exception as e: + message = ast.literal_eval(str(e))["message"] + sys.stderr.write("Echec de la génération du document:\n" + message + "\n") + sys.exit(1) -def signDoc(txDoc): - # Sign TX document - signature = fmt["64"](sign(txDoc.encode(), get_privkey(dunikey, "pubsec"))[:-len(txDoc.encode())]) - signedDoc = txDoc + signature.decode() + # Check document + def checkTXDoc(self): + docList = self.txDoc.splitlines() + for i, line in enumerate(docList): + if re.search("Issuers:", line): + issuerRaw = docList[(i + 1) % len(docList)] + if re.search("Outputs:", line): + outputRaw = docList[(i + 1) % len(docList)].split(":") + outAmount = outputRaw[0] + outPubkey = outputRaw[2].split("SIG(")[1].replace(')','') + if re.search("Comment:", line): + commentRaw = line.split(': ', 1)[1] - return signedDoc + 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(issuerRaw + " envoi " + outAmount + " vers " + outPubkey + " with comment: " + commentRaw + "\n", "yellow")) + sys.exit(1) + else: + print("Le document généré est conforme.") + return self.txDoc -def sendTXDoc(signedDoc): - # Build TX document - querySign = gql( - """ - mutation ($signedDoc: String!){ tx( - rawTx: $signedDoc - ) { - version - issuers - outputs - } - } - """ - ) - paramsSign = { - "signedDoc": signedDoc - } + def signDoc(self): + # Sign TX document + signature = fmt["64"](sign(self.txDoc.encode(), get_privkey(dunikey, "pubsec"))[:-len(self.txDoc.encode())]) + self.signedDoc = self.txDoc + signature.decode() + return self.signedDoc - # Send TX Signed document - try: - client.execute(querySign, variable_values=paramsSign) - print(colored("Transaction effectué avec succès !", "green")) - except Exception as e: - message = ast.literal_eval(str(e))["message"] - sys.stderr.write("Echec de la transaction:\n" + message + "\n") - sys.exit(1) + + def sendTXDoc(self): + # Build TX document + querySign = gql( + """ + mutation ($signedDoc: String!){ tx( + rawTx: $signedDoc + ) { + version + issuers + outputs + } + } + """ + ) + paramsSign = { + "signedDoc": self.signedDoc + } + + # Send TX Signed document + try: + client.execute(querySign, variable_values=paramsSign) + print(colored("Transaction effectué avec succès !", "green")) + except Exception as e: + message = ast.literal_eval(str(e))["message"] + sys.stderr.write("Echec de la transaction:\n" + message + "\n") + sys.exit(1) + + def send(self): + self.genDoc() + self.checkTXDoc() + self.signDoc() + self.sendTXDoc() diff --git a/pay.py b/pay.py index 1acce79..859ef44 100755 --- a/pay.py +++ b/pay.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 import sys -from libs.paylib import sendGenDoc, checkTXDoc, signDoc, sendTXDoc +from libs.paylibClass import Transaction # Get args try: @@ -9,17 +9,13 @@ try: amount = int(sys.argv[2]) if len(sys.argv) > 3: comment = sys.argv[3] + else: + comment = "" except Exception as e: print("Please enter the recipient's public and the amount\n" + str(e)) sys.exit(1) -# Execute workflow -## Generate TX document from server and check it -returnGen = sendGenDoc(recipient, amount, comment='') -docIsOK = checkTXDoc(returnGen, recipient, amount, comment='') - -# Confirm TX document is ok, sign and send it -if docIsOK: - returnSigned = signDoc(returnGen) - sendTXDoc(returnSigned) +# Create transaction and send it +trans = Transaction(recipient, amount, comment) +trans.send()