From e66a3579f13cd54dcfe8c1500a82abb2105cdbee Mon Sep 17 00:00:00 2001 From: John Mac Date: Sun, 30 Jul 2017 18:22:24 +0100 Subject: [PATCH 1/2] Attempt at Issue #31. --- examples/send_transfer.py | 117 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 examples/send_transfer.py diff --git a/examples/send_transfer.py b/examples/send_transfer.py new file mode 100644 index 0000000..83865d4 --- /dev/null +++ b/examples/send_transfer.py @@ -0,0 +1,117 @@ +# coding=utf-8 +""" +Example script that shows how to use PyOTA to send a transfer to an address. +""" + +from __future__ import absolute_import, division, print_function, \ + unicode_literals + +from argparse import ArgumentParser +from getpass import getpass as secure_input +from sys import argv + +from iota import * +from iota import __version__ +from six import binary_type, moves as compat, text_type + + +def main(uri): + # type: (Text, int, Optional[int], bool) -> None + + seed = get_seed() + + if not seed: + print("No seed entered, transfer cancelled.") + return + + addressString = get_address() + + if not addressString: + print("No address entered, transfer cancelled.") + return + + value = get_value() + + print("Sending " + str(value) + "iota to address: " + addressString) + + # Create the API instance. + api = Iota(uri, seed) + + bundle = api.send_transfer( + depth = 100, + transfers = [ + ProposedTransaction( + address = Address(bytearray(addressString)), + value = value, + tag = Tag(b'EXAMPLE'), + message = TryteString.from_string('Hello!'), + ), + ], + ) + + print('Transfer complete') + + +def get_seed(): + # type: () -> binary_type + """ + Prompts the user securely for their seed. + """ + print( + 'Enter seed to send from and press return (typing will not be shown). ' + ) + seed = secure_input('') + + if seed == '': + return None + + return seed.encode('ascii') + + +def get_address(): + # type: () -> binary_type + """ + Prompts the user for the address to send to. + """ + print( + 'Enter address to send to.' + ) + address = secure_input('') + + if address == '': + print("No address") + return None + + return address + + +def get_value(): + """ + Prompts the user for the value to send. + """ + + try: + valueStr = secure_input("Enter a value to send: ") + value = int(valueStr) + return value + except ValueError: + print("Invalid amoumt, defaulting to 0.") + return 0 + +if __name__ == '__main__': + + parser = ArgumentParser( + description = __doc__, + epilog = 'PyOTA v{version}'.format(version=__version__), + ) + + parser.add_argument( + '--uri', + type = text_type, + default = 'http://localhost:14265/', + help = + 'URI of the node to connect to ' + '(defaults to http://localhost:14265/).', + ) + + main(**vars(parser.parse_args(argv[1:]))) From e1cc21d151f4dffc0b85453bdfd5b219f8a1b477 Mon Sep 17 00:00:00 2001 From: John Mac Date: Tue, 1 Aug 2017 21:29:27 +0100 Subject: [PATCH 2/2] Changed example to more suitable format. --- examples/send_transfer.py | 141 +++++++++----------------------------- 1 file changed, 34 insertions(+), 107 deletions(-) diff --git a/examples/send_transfer.py b/examples/send_transfer.py index 83865d4..732c121 100644 --- a/examples/send_transfer.py +++ b/examples/send_transfer.py @@ -2,116 +2,43 @@ """ Example script that shows how to use PyOTA to send a transfer to an address. """ - -from __future__ import absolute_import, division, print_function, \ - unicode_literals - -from argparse import ArgumentParser -from getpass import getpass as secure_input -from sys import argv - from iota import * -from iota import __version__ -from six import binary_type, moves as compat, text_type - - -def main(uri): - # type: (Text, int, Optional[int], bool) -> None - - seed = get_seed() - - if not seed: - print("No seed entered, transfer cancelled.") - return - - addressString = get_address() - - if not addressString: - print("No address entered, transfer cancelled.") - return - - value = get_value() - - print("Sending " + str(value) + "iota to address: " + addressString) - - # Create the API instance. - api = Iota(uri, seed) - - bundle = api.send_transfer( - depth = 100, - transfers = [ - ProposedTransaction( - address = Address(bytearray(addressString)), - value = value, - tag = Tag(b'EXAMPLE'), - message = TryteString.from_string('Hello!'), - ), - ], - ) - print('Transfer complete') +# Create the API instance. +api =\ + Iota( + # URI of a locally running node. + 'http://localhost:14265/', -def get_seed(): - # type: () -> binary_type - """ - Prompts the user securely for their seed. - """ - print( - 'Enter seed to send from and press return (typing will not be shown). ' - ) - seed = secure_input('') - - if seed == '': - return None - - return seed.encode('ascii') - - -def get_address(): - # type: () -> binary_type - """ - Prompts the user for the address to send to. - """ - print( - 'Enter address to send to.' - ) - address = secure_input('') - - if address == '': - print("No address") - return None - - return address - - -def get_value(): - """ - Prompts the user for the value to send. - """ - - try: - valueStr = secure_input("Enter a value to send: ") - value = int(valueStr) - return value - except ValueError: - print("Invalid amoumt, defaulting to 0.") - return 0 - -if __name__ == '__main__': - - parser = ArgumentParser( - description = __doc__, - epilog = 'PyOTA v{version}'.format(version=__version__), - ) - - parser.add_argument( - '--uri', - type = text_type, - default = 'http://localhost:14265/', - help = - 'URI of the node to connect to ' - '(defaults to http://localhost:14265/).', + # Seed used for cryptographic functions. + seed = b'SEED9GOES9HERE' ) - main(**vars(parser.parse_args(argv[1:]))) +# For more information, see :py:meth:`Iota.send_transfer`. +api.send_transfer( + depth = 100, + + # One or more :py:class:`ProposedTransaction` objects to add to the + # bundle. + transfers = [ + ProposedTransaction( + # Recipient of the transfer. + address = + Address( + b'TESTVALUE9DONTUSEINPRODUCTION99999FBFFTG' + b'QFWEHEL9KCAFXBJBXGE9HID9XCOHFIDABHDG9AHDR' + ), + + # Amount of IOTA to transfer. + # This value may be zero. + value = 1, + + # Optional tag to attach to the transfer. + tag = Tag(b'EXAMPLE'), + + # Optional message to include with the transfer. + message = TryteString.from_string('Hello!'), + ), + ], +)