Add private key input to keyfile crate, handle empty bytes in in serialize

This commit is contained in:
nolash 2021-03-22 18:42:56 +01:00
parent 9dda0e6dea
commit 3b1b550136
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
4 changed files with 40 additions and 21 deletions

View File

@ -2,11 +2,12 @@
import logging import logging
import binascii import binascii
# third-party imports # external imports
from rlp import encode as rlp_encode from rlp import encode as rlp_encode
from hexathon import (
# local imports strip_0x,
from crypto_dev_signer.common import strip_hex_prefix, add_hex_prefix add_0x,
)
logg = logging.getLogger(__name__) logg = logging.getLogger(__name__)
@ -23,9 +24,12 @@ class Transaction:
class EIP155Transaction: class EIP155Transaction:
def __init__(self, tx, nonce, chainId=1): def __init__(self, tx, nonce, chainId=1):
to = None
to = binascii.unhexlify(strip_hex_prefix(tx['to'])) data = None
data = binascii.unhexlify(strip_hex_prefix(tx['data'])) if tx['to'] != None:
to = binascii.unhexlify(strip_0x(tx['to'], allow_empty=True))
if tx['data'] != None:
data = binascii.unhexlify(strip_0x(tx['data'], allow_empty=True))
gas_price = None gas_price = None
start_gas = None start_gas = None
@ -75,7 +79,7 @@ class EIP155Transaction:
self.v = chainId self.v = chainId
self.r = b'' self.r = b''
self.s = b'' self.s = b''
self.sender = strip_hex_prefix(tx['from']) self.sender = strip_0x(tx['from'])
def __canonical_order(self): def __canonical_order(self):
@ -93,6 +97,7 @@ class EIP155Transaction:
return s return s
def bytes_serialize(self): def bytes_serialize(self):
s = self.__canonical_order() s = self.__canonical_order()
b = b'' b = b''
@ -108,15 +113,15 @@ class EIP155Transaction:
def serialize(self): def serialize(self):
tx = { tx = {
'nonce': add_hex_prefix(self.nonce.hex()), 'nonce': add_0x(self.nonce.hex(), allow_empty=True),
'gasPrice': add_hex_prefix(self.gas_price.hex()), 'gasPrice': add_0x(self.gas_price.hex()),
'gas': add_hex_prefix(self.start_gas.hex()), 'gas': add_0x(self.start_gas.hex()),
'to': add_hex_prefix(self.to.hex()), 'to': add_0x(self.to.hex()),
'value': add_hex_prefix(self.value.hex()), 'value': add_0x(self.value.hex(), allow_empty=True),
'data': add_hex_prefix(self.data.hex()), 'data': add_0x(self.data.hex()),
'v': add_hex_prefix(self.v.hex()), 'v': add_0x(self.v.hex(), allow_empty=True),
'r': add_hex_prefix(self.r.hex()), 'r': add_0x(self.r.hex(), allow_empty=True),
's': add_hex_prefix(self.s.hex()), 's': add_0x(self.s.hex(), allow_empty=True),
} }
if tx['data'] == '': if tx['data'] == '':
tx['data'] = '0x' tx['data'] = '0x'

View File

@ -44,7 +44,7 @@ class Keystore:
return self.import_raw_key(private_key, password) return self.import_raw_key(private_key, password)
def import_keystore_file(self, keystore_file, password=''): def import_keystore_file(self, keystore_file, password=''):
private_key = keyfile.from_file(keystore_file) private_key = keyfile.from_file(keystore_file, password)
#return self.import_keystore_data(keystore_content, password) #return self.import_keystore_data(keystore_content, password)
return self.import_raw_key(private_key, password) return self.import_raw_key(private_key)
#return kes #return kes

View File

@ -13,7 +13,6 @@ import sha3
# local imports # local imports
from crypto_dev_signer.encoding import private_key_to_address from crypto_dev_signer.encoding import private_key_to_address
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger() logg = logging.getLogger()
algo_keywords = [ algo_keywords = [

View File

@ -8,6 +8,7 @@ import getpass
# external impors # external impors
import coincurve import coincurve
from hexathon import strip_0x
# local imports # local imports
from crypto_dev_signer.keystore.keyfile import ( from crypto_dev_signer.keystore.keyfile import (
@ -21,6 +22,7 @@ logg = logging.getLogger()
argparser = argparse.ArgumentParser() argparser = argparse.ArgumentParser()
argparser.add_argument('-d', type=str, help='decrypt file') argparser.add_argument('-d', type=str, help='decrypt file')
argparser.add_argument('-k', type=str, help='load key from file')
argparser.add_argument('-v', action='store_true', help='be verbose') argparser.add_argument('-v', action='store_true', help='be verbose')
args = argparser.parse_args() args = argparser.parse_args()
@ -31,7 +33,15 @@ mode = 'create'
if args.d: if args.d:
mode = 'decrypt' 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()
def main(): def main():
global pk_hex
passphrase = os.environ.get('PASSPHRASE') passphrase = os.environ.get('PASSPHRASE')
r = None r = None
if mode == 'decrypt': if mode == 'decrypt':
@ -45,7 +55,12 @@ def main():
elif mode == 'create': elif mode == 'create':
if passphrase == None: if passphrase == None:
passphrase = getpass.getpass('encryption phrase: ') passphrase = getpass.getpass('encryption phrase: ')
pk_bytes = os.urandom(32) 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) pk = coincurve.PrivateKey(secret=pk_bytes)
o = to_dict(pk, passphrase) o = to_dict(pk, passphrase)
r = json.dumps(o) r = json.dumps(o)