<p>Firstly, you are off by two bytes. The output script is <code>0020982b5322310807d368d1dc261143095dc7d02fba6d6708bb7621519f312908b2</code>, but you have selected <code>00220020982b5322310807d368d1dc261143095dc7d02fba6d6708bb7621519f312908b2</code>.</p>

<p>Secondly, complex output scripts are never just put in the output. They are always wrapped in some form of Pay to Script Hash. In this case, because it is segwit, it's Pay to Witness Script Hash (P2WSH). So you can't know the actual script used without the output being spent from. Everything in LN is P2WSH, no scripts are just put in the outputs.</p>

<p>To break down this script, first you have <code>00</code>. This is the witness version number, version 0. Then you have <code>20</code> which is a length indicator for the next chunk of data. In this case, that is 32 bytes. Lastly <code>982b5322310807d368d1dc261143095dc7d02fba6d6708bb7621519f312908b2</code> is the SHA256 hash of the script itself. That script will be a 2-of-2 multisig, but you won't be able to know what it actually is until this output is spent from.</p>

CC BY-SA 3.0
2018-04-18T15:28:01.767Z
just to make sure I get this right: as long as the channel is not closed (forcefully or collaboratively) meaning the commitment transaction is not published it will be impossible to know more information about the payment channel? This would mean I can't parse the blockchain for open lightning channels since the SHA256 could be the SHA of any complex script and not only that of the funding transaction?
Rene Pickhardt
CC BY-SA 3.0
2018-04-18T15:30:44.707Z
Yes, that is correct.
Ava Chow
CC BY-SA 3.0
2018-04-18T15:17:07.727Z
2018-04-18T15:17:07.727Z
Ava Chow
<p>My ultimate goal is to parse funding transactions (as well as channel closes in a later step) from the blockchain. In order to do so I wanted to start with a funding transaction and disect it. Unfortunately I am kind of stuck and hope you can help out. </p>

<p>via <a href="https://lnmainnet.gaben.win/" rel="nofollow noreferrer">#recksplorer</a> I clicked on an edge and found <a href="https://www.smartbit.com.au/tx/d4dc9f75283e049b27edd71e8e1368110121408747d0a9ad3e97991a0dc04cb7" rel="nofollow noreferrer">this transaction</a> to be the funding transaction of a payment channel.  I found this transaction in my local copy of the blockchain using <a href="https://github.com/alecalve/python-bitcoin-blockchain-parser" rel="nofollow noreferrer">bitcoin-blockchain-parser (in python)</a> lib</p>

<p>using this code</p>

<pre><code>for i,h in enumerate(tx.hex):
    print('{:02x}'.format(h),end=" ")
    if i%8==7:
        print(" ",end="")
    if i%16==15:
       print()
print()
</code></pre>

<p>I get the following hex representation of my transaction (the two asterix ** where included to emphasize the output script of interest):</p>

<pre><code>02 00 00 00 00 01 01 e3  3d f6 39 1a 02 5c 79 b3  
1a 2a ab 94 9f b5 6d f8  f9 2d 9f 3a bb ab 7d d3  
92 47 7f 31 9f b6 69 01  00 00 00 00 ff ff ff ff  
02 21 4e 00 00 00 00 00  00 22 **00 20 98 2b 53 22  
31 08 07 d3 68 d1 dc 26  11 43 09 5d c7 d0 2f ba  
6d 67 08 bb 76 21 51 9f  31 29 08 b2** e1 99 0d 00  
00 00 00 00 16 00 14 8c  35 c6 98 4d f9 d6 a2 9e  
4e 7d 76 21 19 3c fe 53  82 ce 60 02 48 30 45 02  
21 00 ba 9e 6e 56 a6 4f  a2 53 4a 02 e9 30 20 8e  
80 38 f6 f0 7c a8 d2 0e  b3 e4 03 59 99 20 8f 9d  
a1 15 02 20 3a 12 73 72  c4 bc ee 6f 76 9e 07 fd  
05 0e f0 62 9b 4d e8 e6  43 84 26 46 53 56 03 46  
07 f3 9a eb 01 21 03 0c  fa b1 4c d0 4a 01 5a 58  
40 2a 5e a6 84 e4 d9 b3  4b 33 23 73 04 0c 57 5e  
83 b1 0a d3 90 79 66 00  00 00 00 
</code></pre>

<p>I also found the two output scripts in particular the first one (after byte 58!) which sends 20001 satoshi to a payment channel: </p>

<pre><code>output = tx.outputs[0]
for x in output.script.hex:
    print('{:02x}'.format(h),end=" ")
</code></pre>

<p>00 20 98 2b 53 22 31 08 07 d3 68 d1 dc 26 11 43 09 5d c7 d0 2f ba 6d 67 08 bb 76 21 51 9f 31 29 08 b2</p>

<p>I have not been able to understand the format of the funding transaction itself. according to <a href="https://github.com/lightningnetwork/lightning-rfc/blob/master/03-transactions.md#funding-transaction-output" rel="nofollow noreferrer">BOLT3 it should look like this</a>: </p>

<blockquote>
  <p>2 &lt; pubkey1 > &lt; pubkey2 > 2 OP_CHECKMULTISIG</p>
</blockquote>

<p>I asusme that 2 means OP_2. However with the help of <a href="https://en.bitcoin.it/wiki/Script#Opcodes" rel="nofollow noreferrer">bitcoin wiki</a> I cannot find the hex values of OP_2 and OP_CHECKMULTISIG.</p>

<p>I guess (but can't verify this at the moment) that I am running into problems similar to <a href="https://bitcoin.stackexchange.com/questions/42521/pay-to-script-hash-execution">this question here on this site</a>. Or I am messing up with the endian notations? As mentioned my future goal would be to automatically extract all funding transactions therefor I need to be able to pursue my endeavor and would appreciate your help a lot! Thanks!</p>

<segregated-witness><script><multi-signature><lightning-network><python>
Extract lightning network funding transactions with python bitcoin blockchain parser lib
Rene Pickhardt
# Extract lightning network funding transactions with python bitcoin blockchain parser lib

(Asked by: username_0 on 2018-04-18)

My ultimate goal is to parse funding transactions (as well as channel closes in a later step) from the blockchain. In order to do so I wanted to start with a funding transaction and disect it. Unfortunately I am kind of stuck and hope you can help out.

via [#recksplorer](https://lnmainnet.gaben.win/) I clicked on an edge and found [this transaction](https://www.smartbit.com.au/tx/d4dc9f75283e049b27edd71e8e1368110121408747d0a9ad3e97991a0dc04cb7) to be the funding transaction of a payment channel. I found this transaction in my local copy of the blockchain using [bitcoin-blockchain-parser (in python)](https://github.com/alecalve/python-bitcoin-blockchain-parser) lib

using this code

```
for i,h in enumerate(tx.hex):
    print('{:02x}'.format(h),end=" ")
    if i%8==7:
        print(" ",end="")
    if i%16==15:
       print()
print()

```

I get the following hex representation of my transaction (the two asterix \*\* where included to emphasize the output script of interest):

```
02 00 00 00 00 01 01 e3  3d f6 39 1a 02 5c 79 b3  
1a 2a ab 94 9f b5 6d f8  f9 2d 9f 3a bb ab 7d d3  
92 47 7f 31 9f b6 69 01  00 00 00 00 ff ff ff ff  
02 21 4e 00 00 00 00 00  00 22 **00 20 98 2b 53 22  
31 08 07 d3 68 d1 dc 26  11 43 09 5d c7 d0 2f ba  
6d 67 08 bb 76 21 51 9f  31 29 08 b2** e1 99 0d 00  
00 00 00 00 16 00 14 8c  35 c6 98 4d f9 d6 a2 9e  
4e 7d 76 21 19 3c fe 53  82 ce 60 02 48 30 45 02  
21 00 ba 9e 6e 56 a6 4f  a2 53 4a 02 e9 30 20 8e  
80 38 f6 f0 7c a8 d2 0e  b3 e4 03 59 99 20 8f 9d  
a1 15 02 20 3a 12 73 72  c4 bc ee 6f 76 9e 07 fd  
05 0e f0 62 9b 4d e8 e6  43 84 26 46 53 56 03 46  
07 f3 9a eb 01 21 03 0c  fa b1 4c d0 4a 01 5a 58  
40 2a 5e a6 84 e4 d9 b3  4b 33 23 73 04 0c 57 5e  
83 b1 0a d3 90 79 66 00  00 00 00 

```

I also found the two output scripts in particular the first one (after byte 58!) which sends 20001 satoshi to a payment channel:

```
output = tx.outputs[0]
for x in output.script.hex:
    print('{:02x}'.format(h),end=" ")

```

00 20 98 2b 53 22 31 08 07 d3 68 d1 dc 26 11 43 09 5d c7 d0 2f ba 6d 67 08 bb 76 21 51 9f 31 29 08 b2

I have not been able to understand the format of the funding transaction itself. according to [BOLT3 it should look like this](https://github.com/lightningnetwork/lightning-rfc/blob/master/03-transactions.md#funding-transaction-output):

> 2 < pubkey1 > < pubkey2 > 2 OP\_CHECKMULTISIG

I asusme that 2 means OP\_2. However with the help of [bitcoin wiki](https://en.bitcoin.it/wiki/Script#Opcodes) I cannot find the hex values of OP\_2 and OP\_CHECKMULTISIG.

I guess (but can't verify this at the moment) that I am running into problems similar to [this question here on this site](https://bitcoin.stackexchange.com/questions/42521/pay-to-script-hash-execution). Or I am messing up with the endian notations? As mentioned my future goal would be to automatically extract all funding transactions therefor I need to be able to pursue my endeavor and would appreciate your help a lot! Thanks!

**Tags:** segregated-witness, script, multi-signature, lightning-network, python


---

## 1 answers

### Answer by username_1 on 2018-04-18. Score: 3 | Accepted answer

Firstly, you are off by two bytes. The output script is `0020982b5322310807d368d1dc261143095dc7d02fba6d6708bb7621519f312908b2`, but you have selected `00220020982b5322310807d368d1dc261143095dc7d02fba6d6708bb7621519f312908b2`.

Secondly, complex output scripts are never just put in the output. They are always wrapped in some form of Pay to Script Hash. In this case, because it is segwit, it's Pay to Witness Script Hash (P2WSH). So you can't know the actual script used without the output being spent from. Everything in LN is P2WSH, no scripts are just put in the outputs.

To break down this script, first you have `00`. This is the witness version number, version 0. Then you have `20` which is a length indicator for the next chunk of data. In this case, that is 32 bytes. Lastly `982b5322310807d368d1dc261143095dc7d02fba6d6708bb7621519f312908b2` is the SHA256 hash of the script itself. That script will be a 2-of-2 multisig, but you won't be able to know what it actually is until this output is spent from.

> **Comments:**
> **username_0**: just to make sure I get this right: as long as the channel is not closed (forcefully or collaboratively) meaning the commitment transaction is not published it will be impossible to know more information about the payment channel? This would mean I can't parse the blockchain for open lightning channels since the SHA256 could be the SHA of any complex script and not only that of the funding transaction?  
> **username_1**: Yes, that is correct.  

---

