def update(self, amount, lockhash): self._merkletree = channel.compute_merkletree_with( self._merkletree, lockhash) if self.properties: self.properties = factories.replace( self.properties, locked_amount=self.properties.locked_amount + amount, locksroot=merkleroot(self._merkletree), nonce=self.properties.nonce + 1, ) else: self.properties = factories.BalanceProofProperties( transferred_amount=0, locked_amount=amount, nonce=1, locksroot=merkleroot(self._merkletree), canonical_identifier=self._canonical_identifier, )
def make_signed_transfer_for( channel_state: NettingChannelState = EMPTY, properties: LockedTransferSignedStateProperties = None, defaults: LockedTransferSignedStateProperties = None, compute_locksroot: bool = False, allow_invalid: bool = False, only_transfer: bool = True, ) -> LockedTransferSignedState: properties: LockedTransferSignedStateProperties = create_properties( properties or LockedTransferSignedStateProperties(), defaults or SIGNED_TRANSFER_FOR_CHANNEL_DEFAULTS, ) channel_state = if_empty(channel_state, create(NettingChannelStateProperties())) if not allow_invalid: ok = channel_state.reveal_timeout < properties.expiration < channel_state.settle_timeout assert ok, "Expiration must be between reveal_timeout and settle_timeout." assert privatekey_to_address(properties.pkey) == properties.sender if properties.sender == channel_state.our_state.address: recipient = channel_state.partner_state.address elif properties.sender == channel_state.partner_state.address: recipient = channel_state.our_state.address else: assert False, "Given sender does not participate in given channel." if compute_locksroot: lock = Lock( amount=properties.amount, expiration=properties.expiration, secrethash=sha3(properties.secret), ) locksroot = merkleroot( channel.compute_merkletree_with( merkletree=channel_state.partner_state.merkletree, lockhash=sha3(lock.as_bytes))) else: locksroot = properties.locksroot if only_transfer: transfer_properties = LockedTransferUnsignedStateProperties( locksroot=locksroot, canonical_identifier=channel_state.canonical_identifier, locked_amount=properties.amount, ) else: transfer_properties = LockedTransferUnsignedStateProperties( locksroot=locksroot, canonical_identifier=channel_state.canonical_identifier) transfer = create( LockedTransferSignedStateProperties(recipient=recipient, **transfer_properties.__dict__), defaults=properties, ) if not allow_invalid: is_valid, msg, _ = channel.is_valid_lockedtransfer( transfer_state=transfer, channel_state=channel_state, sender_state=channel_state.partner_state, receiver_state=channel_state.our_state, ) assert is_valid, msg return transfer
def make_signed_transfer_for( channel_state, amount, initiator, target, expiration, secret, identifier=1, nonce=1, transferred_amount=0, locked_amount=None, pkey=UNIT_TRANSFER_PKEY, sender=UNIT_TRANSFER_SENDER, compute_locksroot=False, allow_invalid=False, ): if not allow_invalid: msg = 'expiration must be lower than settle_timeout' assert expiration < channel_state.settle_timeout, msg msg = 'expiration must be larger than settle_timeout' assert expiration > channel_state.reveal_timeout, msg pubkey = pkey.public_key.format(compressed=False) assert publickey_to_address(pubkey) == sender assert sender in (channel_state.our_state.address, channel_state.partner_state.address) if sender == channel_state.our_state.address: recipient = channel_state.partner_state.address else: recipient = channel_state.our_state.address channel_identifier = channel_state.identifier token_address = channel_state.token_address if compute_locksroot: locksroot = merkleroot( channel.compute_merkletree_with( channel_state.partner_state.merkletree, sha3(Lock(amount, expiration, sha3(secret)).as_bytes), )) else: locksroot = EMPTY_MERKLE_ROOT mediated_transfer = make_signed_transfer( amount, initiator, target, expiration, secret, payment_identifier=identifier, nonce=nonce, transferred_amount=transferred_amount, locked_amount=locked_amount, recipient=recipient, channel_identifier=channel_identifier, token=token_address, pkey=pkey, sender=sender, locksroot=locksroot, ) # Do *not* register the transfer here if not allow_invalid: is_valid, msg, _ = channel.is_valid_lockedtransfer( mediated_transfer, channel_state, channel_state.partner_state, channel_state.our_state, ) assert is_valid, msg return mediated_transfer
def make_signed_transfer_for( channel_state: NettingChannelState = EMPTY, 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, pkey: PrivateKey = EMPTY, sender: typing.Address = EMPTY, compute_locksroot: typing.Locksroot = EMPTY, allow_invalid: bool = EMPTY, ) -> LockedTransferSignedState: channel_state = if_empty(channel_state, make_channel_state()) amount = if_empty(amount, 0) 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) pkey = if_empty(pkey, UNIT_TRANSFER_PKEY) sender = if_empty(sender, UNIT_TRANSFER_SENDER) compute_locksroot = if_empty(compute_locksroot, False) allow_invalid = if_empty(allow_invalid, False) if not allow_invalid: msg = 'expiration must be lower than settle_timeout' assert expiration < channel_state.settle_timeout, msg msg = 'expiration must be larger than reveal_timeout' assert expiration > channel_state.reveal_timeout, msg pubkey = pkey.public_key.format(compressed=False) assert publickey_to_address(pubkey) == sender assert sender in (channel_state.our_state.address, channel_state.partner_state.address) if sender == channel_state.our_state.address: recipient = channel_state.partner_state.address else: recipient = channel_state.our_state.address channel_identifier = channel_state.identifier token_address = channel_state.token_address if compute_locksroot: locksroot = merkleroot( channel.compute_merkletree_with( channel_state.partner_state.merkletree, sha3(Lock(amount, expiration, sha3(secret)).as_bytes), )) else: locksroot = EMPTY_MERKLE_ROOT mediated_transfer = make_signed_transfer( amount=amount, initiator=initiator, target=target, expiration=expiration, secret=secret, payment_identifier=identifier, nonce=nonce, transferred_amount=transferred_amount, locksroot=locksroot, locked_amount=locked_amount, recipient=recipient, channel_identifier=channel_identifier, token_network_address=channel_state.token_network_identifier, token=token_address, pkey=pkey, sender=sender, ) # Do *not* register the transfer here if not allow_invalid: is_valid, msg, _ = channel.is_valid_lockedtransfer( transfer_state=mediated_transfer, channel_state=channel_state, sender_state=channel_state.partner_state, receiver_state=channel_state.our_state, ) assert is_valid, msg return mediated_transfer
def make_signed_transfer_for( channel_state: NettingChannelState = EMPTY, properties: LockedTransferSignedStateProperties = None, defaults: LockedTransferSignedStateProperties = None, compute_locksroot: bool = False, allow_invalid: bool = False, only_transfer: bool = True, ) -> LockedTransferSignedState: properties: LockedTransferSignedStateProperties = create_properties( properties or LockedTransferSignedStateProperties(), defaults or SIGNED_TRANSFER_FOR_CHANNEL_DEFAULTS, ) channel_state = if_empty(channel_state, create(NettingChannelStateProperties())) if not allow_invalid: expiration = properties.transfer.expiration valid = channel_state.reveal_timeout < expiration < channel_state.settle_timeout assert valid, 'Expiration must be between reveal_timeout and settle_timeout.' pubkey = properties.pkey.public_key.format(compressed=False) assert publickey_to_address(pubkey) == properties.sender if properties.sender == channel_state.our_state.address: recipient = channel_state.partner_state.address elif properties.sender == channel_state.partner_state.address: recipient = channel_state.our_state.address else: assert False, 'Given sender does not participate in given channel.' if compute_locksroot: lock = Lock( amount=properties.transfer.amount, expiration=properties.transfer.expiration, secrethash=sha3(properties.transfer.secret), ) locksroot = merkleroot(channel.compute_merkletree_with( merkletree=channel_state.partner_state.merkletree, lockhash=sha3(lock.as_bytes), )) else: locksroot = properties.transfer.balance_proof.locksroot if only_transfer: balance_proof_properties = BalanceProofProperties( locksroot=locksroot, channel_identifier=channel_state.identifier, transferred_amount=0, locked_amount=properties.transfer.amount, ) else: balance_proof_properties = BalanceProofProperties( locksroot=locksroot, channel_identifier=channel_state.identifier, ) transfer = create( LockedTransferSignedStateProperties( recipient=recipient, transfer=LockedTransferProperties( balance_proof=balance_proof_properties, ), ), defaults=properties, ) if not allow_invalid: is_valid, msg, _ = channel.is_valid_lockedtransfer( transfer_state=transfer, channel_state=channel_state, sender_state=channel_state.partner_state, receiver_state=channel_state.our_state, ) assert is_valid, msg return transfer
def make_signed_transfer_for( channel_state: NettingChannelState = EMPTY, properties: LockedTransferSignedStateProperties = None, defaults: LockedTransferSignedStateProperties = None, compute_locksroot: bool = False, allow_invalid: bool = False, only_transfer: bool = True, ) -> LockedTransferSignedState: properties: LockedTransferSignedStateProperties = create_properties( properties or LockedTransferSignedStateProperties(), defaults or SIGNED_TRANSFER_FOR_CHANNEL_DEFAULTS, ) channel_state = if_empty(channel_state, create(NettingChannelStateProperties())) if not allow_invalid: expiration = properties.transfer.expiration valid = channel_state.reveal_timeout < expiration < channel_state.settle_timeout assert valid, 'Expiration must be between reveal_timeout and settle_timeout.' pubkey = properties.pkey.public_key.format(compressed=False) assert publickey_to_address(pubkey) == properties.sender if properties.sender == channel_state.our_state.address: recipient = channel_state.partner_state.address elif properties.sender == channel_state.partner_state.address: recipient = channel_state.our_state.address else: assert False, 'Given sender does not participate in given channel.' if compute_locksroot: lock = Lock( amount=properties.transfer.amount, expiration=properties.transfer.expiration, secrethash=sha3(properties.transfer.secret), ) locksroot = merkleroot( channel.compute_merkletree_with( merkletree=channel_state.partner_state.merkletree, lockhash=sha3(lock.as_bytes), )) else: locksroot = properties.transfer.balance_proof.locksroot if only_transfer: balance_proof_properties = BalanceProofProperties( locksroot=locksroot, channel_identifier=channel_state.identifier, transferred_amount=0, locked_amount=properties.transfer.amount, ) else: balance_proof_properties = BalanceProofProperties( locksroot=locksroot, channel_identifier=channel_state.identifier, ) transfer = create( LockedTransferSignedStateProperties( recipient=recipient, transfer=LockedTransferProperties( balance_proof=balance_proof_properties, ), ), defaults=properties, ) if not allow_invalid: is_valid, msg, _ = channel.is_valid_lockedtransfer( transfer_state=transfer, channel_state=channel_state, sender_state=channel_state.partner_state, receiver_state=channel_state.our_state, ) assert is_valid, msg return transfer