Added sanity check and automatic detection Signed-off-by: Lukas Kozak <luks.kozak@gmail.com>pull/1109/head
parent
5b5313a529
commit
db0e679176
@ -1,31 +1,50 @@ |
|||||||
#!/usr/bin/env python3 |
#!/usr/bin/env python3 |
||||||
|
|
||||||
import argparse |
import argparse, binascii |
||||||
import binascii |
import sys, glob, re, os |
||||||
import sys |
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description='Tool to convert your BLS key from binary to the accepted hex format.') |
parser = argparse.ArgumentParser(description='Tool to convert your BLS key from binary to the accepted hex format.') |
||||||
parser.add_argument('file') |
parser.add_argument('-i', '--input', type=str, metavar='input', help='path to BLS keyfile') |
||||||
parser.add_argument('-o', '--output', type=str, metavar='output', help='name your keyfile') |
|
||||||
args = parser.parse_args() |
args = parser.parse_args() |
||||||
|
|
||||||
|
## Finds first bls keyfile in the directory or takes file given by user |
||||||
|
if args.input: |
||||||
|
inputName = args.input |
||||||
|
print(inputName) |
||||||
|
else: |
||||||
|
keylist = glob.glob('UTC*bls_*') |
||||||
|
inputName = keylist[0] |
||||||
|
|
||||||
|
## Read |
||||||
try: |
try: |
||||||
with open(args.file, 'rb') as inputFile: |
with open(inputName, 'rb') as inputFile: |
||||||
binFormat = inputFile.read() |
binFormat = inputFile.read() |
||||||
except IOError: |
except IOError: |
||||||
print('File couldn\'t be opened.') |
print('File couldn\'t be opened.') |
||||||
sys.exit(1) |
sys.exit(1) |
||||||
|
|
||||||
|
## Sanity check and conversion |
||||||
|
try: |
||||||
|
binascii.unhexlify(binFormat) |
||||||
|
except binascii.Error: |
||||||
hexFormat = binascii.hexlify(binFormat) |
hexFormat = binascii.hexlify(binFormat) |
||||||
|
else: |
||||||
|
print("Keyfile is already in text format, no conversion required!") |
||||||
|
sys.exit(0) |
||||||
|
|
||||||
|
## Name output appropriately based on the public key |
||||||
|
matchObj = re.search('UTC.*bls_(.*)', inputName) |
||||||
|
|
||||||
if args.output is not None: |
if matchObj: |
||||||
outputName = args.output |
outputName = matchObj.group(1) + ".key" |
||||||
else: |
else: |
||||||
outputName = 'PUB.key' |
outputName = 'bls.key' |
||||||
|
|
||||||
|
## Write |
||||||
try: |
try: |
||||||
with open(outputName, 'wt') as outputFile: |
with open(outputName, 'w') as outputFile: |
||||||
outputFile.write(hexFormat.decode('ascii')) |
outputFile.write(hexFormat.decode('ascii')) |
||||||
|
print("Keyfile converted to: {}".format(outputName)) |
||||||
except IOError: |
except IOError: |
||||||
print('File couldn\'t be written.') |
print('File couldn\'t be written.') |
||||||
sys.exit(1) |
sys.exit(1) |
||||||
|
Loading…
Reference in new issue