funga-eth/funga/eth/runnable/keyfile.py
lash e5cd1cad58
Implement PBKDF2 support in keyfile
---

Squashed commit of the following:

commit 4a4f76b19c
Author: idaapayo <idaapayo@gmail.com>
Date:   Mon Jan 24 20:12:34 2022 +0300

    remove unused import and renaming pbkdf2 default params

commit 23a94c3ba1
Author: idaapayo <idaapayo@gmail.com>
Date:   Mon Jan 24 20:07:22 2022 +0300

    defaulting to scrypt in to_dict fun

commit 1c0047398a
Author: idaapayo <idaapayo@gmail.com>
Date:   Mon Jan 24 15:12:38 2022 +0300

    making final review  changes

commit 0a4f3eaa98
Merge: b208533 903f659
Author: Mohamed Sohail <kamikazechaser@noreply.localhost>
Date:   Mon Jan 24 11:10:35 2022 +0000

    Merge branch 'master' into Ida/pbkdf2

commit b20853312d
Author: idaapayo <idaapayo@gmail.com>
Date:   Mon Jan 24 13:23:12 2022 +0300

    review changes with tests

commit b9c6db414b
Author: idaapayo <idaapayo@gmail.com>
Date:   Fri Jan 21 11:13:35 2022 +0300

    making review changes

commit 1f5d057a9a
Author: idaapayo <idaapayo@gmail.com>
Date:   Wed Jan 19 14:37:22 2022 +0300

    second pbkdf2 implementation

commit 01598a8c59
Author: idaapayo <idaapayo@gmail.com>
Date:   Wed Jan 19 13:49:29 2022 +0300

    pkdf2 implementation
2022-01-24 17:54:59 +00:00

90 lines
2.3 KiB
Python

# standard imports
import os
import logging
import sys
import json
import argparse
import getpass
# external impors
import coincurve
from hexathon import strip_0x
# local imports
from funga.error import DecryptError
from funga.eth.keystore.keyfile import (
from_file,
to_dict,
)
from funga.eth.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', '--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')
args = argparser.parse_args()
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:
f = open(args.k, 'r')
pk_hex = f.read(66)
f.close()
def main():
global pk_hex
passphrase = os.environ.get('WALLET_PASSPHRASE', os.environ.get('PASSPHRASE'))
if args.z:
passphrase = ''
r = None
if mode == 'decrypt':
if passphrase == None:
passphrase = getpass.getpass('decryption phrase: ')
try:
r = from_file(args.d, passphrase).hex()
except DecryptError:
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: ')
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)
pk = coincurve.PrivateKey(secret=pk_bytes)
o = to_dict(pk_bytes, passphrase)
r = json.dumps(o)
print(r)
if __name__ == '__main__':
main()