main_txsend.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2024 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. mmgen-txsend: Broadcast a transaction signed by 'mmgen-txsign' to the network
  20. """
  21. import sys
  22. from .cfg import gc,Config
  23. from .util import async_run, die
  24. opts_data = {
  25. 'sets': [
  26. ('yes', True, 'quiet', True),
  27. ('abort', True, 'autosign', True),
  28. ],
  29. 'text': {
  30. 'desc': f'Send a signed {gc.proj_name} cryptocoin transaction',
  31. 'usage': '[opts] [signed transaction file]',
  32. 'options': """
  33. -h, --help Print this help message
  34. --, --longhelp Print help message for long options (common options)
  35. -a, --autosign Send an autosigned transaction created by ‘mmgen-txcreate
  36. --autosign’. The removable device is mounted and unmounted
  37. automatically. The transaction file argument must be omitted
  38. when using this option
  39. -A, --abort Abort an unsent transaction created by ‘mmgen-txcreate
  40. --autosign’ and delete it from the removable device. The
  41. transaction may be signed or unsigned.
  42. -d, --outdir= d Specify an alternate directory 'd' for output
  43. -q, --quiet Suppress warnings; overwrite files without prompting
  44. -s, --status Get status of a sent transaction (or the current transaction,
  45. whether sent or unsent, when used with --autosign)
  46. -v, --verbose Be more verbose
  47. -y, --yes Answer 'yes' to prompts, suppress non-essential output
  48. """
  49. }
  50. }
  51. cfg = Config(opts_data=opts_data)
  52. if cfg.autosign and cfg.outdir:
  53. die(1, '--outdir cannot be used in combination with --autosign')
  54. if len(cfg._args) == 1:
  55. infile = cfg._args[0]
  56. from .fileutil import check_infile
  57. check_infile(infile)
  58. elif not cfg._args and cfg.autosign:
  59. from .tx.util import init_removable_device
  60. from .autosign import Signable
  61. asi = init_removable_device(cfg)
  62. asi.do_mount()
  63. si = Signable.automount_transaction(asi)
  64. if cfg.abort:
  65. si.shred_abortable() # prompts user, then raises exception or exits
  66. elif cfg.status:
  67. if si.unsent:
  68. die(1, 'Transaction is unsent')
  69. if si.unsigned:
  70. die(1, 'Transaction is unsigned')
  71. else:
  72. infile = si.get_unsent()
  73. cfg._util.qmsg(f'Got signed transaction file ‘{infile}’')
  74. else:
  75. cfg._opts.usage()
  76. if not cfg.status:
  77. from .ui import do_license_msg
  78. do_license_msg(cfg)
  79. async def main():
  80. from .tx import OnlineSignedTX, SentTX
  81. if cfg.status and cfg.autosign:
  82. tx = await si.get_last_created()
  83. else:
  84. tx = await OnlineSignedTX(
  85. cfg = cfg,
  86. filename = infile,
  87. automount = cfg.autosign,
  88. quiet_open = True)
  89. from .rpc import rpc_init
  90. tx.rpc = await rpc_init(cfg,tx.proto)
  91. cfg._util.vmsg(f'Getting {tx.desc} ‘{tx.infile}’')
  92. if cfg.status:
  93. if tx.coin_txid:
  94. cfg._util.qmsg(f'{tx.proto.coin} txid: {tx.coin_txid.hl()}')
  95. await tx.status.display(usr_req=True)
  96. sys.exit(0)
  97. if not cfg.yes:
  98. tx.info.view_with_prompt('View transaction details?')
  99. if tx.add_comment(): # edits an existing comment, returns true if changed
  100. if not cfg.autosign:
  101. tx.file.write(ask_write_default_yes=True)
  102. if await tx.send():
  103. tx2 = await SentTX(cfg=cfg, data=tx.__dict__, automount=cfg.autosign)
  104. tx2.file.write(
  105. outdir = asi.txauto_dir if cfg.autosign else None,
  106. ask_overwrite = False,
  107. ask_write = False)
  108. tx2.print_contract_addr()
  109. async_run(main())