Dissecting a P2PKH Bitcoin Transaction down to the last Byte
René Pickhardt René Pickhardt
6.12K subscribers
13,963 views
0

 Published On Sep 10, 2018

We aim to understand how bitcoin nodes validate a bitcoin transaction by concatenation of output and input scripts . Therefor we analyze the format of Bitcoin transaction. This is needed to prepare a future video about Revocable Sequence Maturity contracts which are need for the construction of payment channels in the lightning network.

I will appreciate the kindness if you leave a (small!) tip. BTC-address:
1KwjU4UknzbXh1rnP1jAKz9wwjcuYwe9AC
segwit: 38fWwbsxvVeBsJpH4bbHTBai8jT8RUa7DE

Leave an even more anonymous tip via the ⚡ Lightning ⚡ network (: Get the invoice with this curl statement:

curl -i -H "Accept: application/json" -d '{"amount":ENTER_AN_INTEGER_AMOUNT_OF_SATOSHIS_INSTEAD_OF_THIS}' https://ln.rene-pickhardt.de/invoice

Big thanks to Fulmo from Berlin for previous support of my work!

My web site: https://www.rene-pickhardt.de and Twitter:   / renepickhardt   as well as my lightning node: https://ln.rene-pickhardt.de


Useful links and tools:
* https://en.bitcoin.it/wiki/Transaction
* https://wschae.github.io/build/editor... (tool for executing bitcoin scripts)
* http://lenschulwitz.com/base58
* https://en.bitcoin.it/wiki/Base58Chec...

Evaluated Bitcoin transaction:
https://www.blockchain.com/btc/tx/45b...

Version: 01000000
Input counter: 01 ⇨ 1 input script
TxOutHash: 2ad07447509a4fa9f3b45d3bde74b774b46b569dfa1d0728120c355b7b26312f
Hash of the spent transaction (we need this later)
2a d0 74 47 50 9a 4f a9 f3 b4 5d 3b de 74 b7 74 b4 6b 56 9d fa 1d 07 28 12 0c 35 5b 7b 26 31 2f
2f31267b5b350c1228071dfa9d566bb474b774de3b5db4f3a94f9a504774d02a (this transaction was being spent)

TxOut index: 00000000 (first output in the output of previous tx)
Script length: 6a ⇨ 6*16 + 10 = 106 Bytes ⇨ 212 hex digits

Input script:473044022100edf664ebfc12a3998d242fbdd8a1a44c13e4e7f6d7dbddde9ae5610bfd2c9c38021f74ec1e2eca7310baa17c60cbaaffa0503cee680f13fd9a10fd04f2910958c9012103486669962008e0713660b6d69117a65fcecd221d06c1e5077b4d9cd477c0cf98
Sequence: feffffff

Dissect the input
Length for data: 47 ⇨ hex2dec: 71 Bytes ⇨ 142 chars:
signature: 3044022100edf664ebfc12a3998d242fbdd8a1a44c13e4e7f6d7dbddde9ae5610bfd2c9c38021f74ec1e2eca7310baa17c60cbaaffa0503cee680f13fd9a10fd04f2910958c901
Length for pub key: 21 ⇨ hex2dec: 33 Bytes ⇨ 66 chars
pubkey: 03486669962008e0713660b6d69117a65fcecd221d06c1e5077b4d9cd477c0cf98

Check the bitcoin address from the input:
RIPMED160(SHA256(pubkey)): CB226B9CC60C613DDDCB188E984F631764CEF143
Extend with version: 00CB226B9CC60C613DDDCB188E984F631764CEF143

Calculate checksum:
SHA256(SHA256(extended Pubkey)): CE281C015A7230F46041D2DD640A6714725C25A0ED674D5E215318AA206A397F
Checksum = first 4 Bytes: CE281C01
Address in hex: 00CB226B9CC60C613DDDCB188E984F631764CEF143CE281C01

Encode to base58: 1KwjU4UknzbXh1rnP1jAKz9wwjcuYwe9AC

Output:
02102700000000000017a9144c8045932c5a4e57316e6e9c465fc1fba32d6ac38768380000000000001976a9144f6d76f02a5553617ec71820e9036afdcbf7d93c88ac693b0800

Do the same over again but only for the outputs:
01000000
01
911d280701fbaa08cd97646f5c3483284b4f37afeff3bf71f2b580063399bed5
2a000000
6a47304402202eada36986b14698473e34ace8a830725c39c5d3b2d8c5ccc39562758cd2c15c02205574c0d0b4a472ffee99696f2ead63e4c3a3ef9a35c9f71268542849e1721b0d0121031f64ef992820346838522cfa0babafc2638540ed58f9521e70ec0aa66c3bd709
feffffff

Spent Output: (at the time of the tx of course unspent):
#outputs: 02 ⇨ 2 Outputs:
a861000000000000 ⇨ endian 61a8 ⇨ hex2dec: 25000 (Satoshi)
Length: 19 ⇨ hex2dec = 25 Bytes ⇨ 50 hex digits:
76 ⇨ OP_DUP
a9 ⇨ OP_HASH160
Length: 14 ⇨ hex2dec = 20 Bytes ⇨ 40 digits:
Pubkey Hash: cfcc2ce2d8fc82b2b0837b6c1775d5edcffc107f
88 ⇨ OP_EQUALVERIFY
ac ⇨ OP_CHECKSIG

50103b0000000000 ⇨ 3b1050 ⇨ hex2dec = 3870800 (change!)
Length: 19
76 ⇨ OP_DUP
a9 ⇨ OP_HASH160
Length: 14
Data: Pubkey Hash: 3828a52a8d8310c63581ab1fcbf418bffaf23e3a (change address pubkey)
88 ⇨ OP_EQUALVERIFY
ac ⇨ OP_CHECKSIG
Timelock: 91250800

final script which needs to be true so that the Transaction can be minded:
3044022100edf664ebfc12a3998d242fbdd8a1a44c13e4e7f6d7dbddde9ae5610bfd2c9c38021f74ec1e2eca7310baa17c60cbaaffa0503cee680f13fd9a10fd04f2910958c901
03486669962008e0713660b6d69117a65fcecd221d06c1e5077b4d9cd477c0cf98
OP_DUP
OP_HASH160
cfcc2ce2d8fc82b2b0837b6c1775d5edcffc107f
OP_EQUALVERIFY
OP_CHECKSIG

This script needs to evaluate true! Enter it to: https://wschae.github.io/build/editor...

show more

Share/Embed