jaklis/jaklis.py

419 lines
13 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2020-11-18 05:45:20 +01:00
2023-09-10 02:03:42 +02:00
import argparse
import sys
import os
import string
import random
from os.path import join, dirname
2020-11-18 05:45:20 +01:00
from shutil import copyfile
from dotenv import load_dotenv
2020-12-02 09:54:11 +01:00
from duniterpy.key import SigningKey
__version__ = "0.0.5"
2020-11-18 05:45:20 +01:00
2023-09-09 09:38:22 +02:00
MY_PATH = os.path.realpath(sys.argv[0]).replace("jaklis.py", "")
2020-12-05 03:15:05 +01:00
2023-09-10 02:03:42 +02:00
# Get environment variables
2023-09-09 09:38:22 +02:00
if not os.path.isfile(MY_PATH + ".env"):
copyfile(MY_PATH + ".env.template", MY_PATH + ".env")
dotenv_path = join(dirname(__file__), MY_PATH + ".env")
load_dotenv(dotenv_path)
2023-09-10 02:03:42 +02:00
# Set global values (default parameters) regarding environment variables
node = (
os.getenv("DUNITER") + "/gva"
if os.getenv("DUNITER")
else "https://g1v1.p2p.legal/gva"
)
pod = os.getenv("ESNODE") if os.getenv("ESNODE") else "https://g1.data.e-is.pro"
destPubkey = False
2020-11-18 06:38:35 +01:00
# Parse arguments
2023-09-09 09:38:22 +02:00
parser = argparse.ArgumentParser(
2023-09-10 02:03:42 +02:00
description="CLI Client for Cesium+ and Ḡchange",
2023-09-09 09:38:22 +02:00
epilog="current node: '" + node + "', current pod: '" + pod + "'.",
)
parser.add_argument(
"-v",
"--version",
action="store_true",
2023-09-10 02:03:42 +02:00
help="Display the current program version",
2023-09-09 09:38:22 +02:00
)
2023-09-10 02:03:42 +02:00
parser.add_argument("-k", "--key", help="Path to the keyfile (PubSec)")
2023-09-09 09:38:22 +02:00
parser.add_argument(
2023-09-10 02:03:42 +02:00
"-n", "--node", help="Address of the Cesium+, Gchange, or Duniter node to use"
)
# Create subparsers for different commands
subparsers = parser.add_subparsers(title="jaklis Commands", dest="cmd")
read_cmd = subparsers.add_parser("read", help="Read messages")
send_cmd = subparsers.add_parser("send", help="Send a message")
delete_cmd = subparsers.add_parser("delete", help="Delete a message")
getProfile_cmd = subparsers.add_parser("get", help="View a Cesium+ profile")
getPage_cmd = subparsers.add_parser("page", help="View a Cesium+ page")
setProfile_cmd = subparsers.add_parser("set", help="Configure your Cesium+ profile")
eraseProfile_cmd = subparsers.add_parser("erase", help="Erase your Cesium+ profile")
2023-09-09 09:38:22 +02:00
stars_cmd = subparsers.add_parser(
2023-09-10 02:03:42 +02:00
"stars", help="View a profile's stars / Rate a profile (option -s RATING)"
2023-09-09 09:38:22 +02:00
)
2023-09-10 02:03:42 +02:00
unstars_cmd = subparsers.add_parser("unstars", help="Remove a star")
2023-09-09 09:38:22 +02:00
getoffer_cmd = subparsers.add_parser(
2023-09-10 02:03:42 +02:00
"getoffer", help="Get information about a Ḡchange listing"
2023-09-09 09:38:22 +02:00
)
2023-09-10 02:03:42 +02:00
setoffer_cmd = subparsers.add_parser("setoffer", help="Create a Ḡchange listing")
deleteoffer_cmd = subparsers.add_parser("deleteoffer", help="Delete a Ḡchange listing")
pay_cmd = subparsers.add_parser("pay", help="Pay in Ḡ1")
2023-09-09 09:38:22 +02:00
history_cmd = subparsers.add_parser(
2023-09-10 02:03:42 +02:00
"history", help="View Ḡ1 account transaction history"
2023-09-09 09:38:22 +02:00
)
2023-09-10 02:03:42 +02:00
balance_cmd = subparsers.add_parser("balance", help="View Ḡ1 account balance")
id_cmd = subparsers.add_parser("id", help="View public key/username identity")
2023-09-09 09:38:22 +02:00
id_balance_cmd = subparsers.add_parser(
2023-09-10 02:03:42 +02:00
"idBalance", help="View public key/username identity and balance"
2023-09-09 09:38:22 +02:00
)
currentUd = subparsers.add_parser(
2023-09-10 02:03:42 +02:00
"currentUd", help="Display the current Universal Dividend amount"
2023-09-09 09:38:22 +02:00
)
2023-09-10 02:03:42 +02:00
listWallets = subparsers.add_parser("listWallets", help="List all G1 wallets")
2023-09-09 09:38:22 +02:00
geolocProfiles = subparsers.add_parser(
2023-09-10 02:03:42 +02:00
"geolocProfiles", help="Get JSON of all geolocated accounts"
2023-09-09 09:38:22 +02:00
)
2020-11-20 03:04:09 +01:00
2023-09-10 02:03:42 +02:00
# Messaging management commands
2023-09-09 09:38:22 +02:00
read_cmd.add_argument(
2023-09-10 02:03:42 +02:00
"-n", "--number", type=int, default=3, help="Display the last NUMBER messages"
2023-09-09 09:38:22 +02:00
)
2023-09-10 02:03:42 +02:00
read_cmd.add_argument("-j", "--json", action="store_true", help="Output in JSON format")
read_cmd.add_argument("-o", "--outbox", action="store_true", help="Read sent messages")
2023-09-09 09:38:22 +02:00
send_cmd.add_argument(
2023-09-10 02:03:42 +02:00
"-d", "--destinataire", required=True, help="Recipient of the message"
2023-09-09 09:38:22 +02:00
)
2023-09-10 02:03:42 +02:00
send_cmd.add_argument("-t", "--titre", help="Title of the message to send")
send_cmd.add_argument("-m", "--message", help="Message to send")
send_cmd.add_argument("-f", "--fichier", help="Send the message from the 'FILE'")
2023-09-09 09:38:22 +02:00
send_cmd.add_argument(
2023-09-10 02:03:42 +02:00
"-o", "--outbox", action="store_true", help="Send the message to the outbox"
2023-09-09 09:38:22 +02:00
)
delete_cmd.add_argument(
"-i",
"--id",
action="append",
nargs="+",
required=True,
2023-09-10 02:03:42 +02:00
help="ID(s) of the message(s) to delete",
2023-09-09 09:38:22 +02:00
)
delete_cmd.add_argument(
2023-09-10 02:03:42 +02:00
"-o", "--outbox", action="store_true", help="Delete a sent message"
2023-09-09 09:38:22 +02:00
)
2020-11-20 03:04:09 +01:00
2023-09-10 02:03:42 +02:00
# Profile management commands
setProfile_cmd.add_argument("-n", "--name", help="Profile name")
setProfile_cmd.add_argument("-d", "--description", help="Profile description")
setProfile_cmd.add_argument("-v", "--ville", help="Profile city")
setProfile_cmd.add_argument("-a", "--adresse", help="Profile address")
2023-09-09 09:38:22 +02:00
setProfile_cmd.add_argument(
2023-09-10 02:03:42 +02:00
"-pos", "--position", nargs=2, help="Geographical coordinates (lat + lon)"
2023-09-09 09:38:22 +02:00
)
2023-09-10 02:03:42 +02:00
setProfile_cmd.add_argument("-s", "--site", help="Profile website")
setProfile_cmd.add_argument("-A", "--avatar", help="Path to profile avatar in PNG")
2023-09-09 09:38:22 +02:00
2023-09-10 02:03:42 +02:00
getProfile_cmd.add_argument("-p", "--profile", help="Profile name")
2023-09-09 09:38:22 +02:00
getProfile_cmd.add_argument(
"-a",
"--avatar",
action="store_true",
2023-09-10 02:03:42 +02:00
help="Also retrieve the avatar in raw base64 format",
2023-09-09 09:38:22 +02:00
)
2023-09-10 02:03:42 +02:00
getPage_cmd.add_argument("-p", "--page", help="Page name")
2023-09-09 09:38:22 +02:00
getPage_cmd.add_argument(
"-a",
"--avatar",
action="store_true",
2023-09-10 02:03:42 +02:00
help="Also retrieve the page's avatar in raw base64 format",
2023-09-09 09:38:22 +02:00
)
2022-10-23 03:31:27 +02:00
2023-09-10 02:03:42 +02:00
# Likes management commands
stars_cmd.add_argument("-p", "--profile", help="Target profile")
stars_cmd.add_argument("-n", "--number", type=int, help="Number of stars")
unstars_cmd.add_argument("-p", "--profile", help="Profile to unstar")
2020-12-02 08:04:04 +01:00
2023-09-10 02:03:42 +02:00
# Offers management commands
getoffer_cmd.add_argument("-i", "--id", help="Target listing to retrieve")
setoffer_cmd.add_argument("-t", "--title", help="Title of the listing to create")
2023-09-09 09:38:22 +02:00
setoffer_cmd.add_argument(
2023-09-10 02:03:42 +02:00
"-d", "--description", help="Description of the listing to create"
2023-09-09 09:38:22 +02:00
)
2023-09-10 02:03:42 +02:00
setoffer_cmd.add_argument("-c", "--category", help="Category of the listing to create")
2023-09-09 09:38:22 +02:00
setoffer_cmd.add_argument(
"-l",
"--localisation",
nargs=2,
2023-09-10 02:03:42 +02:00
help="Location of the listing to create (lat + lon)",
2023-09-09 09:38:22 +02:00
)
2023-09-10 02:03:42 +02:00
setoffer_cmd.add_argument("-p", "--picture", help="Image of the listing to create")
setoffer_cmd.add_argument("-ci", "--city", help="City of the listing to create")
setoffer_cmd.add_argument("-pr", "--price", help="Price of the listing to create")
deleteoffer_cmd.add_argument("-i", "--id", help="Target listing to delete")
# GVA usage commands
pay_cmd.add_argument("-p", "--pubkey", help="Payment recipient")
pay_cmd.add_argument("-a", "--amount", type=float, help="Transaction amount")
2023-09-09 09:38:22 +02:00
pay_cmd.add_argument(
2023-09-10 02:03:42 +02:00
"-c", "--comment", default="", help="Transaction comment", nargs="*"
2023-09-09 09:38:22 +02:00
)
2023-09-10 02:03:42 +02:00
pay_cmd.add_argument("-m", "--mempool", action="store_true", help="Use mempool sources")
2023-09-09 09:38:22 +02:00
pay_cmd.add_argument(
"-v",
"--verbose",
action="store_true",
2023-09-10 02:03:42 +02:00
help="Display the JSON result of the transaction",
2023-09-09 09:38:22 +02:00
)
2023-09-10 02:03:42 +02:00
history_cmd.add_argument("-p", "--pubkey", help="Public key of the target account")
2023-09-09 09:38:22 +02:00
history_cmd.add_argument(
"-n",
"--number",
type=int,
default=10,
2023-09-10 02:03:42 +02:00
help="Display the last NUMBER transactions",
2023-09-09 09:38:22 +02:00
)
history_cmd.add_argument(
2023-09-10 02:03:42 +02:00
"-j", "--json", action="store_true", help="Display the result in JSON format"
2023-09-09 09:38:22 +02:00
)
history_cmd.add_argument(
2023-09-10 02:03:42 +02:00
"--nocolors", action="store_true", help="Display the result in black and white"
2023-09-09 09:38:22 +02:00
)
2023-09-10 02:03:42 +02:00
balance_cmd.add_argument("-p", "--pubkey", help="Public key of the target account")
2023-09-09 09:38:22 +02:00
balance_cmd.add_argument(
2023-09-10 02:03:42 +02:00
"-m", "--mempool", action="store_true", help="Use mempool sources"
2023-09-09 09:38:22 +02:00
)
2023-09-10 02:03:42 +02:00
id_cmd.add_argument("-p", "--pubkey", help="Public key of the target account")
id_cmd.add_argument("-u", "--username", help="Username of the target account")
id_balance_cmd.add_argument("-p", "--pubkey", help="Public key of the target account")
currentUd.add_argument("-p", "--pubkey", help="Public key of the target account")
2023-09-09 09:38:22 +02:00
listWallets.add_argument(
2023-09-10 02:03:42 +02:00
"-b", "--balance", action="store_true", help="Display balances"
2023-09-09 09:38:22 +02:00
)
listWallets.add_argument(
2023-09-10 02:03:42 +02:00
"--mbr", action="store_true", help="Display raw list of member pubkeys"
2023-09-09 09:38:22 +02:00
)
listWallets.add_argument(
"--non_mbr",
action="store_true",
2023-09-10 02:03:42 +02:00
help="Display raw list of non-member identity pubkeys",
2023-09-09 09:38:22 +02:00
)
listWallets.add_argument(
2023-09-10 02:03:42 +02:00
"--larf", action="store_true", help="Display raw list of non-member pubkeys"
2023-09-09 09:38:22 +02:00
)
listWallets.add_argument(
2023-09-10 02:03:42 +02:00
"--brut", action="store_true", help="Display raw list of all pubkeys"
2023-09-09 09:38:22 +02:00
)
2023-09-10 02:03:42 +02:00
listWallets.add_argument("-p", "--pubkey", help="Useless but needed")
2020-11-18 05:45:20 +01:00
args = parser.parse_args()
2020-12-05 01:34:30 +01:00
cmd = args.cmd
2020-11-18 05:45:20 +01:00
2020-11-22 03:09:30 +01:00
if args.version:
2023-09-09 09:38:22 +02:00
print(__version__)
sys.exit(0)
2020-11-22 03:09:30 +01:00
if not cmd:
parser.print_help()
sys.exit(1)
2023-09-09 09:38:22 +02:00
2020-12-02 09:54:11 +01:00
def createTmpDunikey():
2023-09-10 02:03:42 +02:00
# Generate a pseudo-random nonce
nonce = "".join(
random.choice(string.ascii_letters + string.digits) for _ in range(32)
)
2020-12-02 09:54:11 +01:00
keyPath = "/tmp/secret.dunikey-" + nonce
2023-09-10 02:03:42 +02:00
# Create a dummy key (replace with actual key creation logic)
2023-09-09 09:38:22 +02:00
key = SigningKey.from_credentials(
"sgse547yhd54xv6541srdh", "sfdgwdrhpkxdawsbszqpof1sdg65xc", None
)
2020-12-02 09:54:11 +01:00
key.save_pubsec_file(keyPath)
2020-12-02 09:54:11 +01:00
return keyPath
2023-09-09 09:38:22 +02:00
2023-09-10 02:03:42 +02:00
# Check if a dunikey is needed
2020-12-08 23:15:24 +01:00
try:
pubkey = args.pubkey
except:
pubkey = False
try:
profile = args.profile
except:
profile = False
2023-09-10 02:03:42 +02:00
if cmd in (
"history",
"balance",
"get",
"page",
"id",
"idBalance",
"listWallets",
) and (pubkey or profile):
2020-12-08 23:15:24 +01:00
noNeedDunikey = True
2020-12-02 09:54:11 +01:00
keyPath = False
2020-12-08 23:15:24 +01:00
try:
dunikey = args.pubkey
except:
dunikey = args.profile
2020-12-03 09:09:18 +01:00
else:
2020-12-08 23:15:24 +01:00
noNeedDunikey = False
if args.key:
dunikey = args.key
2020-12-03 09:09:18 +01:00
keyPath = False
2020-12-08 23:15:24 +01:00
else:
2023-09-09 09:38:22 +02:00
dunikey = os.getenv("DUNIKEY")
2020-12-08 23:15:24 +01:00
if not dunikey:
keyPath = createTmpDunikey()
dunikey = keyPath
else:
keyPath = False
2020-12-03 09:09:18 +01:00
if not os.path.isfile(dunikey):
2020-12-08 23:15:24 +01:00
HOME = os.getenv("HOME")
dunikey = HOME + dunikey
if not os.path.isfile(dunikey):
2023-09-10 02:03:42 +02:00
sys.stderr.write("The keyfile {0} is not found.\n".format(dunikey))
2020-12-08 23:15:24 +01:00
sys.exit(1)
2020-12-02 09:54:11 +01:00
2023-09-10 02:03:42 +02:00
# Construct the CesiumPlus object
2023-09-09 09:38:22 +02:00
if cmd in (
"read",
"send",
"delete",
"set",
"get",
"page",
"erase",
"stars",
"unstars",
"getoffer",
"setoffer",
"deleteoffer",
"geolocProfiles",
):
from lib.cesium import CesiumPlus
2020-12-08 23:15:24 +01:00
if args.node:
pod = args.node
2020-12-08 23:15:24 +01:00
cesium = CesiumPlus(dunikey, pod, noNeedDunikey)
# Messaging
if cmd == "read":
cesium.read(args.number, args.outbox, args.json)
elif cmd == "send":
if args.fichier:
2023-09-09 09:38:22 +02:00
with open(args.fichier, "r") as f:
msgT = f.read()
2023-09-09 09:38:22 +02:00
titre = msgT.splitlines(True)[0].replace("\n", "")
msg = "".join(msgT.splitlines(True)[1:])
if args.titre:
titre = args.titre
msg = msgT
elif args.titre and args.message:
titre = args.titre
msg = args.message
else:
2023-09-10 02:03:42 +02:00
titre = input("Enter the message title: ")
msg = input("Enter the message content: ")
cesium.send(titre, msg, args.destinataire, args.outbox)
elif cmd == "delete":
cesium.delete(args.id[0], args.outbox)
# Profiles
elif cmd == "set":
2023-09-09 09:38:22 +02:00
cesium.set(
args.name,
args.description,
args.ville,
args.adresse,
args.position,
args.site,
args.avatar,
)
elif cmd == "get":
cesium.get(args.profile, args.avatar)
2022-10-23 03:31:27 +02:00
elif cmd == "page":
cesium.getPage(args.page, args.avatar)
elif cmd == "erase":
cesium.erase()
2023-09-09 09:38:22 +02:00
elif cmd == "geolocProfiles":
cesium.geolocProfiles(node)
2020-12-15 19:53:25 +01:00
# Stars
elif cmd == "stars":
if args.number or args.number == 0:
cesium.like(args.number, args.profile)
else:
cesium.readLikes(args.profile)
2020-12-15 19:53:25 +01:00
elif cmd == "unstars":
cesium.unLike(args.profile)
2021-02-14 18:18:01 +01:00
# Offers
elif cmd == "getoffer":
cesium.getOffer(args.id)
elif cmd == "setoffer":
2023-09-09 09:38:22 +02:00
cesium.setOffer(
args.title,
args.description,
args.city,
args.localisation,
args.category,
args.price,
args.picture,
)
2021-02-14 18:18:01 +01:00
elif cmd == "deleteoffer":
cesium.deleteOffer(args.id)
2023-09-10 02:03:42 +02:00
# Construct the GvaApi object
2023-09-09 09:38:22 +02:00
elif cmd in (
"pay",
"history",
"balance",
"id",
"idBalance",
"currentUd",
"listWallets",
):
from lib.gva import GvaApi
if args.node:
node = args.node
if args.pubkey:
destPubkey = args.pubkey
2020-12-08 23:15:24 +01:00
gva = GvaApi(dunikey, node, destPubkey, noNeedDunikey)
if cmd == "pay":
gva.pay(args.amount, args.comment, args.mempool, args.verbose)
elif cmd == "history":
2020-12-15 21:19:36 +01:00
gva.history(args.json, args.nocolors, args.number)
elif cmd == "balance":
gva.balance(args.mempool)
elif cmd == "id":
gva.id(args.pubkey, args.username)
2021-02-26 04:11:02 +01:00
elif cmd == "idBalance":
gva.idBalance(args.pubkey)
2021-06-30 06:10:48 +02:00
elif cmd == "currentUd":
gva.currentUd()
2021-08-27 05:02:16 +02:00
elif cmd == "listWallets":
2021-08-28 02:47:20 +02:00
gva.listWallets(args.brut, args.mbr, args.non_mbr, args.larf)
2020-12-02 08:04:04 +01:00
2020-12-02 09:54:11 +01:00
if keyPath:
os.remove(keyPath)