|
| 1 | +# coding=utf-8 |
| 2 | +""" |
| 3 | +Example script that shows how to use PyOTA to send a transfer to an address. |
| 4 | +""" |
| 5 | + |
| 6 | +from __future__ import absolute_import, division, print_function, \ |
| 7 | + unicode_literals |
| 8 | + |
| 9 | +from argparse import ArgumentParser |
| 10 | +from getpass import getpass as secure_input |
| 11 | +from sys import argv |
| 12 | + |
| 13 | +from iota import * |
| 14 | +from iota import __version__ |
| 15 | +from six import binary_type, moves as compat, text_type |
| 16 | + |
| 17 | + |
| 18 | +def main(uri): |
| 19 | + # type: (Text, int, Optional[int], bool) -> None |
| 20 | + |
| 21 | + seed = get_seed() |
| 22 | + |
| 23 | + if not seed: |
| 24 | + print("No seed entered, transfer cancelled.") |
| 25 | + return |
| 26 | + |
| 27 | + addressString = get_address() |
| 28 | + |
| 29 | + if not addressString: |
| 30 | + print("No address entered, transfer cancelled.") |
| 31 | + return |
| 32 | + |
| 33 | + value = get_value() |
| 34 | + |
| 35 | + print("Sending " + str(value) + "iota to address: " + addressString) |
| 36 | + |
| 37 | + # Create the API instance. |
| 38 | + api = Iota(uri, seed) |
| 39 | + |
| 40 | + bundle = api.send_transfer( |
| 41 | + depth = 100, |
| 42 | + transfers = [ |
| 43 | + ProposedTransaction( |
| 44 | + address = Address(bytearray(addressString)), |
| 45 | + value = value, |
| 46 | + tag = Tag(b'EXAMPLE'), |
| 47 | + message = TryteString.from_string('Hello!'), |
| 48 | + ), |
| 49 | + ], |
| 50 | + ) |
| 51 | + |
| 52 | + print('Transfer complete') |
| 53 | + |
| 54 | + |
| 55 | +def get_seed(): |
| 56 | + # type: () -> binary_type |
| 57 | + """ |
| 58 | + Prompts the user securely for their seed. |
| 59 | + """ |
| 60 | + print( |
| 61 | + 'Enter seed to send from and press return (typing will not be shown). ' |
| 62 | + ) |
| 63 | + seed = secure_input('') |
| 64 | + |
| 65 | + if seed == '': |
| 66 | + return None |
| 67 | + |
| 68 | + return seed.encode('ascii') |
| 69 | + |
| 70 | + |
| 71 | +def get_address(): |
| 72 | + # type: () -> binary_type |
| 73 | + """ |
| 74 | + Prompts the user for the address to send to. |
| 75 | + """ |
| 76 | + print( |
| 77 | + 'Enter address to send to.' |
| 78 | + ) |
| 79 | + address = secure_input('') |
| 80 | + |
| 81 | + if address == '': |
| 82 | + print("No address") |
| 83 | + return None |
| 84 | + |
| 85 | + return address |
| 86 | + |
| 87 | + |
| 88 | +def get_value(): |
| 89 | + """ |
| 90 | + Prompts the user for the value to send. |
| 91 | + """ |
| 92 | + |
| 93 | + try: |
| 94 | + valueStr = secure_input("Enter a value to send: ") |
| 95 | + value = int(valueStr) |
| 96 | + return value |
| 97 | + except ValueError: |
| 98 | + print("Invalid amoumt, defaulting to 0.") |
| 99 | + return 0 |
| 100 | + |
| 101 | +if __name__ == '__main__': |
| 102 | + |
| 103 | + parser = ArgumentParser( |
| 104 | + description = __doc__, |
| 105 | + epilog = 'PyOTA v{version}'.format(version=__version__), |
| 106 | + ) |
| 107 | + |
| 108 | + parser.add_argument( |
| 109 | + '--uri', |
| 110 | + type = text_type, |
| 111 | + default = 'http://localhost:14265/', |
| 112 | + help = |
| 113 | + 'URI of the node to connect to ' |
| 114 | + '(defaults to http://localhost:14265/).', |
| 115 | + ) |
| 116 | + |
| 117 | + main(**vars(parser.parse_args(argv[1:]))) |
0 commit comments