2021-10-06 07:44:53 +02:00
|
|
|
# standard imports
|
|
|
|
import re
|
|
|
|
import logging
|
|
|
|
|
|
|
|
# external imports
|
2021-10-06 07:52:19 +02:00
|
|
|
from chainlib.eth.contract import (
|
|
|
|
ABIContractType,
|
|
|
|
ABIContractEncoder,
|
|
|
|
)
|
2021-10-06 07:44:53 +02:00
|
|
|
|
|
|
|
logg = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2021-10-06 07:52:19 +02:00
|
|
|
class CLIEncoder(ABIContractEncoder):
|
2021-10-06 07:44:53 +02:00
|
|
|
|
|
|
|
__re_uint = r'^([uU])[int]*([0-9]+)?$'
|
|
|
|
__re_bytes = r'^([bB])[ytes]*([0-9]+)?$'
|
|
|
|
__re_string = r'^([sS])[tring]*$'
|
|
|
|
__translations = [
|
|
|
|
'to_uint',
|
|
|
|
'to_bytes',
|
|
|
|
'to_string',
|
|
|
|
]
|
|
|
|
|
2021-10-06 07:52:19 +02:00
|
|
|
def __init__(self, signature=None):
|
|
|
|
super(CLIEncoder, self).__init__()
|
|
|
|
self.signature = signature
|
|
|
|
if signature != None:
|
|
|
|
self.method(signature)
|
|
|
|
|
2021-10-06 07:44:53 +02:00
|
|
|
def to_uint(self, typ):
|
|
|
|
s = None
|
|
|
|
a = None
|
|
|
|
m = re.match(self.__re_uint, typ)
|
|
|
|
if m == None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
n = m.group(2)
|
|
|
|
if m.group(2) == None:
|
|
|
|
n = 256
|
|
|
|
s = 'UINT256'.format(m.group(2))
|
|
|
|
a = getattr(ABIContractType, s)
|
|
|
|
return (s, a)
|
|
|
|
|
|
|
|
|
|
|
|
def to_bytes(self, typ):
|
|
|
|
s = None
|
|
|
|
a = None
|
|
|
|
m = re.match(self.__re_bytes, typ)
|
|
|
|
if m == None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
n = m.group(2)
|
|
|
|
if n == None:
|
|
|
|
n = 32
|
|
|
|
s = 'BYTES{}'.format(n)
|
|
|
|
a = getattr(ABIContractType, s)
|
|
|
|
return (s, a)
|
|
|
|
|
|
|
|
|
|
|
|
def to_string(self, typ):
|
|
|
|
m = re.match(self.__re_string, typ)
|
|
|
|
if m == None:
|
|
|
|
return None
|
|
|
|
s = 'STRING'
|
|
|
|
a = getattr(ABIContractType, s)
|
|
|
|
return (s, a)
|
|
|
|
|
|
|
|
|
|
|
|
def translate_type(self, typ):
|
|
|
|
r = None
|
|
|
|
for tr in self.__translations:
|
|
|
|
r = getattr(self, tr)(typ)
|
|
|
|
if r != None:
|
|
|
|
break
|
|
|
|
if r == None:
|
|
|
|
raise ValueError('no translation for type {}'.format(typ))
|
|
|
|
logg.debug('type {} translated to {}'.format(typ, r[0]))
|
|
|
|
return r[1]
|
2021-10-06 07:52:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
def add_from(self, arg):
|
|
|
|
logg.debug('arg {}'.format(arg))
|
|
|
|
(typ, val) = arg.split(':', maxsplit=1)
|
|
|
|
real_typ = self.translate_type(typ)
|
|
|
|
if self.signature != None:
|
|
|
|
self.typ(real_typ)
|
|
|
|
fn = getattr(self, real_typ.value)
|
|
|
|
fn(val)
|