Ejemplo n.º 1
0
 def on_tx_received(self, nick, txhex, offerinfo):
     try:
         tx = btc.deserialize(txhex)
     except IndexError as e:
         return (False, 'malformed txhex. ' + repr(e))
     jlog.info('obtained tx\n' + pprint.pformat(tx))
     goodtx, errmsg = self.verify_unsigned_tx(tx, offerinfo)
     if not goodtx:
         jlog.info('not a good tx, reason=' + errmsg)
         return (False, errmsg)
     jlog.info('goodtx')
     sigs = []
     utxos = offerinfo["utxos"]
     for index, ins in enumerate(tx['ins']):
         utxo = ins['outpoint']['hash'] + ':' + str(
             ins['outpoint']['index'])
         if utxo not in utxos.keys():
             continue
         addr = utxos[utxo]['address']
         amount = utxos[utxo]["value"]
         txs = self.wallet.sign(txhex,
                                index,
                                self.wallet.get_key_from_addr(addr),
                                amount=amount)
         sigmsg = btc.deserialize(txs)["ins"][index]["script"].decode("hex")
         if "txinwitness" in btc.deserialize(txs)["ins"][index].keys():
             #We prepend the witness data since we want (sig, pub, scriptCode);
             #also, the items in witness are not serialize_script-ed.
             sigmsg = "".join([
                 btc.serialize_script_unit(x.decode("hex"))
                 for x in btc.deserialize(txs)["ins"][index]["txinwitness"]
             ]) + sigmsg
         sigs.append(base64.b64encode(sigmsg))
     return (True, sigs)
Ejemplo n.º 2
0
    def on_tx_received(self, nick, txhex, offerinfo):
        """Called when the counterparty has sent an unsigned
        transaction. Sigs are created and returned if and only
        if the transaction passes verification checks (see
        verify_unsigned_tx()).
        """
        try:
            tx = btc.deserialize(txhex)
        except (IndexError, SerializationError,
                SerializationTruncationError) as e:
            return (False, 'malformed txhex. ' + repr(e))
        jlog.info('obtained tx\n' + pprint.pformat(tx))
        goodtx, errmsg = self.verify_unsigned_tx(tx, offerinfo)
        if not goodtx:
            jlog.info('not a good tx, reason=' + errmsg)
            return (False, errmsg)
        jlog.info('goodtx')
        sigs = []
        utxos = offerinfo["utxos"]

        our_inputs = {}
        for index, ins in enumerate(tx['ins']):
            utxo = ins['outpoint']['hash'] + ':' + str(
                ins['outpoint']['index'])
            if utxo not in utxos:
                continue
            script = self.wallet.addr_to_script(utxos[utxo]['address'])
            amount = utxos[utxo]['value']
            our_inputs[index] = (script, amount)

        txs = self.wallet.sign_tx(btc.deserialize(unhexlify(txhex)),
                                  our_inputs)

        for index in our_inputs:
            sigmsg = txs['ins'][index]['script']
            if 'txinwitness' in txs['ins'][index]:
                #We prepend the witness data since we want (sig, pub, scriptCode);
                #also, the items in witness are not serialize_script-ed.
                sigmsg = b''.join(
                    btc.serialize_script_unit(x)
                    for x in txs['ins'][index]['txinwitness']) + sigmsg
            sigs.append(base64.b64encode(sigmsg))
        return (True, sigs)