Add private key bytes to private key converter, address default on keyfile decode

This commit is contained in:
nolash
2021-07-20 17:46:19 +02:00
parent fe83a048ce
commit 8571447ce7
10 changed files with 21 additions and 6 deletions

View File

@@ -12,6 +12,10 @@ from hexathon import (
logg = logging.getLogger(__name__)
def private_key_from_bytes(b):
return coincurve.PrivateKey(secret=b)
def public_key_bytes_to_address(pubk_bytes, result_format='hex'):
h = sha3.keccak_256()
logg.debug('public key bytes {}'.format(pubk_bytes.hex()))

View File

@@ -5,7 +5,7 @@ import logging
# local imports
from crypto_dev_signer.keystore import keyfile
import coincurve
from crypto_dev_signer.encoding import private_key_from_bytes
logg = logging.getLogger(__name__)
@@ -25,7 +25,7 @@ class Keystore:
def import_raw_key(self, b, password=None):
pk = coincurve.PrivateKey(secret=b)
pk = private_key_from_bytes(b)
return self.import_key(pk, password)

View File

@@ -15,13 +15,18 @@ from crypto_dev_signer.keystore.keyfile import (
from_file,
to_dict,
)
from crypto_dev_signer.encoding import (
private_key_to_address,
private_key_from_bytes,
)
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
argparser = argparse.ArgumentParser()
argparser.add_argument('-d', type=str, help='decrypt file')
argparser.add_argument('-d', '--decrypt', dest='d', type=str, help='decrypt file')
argparser.add_argument('--private-key', dest='private_key', action='store_true', help='output private key instead of address')
argparser.add_argument('-z', action='store_true', help='zero-length password')
argparser.add_argument('-k', type=str, help='load key from file')
argparser.add_argument('-v', action='store_true', help='be verbose')
@@ -31,8 +36,11 @@ if args.v:
logg.setLevel(logging.DEBUG)
mode = 'create'
secret = False
if args.d:
mode = 'decrypt'
if args.private_key:
secret = True
pk_hex = os.environ.get('PRIVATE_KEY')
if args.k != None:
@@ -55,6 +63,9 @@ def main():
except AssertionError:
sys.stderr.write('Invalid passphrase\n')
sys.exit(1)
if not secret:
pk = private_key_from_bytes(bytes.fromhex(r))
r = private_key_to_address(pk)
elif mode == 'create':
if passphrase == None:
passphrase = getpass.getpass('encryption phrase: ')