main_seedjoin.py 5.0 KB

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