Example #1
0
def create_senddirecttransfer(channel_state, amount, identifier):
    our_balance_proof = channel_state.our_state.balance_proof

    if our_balance_proof:
        transferred_amount = amount + our_balance_proof.transferred_amount
        locksroot = our_balance_proof.locksroot
    else:
        transferred_amount = amount
        locksroot = EMPTY_MERKLE_ROOT

    nonce = get_next_nonce(channel_state.our_state)
    token = channel_state.token_address
    recipient = channel_state.partner_state.address

    balance_proof = BalanceProofUnsignedState(
        nonce,
        transferred_amount,
        locksroot,
        channel_state.identifier,
    )

    direct_transfer = SendDirectTransfer(
        identifier,
        balance_proof,
        token,
        recipient,
    )

    return direct_transfer
Example #2
0
def _(properties, defaults=None) -> LockedTransferUnsignedState:
    defaults = defaults or LOCKED_TRANSFER_DEFAULTS
    parameters = _properties_to_dict(properties, defaults)

    lock = HashTimeLockState(
        amount=parameters.pop('amount'),
        expiration=parameters.pop('expiration'),
        secrethash=sha3(parameters.pop('secret')),
    )

    balance_proof_parameters = _properties_to_dict(
        parameters.pop('balance_proof'),
        defaults.balance_proof,
    )
    balance_proof_parameters['canonical_identifier'] = CanonicalIdentifier(
        chain_identifier=balance_proof_parameters.pop('chain_id'),
        token_network_address=balance_proof_parameters.pop(
            'token_network_identifier'),
        channel_identifier=balance_proof_parameters.pop('channel_identifier'),
    )
    if balance_proof_parameters['locksroot'] == EMPTY_MERKLE_ROOT:
        balance_proof_parameters['locksroot'] = lock.lockhash
    balance_proof = BalanceProofUnsignedState(**balance_proof_parameters)

    return LockedTransferUnsignedState(balance_proof=balance_proof,
                                       lock=lock,
                                       **parameters)
Example #3
0
def make_transfer(
    amount: typing.TokenAmount = EMPTY,
    initiator: typing.InitiatorAddress = EMPTY,
    target: typing.TargetAddress = EMPTY,
    expiration: typing.BlockExpiration = EMPTY,
    secret: typing.Secret = EMPTY,
    identifier: typing.PaymentID = EMPTY,
    nonce: typing.Nonce = EMPTY,
    transferred_amount: typing.TokenAmount = EMPTY,
    locked_amount: typing.TokenAmount = EMPTY,
    token_network_identifier: typing.TokenNetworkID = EMPTY,
    channel_identifier: typing.ChannelID = EMPTY,
    locksroot: typing.Locksroot = EMPTY,
    token: typing.TargetAddress = EMPTY,
) -> LockedTransferUnsignedState:
    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())
    identifier = if_empty(identifier, 1)
    nonce = if_empty(nonce, 1)
    transferred_amount = if_empty(transferred_amount, 0)
    token_network_identifier = if_empty(token_network_identifier,
                                        UNIT_TOKEN_NETWORK_ADDRESS)
    channel_identifier = if_empty(channel_identifier, UNIT_CHANNEL_ID)
    token = if_empty(token, UNIT_TOKEN_ADDRESS)

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

    if locksroot is EMPTY:
        locksroot = lock.lockhash
        locked_amount = amount
    else:
        assert locked_amount

    unsigned_balance_proof = BalanceProofUnsignedState(
        nonce=nonce,
        transferred_amount=transferred_amount,
        locked_amount=locked_amount,
        locksroot=locksroot,
        token_network_identifier=token_network_identifier,
        channel_identifier=channel_identifier,
        chain_id=UNIT_CHAIN_ID,
    )

    return LockedTransferUnsignedState(
        payment_identifier=identifier,
        token=token,
        balance_proof=unsigned_balance_proof,
        lock=lock,
        initiator=initiator,
        target=target,
    )
Example #4
0
def create_sendmediatedtransfer(channel_state, initiator, target, amount,
                                identifier, expiration, secrethash):

    our_state = channel_state.our_state
    partner_state = channel_state.partner_state
    our_balance_proof = our_state.balance_proof

    msg = 'caller must make sure there is enough balance'
    assert amount <= get_distributable(our_state, partner_state), msg

    msg = 'caller must make sure the channel is open'
    assert get_status(channel_state) == CHANNEL_STATE_OPENED, msg

    lock = HashTimeLockState(
        amount,
        expiration,
        secrethash,
    )

    merkletree = compute_merkletree_with(
        channel_state.our_state.merkletree,
        lock.lockhash,
    )
    # The caller must ensure the same lock is not being used twice
    assert merkletree, 'lock is already registered'

    locksroot = merkleroot(merkletree)

    if our_balance_proof:
        transferred_amount = our_balance_proof.transferred_amount
    else:
        transferred_amount = 0

    token = channel_state.token_address
    nonce = get_next_nonce(channel_state.our_state)
    recipient = channel_state.partner_state.address

    balance_proof = BalanceProofUnsignedState(
        nonce,
        transferred_amount,
        locksroot,
        channel_state.identifier,
    )

    locked_transfer = LockedTransferUnsignedState(
        identifier,
        token,
        balance_proof,
        lock,
        initiator,
        target,
    )

    mediatedtransfer = SendMediatedTransfer(
        locked_transfer,
        recipient,
    )

    return mediatedtransfer, merkletree
Example #5
0
def test_update_pfs():
    balance_proof = BalanceProofUnsignedState.from_dict(
        make_balance_proof(signer=signer, amount=1).to_dict(), )
    message = UpdatePFS.from_balance_proof(
        balance_proof=balance_proof,
        reveal_timeout=1,
    )
    assert message.signature == b''
    message.sign(signer)
    assert recover(message._data_to_sign(), message.signature) == ADDRESS
Example #6
0
def make_balance_proof_from_counter(counter) -> BalanceProofUnsignedState:
    return BalanceProofUnsignedState(
        nonce=next(counter),
        transferred_amount=next(counter),
        locked_amount=next(counter),
        locksroot=sha3(next(counter).to_bytes(1, 'big')),
        token_network_identifier=factories.make_address(),
        channel_identifier=next(counter),
        chain_id=next(counter),
    )
Example #7
0
def create_unlock(
    channel_state: NettingChannelState,
    message_identifier: typing.MessageID,
    payment_identifier: typing.PaymentID,
    secret: typing.Secret,
    lock: HashTimeLockState,
) -> SendUnlockAndMerkleTree:
    our_state = channel_state.our_state

    msg = 'caller must make sure the lock is known'
    assert is_lock_pending(our_state, lock.secrethash), msg

    msg = 'caller must make sure the channel is open'
    assert get_status(channel_state) == CHANNEL_STATE_OPENED, msg

    our_balance_proof = our_state.balance_proof
    if our_balance_proof:
        transferred_amount = lock.amount + our_balance_proof.transferred_amount
    else:
        transferred_amount = lock.amount

    merkletree = compute_merkletree_without(
        our_state.merkletree,
        lock.lockhash,
    )
    locksroot = merkleroot(merkletree)

    token = channel_state.token_address
    nonce = get_next_nonce(our_state)
    recipient = channel_state.partner_state.address
    locked_amount = get_amount_locked(
        our_state) - lock.amount  # the lock is still registered

    balance_proof = BalanceProofUnsignedState(
        nonce,
        transferred_amount,
        locked_amount,
        locksroot,
        channel_state.token_network_identifier,
        channel_state.identifier,
    )

    queue_name = channel_state.identifier
    unlock_lock = SendBalanceProof(
        recipient,
        queue_name,
        message_identifier,
        payment_identifier,
        token,
        secret,
        balance_proof,
    )

    return unlock_lock, merkletree
Example #8
0
def make_balance_proof_from_counter(counter) -> BalanceProofUnsignedState:
    return BalanceProofUnsignedState(
        nonce=next(counter),
        transferred_amount=next(counter),
        locked_amount=next(counter),
        locksroot=Locksroot(keccak(next(counter).to_bytes(1, "big"))),
        canonical_identifier=factories.make_canonical_identifier(
            chain_identifier=next(counter),
            token_network_address=factories.make_address(),
            channel_identifier=next(counter),
        ),
    )
Example #9
0
def create_senddirecttransfer(
        registry_address,
        channel_state,
        amount,
        message_identifier,
        payment_identifier,
):

    our_state = channel_state.our_state
    partner_state = channel_state.partner_state

    msg = 'caller must make sure there is enough balance'
    assert amount <= get_distributable(our_state, partner_state), msg

    msg = 'caller must make sure the channel is open'
    assert get_status(channel_state) == CHANNEL_STATE_OPENED, msg

    our_balance_proof = channel_state.our_state.balance_proof

    if our_balance_proof:
        transferred_amount = amount + our_balance_proof.transferred_amount
        locksroot = our_balance_proof.locksroot
    else:
        transferred_amount = amount
        locksroot = EMPTY_MERKLE_ROOT

    nonce = get_next_nonce(our_state)
    token = channel_state.token_address
    recipient = partner_state.address
    locked_amount = get_amount_locked(our_state)

    balance_proof = BalanceProofUnsignedState(
        nonce,
        transferred_amount,
        locked_amount,
        locksroot,
        channel_state.identifier,
    )

    queue_name = channel_state.identifier
    direct_transfer = SendDirectTransfer(
        recipient,
        queue_name,
        message_identifier,
        payment_identifier,
        balance_proof,
        registry_address,
        token,
    )

    return direct_transfer
Example #10
0
def make_transfer(
    amount,
    initiator,
    target,
    expiration,
    secret,
    identifier=1,
    nonce=1,
    transferred_amount=0,
    locked_amount=None,
    token_network_identifier=UNIT_TOKEN_NETWORK_ADDRESS,
    channel_identifier=UNIT_CHANNEL_ID,
    locksroot=None,
    token=UNIT_TOKEN_ADDRESS,
):

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

    if locksroot is None:
        locksroot = lock.lockhash
        locked_amount = amount
    else:
        assert locked_amount

    unsigned_balance_proof = BalanceProofUnsignedState(
        nonce=nonce,
        transferred_amount=transferred_amount,
        locked_amount=locked_amount,
        locksroot=locksroot,
        token_network_identifier=token_network_identifier,
        channel_address=channel_identifier,
        chain_id=UNIT_CHAIN_ID,
    )

    transfer_state = LockedTransferUnsignedState(
        identifier,
        token,
        unsigned_balance_proof,
        lock,
        initiator,
        target,
    )

    return transfer_state
Example #11
0
def make_transfer(amount,
                  initiator,
                  target,
                  expiration,
                  secret,
                  identifier=1,
                  nonce=1,
                  transferred_amount=0,
                  locked_amount=None,
                  channel_identifier=UNIT_CHANNEL_ADDRESS,
                  locksroot=None,
                  token=UNIT_TOKEN_ADDRESS):

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

    if locksroot is None:
        locksroot = lock.lockhash
        locked_amount = amount
    else:
        assert locked_amount

    unsigned_balance_proof = BalanceProofUnsignedState(
        nonce,
        transferred_amount,
        locked_amount,
        locksroot,
        channel_identifier,
    )

    transfer_state = LockedTransferUnsignedState(
        identifier,
        UNIT_REGISTRY_IDENTIFIER,
        token,
        unsigned_balance_proof,
        lock,
        initiator,
        target,
    )

    return transfer_state
Example #12
0
def create_unlock(channel_state, message_identifier, payment_identifier,
                  secret, lock):
    msg = 'caller must make sure the lock is known'
    assert is_lock_pending(channel_state.our_state, lock.secrethash), msg

    msg = 'caller must make sure the channel is open'
    assert get_status(channel_state) == CHANNEL_STATE_OPENED, msg

    our_balance_proof = channel_state.our_state.balance_proof
    if our_balance_proof:
        transferred_amount = lock.amount + our_balance_proof.transferred_amount
    else:
        transferred_amount = lock.amount

    merkletree = compute_merkletree_without(
        channel_state.our_state.merkletree,
        lock.lockhash,
    )
    locksroot = merkleroot(merkletree)

    token = channel_state.token_address
    nonce = get_next_nonce(channel_state.our_state)
    recipient = channel_state.partner_state.address

    balance_proof = BalanceProofUnsignedState(
        nonce,
        transferred_amount,
        locksroot,
        channel_state.identifier,
    )

    queue_name = channel_state.identifier
    unlock_lock = SendBalanceProof(
        recipient,
        queue_name,
        message_identifier,
        payment_identifier,
        token,
        secret,
        balance_proof,
    )

    return unlock_lock, merkletree
Example #13
0
def _(properties, defaults=None) -> LockedTransferUnsignedState:
    defaults = defaults or LOCKED_TRANSFER_DEFAULTS
    parameters = _properties_to_dict(properties, defaults)

    lock = HashTimeLockState(
        amount=parameters.pop('amount'),
        expiration=parameters.pop('expiration'),
        secrethash=sha3(parameters.pop('secret')),
    )

    balance_proof_parameters = _properties_to_dict(
        parameters.pop('balance_proof'),
        defaults.balance_proof,
    )
    if balance_proof_parameters['locksroot'] == EMPTY_MERKLE_ROOT:
        balance_proof_parameters['locksroot'] = lock.lockhash
    balance_proof = BalanceProofUnsignedState(**balance_proof_parameters)

    return LockedTransferUnsignedState(balance_proof=balance_proof,
                                       lock=lock,
                                       **parameters)
Example #14
0
def create_unlock(channel_state, identifier, secret, lock):
    msg = 'caller must make sure the lock is known'
    assert is_known(channel_state.our_state, lock.hashlock), msg

    our_balance_proof = channel_state.our_state.balance_proof
    if our_balance_proof:
        transferred_amount = lock.amount + our_balance_proof.transferred_amount
    else:
        transferred_amount = lock.amount

    merkletree = compute_merkletree_without(
        channel_state.our_state.merkletree,
        lock.lockhash,
    )
    locksroot = merkleroot(merkletree)

    token = channel_state.token_address
    nonce = get_next_nonce(channel_state.our_state)
    recipient = channel_state.partner_state.address

    balance_proof = BalanceProofUnsignedState(
        nonce,
        transferred_amount,
        locksroot,
        channel_state.identifier,
    )

    unlock_lock = SendBalanceProof2(
        identifier,
        token,
        recipient,
        secret,
        balance_proof,
    )

    return unlock_lock, merkletree
Example #15
0
def _(properties, defaults=None) -> BalanceProofUnsignedState:
    return BalanceProofUnsignedState(
        **_properties_to_kwargs(properties, defaults
                                or BALANCE_PROOF_DEFAULTS), )
Example #16
0
def create_sendlockedtransfer(
    channel_state: NettingChannelState,
    initiator: typing.InitiatorAddress,
    target: typing.TargetAddress,
    amount: typing.PaymentAmount,
    message_identifier: typing.MessageID,
    payment_identifier: typing.PaymentID,
    expiration: typing.BlockExpiration,
    secrethash: typing.SecretHash,
) -> SendLockedTransfer:
    our_state = channel_state.our_state
    partner_state = channel_state.partner_state
    our_balance_proof = our_state.balance_proof

    msg = 'caller must make sure there is enough balance'
    assert amount <= get_distributable(our_state, partner_state), msg

    msg = 'caller must make sure the channel is open'
    assert get_status(channel_state) == CHANNEL_STATE_OPENED, msg

    lock = HashTimeLockState(
        amount,
        expiration,
        secrethash,
    )

    merkletree = compute_merkletree_with(
        channel_state.our_state.merkletree,
        lock.lockhash,
    )
    # The caller must ensure the same lock is not being used twice
    assert merkletree, 'lock is already registered'

    locksroot = merkleroot(merkletree)

    if our_balance_proof:
        transferred_amount = our_balance_proof.transferred_amount
    else:
        transferred_amount = 0

    token = channel_state.token_address
    nonce = get_next_nonce(channel_state.our_state)
    recipient = channel_state.partner_state.address
    locked_amount = get_amount_locked(
        our_state) + amount  # the new lock is not registered yet

    balance_proof = BalanceProofUnsignedState(
        nonce,
        transferred_amount,
        locked_amount,
        locksroot,
        channel_state.token_network_identifier,
        channel_state.identifier,
    )

    locked_transfer = LockedTransferUnsignedState(
        payment_identifier,
        token,
        balance_proof,
        lock,
        initiator,
        target,
    )

    queue_name = channel_state.identifier
    lockedtransfer = SendLockedTransfer(
        recipient,
        queue_name,
        message_identifier,
        locked_transfer,
    )

    return lockedtransfer, merkletree
Example #17
0
 def make_unsigned_balance_proof(nonce):
     return BalanceProofUnsignedState.from_dict(
         make_balance_proof(nonce=nonce,
                            signer=LocalSigner(HOP1_KEY),
                            amount=1).to_dict(), )