Ejemplo n.º 1
0
    def tx_outputs(self, derivation, tx, segwit=False):
        outputs = []
        has_change = False

        for _type, address, amount in tx.outputs():
            info = tx.output_info.get(address)
            if info is not None and not has_change:
                has_change = True  # no more than one change address
                addrtype, hash_160 = b58_address_to_hash160(address)
                index, xpubs, m = info
                if len(xpubs) == 1:
                    script_type = self.types.PAYTOP2SHWITNESS if segwit else self.types.PAYTOADDRESS
                    address_n = self.client_class.expand_path(derivation +
                                                              "/%d/%d" % index)
                    txoutputtype = self.types.TxOutputType(
                        amount=amount,
                        script_type=script_type,
                        address_n=address_n,
                    )
                else:
                    script_type = self.types.PAYTOP2SHWITNESS if segwit else self.types.PAYTOMULTISIG
                    address_n = self.client_class.expand_path("/%d/%d" % index)
                    nodes = map(self.ckd_public.deserialize, xpubs)
                    pubkeys = [
                        self.types.HDNodePathType(node=node,
                                                  address_n=address_n)
                        for node in nodes
                    ]
                    multisig = self.types.MultisigRedeemScriptType(
                        pubkeys=pubkeys, signatures=[b''] * len(pubkeys), m=m)
                    txoutputtype = self.types.TxOutputType(
                        multisig=multisig,
                        amount=amount,
                        address_n=self.client_class.expand_path(derivation +
                                                                "/%d/%d" %
                                                                index),
                        script_type=script_type)
            else:
                txoutputtype = self.types.TxOutputType()
                txoutputtype.amount = amount
                if _type == TYPE_SCRIPT:
                    txoutputtype.script_type = self.types.PAYTOOPRETURN
                    txoutputtype.op_return_data = address[2:]
                elif _type == TYPE_ADDRESS:
                    if is_segwit_address(address):
                        txoutputtype.script_type = self.types.PAYTOWITNESS
                    else:
                        addrtype, hash_160 = b58_address_to_hash160(address)
                        if addrtype == NetworkConstants.ADDRTYPE_P2PKH:
                            txoutputtype.script_type = self.types.PAYTOADDRESS
                        elif addrtype == NetworkConstants.ADDRTYPE_P2SH:
                            txoutputtype.script_type = self.types.PAYTOSCRIPTHASH
                        else:
                            raise BaseException('addrtype: ' + str(addrtype))
                    txoutputtype.address = address

            outputs.append(txoutputtype)

        return outputs
Ejemplo n.º 2
0
 def create_output_by_address():
     txoutputtype = self.types.TxOutputType()
     txoutputtype.amount = amount
     if _type == TYPE_SCRIPT:
         txoutputtype.script_type = self.types.PAYTOOPRETURN
         txoutputtype.op_return_data = address[2:]
     elif _type == TYPE_ADDRESS:
         if is_segwit_address(address):
             txoutputtype.script_type = self.types.PAYTOWITNESS
         else:
             addrtype, hash_160 = b58_address_to_hash160(address)
             if addrtype == constants.net.ADDRTYPE_P2PKH:
                 txoutputtype.script_type = self.types.PAYTOADDRESS
             elif addrtype == constants.net.ADDRTYPE_P2SH:
                 txoutputtype.script_type = self.types.PAYTOSCRIPTHASH
             else:
                 raise Exception('addrtype: ' + str(addrtype))
         txoutputtype.address = address
     return txoutputtype