2022-04-10 21:03:23 +02:00
|
|
|
# standard imports
|
|
|
|
import logging
|
|
|
|
|
|
|
|
# external imports
|
|
|
|
from chaind.error import TxSourceError
|
2022-05-04 09:26:17 +02:00
|
|
|
from chainlib.eth.address import (
|
|
|
|
is_checksum_address,
|
|
|
|
to_checksum_address,
|
|
|
|
)
|
2022-04-10 21:03:23 +02:00
|
|
|
from chainlib.eth.tx import unpack
|
|
|
|
from chainlib.eth.gas import Gas
|
|
|
|
from hexathon import (
|
|
|
|
add_0x,
|
|
|
|
strip_0x,
|
|
|
|
)
|
|
|
|
from funga.eth.transaction import EIP155Transaction
|
|
|
|
|
|
|
|
logg = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class Processor:
|
|
|
|
|
2022-05-04 09:26:17 +02:00
|
|
|
def __init__(self, resolver, source, use_checksum=True):
|
2022-04-10 21:03:23 +02:00
|
|
|
self.resolver = resolver
|
|
|
|
self.source = source
|
|
|
|
self.processor = []
|
2022-05-04 09:26:17 +02:00
|
|
|
self.safe = use_checksum
|
2022-04-10 23:27:36 +02:00
|
|
|
self.conn = None
|
2022-04-10 21:03:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
def add_processor(self, processor):
|
|
|
|
self.processor.append(processor)
|
|
|
|
|
|
|
|
|
2022-04-10 23:27:36 +02:00
|
|
|
def load(self, conn, process=True):
|
|
|
|
self.conn = conn
|
2022-04-10 21:03:23 +02:00
|
|
|
for processor in self.processor:
|
|
|
|
self.content = processor.load(self.source)
|
|
|
|
if self.content != None:
|
|
|
|
if process:
|
|
|
|
try:
|
|
|
|
self.process()
|
|
|
|
except Exception as e:
|
|
|
|
raise TxSourceError('invalid source contents: {}'.format(str(e)))
|
|
|
|
return self.content
|
|
|
|
raise TxSourceError('unparseable source')
|
|
|
|
|
|
|
|
|
|
|
|
# 0: recipient
|
|
|
|
# 1: amount
|
|
|
|
# 2: token identifier (optional, when not specified network gas token will be used)
|
|
|
|
# 3: gas amount (optional)
|
|
|
|
def process(self):
|
|
|
|
txs = []
|
|
|
|
for i, r in enumerate(self.content):
|
|
|
|
logg.debug('processing {}'.format(r))
|
2022-05-04 09:26:17 +02:00
|
|
|
address = r[0]
|
|
|
|
if self.safe:
|
|
|
|
if not is_checksum_address(address):
|
|
|
|
raise ValueError('invalid checksum address {} in record {}'.format(address, i))
|
|
|
|
else:
|
|
|
|
address = to_checksum_address(address)
|
|
|
|
|
|
|
|
self.content[i][0] = add_0x(address)
|
2022-04-10 21:03:23 +02:00
|
|
|
try:
|
|
|
|
self.content[i][1] = int(r[1])
|
|
|
|
except ValueError:
|
|
|
|
self.content[i][1] = int(strip_0x(r[1]), 16)
|
|
|
|
native_token_value = 0
|
|
|
|
|
|
|
|
if len(self.content[i]) == 3:
|
|
|
|
self.content[i].append(native_token_value)
|
|
|
|
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
self.resolver.reset()
|
|
|
|
self.cursor = 0
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
def __next__(self):
|
|
|
|
if self.cursor == len(self.content):
|
|
|
|
raise StopIteration()
|
|
|
|
|
|
|
|
r = self.content[self.cursor]
|
|
|
|
|
|
|
|
value = r[1]
|
|
|
|
gas_value = 0
|
|
|
|
try:
|
|
|
|
gas_value = r[3]
|
|
|
|
except IndexError:
|
|
|
|
pass
|
|
|
|
logg.debug('gasvalue {}'.format(gas_value))
|
|
|
|
data = '0x'
|
|
|
|
|
2022-04-10 23:42:56 +02:00
|
|
|
executable_address = None
|
|
|
|
try:
|
|
|
|
executable_address = r[2]
|
|
|
|
except IndexError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
tx = self.resolver.create(self.conn, r[0], gas_value, data=data, token_value=value, executable_address=executable_address)
|
2022-04-10 21:03:23 +02:00
|
|
|
v = self.resolver.sign(tx)
|
|
|
|
|
|
|
|
self.cursor += 1
|
|
|
|
|
|
|
|
return v
|
|
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
names = []
|
|
|
|
for s in self.processor:
|
|
|
|
names.append(str(s))
|
|
|
|
return ','.join(names)
|