From 116b362ea9914b005c0d9c040786a6982bc4064d Mon Sep 17 00:00:00 2001 From: poka Date: Fri, 27 Aug 2021 05:02:16 +0200 Subject: [PATCH] Add listWallet command --- jaklis.py | 9 ++++-- lib/gva.py | 11 ++++++- lib/gvaWallets.py | 74 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 lib/gvaWallets.py diff --git a/jaklis.py b/jaklis.py index 8b189ee..540fea6 100755 --- a/jaklis.py +++ b/jaklis.py @@ -51,6 +51,7 @@ balance_cmd = subparsers.add_parser('balance', help="Voir le solde d'un compte id_cmd = subparsers.add_parser('id', help="Voir l'identité d'une clé publique/username") id_balance_cmd = subparsers.add_parser('idBalance', help="Voir l'identité d'une clé publique/username et son solde") currentUd = subparsers.add_parser('currentUd', help="Affiche la montant actuel du dividende Universel") +listWallets = subparsers.add_parser('listWallets', help="Liste de toutes les portefeuilles G1") # Messages management read_cmd.add_argument('-n', '--number',type=int, default=3, help="Affiche les NUMBER derniers messages") @@ -112,7 +113,9 @@ id_cmd.add_argument('-p', '--pubkey', help="Clé publique du compte visé") id_cmd.add_argument('-u', '--username', help="Username du compte visé") id_balance_cmd.add_argument('-p', '--pubkey', help="Pubkey du compte visé") currentUd.add_argument('-p', '--pubkey', help="Pubkey du compte visé") - +listWallets.add_argument('-b', '--balance', action='store_true', help="Affiche les soldes") +listWallets.add_argument('--brut', action='store_true', help="Affiche une liste de pubkey brut") +listWallets.add_argument('-p', '--pubkey', help="useless but needed") args = parser.parse_args() cmd = args.cmd @@ -234,7 +237,7 @@ if cmd in ("read","send","delete","set","get","erase","stars","unstars","getoffe cesium.deleteOffer(args.id) # Construct GVA object -elif cmd in ("pay","history","balance","id","idBalance","currentUd"): +elif cmd in ("pay","history","balance","id","idBalance","currentUd","listWallets"): from lib.gva import GvaApi if args.node: @@ -257,6 +260,8 @@ elif cmd in ("pay","history","balance","id","idBalance","currentUd"): gva.idBalance(args.pubkey) elif cmd == "currentUd": gva.currentUd() + elif cmd == "listWallets": + gva.listWallets(args.balance, args.brut) if keyPath: diff --git a/lib/gva.py b/lib/gva.py index c4173c9..4b02cdb 100755 --- a/lib/gva.py +++ b/lib/gva.py @@ -1,4 +1,5 @@ from lib.currentUd import currentUd +from lib.gvaWallets import ListWallets import sys, re from lib.natools import get_privkey from lib.gvaPay import Transaction, PUBKEY_REGEX @@ -73,4 +74,12 @@ class GvaApi(): def currentUd(self): gva = currentUd(self.node) result = gva.sendDoc() - print(result) \ No newline at end of file + print(result) + + def listWallets(self, getBalance, brut): + gva = ListWallets(self.node, getBalance, brut) + result = gva.sendDoc(getBalance, brut) + if brut: + print("\n".join(result)) + else: + print(result) \ No newline at end of file diff --git a/lib/gvaWallets.py b/lib/gvaWallets.py new file mode 100644 index 0000000..a7fdccb --- /dev/null +++ b/lib/gvaWallets.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 + +import sys, re, os.path, json, ast +from termcolor import colored +from lib.natools import fmt, sign, get_privkey +from gql import gql, Client +from gql.transport.aiohttp import AIOHTTPTransport + +PUBKEY_REGEX = "(?![OIl])[1-9A-Za-z]{42,45}" + +class ListWallets: + + def __init__(self, node, getBalance, brut): + self.getBalance = getBalance + self.brut = brut + # Define Duniter GVA node + transport = AIOHTTPTransport(url=node) + self.client = Client(transport=transport, fetch_schema_from_transport=True) + + def sendDoc(self, getBalance=True, brut=False): + # Build wallets generation document + + queryBuild = gql( + """ + { + wallets(pagination: { cursor: "1NiFHXUQDVNXuKE54Q8SdQRWmtKPVtMqWBSb8d8VkiS", ord: ASC, pageSize: 0 }) { + pageInfo { + hasNextPage + endCursor + } + edges { + node { + script + balance { + amount + base + } + idty { + isMember + username + } + } + } + } + } + """) + + # Send wallets document + try: + queryResult = self.client.execute(queryBuild) + except Exception as e: + sys.stderr.write("Echec de récupération du solde:\n" + str(e) + "\n") + sys.exit(1) + + jsonBrut = queryResult['wallets']['edges'] + + walletList = [] + if (brut): + names = [] + for dictionary in jsonBrut: + dataWork = dictionary['node'] + if "script" in dataWork: + names.append(dataWork["script"]) + + return names + else: + for i, trans in enumerate(jsonBrut): + dataWork = trans['node'] + walletList.append(i) + walletList[i] = {} + walletList[i]['pubkey'] = dataWork['script'] + walletList[i]['id'] = dataWork['idty'] + + return json.dumps(walletList, indent=2)