コード例 #1
0
def wasmevent_decode(hrp, types, data):
    if isinstance(data, HexBytes) or isinstance(data, bytes):
        bufs = detail_decode_data(rlp.decode(data))
    else:
        bufs = data
    data1 = []
    if (not any(bufs)) and ('int' in types):
        bufs = ['0']
    if (not isinstance(bufs, tuple)) and (not isinstance(types, tuple)):
        buf = bufs
        type = types
        if type == 'string':
            tem = []
            if isinstance(buf, list):
                if not isinstance(buf[0], list):
                    for i in buf:
                        tem.append(bytes.decode(HexBytes(i)))
                    data1 = ''.join(tem)
                else:
                    data1 = [
                        wasmevent_decode(hrp, {
                            'type': type,
                            'name': ''
                        }, j) for j in buf
                    ]
            elif isinstance(buf, str):
                data1 = bytes.decode(HexBytes(buf))
            elif isinstance(buf, tuple):
                data1 = [
                    wasmevent_decode(hrp, {
                        'type': type,
                        'name': ''
                    }, j) for j in buf
                ]
        elif type.startswith('uint') and not type.endswith(']'):
            digit = 0
            if len(buf) <= 1:
                data1 = int(buf[0], 16)
            else:
                for i in range(len(buf)):
                    digit += int(buf[i], 16) * (256**(len(buf) - 1 - i))
                data1 = digit
        elif type == 'bool':
            if not buf:
                data1 = False
            elif int(buf[0], 16):
                data1 = True
            else:
                data1 = False
        elif type in ['int8', 'int16', 'int32', 'int64']:
            if isinstance(buf, list):
                buf = ''.join(buf)
            temp = int(buf, 16)
            data1 = (temp >> 1) ^ (temp & 1) * (-1)
        elif type == 'float':
            data1 = struct.unpack('>f', HexBytes(buf))
        elif type == 'double':
            data1 = struct.unpack('>d', HexBytes(buf))
        elif type.endswith(']'):
            lasti = type.rindex('[')
            vectype = type[0:lasti]
            if vectype == 'uint8':
                data1 = buf
            else:
                data1 = [
                    wasmevent_decode(hrp, {
                        'type': vectype,
                        'name': ''
                    }, i) for i in buf
                ]
        elif type.startswith('FixedHash'):
            data1 = '0x' + tostring_hex(buf)
            if type.endswith('<20>'):
                temp = []
                try:
                    temp = tobech32address(hrp, data1)
                except:
                    raise (
                        'wasmdecode error ! can not match FixedHash<20> type !'
                    )
                finally:
                    data1 = temp
    else:
        if len(bufs) != len(types):
            if len(bufs[0]) == len(types):
                data1 = []
                for i in range(len(bufs[0])):
                    buf = bufs[0][i]
                    type = types[i]
                    data1.append(wasmevent_decode(hrp, type, buf))
        else:
            data1 = []
            for i in range(len(bufs)):
                buf = bufs[i]
                type = types[i]
                data1.append(wasmevent_decode(hrp, type, buf))
    return data1
コード例 #2
0
def wasmdecode_abi(hrp, types, data, setabi=None):
    if isinstance(data, HexBytes) or isinstance(data, bytes):
        buf = detail_decode_data(rlp.decode(data))
    else:
        buf = data
    type = types['type']
    name = types['name']
    if (not any(buf)) and ('int' in type):
        buf = ['0']
    if type == 'string':
        tem = []
        if isinstance(buf, list):
            if not isinstance(buf[0], list):
                for i in buf:
                    tem.append(bytes.decode(HexBytes(i)))
                data1 = ''.join(tem)
            else:
                data1 = [
                    wasmdecode_abi(hrp, {
                        'type': type,
                        'name': ''
                    }, j, setabi) for j in buf
                ]
        elif isinstance(buf, str):
            data1 = bytes.decode(HexBytes(buf))
        elif isinstance(buf, tuple):
            data1 = [
                wasmdecode_abi(hrp, {
                    'type': type,
                    'name': ''
                }, j, setabi) for j in buf
            ]

    elif type.startswith('uint') and not type.endswith(']'):
        digit = 0
        if len(buf) <= 1:
            data1 = int(buf[0], 16)
        else:
            for i in range(len(buf)):
                digit += int(buf[i], 16) * (256**(len(buf) - 1 - i))
            data1 = digit
    elif type == 'bool':
        if not buf:
            data1 = False
        elif int(buf[0], 16):
            data1 = True
        else:
            data1 = False
    elif type in ['int8', 'int16', 'int32', 'int64']:
        if isinstance(buf, list):
            buf = ''.join(buf)
        temp = int(buf, 16)
        data1 = (temp >> 1) ^ (temp & 1) * (-1)
    elif type == 'float':
        data1 = struct.unpack('>f', HexBytes(buf))
    elif type == 'double':
        data1 = struct.unpack('>d', HexBytes(buf))
    elif type.endswith(']'):
        lasti = type.rindex('[')
        vectype = type[0:lasti]
        if vectype == 'uint8':
            data1 = buf
        else:
            data1 = [
                wasmdecode_abi(hrp, {
                    'type': vectype,
                    'name': ''
                }, i, setabi) for i in buf
            ]

    elif type.startswith('list'):
        i1 = type.index('<')
        i2 = type.index('>')
        itype = type[i1 + 1:i2]
        if isinstance(buf, tuple) and len(buf) <= 1:
            buf = buf[0]
        data1 = [
            wasmdecode_abi(hrp, {
                'type': itype,
                'name': ''
            }, j, setabi) for j in buf
        ]
    elif type.startswith('map'):
        i1 = type.index('<')
        i2 = type.index(',')
        i3 = type.index('>')
        ktype = type[i1 + 1:i2]
        vtype = type[i2 + 1:i3]
        data1 = []
        for j in range(len(buf)):
            if len(buf[j]) <= 1 and len(buf[j][0]) >= 2:
                kvalue = wasmdecode_abi(hrp, {
                    'type': ktype,
                    'name': ''
                }, buf[j][0][0], setabi)
                vvalue = wasmdecode_abi(hrp, {
                    'type': vtype,
                    'name': ''
                }, buf[j][0][1], setabi)
            else:
                kvalue = wasmdecode_abi(hrp, {
                    'type': ktype,
                    'name': ''
                }, buf[j][0], setabi)
                vvalue = wasmdecode_abi(hrp, {
                    'type': vtype,
                    'name': ''
                }, buf[j][1], setabi)
            data1.append([kvalue, vvalue])
    elif type.startswith('pair'):
        i1 = type.index('<')
        i2 = type.index(',')
        i3 = type.index('>')
        ktype = type[i1 + 1:i2]
        vtype = type[i2 + 1:i3]
        data1 = [
            wasmdecode_abi(hrp, {
                'type': ktype,
                'name': ''
            }, buf[0], setabi),
            wasmdecode_abi(hrp, {
                'type': vtype,
                'name': ''
            }, buf[1], setabi)
        ]
    elif type.startswith('set'):
        i1 = type.index('<')
        i2 = type.index('>')
        stype = type[i1 + 1:i2]
        if isinstance(buf, tuple) and len(buf) <= 1:
            buf = buf[0]
        data1 = [
            wasmdecode_abi(hrp, {
                'type': stype,
                'name': ''
            }, j, setabi) for j in buf
        ]
        data1 = set(data1)
    elif type == 'struct':
        structtype = [
            item for item in setabi
            if item['type'] == 'struct' and item['name'] == name
        ]
        if not structtype:
            raise Exception('can not find struct in {} .'.format(name))
        else:
            data1 = [0 for x in range(len(structtype[0]['inputs']))]
            for i in range(len(structtype[0]['inputs'])):
                data1[i] = wasmdecode_abi(hrp, structtype[0]['inputs'][i],
                                          buf[i], setabi)

    elif type.startswith('FixedHash'):
        data1 = '0x' + tostring_hex(buf)
        if type.endswith('<20>'):
            temp = []
            try:
                temp = tobech32address(hrp, data1)
            except:
                raise ('wasmdecode error ! can not match FixedHash<20> type !')
            finally:
                data1 = temp

    else:
        structtype = [item for item in setabi if item['name'] == type]
        if not structtype:
            raise Exception('can not find struct through {} .'.format(type))
        else:
            data1 = [0 for x in range(len(structtype[0]['inputs']))]
            for i in range(len(structtype[0]['inputs'])):
                data1[i] = wasmdecode_abi(hrp, structtype[0]['inputs'][i],
                                          buf[i], setabi)

    return data1
コード例 #3
0
def encode_abi(web3, abi, arguments, vmtype, data=None, setabi=None):
    if vmtype == 1:
        arrinputs = []
        inputlength = len(abi['inputs'])
        if inputlength == len(arguments):
            if arguments:
                arrinputs = abi['inputs']
                paramABI = encodeparameters(arrinputs, arguments, setabi)
            else:
                paramABI = []
        else:
            raise Exception(
                'The number of arguments is not matching the methods required number.'
                'You need to pass {} arguments.'.format(inputlength))
        magicnum = ['00', '61', '73', '6d']
        paramabi = (stringfnv(abi['name']), paramABI)
        if abi['type'] == 'constructor':
            if data:
                data1 = hexstr2bytes(to_hex(data))
                deploydata = rlp_encode((data1, rlp_encode(paramabi)))
                encodata = tostring_hex(magicnum) + tostring_hex(deploydata)
                return '0x' + encodata
            else:
                encodata = tostring_hex(rlp_encode(paramabi))
                return '0x' + encodata
        else:
            deploydata = rlp_encode(paramabi)
            encodata = tostring_hex(deploydata)
            return '0x' + encodata

    else:
        argument_types = get_abi_input_types(abi)
        for j in range(len(argument_types)):
            if argument_types[j]:
                if argument_types[j] == 'address':
                    if arguments[j][:3] == 'lax':
                        hrpgot, data1 = bech32.decode("lax", arguments[j])
                        addr = to_checksum_address(bytes(data1))
                        arguments[j] = addr  # .split(",")
                    elif arguments[j][:3] == 'lat':
                        hrpgot, data1 = bech32.decode("lat", arguments[j])
                        addr = to_checksum_address(bytes(data1))
                        arguments[j] = addr  # .split(",")
                    else:
                        raise Exception("wrong address")
                elif argument_types[j] == 'address[]':
                    for i in range(len(arguments[j])):
                        if arguments[j][i][:3] == 'lax':
                            hrpgot, data1 = bech32.decode(
                                "lax", arguments[j][i])
                            addr = to_checksum_address(bytes(data1))
                            arguments[j][i] = addr
                        elif arguments[j][i][:3] == 'lat':
                            hrpgot, data1 = bech32.decode(
                                "lat", arguments[j][i])
                            addr = to_checksum_address(bytes(data1))
                            arguments[j][i] = addr
                        else:
                            raise Exception("wrong address[]")

        if not check_if_arguments_can_be_encoded(abi, arguments, {}):
            raise TypeError(
                "One or more arguments could not be encoded to the necessary "
                "ABI type.  Expected types are: {0}".format(
                    ', '.join(argument_types), ))

        try:
            normalizers = [
                abi_ens_resolver(web3),
                abi_address_to_hex,
                abi_bytes_to_bytes,
                abi_string_to_text,
            ]
            normalized_arguments = map_abi_data(
                normalizers,
                argument_types,
                arguments,
            )
            encoded_arguments = eth_abi_encode_abi(
                argument_types,
                normalized_arguments,
            )
        except EncodingError as e:
            raise TypeError(
                "One or more arguments could not be encoded to the necessary "
                "ABI type: {0}".format(str(e)))

        if data:
            return to_hex(HexBytes(data) + encoded_arguments)
        else:
            return encode_hex(encoded_arguments)
コード例 #4
0
def wasmevent_decode(
    types,
    data,
):
    if isinstance(data, HexBytes) or isinstance(data, bytes):
        bufs = rlp_decode(hexstr2bytes(to_hex(data)))
    else:
        bufs = data
    data1 = []
    # for i in range(len(bufs)):
    #     buf = bufs[i]
    #     type = types[i]
    if (not isinstance(bufs, tuple)) and (not isinstance(types, tuple)):
        buf = bufs
        type = types
        if type == 'string':
            tem = []
            if isinstance(buf, list):
                for i in buf:
                    tem.append(bytes.decode(HexBytes(i)))
                data1 = ''.join(tem)
            elif isinstance(buf, str):
                data1 = bytes.decode(HexBytes(buf))
            elif isinstance(buf, tuple):
                data1 = [0 for x in range(len(buf))]
                for j in range(len(buf)):
                    data1[j] = wasmevent_decode(types, buf[j])
        elif type.startswith('uint') and not type.endswith(']'):
            digit = 0
            if len(buf) <= 1:
                data1 = int(buf[0], 16)
            else:
                for i in range(len(buf)):
                    digit += int(buf[i], 16) * (256**(len(buf) - 1 - i))
                data1 = digit
        elif type == 'bool':
            if not buf:
                data1 = False
            elif int(buf[0], 16):
                data1 = True
            else:
                data1 = False
        elif type == 'int8':
            temp = np.uint8(decodeuint(buf))
            data1 = np.int8(temp)
        elif type == 'int16':
            temp = np.uint16(decodeuint(buf))
            data1 = np.int16(temp)
        elif type == 'int32':
            temp = np.uint32(decodeuint(buf))
            data1 = np.int32(temp)
        elif type == 'int64':
            temp = np.uint64(decodeuint(buf))
            data1 = np.int64(temp)
        elif type == 'float':
            data1 = struct.unpack('>f', HexBytes(buf))
        elif type == 'double':
            data1 = struct.unpack('>d', HexBytes(buf))
        elif type.endswith(']'):
            lasti = type.rindex('[')
            vectype = type[0:lasti]
            data1 = [0 for x in range(len(buf))]
            if vectype == 'uint8':
                for i in range(len(buf)):
                    data1[i] = hex(int(buf[i], 16))
            else:
                for i in range(len(buf)):
                    data1[i] = wasmevent_decode(types, buf[i])
        elif type.startswith('FixedHash'):
            data1 = '0x' + tostring_hex(buf)
            if type.endswith('<20>'):
                temp = []
                try:
                    temp = tobech32address('lax', data1)
                except:
                    try:
                        temp = tobech32address('lat', data1)
                    except:
                        raise (
                            'wasmdecode error ! can not match FixedHash<20> type !'
                        )
                finally:
                    data1 = temp
    else:
        if len(bufs) != len(types):
            if len(bufs[0]) == len(types):
                data1 = [0 for x in range(len(types))]
                for i in range(len(bufs[0])):
                    buf = bufs[0][i]
                    type = types[i]
                    data1[i] = wasmevent_decode(type, buf)
        else:
            data1 = [0 for x in range(len(bufs))]
            for i in range(len(bufs)):
                buf = bufs[i]
                type = types[i]
                data1[i] = wasmevent_decode(type, buf)
    return data1
コード例 #5
0
def wasmdecode_abi(types, data, setabi=None):
    if isinstance(data, HexBytes) or isinstance(data, bytes):
        buf = rlp_decode(hexstr2bytes(to_hex(data)))
    else:
        buf = data
    type = types['type']
    name = types['name']
    # data1 = buf
    # if isinstance(data,HexBytes):
    #    decode_data = hexstr2bytes(to_hex(data))
    if type == 'string':
        tem = []
        if isinstance(buf, list):
            if not isinstance(buf[0], list):
                for i in buf:
                    tem.append(bytes.decode(HexBytes(i)))
                data1 = ''.join(tem)
            else:
                data1 = [0 for x in range(len(buf))]
                for j in range(len(buf)):
                    data1[j] = wasmdecode_abi({
                        'type': type,
                        'name': ''
                    }, buf[j], setabi)
        elif isinstance(buf, str):
            data1 = bytes.decode(HexBytes(buf))
        elif isinstance(buf, tuple):
            data1 = [0 for x in range(len(buf))]
            for j in range(len(buf)):
                data1[j] = wasmdecode_abi({
                    'type': type,
                    'name': ''
                }, buf[j], setabi)

    elif type.startswith('uint') and not type.endswith(']'):
        digit = 0
        if len(buf) <= 1:
            data1 = int(buf[0], 16)
        else:
            for i in range(len(buf)):
                digit += int(buf[i], 16) * (256**(len(buf) - 1 - i))
            data1 = digit
    elif type == 'bool':
        if not buf:
            data1 = False
        elif int(buf[0], 16):
            data1 = True
        else:
            data1 = False
    elif type == 'int8':
        temp = np.uint8(decodeuint(buf))
        data1 = np.int8(temp)
    elif type == 'int16':
        temp = np.uint16(decodeuint(buf))
        data1 = np.int16(temp)
    elif type == 'int32':
        temp = np.uint32(decodeuint(buf))
        data1 = np.int32(temp)
    elif type == 'int64':
        temp = np.uint64(decodeuint(buf))
        data1 = np.int64(temp)
    elif type == 'float':
        data1 = struct.unpack('>f', HexBytes(buf))
    elif type == 'double':
        data1 = struct.unpack('>d', HexBytes(buf))
    elif type.endswith(']'):
        lasti = type.rindex('[')
        vectype = type[0:lasti]
        if isinstance(buf, tuple) and len(buf) <= 1:
            buf = buf[0]
        data1 = [0 for x in range(len(buf))]
        if vectype == 'uint8':
            for i in range(len(buf)):
                data1[i] = hex(int(buf[i], 16))
        else:
            for i in range(len(buf)):
                data1[i] = wasmdecode_abi({
                    'type': vectype,
                    'name': ''
                }, buf[i], setabi)

    elif type.startswith('list'):
        i1 = type.index('<')
        i2 = type.index('>')
        itype = type[i1 + 1:i2]
        if isinstance(buf, tuple) and len(buf) <= 1:
            buf = buf[0]
        data1 = [0 for x in range(len(buf))]
        for j in range(len(buf)):
            data1[j] = (wasmdecode_abi({
                'type': itype,
                'name': ''
            }, buf[j], setabi))
    elif type.startswith('map'):
        i1 = type.index('<')
        i2 = type.index(',')
        i3 = type.index('>')
        ktype = type[i1 + 1:i2]
        vtype = type[i2 + 1:i3]
        data1 = [0 for x in range(len(buf))]
        for j in range(len(buf)):
            if len(buf[j]) <= 1 and len(buf[j][0]) >= 2:
                kvalue = wasmdecode_abi({
                    'type': ktype,
                    'name': ''
                }, buf[j][0][0], setabi)
                vvalue = wasmdecode_abi({
                    'type': vtype,
                    'name': ''
                }, buf[j][0][1], setabi)
            else:
                kvalue = wasmdecode_abi({
                    'type': ktype,
                    'name': ''
                }, buf[j][0], setabi)
                vvalue = wasmdecode_abi({
                    'type': vtype,
                    'name': ''
                }, buf[j][1], setabi)
            data1[j] = [kvalue, vvalue]
    elif type.startswith('pair'):
        i1 = type.index('<')
        i2 = type.index(',')
        i3 = type.index('>')
        ktype = type[i1 + 1:i2]
        vtype = type[i2 + 1:i3]
        if isinstance(buf, tuple) and len(buf) <= 1:
            buf = buf[0]
        data1 = [0 for x in range(len(buf))]
        data1[0] = wasmdecode_abi({'type': ktype, 'name': ''}, buf[0], setabi)
        data1[1] = wasmdecode_abi({'type': vtype, 'name': ''}, buf[1], setabi)
    elif type.startswith('set'):
        i1 = type.index('<')
        i2 = type.index('>')
        stype = type[i1 + 1:i2]
        if isinstance(buf, tuple) and len(buf) <= 1:
            buf = buf[0]
        data1 = [0 for x in range(len(buf))]
        for j in range(len(buf)):
            data1[j] = wasmdecode_abi({
                'type': stype,
                'name': ''
            }, buf[j], setabi)
        data1 = set(data1)
    elif type == 'struct':
        structtype = [
            item for item in setabi
            if item['type'] == 'struct' and item['name'] == name
        ]
        if not structtype:
            raise Exception('can not find struct in {} .'.format(name))
        else:
            data1 = [0 for x in range(len(structtype[0]['inputs']))]
            for i in range(len(structtype[0]['inputs'])):
                data1[i] = wasmdecode_abi(structtype[0]['inputs'][i], buf[i],
                                          setabi)

    elif type.startswith('FixedHash'):
        data1 = '0x' + tostring_hex(buf)
        if type.endswith('<20>'):
            temp = []
            try:
                temp = tobech32address('lax', data1)
            except:
                try:
                    temp = tobech32address('lat', data1)
                except:
                    raise (
                        'wasmdecode error ! can not match FixedHash<20> type !'
                    )
            finally:
                data1 = temp

    else:
        structtype = [item for item in setabi if item['name'] == type]
        if not structtype:
            raise Exception('can not find struct through {} .'.format(type))
        else:
            data1 = [0 for x in range(len(structtype[0]['inputs']))]
            for i in range(len(structtype[0]['inputs'])):
                data1[i] = wasmdecode_abi(structtype[0]['inputs'][i], buf[i],
                                          setabi)

    return data1