github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/cli/sawtooth_cli/admin_command/keygen.py (about) 1 # Copyright 2017 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 import os 17 import sys 18 19 from sawtooth_cli.exceptions import CliException 20 from sawtooth_cli.admin_command.config import get_key_dir 21 from sawtooth_signing import create_context 22 23 24 def add_keygen_parser(subparsers, parent_parser): 25 """Adds subparser command and flags for 'keygen' command. 26 27 Args: 28 subparsers (:obj:`ArguementParser`): The subcommand parsers. 29 parent_parser (:obj:`ArguementParser`): The parent of the subcomman 30 parsers. 31 """ 32 description = 'Generates keys for the validator to use when signing blocks' 33 34 epilog = ( 35 'The private and public key pair is stored in ' 36 '/etc/sawtooth/keys/<key-name>.priv and ' 37 '/etc/sawtooth/keys/<key-name>.pub.' 38 ) 39 40 parser = subparsers.add_parser( 41 'keygen', 42 help=description, 43 description=description + '.', 44 epilog=epilog, 45 parents=[parent_parser]) 46 47 parser.add_argument( 48 'key_name', 49 help='name of the key to create', 50 nargs='?') 51 52 parser.add_argument( 53 '--force', 54 help="overwrite files if they exist", 55 action='store_true') 56 57 parser.add_argument( 58 '-q', 59 '--quiet', 60 help="do not display output", 61 action='store_true') 62 63 64 def do_keygen(args): 65 """Executes the key generation operation, given the parsed arguments. 66 67 Args: 68 args (:obj:`Namespace`): The parsed args. 69 """ 70 if args.key_name is not None: 71 key_name = args.key_name 72 else: 73 key_name = 'validator' 74 75 key_dir = get_key_dir() 76 77 if not os.path.exists(key_dir): 78 raise CliException("Key directory does not exist: {}".format(key_dir)) 79 80 priv_filename = os.path.join(key_dir, key_name + '.priv') 81 pub_filename = os.path.join(key_dir, key_name + '.pub') 82 83 if not args.force: 84 file_exists = False 85 for filename in [priv_filename, pub_filename]: 86 if os.path.exists(filename): 87 file_exists = True 88 print('file exists: {}'.format(filename), file=sys.stderr) 89 if file_exists: 90 raise CliException( 91 'files exist, rerun with --force to overwrite existing files') 92 93 context = create_context('secp256k1') 94 95 private_key = context.new_random_private_key() 96 public_key = context.get_public_key(private_key) 97 98 try: 99 priv_exists = os.path.exists(priv_filename) 100 with open(priv_filename, 'w') as priv_fd: 101 if not args.quiet: 102 if priv_exists: 103 print('overwriting file: {}'.format(priv_filename)) 104 else: 105 print('writing file: {}'.format(priv_filename)) 106 priv_fd.write(private_key.as_hex()) 107 priv_fd.write('\n') 108 # Get the uid and gid of the key directory 109 keydir_info = os.stat(key_dir) 110 keydir_gid = keydir_info.st_gid 111 keydir_uid = keydir_info.st_uid 112 # Set user and group on keys to the user/group of the key directory 113 os.chown(priv_filename, keydir_uid, keydir_gid) 114 # Set the private key u+rw g+r 115 os.chmod(priv_filename, 0o640) 116 117 pub_exists = os.path.exists(pub_filename) 118 with open(pub_filename, 'w') as pub_fd: 119 if not args.quiet: 120 if pub_exists: 121 print('overwriting file: {}'.format(pub_filename)) 122 else: 123 print('writing file: {}'.format(pub_filename)) 124 pub_fd.write(public_key.as_hex()) 125 pub_fd.write('\n') 126 # Set user and group on keys to the user/group of the key directory 127 os.chown(pub_filename, keydir_uid, keydir_gid) 128 # Set the public key u+rw g+r o+r 129 os.chmod(pub_filename, 0o644) 130 131 except IOError as ioe: 132 raise CliException('IOError: {}'.format(str(ioe)))