funga/crypto_dev_signer/eth/transaction.py

117 lines
3.0 KiB
Python
Raw Normal View History

2020-08-08 10:45:37 +02:00
# standard imports
2020-08-04 23:41:31 +02:00
import logging
import binascii
2020-08-08 10:45:37 +02:00
# third-party imports
2020-08-04 23:41:31 +02:00
from rlp import encode as rlp_encode
2020-08-08 10:45:37 +02:00
# local imports
2020-08-08 11:33:15 +02:00
from crypto_dev_signer.common import strip_hex_prefix, add_hex_prefix
2020-08-04 23:41:31 +02:00
logg = logging.getLogger(__name__)
2020-08-08 10:45:37 +02:00
2020-08-04 23:41:31 +02:00
class Transaction:
2020-08-06 11:07:18 +02:00
def rlp_serialize(self):
raise NotImplementedError
def serialize(self):
raise NotImplementedError
class EIP155Transaction:
2020-08-04 23:41:31 +02:00
def __init__(self, tx, nonce, chainId=1):
to = binascii.unhexlify(strip_hex_prefix(tx['to']))
data = binascii.unhexlify(strip_hex_prefix(tx['data']))
2020-10-26 09:02:29 +01:00
gas_price = None
start_gas = None
value = None
try:
gas_price = int(tx['gasPrice'])
except ValueError:
gas_price = int(tx['gasPrice'], 16)
2021-01-12 14:50:52 +01:00
byts = ((gas_price.bit_length()-1)/8)+1
gas_price = gas_price.to_bytes(int(byts), 'big')
2020-10-26 09:02:29 +01:00
try:
start_gas = int(tx['gas'])
except ValueError:
start_gas = int(tx['gas'], 16)
2021-01-12 14:50:52 +01:00
byts = ((start_gas.bit_length()-1)/8)+1
start_gas = start_gas.to_bytes(int(byts), 'big')
2020-10-26 09:02:29 +01:00
try:
value = int(tx['value'])
except ValueError:
value = int(tx['value'], 16)
2021-01-12 14:50:52 +01:00
byts = ((value.bit_length()-1)/8)+1
value = value.to_bytes(int(byts), 'big')
2020-10-26 09:02:29 +01:00
try:
nonce = int(nonce)
except ValueError:
nonce = int(nonce, 16)
2021-01-12 14:50:52 +01:00
byts = ((nonce.bit_length()-1)/8)+1
nonce = nonce.to_bytes(int(byts), 'big')
try:
chainId = int(chainId)
except ValueError:
chainId = int(chainId, 16)
byts = ((chainId.bit_length()-1)/8)+1
chainId = chainId.to_bytes(int(byts), 'big')
2020-10-26 09:02:29 +01:00
2020-08-04 23:41:31 +02:00
self.nonce = nonce
2020-10-26 09:02:29 +01:00
self.gas_price = gas_price
self.start_gas = start_gas
2020-08-04 23:41:31 +02:00
self.to = to
2020-10-26 09:02:29 +01:00
self.value = value
2020-08-04 23:41:31 +02:00
self.data = data
self.v = chainId
2020-08-06 11:07:18 +02:00
self.r = b''
self.s = b''
self.sender = strip_hex_prefix(tx['from'])
2020-08-04 23:41:31 +02:00
def rlp_serialize(self):
2020-08-04 23:41:31 +02:00
s = [
self.nonce,
self.gas_price,
self.start_gas,
self.to,
self.value,
self.data,
self.v,
self.r,
self.s,
]
return rlp_encode(s)
def serialize(self):
2021-01-12 14:50:52 +01:00
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()),
2021-01-12 14:50:52 +01:00
'value': add_hex_prefix(self.value.hex()),
'data': add_hex_prefix(self.data.hex()),
2021-01-12 14:50:52 +01:00
'v': add_hex_prefix(self.v.hex()),
'r': add_hex_prefix(self.r.hex()),
's': add_hex_prefix(self.s.hex()),
}
2021-01-12 14:50:52 +01:00
if tx['data'] == '':
tx['data'] = '0x'
if tx['value'] == '':
tx['value'] = '0x00'
if tx['nonce'] == '':
tx['nonce'] = '0x00'
return tx