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
|
2021-03-22 18:42:56 +01:00
|
|
|
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')
|
2021-06-09 15:17:19 +02:00
|
|
|
argparser.add_argument('-z', action='store_true', help='zero-length password')
|
2021-03-22 18:42:56 +01:00
|
|
|
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'
|
|
|
|
|
2021-03-22 18:42:56 +01:00
|
|
|
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():
|
2021-03-22 18:42:56 +01:00
|
|
|
global pk_hex
|
|
|
|
|
2021-03-17 22:08:20 +01:00
|
|
|
passphrase = os.environ.get('PASSPHRASE')
|
2021-06-09 15:17:19 +02:00
|
|
|
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: ')
|
2021-03-22 18:42:56 +01:00
|
|
|
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)
|
2021-04-09 16:05:16 +02:00
|
|
|
o = to_dict(pk_bytes, passphrase)
|
2021-03-17 22:08:20 +01:00
|
|
|
r = json.dumps(o)
|
|
|
|
|
|
|
|
print(r)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|