2020-12-12 20:59:40 +01:00
|
|
|
import sys, re
|
|
|
|
from lib.natools import get_privkey
|
|
|
|
from lib.gvaPay import Transaction, PUBKEY_REGEX
|
|
|
|
from lib.gvaHistory import History
|
|
|
|
from lib.gvaBalance import Balance
|
|
|
|
|
|
|
|
class GvaApi():
|
|
|
|
def __init__(self, dunikey, node, pubkey, noNeedDunikey=False):
|
|
|
|
self.noNeedDunikey = noNeedDunikey
|
|
|
|
self.dunikey = dunikey
|
|
|
|
self.node = node
|
|
|
|
if noNeedDunikey:
|
|
|
|
self.pubkey = self.dunikey
|
|
|
|
else:
|
|
|
|
self.pubkey = get_privkey(dunikey, "pubsec").pubkey
|
|
|
|
|
|
|
|
if pubkey:
|
|
|
|
self.destPubkey = pubkey
|
|
|
|
else:
|
|
|
|
self.destPubkey = self.pubkey
|
|
|
|
|
|
|
|
try:
|
|
|
|
if not re.match(PUBKEY_REGEX, self.pubkey) or len(self.pubkey) > 45:
|
|
|
|
raise ValueError("La clé publique n'est pas au bon format.")
|
|
|
|
except:
|
|
|
|
sys.stderr.write("La clé publique n'est pas au bon format.\n")
|
|
|
|
raise
|
|
|
|
|
|
|
|
try:
|
|
|
|
if not re.match(PUBKEY_REGEX, self.destPubkey) or len(self.destPubkey) > 45:
|
|
|
|
raise ValueError("La clé publique n'est pas au bon format.")
|
|
|
|
except:
|
|
|
|
sys.stderr.write("La clé publique n'est pas au bon format.\n")
|
|
|
|
raise
|
|
|
|
|
|
|
|
#################### Payments ####################
|
|
|
|
|
|
|
|
def pay(self, amount, comment, mempool, verbose):
|
|
|
|
gva = Transaction(self.dunikey, self.node, self.destPubkey, amount, comment, mempool, verbose)
|
|
|
|
gva.genDoc()
|
|
|
|
gva.checkTXDoc()
|
|
|
|
gva.signDoc()
|
|
|
|
return gva.sendTXDoc()
|
|
|
|
|
2021-02-24 16:13:19 +01:00
|
|
|
def history(self, isJSON=False, noColors=False, number=10):
|
2020-12-12 20:59:40 +01:00
|
|
|
gva = History(self.dunikey, self.node, self.destPubkey)
|
2021-02-24 16:13:19 +01:00
|
|
|
gva.sendDoc(number)
|
2020-12-12 20:59:40 +01:00
|
|
|
transList = gva.parseHistory()
|
|
|
|
|
|
|
|
if isJSON:
|
|
|
|
transJson = gva.jsonHistory(transList)
|
|
|
|
print(transJson)
|
|
|
|
else:
|
|
|
|
gva.printHistory(transList, noColors)
|
|
|
|
|
|
|
|
def balance(self, useMempool):
|
|
|
|
gva = Balance(self.dunikey, self.node, self.destPubkey, useMempool)
|
|
|
|
balanceValue = gva.sendDoc()
|
|
|
|
print(balanceValue)
|