本文整理汇总了Python中web3.Web3.toBytes方法的典型用法代码示例。如果您正苦于以下问题:Python Web3.toBytes方法的具体用法?Python Web3.toBytes怎么用?Python Web3.toBytes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类web3.Web3
的用法示例。
在下文中一共展示了Web3.toBytes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_setup_name
# 需要导入模块: from web3 import Web3 [as 别名]
# 或者: from web3.Web3 import toBytes [as 别名]
def test_setup_name(ens, name, normalized_name, namehash_hex):
address = ens.web3.eth.accounts[3]
assert not ens.name(address)
owner = ens.owner('tester.eth')
ens.setup_name(name, address)
assert ens.name(address) == normalized_name
# check that the correct namehash is set:
node = Web3.toBytes(hexstr=namehash_hex)
assert ens.resolver(normalized_name).caller.addr(node) == address
# check that the correct owner is set:
assert ens.owner(name) == owner
ens.setup_name(None, address)
ens.setup_address(name, None)
assert not ens.name(address)
assert not ens.address(name)
示例2: test_set_address
# 需要导入模块: from web3 import Web3 [as 别名]
# 或者: from web3.Web3 import toBytes [as 别名]
def test_set_address(ens, name, full_name, namehash_hex, TEST_ADDRESS):
assert ens.address(name) is None
owner = ens.owner('tester.eth')
ens.setup_address(name, TEST_ADDRESS)
assert is_same_address(ens.address(name), TEST_ADDRESS)
namehash = Web3.toBytes(hexstr=namehash_hex)
normal_name = ens.nameprep(full_name)
assert is_same_address(ens.address(name), TEST_ADDRESS)
# check that the correct namehash is set:
assert is_same_address(ens.resolver(normal_name).caller.addr(namehash), TEST_ADDRESS)
# check that the correct owner is set:
assert ens.owner(name) == owner
ens.setup_address(name, None)
assert ens.address(name) is None
示例3: pack_balance_proof
# 需要导入模块: from web3 import Web3 [as 别名]
# 或者: from web3.Web3 import toBytes [as 别名]
def pack_balance_proof(
token_network_address: HexAddress,
chain_identifier: ChainID,
channel_identifier: ChannelID,
balance_hash: BalanceHash,
nonce: Nonce,
additional_hash: AdditionalHash,
msg_type: MessageTypeId,
) -> bytes:
return (
Web3.toBytes(hexstr=token_network_address)
+ encode_single("uint256", chain_identifier)
+ encode_single("uint256", msg_type)
+ encode_single("uint256", channel_identifier)
+ balance_hash
+ encode_single("uint256", nonce)
+ additional_hash
)
示例4: pack_cooperative_settle_message
# 需要导入模块: from web3 import Web3 [as 别名]
# 或者: from web3.Web3 import toBytes [as 别名]
def pack_cooperative_settle_message(
token_network_address: HexAddress,
chain_identifier: ChainID,
channel_identifier: ChannelID,
participant1_address: HexAddress,
participant1_balance: TokenAmount,
participant2_address: HexAddress,
participant2_balance: TokenAmount,
) -> bytes:
return (
Web3.toBytes(hexstr=token_network_address)
+ encode_single("uint256", chain_identifier)
+ encode_single("uint256", MessageTypeId.COOPERATIVE_SETTLE)
+ encode_single("uint256", channel_identifier)
+ Web3.toBytes(hexstr=participant1_address)
+ encode_single("uint256", participant1_balance)
+ Web3.toBytes(hexstr=participant2_address)
+ encode_single("uint256", participant2_balance)
)
示例5: pack_withdraw_message
# 需要导入模块: from web3 import Web3 [as 别名]
# 或者: from web3.Web3 import toBytes [as 别名]
def pack_withdraw_message(
token_network_address: HexAddress,
chain_identifier: ChainID,
channel_identifier: ChannelID,
participant: HexAddress,
amount_to_withdraw: TokenAmount,
expiration_block: BlockExpiration,
) -> bytes:
return (
Web3.toBytes(hexstr=token_network_address)
+ encode_single("uint256", chain_identifier)
+ encode_single("uint256", MessageTypeId.WITHDRAW)
+ encode_single("uint256", channel_identifier)
+ Web3.toBytes(hexstr=participant)
+ encode_single("uint256", amount_to_withdraw)
+ encode_single("uint256", expiration_block)
)
示例6: pack_reward_proof
# 需要导入模块: from web3 import Web3 [as 别名]
# 或者: from web3.Web3 import toBytes [as 别名]
def pack_reward_proof(
monitoring_service_contract_address: HexAddress,
chain_id: ChainID,
token_network_address: HexAddress,
non_closing_participant: HexAddress,
non_closing_signature: Signature,
reward_amount: TokenAmount,
) -> bytes:
return (
Web3.toBytes(hexstr=monitoring_service_contract_address)
+ encode_single("uint256", chain_id)
+ encode_single("uint256", MessageTypeId.MSReward)
+ Web3.toBytes(hexstr=token_network_address)
+ Web3.toBytes(hexstr=non_closing_participant)
+ non_closing_signature
+ encode_single("uint256", reward_amount)
)
示例7: set_contract
# 需要导入模块: from web3 import Web3 [as 别名]
# 或者: from web3.Web3 import toBytes [as 别名]
def set_contract(self):
self.contract = self.network.web3.eth.contract(address=self.contract_address, abi=self.abi)
initiate_func = self.contract.functions.initiate(
self.locktime_unix,
self.secret_hash,
self.recipient_address,
self.token_address,
bool(self.token),
self.token_value_base_units,
)
tx_dict = {
'nonce': self.network.web3.eth.getTransactionCount(self.sender_address),
'from': self.sender_address,
'value': self.value_base_units,
}
tx_dict = initiate_func.buildTransaction(tx_dict)
self.gas_limit = initiate_func.estimateGas({
key: value for key, value in tx_dict.items() if key not in ('to', 'data')
})
self.tx = Transaction(
nonce=tx_dict['nonce'],
gasprice=tx_dict['gasPrice'],
startgas=self.gas_limit,
to=tx_dict['to'],
value=tx_dict['value'],
data=Web3.toBytes(hexstr=tx_dict['data']),
)
示例8: extract_secret_from_redeem_transaction
# 需要导入模块: from web3 import Web3 [as 别名]
# 或者: from web3.Web3 import toBytes [as 别名]
def extract_secret_from_redeem_transaction(self, tx_address: str) -> str:
'''
Extracting secret from redeem transaction.
Args:
tx_address (str): address of the redeem transaction
Returns:
str,: Secret string
Raises:
ValueError: When given transaction was not a redeem type transaction
Example:
>>> from clove.network import EthereumTestnet
>>> network = EthereumTestnet()
>>> network.extract_secret_from_redeem_transaction('0x9e41847c3cc780e4cb59902cf55657f0ee92642d9dee4145e090cbf206d4748f') # noqa: E501
b2eefaadbbefeb9d9467092b612464db7c6724f71b5c1d70c85853845728f0e9
'''
tx_dict = self.get_transaction(tx_address)
method_id = self.extract_method_id(tx_dict['input'])
if method_id != self.redeem:
logger.debug('Not a redeem transaction.')
raise ValueError('Not a redeem transaction.')
method_name = self.get_method_name(method_id)
input_types = get_abi_input_types(find_matching_fn_abi(self.abi, fn_identifier=method_name))
input_values = decode_abi(input_types, Web3.toBytes(hexstr=tx_dict['input'][10:]))
return input_values[0].hex()
示例9: __init__
# 需要导入模块: from web3 import Web3 [as 别名]
# 或者: from web3.Web3 import toBytes [as 别名]
def __init__(self, network, tx_dict):
self.network = network
self.tx_dict = tx_dict
self.abi = self.network.abi
self.method_id = self.network.extract_method_id(tx_dict['input'])
self.type = self.network.get_method_name(self.method_id)
self.token = None
if self.method_id != self.network.initiate:
logger.warning('Not a contract transaction.')
raise ValueError('Not a contract transaction.')
input_types = get_abi_input_types(find_matching_fn_abi(self.abi, fn_identifier=self.type))
input_names = get_abi_input_names(find_matching_fn_abi(self.abi, fn_identifier=self.type))
input_values = decode_abi(input_types, Web3.toBytes(hexstr=self.tx_dict['input'][10:]))
self.inputs = dict(zip(input_names, input_values))
self.locktime = datetime.utcfromtimestamp(self.inputs['_expiration'])
self.recipient_address = Web3.toChecksumAddress(self.inputs['_participant'])
self.refund_address = self.tx_dict['from']
self.secret_hash = self.inputs['_hash'].hex()
self.contract_address = Web3.toChecksumAddress(self.tx_dict['to'])
self.block_number = self.tx_dict['blockNumber']
self.confirmations = self.network.get_latest_block - self.block_number
self.balance = self.get_balance()
if self.is_token_contract:
self.value_base_units = self.inputs['_value']
self.token_address = Web3.toChecksumAddress(self.inputs['_token'])
self.token = self.network.get_token_by_address(self.token_address)
self.value = self.token.value_from_base_units(self.value_base_units)
self.symbol = self.token.symbol
else:
self.value_base_units = self.tx_dict['value']
self.value = self.network.value_from_base_units(self.value_base_units)
self.symbol = self.network.default_symbol
示例10: redeem
# 需要导入模块: from web3 import Web3 [as 别名]
# 或者: from web3.Web3 import toBytes [as 别名]
def redeem(self, secret: str) -> EthereumTokenTransaction:
'''
Creates transaction that can redeem a contract.
Args:
secret (str): transaction secret that should match the contract secret hash (after hashing)
Returns:
EthereumTokenTransaction: unsigned transaction object with redeem transaction
Raises:
ValueError: if contract balance is 0
'''
if self.balance == 0:
raise ValueError("Balance of this contract is 0.")
contract = self.contract
redeem_func = contract.functions.redeem(secret)
tx_dict = {
'nonce': self.network.web3.eth.getTransactionCount(self.recipient_address),
'value': 0,
'gas': ETH_REDEEM_GAS_LIMIT,
}
tx_dict = redeem_func.buildTransaction(tx_dict)
transaction = EthereumTokenTransaction(network=self.network)
transaction.tx = Transaction(
nonce=tx_dict['nonce'],
gasprice=tx_dict['gasPrice'],
startgas=tx_dict['gas'],
to=tx_dict['to'],
value=tx_dict['value'],
data=Web3.toBytes(hexstr=tx_dict['data']),
)
transaction.value = self.value
transaction.token = self.token
transaction.recipient_address = self.recipient_address
return transaction
示例11: encode_int32
# 需要导入模块: from web3 import Web3 [as 别名]
# 或者: from web3.Web3 import toBytes [as 别名]
def encode_int32(val):
return Web3.toBytes(val).rjust(32, b'\x00')
示例12: __init__
# 需要导入模块: from web3 import Web3 [as 别名]
# 或者: from web3.Web3 import toBytes [as 别名]
def __init__(self, private_key_path: Optional[str] = None):
"""
Instantiate an ethereum crypto object.
:param private_key_path: the private key path of the agent
"""
super().__init__(private_key_path=private_key_path)
bytes_representation = Web3.toBytes(hexstr=self.entity.key.hex())
self._public_key = str(keys.PrivateKey(bytes_representation).public_key)
self._address = str(self.entity.address)
示例13: bytes32
# 需要导入模块: from web3 import Web3 [as 别名]
# 或者: from web3.Web3 import toBytes [as 别名]
def bytes32(val):
if isinstance(val, int):
result = Web3.toBytes(val)
else:
raise TypeError('val %r could not be converted to bytes')
if len(result) < 32:
return result.rjust(32, b'\0')
else:
return result
示例14: test_to_bytes_primitive
# 需要导入模块: from web3 import Web3 [as 别名]
# 或者: from web3.Web3 import toBytes [as 别名]
def test_to_bytes_primitive(val, expected):
assert Web3.toBytes(val) == expected
示例15: test_to_bytes_hexstr
# 需要导入模块: from web3 import Web3 [as 别名]
# 或者: from web3.Web3 import toBytes [as 别名]
def test_to_bytes_hexstr(val, expected):
assert Web3.toBytes(hexstr=val) == expected