main_txdo.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #!/usr/bin/env python3
  2. #
  3. # MMGen Wallet, a terminal-based cryptocurrency wallet
  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-txdo: Create, sign and broadcast an online MMGen transaction
  20. """
  21. from .cfg import gc, Config
  22. from .util import die, fmt_list, async_run
  23. from .subseed import SubSeedIdxRange
  24. opts_data = {
  25. 'sets': [('yes', True, 'quiet', True)],
  26. 'text': {
  27. 'desc': f'Create, sign and send an {gc.proj_name} transaction',
  28. 'usage': '[opts] {u_args} [addr file ...] [seed source ...]',
  29. 'options': """
  30. -h, --help Print this help message
  31. --, --longhelp Print help message for long (global) options
  32. -A, --fee-adjust= f Adjust transaction fee by factor 'f' (see below)
  33. -b, --brain-params=l,p Use seed length 'l' and hash preset 'p' for
  34. brainwallet input
  35. -B, --no-blank Don't blank screen before displaying {a_info}
  36. -c, --comment-file= f Source the transaction's comment from file 'f'
  37. -C, --fee-estimate-confs=c Desired number of confirmations for fee estimation
  38. (default: {cfg.fee_estimate_confs})
  39. -d, --outdir= d Specify an alternate directory 'd' for output
  40. -D, --contract-data= D Path to hex-encoded contract data (ETH only)
  41. -e, --echo-passphrase Print passphrase to screen when typing it
  42. -E, --fee-estimate-mode=M Specify the network fee estimate mode. Choices:
  43. {fe_all}. Default: {fe_dfl!r}
  44. -f, --fee= f Transaction fee, as a decimal {cu} amount or as
  45. {fu} (an integer followed by {fl!r}).
  46. See FEE SPECIFICATION below. If omitted, fee will be
  47. calculated using network fee estimation.
  48. -g, --gas= g Specify start gas amount in Wei (ETH only)
  49. -H, --hidden-incog-input-params=f,o Read hidden incognito data from file
  50. 'f' at offset 'o' (comma-separated)
  51. -i, --in-fmt= f Input is from wallet format 'f' (see FMT CODES below)
  52. -I, --inputs= i Specify transaction inputs (comma-separated list of
  53. MMGen IDs or coin addresses). Note that ALL unspent
  54. outputs associated with each address will be included.
  55. -l, --seed-len= l Specify wallet seed length of 'l' bits. This option
  56. is required only for brainwallet and incognito inputs
  57. with non-standard (< {dsl}-bit) seed lengths.
  58. -k, --keys-from-file=f Provide additional keys for non-{pnm} addresses
  59. -K, --keygen-backend=n Use backend 'n' for public key generation. Options
  60. for {coin_id}: {kgs}
  61. -l, --locktime= t Lock time (block height or unix seconds) (default: 0)
  62. -L, --autochg-ignore-labels Ignore labels when autoselecting change addresses
  63. -m, --minconf=n Minimum number of confirmations required to spend
  64. outputs (default: 1)
  65. -M, --mmgen-keys-from-file=f Provide keys for {pnm} addresses in a key-
  66. address file (output of '{pnl}-keygen'). Permits
  67. online signing without an {pnm} seed source. The
  68. key-address file is also used to verify {pnm}-to-{cu}
  69. mappings, so the user should record its checksum.
  70. -O, --old-incog-fmt Specify old-format incognito input
  71. -p, --hash-preset= p Use the scrypt hash parameters defined by preset 'p'
  72. for password hashing (default: '{gc.dfl_hash_preset}')
  73. -P, --passwd-file= f Get {pnm} wallet passphrase from file 'f'
  74. -R, --no-rbf Make transaction non-replaceable (non-replace-by-fee
  75. according to BIP 125)
  76. -q, --quiet Suppress warnings; overwrite files without prompting
  77. -u, --subseeds= n The number of subseed pairs to scan for (default: {ss},
  78. maximum: {ss_max}). Only the default or first supplied
  79. wallet is scanned for subseeds.
  80. -v, --verbose Produce more verbose output
  81. -V, --vsize-adj= f Adjust transaction's estimated vsize by factor 'f'
  82. -X, --cached-balances Use cached balances (Ethereum only)
  83. -y, --yes Answer 'yes' to prompts, suppress non-essential output
  84. -z, --show-hash-presets Show information on available hash presets
  85. """,
  86. 'notes': """
  87. {c}\n{F}
  88. SIGNING NOTES
  89. {s}
  90. Seed source files must have the canonical extensions listed in the 'FileExt'
  91. column below:
  92. FMT CODES:
  93. {f}
  94. {x}"""
  95. },
  96. 'code': {
  97. 'usage': lambda cfg, proto, help_notes, s: s.format(
  98. u_args = help_notes('txcreate_args')),
  99. 'options': lambda cfg, proto, help_notes, s: s.format(
  100. gc = gc,
  101. cfg = cfg,
  102. pnm = gc.proj_name,
  103. pnl = gc.proj_name.lower(),
  104. a_info = help_notes('account_info_desc'),
  105. kgs = help_notes('keygen_backends'),
  106. coin_id = help_notes('coin_id'),
  107. fu = help_notes('rel_fee_desc'),
  108. fl = help_notes('fee_spec_letters'),
  109. ss = help_notes('dfl_subseeds'),
  110. ss_max = SubSeedIdxRange.max_idx,
  111. fe_all = fmt_list(cfg._autoset_opts['fee_estimate_mode'].choices, fmt='no_spc'),
  112. fe_dfl = cfg._autoset_opts['fee_estimate_mode'].choices[0],
  113. dsl = help_notes('dfl_seed_len'),
  114. cu = proto.coin),
  115. 'notes': lambda cfg, help_notes, s: s.format(
  116. c = help_notes('txcreate'),
  117. F = help_notes('fee'),
  118. s = help_notes('txsign'),
  119. f = help_notes('fmt_codes'),
  120. x = help_notes('txcreate_examples')),
  121. }
  122. }
  123. cfg = Config(opts_data=opts_data)
  124. from .tx import NewTX, SentTX
  125. from .tx.sign import txsign, get_seed_files, get_keyaddrlist, get_keylist
  126. seed_files = get_seed_files(cfg, cfg._args)
  127. async def main():
  128. tx1 = await NewTX(cfg=cfg, proto=cfg._proto)
  129. from .rpc import rpc_init
  130. tx1.rpc = await rpc_init(cfg)
  131. tx2 = await tx1.create(
  132. cmd_args = cfg._args,
  133. locktime = int(cfg.locktime or 0),
  134. caller = 'txdo')
  135. kal = get_keyaddrlist(cfg, cfg._proto)
  136. kl = get_keylist(cfg)
  137. tx3 = await txsign(cfg, tx2, seed_files, kl, kal)
  138. if tx3:
  139. tx3.file.write(ask_write=False)
  140. tx4 = await SentTX(cfg=cfg, data=tx3.__dict__)
  141. if await tx4.send():
  142. tx4.file.write(ask_overwrite=False, ask_write=False)
  143. tx4.print_contract_addr()
  144. else:
  145. die(2, 'Transaction could not be signed')
  146. async_run(main())