当前位置: 首页>>代码示例>>Python>>正文


Python utils.sha3函数代码示例

本文整理汇总了Python中raiden.utils.sha3函数的典型用法代码示例。如果您正苦于以下问题:Python sha3函数的具体用法?Python sha3怎么用?Python sha3使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了sha3函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_three

def test_three():
    hash_0 = b'a' * 32
    hash_1 = b'b' * 32
    hash_2 = b'c' * 32

    leaves = [hash_0, hash_1, hash_2]
    layers = compute_layers(leaves)
    tree = MerkleTreeState(layers)
    root = merkleroot(tree)

    hash_01 = (
        b'me\xef\x9c\xa9=5\x16\xa4\xd3\x8a\xb7\xd9\x89\xc2\xb5\x00'
        b'\xe2\xfc\x89\xcc\xdc\xf8x\xf9\xc4m\xaa\xf6\xad\r['
    )
    assert sha3(hash_0 + hash_1) == hash_01
    calculated_root = sha3(hash_2 + hash_01)

    proof0 = compute_merkleproof_for(tree, hash_0)
    proof1 = compute_merkleproof_for(tree, hash_1)
    proof2 = compute_merkleproof_for(tree, hash_2)

    assert proof0 == [hash_1, hash_2]
    assert root == calculated_root
    assert validate_proof(proof0, root, hash_0)

    assert proof1 == [hash_0, hash_2]
    assert root == calculated_root
    assert validate_proof(proof1, root, hash_1)

    # with an odd number of values, the last value wont appear by itself in the
    # proof since it isn't hashed with another value
    assert proof2 == [sha3(hash_0 + hash_1)]
    assert root == calculated_root
    assert validate_proof(proof2, root, hash_2)
开发者ID:destenson,项目名称:raiden-network--raiden,代码行数:34,代码来源:test_mtree.py

示例2: test_mediated_transfer

def test_mediated_transfer(iterations=ITERATIONS):
    identifier = 1
    amount = 1
    expiration = 1
    hashlock = sha3(ADDRESS)
    lock = Lock(amount, expiration, hashlock)

    nonce = 1
    asset = ADDRESS
    balance = 1
    recipient = ADDRESS
    locksroot = sha3(ADDRESS)
    target = ADDRESS
    initiator = ADDRESS
    msg = MediatedTransfer(
        identifier,
        nonce,
        asset,
        balance,
        recipient,
        locksroot,
        lock,
        target,
        initiator,
        fee=0,
    )
    msg.sign(PRIVKEY, ADDRESS)

    run_timeit('MediatedTranfer', msg, iterations=iterations)
开发者ID:raiden-network,项目名称:raiden,代码行数:29,代码来源:speed_decoding.py

示例3: test_hash

def test_hash():
    ping = Ping(nonce=0, current_protocol_version=constants.PROTOCOL_VERSION)
    ping.sign(PRIVKEY)
    data = ping.encode()
    msghash = sha3(data)
    decoded_ping = decode(data)
    assert sha3(decoded_ping.encode()) == msghash
开发者ID:hackaugusto,项目名称:raiden,代码行数:7,代码来源:test_binary_encoding.py

示例4: mediated_transfer

def mediated_transfer(initiator_app, target_app, asset, amount):  # pylint: disable=too-many-arguments
    """ Nice to read shortcut to make a MediatedTransfer.

    The secret will be revealed and the apps will be synchronized.
    """
    has_channel = target_app.raiden.address in initiator_app.raiden.assetmanagers[asset].channel

    # api.transfer() would do a DirectTransfer
    if has_channel:
        initiator_channel = channel(initiator_app, target_app, asset)
        secret = sha3('{}{}'.format(
            initiator_channel.nettingcontract_address,
            str(initiator_channel.our_state.nonce),
        ))
        hashlock = sha3(secret)
        transfermanager = target_app.raiden.assetmanagers[asset].transfermanager

        task = MediatedTransferTask(
            transfermanager,
            amount,
            target_app.address,
            hashlock,
            expiration=None,
            originating_transfer=None,
            secret=secret,
        )
        task.start()
        task.join()
    else:
        initiator_app.raiden.api.transfer(asset, amount, target_app.raiden.address)
开发者ID:ahuachen,项目名称:raiden,代码行数:30,代码来源:transfer.py

示例5: test_hash

def test_hash():
    ping = Ping(nonce=0)
    ping.sign(PRIVKEY)
    data = ping.encode()
    msghash = sha3(data)
    decoded_ping = decode(data)
    assert sha3(decoded_ping.encode()) == msghash
开发者ID:compumatrix,项目名称:raiden,代码行数:7,代码来源:test_messages.py

示例6: unlock

    def unlock(self, ctx, locked_encoded, merkleproof_encoded, secret):
        if self.settled is not None:
            raise RuntimeError('Contract is settled.')

        if self.closed is None:
            raise RuntimeError('Contract is open.')

        if ctx['msg.sender'] not in self.participants:
            raise ValueError('Unknow address.')

        partner = self.partner(ctx['msg.sender'])
        state = self.participants[partner]
        transfer = state.transfer

        # if partner haven't made a single transfer
        if transfer is None:
            return

        merkle_proof = tuple32(merkleproof_encoded)
        lock = Lock.from_bytes(locked_encoded)

        hashlock = lock.hashlock
        if hashlock != sha3(secret):
            raise ValueError('Invalid secret')

        is_valid_proof = check_proof(
            merkle_proof,
            transfer.locksroot,
            sha3(transfer.lock.as_bytes),
        )

        if not is_valid_proof:
            raise ValueError('Invalid merkle proof')

        transfer.append(lock)
开发者ID:ahuachen,项目名称:raiden,代码行数:35,代码来源:net_contract.py

示例7: withdraw_lock

    def withdraw_lock(self, secret):
        """ A lock was released by the sender, withdraw it's funds and update
        the state.
        """
        hashlock = sha3(secret)

        if not self.our_state.balance_proof.is_known(hashlock):
            msg = 'The secret doesnt withdraw any hashlock. hashlock:{} asset:{}'.format(
                pex(hashlock),
                pex(self.asset_address),
            )
            raise ValueError(msg)

        lock = self.our_state.balance_proof.get_lock_by_hashlock(hashlock)

        if log.isEnabledFor(logging.DEBUG):
            log.debug(
                'ASSET WITHDRAWED %s < %s asset:%s hashlock:%s lockhash:%s amount:%s',
                pex(self.our_state.address),
                pex(self.partner_state.address),
                pex(self.asset_address),
                pex(hashlock),
                pex(sha3(lock.as_bytes)),
                lock.amount,
            )

        self.our_state.release_lock(self.partner_state, secret)
开发者ID:utzig,项目名称:raiden,代码行数:27,代码来源:channel.py

示例8: test_registry

def test_registry(state, registry, events):
    asset_address1 = sha3('asset')[:20]
    asset_address2 = sha3('address')[:20]
    unregistered_address = sha3('mainz')[:20]

    contract_address1 = registry.addAsset(asset_address1)
    contract_address2 = registry.addAsset(asset_address2)

    with pytest.raises(TransactionFailed):
        registry.addAsset(asset_address1)

    channel_manager_address = registry.channelManagerByAsset(asset_address1)
    assert channel_manager_address == contract_address1

    with pytest.raises(TransactionFailed):
        registry.channelManagerByAsset(unregistered_address)

    addresses = registry.assetAddresses()

    assert len(addresses) == 2
    assert addresses[0] == asset_address1.encode('hex')
    assert addresses[1] == asset_address2.encode('hex')

    assert len(events) == 2

    assert events[0]['_event_type'] == 'AssetAdded'
    assert events[0]['assetAddress'] == asset_address1.encode('hex')
    assert events[0]['channelManagerAddress'] == contract_address1

    assert events[1]['_event_type'] == 'AssetAdded'
    assert events[1]['assetAddress'] == asset_address2.encode('hex')
    assert events[1]['channelManagerAddress'] == contract_address2
开发者ID:compumatrix,项目名称:raiden,代码行数:32,代码来源:test_registry.py

示例9: make_signed_transfer_from_counter

def make_signed_transfer_from_counter(counter):
    lock = Lock(
        amount=next(counter),
        expiration=next(counter),
        secrethash=sha3(factories.make_secret(next(counter))),
    )

    signed_transfer = factories.make_signed_transfer(
        amount=next(counter),
        initiator=factories.make_address(),
        target=factories.make_address(),
        expiration=next(counter),
        secret=factories.make_secret(next(counter)),
        payment_identifier=next(counter),
        message_identifier=next(counter),
        nonce=next(counter),
        transferred_amount=next(counter),
        locked_amount=next(counter),
        locksroot=sha3(lock.as_bytes),
        recipient=factories.make_address(),
        channel_identifier=next(counter),
        token_network_address=factories.make_address(),
        token=factories.make_address(),
        pkey=factories.HOP1_KEY,
        sender=factories.HOP1,
    )

    return signed_transfer
开发者ID:hackaugusto,项目名称:raiden,代码行数:28,代码来源:test_sqlite.py

示例10: secret_request_with_wrong_secrethash

 def secret_request_with_wrong_secrethash(self, previous_action, secret):
     assume(sha3(secret) != sha3(previous_action.transfer.secret))
     self._assume_channel_opened(previous_action)
     transfer = deepcopy(previous_action.transfer)
     transfer.secret = secret
     action = self._receive_secret_request(transfer)
     return self._unauthentic_secret_request(action)
开发者ID:hackaugusto,项目名称:raiden,代码行数:7,代码来源:test_state_changes.py

示例11: test_settlement

def test_settlement(raiden_network, settle_timeout):
    app0, app1 = raiden_network  # pylint: disable=unbalanced-tuple-unpacking

    setup_messages_cb()

    asset_manager0 = app0.raiden.managers_by_asset_address.values()[0]
    asset_manager1 = app1.raiden.managers_by_asset_address.values()[0]

    chain0 = app0.raiden.chain

    channel0 = asset_manager0.partneraddress_channel[app1.raiden.address]
    channel1 = asset_manager1.partneraddress_channel[app0.raiden.address]

    balance0 = channel0.balance
    balance1 = channel1.balance

    amount = 10
    expiration = 5
    secret = 'secret'
    hashlock = sha3(secret)

    assert app1.raiden.address in asset_manager0.partneraddress_channel
    assert asset_manager0.asset_address == asset_manager1.asset_address
    assert channel0.external_state.netting_channel.address == channel1.external_state.netting_channel.address

    transfermessage = channel0.create_lockedtransfer(amount, expiration, hashlock)
    app0.raiden.sign(transfermessage)
    channel0.register_transfer(transfermessage)
    channel1.register_transfer(transfermessage)

    assert_synched_channels(
        channel0, balance0, [transfermessage.lock],
        channel1, balance1, []
    )

    # Bob learns the secret, but Alice did not send a signed updated balance to
    # reflect this Bob wants to settle

    # get proof, that locked transfermessage was in merkle tree, with locked.root
    merkle_proof = channel1.our_state.locked.get_proof(transfermessage)
    root = channel1.our_state.locked.root
    assert check_proof(merkle_proof, root, sha3(transfermessage.lock.as_bytes))

    channel0.external_state.netting_channel.close(
        app0.raiden.address,
        transfermessage,
        None,
    )

    unlocked = [(merkle_proof, transfermessage.lock.as_bytes, secret)]

    channel0.external_state.netting_channel.unlock(
        app0.raiden.address,
        unlocked,
    )

    for _ in range(settle_timeout):
        chain0.next_block()

    channel0.external_state.netting_channel.settle()
开发者ID:compumatrix,项目名称:raiden,代码行数:60,代码来源:test_settlement.py

示例12: test_locked_transfer

def test_locked_transfer():
    apps = create_network(num_nodes=2, num_assets=1, channels_per_node=1)
    a0, a1 = apps
    c0 = a0.raiden.assetmanagers.values()[0].channels.values()[0]
    c1 = a1.raiden.assetmanagers.values()[0].channels.values()[0]

    b0, b1 = c0.balance, c1.balance

    amount = 10
    secret = 'secret'
    expiration = a0.raiden.chain.block_number + 100
    hashlock = sha3(secret)
    t = c0.create_lockedtransfer(amount=amount, expiration=expiration, hashlock=hashlock)
    c0.raiden.sign(t)
    c0.register_transfer(t)
    c1.register_transfer(t)

    assert hashlock in c1.locked
    assert hashlock in c0.partner.locked
    assert len(c0.locked) == 0
    assert len(c0.partner.locked) == 1
    assert len(c1.locked) == 1
    assert len(c1.partner.locked) == 0

    assert c0.balance == b0
    assert c0.distributable == c0.balance - amount
    assert c1.balance == c1.distributable == b1
    assert c0.locked.outstanding == 0
    assert c0.partner.locked.outstanding == amount
    assert c1.locked.outstanding == amount
    assert c1.partner.locked.outstanding == 0

    assert c0.locked.root == ''
    assert c1.partner.locked.root == ''

    assert c1.locked.root == merkleroot(
        [sha3(tx.lock.asstring) for tx in c1.locked.locked.values()])
    assert c0.partner.locked.root == c1.locked.root

    # reveal secret

    c0.claim_locked(secret)
    c1.claim_locked(secret)

    assert c0.balance == b0 - amount
    assert c0.distributable == c0.balance
    assert c1.balance == c1.distributable == b1 + amount
    assert c0.locked.outstanding == 0
    assert c0.partner.locked.outstanding == 0
    assert c1.locked.outstanding == 0
    assert c1.partner.locked.outstanding == 0
    assert len(c0.locked) == 0
    assert len(c0.partner.locked) == 0
    assert len(c1.locked) == 0
    assert len(c1.partner.locked) == 0
    assert c0.locked.root == ''
    assert c1.partner.locked.root == ''
    assert c1.locked.root == ''
    assert c0.partner.locked.root == ''
开发者ID:cubedro,项目名称:raiden,代码行数:59,代码来源:test_channel.py

示例13: generate_accounts

def generate_accounts(seeds):
    """Create private keys and addresses for all seeds.
    """
    return {
        seed: dict(
            privatekey=encode_hex(sha3(seed)),
            address=encode_hex(privatekey_to_address(sha3(seed)))
        ) for seed in seeds}
开发者ID:destenson,项目名称:raiden-network--raiden,代码行数:8,代码来源:genesis_builder.py

示例14: test_compute_layers_duplicated

def test_compute_layers_duplicated():
    hash_0 = sha3(b'x')
    hash_1 = sha3(b'y')

    with pytest.raises(ValueError):
        compute_layers([hash_0, hash_0])

    with pytest.raises(ValueError):
        compute_layers([hash_0, hash_1, hash_0])
开发者ID:destenson,项目名称:raiden-network--raiden,代码行数:9,代码来源:test_mtree.py

示例15: test_settle_with_locked_mediated_transfer_for_counterparty

def test_settle_with_locked_mediated_transfer_for_counterparty(
        deposit,
        settle_timeout,
        reveal_timeout,
        tester_chain,
        tester_channels,
        tester_token):

    """ Test settle with a locked mediated transfer for the counter party. """

    pkey0, pkey1, nettingchannel, channel0, channel1 = tester_channels[0]
    address0 = privatekey_to_address(pkey0)
    address1 = privatekey_to_address(pkey1)

    initial0 = tester_token.balanceOf(address0, sender=pkey0)
    initial1 = tester_token.balanceOf(address1, sender=pkey0)

    transferred_amount0 = 30
    increase_transferred_amount(channel0, channel1, transferred_amount0)

    expiration0 = tester_chain.block.number + reveal_timeout + 5
    new_block = Block(tester_chain.block.number)
    channel0.state_transition(new_block)
    channel1.state_transition(new_block)
    lock0 = Lock(amount=29, expiration=expiration0, hashlock=sha3(b'lock1'))
    mediated0 = make_mediated_transfer(
        channel0,
        channel1,
        address0,
        address1,
        lock0,
        pkey0,
        tester_chain.block.number,
    )

    nettingchannel.close(sender=pkey0)

    mediated0_hash = sha3(mediated0.packed().data[:-65])
    nettingchannel.updateTransfer(
        mediated0.nonce,
        mediated0.transferred_amount,
        mediated0.locksroot,
        mediated0_hash,
        mediated0.signature,
        sender=pkey1,
    )

    tester_chain.mine(number_of_blocks=settle_timeout + 1)
    nettingchannel.settle(sender=pkey1)

    # the balances only change by transferred_amount because the lock was /not/ unlocked
    balance0 = initial0 + deposit - transferred_amount0
    balance1 = initial1 + transferred_amount0

    assert tester_token.balanceOf(nettingchannel.address, sender=pkey0) == 0
    assert tester_token.balanceOf(address0, sender=pkey0) == balance0
    assert tester_token.balanceOf(address1, sender=pkey0) == balance1
开发者ID:destenson,项目名称:raiden-network--raiden,代码行数:57,代码来源:test_settle.py


注:本文中的raiden.utils.sha3函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。