common.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/usr/bin/env python3
  2. # -*- coding: UTF-8 -*-
  3. #
  4. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  5. # Copyright (C)2013-2018 The MMGen Project <mmgen@tuta.io>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. """
  20. common.py: Common imports for all MMGen scripts
  21. """
  22. import sys,os
  23. from mmgen.exception import *
  24. from mmgen.globalvars import g
  25. import mmgen.opts as opts
  26. from mmgen.opts import opt
  27. from mmgen.util import *
  28. def set_debug_all():
  29. if os.getenv('MMGEN_DEBUG_ALL'):
  30. for name in g.env_opts:
  31. if name[:11] == 'MMGEN_DEBUG':
  32. os.environ[name] = '1'
  33. def help_notes(k):
  34. from mmgen.seed import SeedSource
  35. from mmgen.tx import MMGenTX
  36. def fee_spec_letters(use_quotes=False):
  37. cu = g.proto.coin_amt.units
  38. sep,conj = ((',',' or '),("','","' or '"))[use_quotes]
  39. return sep.join(u[0] for u in cu[:-1]) + ('',conj)[len(cu)>1] + cu[-1][0]
  40. def fee_spec_names():
  41. cu = g.proto.coin_amt.units
  42. return ', '.join(cu[:-1]) + ('',' and ')[len(cu)>1] + cu[-1] + ('',',\nrespectively')[len(cu)>1]
  43. return {
  44. 'rel_fee_desc': MMGenTX().rel_fee_desc,
  45. 'fee_spec_letters': fee_spec_letters(),
  46. 'passwd': """
  47. For passphrases all combinations of whitespace are equal, and leading and
  48. trailing space are ignored. This permits reading passphrase or brainwallet
  49. data from a multi-line file with free spacing and indentation.
  50. """.strip(),
  51. 'brainwallet': """
  52. BRAINWALLET NOTE:
  53. To thwart dictionary attacks, it's recommended to use a strong hash preset
  54. with brainwallets. For a brainwallet passphrase to generate the correct
  55. seed, the same seed length and hash preset parameters must always be used.
  56. """.strip(),
  57. 'txcreate': """
  58. The transaction's outputs are specified on the command line, while its inputs
  59. are chosen from a list of the user's unpent outputs via an interactive menu.
  60. If the transaction fee is not specified on the command line (see FEE
  61. SPECIFICATION below), it will be calculated dynamically using network fee
  62. estimation for the default (or user-specified) number of confirmations.
  63. If network fee estimation fails, the user will be prompted for a fee.
  64. Network-estimated fees will be multiplied by the value of '--tx-fee-adj',
  65. if specified.
  66. Ages of transactions are approximate based on an average block discovery
  67. interval of one per {g.proto.secs_per_block} seconds.
  68. All addresses on the command line can be either {pnu} addresses or {pnm}
  69. addresses of the form <seed ID>:<index>.
  70. To send the value of all inputs (minus TX fee) to a single output, specify
  71. one address with no amount on the command line.
  72. """.format(g=g,pnm=g.proj_name,pnu=g.proto.name.capitalize()),
  73. 'fee': """
  74. FEE SPECIFICATION: Transaction fees, both on the command line and at the
  75. interactive prompt, may be specified as either absolute {c} amounts, using
  76. a plain decimal number, or as {r}, using an integer followed by
  77. '{l}', for {u}.
  78. """.format( c=g.coin,
  79. r=MMGenTX().rel_fee_desc,
  80. l=fee_spec_letters(use_quotes=True),
  81. u=fee_spec_names() ),
  82. 'txsign': """
  83. Transactions may contain both {pnm} or non-{pnm} input addresses.
  84. To sign non-{pnm} inputs, a {dn} wallet dump or flat key list is used
  85. as the key source ('--keys-from-file' option).
  86. To sign {pnm} inputs, key data is generated from a seed as with the
  87. {pnl}-addrgen and {pnl}-keygen commands. Alternatively, a key-address file
  88. may be used (--mmgen-keys-from-file option).
  89. Multiple wallets or other seed files can be listed on the command line in
  90. any order. If the seeds required to sign the transaction's inputs are not
  91. found in these files (or in the default wallet), the user will be prompted
  92. for seed data interactively.
  93. To prevent an attacker from crafting transactions with bogus {pnm}-to-{pnu}
  94. address mappings, all outputs to {pnm} addresses are verified with a seed
  95. source. Therefore, seed files or a key-address file for all {pnm} outputs
  96. must also be supplied on the command line if the data can't be found in the
  97. default wallet.
  98. Seed source files must have the canonical extensions listed in the 'FileExt'
  99. column below:
  100. {n_fmt}
  101. """.format( dn=g.proto.daemon_name,
  102. n_fmt='\n '.join(SeedSource.format_fmt_codes().splitlines()),
  103. pnm=g.proj_name,
  104. pnu=g.proto.name.capitalize(),
  105. pnl=g.proj_name.lower())
  106. }[k] + ('-α' if g.debug_utf8 else '')