tfchain/TFChainWallet.py
def _create_contract(self, recipient, amount, refund_time, source, refund, data, secret_hash, submit):
        """
        Create a new atomic swap contract,
        the logic for both the initiate as well as participate phase.
        """
        # define the amount
        amount = Currency(value=amount)
        if amount <= 0:
            raise ValueError("no amount is defined to be swapped")

        # define the miner fee
        miner_fee = self._minium_miner_fee

        # ensure the amount is bigger than the miner fee,
        # otherwise the contract cannot be redeemed/refunded
        if amount <= miner_fee:
            raise tfchain.errors.AtomicSwapInsufficientAmountError(amount=amount, minimum_miner_fee=miner_fee)

        # define the coin inputs
        balance = self._wallet.balance
        inputs, remainder, suggested_refund = balance.fund(amount+miner_fee, source=source)

        # define the refund
        if refund is not None:
            refund = ConditionTypes.from_recipient(refund)
        elif suggested_refund is not None:
            refund = ConditionTypes.from_recipient(suggested_refund)
        else:
            refund = ConditionTypes.from_recipient(self._wallet.address)

        # define the sender
        if isinstance(refund, ConditionUnlockHash):
            sender = refund.unlockhash
        else:
            sender = self._wallet.address

        # create and populate the transaction
        txn = tftransactions.new()
        txn.coin_inputs = inputs
        txn.miner_fee_add(self._minium_miner_fee)
        txn.data = data

        # define refund time already, so we can use the chain time as the current time
        if isinstance(refund_time, str):
            chain_time = self._chain_time
            refund_time = OutputLock(value=refund_time, current_timestamp=chain_time).value
        elif not isinstance(refund_time, int):
            raise TypeError("expected refund time to be an integer or string, not to be of type {}".format(type(refund_time)))

        # define the atomic swap contract and add it as a coin output
        asc = ConditionTypes.atomic_swap_new(
            sender=sender, receiver=recipient, hashed_secret=secret_hash, lock_time=refund_time)
        txn.coin_output_add(condition=asc, value=amount)

        # optionally add a refund coin output
        if remainder > 0:
            txn.coin_output_add(condition=refund, value=remainder)

        # get all signature requests
        sig_requests = txn.signature_requests_new()
        if len(sig_requests) == 0:
            raise Exception("BUG: sig requests should not be empty at this point, please fix or report as an issue")

        # fulfill the signature requests that we can fulfill
        for request in sig_requests:
            try:
                key_pair = self._wallet.key_pair_get(request.wallet_address)
                input_hash = request.input_hash_new(public_key=key_pair.public_key)
                signature = key_pair.sign(input_hash)
                request.signature_fulfill(public_key=key_pair.public_key, signature=signature)
            except KeyError:
                pass # this is acceptable due to how we directly try the key_pair_get method

        # assign all coin output ID's for atomic swap contracts,
        # as we always care about the contract's output ID and
        # the refund coin output has to be our coin output
        for idx, co in enumerate(txn.coin_outputs):
            co.id = txn.coin_outputid_new(idx)

        # submit if possible
        submit = submit and txn.is_fulfilled()
        if submit:
            txn.id = self._transaction_put(transaction=txn)
            # update balance
            for ci in txn.coin_inputs:
                balance.output_add(ci.parent_output, confirmed=False, spent=True)
            addresses = self._wallet.addresses + balance.addresses_multisig
            for idx, co in enumerate(txn.coin_outputs):
                if str(co.condition.unlockhash) in addresses:
                    balance.output_add(co, confirmed=False, spent=False)

        # return the txn, as well as the submit status as a boolean
        return TransactionSendResult(txn, submit)

        Create a new atomic swap contract,
        the logic for both the initiate as well as participate phase.
        
Create a new atomic swap contract,
the logic for both the initiate as well as participate phase.
Create a new atomic swap contract the logic for both the initiate as well as participate phase .
def _create_contract(self, recipient, amount, refund_time, source, refund, data, secret_hash, submit):
        amount = Currency(value=amount)
        if amount <= 0:
            raise ValueError("no amount is defined to be swapped")
        miner_fee = self._minium_miner_fee
        if amount <= miner_fee:
            raise tfchain.errors.AtomicSwapInsufficientAmountError(amount=amount, minimum_miner_fee=miner_fee)
        balance = self._wallet.balance
        inputs, remainder, suggested_refund = balance.fund(amount+miner_fee, source=source)
        if refund is not None:
            refund = ConditionTypes.from_recipient(refund)
        elif suggested_refund is not None:
            refund = ConditionTypes.from_recipient(suggested_refund)
        else:
            refund = ConditionTypes.from_recipient(self._wallet.address)
        if isinstance(refund, ConditionUnlockHash):
            sender = refund.unlockhash
        else:
            sender = self._wallet.address
        txn = tftransactions.new()
        txn.coin_inputs = inputs
        txn.miner_fee_add(self._minium_miner_fee)
        txn.data = data
        if isinstance(refund_time, str):
            chain_time = self._chain_time
            refund_time = OutputLock(value=refund_time, current_timestamp=chain_time).value
        elif not isinstance(refund_time, int):
            raise TypeError("expected refund time to be an integer or string, not to be of type {}".format(type(refund_time)))
        asc = ConditionTypes.atomic_swap_new(
            sender=sender, receiver=recipient, hashed_secret=secret_hash, lock_time=refund_time)
        txn.coin_output_add(condition=asc, value=amount)
        if remainder > 0:
            txn.coin_output_add(condition=refund, value=remainder)
        sig_requests = txn.signature_requests_new()
        if len(sig_requests) == 0:
            raise Exception("BUG: sig requests should not be empty at this point, please fix or report as an issue")
        for request in sig_requests:
            try:
                key_pair = self._wallet.key_pair_get(request.wallet_address)
                input_hash = request.input_hash_new(public_key=key_pair.public_key)
                signature = key_pair.sign(input_hash)
                request.signature_fulfill(public_key=key_pair.public_key, signature=signature)
            except KeyError:
                pass 
        for idx, co in enumerate(txn.coin_outputs):
            co.id = txn.coin_outputid_new(idx)
        submit = submit and txn.is_fulfilled()
        if submit:
            txn.id = self._transaction_put(transaction=txn)
            for ci in txn.coin_inputs:
                balance.output_add(ci.parent_output, confirmed=False, spent=True)
            addresses = self._wallet.addresses + balance.addresses_multisig
            for idx, co in enumerate(txn.coin_outputs):
                if str(co.condition.unlockhash) in addresses:
                    balance.output_add(co, confirmed=False, spent=False)
        return TransactionSendResult(txn, submit)
def _create_contract ( self , recipient , amount , refund_time , source , refund , data , secret_hash , submit ) : amount = Currency ( value = amount ) if amount <= 0 : raise ValueError ( "no amount is defined to be swapped" ) miner_fee = self . _minium_miner_fee if amount <= miner_fee : raise tfchain . errors . AtomicSwapInsufficientAmountError ( amount = amount , minimum_miner_fee = miner_fee ) balance = self . _wallet . balance inputs , remainder , suggested_refund = balance . fund ( amount + miner_fee , source = source ) if refund is not None : refund = ConditionTypes . from_recipient ( refund ) elif suggested_refund is not None : refund = ConditionTypes . from_recipient ( suggested_refund ) else : refund = ConditionTypes . from_recipient ( self . _wallet . address ) if isinstance ( refund , ConditionUnlockHash ) : sender = refund . unlockhash else : sender = self . _wallet . address txn = tftransactions . new ( ) txn . coin_inputs = inputs txn . miner_fee_add ( self . _minium_miner_fee ) txn . data = data if isinstance ( refund_time , str ) : chain_time = self . _chain_time refund_time = OutputLock ( value = refund_time , current_timestamp = chain_time ) . value elif not isinstance ( refund_time , int ) : raise TypeError ( "expected refund time to be an integer or string, not to be of type {}" . format ( type ( refund_time ) ) ) asc = ConditionTypes . atomic_swap_new ( sender = sender , receiver = recipient , hashed_secret = secret_hash , lock_time = refund_time ) txn . coin_output_add ( condition = asc , value = amount ) if remainder > 0 : txn . coin_output_add ( condition = refund , value = remainder ) sig_requests = txn . signature_requests_new ( ) if len ( sig_requests ) == 0 : raise Exception ( "BUG: sig requests should not be empty at this point, please fix or report as an issue" ) for request in sig_requests : try : key_pair = self . _wallet . key_pair_get ( request . wallet_address ) input_hash = request . input_hash_new ( public_key = key_pair . public_key ) signature = key_pair . sign ( input_hash ) request . signature_fulfill ( public_key = key_pair . public_key , signature = signature ) except KeyError : pass for idx , co in enumerate ( txn . coin_outputs ) : co . id = txn . coin_outputid_new ( idx ) submit = submit and txn . is_fulfilled ( ) if submit : txn . id = self . _transaction_put ( transaction = txn ) for ci in txn . coin_inputs : balance . output_add ( ci . parent_output , confirmed = False , spent = True ) addresses = self . _wallet . addresses + balance . addresses_multisig for idx , co in enumerate ( txn . coin_outputs ) : if str ( co . condition . unlockhash ) in addresses : balance . output_add ( co , confirmed = False , spent = False ) return TransactionSendResult ( txn , submit )
Create a new atomic swap contract,
the logic for both the initiate as well as participate phase.
Create a new atomic swap contract the logic for both the initiate as well as participate phase .
"""
        Create a new atomic swap contract,
        the logic for both the initiate as well as participate phase.
        """ # define the amount # define the miner fee # ensure the amount is bigger than the miner fee, # otherwise the contract cannot be redeemed/refunded # define the coin inputs # define the refund # define the sender # create and populate the transaction # define refund time already, so we can use the chain time as the current time # define the atomic swap contract and add it as a coin output # optionally add a refund coin output # get all signature requests # fulfill the signature requests that we can fulfill # this is acceptable due to how we directly try the key_pair_get method # assign all coin output ID's for atomic swap contracts, # as we always care about the contract's output ID and # the refund coin output has to be our coin output # submit if possible # update balance # return the txn, as well as the submit status as a boolean
self
recipient
amount
refund_time
source
refund
data
secret_hash
submit