diff --git a/Pipfile b/Pipfile index d42dd9c0734eadd50fd91b585e440a348a7b76be..f24270df2fc4ec7b900d16a5b992ca3907c6a455 100644 --- a/Pipfile +++ b/Pipfile @@ -4,7 +4,7 @@ verify_ssl = true name = "pypi" [packages] -commandlines = "*" +click = "*" ipaddress = "*" tabulate = "*" scrypt = "*" diff --git a/Pipfile.lock b/Pipfile.lock index cbdf599928ea2d2729e1f51738f13e4622a0d6d4..1fda84400b8342d5233f4739915dbe97a6f7588a 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "44e71031df1ac2e4bd25e523804020e5d05d2d8b71a33d0d84f2a296523345e1" + "sha256": "cd8383687b27bd13d038a1d59ead6e80bd301a266dcdaf6eeb4f330e36cd629d" }, "pipfile-spec": 6, "requires": { @@ -53,13 +53,13 @@ ], "version": "==1.11.5" }, - "commandlines": { + "click": { "hashes": [ - "sha256:86b650b78470ac95966d7b1a9d215c16591bccb34b28ae2bb9026c3b4166fd64", - "sha256:b7dbf71b8dec42c16e9694b87e0e121d288e5a40d5d4f0dd1c0651ab7af06837" + "sha256:2335065e6395b9e67ca716de5f7526736bfa6ceead690adf616d925bdc622b13", + "sha256:5b94b49521f6456670fdb30cd82a4eca9412788a93fa6dd6df72c94d5a8ff2d7" ], "index": "pypi", - "version": "==0.4.1" + "version": "==7.0" }, "e1839a8": { "editable": true, @@ -84,7 +84,6 @@ "hashes": [ "sha256:a988718abfad80b6b157acce7bf130a30876d27603738ac39f140993246b25b3" ], - "markers": "python_version >= '2.7' and python_version != '3.1.*' and python_version != '3.2.*' and python_version != '3.3.*' and python_version != '3.0.*'", "version": "==2.19" }, "pynacl": { @@ -110,7 +109,6 @@ "sha256:f67814c38162f4deb31f68d590771a29d5ae3b1bd64b75cf232308e5c74777e0" ], "index": "pypi", - "markers": "python_version >= '2.7' and python_version != '3.1.*' and python_version != '3.2.*' and python_version != '3.3.*' and python_version != '3.0.*'", "version": "==1.3.0" }, "scrypt": { @@ -133,10 +131,10 @@ }, "six": { "hashes": [ - "sha256:70e8a77beed4562e7f14fe23a786b54f6296e34344c23bc42f07b15018ff98e9", - "sha256:832dc0e10feb1aa2c68dcc57dbb658f1c7e65b9b61af69048abc87a2db00a0eb" + "sha256:3350809f0555b11f552448330d0b52d5f24c91a322ea4a15ef22629740f3761c", + "sha256:d16a0141ec1a18405cd4ce8b4613101da75da0e9a7aec5bdd4fa804d0e0eba73" ], - "version": "==1.11.0" + "version": "==1.12.0" }, "tabulate": { "hashes": [ @@ -192,17 +190,17 @@ }, "certifi": { "hashes": [ - "sha256:339dc09518b07e2fa7eda5450740925974815557727d6bd35d319c1524a04a4c", - "sha256:6d58c986d22b038c8c0df30d639f23a3e6d172a05c3583e766f4c0b785c0986a" + "sha256:47f9c83ef4c0c621eaef743f133f09fa8a74a9b75f037e8624f83bd1b6626cb7", + "sha256:993f830721089fef441cdfeb4b2c8c9df86f0c63239f06bd025a76a7daddb033" ], - "version": "==2018.10.15" + "version": "==2018.11.29" }, "cfgv": { "hashes": [ - "sha256:73f48a752bd7aab103c4b882d6596c6360b7aa63b34073dd2c35c7b4b8f93010", - "sha256:d1791caa9ff5c0c7bce80e7ecc1921752a2eb7c2463a08ed9b6c96b85a2f75aa" + "sha256:c7c332338fc9936b38aea5916020caeb5d34e40774992893fc4353957e1394bd", + "sha256:d25d51bd5e8514dcb55fb07c773db316a2ee9db0c07aefc028c8a7a7b1e45321" ], - "version": "==1.1.0" + "version": "==1.2.0" }, "chardet": { "hashes": [ @@ -216,7 +214,7 @@ "sha256:2335065e6395b9e67ca716de5f7526736bfa6ceead690adf616d925bdc622b13", "sha256:5b94b49521f6456670fdb30cd82a4eca9412788a93fa6dd6df72c94d5a8ff2d7" ], - "markers": "python_version != '3.2.*' and python_version != '3.0.*' and python_version != '3.3.*' and python_version != '3.1.*' and python_version >= '2.7'", + "index": "pypi", "version": "==7.0" }, "docutils": { @@ -229,25 +227,24 @@ }, "identify": { "hashes": [ - "sha256:5e956558a9a1e3b3891d7c6609fc9709657a11878af288ace484d1a46a93922b", - "sha256:623086059219cc7b86c77a3891f3700cb175d4ce02b8fb8802b047301d71e783" + "sha256:08826e68e39e7de53cc2ddd8f6228a4e463b4bacb20565e5301c3ec690e68d27", + "sha256:2364e24a7699fea0dc910e90740adbab43eef3746eeea4e016029c34123ce66d" ], - "version": "==1.1.7" + "version": "==1.1.8" }, "idna": { "hashes": [ - "sha256:156a6814fb5ac1fc6850fb002e0852d56c0c8d2531923a51032d1b70760e186e", - "sha256:684a38a6f903c1d71d6d5fac066b58d7768af4de2b832e426ec79c30daa94a16" + "sha256:c357b3f628cf53ae2c4c05627ecc484553142ca23264e593d327bcde5e9c3407", + "sha256:ea8b7f6188e6fa117537c3df7da9fc686d485087abf6ac197f9c46432f7e4a3c" ], - "version": "==2.7" + "version": "==2.8" }, "importlib-metadata": { "hashes": [ - "sha256:36b02c84f9001adf65209fefdf951be8e9014a95eab9938c0779ad5670359b1c", - "sha256:60b6481a72908c93ccb707abeb926fb5a15319b9e6f0b76639a718837ee12de0" + "sha256:a17ce1a8c7bff1e8674cb12c992375d8d0800c9190177ecf0ad93e0097224095", + "sha256:b50191ead8c70adfa12495fba19ce6d75f2e0275c14c5a7beb653d6799b512bd" ], - "markers": "python_version != '3.2' and python_version != '3.0' and python_version != '3.1' and python_version != '3.3' and python_version >= '2.7'", - "version": "==0.6" + "version": "==0.8" }, "nodeenv": { "hashes": [ @@ -264,18 +261,18 @@ }, "pre-commit": { "hashes": [ - "sha256:7542bd8ae1c58745175ea0a9295964ee82a10f7e18c4344f5e4c02bd85d02561", - "sha256:87f687da6a2651d5067cfec95b854b004e95b70143cbf2369604bb3acbce25ec" + "sha256:33bb9bf599c334d458fa9e311bde54e0c306a651473b6a36fdb36a61c8605c89", + "sha256:e233f5cf3230ae9ed9ada132e9cf6890e18cc937adc669353fb64394f6e80c17" ], "index": "pypi", - "version": "==1.12.0" + "version": "==1.13.0" }, "pygments": { "hashes": [ - "sha256:78f3f434bcc5d6ee09020f92ba487f95ba50f1e3ef83ae96b9d5ffa1bab25c5d", - "sha256:dbae1046def0efb574852fab9e90209b23f556367b5a320c0bcb871c77c3e8cc" + "sha256:5ffada19f6203563680669ee7f53b64dabbeb100eb51b61996085e99c03b284a", + "sha256:e8218dd399a61674745138520d0d4cf2621d7e032439341bc3f647bff125818d" ], - "version": "==2.2.0" + "version": "==2.3.1" }, "pyyaml": { "hashes": [ @@ -296,10 +293,10 @@ }, "requests": { "hashes": [ - "sha256:65b3a120e4329e33c9889db89c80976c5272f56ea92d3e74da8a463992e3ff54", - "sha256:ea881206e59f41dbd0bd445437d792e43906703fff75ca8ff43ccdb11f33f263" + "sha256:502a824f31acdacb3a35b6690b5fbf0bc41d63a24a45c4004352b0242707598e", + "sha256:7bf2a778576d825600030a110f3c0e3e8edc51dfaafe1c146e39a2027784957b" ], - "version": "==2.20.1" + "version": "==2.21.0" }, "requests-toolbelt": { "hashes": [ @@ -310,10 +307,10 @@ }, "six": { "hashes": [ - "sha256:70e8a77beed4562e7f14fe23a786b54f6296e34344c23bc42f07b15018ff98e9", - "sha256:832dc0e10feb1aa2c68dcc57dbb658f1c7e65b9b61af69048abc87a2db00a0eb" + "sha256:3350809f0555b11f552448330d0b52d5f24c91a322ea4a15ef22629740f3761c", + "sha256:d16a0141ec1a18405cd4ce8b4613101da75da0e9a7aec5bdd4fa804d0e0eba73" ], - "version": "==1.11.0" + "version": "==1.12.0" }, "toml": { "hashes": [ @@ -327,7 +324,6 @@ "sha256:3c4d4a5a41ef162dd61f1edb86b0e1c7859054ab656b2e7c7b77e7fbf6d9f392", "sha256:5b4d5549984503050883bc126280b386f5f4ca87e6c023c5d015655ad75bdebb" ], - "markers": "python_version >= '2.6' and python_version != '3.1.*' and python_version != '3.0.*'", "version": "==4.28.1" }, "twine": { @@ -343,16 +339,14 @@ "sha256:61bf29cada3fc2fbefad4fdf059ea4bd1b4a86d2b6d15e1c7c0b582b9752fe39", "sha256:de9529817c93f27c8ccbfead6985011db27bd0ddfcdb2d86f3f663385c6a9c22" ], - "markers": "python_version != '3.2.*' and python_version < '4' and python_version != '3.0.*' and python_version != '3.3.*' and python_version != '3.1.*' and python_version >= '2.7'", "version": "==1.24.1" }, "virtualenv": { "hashes": [ - "sha256:686176c23a538ecc56d27ed9d5217abd34644823d6391cbeb232f42bf722baad", - "sha256:f899fafcd92e1150f40c8215328be38ff24b519cd95357fa6e78e006c7638208" + "sha256:34b9ae3742abed2f95d3970acf4d80533261d6061b51160b197f84e5b4c98b4c", + "sha256:fa736831a7b18bd2bfeef746beb622a92509e9733d645952da136b0639cd40cd" ], - "markers": "python_version != '3.2.*' and python_version != '3.0.*' and python_version != '3.3.*' and python_version != '3.1.*' and python_version >= '2.7'", - "version": "==16.1.0" + "version": "==16.2.0" }, "webencodings": { "hashes": [ @@ -363,11 +357,18 @@ }, "wheel": { "hashes": [ - "sha256:196c9842d79262bb66fcf59faa4bd0deb27da911dbc7c6cdca931080eb1f0783", - "sha256:c93e2d711f5f9841e17f53b0e6c0ff85593f3b416b6eec7a9452041a59a42688" + "sha256:029703bf514e16c8271c3821806a1c171220cc5bdd325cbf4e7da1e056a01db6", + "sha256:1e53cdb3f808d5ccd0df57f964263752aa74ea7359526d3da6c02114ec1e1d44" ], "index": "pypi", - "version": "==0.32.2" + "version": "==0.32.3" + }, + "zipp": { + "hashes": [ + "sha256:55ca87266c38af6658b84db8cfb7343cdb0bf275f93c7afaea0d8e7a209c7478", + "sha256:682b3e1c62b7026afe24eadf6be579fb45fec54c07ea218bded8092af07a68c4" + ], + "version": "==0.3.3" } } } diff --git a/bin/silkaj b/bin/silkaj index b5986a045e002e1cc905563bde5e0434cd05a8c0..542ff7fb85067ce74b4d21f0c8108490055570be 100755 --- a/bin/silkaj +++ b/bin/silkaj @@ -2,12 +2,7 @@ from sys import exit -from silkaj.network_tools import check_port, best_node, EndPoint from silkaj.cli_tools import manage_cmd if __name__ == "__main__": - ep = EndPoint().ep - if not check_port(ep["port"]): - exit(1) - best_node(ep, True) manage_cmd() diff --git a/setup.py b/setup.py index be983bbe39bd3e94f3ff2726bbed4c5529082a53..91bbbfd1fb15f2296c83f08e2e3e3030d6b7632d 100644 --- a/setup.py +++ b/setup.py @@ -25,7 +25,7 @@ setuptools.setup( "Operating System :: OS Independent", ), install_requires=[ - "commandlines", + "Click", "ipaddress", "tabulate", "pynacl", diff --git a/silkaj/auth.py b/silkaj/auth.py index 23e9edec5763cdc930b97ef7c6fad43d92c21c88..e9c336f133dce43d4249156bfcd5be7f8fbe7b02 100644 --- a/silkaj/auth.py +++ b/silkaj/auth.py @@ -7,25 +7,25 @@ import pyaes from getpass import getpass from os import path from re import compile, search +import click -def auth_method(cli_args): - if cli_args.contains_switches("auth-seed"): +@click.pass_context +def auth_method(ctx): + if ctx.obj["AUTH_SEED"]: return auth_by_seed() - if cli_args.contains_switches("auth-file"): - return auth_by_auth_file(cli_args) - if cli_args.contains_switches("auth-wif"): + if ctx.obj["AUTH_FILE"]: + return auth_by_auth_file() + if ctx.obj["AUTH_WIF"]: return auth_by_wif() else: - return auth_by_scrypt(cli_args) + return auth_by_scrypt() -def generate_auth_file(cli_args): - if cli_args.contains_definitions("file"): - file = cli_args.get_definition("file") - else: +def generate_auth_file(file): + if not file: file = "authfile" - seed = auth_method(cli_args) + seed = auth_method() with open(file, "w") as f: f.write(seed) print( @@ -35,9 +35,10 @@ def generate_auth_file(cli_args): ) -def auth_by_auth_file(cli_args): - if cli_args.contains_definitions("file"): - file = cli_args.get_definition("file") +@click.pass_context +def auth_by_auth_file(ctx): + if ctx.obj["AUTH_FILE_PATH"]: + file = ctx.obj["AUTH_FILE_PATH"] else: file = "authfile" if not path.isfile(file): @@ -69,20 +70,13 @@ def auth_by_seed(): return seed -def auth_by_scrypt(cli_args): +@click.pass_context +def auth_by_scrypt(ctx): salt = getpass("Please enter your Scrypt Salt (Secret identifier): ") password = getpass("Please enter your Scrypt password (masked): ") - if ( - cli_args.contains_definitions("n") - and cli_args.contains_definitions("r") - and cli_args.contains_definitions("p") - ): - n, r, p = ( - cli_args.get_definition("n"), - cli_args.get_definition("r"), - cli_args.get_definition("p"), - ) + if ctx.obj["AUTH_SCRYPT_PARAMS"]: + n, r, p = ctx.obj["AUTH_SCRYPT_PARAMS"].split(",") if n.isnumeric() and r.isnumeric() and p.isnumeric(): n, r, p = int(n), int(r), int(p) if n <= 0 or n > 65536 or r <= 0 or r > 512 or p <= 0 or p > 32: diff --git a/silkaj/cert.py b/silkaj/cert.py index 4023f7dfc83f21313355cb2fb73358c28b7b40db..f821cc1f4dc3f104fe4b04da2bd1145dc00cdac9 100644 --- a/silkaj/cert.py +++ b/silkaj/cert.py @@ -11,15 +11,15 @@ from silkaj.license import license_approval from silkaj.wot import is_member, get_uid_from_pubkey, get_informations_for_identity -def send_certification(cli_args): - id_to_certify = get_informations_for_identity(cli_args.subsubcmd) +def send_certification(id_to_certify): + id_to_certify = get_informations_for_identity(id_to_certify) main_id_to_certify = id_to_certify["uids"][0] # Display license and ask for confirmation license_approval(HeadBlock().head_block["currency"]) # Authentication - seed = auth_method(cli_args) + seed = auth_method() # Check whether current user is member issuer_pubkey = get_publickey_from_seed(seed) diff --git a/silkaj/cli_tools.py b/silkaj/cli_tools.py index c97b263f78f63f49722a979ae14951d9c9739267..67c567bb00ca901bdd4c0f4fa24ea93198d77cc4 100644 --- a/silkaj/cli_tools.py +++ b/silkaj/cli_tools.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- +import click from sys import stderr -from commandlines import Command from silkaj.tx import send_transaction from silkaj.money import cmd_amount from silkaj.cert import send_certification @@ -27,6 +27,163 @@ from silkaj.constants import ( ) +@click.group() +@click.version_option(version=SILKAJ_VERSION) +@click.option( + "--peer", + "-p", + help="Default endpoint will reach Ğ1 currency with `https://g1.duniter.org` endpoint.\ + Custom endpoint can be specified with `-p` option followed by <domain>:<port>", +) +@click.option( + "--gtest", "-gt", is_flag=True, help="ĞTest: `https://g1-test.duniter.org` endpoint" +) +@click.option( + "--auth-scrypt", is_flag=True, help="Scrypt authentication: default method" +) +@click.option( + "--scrypt-params", + "--nrp", + help="Scrypt parameters seperated by commas: defaults N,r,p: 4096,16,1", +) +@click.option( + "--auth-file", is_flag=True, help="Authentication file. Defaults to: './authfile'" +) +@click.option("--file", help="Path file specification with '--auth-file'") +@click.option("--auth-seed", is_flag=True, help="Seed authentication") +@click.option("--auth-wif", is_flag=True, help="WIF and EWIF authentication methods") +@click.pass_context +def cli( + ctx, peer, gtest, auth_scrypt, scrypt_params, auth_file, file, auth_seed, auth_wif +): + ctx.obj = dict() + ctx.ensure_object(dict) + ctx.obj["PEER"] = peer + ctx.obj["GTEST"] = gtest + ctx.obj["AUTH_SCRYPT"] = auth_scrypt + ctx.obj["AUTH_SCRYPT_PARAMS"] = scrypt_params + ctx.obj["AUTH_FILE"] = auth_file + ctx.obj["AUTH_FILE_PATH"] = file + ctx.obj["AUTH_SEED"] = auth_seed + ctx.obj["AUTH_WIF"] = auth_wif + + +def manage_cmd(): + cli(obj={}) + + +@cli.command("about", help="Display informations about the programm") +def cliAbout(): + about() + + +@cli.command("amount", help="Get amount of pubkeys") +@click.option( + "--pubkeys", "-p", help="pubkeys and/or ids separated with colon: <pubkey:pubkey>" +) +def cliAmount(pubkeys): + cmd_amount(pubkeys) + + +@cli.command("argos", help="Display currency information formated for Argos or BitBar") +def cliArgos(): + argos_info() + + +@cli.command("blocks", help="Display blocks") +@click.option( + "--nbr", + "-n", + required=True, + type=int, + help="Number of blocks (`0` for current window size), Details blocks are displayed under n <= 30.", +) +@click.option("--detailed", "-d", is_flag=True, help="This flag force detailed view") +def cliBlocks(nbr, detailed): + list_issuers(nbr, detailed) + + +@cli.command("cert", help="Send certification") +@click.argument("id_to_certify") +def cliCert(id_to_certify): + send_certification(id_to_certify) + + +@cli.command("diffi", help="List proof-of-work difficulty to generate next block") +def cliDiffi(): + difficulties() + + +@cli.command( + "generate_auth_file", help="Generate file to store the seed of the account" +) +@click.option("--file", help="Path file") +def cliGenerateAuthFile(file): + generate_auth_file(file) + + +@cli.command("id", help="Get corresponding identity or pubkey from pubkey or identity") +@click.argument("id_pubkey") +def cliId(id_pubkey): + id_pubkey_correspondence(id_pubkey) + + +@cli.command("info", help="Display information about currency") +def cliInfo(): + currency_info() + + +@cli.command("license", help="Display Ğ1 license") +def cliLicense(): + display_license() + + +@cli.command("net", help="Display network") +@click.option( + "--discover", "-d", is_flag=True, help="Discover all network (could take a while)" +) +@click.option( + "--sort", + "-s", + help='Sort column names comma-separated (for example "-s block,diffi"), optional. Default sort: block,member,diffi,uid', +) +def cliNetwork(discover, sort): + network_info(discover, sort) + + +@cli.command("tx", help="Send transaction") +@click.option("--amount", type=float, help="Quantitative value") +@click.option("--amountUD", type=float, help="Relative value") +@click.option("--allSources", is_flag=True, help="Send all sources") +@click.option( + "--output", + help="Pubkey(s)’ recipients + optional checksum: <pubkey>[!checksum]:[<pubkey>[!checksum]]", +) +@click.option("--comment", help="Comment") +@click.option( + "--outputBackChange", + help="Pubkey recipient to send the rest of the transaction: <pubkey[!checksum]>", +) +@click.option("--yes", "-y", is_flag=True, help="Assume yes. No prompt confirmation") +def cliTransaction( + amount, amountud, allsources, output, comment, outputbackchange, yes +): + send_transaction( + amount, amountud, allsources, output, comment, outputbackchange, yes + ) + + +@cli.command("usage", help="Display usage") +def cliUsage(): + usage() + + +@cli.command("wot", help="Display received and sent certifications of an id") +@click.argument("id") +def cliWot(id): + received_sent_certifications(id) + + def usage(): message_exit( "Silkaj: command line client for Duniter currencies\ @@ -102,83 +259,6 @@ def usage(): ) -def manage_cmd(): - cli_args = Command() - if cli_args.is_version_request(): - message_exit(SILKAJ_VERSION) - - subcmd = [ - "license", - "about", - "info", - "diffi", - "net", - "network", - "issuers", - "argos", - "amount", - "tx", - "transaction", - "cert", - "generate_auth_file", - "id", - "identities", - "wot", - ] - if ( - cli_args.is_help_request() - or cli_args.is_usage_request() - or cli_args.subcmd not in subcmd - ): - usage() - - if cli_args.subcmd == "about": - about() - elif cli_args.subcmd == "info": - currency_info() - - elif cli_args.subcmd == "diffi": - difficulties() - - elif cli_args.subcmd == "net" or cli_args.subcmd == "network": - if cli_args.contains_switches("sort"): - set_network_sort_keys(cli_args.get_definition("sort")) - if cli_args.contains_switches("s"): - set_network_sort_keys(cli_args.get_definition("s")) - network_info(cli_args.contains_switches("discover")) - - elif ( - cli_args.subcmd == "issuers" - and cli_args.subsubcmd - and int(cli_args.subsubcmd) >= 0 - ): - list_issuers(int(cli_args.subsubcmd), cli_args.contains_switches("last")) - - elif cli_args.subcmd == "argos": - argos_info() - - elif cli_args.subcmd == "amount": - cmd_amount(cli_args) - - elif cli_args.subcmd == "tx" or cli_args.subcmd == "transaction": - send_transaction(cli_args) - - elif cli_args.subcmd == "cert": - send_certification(cli_args) - - elif cli_args.subcmd == "generate_auth_file": - generate_auth_file(cli_args) - - elif cli_args.subcmd == "id" or cli_args.subcmd == "identities": - id_pubkey_correspondence(cli_args.subsubcmd) - - elif cli_args.subcmd == "wot": - received_sent_certifications(cli_args.subsubcmd) - - elif cli_args.subcmd == "license": - display_license() - - def about(): print( "\ diff --git a/silkaj/commands.py b/silkaj/commands.py index 374a16d217f309c769f8a0bd2a31acf7b3269f11..89c9499aa2f165786e7ad1cd5244852121e1586e 100644 --- a/silkaj/commands.py +++ b/silkaj/commands.py @@ -126,6 +126,8 @@ network_sort_keys = ["block", "member", "diffi", "uid"] def set_network_sort_keys(some_keys): global network_sort_keys + if some_keys is None: + return if some_keys.endswith(","): message_exit( "Argument 'sort' ends with a comma, you have probably inserted a space after the comma, which is incorrect." @@ -143,7 +145,8 @@ def get_network_sort_key(endpoint): return tuple(t) -def network_info(discover): +def network_info(discover, sort): + set_network_sort_keys(sort) rows, columns = popen("stty size", "r").read().split() wide = int(columns) if wide < 146: @@ -222,7 +225,9 @@ def network_info(discover): print(tabulate(endpoints, headers="keys", tablefmt="orgtbl", stralign="center")) -def list_issuers(nbr, last): +def list_issuers(nbr, detailed): + if nbr < 0: + message_exit("The number of blocks should be positive") head_block = HeadBlock().head_block current_nbr = head_block["number"] if nbr == 0: @@ -233,7 +238,7 @@ def list_issuers(nbr, last): while j < len(blocks): issuer = OrderedDict() issuer["pubkey"] = blocks[j]["issuer"] - if last or nbr <= 30: + if detailed or nbr <= 30: issuer["block"] = blocks[j]["number"] issuer["gentime"] = convert_time(blocks[j]["time"], "hour") issuer["mediantime"] = convert_time(blocks[j]["medianTime"], "hour") @@ -258,7 +263,7 @@ def list_issuers(nbr, last): ), end=" ", ) - if last or nbr <= 30: + if detailed or nbr <= 30: sorted_list = sorted(list_issuers, key=itemgetter("block"), reverse=True) print( "\n" diff --git a/silkaj/money.py b/silkaj/money.py index 301cecbdc6dc6adb9e3cb37d8d13305967cbc8af..70f65dd55a44ae0d33731d66ccca470ec890dbec 100644 --- a/silkaj/money.py +++ b/silkaj/money.py @@ -1,3 +1,4 @@ +import click from silkaj.network_tools import get_request, HeadBlock from silkaj.crypto_tools import get_publickey_from_seed from silkaj.tools import CurrencySymbol @@ -5,9 +6,15 @@ from silkaj.auth import auth_method from silkaj.wot import check_public_key -def cmd_amount(cli_args): - if not cli_args.subsubcmd.startswith("--auth-"): - pubkeys = cli_args.subsubcmd.split(":") +@click.pass_context +def cmd_amount(ctx, pubkeys): + if not ( + ctx.obj["AUTH_SCRYPT"] + or ctx.obj["AUTH_FILE"] + or ctx.obj["AUTH_SEED"] + or ctx.obj["AUTH_WIF"] + ): + pubkeys = pubkeys.split(":") for pubkey in pubkeys: pubkey = check_public_key(pubkey, True) if not pubkey: @@ -21,7 +28,7 @@ def cmd_amount(cli_args): if len(pubkeys) > 1: show_amount_from_pubkey("Total", total) else: - seed = auth_method(cli_args) + seed = auth_method() pubkey = get_publickey_from_seed(seed) show_amount_from_pubkey(pubkey, get_amount_from_pubkey(pubkey)) diff --git a/silkaj/network_tools.py b/silkaj/network_tools.py index dd04f6da1d9dbac1c67b90f5b644e6483fb9a7bc..85e9860601c85054d8cd63dbc033e74c1ac58d64 100644 --- a/silkaj/network_tools.py +++ b/silkaj/network_tools.py @@ -5,7 +5,7 @@ import socket import urllib.request import logging from sys import exit, stderr -from commandlines import Command +import click from silkaj.constants import ( G1_DEFAULT_ENDPOINT, @@ -67,6 +67,7 @@ def parse_endpoints(rep): j = 0 return endpoints + class EndPoint(object): __instance = None @@ -76,15 +77,15 @@ class EndPoint(object): EndPoint.__instance = object.__new__(cls) return EndPoint.__instance - def __init__(self): - cli_args = Command() + @click.pass_context + def __init__(ctx, self): ep = dict() - if cli_args.contains_switches("p"): - ep["domain"], ep["port"] = cli_args.get_definition("p").rsplit(":", 1) + if ctx.obj["PEER"]: + ep["domain"], ep["port"] = ctx.obj["PEER"].rsplit(":", 1) else: ep["domain"], ep["port"] = ( G1_TEST_DEFAULT_ENDPOINT - if cli_args.contains_switches("gtest") + if ctx.obj["GTEST"] else G1_DEFAULT_ENDPOINT ) if ep["domain"].startswith("[") and ep["domain"].endswith("]"): @@ -136,7 +137,8 @@ def check_ip(address): return 0 -def get_request(path, ep=EndPoint().ep): +def get_request(path, ep=None): + ep = EndPoint().ep address = best_node(ep, False) if address is None: return address @@ -149,7 +151,8 @@ def get_request(path, ep=EndPoint().ep): return loads(response.read().decode(encoding)) -def post_request(path, postdata, ep=EndPoint().ep): +def post_request(path, postdata, ep=None): + ep = EndPoint().ep address = best_node(ep, False) if address is None: return address diff --git a/silkaj/tx.py b/silkaj/tx.py index 5f14c006e5b3acdd929923c68285ee2b00a27c7c..89bc50b7fdf7e4ae01cea24eb63badb77e7e2207 100644 --- a/silkaj/tx.py +++ b/silkaj/tx.py @@ -17,12 +17,16 @@ from silkaj.money import get_sources, get_amount_from_pubkey, UDValue from silkaj.constants import NO_MATCHING_ID -def send_transaction(cli_args): +def send_transaction( + amount, amountUD, allSources, output, comment, outputBackChange, yes +): """ Main function """ - tx_amount, output, comment, allSources, outputBackChange = cmd_transaction(cli_args) - seed = auth_method(cli_args) + tx_amount, comment = cmd_transaction( + amount, amountUD, allSources, output, comment, outputBackChange + ) + seed = auth_method() issuer_pubkey = get_publickey_from_seed(seed) pubkey_amount = get_amount_from_pubkey(issuer_pubkey)[0] @@ -36,8 +40,7 @@ def send_transaction(cli_args): ) if ( - cli_args.contains_switches("yes") - or cli_args.contains_switches("y") + yes or input( tabulate( transaction_confirmation( @@ -60,36 +63,22 @@ def send_transaction(cli_args): ) -def cmd_transaction(cli_args): +def cmd_transaction(amount, amountUD, allSources, output, comment, outputBackChange): """ - Retrieve values from command line interface + Check command line interface arguments """ - if not ( - cli_args.contains_definitions("amount") - or cli_args.contains_definitions("amountUD") - ): - message_exit("--amount or --amountUD is not set") - if not cli_args.contains_definitions("output"): + if not (amount or amountUD or allSources): + message_exit("--amount nor --amountUD nor --allSources is set") + if not output: message_exit("--output is not set") - - if cli_args.contains_definitions("amount"): - tx_amount = float(cli_args.get_definition("amount")) * 100 - if cli_args.contains_definitions("amountUD"): - tx_amount = float(cli_args.get_definition("amountUD")) * UDValue().ud_value - - output = cli_args.get_definition("output") - comment = ( - cli_args.get_definition("comment") - if cli_args.contains_definitions("comment") - else "" - ) - allSources = cli_args.contains_switches("allSources") - - if cli_args.contains_definitions("outputBackChange"): - outputBackChange = cli_args.get_definition("outputBackChange") - else: - outputBackChange = None - return tx_amount, output, comment, allSources, outputBackChange + if amount: + tx_amount = float(amount) * 100 + if amountUD: + tx_amount = float(amountUD) * UDValue().ud_value + if allSources: + tx_amount = 0 + comment = comment if comment else "" + return tx_amount, comment def check_transaction_values(