funga

Signer and keystore daemon and library for cryptocurrency software development
Log | Files | Refs | README | LICENSE

commit 3b1b5501364285934cf6d30a11438119999fb536
parent 9dda0e6dea4678a55f0efc08fc50d7e57b0d752f
Author: nolash <dev@holbrook.no>
Date:   Mon, 22 Mar 2021 18:42:56 +0100

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

Diffstat:
Mcrypto_dev_signer/eth/transaction.py | 39++++++++++++++++++++++-----------------
Mcrypto_dev_signer/keystore/interface.py | 4++--
Mcrypto_dev_signer/keystore/keyfile.py | 1-
Mcrypto_dev_signer/runnable/keyfile.py | 17++++++++++++++++-
4 files changed, 40 insertions(+), 21 deletions(-)

diff --git a/crypto_dev_signer/eth/transaction.py b/crypto_dev_signer/eth/transaction.py @@ -2,11 +2,12 @@ import logging import binascii -# third-party imports +# external imports from rlp import encode as rlp_encode - -# local imports -from crypto_dev_signer.common import strip_hex_prefix, add_hex_prefix +from hexathon import ( + strip_0x, + add_0x, + ) logg = logging.getLogger(__name__) @@ -23,9 +24,12 @@ class Transaction: class EIP155Transaction: def __init__(self, tx, nonce, chainId=1): - - to = binascii.unhexlify(strip_hex_prefix(tx['to'])) - data = binascii.unhexlify(strip_hex_prefix(tx['data'])) + to = None + data = None + 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 start_gas = None @@ -75,7 +79,7 @@ class EIP155Transaction: self.v = chainId self.r = b'' self.s = b'' - self.sender = strip_hex_prefix(tx['from']) + self.sender = strip_0x(tx['from']) def __canonical_order(self): @@ -93,6 +97,7 @@ class EIP155Transaction: return s + def bytes_serialize(self): s = self.__canonical_order() b = b'' @@ -108,15 +113,15 @@ class EIP155Transaction: def serialize(self): tx = { - 'nonce': add_hex_prefix(self.nonce.hex()), - 'gasPrice': add_hex_prefix(self.gas_price.hex()), - 'gas': add_hex_prefix(self.start_gas.hex()), - 'to': add_hex_prefix(self.to.hex()), - 'value': add_hex_prefix(self.value.hex()), - 'data': add_hex_prefix(self.data.hex()), - 'v': add_hex_prefix(self.v.hex()), - 'r': add_hex_prefix(self.r.hex()), - 's': add_hex_prefix(self.s.hex()), + 'nonce': add_0x(self.nonce.hex(), allow_empty=True), + 'gasPrice': add_0x(self.gas_price.hex()), + 'gas': add_0x(self.start_gas.hex()), + 'to': add_0x(self.to.hex()), + 'value': add_0x(self.value.hex(), allow_empty=True), + 'data': add_0x(self.data.hex()), + 'v': add_0x(self.v.hex(), allow_empty=True), + 'r': add_0x(self.r.hex(), allow_empty=True), + 's': add_0x(self.s.hex(), allow_empty=True), } if tx['data'] == '': tx['data'] = '0x' diff --git a/crypto_dev_signer/keystore/interface.py b/crypto_dev_signer/keystore/interface.py @@ -44,7 +44,7 @@ class Keystore: return self.import_raw_key(private_key, 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_raw_key(private_key, password) + return self.import_raw_key(private_key) #return kes diff --git a/crypto_dev_signer/keystore/keyfile.py b/crypto_dev_signer/keystore/keyfile.py @@ -13,7 +13,6 @@ import sha3 # local imports from crypto_dev_signer.encoding import private_key_to_address -logging.basicConfig(level=logging.DEBUG) logg = logging.getLogger() algo_keywords = [ diff --git a/crypto_dev_signer/runnable/keyfile.py b/crypto_dev_signer/runnable/keyfile.py @@ -8,6 +8,7 @@ import getpass # external impors import coincurve +from hexathon import strip_0x # local imports from crypto_dev_signer.keystore.keyfile import ( @@ -21,6 +22,7 @@ logg = logging.getLogger() argparser = argparse.ArgumentParser() 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') args = argparser.parse_args() @@ -31,7 +33,15 @@ mode = 'create' if args.d: 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(): + global pk_hex + passphrase = os.environ.get('PASSPHRASE') r = None if mode == 'decrypt': @@ -45,7 +55,12 @@ def main(): elif mode == 'create': if passphrase == None: 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) o = to_dict(pk, passphrase) r = json.dumps(o)