This document summarizes elements of the Bitcoin protocol for developers. It describes the blockchain network protocol, how transactions are structured with inputs and outputs, and how Bitcoin scripting works to lock and unlock transactions based on signatures and public keys. Bitcoin scripting uses a stack-based approach to evaluate transactions in a non-Turing complete manner. Examples are provided of common script patterns and a more complex script for an odd/even betting contract.
Exploring the Future Potential of AI-Enabled Smartphone Processors
Bitcoin protocol for developers at techfest
1. Alberto Gómez Toribio ( @gotoalberto )
Bitcoin protocol for developers
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by-nc-sa/3.0/
c_b_n_a
Leganés
6 Febrero 2013
2. Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by-nc-sa/3.0/
c_b_n_a
Leganés
6 Febrero 2013
Alberto Gómez @gotoalberto
Alvaro Polo @apolovald
“Software development enthusiast and aviation geek. Give me a
higher-order function and I shall move the world.”
“Vocational coder since childhood, he will annoy you with concepts
like "monad" and "actor model" but gets the work done.”
“Theoretical computer science believer by night, pragmatic ship-it developer by day.
My brain runs on glucose, caffeine, general abstract nonsense and type theory.”
“OpenData and Bitcoin Developer on fire.
Great coders are today’s rock stars. That’s it!”
Coinffeine.com
Sebastián Ortega @_sortega
Ximo Guanter
A P2P Bitcoin exchange
3. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
What is the Bitcoin Protocol?
● Network protocol
● Transactions Mechanics
○ Blockchain mining
○ Fees and commitment rules
○ Sharing protocol: Gossip, Bittorrent, Gnutella...
○ Validations of the money source
○ Protocol to spend the money
○ Bitcoin Scripting
4. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
Network protocol
● Blockchain
○ The Blockchain is a distributed ledger book
○ Create a Block in chain require a proof of work
dc7047be… = SHA256(new_block_hash)
○ Forks are discarded, the most complex chain wins.
5. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
Network protocol
● Other services:
○ The Blockchain is used to storage
usernames.
○ Tweets are shared between users using a DHT protocol.
○ The older messages are discarded.
○ Incentive to generate blocks on Blockchain:
Sponsored messages.
6. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
Network protocol
● Colored Coins:
○ The Blockchain is used to storage key-value strings,
as internet web domains / IP.
○ You can register a domain spending Namecoins.
○ The fee by register a name is decreased in time.
17. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
Bitcoin Script
● It Allows to define how an output will be spent
and by whom.
● Is a non Turing-Complete language which is
evaluated as a Stack, from left to right.
● It just allows to write pure functions
(without context)
● Is a non Turing-Complete language which is
evaluated with a stack machine.
18. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
How Bitcoin Script works?
OP_DUP OP_HASH160 cb1f48… OP_EQUALVERIFY OP_CHECKSIG
(Public Key Hash)
19. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
How Bitcoin Script works?
OP_DUP OP_HASH160 cb1f48… OP_EQUALVERIFY OP_CHECKSIGbbba3f5… 172D5w7C…
(Public Key)(Signature) (Public Key Hash)
20. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
How Bitcoin Script works?
Stack
OP_DUP OP_HASH160 cb1f48… OP_EQUALVERIFY OP_CHECKSIGbbba3f5… 172D5w7C…
(Public Key)(Signature) (Public Key Hash)
21. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
How Bitcoin Script works?
Stack
OP_DUP OP_HASH160 cb1f48… OP_EQUALVERIFY OP_CHECKSIGbbba3f5… 172D5w7C…
(Public Key)(Signature) (Public Key Hash)
22. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
How Bitcoin Script works?
Stack
bbba3f5…(Signature)
OP_DUP OP_HASH160 cb1f48… OP_EQUALVERIFY OP_CHECKSIG172D5w7C…
(Public Key) (Public Key Hash)
23. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
How Bitcoin Script works?
Stack
(Signature) bbba3f5…
OP_DUP OP_HASH160 cb1f48… OP_EQUALVERIFY OP_CHECKSIG172D5w7C…
(Public Key) (Public Key Hash)
24. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
How Bitcoin Script works?
Stack
bbba3f5…
172D5w7C…
(Signature)
(Public Key)
OP_DUP OP_HASH160 cb1f48… OP_EQUALVERIFY OP_CHECKSIG
(Public Key Hash)
25. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
How Bitcoin Script works?
Stack
bbba3f5…
172D5w7C…
(Signature)
(Public Key)
OP_DUP OP_HASH160 cb1f48… OP_EQUALVERIFY OP_CHECKSIG
(Public Key Hash)
26. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
How Bitcoin Script works?
Stack
bbba3f5…
172D5w7C…(Public Key)
(Signature)
172D5w7C…(Public Key)
OP_HASH160 cb1f48… OP_EQUALVERIFY OP_CHECKSIG
(Public Key Hash)
27. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
How Bitcoin Script works?
Stack
bbba3f5…
172D5w7C…(Public Key)
(Signature)
172D5w7C…(Public Key)
OP_HASH160 cb1f48… OP_EQUALVERIFY OP_CHECKSIG
(Public Key Hash)
28. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
How Bitcoin Script works?
Stack
bbba3f5…
172D5w7C…(Public Key)
(Signature)
cb1f48…(Public Key Hash)
cb1f48… OP_EQUALVERIFY OP_CHECKSIG
(Public Key Hash)
29. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
How Bitcoin Script works?
Stack
bbba3f5…
172D5w7C…(Public Key)
(Signature)
cb1f48…(Public Key Hash)
cb1f48… OP_EQUALVERIFY OP_CHECKSIG
(Public Key Hash)
30. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
How Bitcoin Script works?
Stack
bbba3f5…
172D5w7C…(Public Key)
(Signature)
cb1f48…(Public Key Hash)
cb1f48…(Public Key Hash)
OP_EQUALVERIFY OP_CHECKSIG
31. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
How Bitcoin Script works?
Stack
bbba3f5…
172D5w7C…(Public Key)
(Signature)
cb1f48…(Public Key Hash)
cb1f48…(Public Key Hash)
OP_EQUALVERIFY OP_CHECKSIG
32. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
How Bitcoin Script works?
Stack
bbba3f5…
172D5w7C…(Public Key)
(Signature)
OP_CHECKSIG
33. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
How Bitcoin Script works?
Stack
bbba3f5…
172D5w7C…(Public Key)
(Signature)
OP_CHECKSIG
34. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
How Bitcoin Script works?
Stack
(EMPTY)
(EMPTY)
SUCCESS!
45. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
The sky is the limit! (Odd/Even Bet)
Stack
<5aa>
<7bb>
<PubKeyB>
<7bb>
OP_SWAP
OP_HASH160 <bHash> OP_EQUALVERIFY <1> OP_LEFT OP_SWAP <1> OP_LEFT OP_ADD <2>
OP_SWAP OP_MOD
OP_IF
OP_DUP OP_HASH160 <pubKeyAHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ELSE
OP_DUP OP_HASH160 <pubKeyBHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ENDIF
<Sig>
46. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
The sky is the limit! (Odd/Even Bet)
Stack
OP_HASH160 <bHash> OP_EQUALVERIFY <1> OP_LEFT OP_SWAP <1> OP_LEFT OP_ADD <2>
OP_SWAP OP_MOD
OP_IF
OP_DUP OP_HASH160 <pubKeyAHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ELSE
OP_DUP OP_HASH160 <pubKeyBHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ENDIF
<7bb>
<5aa>
<PubKeyB>
<7bb>
<Sig>
47. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
The sky is the limit! (Odd/Even Bet)
Stack
<bHash> OP_EQUALVERIFY <1> OP_LEFT OP_SWAP <1> OP_LEFT OP_ADD <2>
OP_SWAP OP_MOD
OP_IF
OP_DUP OP_HASH160 <pubKeyAHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ELSE
OP_DUP OP_HASH160 <pubKeyBHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ENDIF
<bHash>
<5aa>
<PubKeyB>
<7bb>
<Sig>
48. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
The sky is the limit! (Odd/Even Bet)
Stack
OP_EQUALVERIFY <1> OP_LEFT OP_SWAP <1> OP_LEFT OP_ADD <2>
OP_SWAP OP_MOD
OP_IF
OP_DUP OP_HASH160 <pubKeyAHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ELSE
OP_DUP OP_HASH160 <pubKeyBHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ENDIF
<bHash>
<5aa>
<PubKeyB>
<7bb>
<bHash>
<Sig>
49. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
The sky is the limit! (Odd/Even Bet)
Stack
<1> OP_LEFT OP_SWAP <1> OP_LEFT OP_ADD <2>
OP_SWAP OP_MOD
OP_IF
OP_DUP OP_HASH160 <pubKeyAHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ELSE
OP_DUP OP_HASH160 <pubKeyBHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ENDIF
<5aa>
<PubKeyB>
<7bb>
<Sig>
50. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
The sky is the limit! (Odd/Even Bet)
Stack
OP_LEFT OP_SWAP <1> OP_LEFT OP_ADD <2>
OP_SWAP OP_MOD
OP_IF
OP_DUP OP_HASH160 <pubKeyAHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ELSE
OP_DUP OP_HASH160 <pubKeyBHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ENDIF
<5aa>
<PubKeyB>
<7bb>
<Sig>
<1>
51. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
The sky is the limit! (Odd/Even Bet)
Stack
OP_SWAP <1> OP_LEFT OP_ADD <2>
OP_SWAP OP_MOD
OP_IF
OP_DUP OP_HASH160 <pubKeyAHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ELSE
OP_DUP OP_HASH160 <pubKeyBHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ENDIF
<5>
<PubKeyB>
<7bb>
<Sig>
52. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
The sky is the limit! (Odd/Even Bet)
Stack
<1> OP_LEFT OP_ADD <2>
OP_SWAP OP_MOD
OP_IF
OP_DUP OP_HASH160 <pubKeyAHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ELSE
OP_DUP OP_HASH160 <pubKeyBHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ENDIF
<7bb>
<PubKeyB>
<5>
<Sig>
53. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
The sky is the limit! (Odd/Even Bet)
Stack
OP_LEFT OP_ADD <2>
OP_SWAP OP_MOD
OP_IF
OP_DUP OP_HASH160 <pubKeyAHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ELSE
OP_DUP OP_HASH160 <pubKeyBHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ENDIF
<7bb>
<PubKeyB>
<5>
<Sig>
<1>
54. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
The sky is the limit! (Odd/Even Bet)
Stack
OP_ADD <2>
OP_SWAP OP_MOD
OP_IF
OP_DUP OP_HASH160 <pubKeyAHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ELSE
OP_DUP OP_HASH160 <pubKeyBHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ENDIF
<7>
<PubKeyB>
<5>
<Sig>
55. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
The sky is the limit! (Odd/Even Bet)
Stack
<PubKeyB>
<12>
<Sig>
<2>
OP_SWAP OP_MOD
OP_IF
OP_DUP OP_HASH160 <pubKeyAHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ELSE
OP_DUP OP_HASH160 <pubKeyBHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ENDIF
56. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
The sky is the limit! (Odd/Even Bet)
Stack
<PubKeyB>
<2>
<12>
<Sig>
OP_SWAP OP_MOD
OP_IF
OP_DUP OP_HASH160 <pubKeyAHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ELSE
OP_DUP OP_HASH160 <pubKeyBHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ENDIF
57. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
The sky is the limit! (Odd/Even Bet)
Stack
<PubKeyB>
<12>
<2>
<Sig>
OP_MOD
OP_IF
OP_DUP OP_HASH160 <pubKeyAHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ELSE
OP_DUP OP_HASH160 <pubKeyBHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ENDIF
58. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
The sky is the limit! (Odd/Even Bet)
Stack
<PubKeyB>
<0>
<Sig>
OP_IF
OP_DUP OP_HASH160 <pubKeyAHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ELSE
OP_DUP OP_HASH160 <pubKeyBHash> OP_EQUALVERIFY OP_CHECKSIG
OP_ENDIF
59. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
The sky is the limit! (Odd/Even Bet)
Stack
<PubKeyB>
<Sig>
(Standard Script)
OP_DUP OP_HASH160 <pubKeyBHash> OP_EQUALVERIFY OP_CHECKSIG
60. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
The sky is the limit! (Odd/Even Bet)
● Possible real use:
○ Random user to pay the transaction fees.
○ Help to define asymmetric scenario (secrets)
61. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
Context: Oracles
● The oracle contract allow define how the money
is spent including external state.
○ Allow to pay, only if an external condition is true, for
example the result of a search in google, or the API SEUR
response.
○ Allows to make reversible transactions.
62. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
Context: Oracles
● Example of a reversible payment using a Oracle
○ First you generate (in private) a multisig transaction as
this:
TX1
in { 1 BTC BOB }
out { 1 BTC MULTISIGVERIFY BOB SAM }
○ Then you must obtain signed from counterpart:
<SeurAPI(trackID) == ‘DELIVERED’>
OP_HASH160 <ExternalScriptHash> OP_EQUALSVERIFY
<OraclePubKey> <BobPubKey> 2 OP_CHECKMULTISIGVERIFY }
TX3
in { TX1[0] }
out { 1 BTC
<SeurAPI(trackID) == ‘RETURNED’>
OP_HASH160 <ExternalScriptHash> OP_EQUALSVERIFY
<OraclePubKey> <SamPubKey> 2 OP_CHECKMULTISIGVERIFY }
TX2
in { TX1[0] }
out { 1 BTC
63. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
Context: Oracles
● Execution
<SeurAPI(trackID) == ‘RETURNED’>
OP_HASH160 <ExternalScriptHash>
OP_EQUALSVERIFY
<OraclePubKey> <SamPubKey> 2
OP_CHECKMULTISIGVERIFY }
TX2
in { TX1[0] }
out { 1 BTC
71. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
Context: Oracles
● Execution
Stack
SUCCESS!
72. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
Fees? Micro Payment Channels!
● Transactions from a checkpoint without
broadcast
● Allows commit money safely
● Allows to pay on demand by the second
● For example: decentralized WiFi Hotspots or
subcontracting services in third world countries.
73. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
Micro Payment Channels Mechanic
TX1
IN {0: 1 BTC }
OUT {0: 1 BTC MULTISIG BOB SAM }
74. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
Micro Payment Channels Mechanic
TX1
IN {0: 1 BTC }
OUT {0: 1 BTC MULTISIG BOB SAM }
IN {TX1[0]: 1 BTC }
OUT { 0 : 1 BTC BOB }
LOCKTIME : 18
TX2
75. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
Micro Payment Channels Mechanic
TX1
IN {0: 1 BTC }
OUT {0: 1 BTC MULTISIG BOB SAM }
IN {TX1[0]: 1 BTC }
OUT { 0 : 1 BTC BOB }
LOCKTIME : 18
TX2
76. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
Micro Payment Channels Mechanic
TX1
IN {0: 1 BTC }
OUT {0: 1 BTC MULTISIG BOB SAM }
IN {TX1[0]: 1 BTC }
OUT { 0 : 1 BTC BOB }
LOCKTIME : 18
TX2
77. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
Micro Payment Channels Mechanic
TX1
IN {0: 1 BTC }
OUT {0: 1 BTC MULTISIG BOB SAM }
IN {TX1[0]: 1 BTC }
OUT { 0 : 1 BTC BOB }
LOCKTIME : 18
TX2
IN {TX1[0] 1 BTC }
OUT {0: 0,9 BTC BOB
0,1 BTC SAM }
78. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
Micro Payment Channels Mechanic
TX1
IN {0: 1 BTC }
OUT {0: 1 BTC MULTISIG BOB SAM }
IN {TX1[0]: 1 BTC }
OUT { 0 : 1 BTC BOB }
LOCKTIME : 18
TX2
IN {TX1[0] 1 BTC }
OUT {0: 0,8 BTC BOB
0,2 BTC SAM }
79. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
Micro Payment Channels Mechanic
TX1
IN {0: 1 BTC }
OUT {0: 1 BTC MULTISIG BOB SAM }
IN {TX1[0]: 1 BTC }
OUT { 0 : 1 BTC BOB }
LOCKTIME : 18
TX2
IN {TX1[0] 1 BTC }
OUT {0: 0,7 BTC BOB
0,3 BTC SAM }
80. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
Micro Payment Channels Mechanic
TX1
IN {0: 1 BTC }
OUT {0: 1 BTC MULTISIG BOB SAM }
IN {TX1[0]: 1 BTC }
OUT { 0 : 1 BTC BOB }
LOCKTIME : 18
TX2
IN {TX1[0] 1 BTC }
OUT {0: 0,6 BTC BOB
0,4 BTC SAM }
BREAK!
81. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
Micro Payment Channels Mechanic
TX1
IN {0: 1 BTC }
OUT {0: 1 BTC MULTISIG BOB SAM }
IN {TX1[0]: 1 BTC }
OUT { 0 : 1 BTC BOB }
LOCKTIME : 18
TX2
IN {TX1[0] 1 BTC }
OUT {0: 0,6 BTC BOB
0,4 BTC SAM }
82. Bitcoin Protocol for Developers #BitcoinT3F
Leganés
6 Febrero 2013
THANKS!
● BigData is the current wave, P2P is the next.
● APIs and P2P are the next challenge on finantial world.
● Bitcoin technology and protocol is here to stay.