def bech32_address_bytes(val): if not is_empty_or_checksum_address(val): if val[0:3] == MIANNETHRP: _, result = decode(MIANNETHRP, val) elif val[0:3] == TESTNETHRP: _, result = decode(TESTNETHRP, val) else: raise ValueError(val) val = bytes(result) return val
def encode_abi(web3, abi, arguments, vmtype, data=None, setabi=None): arguments = list(arguments) if vmtype == 1: 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.insert(0, fnv1_64(bytes(abi['name'], 'utf8'))) if abi['type'] == 'constructor': if data: data1 = bytes.fromhex(str(data, encoding='utf8')) deploydata = rlp.encode([data1, rlp.encode(paramabi)]) encodata = ''.join(magicnum) + deploydata.hex() return '0x' + encodata else: return '0x' + rlp.encode(paramabi).hex() else: encodata = rlp.encode(paramabi).hex() 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': hrpgot, data1 = bech32.decode(arguments[j][:3], arguments[j]) addr = to_checksum_address(bytes(data1)) arguments[j] = addr # .split(",") elif argument_types[j] == 'address[]': for i in range(len(arguments[j])): hrpgot, data1 = bech32.decode(arguments[j][i][:3], arguments[j][i]) addr = to_checksum_address(bytes(data1)) arguments[j][i] = addr 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)
def encodeparameters(types, params, setabi=None): arrlp = [] if isinstance(params, set): params = list(params) for i in range(len(params)): param = params[i] type = types[i]['type'] name = types[i]['name'] if type == 'string': arrlp.append(bytes(param, 'utf-8')) elif type.startswith('uint') and not type.endswith(']'): temp = encodeuint(param) arrlp.append(int(''.join(temp), 16)) elif type == 'bool': arrlp.append(1 if param else 0) elif type.startswith("int") and not type.endswith("]"): temp1 = (param << 1) ^ (param >> 63) temp = encodeuint(temp1) arrlp.append(int(''.join(temp), 16)) elif type == 'float': temp1 = float_to_hex(param) temp = hexstr2bytes(temp1) arrlp.append(temp) elif type == 'double': temp1 = double_to_hex(param) temp = hexstr2bytes(temp1) arrlp.append(temp) elif type.endswith(']'): vectype = type.split('[')[0] temp = [] vecLen = type[type.index('[') + 1:].split(']')[0] if vectype == 'uint8' and vecLen == "": arrlp.append(param) else: for i in param: temp.append( encodeparameters([{ 'type': vectype, 'name': '' }], [i], setabi)[0]) arrlp.append(temp) elif type.startswith('list'): i1 = type.index('<') i2 = type.index('>') itype = type[i1 + 1:i2] temp = [] for j in param: temp.append( encodeparameters([{ 'type': itype, 'name': '' }], [j], setabi)[0]) arrlp.append(temp) elif type.startswith('map'): i1 = type.index('<') i2 = type.index(',') i3 = type.index('>') ktype = type[i1 + 1:i2] vtype = type[i2 + 1:i3] temp = [] for j in param: kvalue = encodeparameters([{ 'type': ktype, 'name': '' }], [j[0]], setabi)[0] vvalue = encodeparameters([{ 'type': vtype, 'name': '' }], [j[1]], setabi)[0] temp.append([kvalue, vvalue]) arrlp.append(temp) elif type.startswith('pair'): i1 = type.index('<') i2 = type.index(',') i3 = type.index('>') ktype = type[i1 + 1:i2] vtype = type[i2 + 1:i3] kvalue = encodeparameters([{ 'type': ktype, 'name': '' }], [param[0]], setabi)[0] vvalue = encodeparameters([{ 'type': vtype, 'name': '' }], [param[1]], setabi)[0] arrlp.append([kvalue, vvalue]) elif type.startswith('set'): temp = [] i1 = type.index('<') i2 = type.index('>') stype = type[i1 + 1:i2] for j in param: temp.append( encodeparameters([{ 'type': stype, 'name': '' }], [j], setabi)[0]) arrlp.append(temp) elif type == 'struct': temp = [] 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: temp.append( encodeparameters(structtype[0]['inputs'], param, setabi)) arrlp.append(temp) elif type == 'FixedHash<20>': temp = [] if isinstance(param, str): p1 = param[0:3] p2 = param elif isinstance(param, tuple): p1 = param[0][0:3] p2 = param[0] if p1 == 'lax' or p1 == 'lat': hrpgot, data1 = bech32.decode(p1, p2) for i in data1: temp1 = hex(i).replace('0x', '') if len(temp1) == 1: temp1 = '0' + temp1 temp.append(temp1) del temp1 arrlp.append(temp) elif type.startswith( 'FixedHash' ): # 不是很确定,把'FixedHash'开头的类型理解为 ['0x33','0x6a'.'0x5e']这样的字节数组 arrlp.append(param) else: structtype = [item for item in setabi if item['name'] == type] if not structtype: raise Exception( 'can not find struct through {} .'.format(type)) else: temp = encodeparameters(structtype[0]['inputs'], param, setabi) arrlp.append(temp) return arrlp
def encodeparameters(types, params, setabi=None): arrlp = [] if isinstance(params, set): params = list(params) for i in range(len(params)): param = params[i] type = types[i]['type'] name = types[i]['name'] if type == 'string': temp = stringtohex(bytes(param, 'utf-8')) if isinstance(param, str) and len(params) <= 1: arrlp = temp else: arrlp = handlearrlp(arrlp, temp) elif type.startswith('uint') and not type.endswith(']'): temp = encodeuint(param) if len(params) <= 1: arrlp = temp else: arrlp = handlearrlp(arrlp, temp) elif type == 'bool': if param: temp = '01' else: temp = '0' if len(params) <= 1: arrlp.append(temp) else: arrlp = handlearrlp(arrlp, temp) elif type == 'int8': temp1 = np.uint8(param) temp = encodeuint(temp1) if len(params) <= 1: arrlp = temp else: arrlp = handlearrlp(arrlp, temp) elif type == 'int16': temp1 = np.uint16(param) temp = encodeuint(temp1) if len(params) <= 1: arrlp = temp else: arrlp = handlearrlp(arrlp, temp) elif type == 'int32': temp1 = np.uint32(param) temp = encodeuint(temp1) if len(params) <= 1: arrlp = temp else: arrlp = handlearrlp(arrlp, temp) elif type == 'int64': temp1 = np.uint64(param) temp = encodeuint(temp1) if len(params) <= 1: arrlp = temp else: arrlp = handlearrlp(arrlp, temp) elif type == 'float': temp1 = float_to_hex(param) temp = hexstr2bytes(temp1) if len(params) <= 1: arrlp = temp else: arrlp = handlearrlp(arrlp, temp) elif type == 'double': temp1 = double_to_hex(param) temp = hexstr2bytes(temp1) if len(params) <= 1: arrlp = temp else: arrlp = handlearrlp(arrlp, temp) elif type.endswith(']'): vectype = type.split('[')[0] temp = [] if vectype == 'uint8': for i in param: if isinstance(i, list): temp.append(hex(i[0]).replace('0x', '')) else: if isinstance(i, str): temp.append(hex(int(i, 16)).replace('0x', '')) else: temp.append(hex(i).replace('0x', '')) if len(params) <= 1: arrlp = temp else: arrlp = handlearrlp(arrlp, temp) else: for i in param: temp.append( encodeparameters([{ 'type': vectype, 'name': '' }], [i], setabi)) if len(params) <= 1: arrlp = tuple(temp) ##转化为字节数组列表模式 else: arrlp = tuplearrlp(arrlp, temp) elif type.startswith('list'): i1 = type.index('<') i2 = type.index('>') itype = type[i1 + 1:i2] temp = [] for j in param: temp.append( encodeparameters([{ 'type': itype, 'name': '' }], [j], setabi)) if len(params) <= 1: arrlp = tuple(temp) ##转化为字节数组列表模式 else: arrlp = tuplearrlp(arrlp, temp) elif type.startswith('map'): i1 = type.index('<') i2 = type.index(',') i3 = type.index('>') ktype = type[i1 + 1:i2] vtype = type[i2 + 1:i3] temp = [] for j in param: kvalue = encodeparameters([{ 'type': ktype, 'name': '' }], [j[0]], setabi) vvalue = encodeparameters([{ 'type': vtype, 'name': '' }], [j[1]], setabi) temp1 = (kvalue, vvalue) temp.append(temp1) if len(params) <= 1: arrlp = tuple(temp) ##转化为字节数组列表模式 else: arrlp = tuplearrlp(arrlp, temp) elif type.startswith('pair'): temp = [] i1 = type.index('<') i2 = type.index(',') i3 = type.index('>') ktype = type[i1 + 1:i2] vtype = type[i2 + 1:i3] kvalue = encodeparameters([{ 'type': ktype, 'name': '' }], [param[0]], setabi) vvalue = encodeparameters([{ 'type': vtype, 'name': '' }], [param[1]], setabi) temp.append(kvalue) temp.append(vvalue) if len(params) <= 1: arrlp = tuple(temp) ##转化为字节数组列表模式 else: arrlp = tuplearrlp(arrlp, temp) elif type.startswith('set'): temp = [] i1 = type.index('<') i2 = type.index('>') stype = type[i1 + 1:i2] for j in param: temp.append( encodeparameters([{ 'type': stype, 'name': '' }], [j], setabi)) if len(params) <= 1: arrlp = tuple(temp) ##转化为字节数组列表模式 else: arrlp = tuplearrlp(arrlp, temp) elif type == 'struct': temp = [] 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: temp.append( encodeparameters(structtype[0]['inputs'], param, setabi)) if len(params) <= 1: arrlp = tuple(temp) ##转化为字节数组列表模式 else: arrlp = tuplearrlp(arrlp, temp) elif type == 'FixedHash<20>': temp = [] if isinstance(param, str): p1 = param[0:3] p2 = param elif isinstance(param, tuple): p1 = param[0][0:3] p2 = param[0] if p1 == 'lax' or p1 == 'lat': hrpgot, data1 = bech32.decode(p1, p2) for i in data1: temp1 = hex(i).replace('0x', '') if len(temp1) == 1: temp1 = '0' + temp1 temp.append(temp1) del temp1 if len(params) <= 1: arrlp = temp else: arrlp = handlearrlp(arrlp, temp) elif type.startswith( 'FixedHash' ): # 不是很确定,把'FixedHash'开头的类型理解为 ['0x33','0x6a'.'0x5e']这样的字节数组 temp = [] for i in param: if isinstance(i, list): temp.append(hex(i[0]).replace('0x', '')) else: temp.append(hex(i).replace('0x', '')) if len(params) <= 1: arrlp = temp else: arrlp = handlearrlp(arrlp, 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: temp = [] temp = encodeparameters(structtype[0]['inputs'], param, setabi) if len(params) <= 1: arrlp = temp else: arrlp = handlearrlp(arrlp, temp) return arrlp
def bech32_address_to_address_bytes(address_bytes: bytes, hrp=DEFAULTHRP) -> str: _, data = decode(hrp, address_bytes) return bytes(data).hex()
def bech32_address_bytes(val): if not is_empty_or_checksum_address(val): _, result = decode(val[0:3], val) val = bytes(result) return val
"type": "string" }], "payable": False, "stateMutability": "view", "type": "function" }] tx = contract_deploy(bytecode, from_address) print(tx) tx_receipt = platon.waitForTransactionReceipt(tx) print(tx_receipt) contractAddress = tx_receipt.contractAddress print(contractAddress) # contractAddress = bech32.bech32_decode(contractAddress) hrpgot, data = bech32.decode("lax", from_address) address = to_checksum_address(bytes(data)) print('qqq') print(address) def SendTxn(txn): signed_txn = platon.account.signTransaction(txn, private_key=send_privatekey) res = platon.sendRawTransaction(signed_txn.rawTransaction).hex() txn_receipt = platon.waitForTransactionReceipt(res) print(res) return txn_receipt # print(len([10, 14, 31, 23, 10, 20, 20, 9, 15, 9, 0, 15, 12, 20, 6, 15, 12, 8, 5, 6, 28, 22, 7, 25, 11, 30, 2, 27, 10, 23, 19, 11]))