當前位置: 首頁>>代碼示例>>Python>>正文


Python Web3.toBytes方法代碼示例

本文整理匯總了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) 
開發者ID:ethereum,項目名稱:web3.py,代碼行數:21,代碼來源:test_setup_name.py

示例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 
開發者ID:ethereum,項目名稱:web3.py,代碼行數:21,代碼來源:test_setup_address.py

示例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
    ) 
開發者ID:raiden-network,項目名稱:raiden-contracts,代碼行數:20,代碼來源:proofs.py

示例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)
    ) 
開發者ID:raiden-network,項目名稱:raiden-contracts,代碼行數:21,代碼來源:proofs.py

示例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)
    ) 
開發者ID:raiden-network,項目名稱:raiden-contracts,代碼行數:19,代碼來源:proofs.py

示例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)
    ) 
開發者ID:raiden-network,項目名稱:raiden-contracts,代碼行數:19,代碼來源:proofs.py

示例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']),
        ) 
開發者ID:Lamden,項目名稱:clove,代碼行數:34,代碼來源:transaction.py

示例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() 
開發者ID:Lamden,項目名稱:clove,代碼行數:30,代碼來源:base.py

示例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 
開發者ID:Lamden,項目名稱:clove,代碼行數:39,代碼來源:contract.py

示例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 
開發者ID:Lamden,項目名稱:clove,代碼行數:40,代碼來源:contract.py

示例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') 
開發者ID:ethereum,項目名稱:casper,代碼行數:4,代碼來源:utils.py

示例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) 
開發者ID:fetchai,項目名稱:agents-aea,代碼行數:12,代碼來源:ethereum.py

示例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 
開發者ID:ethereum,項目名稱:web3.py,代碼行數:11,代碼來源:conftest.py

示例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 
開發者ID:ethereum,項目名稱:web3.py,代碼行數:4,代碼來源:test_conversions.py

示例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 
開發者ID:ethereum,項目名稱:web3.py,代碼行數:4,代碼來源:test_conversions.py


注:本文中的web3.Web3.toBytes方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。