Add listWallet command

This commit is contained in:
poka 2021-08-27 05:02:16 +02:00
parent a6f26331d7
commit 116b362ea9
3 changed files with 91 additions and 3 deletions

View File

@ -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:

View File

@ -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)
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)

74
lib/gvaWallets.py Normal file
View File

@ -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)