funga/crypto_dev_signer/runnable/keyfile.py

76 lines
1.8 KiB
Python
Raw Normal View History

2021-03-17 15:34:51 +01:00
# standard imports
2021-03-17 21:51:43 +01:00
import os
2021-03-17 15:34:51 +01:00
import logging
import sys
2021-03-17 21:51:43 +01:00
import json
import argparse
2021-03-17 22:08:20 +01:00
import getpass
2021-03-17 21:51:43 +01:00
# external impors
import coincurve
from hexathon import strip_0x
2021-03-17 15:34:51 +01:00
# local imports
2021-03-17 21:51:43 +01:00
from crypto_dev_signer.keystore.keyfile import (
from_file,
to_dict,
)
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
argparser = argparse.ArgumentParser()
argparser.add_argument('-d', type=str, help='decrypt file')
argparser.add_argument('-z', action='store_true', help='zero-length password')
argparser.add_argument('-k', type=str, help='load key from file')
2021-03-17 21:51:43 +01:00
argparser.add_argument('-v', action='store_true', help='be verbose')
args = argparser.parse_args()
if args.v:
logg.setLevel(logging.DEBUG)
2021-03-17 22:08:20 +01:00
mode = 'create'
2021-03-17 21:51:43 +01:00
if args.d:
2021-03-17 22:08:20 +01:00
mode = 'decrypt'
pk_hex = os.environ.get('PRIVATE_KEY')
if args.k != None:
f = open(args.k, 'r')
pk_hex = f.read(66)
f.close()
2021-03-17 22:08:20 +01:00
def main():
global pk_hex
2021-03-17 22:08:20 +01:00
passphrase = os.environ.get('PASSPHRASE')
if args.z:
passphrase = ''
2021-03-17 22:08:20 +01:00
r = None
if mode == 'decrypt':
if passphrase == None:
passphrase = getpass.getpass('decryption phrase: ')
try:
r = from_file(args.d, passphrase).hex()
except AssertionError:
sys.stderr.write('Invalid passphrase\n')
sys.exit(1)
elif mode == 'create':
if passphrase == None:
passphrase = getpass.getpass('encryption phrase: ')
pk_bytes = None
if pk_hex != None:
pk_hex = strip_0x(pk_hex)
pk_bytes = bytes.fromhex(pk_hex)
else:
pk_bytes = os.urandom(32)
2021-03-17 22:08:20 +01:00
pk = coincurve.PrivateKey(secret=pk_bytes)
o = to_dict(pk_bytes, passphrase)
2021-03-17 22:08:20 +01:00
r = json.dumps(o)
print(r)
if __name__ == '__main__':
main()