Esempio n. 1
0
    def create_deposit_tx(self, hash160):
        """Return a mocked deposit transaction.

        This uses the hex from some transaction I had lying around and then
        modifies the outputs to pay to the multisig script hash. I basically
        just needed some UTXO's to pay for this thing.
        """
        tx_bytes = codecs.decode(
            "010000000295c5ca7c5d339d476456e5798bc5d483c37234adedeea1d6d58bd7"
            "4dcb3ab488000000006b483045022100bdb985c42ff8db57bd936fd2c7567e0b"
            "4220012b568a956c8b1dcfdef049effb0220637c5f5aad734f3fe42f8a5e2879"
            "174d219dcda516a370d8fd9d2a8d97193668012102a00465ffe29a8a3abd021b"
            "8037fb4467c0a734588449694dda8309495de5dc8affffffff1443c6572f221d"
            "02e072b5efd3af62833ade83c681e8c1ed6620a4020c300aac000000006b4830"
            "4502210086414fe8cc24bbcdebc4238201c3d021d4cd0b0a39b69538227fcfd5"
            "cca8df8c0220540cd8d8ab4f7a03a89fb686cec4f4a47f0b4d9d477c383b040b"
            "a1d7f0b5313c01210237c3846c8f7f86c86078344ff0e09c73d48bec4a1e60dd"
            "99df1ddb2816d1196dffffffff02b0ad01000000000017a9147487cdfa3235a3"
            "479dc05a10504bd8127aa0bab08780380100000000001976a914928f936fc8f9"
            "5dc733d64ebef37d7f57ea70813a88ac00000000",
            "hex_codec",
        )
        tx = Transaction.from_bytes(tx_bytes)[0]

        # Modify the transaction to use the hash160 of a redeem script
        tx.outputs[0].script = Script.build_p2sh(hash160)
        return tx
Esempio n. 2
0
    def verify_half_signed_tx(self, tx_from_user):
        """Verify a half-signed refund is a valid transaction."""
        redeem_script = get_redeem_script(tx_from_user)

        # Verify partial signature in refund transaction
        script_pubkey = Script.build_p2sh(redeem_script.hash160())
        if not tx_from_user.verify_partial_multisig(0, script_pubkey):
            raise TransactionVerificationError("Half-signed transaction could not be verified.")

        return True
Esempio n. 3
0
    def receive_payment(self, deposit_txid, payment_tx):
        """Receive and process a payment within the channel.

        The customer makes a payment in the channel by sending the merchant a
        half-signed payment transaction. The merchant signs the other half of
        the transaction and saves it in its records (but does not broadcast it
        or send it to the customer). The merchant responds with 200 to verify
        that the payment was handled successfully.

        Args:
            deposit_txid (string): string representation of the deposit
                transaction hash. This is used to look up the payment channel.
            payment_tx (string): half-signed payment transaction from a
                customer.
        Returns:
            (string): payment transaction id
        """
        with self.lock:
            # Parse payment channel `payment` parameters
            payment_tx = Transaction.from_hex(payment_tx)

            # Get channel and addresses related to the deposit
            channel = self._db.pc.lookup(deposit_txid)

            if not channel:
                raise PaymentChannelNotFoundError('Related channel not found.')

            # Get merchant public key information from payment channel
            redeem_script = PaymentChannelRedeemScript.from_bytes(payment_tx.inputs[0].script[-1])
            merch_pubkey = redeem_script.merchant_public_key

            # Verify that the payment has a valid signature from the customer
            txn_copy = payment_tx._copy_for_sig(0, Transaction.SIG_HASH_ALL, redeem_script)
            msg_to_sign = bytes(Hash.dhash(bytes(txn_copy) + pack_u32(Transaction.SIG_HASH_ALL)))
            sig = Signature.from_der(payment_tx.inputs[0].script[0][:-1])
            if not redeem_script.customer_public_key.verify(msg_to_sign, sig, False):
                raise BadTransactionError('Invalid payment signature.')

            # Verify the length of the script is what we expect
            if len(payment_tx.inputs[0].script) != 3:
                raise BadTransactionError('Invalid payment channel transaction structure.')

            # Verify the script template is valid for accepting a merchant signature
            if (not Script.validate_template(payment_tx.inputs[0].script, [bytes, 'OP_1', bytes]) and
                    not Script.validate_template(payment_tx.inputs[0].script, [bytes, 'OP_TRUE', bytes])):
                raise BadTransactionError('Invalid payment channel transaction structure.')

            # Verify that the payment channel is ready
            if channel.state == ChannelSQLite3.CONFIRMING:
                raise ChannelClosedError('Payment channel not ready.')
            elif channel.state == ChannelSQLite3.CLOSED:
                raise ChannelClosedError('Payment channel closed.')

            # Verify that payment is made to the merchant's pubkey
            index = payment_tx.output_index_for_address(merch_pubkey.hash160())
            if index is None:
                raise BadTransactionError('Payment must pay to merchant pubkey.')

            # Verify that both payments are not below the dust limit
            if any(p.value < PaymentServer.DUST_LIMIT for p in payment_tx.outputs):
                raise BadTransactionError(
                    'Final payment must have outputs greater than {}.'.format(PaymentServer.DUST_LIMIT))

            # Validate that the payment is more than the last one
            new_pmt_amt = payment_tx.outputs[index].value
            if new_pmt_amt <= channel.last_payment_amount:
                raise BadTransactionError('Payment must be greater than 0.')

            # Verify that the transaction has adequate fees
            net_pmt_amount = sum([d.value for d in payment_tx.outputs])
            deposit_amount = channel.amount
            if deposit_amount < net_pmt_amount + PaymentServer.MIN_TX_FEE:
                raise BadTransactionError('Payment must have adequate fees.')

            # Update the current payment transaction
            self._db.pc.update_payment(deposit_txid, payment_tx, new_pmt_amt)
            self._db.pmt.create(deposit_txid, payment_tx, new_pmt_amt - channel.last_payment_amount)

            return str(payment_tx.hash)