Changes transactions supported

This commit is contained in:
poka 2020-11-19 02:51:35 +01:00
parent 98ccc9b6ae
commit 550a0a68ef
1 changed files with 23 additions and 7 deletions

View File

@ -19,12 +19,13 @@ transport = AIOHTTPTransport(url=node)
client = Client(transport=transport, fetch_schema_from_transport=True)
class Transaction():
class Transaction:
def __init__(self, recipient, amount, comment=''):
self.recipient = recipient
self.amount = amount
self.comment = comment
self.issuer = get_privkey(dunikey, "pubsec").pubkey
self.isChange = False
if not re.match(r"(?![OIl])[1-9A-Za-z]{42,45}", self.recipient):
sys.stderr.write("Issuer pubkey is not to good format.\n")
@ -34,11 +35,12 @@ class Transaction():
# Build TX generation document
queryBuild = gql(
"""
query ($recipient: String!, $issuer: String!, $amount: Int!, $comment: String!){ genTxs(
query ($recipient: String!, $issuer: String!, $amount: Int!, $comment: String!, $isChange: Boolean!){ genTxs(
amount: $amount
comment: $comment
issuer: $issuer
recipient: $recipient
useMempoolSources: $isChange
)
}
"""
@ -47,7 +49,8 @@ class Transaction():
"recipient": self.recipient,
"issuer": self.issuer,
"amount": self.amount,
"comment": self.comment
"comment": self.comment,
"isChange": self.isChange
}
# Send TX document
@ -73,12 +76,19 @@ class Transaction():
if re.search("Comment:", line):
commentRaw = line.split(': ', 1)[1]
if issuerRaw != self.issuer or int(outAmount) != self.amount or outPubkey != self.recipient or commentRaw != self.comment:
# Check if is change transaction
if issuerRaw == outPubkey:
print("Ce document contient une transaction de change")
self.isChange = True
# Check validity of the document
elif 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.")
self.isChange = False
return self.txDoc
@ -109,13 +119,19 @@ class Transaction():
# Send TX Signed document
try:
client.execute(querySign, variable_values=paramsSign)
print(colored("Transaction effectué avec succès !", "green"))
txResult = str(client.execute(querySign, variable_values=paramsSign))
except Exception as e:
message = ast.literal_eval(str(e))["message"]
sys.stderr.write("Echec de la transaction:\n" + message + "\n")
sys.exit(1)
else:
# print(txResult)
if self.isChange:
self.send()
else:
print(colored("Transaction effectué avec succès !", "green"))
return txResult
def send(self):
self.genDoc()
self.checkTXDoc()