github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/cli/sawtooth_cli/keygen.py (about)

     1  # Copyright 2016 Intel Corporation
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #     http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  # ------------------------------------------------------------------------------
    15  
    16  from __future__ import print_function
    17  
    18  import getpass
    19  import os
    20  import sys
    21  
    22  from sawtooth_cli.exceptions import CliException
    23  from sawtooth_signing import create_context
    24  
    25  
    26  def add_keygen_parser(subparsers, parent_parser):
    27      parser = subparsers.add_parser(
    28          'keygen',
    29          help='Creates user signing keys',
    30          description='Generates keys with which the user can sign '
    31          'transactions and batches.',
    32          epilog='The private and public key files are stored in '
    33          '<key-dir>/<key-name>.priv and <key-dir>/<key-name>.pub. '
    34          '<key-dir> defaults to ~/.sawtooth and <key-name> defaults to $USER.',
    35          parents=[parent_parser])
    36  
    37      parser.add_argument(
    38          'key_name',
    39          help='specify the name of the key to create',
    40          nargs='?')
    41  
    42      parser.add_argument(
    43          '--key-dir',
    44          help="specify the directory for the key files")
    45  
    46      parser.add_argument(
    47          '--force',
    48          help="overwrite files if they exist",
    49          action='store_true')
    50  
    51      parser.add_argument(
    52          '-q',
    53          '--quiet',
    54          help="do not display output",
    55          action='store_true')
    56  
    57  
    58  def do_keygen(args):
    59      if args.key_name is not None:
    60          key_name = args.key_name
    61      else:
    62          key_name = getpass.getuser()
    63  
    64      if args.key_dir is not None:
    65          key_dir = args.key_dir
    66          if not os.path.exists(key_dir):
    67              raise CliException('no such directory: {}'.format(key_dir))
    68      else:
    69          key_dir = os.path.join(os.path.expanduser('~'), '.sawtooth', 'keys')
    70          if not os.path.exists(key_dir):
    71              if not args.quiet:
    72                  print('creating key directory: {}'.format(key_dir))
    73              try:
    74                  os.makedirs(key_dir, 0o755)
    75              except IOError as e:
    76                  raise CliException('IOError: {}'.format(str(e)))
    77  
    78      priv_filename = os.path.join(key_dir, key_name + '.priv')
    79      pub_filename = os.path.join(key_dir, key_name + '.pub')
    80  
    81      if not args.force:
    82          file_exists = False
    83          for filename in [priv_filename, pub_filename]:
    84              if os.path.exists(filename):
    85                  file_exists = True
    86                  print('file exists: {}'.format(filename), file=sys.stderr)
    87          if file_exists:
    88              raise CliException(
    89                  'files exist, rerun with --force to overwrite existing files')
    90  
    91      context = create_context('secp256k1')
    92      private_key = context.new_random_private_key()
    93      public_key = context.get_public_key(private_key)
    94  
    95      try:
    96          priv_exists = os.path.exists(priv_filename)
    97          with open(priv_filename, 'w') as priv_fd:
    98              if not args.quiet:
    99                  if priv_exists:
   100                      print('overwriting file: {}'.format(priv_filename))
   101                  else:
   102                      print('writing file: {}'.format(priv_filename))
   103              priv_fd.write(private_key.as_hex())
   104              priv_fd.write('\n')
   105              # Set the private key u+rw g+r
   106              os.chmod(priv_filename, 0o640)
   107  
   108          pub_exists = os.path.exists(pub_filename)
   109          with open(pub_filename, 'w') as pub_fd:
   110              if not args.quiet:
   111                  if pub_exists:
   112                      print('overwriting file: {}'.format(pub_filename))
   113                  else:
   114                      print('writing file: {}'.format(pub_filename))
   115              pub_fd.write(public_key.as_hex())
   116              pub_fd.write('\n')
   117              # Set the public key u+rw g+r o+r
   118              os.chmod(pub_filename, 0o644)
   119  
   120      except IOError as ioe:
   121          raise CliException('IOError: {}'.format(str(ioe)))