<p>This question seems very similar to <a href="https://bitcoin.stackexchange.com/questions/74103/extract-lightning-network-funding-transactions-with-python-bitcoin-blockchain-pa">Extract lightning network funding transactions with python bitcoin blockchain parser lib</a> in which the answer was given that you cannot really scan the blockchain for lightning channels.</p>

<p>However there is more to add:</p>

<p>The situation becomes worse with 2p ecdsa or scriptless scripts via which you can theoretically open a channel and close it via P2PKH transaction. The same holds true when Bitcoin gets its soft fork for schnorr signatures. In that way those tx (or smart contacts) become indistinguishable from regular Bitcoin txs. </p>

<p>But : you can look at the gossip protocol of the lightning network for open channels. Assume you have a full history of open and closed channels (which would not include private channels) you could probably run some machine learning algorithms / classifier to create a probabilistic model. Assuming most channels are public you should have a relatively good training / test set. However as long as channels are not closed all tx are P2WSH tx which are not insistinguishable as mentioned by the above linked thread. So a machine learning model would only make sense (if at all) to detect former / closed channels. </p>

CC BY-SA 4.0
2019-05-08T15:17:53.000Z
Doesn't "some machine learning algorithms / classifier" work under the assumption that there is some hidden pattern in the data which we just don't see? Where does the pattern come from in this use case, when the data is some random keys and hashes?
Sergei Tikhomirov
CC BY-SA 4.0
2019-05-08T16:56:32.463Z
In the witness scripts that you see after the channel is closed. In there you would find a certain structure for the outputs, timelock and htlcs. On the other hand you could probably just look for such patterns right away without machine learning. Actually we could compare how many such txs we find with the number of previously closed lightning channels and see how accurate they would be. Also this would /should only work for force close and unilateral closes. The machine learning part was only suggested because you specifically asked for probabilisitic model. Wouldn't really recommend that.
Rene Pickhardt
CC BY-SA 4.0
2019-05-08T14:21:03.690Z
2019-05-08T14:21:03.690Z
Rene Pickhardt
<p>A lightning funding transaction is a P2WSH transaction, which appears on chain as paying to some random-looking script hash, but the <code>redeemScript</code> which can unlock and spend the money is not revealed on chain until the transaction gets spent (which is when the channel gets closed in Lightning's case).</p>

<p>You can recreate the <code>redeemScript</code> if you know the funding public keys used for it, which are broadcast over Lightning's gossip network as part of a <code>channel_announcement</code> message. If you take the two funding public keys, where the first key is the lexicographically lesser of the two, the <code>redeemScript</code> is simply:</p>

<pre><code>OP_2 &lt;pubKey1&gt; &lt;pubKey2&gt; OP_2 OP_CHECKMULTISIG
</code></pre>

<p>By hashing this and comparing it to the hash the unspent P2WSH transaction spends to, you can verify that the transaction is a Lightning channel.</p>

<p>But without knowledge of those funding public keys (private channels or non-lightning txs), you cannot know the content of the <code>redeemScript</code> until the transaction is spent.</p>

CC BY-SA 4.0
2019-05-08T14:19:00.967Z
2019-05-08T14:19:00.967Z
Mark H
<p>Is there a way to tell (probabilistically, maybe) by looking at an on-chain transaction that it is related to Lightning (is [likely to be] a channel opening or closure)?</p>

<lightning-network><ln-opening-channels><ln-closing-channels>
Distinguishing Lightning channels on-chain
Sergei Tikhomirov
# Distinguishing Lightning channels on-chain

(Asked by: username_0 on 2019-05-08)

Is there a way to tell (probabilistically, maybe) by looking at an on-chain transaction that it is related to Lightning (is [likely to be] a channel opening or closure)?

**Tags:** lightning-network, ln-opening-channels, ln-closing-channels


---

## 2 answers

### Answer by username_2 on 2019-05-08. Score: 3 | Accepted answer

A lightning funding transaction is a P2WSH transaction, which appears on chain as paying to some random-looking script hash, but the `redeemScript` which can unlock and spend the money is not revealed on chain until the transaction gets spent (which is when the channel gets closed in Lightning's case).

You can recreate the `redeemScript` if you know the funding public keys used for it, which are broadcast over Lightning's gossip network as part of a `channel_announcement` message. If you take the two funding public keys, where the first key is the lexicographically lesser of the two, the `redeemScript` is simply:

```
OP_2 <pubKey1> <pubKey2> OP_2 OP_CHECKMULTISIG

```

By hashing this and comparing it to the hash the unspent P2WSH transaction spends to, you can verify that the transaction is a Lightning channel.

But without knowledge of those funding public keys (private channels or non-lightning txs), you cannot know the content of the `redeemScript` until the transaction is spent.


---

### Answer by username_1 on 2019-05-08. Score: 0

This question seems very similar to [Extract lightning network funding transactions with python bitcoin blockchain parser lib](https://bitcoin.stackexchange.com/questions/74103/extract-lightning-network-funding-transactions-with-python-bitcoin-blockchain-pa) in which the answer was given that you cannot really scan the blockchain for lightning channels.

However there is more to add:

The situation becomes worse with 2p ecdsa or scriptless scripts via which you can theoretically open a channel and close it via P2PKH transaction. The same holds true when Bitcoin gets its soft fork for schnorr signatures. In that way those tx (or smart contacts) become indistinguishable from regular Bitcoin txs.

But : you can look at the gossip protocol of the lightning network for open channels. Assume you have a full history of open and closed channels (which would not include private channels) you could probably run some machine learning algorithms / classifier to create a probabilistic model. Assuming most channels are public you should have a relatively good training / test set. However as long as channels are not closed all tx are P2WSH tx which are not insistinguishable as mentioned by the above linked thread. So a machine learning model would only make sense (if at all) to detect former / closed channels.

> **Comments:**
> **username_0**: Doesn't "some machine learning algorithms / classifier" work under the assumption that there is some hidden pattern in the data which we just don't see? Where does the pattern come from in this use case, when the data is some random keys and hashes?  
> **username_1**: In the witness scripts that you see after the channel is closed. In there you would find a certain structure for the outputs, timelock and htlcs. On the other hand you could probably just look for such patterns right away without machine learning. Actually we could compare how many such txs we find with the number of previously closed lightning channels and see how accurate they would be. Also this would /should only work for force close and unilateral closes. The machine learning part was only suggested because you specifically asked for probabilisitic model. Wouldn't really recommend that.  

---

