main_txdo.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #!/usr/bin/env python3
  2. #
  3. # MMGen Wallet, a terminal-based cryptocurrency wallet
  4. # Copyright (C)2013-2025 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 send 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. target = gc.prog_name.split('-')[1].removesuffix('do')
  25. opts_data = {
  26. 'filter_codes': {
  27. 'tx': ['-', 't'],
  28. 'swaptx': ['-', 's'],
  29. }[target],
  30. 'sets': [('yes', True, 'quiet', True)],
  31. 'text': {
  32. 'desc': {
  33. 'tx': f'Create, sign and send an {gc.proj_name} transaction',
  34. 'swaptx': f'Create, sign and send a DEX swap transaction from one {gc.proj_name} tracking wallet to another',
  35. }[target],
  36. 'usage': '[opts] {u_args} [addr file ...] [seed source ...]',
  37. 'options': """
  38. -- -h, --help Print this help message
  39. -- --, --longhelp Print help message for long (global) options
  40. r- -A, --fee-adjust= f Adjust transaction fee by factor ‘f’ (see below)
  41. -- -b, --brain-params=l,p Use seed length 'l' and hash preset 'p' for
  42. + brainwallet input
  43. -- -B, --no-blank Don't blank screen before displaying {a_info}
  44. -- -c, --comment-file= f Source the transaction's comment from file 'f'
  45. b- -C, --fee-estimate-confs=c Desired number of confirmations for fee estimation
  46. + (default: {cfg.fee_estimate_confs})
  47. -- -d, --outdir= d Specify an alternate directory 'd' for output
  48. e- -D, --contract-data= D Path to file containing hex-encoded contract data
  49. -- -e, --echo-passphrase Print passphrase to screen when typing it
  50. b- -E, --fee-estimate-mode=M Specify the network fee estimate mode. Choices:
  51. + {fe_all}. Default: {fe_dfl!r}
  52. r- -f, --fee= f Transaction fee, as a decimal {cu} amount or as
  53. + {fu} (an integer followed by {fl}).
  54. + See FEE SPECIFICATION below. If omitted, fee will be
  55. + calculated using network fee estimation.
  56. et -g, --gas=N Set the gas limit (see GAS LIMIT below)
  57. -s -g, --gas=N Set the gas limit for Ethereum (see GAS LIMIT below)
  58. -s -G, --router-gas=N Set the gas limit for the Ethereum router contract
  59. + (integer). When unset, a hardcoded default will be
  60. + used. Applicable only for swaps from token assets.
  61. -- -H, --hidden-incog-input-params=f,o Read hidden incognito data from file
  62. + 'f' at offset 'o' (comma-separated)
  63. -- -i, --in-fmt= f Input is from wallet format 'f' (see FMT CODES below)
  64. -- -I, --inputs= i Specify transaction inputs (comma-separated list of
  65. + MMGen IDs or coin addresses). Note that ALL unspent
  66. + outputs associated with each address will be included.
  67. -- -l, --seed-len= l Specify wallet seed length of 'l' bits. This option
  68. + is required only for brainwallet and incognito inputs
  69. + with non-standard (< {dsl}-bit) seed lengths.
  70. -- -k, --keys-from-file=f Provide additional keys for non-{pnm} addresses
  71. -- -K, --keygen-backend=n Use backend 'n' for public key generation. Options
  72. + for {coin_id}: {kgs}
  73. -s -l, --trade-limit=L Minimum swap amount, as either percentage or absolute
  74. + coin amount (see TRADE LIMIT below)
  75. bt -l, --locktime= t Lock time (block height or unix seconds) (default: 0)
  76. b- -L, --autochg-ignore-labels Ignore labels when autoselecting change addresses
  77. -- -m, --minconf=n Minimum number of confirmations required to spend
  78. + outputs (default: 1)
  79. -- -M, --mmgen-keys-from-file=f Provide keys for {pnm} addresses in a key-
  80. + address file (output of '{pnl}-keygen'). Permits
  81. + online signing without an {pnm} seed source. The
  82. + key-address file is also used to verify {pnm}-to-{cu}
  83. + mappings, so the user should record its checksum.
  84. e- -n, --tx-proxy=P Send transaction via public TX proxy ‘P’ (supported
  85. + proxies: {tx_proxies}). This is done via a publicly
  86. + accessible web page, so no API key or registration is
  87. + required.
  88. -- -O, --old-incog-fmt Specify old-format incognito input
  89. -- -p, --hash-preset= p Use the scrypt hash parameters defined by preset 'p'
  90. + for password hashing (default: '{gc.dfl_hash_preset}')
  91. -- -P, --passwd-file= f Get {pnm} wallet passphrase from file 'f'
  92. -- -q, --quiet Suppress warnings; overwrite files without prompting
  93. -s -r, --stream-interval=N Set block interval for streaming swap (default: {si})
  94. bt -R, --no-rbf Make transaction non-replaceable (non-replace-by-fee
  95. + according to BIP 125)
  96. -s -s, --swap-proto Swap protocol to use (Default: {x_dfl},
  97. + Choices: {x_all})
  98. -- -T, --txhex-idx=N Send only part ‘N’ of a multi-part transaction.
  99. + Indexing begins with one.
  100. -- -u, --subseeds= n The number of subseed pairs to scan for (default: {ss},
  101. + maximum: {ss_max}). Only the default or first supplied
  102. + wallet is scanned for subseeds.
  103. -- -v, --verbose Produce more verbose output
  104. b- -V, --vsize-adj= f Adjust transaction's estimated vsize by factor 'f'
  105. e- -w, --wait Wait for transaction confirmation
  106. rs -x, --proxy=P Fetch the swap quote via SOCKS5h proxy ‘P’ (host:port).
  107. + Use special value ‘env’ to honor *_PROXY environment
  108. + vars instead.
  109. X- -x, --proxy=P Connect to remote server(s) via SOCKS5h proxy ‘P’
  110. + (host:port). Use special value ‘env’ to honor *_PROXY
  111. + environment vars instead.
  112. e- -X, --cached-balances Use cached balances
  113. -- -y, --yes Answer 'yes' to prompts, suppress non-essential output
  114. -- -z, --show-hash-presets Show information on available hash presets
  115. """,
  116. 'notes': """
  117. {c}
  118. {n_at}
  119. {g}{F}
  120. SIGNING NOTES
  121. {s}
  122. Seed source files must have the canonical extensions listed in the 'FileExt'
  123. column below:
  124. {f}
  125. {x}"""
  126. },
  127. 'code': {
  128. 'usage': lambda cfg, proto, help_notes, s: s.format(
  129. u_args = help_notes(f'{target}create_args')),
  130. 'options': lambda cfg, proto, help_notes, s: s.format(
  131. gc = gc,
  132. cfg = cfg,
  133. cu = proto.coin,
  134. pnm = gc.proj_name,
  135. pnl = gc.proj_name.lower(),
  136. a_info = help_notes('account_info_desc'),
  137. kgs = help_notes('keygen_backends'),
  138. coin_id = help_notes('coin_id'),
  139. fu = help_notes('rel_fee_desc'),
  140. fl = help_notes('fee_spec_letters', use_quotes=True),
  141. dsl = help_notes('dfl_seed_len'),
  142. ss = help_notes('dfl_subseeds'),
  143. si = help_notes('stream_interval'),
  144. tx_proxies = help_notes('tx_proxies'),
  145. ss_max = SubSeedIdxRange.max_idx,
  146. fe_all = fmt_list(cfg._autoset_opts['fee_estimate_mode'].choices, fmt='no_spc'),
  147. fe_dfl = cfg._autoset_opts['fee_estimate_mode'].choices[0],
  148. x_all = fmt_list(cfg._autoset_opts['swap_proto'].choices, fmt='no_spc'),
  149. x_dfl = cfg._autoset_opts['swap_proto'].choices[0]),
  150. 'notes': lambda cfg, help_mod, help_notes, s: s.format(
  151. c = help_mod(f'{target}create'),
  152. g = help_notes('gas_limit', target),
  153. F = help_notes('fee'),
  154. n_at = help_notes('address_types'),
  155. f = help_notes('fmt_codes'),
  156. s = help_mod('txsign'),
  157. x = help_mod(f'{target}create_examples'))
  158. }
  159. }
  160. cfg = Config(opts_data=opts_data)
  161. from .tx import NewTX, SentTX
  162. from .tx.keys import TxKeys, pop_seedfiles
  163. seedfiles = pop_seedfiles(cfg)
  164. async def main():
  165. if target == 'swaptx':
  166. from .tx.new_swap import get_send_proto
  167. proto = get_send_proto(cfg)
  168. else:
  169. proto = cfg._proto
  170. tx1 = NewTX(cfg=cfg, proto=proto, target=target)
  171. tx2 = await tx1.create(
  172. cmd_args = cfg._args,
  173. locktime = int(cfg.locktime or 0),
  174. caller = 'txdo')
  175. tx3 = await tx2.sign(TxKeys(cfg, tx2, seedfiles=seedfiles).keys)
  176. if tx3:
  177. tx3.file.write(ask_write=False)
  178. tx4 = await SentTX(cfg=cfg, data=tx3.__dict__)
  179. await tx4.send(cfg, asi=None)
  180. else:
  181. die(2, 'Transaction could not be signed')
  182. async_run(cfg, main)