def make_signed_balance_proof_from_unsigned( unsigned: BalanceProofUnsignedState, signer: Signer) -> BalanceProofSignedState: balance_hash = hash_balance_data( transferred_amount=unsigned.transferred_amount, locked_amount=unsigned.locked_amount, locksroot=unsigned.locksroot, ) additional_hash = make_additional_hash() data_to_sign = balance_proof.pack_balance_proof( balance_hash=balance_hash, additional_hash=additional_hash, canonical_identifier=unsigned.canonical_identifier, nonce=unsigned.nonce, ) signature = signer.sign(data=data_to_sign) sender = signer.address return BalanceProofSignedState( nonce=unsigned.nonce, transferred_amount=unsigned.transferred_amount, locked_amount=unsigned.locked_amount, locksroot=unsigned.locksroot, message_hash=additional_hash, signature=signature, sender=sender, canonical_identifier=unsigned.canonical_identifier, )
def make_signed_balance_proof( nonce, transferred_amount, channel_address, locksroot, extra_hash, private_key, sender_address ): data_to_sign = balance_proof.signing_data( nonce, transferred_amount, channel_address, locksroot, extra_hash, ) signature = signing.sign(data_to_sign, private_key) signed_balance_proof = BalanceProofSignedState( nonce, transferred_amount, locksroot, channel_address, extra_hash, signature, sender_address, ) return signed_balance_proof
def lockedtransfersigned_from_message(message): """ Create LockedTransferSignedState from a LockedTransfer message. """ balance_proof = BalanceProofSignedState( message.nonce, message.transferred_amount, message.locksroot, message.channel, message.message_hash, message.signature, message.sender, ) lock = HashTimeLockState( message.lock.amount, message.lock.expiration, message.lock.secrethash, ) transfer_state = LockedTransferSignedState( message.payment_identifier, message.token, balance_proof, lock, message.initiator, message.target, ) return transfer_state
def _(properties: BalanceProofSignedStateProperties, defaults=None) -> BalanceProofSignedState: defaults = defaults or BALANCE_PROOF_SIGNED_STATE_DEFAULTS params = _properties_to_dict(properties, defaults) params.update( _properties_to_dict(params.pop('balance_proof'), defaults.balance_proof), ) signer = LocalSigner(params.pop('pkey')) if params['signature'] is EMPTY: keys = ('transferred_amount', 'locked_amount', 'locksroot') balance_hash = hash_balance_data(**_partial_dict(params, *keys)) canonical_identifier = CanonicalIdentifier( chain_identifier=params.pop('chain_id'), token_network_address=params.pop('token_network_identifier'), channel_identifier=params.pop('channel_identifier'), ) params['canonical_identifier'] = canonical_identifier data_to_sign = balance_proof.pack_balance_proof( balance_hash=balance_hash, additional_hash=params['message_hash'], canonical_identifier=canonical_identifier, nonce=params.get('nonce'), ) params['signature'] = signer.sign(data=data_to_sign) return BalanceProofSignedState(**params)
def make_signed_balance_proof( nonce: typing.Nonce = EMPTY, transferred_amount: typing.TokenAmount = EMPTY, locked_amount: typing.TokenAmount = EMPTY, token_network_address: typing.TokenNetworkID = EMPTY, channel_identifier: typing.ChannelID = EMPTY, locksroot: typing.Locksroot = EMPTY, extra_hash: typing.Keccak256 = EMPTY, private_key: bytes = EMPTY, sender_address: typing.Address = EMPTY, ) -> BalanceProofSignedState: nonce = if_empty(nonce, make_uint256()) transferred_amount = if_empty(transferred_amount, make_uint256()) locked_amount = if_empty(locked_amount, make_uint256()) token_network_address = if_empty(token_network_address, make_address()) channel_identifier = if_empty(channel_identifier, make_uint256()) locksroot = if_empty(locksroot, make_32bytes()) extra_hash = if_empty(extra_hash, make_keccak_hash()) private_key = if_empty(private_key, make_privatekey()) sender_address = if_empty(sender_address, make_address()) signer = LocalSigner(private_key) balance_hash = hash_balance_data( transferred_amount=transferred_amount, locked_amount=locked_amount, locksroot=locksroot, ) data_to_sign = balance_proof.pack_balance_proof( nonce=nonce, balance_hash=balance_hash, additional_hash=extra_hash, canonical_identifier=CanonicalIdentifier( chain_identifier=UNIT_CHAIN_ID, token_network_address=token_network_address, channel_identifier=channel_identifier, ), ) signature = signer.sign(data=data_to_sign) return BalanceProofSignedState( nonce=nonce, transferred_amount=transferred_amount, locked_amount=locked_amount, locksroot=locksroot, message_hash=extra_hash, signature=signature, sender=sender_address, canonical_identifier=CanonicalIdentifier( chain_identifier=UNIT_CHAIN_ID, token_network_address=token_network_address, channel_identifier=channel_identifier, ), )
def make_signed_balance_proof( nonce, transferred_amount, locked_amount, token_network_address, channel_address, locksroot, extra_hash, private_key, sender_address, ): data_to_sign = balance_proof.signing_data( nonce, transferred_amount, locked_amount, channel_address, locksroot, extra_hash, ) balance_hash = hash_balance_data( transferred_amount, locked_amount, locksroot, ) data_to_sign = balance_proof.pack_signing_data( nonce=nonce, balance_hash=balance_hash, additional_hash=extra_hash, channel_identifier=channel_address, token_network_identifier=token_network_address, chain_id=UNIT_CHAIN_ID, ) signature = signing.sign(data_to_sign, private_key) signed_balance_proof = BalanceProofSignedState( nonce, transferred_amount, locked_amount, locksroot, token_network_address, channel_address, extra_hash, signature, sender_address, UNIT_CHAIN_ID, ) return signed_balance_proof
def balanceproof_from_envelope(envelope_message: EnvelopeMessage) -> BalanceProofSignedState: assert envelope_message.sender, "envelope_message must be signed" return BalanceProofSignedState( nonce=envelope_message.nonce, transferred_amount=envelope_message.transferred_amount, locked_amount=envelope_message.locked_amount, locksroot=envelope_message.locksroot, message_hash=AdditionalHash(envelope_message.message_hash), signature=envelope_message.signature, sender=envelope_message.sender, canonical_identifier=CanonicalIdentifier( chain_identifier=envelope_message.chain_id, token_network_address=envelope_message.token_network_address, channel_identifier=envelope_message.channel_identifier, ), )
def _(properties: BalanceProofSignedStateProperties, defaults=None) -> BalanceProofSignedState: defaults = defaults or BalanceProofSignedStateProperties.DEFAULTS params = create_properties(properties, defaults).__dict__ signer = LocalSigner(params.pop("pkey")) if params["signature"] is GENERATE: keys = ("transferred_amount", "locked_amount", "locksroot") balance_hash = hash_balance_data(**_partial_dict(params, *keys)) data_to_sign = balance_proof.pack_balance_proof( balance_hash=balance_hash, additional_hash=params["message_hash"], canonical_identifier=params["canonical_identifier"], nonce=params.get("nonce"), ) params["signature"] = signer.sign(data=data_to_sign) return BalanceProofSignedState(**params)
def _(properties: BalanceProofSignedStateProperties, defaults=None) -> BalanceProofSignedState: defaults = defaults or BALANCE_PROOF_SIGNED_STATE_DEFAULTS params = _properties_to_dict(properties, defaults) params.update( _properties_to_dict(params.pop('balance_proof'), defaults.balance_proof), ) if params['signature'] is EMPTY: keys = ('transferred_amount', 'locked_amount', 'locksroot') balance_hash = hash_balance_data(**_partial_dict(params, *keys)) keys = ('nonce', 'channel_identifier', 'token_network_identifier') data_to_sign = balance_proof.pack_balance_proof( balance_hash=balance_hash, additional_hash=params['message_hash'], chain_id=UNIT_CHAIN_ID, **_partial_dict(params, *keys), ) params['signature'] = eth_sign(privkey=params.pop('pkey'), data=data_to_sign) return BalanceProofSignedState(**params)