main_seedjoin.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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-seedjoin: Regenerate an MMGen deterministic wallet from seed shares
  20. created by 'mmgen-seedsplit'
  21. """
  22. from .cfg import gc, Config
  23. from .util import msg, msg_r, die
  24. from .color import yellow
  25. from .seed import Seed
  26. from .seedsplit import SeedSplitIDString, MasterShareIdx, SeedShareMasterJoining
  27. from .wallet import Wallet
  28. opts_data = {
  29. 'text': {
  30. 'desc': """Regenerate an MMGen deterministic wallet from seed shares
  31. created by 'mmgen-seedsplit'""",
  32. 'usage': '[options] share1 share2 [...shareN]',
  33. 'options': """
  34. -h, --help Print this help message
  35. --, --longhelp Print help message for long (global) options
  36. -d, --outdir= d Output file to directory 'd' instead of working dir
  37. -e, --echo-passphrase Echo passphrases and other user input to screen
  38. -i, --id-str= s ID String of split (required for master share join only)
  39. -H, --hidden-incog-input-params=f,o Read hidden incognito data from file
  40. 'f' at offset 'o' (comma-separated). NOTE: only the
  41. first share may be in hidden incognito format!
  42. -J, --hidden-incog-output-params=f,o Write hidden incognito data to file
  43. 'f' at offset 'o' (comma-separated). File 'f' will be
  44. created if necessary and filled with random data.
  45. -o, --out-fmt= f Output to wallet format 'f' (see FMT CODES below)
  46. -O, --old-incog-fmt Specify old-format incognito input
  47. -L, --label= l Specify a label 'l' for output wallet
  48. -M, --master-share=i Use a master share with index 'i' (min:{ms_min}, max:{ms_max})
  49. -p, --hash-preset= p Use the scrypt hash parameters defined by preset 'p'
  50. for password hashing (default: '{gc.dfl_hash_preset}')
  51. -z, --show-hash-presets Show information on available hash presets
  52. -P, --passwd-file= f Get wallet passphrase from file 'f'
  53. -q, --quiet Produce quieter output; suppress some warnings
  54. -r, --usr-randchars=n Get 'n' characters of additional randomness from user
  55. (min={cfg.min_urandchars}, max={cfg.max_urandchars}, default={cfg.usr_randchars})
  56. -S, --stdout Write wallet data to stdout instead of file
  57. -v, --verbose Produce more verbose output
  58. """,
  59. 'notes': """
  60. COMMAND NOTES:
  61. When joining with a master share, the master share must be listed first.
  62. The remaining shares may be listed in any order.
  63. The --id-str option is required only for master share joins. For ordinary
  64. joins it will be ignored.
  65. For usage examples, see the help screen for the 'mmgen-seedsplit' command.
  66. {n_pw}
  67. FMT CODES:
  68. {f}
  69. """
  70. },
  71. 'code': {
  72. 'options': lambda cfg, s: s.format(
  73. ms_min = MasterShareIdx.min_val,
  74. ms_max = MasterShareIdx.max_val,
  75. cfg = cfg,
  76. gc = gc,
  77. ),
  78. 'notes': lambda cfg, help_notes, s: s.format(
  79. f = help_notes('fmt_codes'),
  80. n_pw = help_notes('passwd'),
  81. )
  82. }
  83. }
  84. def print_shares_info():
  85. si = 0
  86. out = '\nComputed shares:\n'
  87. if cfg.master_share:
  88. fs = '{:3}: {}->{} ' + yellow('(master share #{}, split id ') + '{}' + yellow(', share count {})\n')
  89. out += fs.format(
  90. 1,
  91. shares[0].sid,
  92. share1.sid,
  93. master_idx,
  94. id_str.hl2(encl='‘’'),
  95. len(shares))
  96. si = 1
  97. for n, s in enumerate(shares[si:], si+1):
  98. out += f'{n:3}: {s.sid}\n'
  99. cfg._util.qmsg(out)
  100. cfg = Config(opts_data=opts_data)
  101. if len(cfg._args) + bool(cfg.hidden_incog_input_params) < 2:
  102. cfg._usage()
  103. if cfg.master_share:
  104. master_idx = MasterShareIdx(cfg.master_share)
  105. id_str = SeedSplitIDString(cfg.id_str or 'default')
  106. if cfg.id_str and not cfg.master_share:
  107. die(1,'--id-str option meaningless in context of non-master-share join')
  108. from .fileutil import check_infile
  109. from .wallet import check_wallet_extension
  110. for arg in cfg._args:
  111. check_wallet_extension(arg)
  112. check_infile(arg)
  113. from .ui import do_license_msg
  114. do_license_msg(cfg)
  115. cfg._util.qmsg('Input files:\n {}\n'.format('\n '.join(cfg._args)))
  116. shares = [Wallet(cfg).seed] if cfg.hidden_incog_input_params else []
  117. shares += [Wallet(cfg,fn).seed for fn in cfg._args]
  118. if cfg.master_share:
  119. share1 = SeedShareMasterJoining(cfg, master_idx, shares[0], id_str, len(shares)).derived_seed
  120. else:
  121. share1 = shares[0]
  122. print_shares_info()
  123. msg_r('Joining {n}-of-{n} XOR split...'.format(n=len(shares)))
  124. seed_out = Seed.join_shares(cfg, [share1] + shares[1:])
  125. msg(f'OK\nJoined Seed ID: {seed_out.sid.hl()}')
  126. Wallet(cfg,seed=seed_out).write_to_file()