def maybe_add_backup_from_tx(self, tx):
    funding_address = None
    node_id_prefix = None
    for (i, o) in enumerate(tx.outputs()):
        script_type = get_script_type_from_output_script(o.scriptpubkey)
        if (script_type == 'p2wsh'):
            funding_index = i
            funding_address = o.address
            for o2 in tx.outputs():
                if o2.scriptpubkey.startswith(bytes([opcodes.OP_RETURN])):
                    encrypted_data = o2.scriptpubkey[2:]
                    data = self.decrypt_cb_data(encrypted_data, funding_address)
                    if data.startswith(CB_MAGIC_BYTES):
                        node_id_prefix = data[4:]
    if (node_id_prefix is None):
        return
    funding_txid = tx.txid()
    cb_storage = OnchainChannelBackupStorage(node_id_prefix=node_id_prefix, funding_txid=funding_txid, funding_index=funding_index, funding_address=funding_address, is_initiator=True)
    channel_id = cb_storage.channel_id().hex()
    if (channel_id in self.db.get_dict('channels')):
        return
    self.logger.info(f'adding backup from tx')
    d = self.db.get_dict('onchain_channel_backups')
    d[channel_id] = cb_storage
    cb = ChannelBackup(cb_storage, sweep_address=self.sweep_address, lnworker=self)
    self.wallet.save_db()
    with self.lock:
        self._channel_backups[bfh(channel_id)] = cb
    util.trigger_callback('channels_updated', self.wallet)
    self.lnwatcher.add_channel(cb.funding_outpoint.to_str(), cb.get_funding_address())
def maybe_add_backup_from_tx(self, tx):
    funding_address = None
    node_id_prefix = None
    for (i, o) in enumerate(tx.outputs()):
        script_type = get_script_type_from_output_script(o.scriptpubkey)
        if (script_type == 'p2wsh'):
            funding_index = i
            funding_address = o.address
            for o2 in tx.outputs():
                if o2.scriptpubkey.startswith(bytes([opcodes.OP_RETURN])):
                    encrypted_data = o2.scriptpubkey[2:]
                    data = self.decrypt_cb_data(encrypted_data, funding_address)
                    if data.startswith(CB_MAGIC_BYTES):
                        node_id_prefix = data[4:]
    if (node_id_prefix is None):
        return
    funding_txid = tx.txid()
    cb_storage = OnchainChannelBackupStorage(node_id_prefix=node_id_prefix, funding_txid=funding_txid, funding_index=funding_index, funding_address=funding_address, is_initiator=True)
    channel_id = cb_storage.channel_id().hex()
    if (channel_id in self.db.get_dict('channels')):
        return
    self.logger.info(f'adding backup from tx')
    d = self.db.get_dict('onchain_channel_backups')
    d[channel_id] = cb_storage
    cb = ChannelBackup(cb_storage, sweep_address=self.sweep_address, lnworker=self)
    self.wallet.save_db()
    with self.lock:
        self._channel_backups[bfh(channel_id)] = cb
    util.trigger_callback('channels_updated', self.wallet)
    self.lnwatcher.add_channel(cb.funding_outpoint.to_str(), cb.get_funding_address())