main_seedjoin.py 5.0 KB

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