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']))
|
|
|
|
|
|
|
|
self.nonce = nonce
|
|
|
|
self.gas_price = int(tx['gasPrice'])
|
|
|
|
self.start_gas = int(tx['gas'])
|
|
|
|
self.to = to
|
|
|
|
self.value = int(tx['value'])
|
|
|
|
self.data = data
|
|
|
|
self.v = chainId
|
2020-08-06 11:07:18 +02:00
|
|
|
self.r = b''
|
|
|
|
self.s = b''
|
2020-08-05 22:00:23 +02:00
|
|
|
self.sender = strip_hex_prefix(tx['from'])
|
2020-08-04 23:41:31 +02:00
|
|
|
|
|
|
|
|
2020-08-05 22:00:23 +02:00
|
|
|
def rlp_serialize(self):
|
2020-08-04 23:41:31 +02:00
|
|
|
b = self.nonce.to_bytes(8, byteorder='little')
|
|
|
|
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)
|
2020-08-05 22:00:23 +02:00
|
|
|
|
|
|
|
def serialize(self):
|
|
|
|
return {
|
2020-08-07 23:01:49 +02:00
|
|
|
'nonce': add_hex_prefix(hex(self.nonce)),
|
|
|
|
'gasPrice': add_hex_prefix(hex(self.gas_price)),
|
|
|
|
'gas': add_hex_prefix(hex(self.start_gas)),
|
|
|
|
'to': add_hex_prefix(self.to.hex()),
|
|
|
|
'value': add_hex_prefix(hex(self.value)),
|
|
|
|
'data': add_hex_prefix(self.data.hex()),
|
|
|
|
'v': add_hex_prefix(hex(self.v)),
|
|
|
|
'r': add_hex_prefix(self.r.hex()),
|
|
|
|
's': add_hex_prefix(self.s.hex()),
|
2020-08-05 22:00:23 +02:00
|
|
|
}
|