예제 #1
0
def _(properties, defaults=None) -> LockedTransferSignedState:
    defaults = defaults or LOCKED_TRANSFER_SIGNED_STATE_DEFAULTS
    params = _properties_to_dict(properties, defaults)

    transfer_params = _properties_to_dict(params.pop('transfer'), defaults.transfer)
    balance_proof_params = _properties_to_dict(
        transfer_params.pop('balance_proof'),
        defaults.transfer.balance_proof,
    )

    lock = Lock(
        amount=transfer_params.pop('amount'),
        expiration=transfer_params.pop('expiration'),
        secrethash=sha3(transfer_params.pop('secret')),
    )

    pkey = params.pop('pkey')
    sender = params.pop('sender')
    params.update(transfer_params)
    params.update(balance_proof_params)
    params['token_network_address'] = params.pop('token_network_identifier')
    if params['locksroot'] == EMPTY_MERKLE_ROOT:
        params['locksroot'] = lock.lockhash

    locked_transfer = LockedTransfer(lock=lock, **params)
    locked_transfer.sign(pkey)

    assert locked_transfer.sender == sender

    return lockedtransfersigned_from_message(locked_transfer)
예제 #2
0
def handle_message_refundtransfer(raiden: RaidenService, message: RefundTransfer):
    token_network_address = message.token_network_address
    from_transfer = lockedtransfersigned_from_message(message)
    chain_state = views.state_from_raiden(raiden)

    routes = get_best_routes(
        chain_state,
        token_network_address,
        raiden.address,
        from_transfer.target,
        from_transfer.lock.amount,
        message.sender,
    )

    role = views.get_transfer_role(
        chain_state,
        from_transfer.lock.secrethash,
    )

    if role == 'initiator':
        secret = random_secret()
        state_change = ReceiveTransferRefundCancelRoute(
            message.sender,
            routes,
            from_transfer,
            secret,
        )
    else:
        state_change = ReceiveTransferRefund(
            message.sender,
            from_transfer,
            routes,
        )

    raiden.handle_state_change(state_change)
예제 #3
0
    def handle_message_refundtransfer(self, raiden: RaidenService,
                                      message: RefundTransfer):
        token_network_address = message.token_network_address
        from_transfer = lockedtransfersigned_from_message(message)
        chain_state = views.state_from_raiden(raiden)

        routes = get_best_routes(
            chain_state,
            token_network_address,
            raiden.address,
            from_transfer.target,
            from_transfer.lock.amount,
            message.sender,
        )

        role = views.get_transfer_role(
            chain_state,
            from_transfer.lock.secrethash,
        )

        if role == 'initiator':
            secret = random_secret()
            state_change = ReceiveTransferRefundCancelRoute(
                routes=routes,
                transfer=from_transfer,
                secret=secret,
            )
        else:
            state_change = ReceiveTransferRefund(
                transfer=from_transfer,
                routes=routes,
            )

        raiden.handle_state_change(state_change)
예제 #4
0
def _(properties, defaults=None) -> LockedTransferSignedState:
    defaults = defaults or LOCKED_TRANSFER_SIGNED_STATE_DEFAULTS
    params = _properties_to_dict(properties, defaults)

    transfer_params = _properties_to_dict(params.pop('transfer'),
                                          defaults.transfer)
    balance_proof_params = _properties_to_dict(
        transfer_params.pop('balance_proof'),
        defaults.transfer.balance_proof,
    )

    lock = Lock(
        amount=transfer_params.pop('amount'),
        expiration=transfer_params.pop('expiration'),
        secrethash=sha3(transfer_params.pop('secret')),
    )

    pkey = params.pop('pkey')
    signer = LocalSigner(pkey)
    sender = params.pop('sender')
    params.update(transfer_params)
    params.update(balance_proof_params)
    params['token_network_address'] = params.pop('token_network_identifier')
    if params['locksroot'] == EMPTY_MERKLE_ROOT:
        params['locksroot'] = lock.lockhash

    locked_transfer = LockedTransfer(lock=lock, **params)
    locked_transfer.sign(signer)

    assert locked_transfer.sender == sender

    return lockedtransfersigned_from_message(locked_transfer)
예제 #5
0
파일: factories.py 프로젝트: offerm/raiden
def _(properties, defaults=None) -> LockedTransferSignedState:
    transfer: LockedTransferSignedStateProperties = create_properties(
        properties, defaults)
    params = {key: value for key, value in transfer.__dict__.items()}

    lock = Lock(amount=transfer.amount,
                expiration=transfer.expiration,
                secrethash=sha3(transfer.secret))

    pkey = params.pop("pkey")
    signer = LocalSigner(pkey)
    sender = params.pop("sender")
    canonical_identifier = params.pop("canonical_identifier")
    params["chain_id"] = int(canonical_identifier.chain_identifier)
    params["channel_identifier"] = int(canonical_identifier.channel_identifier)
    params[
        "token_network_address"] = canonical_identifier.token_network_address
    if params["locksroot"] == EMPTY_MERKLE_ROOT:
        params["locksroot"] = lock.lockhash

    locked_transfer = LockedTransfer(lock=lock, **params)
    locked_transfer.sign(signer)

    assert locked_transfer.sender == sender

    return lockedtransfersigned_from_message(locked_transfer)
예제 #6
0
파일: factories.py 프로젝트: onyb/raiden
def make_signed_transfer(
        amount,
        initiator,
        target,
        expiration,
        secret,
        payment_identifier=1,
        message_identifier=None,
        nonce=1,
        transferred_amount=0,
        locked_amount=None,
        locksroot=EMPTY_MERKLE_ROOT,
        recipient=UNIT_TRANSFER_TARGET,
        channel_identifier=UNIT_CHANNEL_ID,
        token_network_address=UNIT_TOKEN_NETWORK_ADDRESS,
        token=UNIT_TOKEN_ADDRESS,
        pkey=UNIT_TRANSFER_PKEY,
        sender=UNIT_TRANSFER_SENDER,
):

    if message_identifier is None:
        message_identifier = random.randint(0, UINT64_MAX)

    secrethash = sha3(secret)
    lock = Lock(
        amount,
        expiration,
        secrethash,
    )

    if locksroot == EMPTY_MERKLE_ROOT:
        locksroot = sha3(lock.as_bytes)

    if locked_amount is None:
        locked_amount = amount
    else:
        assert locked_amount >= amount

    transfer = LockedTransfer(
        chain_id=UNIT_CHAIN_ID,
        message_identifier=message_identifier,
        payment_identifier=payment_identifier,
        nonce=nonce,
        token_network_address=token_network_address,
        token=token,
        channel_identifier=channel_identifier,
        transferred_amount=transferred_amount,
        locked_amount=locked_amount,
        recipient=recipient,
        locksroot=locksroot,
        lock=lock,
        target=target,
        initiator=initiator,
    )
    transfer.sign(pkey)
    assert transfer.sender == sender

    return lockedtransfersigned_from_message(transfer)
예제 #7
0
def make_signed_transfer(
        amount,
        initiator,
        target,
        expiration,
        secret,
        payment_identifier=1,
        message_identifier=None,
        nonce=1,
        transferred_amount=0,
        locked_amount=None,
        locksroot=EMPTY_MERKLE_ROOT,
        recipient=UNIT_TRANSFER_TARGET,
        channel_identifier=UNIT_CHANNEL_ID,
        token=UNIT_TOKEN_ADDRESS,
        pkey=UNIT_TRANSFER_PKEY,
        sender=UNIT_TRANSFER_SENDER,
):

    if message_identifier is None:
        message_identifier = random.randint(0, UINT64_MAX)

    secrethash = sha3(secret)
    lock = Lock(
        amount,
        expiration,
        secrethash,
    )

    if locksroot == EMPTY_MERKLE_ROOT:
        locksroot = sha3(lock.as_bytes)

    if locked_amount is None:
        locked_amount = amount
    else:
        assert locked_amount >= amount

    transfer = LockedTransfer(
        chain_id=UNIT_CHAIN_ID,
        message_identifier=message_identifier,
        payment_identifier=payment_identifier,
        nonce=nonce,
        token_network_address=UNIT_TOKEN_NETWORK_ADDRESS,
        token=token,
        channel_identifier=channel_identifier,
        transferred_amount=transferred_amount,
        locked_amount=locked_amount,
        recipient=recipient,
        locksroot=locksroot,
        lock=lock,
        target=target,
        initiator=initiator,
    )
    transfer.sign(pkey)
    assert transfer.sender == sender

    return lockedtransfersigned_from_message(transfer)
예제 #8
0
def make_mediated_transfer(registry_address,
                           from_channel,
                           partner_channel,
                           initiator,
                           target,
                           lock,
                           pkey,
                           secret=None):
    """ Helper to create and register a mediated transfer from `from_channel` to
    `partner_channel`."""
    payment_identifier = channel.get_next_nonce(from_channel.our_state)
    message_identifier = random.randint(0, UINT64_MAX)

    lockedtransfer = channel.send_lockedtransfer(
        registry_address,
        from_channel,
        initiator,
        target,
        lock.amount,
        message_identifier,
        payment_identifier,
        lock.expiration,
        lock.secrethash,
    )
    mediated_transfer_msg = LockedTransfer.from_event(lockedtransfer)

    address = privatekey_to_address(pkey)
    sign_key = PrivateKey(pkey)
    mediated_transfer_msg.sign(sign_key, address)

    # compute the signature
    balance_proof = balanceproof_from_envelope(mediated_transfer_msg)
    lockedtransfer.balance_proof = balance_proof

    # if this fails it's not the right key for the current `from_channel`
    assert mediated_transfer_msg.sender == from_channel.our_state.address
    receive_lockedtransfer = lockedtransfersigned_from_message(
        mediated_transfer_msg)

    channel.handle_receive_lockedtransfer(
        partner_channel,
        receive_lockedtransfer,
    )

    if secret is not None:
        random_sender = make_address()

        from_secretreveal = ReceiveSecretReveal(secret, random_sender)
        channel.handle_receive_secretreveal(from_channel, from_secretreveal)

        partner_secretreveal = ReceiveSecretReveal(secret, random_sender)
        channel.handle_receive_secretreveal(partner_channel,
                                            partner_secretreveal)

    return mediated_transfer_msg
예제 #9
0
def target_init(transfer: LockedTransfer):
    from_transfer = lockedtransfersigned_from_message(transfer)
    from_route = RouteState(
        transfer.sender,
        from_transfer.balance_proof.channel_address,
    )
    init_target_statechange = ActionInitTarget(
        from_route,
        from_transfer,
    )
    return init_target_statechange
예제 #10
0
def target_init(transfer: LockedTransfer):
    from_transfer = lockedtransfersigned_from_message(transfer)
    from_route = RouteState(
        transfer.sender,
        from_transfer.balance_proof.channel_identifier,
    )
    init_target_statechange = ActionInitTarget(
        from_route,
        from_transfer,
    )
    return init_target_statechange
예제 #11
0
def make_signed_transfer(
    amount,
    initiator,
    target,
    expiration,
    secret,
    payment_identifier=1,
    message_identifier=None,
    nonce=1,
    transferred_amount=0,
    locked_amount=None,
    recipient=UNIT_TRANSFER_TARGET,
    channel_identifier=UNIT_CHANNEL_ADDRESS,
    token=UNIT_TOKEN_ADDRESS,
    pkey=UNIT_TRANSFER_PKEY,
    sender=UNIT_TRANSFER_SENDER,
):

    if message_identifier is None:
        message_identifier = random.randint(0, UINT64_MAX)

    secrethash = sha3(secret)
    lock = Lock(
        amount,
        expiration,
        secrethash,
    )

    if locked_amount is None:
        locked_amount = amount
    else:
        assert locked_amount >= amount

    transfer = LockedTransfer(
        message_identifier,
        payment_identifier,
        nonce,
        UNIT_REGISTRY_IDENTIFIER,
        token,
        channel_identifier,
        transferred_amount,
        locked_amount,
        recipient,
        lock.lockhash,
        lock,
        target,
        initiator,
    )
    transfer.sign(pkey)
    assert transfer.sender == sender

    return lockedtransfersigned_from_message(transfer)
예제 #12
0
def target_init(raiden, transfer):
    from_transfer = lockedtransfersigned_from_message(transfer)
    from_route = RouteState2(
        transfer.sender,
        from_transfer.balance_proof.channel_address,
    )
    registry_address = raiden.default_registry.address
    init_target_statechange = ActionInitTarget2(
        registry_address,
        from_route,
        from_transfer,
    )
    return init_target_statechange
예제 #13
0
def make_mediated_transfer(
        from_channel,
        partner_channel,
        initiator,
        target,
        lock,
        pkey,
        secret=None,
):
    """ Helper to create and register a mediated transfer from `from_channel` to
    `partner_channel`."""
    payment_identifier = channel.get_next_nonce(from_channel.our_state)
    message_identifier = random.randint(0, UINT64_MAX)

    lockedtransfer = channel.send_lockedtransfer(
        from_channel,
        initiator,
        target,
        lock.amount,
        message_identifier,
        payment_identifier,
        lock.expiration,
        lock.secrethash,
    )
    mediated_transfer_msg = LockedTransfer.from_event(lockedtransfer)

    sign_key = PrivateKey(pkey)
    mediated_transfer_msg.sign(sign_key)

    # compute the signature
    balance_proof = balanceproof_from_envelope(mediated_transfer_msg)
    lockedtransfer.balance_proof = balance_proof

    # if this fails it's not the right key for the current `from_channel`
    assert mediated_transfer_msg.sender == from_channel.our_state.address
    receive_lockedtransfer = lockedtransfersigned_from_message(mediated_transfer_msg)

    channel.handle_receive_lockedtransfer(
        partner_channel,
        receive_lockedtransfer,
    )

    if secret is not None:
        secrethash = sha3(secret)

        channel.register_secret(from_channel, secret, secrethash)
        channel.register_secret(partner_channel, secret, secrethash)

    return mediated_transfer_msg
예제 #14
0
def make_mediated_transfer(
        from_channel,
        partner_channel,
        initiator,
        target,
        lock,
        pkey,
        secret=None,
):
    """ Helper to create and register a mediated transfer from `from_channel` to
    `partner_channel`."""
    payment_identifier = channel.get_next_nonce(from_channel.our_state)
    message_identifier = random.randint(0, UINT64_MAX)

    lockedtransfer = channel.send_lockedtransfer(
        from_channel,
        initiator,
        target,
        lock.amount,
        message_identifier,
        payment_identifier,
        lock.expiration,
        lock.secrethash,
    )
    mediated_transfer_msg = LockedTransfer.from_event(lockedtransfer)

    sign_key = PrivateKey(pkey)
    mediated_transfer_msg.sign(sign_key, NETWORKNAME_TO_ID[TESTS])

    # compute the signature
    balance_proof = balanceproof_from_envelope(mediated_transfer_msg)
    lockedtransfer.balance_proof = balance_proof

    # if this fails it's not the right key for the current `from_channel`
    assert mediated_transfer_msg.sender == from_channel.our_state.address
    receive_lockedtransfer = lockedtransfersigned_from_message(mediated_transfer_msg)

    channel.handle_receive_lockedtransfer(
        partner_channel,
        receive_lockedtransfer,
    )

    if secret is not None:
        secrethash = sha3(secret)

        channel.register_secret(from_channel, secret, secrethash)
        channel.register_secret(partner_channel, secret, secrethash)

    return mediated_transfer_msg
예제 #15
0
def mediator_init(raiden, transfer: LockedTransfer):
    from_transfer = lockedtransfersigned_from_message(transfer)
    routes = routing.get_best_routes(
        chain_state=views.state_from_raiden(raiden),
        token_network_id=TokenNetworkID(
            from_transfer.balance_proof.token_network_identifier),
        from_address=raiden.address,
        to_address=from_transfer.target,
        amount=PaymentAmount(from_transfer.lock.amount
                             ),  # FIXME: mypy; deprecated through #3863
        previous_address=transfer.sender,
        config=raiden.config,
        privkey=raiden.privkey,
    )
    from_route = RouteState(transfer.sender,
                            from_transfer.balance_proof.channel_identifier)
    init_mediator_statechange = ActionInitMediator(routes, from_route,
                                                   from_transfer)
    return init_mediator_statechange
예제 #16
0
def mediator_init(raiden, transfer: LockedTransfer) -> ActionInitMediator:
    from_transfer = lockedtransfersigned_from_message(transfer)
    # Feedback token not used here, will be removed with source routing
    routes, _ = routing.get_best_routes(
        chain_state=views.state_from_raiden(raiden),
        token_network_id=TokenNetworkID(
            from_transfer.balance_proof.token_network_identifier),
        one_to_n_address=raiden.default_one_to_n_address,
        from_address=raiden.address,
        to_address=from_transfer.target,
        amount=PaymentAmount(from_transfer.lock.amount
                             ),  # FIXME: mypy; deprecated through #3863
        previous_address=transfer.sender,
        config=raiden.config,
        privkey=raiden.privkey,
    )
    from_route = RouteState(transfer.sender,
                            from_transfer.balance_proof.channel_identifier)
    return ActionInitMediator(routes, from_route, from_transfer)
예제 #17
0
def mediator_init(raiden, transfer: LockedTransfer):
    from_transfer = lockedtransfersigned_from_message(transfer)
    routes = routing.get_best_routes(
        views.state_from_raiden(raiden),
        from_transfer.balance_proof.token_network_identifier,
        raiden.address,
        from_transfer.target,
        from_transfer.lock.amount,
        transfer.sender,
    )
    from_route = RouteState(
        transfer.sender,
        from_transfer.balance_proof.channel_address,
    )
    init_mediator_statechange = ActionInitMediator(
        routes,
        from_route,
        from_transfer,
    )
    return init_mediator_statechange
예제 #18
0
def mediator_init(raiden, transfer: LockedTransfer):
    from_transfer = lockedtransfersigned_from_message(transfer)
    routes = routing.get_best_routes(
        views.state_from_raiden(raiden),
        from_transfer.balance_proof.token_network_identifier,
        raiden.address,
        from_transfer.target,
        from_transfer.lock.amount,
        transfer.sender,
    )
    from_route = RouteState(
        transfer.sender,
        from_transfer.balance_proof.channel_address,
    )
    init_mediator_statechange = ActionInitMediator(
        routes,
        from_route,
        from_transfer,
    )
    return init_mediator_statechange
예제 #19
0
    def handle_message_refundtransfer(raiden: RaidenService,
                                      message: RefundTransfer) -> None:
        token_network_address = message.token_network_address
        from_transfer = lockedtransfersigned_from_message(message)
        chain_state = views.state_from_raiden(raiden)

        # FIXME: Shouldn't request routes here
        routes, _ = get_best_routes(
            chain_state=chain_state,
            token_network_id=TokenNetworkID(token_network_address),
            one_to_n_address=raiden.default_one_to_n_address,
            from_address=InitiatorAddress(raiden.address),
            to_address=from_transfer.target,
            amount=PaymentAmount(
                from_transfer.lock.amount),  # FIXME: mypy; deprecated by #3863
            previous_address=message.sender,
            config=raiden.config,
            privkey=raiden.privkey,
        )

        role = views.get_transfer_role(
            chain_state=chain_state, secrethash=from_transfer.lock.secrethash)

        state_change: StateChange
        if role == "initiator":
            old_secret = views.get_transfer_secret(
                chain_state, from_transfer.lock.secrethash)
            # We currently don't allow multi routes if the initiator does not
            # hold the secret. In such case we remove all other possible routes
            # which allow the API call to return with with an error message.
            if old_secret == EMPTY_SECRET:
                routes = list()

            secret = random_secret()
            state_change = ReceiveTransferRefundCancelRoute(
                routes=routes, transfer=from_transfer, secret=secret)
        else:
            state_change = ReceiveTransferRefund(transfer=from_transfer,
                                                 routes=routes)

        raiden.handle_and_track_state_change(state_change)
예제 #20
0
def make_signed_transfer_state(
    amount: typing.TokenAmount = EMPTY,
    initiator: typing.InitiatorAddress = EMPTY,
    target: typing.TargetAddress = EMPTY,
    expiration: typing.BlockExpiration = EMPTY,
    secret: typing.Secret = EMPTY,
    payment_identifier: typing.PaymentID = EMPTY,
    message_identifier: typing.MessageID = EMPTY,
    nonce: typing.Nonce = EMPTY,
    transferred_amount: typing.TokenAmount = EMPTY,
    locked_amount: typing.TokenAmount = EMPTY,
    locksroot: typing.Locksroot = EMPTY,
    recipient: typing.Address = EMPTY,
    channel_identifier: typing.ChannelID = EMPTY,
    token_network_address: typing.TokenNetworkID = EMPTY,
    token: typing.TargetAddress = EMPTY,
    pkey: bytes = EMPTY,
    sender: typing.Address = EMPTY,
) -> LockedTransferSignedState:

    transfer = make_signed_transfer(
        amount=amount,
        initiator=initiator,
        target=target,
        expiration=expiration,
        secret=secret,
        payment_identifier=payment_identifier,
        message_identifier=message_identifier,
        nonce=nonce,
        transferred_amount=transferred_amount,
        locked_amount=locked_amount,
        locksroot=locksroot,
        recipient=recipient,
        channel_identifier=channel_identifier,
        token_network_address=token_network_address,
        token=token,
        pkey=pkey,
        sender=sender,
    )
    return lockedtransfersigned_from_message(transfer)
예제 #21
0
def make_signed_transfer(
        amount,
        initiator,
        target,
        expiration,
        secret,
        identifier=1,
        nonce=1,
        transferred_amount=0,
        recipient=UNIT_TRANSFER_TARGET,
        channel_identifier=UNIT_CHANNEL_ADDRESS,
        token=UNIT_TOKEN_ADDRESS,
        pkey=UNIT_TRANSFER_PKEY,
        sender=UNIT_TRANSFER_SENDER
):

    secrethash = sha3(secret)
    lock = Lock(
        amount,
        expiration,
        secrethash,
    )

    transfer = LockedTransfer(
        identifier,
        nonce,
        token,
        channel_identifier,
        transferred_amount,
        recipient,
        lock.lockhash,
        lock,
        target,
        initiator,
    )
    transfer.sign(pkey, sender)

    return lockedtransfersigned_from_message(transfer)
예제 #22
0
def handle_message_refundtransfer(raiden: 'RaidenService',
                                  message: RefundTransfer):
    registry_address = message.registry_address
    from_transfer = lockedtransfersigned_from_message(message)
    node_state = views.state_from_raiden(raiden)

    routes = get_best_routes(
        node_state,
        registry_address,
        from_transfer.token,
        raiden.address,
        from_transfer.target,
        from_transfer.lock.amount,
        message.sender,
    )

    role = views.get_transfer_role(
        node_state,
        from_transfer.lock.secrethash,
    )

    if role == 'initiator':
        secret = random_secret()
        state_change = ReceiveTransferRefundCancelRoute(
            registry_address,
            message.sender,
            routes,
            from_transfer,
            secret,
        )
    else:
        state_change = ReceiveTransferRefund(
            message.message_identifier,
            message.sender,
            from_transfer,
        )

    raiden.handle_state_change(state_change)
예제 #23
0
def mediator_init(raiden, transfer):
    from_transfer = lockedtransfersigned_from_message(transfer)
    registry_address = raiden.default_registry.address
    routes = routing.get_best_routes(
        views.state_from_raiden(raiden),
        registry_address,
        from_transfer.token,
        raiden.address,
        from_transfer.target,
        from_transfer.lock.amount,
        transfer.sender,
    )
    from_route = RouteState2(
        transfer.sender,
        from_transfer.balance_proof.channel_address,
    )
    init_mediator_statechange = ActionInitMediator2(
        registry_address,
        routes,
        from_route,
        from_transfer,
    )
    return init_mediator_statechange
예제 #24
0
    def handle_message_refundtransfer(raiden: RaidenService,
                                      message: RefundTransfer):
        token_network_address = message.token_network_address
        from_transfer = lockedtransfersigned_from_message(message)
        chain_state = views.state_from_raiden(raiden)

        routes = get_best_routes(
            chain_state=chain_state,
            token_network_id=TokenNetworkID(token_network_address),
            from_address=InitiatorAddress(raiden.address),
            to_address=from_transfer.target,
            amount=from_transfer.lock.amount,
            previous_address=message.sender,
            config=raiden.config,
            privkey=raiden.privkey,
        )

        role = views.get_transfer_role(
            chain_state,
            from_transfer.lock.secrethash,
        )

        state_change: StateChange
        if role == 'initiator':
            secret = random_secret()
            state_change = ReceiveTransferRefundCancelRoute(
                routes=routes,
                transfer=from_transfer,
                secret=secret,
            )
        else:
            state_change = ReceiveTransferRefund(
                transfer=from_transfer,
                routes=routes,
            )

        raiden.handle_and_track_state_change(state_change)
예제 #25
0
def make_signed_transfer(
        amount: typing.TokenAmount = EMPTY,
        initiator: typing.InitiatorAddress = EMPTY,
        target: typing.TargetAddress = EMPTY,
        expiration: typing.BlockExpiration = EMPTY,
        secret: typing.Secret = EMPTY,
        payment_identifier: typing.PaymentID = EMPTY,
        message_identifier: typing.MessageID = EMPTY,
        nonce: typing.Nonce = EMPTY,
        transferred_amount: typing.TokenAmount = EMPTY,
        locked_amount: typing.TokenAmount = EMPTY,
        locksroot: typing.Locksroot = EMPTY,
        recipient: typing.Address = EMPTY,
        channel_identifier: typing.ChannelID = EMPTY,
        token_network_address: typing.TokenNetworkID = EMPTY,
        token: typing.TargetAddress = EMPTY,
        pkey: PrivateKey = EMPTY,
        sender: typing.Address = EMPTY,
) -> LockedTransferSignedState:

    amount = if_empty(amount, UNIT_TRANSFER_AMOUNT)
    initiator = if_empty(initiator, make_address())
    target = if_empty(target, make_address())
    expiration = if_empty(expiration, UNIT_REVEAL_TIMEOUT)
    secret = if_empty(secret, make_secret())
    payment_identifier = if_empty(payment_identifier, 1)
    message_identifier = if_empty(message_identifier, make_message_identifier())
    nonce = if_empty(nonce, 1)
    transferred_amount = if_empty(transferred_amount, 0)
    locked_amount = if_empty(locked_amount, amount)
    locksroot = if_empty(locksroot, EMPTY_MERKLE_ROOT)
    recipient = if_empty(recipient, UNIT_TRANSFER_TARGET)
    channel_identifier = if_empty(channel_identifier, UNIT_CHANNEL_ID)
    token_network_address = if_empty(token_network_address, UNIT_TOKEN_NETWORK_ADDRESS)
    token = if_empty(token, UNIT_TOKEN_ADDRESS)
    pkey = if_empty(pkey, UNIT_TRANSFER_PKEY)
    sender = if_empty(sender, UNIT_TRANSFER_SENDER)

    assert locked_amount >= amount

    secrethash = sha3(secret)
    lock = Lock(
        amount=amount,
        expiration=expiration,
        secrethash=secrethash,
    )

    if locksroot == EMPTY_MERKLE_ROOT:
        locksroot = sha3(lock.as_bytes)

    transfer = LockedTransfer(
        chain_id=UNIT_CHAIN_ID,
        message_identifier=message_identifier,
        payment_identifier=payment_identifier,
        nonce=nonce,
        token_network_address=token_network_address,
        token=token,
        channel_identifier=channel_identifier,
        transferred_amount=transferred_amount,
        locked_amount=locked_amount,
        recipient=recipient,
        locksroot=locksroot,
        lock=lock,
        target=target,
        initiator=initiator,
    )
    transfer.sign(pkey)
    assert transfer.sender == sender

    return lockedtransfersigned_from_message(transfer)
예제 #26
0
def make_signed_transfer(
    amount: typing.TokenAmount = EMPTY,
    initiator: typing.InitiatorAddress = EMPTY,
    target: typing.TargetAddress = EMPTY,
    expiration: typing.BlockExpiration = EMPTY,
    secret: typing.Secret = EMPTY,
    payment_identifier: typing.PaymentID = EMPTY,
    message_identifier: typing.MessageID = EMPTY,
    nonce: typing.Nonce = EMPTY,
    transferred_amount: typing.TokenAmount = EMPTY,
    locked_amount: typing.TokenAmount = EMPTY,
    locksroot: typing.Locksroot = EMPTY,
    recipient: typing.Address = EMPTY,
    channel_identifier: typing.ChannelID = EMPTY,
    token_network_address: typing.TokenNetworkID = EMPTY,
    token: typing.TargetAddress = EMPTY,
    pkey: bytes = EMPTY,
    sender: typing.Address = EMPTY,
) -> LockedTransferSignedState:

    amount = if_empty(amount, UNIT_TRANSFER_AMOUNT)
    initiator = if_empty(initiator, make_address())
    target = if_empty(target, make_address())
    expiration = if_empty(expiration, UNIT_REVEAL_TIMEOUT)
    secret = if_empty(secret, make_secret())
    payment_identifier = if_empty(payment_identifier, 1)
    message_identifier = if_empty(message_identifier,
                                  make_message_identifier())
    nonce = if_empty(nonce, 1)
    transferred_amount = if_empty(transferred_amount, 0)
    locked_amount = if_empty(locked_amount, amount)
    locksroot = if_empty(locksroot, EMPTY_MERKLE_ROOT)
    recipient = if_empty(recipient, UNIT_TRANSFER_TARGET)
    channel_identifier = if_empty(channel_identifier, UNIT_CHANNEL_ID)
    token_network_address = if_empty(token_network_address,
                                     UNIT_TOKEN_NETWORK_ADDRESS)
    token = if_empty(token, UNIT_TOKEN_ADDRESS)
    pkey = if_empty(pkey, UNIT_TRANSFER_PKEY)
    signer = LocalSigner(pkey)
    sender = if_empty(sender, UNIT_TRANSFER_SENDER)

    assert locked_amount >= amount

    secrethash = sha3(secret)
    lock = Lock(
        amount=amount,
        expiration=expiration,
        secrethash=secrethash,
    )

    if locksroot == EMPTY_MERKLE_ROOT:
        locksroot = sha3(lock.as_bytes)

    transfer = LockedTransfer(
        chain_id=UNIT_CHAIN_ID,
        message_identifier=message_identifier,
        payment_identifier=payment_identifier,
        nonce=nonce,
        token_network_address=token_network_address,
        token=token,
        channel_identifier=channel_identifier,
        transferred_amount=transferred_amount,
        locked_amount=locked_amount,
        recipient=recipient,
        locksroot=locksroot,
        lock=lock,
        target=target,
        initiator=initiator,
    )
    transfer.sign(signer)
    assert transfer.sender == sender

    return lockedtransfersigned_from_message(transfer)
예제 #27
0
def target_init(transfer: LockedTransfer) -> ActionInitTarget:
    from_transfer = lockedtransfersigned_from_message(transfer)
    from_route = RouteState(transfer.sender,
                            from_transfer.balance_proof.channel_identifier)
    return ActionInitTarget(from_route, from_transfer)