Skip to content

Commit

Permalink
Merge pull request #69 from ltonetwork/seed-nonce
Browse files Browse the repository at this point in the history
Nonce for seed + account name as recipient
  • Loading branch information
jasny authored Oct 4, 2024
2 parents 4dda237 + 6137f38 commit f356717
Show file tree
Hide file tree
Showing 17 changed files with 151 additions and 92 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ __pycache__/
/test.txt
/test.txt
/src/lto_cli/testing.py
Pipfile.lock
9 changes: 5 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
language: python
python:
- '3.8'
- '3.9'
- '3.10'
dist: jammy

python:
- 3.10
- 3.9
- 3.8

env:
global:
- RECIPIENT=3MyGpJh6Bb8auF3HtSr2dSJjqQVxgqLynpK
Expand Down
19 changes: 19 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
lto = "*"
argparse = "*"
configparser = "*"
pathlib = "*"
importlib-metadata = "*"
setuptools = "*"

[dev-packages]
build = "*"

[requires]
python_version = "3.12"
python_full_version = "3.12.3"
4 changes: 3 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ install_requires =
configparser
pathlib
importlib-metadata~=4.8

include_package_data = True


[options.packages.find]
Expand All @@ -35,3 +35,5 @@ where = src
console_scripts =
lto = lto_cli.cli:main

[options.package_data]
* = header.txt
48 changes: 16 additions & 32 deletions src/lto_cli/cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import argparse
import sys
import os

from lto_cli import config
from lto_cli.commands import transfer
Expand All @@ -18,41 +19,23 @@
from importlib_metadata import version

# IF ERROR MODULE NOT FOUND:
# export PYTHONPATH=$PYTHONPATH:'pwd.../lto-api.python'
# export PYTHONPATH=$PYTHONPATH:'pwd.../lto-cli/src'

def main():
config.check_directory()
dir = os.path.dirname(os.path.abspath(__file__))

parser = argparse.ArgumentParser(prog='lto', description=''
' _____ _____ _______ \n'
' /\ \ /\ \ /::\ \ \n'
' /::\____\ /::\ \ /::::\ \ \n'
' /:::/ / \:::\ \ /::::::\ \ \n'
' /:::/ / \:::\ \ /::::::::\ \ \n'
' /:::/ / \:::\ \ /:::/~~\:::\ \ \n'
' /:::/ / \:::\ \ /:::/ \:::\ \ \n'
' /:::/ / /::::\ \ /:::/ / \:::\ \ \n'
' /:::/ / /::::::\ \ /:::/____/ \:::\____\ \n'
' /:::/ / /:::/\:::\ \ |:::| | |:::| | \n'
'/:::/____/ /:::/ \:::\____\ |:::|____| |:::|____| \n'
'\:::\ \ /:::/ \::/ / \:::\ \ /:::/ / \n'
' \:::\ \ /:::/ / \/____/ \:::\ \ /:::/ / \n'
' \:::\ \ /:::/ / \:::\ /:::/ / \n'
' \:::\ \ /:::/ / \:::\__/:::/ / \n'
' \:::\ \ \::/ / \::::::::/ / \n'
' \:::\ \ \/____/ \::::::/ / \n'
' \:::\ \ \::::/ / \n'
' \:::\____\ \::/____/ \n'
' \::/ / \n'
' \/____/ \n\n'
'LTO Network CLI client, visit the github page for more information https://github.com/ltonetwork/lto-cli',
usage=argparse.SUPPRESS, formatter_class=argparse.RawDescriptionHelpFormatter)
# Read file header.txt
with open(os.path.join(dir, 'header.txt'), 'r') as file:
header = file.read()

parser = argparse.ArgumentParser(prog='lto', description=header, usage=argparse.SUPPRESS, formatter_class=argparse.RawDescriptionHelpFormatter)

parser.add_argument('--version', action='store_true', required=False, help="Display the version of the package")
subparsers = parser.add_subparsers(dest='subparser-name', help='sub-command help')

# --------------------------------------------------------------
parser_data = subparsers.add_parser('data', help="Create a data transaction or get the data associated with one account, type 'lto data --help' for more informations")
parser_data = subparsers.add_parser('data', help="Create a data transaction or get the data associated with one account, type 'lto data --help' for more information")
data_subparser = parser_data.add_subparsers(dest='subparser-name-data')

parser_set = data_subparser.add_parser('set', help="Create a data transaction, for more information on how to pipe the data type 'lto data set --help")
Expand All @@ -72,10 +55,10 @@ def main():
parser_get.add_argument('--testnet', '-T', action='store_const', dest='network', const='T', required=False, help='Short for --network=T')

# --------------------------------------------------------------
parser_account = subparsers.add_parser('account', help="Create remove and manage accounts, type 'lto account --help' for more informations")
parser_account = subparsers.add_parser('account', help="Create remove and manage accounts, type 'lto account --help' for more information")
account_subparser = parser_account.add_subparsers(dest='subparser-name-account')

parser_create = account_subparser.add_parser('create', help="Allow to create an account with two optional parameter, --name and --network")
parser_create = account_subparser.add_parser('create', help="Create an account")
parser_create.add_argument('--name', required=False, type=str, nargs=1)
parser_create.add_argument('--network', type=str, nargs=1, required=False, help='Optional network parameter, if not specified default is L')
parser_create.add_argument('--testnet', '-T', action='store_const', dest='network', const='T', required=False, help='Short for --network=T')
Expand Down Expand Up @@ -106,10 +89,11 @@ def main():
parser_seed.add_argument('--name', required=False, type=str, nargs=1)
parser_seed.add_argument('--network', type=str, nargs=1, required=False, help ='Optional network parameter, if not specified default is L')
parser_seed.add_argument('--testnet', '-T', action='store_const', dest='network', const='T', required=False, help='Short for --network=T')
parser_seed.add_argument('--nonce', type=int, nargs=1, required=False, help ='You can create multiple accounts from a single seed phrase, by passing a nonce. Only a numeric nonce is supported.')

# --------------------------------------------------------------
parser_balance = subparsers.add_parser('balance', help="Get the account balance, if not specified the default account is selected")
parser_balance.add_argument('address', nargs='?', type=str, help='Insert the desired account address')
parser_balance.add_argument('address', nargs='?', type=str, help='Insert the desired account address or name')
parser_balance.add_argument('--account', type=str, nargs=1, required=False, help="The account can be identified by address or name. In addition, an address of an account not stored locally can also be used")
parser_balance.add_argument('--network', type=str, nargs=1, required=False, help ='Optional network parameter, if not specified default is L')
parser_balance.add_argument('--testnet', '-T', action='store_const', dest='network', const='T', required=False, help='Short for --network=T')
Expand Down Expand Up @@ -169,7 +153,7 @@ def main():
parser_association_revoke.add_argument('--sponsor', type=str , nargs=1, required=False, help="Use this option to select an account for sponsoring the transaction")

# --------------------------------------------------------------
parser_broadcast = subparsers.add_parser('broadcast', help="Takes as input a transaction (signed or unsigned) and broadcast it to the network, type 'lto broadcast --help' for more informations")
parser_broadcast = subparsers.add_parser('broadcast', help="Takes as input a transaction (signed or unsigned) and broadcast it to the network, type 'lto broadcast --help' for more information")
parser_broadcast.add_argument('stdin', nargs='?', type=argparse.FileType('r'), default=sys.stdin, help="Takes the json transaction as input: echo '$TX_JSON' | lto broadcast")
parser_broadcast.add_argument('--account', type=str , nargs=1, required=False, help="Use this option to select one of the accounts previously stored for signing the transaction. The account can be referenced by name or address, if this option is omitted, the default account is used")
parser_broadcast.add_argument('--network', type=str, nargs=1, required=False, help ='Optional network parameter, if not specified default is L')
Expand All @@ -178,7 +162,7 @@ def main():
parser_broadcast.add_argument('--unsigned', action='store_true', required=False, help="Use this option to ensure the transaction is already signed, and will not be signed by the CLI wallett")

# --------------------------------------------------------------
parser_script = subparsers.add_parser('script', help="Set a script for the account, type 'lto script --help' for more informations")
parser_script = subparsers.add_parser('script', help="Set a script for the account, type 'lto script --help' for more information")
parser_script.add_argument('stdin', nargs='?', type=argparse.FileType('r'), default=sys.stdin, help="Takes the ride script as input: echo '$SCRIPT' | lto script")
parser_script.add_argument('--account', type=str, nargs=1, required=False, help="Use this option to select one of the accounts previously stored for signing the transaction. The account can be referenced by name or address, if this option is omitted, the default account is used")
parser_script.add_argument('--network', type=str, nargs=1, required=False, help='Optional network parameter, if not specified default is L')
Expand Down Expand Up @@ -285,7 +269,7 @@ def main():
parser_transfer.add_argument('--sponsor', type=str , nargs=1, required=False, help="Use this option to select an account for sponsoring the transaction")

# --------------------------------------------------------------
parser_burn = subparsers.add_parser('burn', help="Create a Burn Transaction, type 'lto burn --help' for more informations")
parser_burn = subparsers.add_parser('burn', help="Create a Burn Transaction, type 'lto burn --help' for more information")
parser_burn.add_argument('--amount', type=float, nargs=1, required=True, help="Specify the amounts of token to burn, 1 equals 1 LTO")
parser_burn.add_argument('--account', type=str, nargs=1, required=False, help="Use this option to select one of the accounts previously stored. The account can be referenced by name or address, if this option is omitted, the default account is used")
parser_burn.add_argument('--network', type=str, nargs=1, required=False, help='Optional network parameter, if not specified default is L')
Expand Down
3 changes: 2 additions & 1 deletion src/lto_cli/commands/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ def func(name_space, parser, subparser):
if not (chain_id.isalpha() and len(chain_id) == 1):
parser.error('The --network parameter accepts only CHAR type')
sec_name = name_space.name[0] if name_space.name else ''
nonce = name_space.nonce[0] if name_space.nonce else 0
factory = AccountFactory(chain_id)
seed = name_space.stdin.read().splitlines()
if not seed:
parser.error("Seed missing, type 'lto account seed --help' for instructions")
account = factory.create_from_seed(seed[0])
account = factory.create_from_seed(seed[0], nonce)
config.write_to_file(chain_id, account, sec_name, parser)
print(account.address)

Expand Down
8 changes: 5 additions & 3 deletions src/lto_cli/commands/association.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ def func(name_space, parser, subparser):

chain_id = handle.check(name_space.network[0], parser) if name_space.network else 'L'
account_name = vars(name_space)['account'][0] if vars(name_space)['account'] else ''

if vars(name_space)['subparser-name-association'] in ['issue','revoke']:
sponsor = vars(name_space)['sponsor'][0] if vars(name_space)['sponsor'] else None
association_type = name_space.type[0]
recipient = name_space.recipient[0]
recipient = handle.get_address(chain_id, parser, name_space.recipient[0])

subject = ''
if name_space.subject:
subject = name_space.subject[0]
Expand All @@ -29,7 +31,7 @@ def func(name_space, parser, subparser):
transaction = transaction.broadcast_to(handle.get_node(chain_id, parser))
elif vars(name_space)['no_broadcast'] is False:
parser.error(
"Use the '--unsigned' option only in combination with the '--no-broadcast' option. Type 'lto association issue --help' for more informations ")
"Use the '--unsigned' option only in combination with the '--no-broadcast' option. Type 'lto association issue --help' for more information")
else: # revoke case
transaction = RevokeAssociation(recipient=recipient, association_type=association_type, subject=decode(subject, "hex"))
if vars(name_space)['unsigned'] is False:
Expand All @@ -41,7 +43,7 @@ def func(name_space, parser, subparser):
transaction = transaction.broadcast_to(handle.get_node(chain_id, parser))
elif vars(name_space)['no_broadcast'] is False:
parser.error(
"Use the '--unsigned' option only in combination with the '--no-broadcast' option. Type 'lto association revoke --help' for more informations ")
"Use the '--unsigned' option only in combination with the '--no-broadcast' option. Type 'lto association revoke --help' for more information")
handle.pretty_print(transaction)
else:
node = handle.get_node(chain_id, parser)
Expand Down
23 changes: 10 additions & 13 deletions src/lto_cli/commands/balance.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,19 @@ def print_balance(name_space, balances):
print('Generating:', balances['generating'] / 100000000)


def validate_address(address, node):
return node.validate_address(address)


def func(name_space, parser):
chain_id = handle.check(name_space.network[0], parser) if name_space.network else 'L'
address = name_space.address if name_space.address else None
if not address:
address = name_space.account[0] if name_space.account else handle.get_account(chain_id, parser).address
node = handle.get_node(chain_id, parser)

if validate_address(address, node):
balances = node.balance_details(address)
print_balance(name_space, balances)
if name_space.address and name_space.account:
parser.error('Specify an address or account, not both')

if name_space.account:
address = handle.get_account(chain_id, parser, name_space.account[0]).address
elif name_space.address:
address = handle.get_address(chain_id, parser, name_space.address)
else:
account = handle.get_account(chain_id, parser, address)
balances = node.balance_details(account.address)
print_balance(name_space, balances)
address = handle.get_account(chain_id, parser).address

balances = node.balance_details(address)
print_balance(name_space, balances)
4 changes: 2 additions & 2 deletions src/lto_cli/commands/burn.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ def func(name_space, parser):
if vars(name_space)['no_broadcast'] is False:
transaction = transaction.broadcast_to(handle.get_node(chain_id, parser))
elif vars(name_space)['no_broadcast'] is False:
parser.error("Use the '--unsigned' option only in combination with the '--no-broadcast' option. Type 'lto transaction --help' for more informations ")
handle.pretty_print(transaction)
parser.error("Use the '--unsigned' option only in combination with the '--no-broadcast' option. Type 'lto transaction --help' for more information")
handle.pretty_print(transaction)
14 changes: 7 additions & 7 deletions src/lto_cli/commands/data.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import json

from lto.accounts.ed25519.account_factory_ed25519 import AccountFactoryED25519 as AccountFactory
from lto_cli import config
from lto_cli import handle_default as handle
from lto.transactions.data import Data

from lto_cli.handle_default import validate_address


def data_set(name_space, parser):
chain_id = handle.check(name_space.network[0], parser) if name_space.network else 'L'
Expand All @@ -24,7 +24,7 @@ def data_set(name_space, parser):
elif vars(name_space)['no_broadcast'] is False:
parser.error(
"Use the '--unsigned' option only in combination with the '--no-broadcast' option. Type 'lto anchor "
"--help' for more informations ")
"--help' for more information")
handle.pretty_print(transaction)


Expand All @@ -35,18 +35,18 @@ def data_get(name_space, parser):
if not address:
address = handle.get_account(chain_id, parser).address
node = handle.get_node(chain_id, parser)
if not node.validate_address(address):
parser.error('{} address is not valid'.format(address))
if not validate_address(address):
parser.error(f'{address} address is not valid')
if key:
value = node.get_data_by_key(address, key)
if not value:
print('No data found for {}'.format(address))
print(f'No data found for {address}')
else:
print(value)
else:
value = node.get_data(address)
if not value:
print('No data found for {}'.format(address))
print(f'No data found for {address}')
else:
for x in value:
print(x)
Expand Down
13 changes: 8 additions & 5 deletions src/lto_cli/commands/leasing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ def func(name_space, parser, subparser):
account_name = vars(name_space)['account'][0] if vars(name_space)['account'] else ''

if vars(name_space)['subparser-name-lease'] == 'create':
recipient = handle.get_address(chain_id, parser, name_space.recipient[0])
sponsor = vars(name_space)['sponsor'][0] if vars(name_space)['sponsor'] else None
transaction = Lease(recipient=name_space.recipient[0], amount=int(name_space.amount[0] * 100000000))
transaction = Lease(recipient=recipient, amount=int(name_space.amount[0] * 100000000))
if vars(name_space)['unsigned'] is False:
transaction.sign_with(handle.get_account(chain_id, parser, account_name))
if sponsor:
Expand All @@ -22,7 +23,7 @@ def func(name_space, parser, subparser):
transaction = transaction.broadcast_to(handle.get_node(chain_id, parser))
elif vars(name_space)['no_broadcast'] is False:
parser.error(
"Use the '--unsigned' option only in combination with the '--no-broadcast' option. Type 'lto lease create --help' for more informations ")
"Use the '--unsigned' option only in combination with the '--no-broadcast' option. Type 'lto lease create --help' for more information")
handle.pretty_print(transaction)

elif vars(name_space)['subparser-name-lease'] == 'cancel':
Expand All @@ -36,7 +37,7 @@ def func(name_space, parser, subparser):
transaction = transaction.broadcast_to(handle.get_node(chain_id, parser))
elif vars(name_space)['no_broadcast'] is False:
parser.error(
"Use the '--unsigned' option only in combination with the '--no-broadcast' option. Type 'lto lease cancel --help' for more informations ")
"Use the '--unsigned' option only in combination with the '--no-broadcast' option. Type 'lto lease cancel --help' for more information")
handle.pretty_print(transaction)

elif vars(name_space)['subparser-name-lease'] == 'out':
Expand All @@ -46,12 +47,12 @@ def func(name_space, parser, subparser):
flag = 0
for x in value:
if x['sender'] == address: # outbound
print(x['sender'], ':', x['amount'] /100000000)
print(x['recipient'], ':', x['amount'] /100000000)
flag +=1
if flag == 0:
print("No outbound leases")

else: # out
elif vars(name_space)['subparser-name-lease'] == 'in':
node = handle.get_node(chain_id, parser)
address = handle.get_account(chain_id, parser, account_name).address
value = node.lease_list(address)
Expand All @@ -63,3 +64,5 @@ def func(name_space, parser, subparser):
if flag == 0:
print("No inbound leases")

else:
parser.error("Unknown command. Type 'lto lease --help' for more information.")
Loading

0 comments on commit f356717

Please sign in to comment.