<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://en.bitcoin.it/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Hal</id>
	<title>Bitcoin Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://en.bitcoin.it/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Hal"/>
	<link rel="alternate" type="text/html" href="https://en.bitcoin.it/wiki/Special:Contributions/Hal"/>
	<updated>2026-04-10T05:38:19Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.8</generator>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Lazy_API&amp;diff=6689</id>
		<title>Lazy API</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Lazy_API&amp;diff=6689"/>
		<updated>2011-04-04T01:50:21Z</updated>

		<summary type="html">&lt;p&gt;Hal: Add recheck step for safety&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;For the incredibly lazy and/or incompetent web developer, I present the lazy man&#039;s bitcoin API (copied from [http://www.bitcoin.org/smf/index.php?topic=4324.msg77187#msg77187 a forum post]):&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Problem:&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
Lazy web designer (me) wants to use bitcoins without dealing with installing bitcoin on a server, installing a shopping card interface, or using ugly merchant services with callbacks.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Solution for sending bitcoins:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Use the [https://mtgox.com/support/tradeAPI MtGox API]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Solution for receiving bitcoins:&#039;&#039;&#039;&lt;br /&gt;
# Input a list of bitcoin receiving addresses to your database&lt;br /&gt;
# Give a bitcoin address to a potential customer&lt;br /&gt;
# Have the customer tell you when they have sent the coins and have at least 1 confirmation&lt;br /&gt;
# Check blockexplorer to see if they sent the right amount (i.e. http://blockexplorer.com/q/getreceivedbyaddress/19hMEAaRMbEhfSkeU4GT8mgSuyR4t4M6TH)&lt;br /&gt;
# Wait for more blocks (confirmations) if paranoid  (look for the latest block number to go up here: http://blockexplorer.com/q/getblockcount)&lt;br /&gt;
# Recheck blockexplorer to make sure the address still shows the right amount, in case the block chain reorganizes&lt;br /&gt;
# Give them what they paid for&lt;br /&gt;
# After a reasonable amount of time has passed, you can re-use the address for another customer&lt;br /&gt;
&lt;br /&gt;
You could avoid having a list of addresses and reusing them if one of the wallet services someday lets you get a new address via API call, but this will work for now.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Bad idea:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Selling bars of gold this way (owner of blockexplorer.com could rip you off)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Good(?) idea:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Selling naked pictures of your grandma this way (owner of blockexplorer.com won&#039;t bother)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Shameless begging:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
If this info is useful to you, please consider a donation: 19hMEAaRMbEhfSkeU4GT8mgSuyR4t4M6TH&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer]]&lt;/div&gt;</summary>
		<author><name>Hal</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Protocol_rules&amp;diff=5533</id>
		<title>Protocol rules</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Protocol_rules&amp;diff=5533"/>
		<updated>2011-03-16T22:44:17Z</updated>

		<summary type="html">&lt;p&gt;Hal: /* &amp;quot;block&amp;quot; messages */ Add orphan block recursion&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Rules&#039;&#039;&#039; for [[:Category:Clients|clients]].&lt;br /&gt;
&lt;br /&gt;
The wiki substantially documents the [[Protocol_specification|Bitcoin protocol]], but equally important are the rules used by the client to process messages. It&#039;s crucial that clients follow certain rules in order to maintain consistency across the network, and to protect the Bitcoin security guarantees.&lt;br /&gt;
&lt;br /&gt;
Here, the focus is on handling tx and block messages, because that is the tricky logic. This will skip over the method of requesting and forwarding these messages for now, and describe what to do when they are received. Also, this will describe the minimal data structures in rather abstract terms, ignoring the client&#039;s various indexes, maps and hash tables used for efficiency. This will be a conceptual description. This is all based on a fairly literal reading of the source code.&lt;br /&gt;
&lt;br /&gt;
Mining (block generation) rules are not yet presented.&lt;br /&gt;
&lt;br /&gt;
== Data structures ==&lt;br /&gt;
&lt;br /&gt;
The main data structures are [[transactions]] and [[blocks]]. Blocks are composed of the &#039;&#039;block header&#039;&#039; followed by transactions in the block. Transactions are identified by their hash; blocks by the hash of their header. Blocks have prev pointers that link them into a graph.&lt;br /&gt;
&lt;br /&gt;
Conceptually, the client has the following data structures:&lt;br /&gt;
&lt;br /&gt;
=== Transactions ===&lt;br /&gt;
&lt;br /&gt;
There are two collections of transactions:&lt;br /&gt;
&lt;br /&gt;
;transaction pool&lt;br /&gt;
: an unordered collection of transactions that are not in blocks in the main chain, but for which we have input transactions&lt;br /&gt;
&lt;br /&gt;
;orphan transactions&lt;br /&gt;
: transactions that can&#039;t go into the pool due to one or more missing input transactions&lt;br /&gt;
&lt;br /&gt;
=== Blocks ===&lt;br /&gt;
&lt;br /&gt;
There are 3 categories of blocks:&lt;br /&gt;
&lt;br /&gt;
;blocks in the main branch&lt;br /&gt;
: the transactions in these blocks are considered at least tentatively confirmed&lt;br /&gt;
&lt;br /&gt;
;blocks on side branches off the main branch&lt;br /&gt;
: these blocks have at least tentatively lost the race to be in the main branch&lt;br /&gt;
&lt;br /&gt;
;orphan blocks&lt;br /&gt;
: these are blocks which don&#039;t link into the main branch, normally because of a missing predecessor or nth-level predecessor&lt;br /&gt;
&lt;br /&gt;
Blocks in the first two categories form a tree rooted at the genesis block, linked by the prev pointer, which points toward the root. (It is a very linear tree with few and short branches off the main branch.) The main branch is defined as the branch with highest total difficulty, summing the difficulties for each block in the branch.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== [[Protocol_specification#tx|&amp;quot;tx&amp;quot;]] messages ==&lt;br /&gt;
&lt;br /&gt;
These messages hold a single transaction.&lt;br /&gt;
&lt;br /&gt;
# Check syntactic correctness&lt;br /&gt;
# Make sure neither in or out lists are empty&lt;br /&gt;
# Size in bytes &amp;lt; MAX_BLOCK_SIZE&lt;br /&gt;
# Each output value, as well as the total, must be in legal money range&lt;br /&gt;
# Make sure none of the inputs have hash=0, n=-1 (&#039;&#039;coinbase&#039;&#039; transactions)&lt;br /&gt;
# Check that nLockTime &amp;lt;= INT_MAX, size in bytes &amp;gt;= 100, and sig opcount &amp;lt;= 2&lt;br /&gt;
# Reject &amp;quot;nonstandard&amp;quot; transactions: scriptSig doing anything other than pushing numbers on the stack, or scriptPubkey not matching the two usual forms&lt;br /&gt;
# Reject if we already have matching tx in the pool, or in a block in the main branch&lt;br /&gt;
# Reject if any other tx in the pool uses the same transaction output as one used by this tx.&lt;br /&gt;
# For each input, look in the main branch and the transaction pool to find the referenced output transaction. If the output transaction is missing for any input, this will be an orphan transaction. Add to the orphan transactions, if a matching transaction is not in there already.&lt;br /&gt;
# For each input, if we are using the &#039;&#039;n&#039;&#039;th output of the earlier transaction, but it has fewer than n+1 outputs, reject this transaction&lt;br /&gt;
# For each input, if the referenced output transaction is coinbase (i.e. only 1 input, with hash=0, n=-1), it must have at least COINBASE_MATURITY confirmations; else reject this transaction&lt;br /&gt;
# Verify crypto signatures for each input; reject if any are bad&lt;br /&gt;
# For each input, if the referenced output has already been spent by a transaction in the main branch, reject this transaction&lt;br /&gt;
# Using the referenced output transactions to get input values, check that each input value, as well as the sum, are in legal money range&lt;br /&gt;
# Reject if the sum of input values &amp;lt; sum of output values&lt;br /&gt;
# Reject if transaction fee (defined as sum of input values minus sum of output values) would be too low to get into an empty block&lt;br /&gt;
# Add to transaction pool&lt;br /&gt;
# &amp;quot;Add to wallet if mine&amp;quot;&lt;br /&gt;
# Relay transaction to peers&lt;br /&gt;
# For each orphan transaction that uses this one as one of its inputs, run all these steps (including this one) recursively on that orphan&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== [[Protocol_specification#block|&amp;quot;block&amp;quot;]] messages ==&lt;br /&gt;
&lt;br /&gt;
These messages hold a single block.&lt;br /&gt;
&lt;br /&gt;
# Check syntactic correctness&lt;br /&gt;
# Reject if duplicate of block we have in any of the three categories&lt;br /&gt;
# Transaction list must be non-empty&lt;br /&gt;
# Block hash must satisfy claimed &#039;&#039;nBits&#039;&#039; proof of work&lt;br /&gt;
# Block timestamp must not be more than two hours in the future&lt;br /&gt;
# First transaction must be coinbase (i.e. only 1 input, with hash=0, n=-1), the rest must not be&lt;br /&gt;
# For each transaction, apply &amp;quot;tx&amp;quot; checks 2-4&lt;br /&gt;
# For the coinbase (first) transaction, scriptSig length must be 2-100&lt;br /&gt;
# For the other transactions, reject if any input has hash=0, n=-1&lt;br /&gt;
# Reject if sum of transaction sig opcounts &amp;gt; MAX_BLOCK_SIGOPS&lt;br /&gt;
# Verify Merkle hash&lt;br /&gt;
# Check if prev block (matching &#039;&#039;prev&#039;&#039; hash) is in main branch or side branches. If not, add this to orphan blocks, then query peer we got this from for 1st missing orphan block in &#039;&#039;prev&#039;&#039; chain; done with block&lt;br /&gt;
# Check that &#039;&#039;nBits&#039;&#039; value matches the difficulty rules&lt;br /&gt;
# Reject if timestamp is before prev block (for a complicated definition of &amp;quot;before&amp;quot;)&lt;br /&gt;
# For certain old blocks (i.e. on initial block download) check that hash matches known values&lt;br /&gt;
# Add block into the tree. There are three cases: 1. block further extends the main branch; 2. block extends a side branch but does not add enough difficulty to make it become the new main branch; 3. block extends a side branch and makes it the new main branch.&lt;br /&gt;
# For case 1, adding to main branch:&lt;br /&gt;
## For all but the coinbase transaction, apply the following:&lt;br /&gt;
### For each input, look in the main branch to find the referenced output transaction. Reject if the output transaction is missing for any input.&lt;br /&gt;
### For each input, if we are using the &#039;&#039;n&#039;&#039;th output of the earlier transaction, but it has fewer than n+1 outputs, reject.&lt;br /&gt;
### For each input, if the referenced output transaction is coinbase (i.e. only 1 input, with hash=0, n=-1), it must have at least COINBASE_MATURITY confirmations; else reject.&lt;br /&gt;
### Verify crypto signatures for each input; reject if any are bad&lt;br /&gt;
### For each input, if the referenced output has already been spent by a transaction in the main branch, reject&lt;br /&gt;
### Using the referenced output transactions to get input values, check that each input value, as well as the sum, are in legal money range&lt;br /&gt;
### Reject if the sum of input values &amp;lt; sum of output values&lt;br /&gt;
## Reject if coinbase value &amp;gt; sum of block creation fee and transaction fees&lt;br /&gt;
## (If we have not rejected):&lt;br /&gt;
## For each transaction, &amp;quot;Add to wallet if mine&amp;quot;&lt;br /&gt;
## For each transaction in the block, delete any matching transaction from the transaction pool&lt;br /&gt;
## Relay block to our peers&lt;br /&gt;
## If we rejected, the block is not counted as part of the main branch&lt;br /&gt;
# For case 2, adding to a side branch, we don&#039;t do anything.&lt;br /&gt;
# For case 3, a side branch becoming the main branch:&lt;br /&gt;
## Find the &#039;&#039;fork&#039;&#039; block on the main branch which this side branch forks off of&lt;br /&gt;
## Redefine the main branch to only go up to this &#039;&#039;fork&#039;&#039; block&lt;br /&gt;
## For each block on the side branch, from the child of the &#039;&#039;fork&#039;&#039; block to the leaf, add to the main branch:&lt;br /&gt;
### Do &amp;quot;branch&amp;quot; checks 3-11&lt;br /&gt;
### For all but the coinbase transaction, apply the following:&lt;br /&gt;
#### For each input, look in the main branch to find the referenced output transaction. Reject if the output transaction is missing for any input.&lt;br /&gt;
#### For each input, if we are using the &#039;&#039;n&#039;&#039;th output of the earlier transaction, but it has fewer than n+1 outputs, reject.&lt;br /&gt;
#### For each input, if the referenced output transaction is coinbase (i.e. only 1 input, with hash=0, n=-1), it must have at least COINBASE_MATURITY confirmations; else reject.&lt;br /&gt;
#### Verify crypto signatures for each input; reject if any are bad&lt;br /&gt;
#### For each input, if the referenced output has already been spent by a transaction in the main branch, reject&lt;br /&gt;
#### Using the referenced output transactions to get input values, check that each input value, as well as the sum, are in legal money range&lt;br /&gt;
#### Reject if the sum of input values &amp;lt; sum of output values&lt;br /&gt;
### Reject if coinbase value &amp;gt; sum of block creation fee and transaction fees&lt;br /&gt;
### (If we have not rejected):&lt;br /&gt;
### For each transaction, &amp;quot;Add to wallet if mine&amp;quot;&lt;br /&gt;
## If we reject at any point, leave the main branch as what it was originally, done with block&lt;br /&gt;
## For each block in the old main branch, from the leaf down to the child of the &#039;&#039;fork&#039;&#039; block:&lt;br /&gt;
### For each non-coinbase transaction in the block:&lt;br /&gt;
#### Apply &amp;quot;tx&amp;quot; checks 2-9, except in step 8, only look in the transaction pool for duplicates, not the main branch&lt;br /&gt;
#### Add to transaction pool if accepted, else go on to next transaction&lt;br /&gt;
## For each block in the new main branch, from the child of the &#039;&#039;fork&#039;&#039; node to the leaf:&lt;br /&gt;
### For each transaction in the block, delete any matching transaction from the transaction pool&lt;br /&gt;
## Relay block to our peers&lt;br /&gt;
# For each orphan block for which this block is its &#039;&#039;prev&#039;&#039;, run all these steps (including this one) recursively on that orphan&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
&lt;br /&gt;
* [[Protocol specification]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Technical]][[Category:Developer]]&lt;/div&gt;</summary>
		<author><name>Hal</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Protocol_rules&amp;diff=5532</id>
		<title>Protocol rules</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Protocol_rules&amp;diff=5532"/>
		<updated>2011-03-16T22:19:43Z</updated>

		<summary type="html">&lt;p&gt;Hal: First cut at final block rules&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Rules&#039;&#039;&#039; for [[:Category:Clients|clients]].&lt;br /&gt;
&lt;br /&gt;
The wiki substantially documents the [[Protocol_specification|Bitcoin protocol]], but equally important are the rules used by the client to process messages. It&#039;s crucial that clients follow certain rules in order to maintain consistency across the network, and to protect the Bitcoin security guarantees.&lt;br /&gt;
&lt;br /&gt;
Here, the focus is on handling tx and block messages, because that is the tricky logic. This will skip over the method of requesting and forwarding these messages for now, and describe what to do when they are received. Also, this will describe the minimal data structures in rather abstract terms, ignoring the client&#039;s various indexes, maps and hash tables used for efficiency. This will be a conceptual description. This is all based on a fairly literal reading of the source code.&lt;br /&gt;
&lt;br /&gt;
Mining (block generation) rules are not yet presented.&lt;br /&gt;
&lt;br /&gt;
== Data structures ==&lt;br /&gt;
&lt;br /&gt;
The main data structures are [[transactions]] and [[blocks]]. Blocks are composed of the &#039;&#039;block header&#039;&#039; followed by transactions in the block. Transactions are identified by their hash; blocks by the hash of their header. Blocks have prev pointers that link them into a graph.&lt;br /&gt;
&lt;br /&gt;
Conceptually, the client has the following data structures:&lt;br /&gt;
&lt;br /&gt;
=== Transactions ===&lt;br /&gt;
&lt;br /&gt;
There are two collections of transactions:&lt;br /&gt;
&lt;br /&gt;
;transaction pool&lt;br /&gt;
: an unordered collection of transactions that are not in blocks in the main chain, but for which we have input transactions&lt;br /&gt;
&lt;br /&gt;
;orphan transactions&lt;br /&gt;
: transactions that can&#039;t go into the pool due to one or more missing input transactions&lt;br /&gt;
&lt;br /&gt;
=== Blocks ===&lt;br /&gt;
&lt;br /&gt;
There are 3 categories of blocks:&lt;br /&gt;
&lt;br /&gt;
;blocks in the main branch&lt;br /&gt;
: the transactions in these blocks are considered at least tentatively confirmed&lt;br /&gt;
&lt;br /&gt;
;blocks on side branches off the main branch&lt;br /&gt;
: these blocks have at least tentatively lost the race to be in the main branch&lt;br /&gt;
&lt;br /&gt;
;orphan blocks&lt;br /&gt;
: these are blocks which don&#039;t link into the main branch, normally because of a missing predecessor or nth-level predecessor&lt;br /&gt;
&lt;br /&gt;
Blocks in the first two categories form a tree rooted at the genesis block, linked by the prev pointer, which points toward the root. (It is a very linear tree with few and short branches off the main branch.) The main branch is defined as the branch with highest total difficulty, summing the difficulties for each block in the branch.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== [[Protocol_specification#tx|&amp;quot;tx&amp;quot;]] messages ==&lt;br /&gt;
&lt;br /&gt;
These messages hold a single transaction.&lt;br /&gt;
&lt;br /&gt;
# Check syntactic correctness&lt;br /&gt;
# Make sure neither in or out lists are empty&lt;br /&gt;
# Size in bytes &amp;lt; MAX_BLOCK_SIZE&lt;br /&gt;
# Each output value, as well as the total, must be in legal money range&lt;br /&gt;
# Make sure none of the inputs have hash=0, n=-1 (&#039;&#039;coinbase&#039;&#039; transactions)&lt;br /&gt;
# Check that nLockTime &amp;lt;= INT_MAX, size in bytes &amp;gt;= 100, and sig opcount &amp;lt;= 2&lt;br /&gt;
# Reject &amp;quot;nonstandard&amp;quot; transactions: scriptSig doing anything other than pushing numbers on the stack, or scriptPubkey not matching the two usual forms&lt;br /&gt;
# Reject if we already have matching tx in the pool, or in a block in the main branch&lt;br /&gt;
# Reject if any other tx in the pool uses the same transaction output as one used by this tx.&lt;br /&gt;
# For each input, look in the main branch and the transaction pool to find the referenced output transaction. If the output transaction is missing for any input, this will be an orphan transaction. Add to the orphan transactions, if a matching transaction is not in there already.&lt;br /&gt;
# For each input, if we are using the &#039;&#039;n&#039;&#039;th output of the earlier transaction, but it has fewer than n+1 outputs, reject this transaction&lt;br /&gt;
# For each input, if the referenced output transaction is coinbase (i.e. only 1 input, with hash=0, n=-1), it must have at least COINBASE_MATURITY confirmations; else reject this transaction&lt;br /&gt;
# Verify crypto signatures for each input; reject if any are bad&lt;br /&gt;
# For each input, if the referenced output has already been spent by a transaction in the main branch, reject this transaction&lt;br /&gt;
# Using the referenced output transactions to get input values, check that each input value, as well as the sum, are in legal money range&lt;br /&gt;
# Reject if the sum of input values &amp;lt; sum of output values&lt;br /&gt;
# Reject if transaction fee (defined as sum of input values minus sum of output values) would be too low to get into an empty block&lt;br /&gt;
# Add to transaction pool&lt;br /&gt;
# &amp;quot;Add to wallet if mine&amp;quot;&lt;br /&gt;
# Relay transaction to peers&lt;br /&gt;
# For each orphan transaction that uses this one as one of its inputs, run all these steps (including this one) recursively on that orphan&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== [[Protocol_specification#block|&amp;quot;block&amp;quot;]] messages ==&lt;br /&gt;
&lt;br /&gt;
These messages hold a single block.&lt;br /&gt;
&lt;br /&gt;
# Check syntactic correctness&lt;br /&gt;
# Reject if duplicate of block we have in any of the three categories&lt;br /&gt;
# Transaction list must be non-empty&lt;br /&gt;
# Block hash must satisfy claimed &#039;&#039;nBits&#039;&#039; proof of work&lt;br /&gt;
# Block timestamp must not be more than two hours in the future&lt;br /&gt;
# First transaction must be coinbase (i.e. only 1 input, with hash=0, n=-1), the rest must not be&lt;br /&gt;
# For each transaction, apply &amp;quot;tx&amp;quot; checks 2-4&lt;br /&gt;
# For the coinbase (first) transaction, scriptSig length must be 2-100&lt;br /&gt;
# For the other transactions, reject if any input has hash=0, n=-1&lt;br /&gt;
# Reject if sum of transaction sig opcounts &amp;gt; MAX_BLOCK_SIGOPS&lt;br /&gt;
# Verify Merkle hash&lt;br /&gt;
# Check if prev block (matching &#039;&#039;prev&#039;&#039; hash) is in main branch or side branches. If not, add this to orphan blocks, then query peer we got this from for 1st missing orphan block in &#039;&#039;prev&#039;&#039; chain; done with block&lt;br /&gt;
# Check that &#039;&#039;nBits&#039;&#039; value matches the difficulty rules&lt;br /&gt;
# Reject if timestamp is before prev block (for a complicated definition of &amp;quot;before&amp;quot;)&lt;br /&gt;
# For certain old blocks (i.e. on initial block download) check that hash matches known values&lt;br /&gt;
# Add block into the tree. There are three cases: 1. block further extends the main branch; 2. block extends a side branch but does not add enough difficulty to make it become the new main branch; 3. block extends a side branch and makes it the new main branch.&lt;br /&gt;
# For case 1, adding to main branch:&lt;br /&gt;
## For all but the coinbase transaction, apply the following:&lt;br /&gt;
### For each input, look in the main branch to find the referenced output transaction. Reject if the output transaction is missing for any input.&lt;br /&gt;
### For each input, if we are using the &#039;&#039;n&#039;&#039;th output of the earlier transaction, but it has fewer than n+1 outputs, reject.&lt;br /&gt;
### For each input, if the referenced output transaction is coinbase (i.e. only 1 input, with hash=0, n=-1), it must have at least COINBASE_MATURITY confirmations; else reject.&lt;br /&gt;
### Verify crypto signatures for each input; reject if any are bad&lt;br /&gt;
### For each input, if the referenced output has already been spent by a transaction in the main branch, reject&lt;br /&gt;
### Using the referenced output transactions to get input values, check that each input value, as well as the sum, are in legal money range&lt;br /&gt;
### Reject if the sum of input values &amp;lt; sum of output values&lt;br /&gt;
## Reject if coinbase value &amp;gt; sum of block creation fee and transaction fees&lt;br /&gt;
## (If we have not rejected):&lt;br /&gt;
## For each transaction, &amp;quot;Add to wallet if mine&amp;quot;&lt;br /&gt;
## For each transaction in the block, delete any matching transaction from the transaction pool&lt;br /&gt;
## Relay block to our peers&lt;br /&gt;
## If we rejected, the block is not counted as part of the main branch&lt;br /&gt;
# For case 2, adding to a side branch, we don&#039;t do anything.&lt;br /&gt;
# For case 3, a side branch becoming the main branch:&lt;br /&gt;
## Find the &#039;&#039;fork&#039;&#039; block on the main branch which this side branch forks off of&lt;br /&gt;
## Redefine the main branch to only go up to this &#039;&#039;fork&#039;&#039; block&lt;br /&gt;
## For each block on the side branch, from the child of the &#039;&#039;fork&#039;&#039; block to the leaf, add to the main branch:&lt;br /&gt;
### Do &amp;quot;branch&amp;quot; checks 3-11&lt;br /&gt;
### For all but the coinbase transaction, apply the following:&lt;br /&gt;
#### For each input, look in the main branch to find the referenced output transaction. Reject if the output transaction is missing for any input.&lt;br /&gt;
#### For each input, if we are using the &#039;&#039;n&#039;&#039;th output of the earlier transaction, but it has fewer than n+1 outputs, reject.&lt;br /&gt;
#### For each input, if the referenced output transaction is coinbase (i.e. only 1 input, with hash=0, n=-1), it must have at least COINBASE_MATURITY confirmations; else reject.&lt;br /&gt;
#### Verify crypto signatures for each input; reject if any are bad&lt;br /&gt;
#### For each input, if the referenced output has already been spent by a transaction in the main branch, reject&lt;br /&gt;
#### Using the referenced output transactions to get input values, check that each input value, as well as the sum, are in legal money range&lt;br /&gt;
#### Reject if the sum of input values &amp;lt; sum of output values&lt;br /&gt;
### Reject if coinbase value &amp;gt; sum of block creation fee and transaction fees&lt;br /&gt;
### (If we have not rejected):&lt;br /&gt;
### For each transaction, &amp;quot;Add to wallet if mine&amp;quot;&lt;br /&gt;
## If we reject at any point, leave the main branch as what it was originally, done with block&lt;br /&gt;
## For each block in the old main branch, from the leaf down to the child of the &#039;&#039;fork&#039;&#039; block:&lt;br /&gt;
### For each non-coinbase transaction in the block:&lt;br /&gt;
#### Apply &amp;quot;tx&amp;quot; checks 2-9, except in step 8, only look in the transaction pool for duplicates, not the main branch&lt;br /&gt;
#### Add to transaction pool if accepted, else go on to next transaction&lt;br /&gt;
## For each block in the new main branch, from the child of the &#039;&#039;fork&#039;&#039; node to the leaf:&lt;br /&gt;
### For each transaction in the block, delete any matching transaction from the transaction pool&lt;br /&gt;
## Relay block to our peers&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
&lt;br /&gt;
* [[Protocol specification]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Technical]][[Category:Developer]]&lt;/div&gt;</summary>
		<author><name>Hal</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Protocol_rules&amp;diff=5498</id>
		<title>Protocol rules</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Protocol_rules&amp;diff=5498"/>
		<updated>2011-03-15T22:17:55Z</updated>

		<summary type="html">&lt;p&gt;Hal: Add some links&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Rules&#039;&#039;&#039; for [[:Category:Clients|clients]].&lt;br /&gt;
&lt;br /&gt;
The wiki substantially documents the [[Protocol_specification|Bitcoin protocol]], but equally important are the rules used by the client to process messages. It&#039;s crucial that clients follow certain rules in order to maintain consistency across the network, and to protect the Bitcoin security guarantees.&lt;br /&gt;
&lt;br /&gt;
Here, the focus is on handling tx and block messages, because that is the tricky logic. This will skip over the method of requesting and forwarding these messages for now, and describe what to do when they are received. Also, this will describe the minimal data structures in rather abstract terms, ignoring the client&#039;s various indexes, maps and hash tables used for efficiency. This will be a conceptual description. This is all based on a fairly literal reading of the source code.&lt;br /&gt;
&lt;br /&gt;
Mining (block generation) rules are not yet presented.&lt;br /&gt;
&lt;br /&gt;
== Data structures ==&lt;br /&gt;
&lt;br /&gt;
The main data structures are [[transactions]] and [[blocks]]. Blocks are composed of the &#039;&#039;block header&#039;&#039; followed by transactions in the block. Transactions are identified by their hash; blocks by the hash of their header. Blocks have prev pointers that link them into a graph.&lt;br /&gt;
&lt;br /&gt;
Conceptually, the client has the following data structures:&lt;br /&gt;
&lt;br /&gt;
=== Transactions ===&lt;br /&gt;
&lt;br /&gt;
There are two collections of transactions:&lt;br /&gt;
&lt;br /&gt;
;transaction pool&lt;br /&gt;
: an unordered collection of transactions that are not in blocks in the main chain, but for which we have input transactions&lt;br /&gt;
&lt;br /&gt;
;orphan transactions&lt;br /&gt;
: transactions that can&#039;t go into the pool due to one or more missing input transactions&lt;br /&gt;
&lt;br /&gt;
=== Blocks ===&lt;br /&gt;
&lt;br /&gt;
There are 3 categories of blocks:&lt;br /&gt;
&lt;br /&gt;
;blocks in the main branch&lt;br /&gt;
: the transactions in these blocks are considered at least tentatively confirmed&lt;br /&gt;
&lt;br /&gt;
;blocks on side branches off the main branch&lt;br /&gt;
: these blocks have at least tentatively lost the race to be in the main branch&lt;br /&gt;
&lt;br /&gt;
;orphan blocks&lt;br /&gt;
: these are blocks which don&#039;t link into the main branch, normally because of a missing predecessor or nth-level predecessor&lt;br /&gt;
&lt;br /&gt;
Blocks in the first two categories form a tree rooted at the genesis block, linked by the prev pointer, which points toward the root. (It is a very linear tree with few and short branches off the main branch.) The main branch is defined as the branch with highest total difficulty, summing the difficulties for each block in the branch.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== [[Protocol_specification#tx|&amp;quot;tx&amp;quot;]] messages ==&lt;br /&gt;
&lt;br /&gt;
These messages hold a single transaction.&lt;br /&gt;
&lt;br /&gt;
# Check syntactic correctness&lt;br /&gt;
# Make sure neither in or out lists are empty&lt;br /&gt;
# Size in bytes &amp;lt; MAX_BLOCK_SIZE&lt;br /&gt;
# Each output value, as well as the total, must be in legal money range&lt;br /&gt;
# Make sure none of the inputs have hash=0, n=-1 (&#039;&#039;coinbase&#039;&#039; transactions)&lt;br /&gt;
# Check that nLockTime &amp;lt;= INT_MAX, size in bytes &amp;gt;= 100, and sig opcount &amp;lt;= 2&lt;br /&gt;
# Reject &amp;quot;nonstandard&amp;quot; transactions: scriptSig doing anything other than pushing numbers on the stack, or scriptPubkey not matching the two usual forms&lt;br /&gt;
# Reject if we already have matching tx in the pool, or in a block in the main branch&lt;br /&gt;
# Reject if any other tx in the pool uses the same transaction output as one used by this tx.&lt;br /&gt;
# For each input, look in the main branch and the transaction pool to find the referenced output transaction. If the output transaction is missing for any input, this will be an orphan transaction. Add to the orphan transactions, if a matching transaction is not in there already.&lt;br /&gt;
# For each input, if we are using the &#039;&#039;n&#039;&#039;th output of the earlier transaction, but it has fewer than n+1 outputs, reject this transaction&lt;br /&gt;
# For each input, if the referenced output transaction is coinbase (i.e. only 1 input, with hash=0, n=-1), it must have at least COINBASE_MATURITY confirmations; else reject this transaction&lt;br /&gt;
# Verify crypto signatures for each input; reject if any are bad&lt;br /&gt;
# For each input, if the referenced output has already been spent by a transaction in the main branch or the transaction pool, reject this transaction&lt;br /&gt;
# Using the referenced output transactions to get input values, check that each input value, as well as the sum, are in legal money range&lt;br /&gt;
# Reject if the sum of input values &amp;lt; sum of output values&lt;br /&gt;
# Reject if transaction fee (defined as sum of input values minus sum of output values) would be too low to get into an empty block&lt;br /&gt;
# Add to transaction pool&lt;br /&gt;
# Add to wallet if mine&lt;br /&gt;
# Relay transaction to peers&lt;br /&gt;
# For each orphan transaction that uses this one as one of its inputs, run all these steps (including this one) recursively on that orphan&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== [[Protocol_specification#block|&amp;quot;block&amp;quot;]] messages ==&lt;br /&gt;
&lt;br /&gt;
These messages hold a single block.&lt;br /&gt;
&lt;br /&gt;
# Check syntactic correctness&lt;br /&gt;
# Reject if duplicate of block we have in any of the three categories&lt;br /&gt;
# Transaction list must be non-empty&lt;br /&gt;
# Block hash must satisfy claimed &#039;&#039;nBits&#039;&#039; proof of work&lt;br /&gt;
# Block timestamp must not be more than two hours in the future&lt;br /&gt;
# First transaction must be coinbase (i.e. only 1 input, with hash=0, n=-1), the rest must not be&lt;br /&gt;
# For each transaction, apply &amp;quot;tx&amp;quot; checks 2-4&lt;br /&gt;
# For the coinbase (first) transaction, scriptSig length must be 2-100&lt;br /&gt;
# For the other transactions, reject if any input has hash=0, n=-1&lt;br /&gt;
# Reject if sum of transaction sig opcounts &amp;gt; MAX_BLOCK_SIGOPS&lt;br /&gt;
# Verify Merkle hash&lt;br /&gt;
# Check if prev block (matching &#039;&#039;prev&#039;&#039; hash) is in main branch or side branches. If not, add this to orphan blocks, then query peer we got this from for 1st missing orphan block in &#039;&#039;prev&#039;&#039; chain; done with block&lt;br /&gt;
# Check that &#039;&#039;nBits&#039;&#039; value matches the difficulty rules&lt;br /&gt;
# Reject if timestamp is before prev block (for a complicated definition of &amp;quot;before&amp;quot;)&lt;br /&gt;
# For certain blocks, on initial block download, check that hash is what it should be&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
&lt;br /&gt;
* [[Protocol specification]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Technical]][[Category:Developer]]&lt;/div&gt;</summary>
		<author><name>Hal</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Protocol_rules&amp;diff=5448</id>
		<title>Protocol rules</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Protocol_rules&amp;diff=5448"/>
		<updated>2011-03-15T01:19:49Z</updated>

		<summary type="html">&lt;p&gt;Hal: Call the main chain the main branch&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Rules for clients ==&lt;br /&gt;
&lt;br /&gt;
The wiki substantially documents the Bitcoin protocol, but equally important are the rules used by the client to process messages. It&#039;s crucial that clients follow certain rules in order to maintain consistency across the network, and to protect the Bitcoin security guarantees.&lt;br /&gt;
&lt;br /&gt;
Here, the focus is on handling tx and block messages, because that is the tricky logic. This will skip over the method of requesting and forwarding these messages for now, and describe what to do when they are received. Also, this will describe the minimal data structures in rather abstract terms, ignoring the client&#039;s various indexes, maps and hash tables used for efficiency. This will be a conceptual description. This is all based on a fairly literal reading of the source code.&lt;br /&gt;
&lt;br /&gt;
Mining (block generation) rules are not yet presented.&lt;br /&gt;
&lt;br /&gt;
=== Data structures ===&lt;br /&gt;
&lt;br /&gt;
The main data structures are &#039;&#039;transactions&#039;&#039; and &#039;&#039;blocks&#039;&#039;. Blocks are composed of the &#039;&#039;block header&#039;&#039; followed by transactions in the block. Transactions are identified by their hash; blocks by the hash of their header. Blocks have prev pointers that link them into a graph.&lt;br /&gt;
&lt;br /&gt;
Conceptually, the client has the following data structures:&lt;br /&gt;
&lt;br /&gt;
==== Transactions ====&lt;br /&gt;
&lt;br /&gt;
There are two collections of transactions:&lt;br /&gt;
&lt;br /&gt;
;transaction pool&lt;br /&gt;
: an unordered collection of transactions that are not in blocks in the main chain, but for which we have input transactions&lt;br /&gt;
&lt;br /&gt;
;orphan transactions&lt;br /&gt;
: transactions that can&#039;t go into the pool due to one or more missing input transactions&lt;br /&gt;
&lt;br /&gt;
==== Blocks ====&lt;br /&gt;
&lt;br /&gt;
There are 3 categories of blocks:&lt;br /&gt;
&lt;br /&gt;
;blocks in the main branch&lt;br /&gt;
: the transactions in these blocks are considered at least tentatively confirmed&lt;br /&gt;
&lt;br /&gt;
;blocks on side branches off the main branch&lt;br /&gt;
: these blocks have at least tentatively lost the race to be in the main branch&lt;br /&gt;
&lt;br /&gt;
;orphan blocks&lt;br /&gt;
: these are blocks which don&#039;t link into the main branch, normally because of a missing predecessor or nth-level predecessor&lt;br /&gt;
&lt;br /&gt;
Blocks in the first two categories form a tree rooted at the genesis block, linked by the prev pointer, which points toward the root. (It is a very linear tree with few and short branches off the main branch.) The main branch is defined as the branch with highest total difficulty, summing the difficulties for each block in the branch.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== &amp;quot;tx&amp;quot; messages ===&lt;br /&gt;
&lt;br /&gt;
These messages hold a single transaction.&lt;br /&gt;
&lt;br /&gt;
# Check syntactic correctness&lt;br /&gt;
# Make sure neither in or out lists are empty&lt;br /&gt;
# Size in bytes &amp;lt; MAX_BLOCK_SIZE&lt;br /&gt;
# Each output value, as well as the total, must be in legal money range&lt;br /&gt;
# Make sure none of the inputs have hash=0, n=-1 (&#039;&#039;coinbase&#039;&#039; transactions)&lt;br /&gt;
# Check that nLockTime &amp;lt;= INT_MAX, size in bytes &amp;gt;= 100, and sig opcount &amp;lt;= 2&lt;br /&gt;
# Reject &amp;quot;nonstandard&amp;quot; transactions: scriptSig doing anything other than pushing numbers on the stack, or scriptPubkey not matching the two usual forms&lt;br /&gt;
# Reject if we already have matching tx in the pool, or in a block in the main branch&lt;br /&gt;
# Reject if any other tx in the pool uses the same transaction output as one used by this tx.&lt;br /&gt;
# For each input, look in the main branch and the transaction pool to find the referenced output transaction. If the output transaction is missing for any input, this will be an orphan transaction. Add to the orphan transactions, if a matching transaction is not in there already.&lt;br /&gt;
# For each input, if we are using the &#039;&#039;n&#039;&#039;th output of the earlier transaction, but it has fewer than n+1 outputs, reject this transaction&lt;br /&gt;
# For each input, if the referenced output transaction is coinbase (i.e. only 1 input, with hash=0, n=-1), it must have at least COINBASE_MATURITY confirmations; else reject this transaction&lt;br /&gt;
# Verify crypto signatures for each input; reject if any are bad&lt;br /&gt;
# For each input, if the referenced output has already been spent by a transaction in the main branch or the transaction pool, reject this transaction&lt;br /&gt;
# Using the referenced output transactions to get input values, check that each input value, as well as the sum, are in legal money range&lt;br /&gt;
# Reject if the sum of input values &amp;lt; sum of output values&lt;br /&gt;
# Reject if transaction fee (defined as sum of input values minus sum of output values) would be too low to get into an empty block&lt;br /&gt;
# Add to transaction pool&lt;br /&gt;
# Add to wallet if mine&lt;br /&gt;
# Relay transaction to peers&lt;br /&gt;
# For each orphan transaction that uses this one as one of its inputs, run all these steps (including this one) recursively on that orphan&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== &amp;quot;block&amp;quot; messages ===&lt;br /&gt;
&lt;br /&gt;
These messages hold a single block.&lt;br /&gt;
&lt;br /&gt;
# Check syntactic correctness&lt;br /&gt;
# Reject if duplicate of block we have in any of the three categories&lt;br /&gt;
# Transaction list must be non-empty&lt;br /&gt;
# Block hash must satisfy claimed &#039;&#039;nBits&#039;&#039; proof of work&lt;br /&gt;
# Block timestamp must not be more than two hours in the future&lt;br /&gt;
# First transaction must be coinbase (i.e. only 1 input, with hash=0, n=-1), the rest must not be&lt;br /&gt;
# For each transaction, apply &amp;quot;tx&amp;quot; checks 2-4&lt;br /&gt;
# For the coinbase (first) transaction, scriptSig length must be 2-100&lt;br /&gt;
# For the other transactions, reject if any input has hash=0, n=-1&lt;br /&gt;
# Reject if sum of transaction sig opcounts &amp;gt; MAX_BLOCK_SIGOPS&lt;br /&gt;
# Verify Merkle hash&lt;br /&gt;
# Check if prev block (matching &#039;&#039;prev&#039;&#039; hash) is in main branch or side branches. If not, add this to orphan blocks, then query peer we got this from for 1st missing orphan block in &#039;&#039;prev&#039;&#039; chain; done with block&lt;br /&gt;
# Check that &#039;&#039;nBits&#039;&#039; value matches the difficulty rules&lt;br /&gt;
# Reject if timestamp is before prev block (for a complicated definition of &amp;quot;before&amp;quot;)&lt;br /&gt;
# For certain blocks, on initial block download, check that hash is what it should be&lt;br /&gt;
&lt;br /&gt;
[[Category:Technical]][[Category:Developer]]&lt;/div&gt;</summary>
		<author><name>Hal</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Protocol_rules&amp;diff=5415</id>
		<title>Protocol rules</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Protocol_rules&amp;diff=5415"/>
		<updated>2011-03-14T03:28:28Z</updated>

		<summary type="html">&lt;p&gt;Hal: More on blocks&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Rules for clients ==&lt;br /&gt;
&lt;br /&gt;
The wiki substantially documents the Bitcoin protocol, but equally important are the rules used by the client to process messages. It&#039;s crucial that clients follow certain rules in order to maintain consistency across the network, and to protect the Bitcoin security guarantees.&lt;br /&gt;
&lt;br /&gt;
Here, the focus is on handling tx and block messages, because that is the tricky logic. This will skip over the method of requesting and forwarding these messages for now, and describe what to do when they are received. Also, this will describe the minimal data structures in rather abstract terms, ignoring the client&#039;s various indexes, maps and hash tables used for efficiency. This will be a conceptual description. This is all based on a fairly literal reading of the source code.&lt;br /&gt;
&lt;br /&gt;
Mining (block generation) rules are not yet presented.&lt;br /&gt;
&lt;br /&gt;
=== Data structures ===&lt;br /&gt;
&lt;br /&gt;
The main data structures are &#039;&#039;transactions&#039;&#039; and &#039;&#039;blocks&#039;&#039;. Blocks are composed of the &#039;&#039;block header&#039;&#039; followed by transactions in the block. Transactions are identified by their hash; blocks by the hash of their header. Blocks have prev and next pointers that link them into a graph.&lt;br /&gt;
&lt;br /&gt;
Conceptually, the client has the following data structures:&lt;br /&gt;
&lt;br /&gt;
==== Transactions ====&lt;br /&gt;
&lt;br /&gt;
There are two collections of transactions:&lt;br /&gt;
&lt;br /&gt;
;transaction pool&lt;br /&gt;
: an unordered collection of transactions that are not in blocks in the main chain, but for which we have input transactions&lt;br /&gt;
&lt;br /&gt;
;orphan transactions&lt;br /&gt;
: transactions that can&#039;t go into the pool due to one or more missing input transactions&lt;br /&gt;
&lt;br /&gt;
==== Blocks ====&lt;br /&gt;
&lt;br /&gt;
There are 3 categories of blocks:&lt;br /&gt;
&lt;br /&gt;
;blocks in the main chain&lt;br /&gt;
: the transactions in these blocks are considered at least tentatively confirmed&lt;br /&gt;
&lt;br /&gt;
;blocks on side branches off the main chain&lt;br /&gt;
: these blocks have at least tentatively lost the race to be in the main chain&lt;br /&gt;
&lt;br /&gt;
;orphan blocks&lt;br /&gt;
: these are blocks which don&#039;t link into the main chain, normally because of a missing predecessor or nth-level predecessor&lt;br /&gt;
&lt;br /&gt;
Blocks in the first two categories form a tree rooted at the genesis block, linked by the prev pointer, which points toward the root. (It is a very linear tree with few and short branches off the main branch.) Following the next pointers from the root defines the main chain.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== &amp;quot;tx&amp;quot; messages ===&lt;br /&gt;
&lt;br /&gt;
These messages hold a single transaction.&lt;br /&gt;
&lt;br /&gt;
# Check syntactic correctness&lt;br /&gt;
# Make sure neither in or out lists are empty&lt;br /&gt;
# Size in bytes &amp;lt; MAX_BLOCK_SIZE&lt;br /&gt;
# Each output value, as well as the total, must be in legal money range&lt;br /&gt;
# Make sure none of the inputs have hash=0, n=-1 (&#039;&#039;coinbase&#039;&#039; transactions)&lt;br /&gt;
# Check that nLockTime &amp;lt;= INT_MAX, size in bytes &amp;gt;= 100, and sig opcount &amp;lt;= 2&lt;br /&gt;
# Reject &amp;quot;nonstandard&amp;quot; transactions: scriptSig doing anything other than pushing numbers on the stack, or scriptPubkey not matching the two usual forms&lt;br /&gt;
# Reject if we already have matching tx in the pool, or in a block in the main chain&lt;br /&gt;
# Reject if any other tx in the pool uses the same transaction output as one used by this tx.&lt;br /&gt;
# For each input, look in the main chain and the transaction pool to find the referenced output transaction. If the output transaction is missing for any input, this will be an orphan transaction. Add to the orphan transactions, if a matching transaction is not in there already.&lt;br /&gt;
# For each input, if we are using the &#039;&#039;n&#039;&#039;th output of the earlier transaction, but it has fewer than n+1 outputs, reject this transaction&lt;br /&gt;
# For each input, if the referenced output transaction is coinbase (i.e. only 1 input, with hash=0, n=-1), it must have at least COINBASE_MATURITY confirmations; else reject this transaction&lt;br /&gt;
# Verify crypto signatures for each input; reject if any are bad&lt;br /&gt;
# For each input, if the referenced output has already been spent by a transaction in the main chain or the transaction pool, reject this transaction&lt;br /&gt;
# Using the referenced output transactions to get input values, check that each input value, as well as the sum, are in legal money range&lt;br /&gt;
# Reject if the sum of input values &amp;lt; sum of output values&lt;br /&gt;
# Reject if transaction fee (defined as sum of input values minus sum of output values) would be too low to get into an empty block&lt;br /&gt;
# Add to transaction pool&lt;br /&gt;
# Add to wallet if mine&lt;br /&gt;
# Relay transaction to peers&lt;br /&gt;
# For each orphan transaction that uses this one as one of its inputs, run all these steps (including this one) recursively on that orphan&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== &amp;quot;block&amp;quot; messages ===&lt;br /&gt;
&lt;br /&gt;
These messages hold a single block.&lt;br /&gt;
&lt;br /&gt;
# Check syntactic correctness&lt;br /&gt;
# Reject if duplicate of block we have in any of the three categories&lt;br /&gt;
# Transaction list must be non-empty&lt;br /&gt;
# Block hash must satisfy claimed &#039;&#039;nBits&#039;&#039; proof of work&lt;br /&gt;
# Block timestamp must not be more than two hours in the future&lt;br /&gt;
# First transaction must be coinbase (i.e. only 1 input, with hash=0, n=-1), the rest must not be&lt;br /&gt;
# For each transaction, apply &amp;quot;tx&amp;quot; checks 2-4&lt;br /&gt;
# For the coinbase (first) transaction, scriptSig length must be 2-100&lt;br /&gt;
# For the other transactions, reject if any input has hash=0, n=-1&lt;br /&gt;
# Reject if sum of transaction sig opcounts &amp;gt; MAX_BLOCK_SIGOPS&lt;br /&gt;
# Verify Merkle hash&lt;br /&gt;
# Check if prev block (matching &#039;&#039;prev&#039;&#039; hash) is in main chain or side chains. If not, add this to orphan blocks, then query peer we got this from for 1st missing orphan block in &#039;&#039;prev&#039;&#039; chain; done with block&lt;br /&gt;
# Check that &#039;&#039;nBits&#039;&#039; value matches the difficulty rules&lt;br /&gt;
# Reject if timestamp is before prev block (for a complicated definition of &amp;quot;before&amp;quot;)&lt;br /&gt;
# For certain blocks, on initial block download, check that hash is what it should be&lt;br /&gt;
&lt;br /&gt;
[[Category:Technical]][[Category:Developer]]&lt;/div&gt;</summary>
		<author><name>Hal</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Protocol_rules&amp;diff=5412</id>
		<title>Protocol rules</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Protocol_rules&amp;diff=5412"/>
		<updated>2011-03-14T01:09:15Z</updated>

		<summary type="html">&lt;p&gt;Hal: Starting blocks&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Rules for clients ==&lt;br /&gt;
&lt;br /&gt;
The wiki substantially documents the Bitcoin protocol, but equally important are the rules used by the client to process messages. It&#039;s crucial that clients follow certain rules in order to maintain consistency across the network, and to protect the Bitcoin security guarantees.&lt;br /&gt;
&lt;br /&gt;
Here, the focus is on handling tx and block messages, because that is the tricky logic. This will skip over the method of requesting and forwarding these messages for now, and describe what to do when they are received. Also, this will describe the minimal data structures in rather abstract terms, ignoring the client&#039;s various indexes, maps and hash tables used for efficiency. This will be a conceptual description. This is all based on a fairly literal reading of the source code.&lt;br /&gt;
&lt;br /&gt;
Mining (block generation) rules are not yet presented.&lt;br /&gt;
&lt;br /&gt;
=== Data structures ===&lt;br /&gt;
&lt;br /&gt;
The main data structures are &#039;&#039;transactions&#039;&#039; and &#039;&#039;blocks&#039;&#039;. Blocks are composed of the &#039;&#039;block header&#039;&#039; followed by transactions in the block. Transactions are identified by their hash; blocks by the hash of their header. Blocks have prev and next pointers that link them into a graph.&lt;br /&gt;
&lt;br /&gt;
Conceptually, the client has the following data structures:&lt;br /&gt;
&lt;br /&gt;
==== Transactions ====&lt;br /&gt;
&lt;br /&gt;
There are two collections of transactions:&lt;br /&gt;
&lt;br /&gt;
;transaction pool&lt;br /&gt;
: an unordered collection of transactions that are not in blocks in the main chain, but for which we have input transactions&lt;br /&gt;
&lt;br /&gt;
;orphan transactions&lt;br /&gt;
: transactions that can&#039;t go into the pool due to one or more missing input transactions&lt;br /&gt;
&lt;br /&gt;
==== Blocks ====&lt;br /&gt;
&lt;br /&gt;
There are 3 categories of blocks:&lt;br /&gt;
&lt;br /&gt;
;blocks in the main chain&lt;br /&gt;
: the transactions in these blocks are considered at least tentatively confirmed&lt;br /&gt;
&lt;br /&gt;
;blocks on side branches off the main chain&lt;br /&gt;
: these blocks have at least tentatively lost the race to be in the main chain&lt;br /&gt;
&lt;br /&gt;
;orphan blocks&lt;br /&gt;
: these are blocks which don&#039;t link into the main chain, normally because of a missing predecessor or nth-level predecessor&lt;br /&gt;
&lt;br /&gt;
Blocks in the first two categories form a tree rooted at the genesis block, linked by the prev pointer, which points toward the root. (It is a very linear tree with few and short branches off the main branch.) Following the next pointers from the root defines the main chain.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== &amp;quot;tx&amp;quot; messages ===&lt;br /&gt;
&lt;br /&gt;
These messages hold a single transaction.&lt;br /&gt;
&lt;br /&gt;
# Check syntactic correctness&lt;br /&gt;
# Make sure neither in or out lists are empty&lt;br /&gt;
# Size in bytes &amp;lt; MAX_BLOCK_SIZE&lt;br /&gt;
# Each output value, as well as the total, must be in legal money range&lt;br /&gt;
# Make sure none of the inputs have hash=0, n=-1 (&#039;&#039;coinbase&#039;&#039; transactions)&lt;br /&gt;
# Check that nLockTime &amp;lt;= INT_MAX, size in bytes &amp;gt;= 100, and sig opcount &amp;lt;= 2&lt;br /&gt;
# Reject &amp;quot;nonstandard&amp;quot; transactions: scriptSig doing anything other than pushing numbers on the stack, or scriptPubkey not matching the two usual forms&lt;br /&gt;
# Reject if we already have matching tx in the pool, or in a block in the main chain&lt;br /&gt;
# Reject if any other tx in the pool uses the same transaction output as one used by this tx.&lt;br /&gt;
# For each input, look in the main chain and the transaction pool to find the referenced output transaction. If the output transaction is missing for any input, this will be an orphan transaction. Add to the orphan transactions, if a matching transaction is not in there already.&lt;br /&gt;
# For each input, if we are using the &#039;&#039;n&#039;&#039;th output of the earlier transaction, but it has fewer than n+1 outputs, reject this transaction&lt;br /&gt;
# For each input, if the referenced output transaction is coinbase (i.e. only 1 input, with hash=0, n=-1), it must have at least COINBASE_MATURITY confirmations; else reject this transaction&lt;br /&gt;
# Verify crypto signatures for each input; reject if any are bad&lt;br /&gt;
# For each input, if the referenced output has already been spent by a transaction in the main chain or the transaction pool, reject this transaction&lt;br /&gt;
# Using the referenced output transactions to get input values, check that each input value, as well as the sum, are in legal money range&lt;br /&gt;
# Reject if the sum of input values &amp;lt; sum of output values&lt;br /&gt;
# Reject if transaction fee (defined as sum of input values minus sum of output values) would be too low to get into an empty block&lt;br /&gt;
# Add to transaction pool&lt;br /&gt;
# Add to wallet if mine&lt;br /&gt;
# Relay transaction to peers&lt;br /&gt;
# For each orphan transaction that uses this one as one of its inputs, run all these steps (including this one) recursively on that orphan&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== &amp;quot;block&amp;quot; messages ===&lt;br /&gt;
&lt;br /&gt;
These messages hold a single block.&lt;br /&gt;
&lt;br /&gt;
# Check syntactic correctness&lt;br /&gt;
# Reject if duplicate of block we have in any of the three categories&lt;br /&gt;
# Transaction list must be non-empty&lt;br /&gt;
# Block hash must satisfy claimed &#039;&#039;nBits&#039;&#039; proof of work&lt;br /&gt;
# Block timestamp must not be more than two hours in the future&lt;br /&gt;
# First transaction must be coinbase (i.e. only 1 input, with hash=0, n=-1), the rest must not be&lt;br /&gt;
# For each transaction, apply &amp;quot;tx&amp;quot; checks 2-4&lt;br /&gt;
# For the coinbase (first) transaction, scriptSig length must be 2-100&lt;br /&gt;
# For the other transactions, reject if any input has hash=0, n=-1&lt;br /&gt;
# Reject if sum of transaction sig opcounts &amp;gt; MAX_BLOCK_SIGOPS&lt;br /&gt;
# Verify Merkle hash&lt;br /&gt;
# Apply the following tests to all but the coinbase transactions:&lt;br /&gt;
## For each input, look in the main chain for the referenced output transaction&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Technical]][[Category:Developer]]&lt;/div&gt;</summary>
		<author><name>Hal</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Protocol_rules&amp;diff=5410</id>
		<title>Protocol rules</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Protocol_rules&amp;diff=5410"/>
		<updated>2011-03-14T00:39:05Z</updated>

		<summary type="html">&lt;p&gt;Hal: Finish first cut at tx rules&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Rules for clients ==&lt;br /&gt;
&lt;br /&gt;
The wiki substantially documents the Bitcoin protocol, but equally important are the rules used by the client to process messages. It&#039;s crucial that clients follow certain rules in order to maintain consistency across the network, and to protect the Bitcoin security guarantees.&lt;br /&gt;
&lt;br /&gt;
Here, the focus is on handling tx and block messages, because that is the tricky logic. This will skip over the method of requesting and forwarding these messages for now, and describe what to do when they are received. Also, this will describe the minimal data structures in rather abstract terms, ignoring the client&#039;s various indexes, maps and hash tables used for efficiency. This will be a conceptual description. This is all based on a fairly literal reading of the source code.&lt;br /&gt;
&lt;br /&gt;
Mining (block generation) rules are not yet presented.&lt;br /&gt;
&lt;br /&gt;
=== Data structures ===&lt;br /&gt;
&lt;br /&gt;
The main data structures are &#039;&#039;transactions&#039;&#039; and &#039;&#039;blocks&#039;&#039;. Blocks are composed of the &#039;&#039;block header&#039;&#039; followed by transactions in the block. Transactions are identified by their hash; blocks by the hash of their header. Blocks have prev and next pointers that link them into a graph.&lt;br /&gt;
&lt;br /&gt;
Conceptually, the client has the following data structures:&lt;br /&gt;
&lt;br /&gt;
==== Transactions ====&lt;br /&gt;
&lt;br /&gt;
There are two collections of transactions:&lt;br /&gt;
&lt;br /&gt;
;transaction pool&lt;br /&gt;
: an unordered collection of transactions that are not in blocks in the main chain, but for which we have input transactions&lt;br /&gt;
&lt;br /&gt;
;orphan transactions&lt;br /&gt;
: transactions that can&#039;t go into the pool due to one or more missing input transactions&lt;br /&gt;
&lt;br /&gt;
==== Blocks ====&lt;br /&gt;
&lt;br /&gt;
There are 3 categories of blocks:&lt;br /&gt;
&lt;br /&gt;
;blocks in the main chain&lt;br /&gt;
: the transactions in these blocks are considered at least tentatively confirmed&lt;br /&gt;
&lt;br /&gt;
;blocks on side branches off the main chain&lt;br /&gt;
: these blocks have at least tentatively lost the race to be in the main chain&lt;br /&gt;
&lt;br /&gt;
;orphan blocks&lt;br /&gt;
: these are blocks which don&#039;t link into the main chain, normally because of a missing predecessor or nth-level predecessor&lt;br /&gt;
&lt;br /&gt;
Blocks in the first two categories form a tree rooted at the genesis block, linked by the prev pointer, which points toward the root. (It is a very linear tree with few and short branches off the main branch.) Following the next pointers from the root defines the main chain.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== &amp;quot;tx&amp;quot; messages ===&lt;br /&gt;
&lt;br /&gt;
These messages hold a single transaction.&lt;br /&gt;
&lt;br /&gt;
# Check syntactic correctness&lt;br /&gt;
# Make sure neither in or out lists are empty&lt;br /&gt;
# Size in bytes &amp;lt; MAX_BLOCK_SIZE&lt;br /&gt;
# Each output value, as well as the total, must be in legal money range&lt;br /&gt;
# Make sure none of the inputs have hash=0, n=-1 (&#039;&#039;coinbase&#039;&#039; transactions)&lt;br /&gt;
# Check that nLockTime &amp;lt;= INT_MAX, size in bytes &amp;gt;= 100, and sig opcount &amp;lt;= 2&lt;br /&gt;
# Reject &amp;quot;nonstandard&amp;quot; transactions: scriptSig doing anything other than pushing numbers on the stack, or scriptPubkey not matching the two usual forms&lt;br /&gt;
# Reject if we already have matching tx in the pool, or in a block in the main chain&lt;br /&gt;
# Reject if any other tx in the pool uses the same transaction output as one used by this tx.&lt;br /&gt;
# For each input, look in the main chain and the transaction pool to find the referenced output transaction. If the output transaction is missing for any input, this will be an orphan transaction. Add to the orphan transactions, if a matching transaction is not in there already.&lt;br /&gt;
# For each input, if we are using the &#039;&#039;n&#039;&#039;th output of the earlier transaction, but it has fewer than n+1 outputs, reject this transaction&lt;br /&gt;
# For each input, if the referenced output transaction is coinbase (i.e. only 1 input, with hash=0, n=-1), it must have at least COINBASE_MATURITY confirmations; else reject this transaction&lt;br /&gt;
# Verify crypto signatures for each input; reject if any are bad&lt;br /&gt;
# For each input, if the referenced output has already been spent by a transaction in the main chain or the transaction pool, reject this transaction&lt;br /&gt;
# Using the referenced output transactions to get input values, check that each input value, as well as the sum, are in legal money range&lt;br /&gt;
# Reject if the sum of input values &amp;lt; sum of output values&lt;br /&gt;
# Reject if transaction fee (defined as sum of input values minus sum of output values) would be too low to get into an empty block&lt;br /&gt;
# Add to transaction pool&lt;br /&gt;
# Add to wallet if mine&lt;br /&gt;
# Relay transaction to peers&lt;br /&gt;
# For each orphan transaction that uses this one as one of its inputs, run all these steps (including this one) recursively on that orphan&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Technical]][[Category:Developer]]&lt;/div&gt;</summary>
		<author><name>Hal</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Protocol_rules&amp;diff=5401</id>
		<title>Protocol rules</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Protocol_rules&amp;diff=5401"/>
		<updated>2011-03-13T22:59:34Z</updated>

		<summary type="html">&lt;p&gt;Hal: Initially, about half the tx rules are done&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Rules for clients ==&lt;br /&gt;
&lt;br /&gt;
The wiki substantially documents the Bitcoin protocol, but equally important are the rules used by the client to process messages. It&#039;s crucial that clients follow certain rules in order to maintain consistency across the network, and to protect the Bitcoin security guarantees.&lt;br /&gt;
&lt;br /&gt;
Here, the focus is on handling tx and block messages, because that is the tricky logic. This will skip over the method of requesting and forwarding these messages for now, and describe what to do when they are received. Also, this will describe the minimal data structures in rather abstract terms, ignoring the client&#039;s various indexes, maps and hash tables used for efficiency. This will be a conceptual description. This is all based on a fairly literal reading of the source code.&lt;br /&gt;
&lt;br /&gt;
Mining (block generation) rules are not yet presented.&lt;br /&gt;
&lt;br /&gt;
=== Data structures ===&lt;br /&gt;
&lt;br /&gt;
The main data structures are &#039;&#039;transactions&#039;&#039; and &#039;&#039;blocks&#039;&#039;. Blocks are composed of the &#039;&#039;block header&#039;&#039; followed by transactions in the block. Transactions are identified by their hash; blocks by the hash of their header. Blocks have prev and next pointers that link them into a graph.&lt;br /&gt;
&lt;br /&gt;
Conceptually, the client has the following data structures:&lt;br /&gt;
&lt;br /&gt;
==== Transactions ====&lt;br /&gt;
&lt;br /&gt;
There are two collections of transactions:&lt;br /&gt;
&lt;br /&gt;
;transaction pool&lt;br /&gt;
: an unordered collection of transactions that are not in blocks in the main chain, but for which we have input transactions&lt;br /&gt;
&lt;br /&gt;
;orphan transactions&lt;br /&gt;
: transactions that can&#039;t go into the pool due to one or more missing input transactions&lt;br /&gt;
&lt;br /&gt;
==== Blocks ====&lt;br /&gt;
&lt;br /&gt;
There are 3 categories of blocks:&lt;br /&gt;
&lt;br /&gt;
;blocks in the main chain&lt;br /&gt;
: the transactions in these blocks are considered at least tentatively confirmed&lt;br /&gt;
&lt;br /&gt;
;blocks on side branches off the main chain&lt;br /&gt;
: these blocks have at least tentatively lost the race to be in the main chain&lt;br /&gt;
&lt;br /&gt;
;orphan blocks&lt;br /&gt;
: these are blocks which don&#039;t link into the main chain, normally because of a missing predecessor or nth-level predecessor&lt;br /&gt;
&lt;br /&gt;
Blocks in the first two categories form a tree rooted at the genesis block, linked by the prev pointer, which points toward the root. (It is a very linear tree with few and short branches off the main branch.) Following the next pointers from the root defines the main chain.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== &amp;quot;tx&amp;quot; messages ===&lt;br /&gt;
&lt;br /&gt;
These messages hold a single transaction.&lt;br /&gt;
&lt;br /&gt;
# Check syntactic correctness&lt;br /&gt;
# Make sure neither in or out lists are empty&lt;br /&gt;
# Size in bytes &amp;lt; MAX_BLOCK_SIZE&lt;br /&gt;
# Each output value, as well as the total, must be in legal money range&lt;br /&gt;
# Make sure none of the inputs have hash=0, n=-1 (&#039;&#039;coinbase&#039;&#039; transactions)&lt;br /&gt;
# Check that nLockTime &amp;lt;= INT_MAX, size in bytes &amp;gt;= 100, and sig opcount &amp;lt;= 2&lt;br /&gt;
# Reject &amp;quot;nonstandard&amp;quot; transactions: scriptSig doing anything other than pushing numbers on the stack, or scriptPubkey not matching the two usual forms&lt;br /&gt;
# Reject if we already have matching tx in the pool, or in a block in the main chain&lt;br /&gt;
# Reject if any other tx in the pool uses the same transaction output as one used by this tx.&lt;br /&gt;
# For each input, look in the main chain and the transaction pool to find the referenced output transaction. If the output transaction is missing for any input, this will be an orphan transaction. Add to the orphan transactions, if a matching transaction is not in there already.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Technical]][[Category:Developer]]&lt;/div&gt;</summary>
		<author><name>Hal</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Transaction&amp;diff=3325</id>
		<title>Transaction</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Transaction&amp;diff=3325"/>
		<updated>2011-02-09T19:06:42Z</updated>

		<summary type="html">&lt;p&gt;Hal: /* Verification */ Clarify that scripts are not concatenated&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;A transaction is a signed section of data that is broadcast to the [[network]] and collected into [[block|blocks]]. They reference a previous transaction and dedicate a certain number of bitcoins from it to a new public key (Bitcoin address). They are not encrypted (nothing in Bitcoin is encrypted).&lt;br /&gt;
&lt;br /&gt;
[[Block Explorer]] is a site where every transaction included within the block chain can be view/browsed.  This is useful for seeing the technical details of transaction in action, and for payment verification purposes.&lt;br /&gt;
&lt;br /&gt;
=== Example Bitcoin Transaction ===&lt;br /&gt;
&lt;br /&gt;
==== Data ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;Input:&lt;br /&gt;
Previous tx: f5d8ee39a430901c91a5917b9f2dc19d6d1a0e9cea205b009ca73dd04470b9a6&lt;br /&gt;
Index: 0&lt;br /&gt;
scriptSig: 304502206e21798a42fae0e854281abd38bacd1aeed3ee3738d9e1446618c4571d10&lt;br /&gt;
90db022100e2ac980643b0b82c0e88ffdfec6b64e3e6ba35e7ba5fdd7d5d6cc8d25c6b241501&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
Value: 5000000000&lt;br /&gt;
scriptPubKey: OP_DUP OP_HASH160 404371705fa9bd789a2fcd52d2c580b65d35549d&lt;br /&gt;
OP_EQUALVERIFY OP_CHECKSIG&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Explanation ====&lt;br /&gt;
&lt;br /&gt;
The input in this transaction imports 50 BTC from output #0 in transaction f5d8... Then the output sends 50 BTC to a Bitcoin address (expressed here in hexadecimal 4043... instead of the normal base58). When the recipient wants to spend this money, he will reference output #0 of this transaction in an input of his own transaction.&lt;br /&gt;
&lt;br /&gt;
===== Input =====&lt;br /&gt;
&lt;br /&gt;
An &#039;&#039;&#039;input&#039;&#039;&#039; is a reference to an output in a different transaction. Multiple inputs are often listed in a transaction. The values of the referenced outputs are added up, and the total is usable in the outputs of this transaction. &#039;&#039;&#039;Previous tx&#039;&#039;&#039; is a [[hash]] of a previous transaction. &#039;&#039;&#039;Index&#039;&#039;&#039; is the specific output in the referenced transaction. &#039;&#039;&#039;ScriptSig&#039;&#039;&#039; is the first half of a script (discussed in more detail later).&lt;br /&gt;
&lt;br /&gt;
The script contains two components, a signature and a public key. The public key belongs to the redeemer of the output transaction and proves the creator is allowed to redeem the outputs value. The other component is an ECDSA signature over a hash of a simplified version of the transaction. It, combined with the public key, proves the transaction was created by the real owner of the address in question. Various flags define how the transaction is simplified and can be used to create different types of payment.&lt;br /&gt;
&lt;br /&gt;
===== Output =====&lt;br /&gt;
&lt;br /&gt;
An &#039;&#039;&#039;output&#039;&#039;&#039; contains instructions for sending bitcoins. &#039;&#039;&#039;Value&#039;&#039;&#039; is the number of nanocoins that this output will be worth when claimed (1 bitcoin = 100,000,000 nanocoins). &#039;&#039;&#039;ScriptPubKey&#039;&#039;&#039; is the second half of a script (discussed later). There can be more than one output, and they share the combined value of the inputs. Because an output can only ever be referenced by a single input, the entire combined input value needs to be sent in an output if you don&#039;t want to lose it. If the input is worth 50 BTC but you only want to send 25 BTC, Bitcoin will create two outputs worth 25 BTC: one to the destination, and one back to you (known as &amp;quot;change&amp;quot;, though you send it to yourself). Any input bitcoins not redeemed in an output is considered a [[transaction fee]]; whoever generates the block will get it.&lt;br /&gt;
[[File:transaction.png|thumb|A sends 100 BTC to C and C generates 50 BTC. C sends 101 BTC to D, and he needs to send himself some change. D sends the 101 BTC to someone else, but they haven&#039;t redeemed it yet. Only D&#039;s output and B&#039;s change are capable of being spent in the current state.]]&lt;br /&gt;
&lt;br /&gt;
===== Verification =====&lt;br /&gt;
&lt;br /&gt;
To verify that inputs are authorized to collect the values of referenced outputs, Bitcoin uses a custom Forth-like [[script|scripting]] system. The input&#039;s scriptSig and the &#039;&#039;referenced&#039;&#039; output&#039;s scriptPubKey are evaluated (in that order), with scriptPubKey using the values left on the stack by scriptSig. The input is authorized if scriptPubKey returns true. Through the scripting system, the sender can create very complex conditions that people have to meet in order to claim the output&#039;s value. For example, it&#039;s possible to create an output that can be claimed by anyone without any authorization. It&#039;s also possible to require that an input be signed by ten different keys, or be redeemable with a password instead of a key.&lt;br /&gt;
&lt;br /&gt;
=== Types of Transaction ===&lt;br /&gt;
Bitcoin currently only creates three different scriptSig/scriptPubKey pairs, however. These are:&lt;br /&gt;
&lt;br /&gt;
==== Transfer to IP address ====&lt;br /&gt;
&lt;br /&gt;
 scriptPubKey: &amp;lt;pubKey&amp;gt; OP_CHECKSIG&lt;br /&gt;
 scriptSig: &amp;lt;sig&amp;gt;&lt;br /&gt;
The sender gets the public key when talking to the recipient over IP. When redeeming coins that have been sent to an IP address, the recipient provides only a signature. The signature is checked against the public key in scriptPubKey.&lt;br /&gt;
&lt;br /&gt;
Checking process:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; &lt;br /&gt;
|-&lt;br /&gt;
! Stack &lt;br /&gt;
! Script &lt;br /&gt;
! Description &lt;br /&gt;
|-&lt;br /&gt;
|Empty.&lt;br /&gt;
|&amp;lt;sig&amp;gt; &amp;lt;pubKey&amp;gt; OP_CHECKSIG&lt;br /&gt;
|scriptSig and scriptPubKey are combined.&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;sig&amp;gt; &amp;lt;pubKey&amp;gt;&lt;br /&gt;
| OP_CHECKSIG&lt;br /&gt;
|Constants are added to the stack.&lt;br /&gt;
|-&lt;br /&gt;
|true&lt;br /&gt;
|Empty.&lt;br /&gt;
|Signature is checked for top two stack items.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Transfer to Bitcoin address ====&lt;br /&gt;
&lt;br /&gt;
 scriptPubKey: OP_DUP OP_HASH160 &amp;lt;pubKeyHash&amp;gt; OP_EQUALVERIFY OP_CHECKSIG&lt;br /&gt;
 scriptSig: &amp;lt;sig&amp;gt; &amp;lt;pubKey&amp;gt;&lt;br /&gt;
A Bitcoin [[address]] is only a hash, so the sender can&#039;t provide a full public key in scriptPubKey. When redeeming coins that have been sent to a Bitcoin address, the recipient provides both the signature and the public key. The script verifies that the provided public key does hash to the hash in scriptPubKey, and then it also checks the signature against the public key.&lt;br /&gt;
&lt;br /&gt;
Checking process:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; &lt;br /&gt;
|-&lt;br /&gt;
! Stack &lt;br /&gt;
! Script &lt;br /&gt;
! Description &lt;br /&gt;
|-&lt;br /&gt;
|Empty.&lt;br /&gt;
| &amp;lt;sig&amp;gt; &amp;lt;pubKey&amp;gt; OP_DUP OP_HASH160 &amp;lt;pubKeyHash&amp;gt; OP_EQUALVERIFY OP_CHECKSIG &lt;br /&gt;
| scriptSig and scriptPubKey are combined.&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;sig&amp;gt; &amp;lt;pubKey&amp;gt;&lt;br /&gt;
| OP_DUP OP_HASH160 &amp;lt;pubKeyHash&amp;gt; OP_EQUALVERIFY OP_CHECKSIG &lt;br /&gt;
| Constants are added to the stack.&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;sig&amp;gt; &amp;lt;pubKey&amp;gt; &amp;lt;pubKey&amp;gt;&lt;br /&gt;
| OP_HASH160 &amp;lt;pubKeyHash&amp;gt; OP_EQUALVERIFY OP_CHECKSIG &lt;br /&gt;
| Top stack item is duplicated.&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;sig&amp;gt; &amp;lt;pubKey&amp;gt; &amp;lt;pubHashA&amp;gt;&lt;br /&gt;
|&amp;lt;pubKeyHash&amp;gt; OP_EQUALVERIFY OP_CHECKSIG&lt;br /&gt;
| Top stack item is hashed.&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;sig&amp;gt; &amp;lt;pubKey&amp;gt; &amp;lt;pubHashA&amp;gt; &amp;lt;pubKeyHash&amp;gt;&lt;br /&gt;
|OP_EQUALVERIFY OP_CHECKSIG&lt;br /&gt;
| Constant added.&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;sig&amp;gt; &amp;lt;pubKey&amp;gt;&lt;br /&gt;
|OP_CHECKSIG&lt;br /&gt;
| Equality is checked between the top two stack items.&lt;br /&gt;
|-&lt;br /&gt;
|true&lt;br /&gt;
|Empty.&lt;br /&gt;
|Signature is checked for top two stack items.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Generation ====&lt;br /&gt;
&lt;br /&gt;
Generations have a single input, and this input has a &amp;quot;coinbase&amp;quot; parameter instead of a scriptSig. The data in &amp;quot;coinbase&amp;quot; can be anything; it isn&#039;t used. Bitcoin puts the current compact-format [[target]] and the arbitrary-precision &amp;quot;extraNonce&amp;quot; number there, which increments every time the Nonce field in the [[block_hashing_algorithm|block header]] overflows. Outputs can be anything, but Bitcoin creates one exactly like an IP address transaction.&lt;br /&gt;
&lt;br /&gt;
[[Category:Technical]]&lt;br /&gt;
[[Category:Vocabulary]]&lt;/div&gt;</summary>
		<author><name>Hal</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=OP_CHECKSIG&amp;diff=2715</id>
		<title>OP CHECKSIG</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=OP_CHECKSIG&amp;diff=2715"/>
		<updated>2011-01-25T00:36:33Z</updated>

		<summary type="html">&lt;p&gt;Hal: Add SIGHASH_ALL, other minor changes&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;OP_CHECKSIG is used to verify that a signature for a tx output is valid&lt;br /&gt;
&lt;br /&gt;
== Parameters ==&lt;br /&gt;
&lt;br /&gt;
In addition to the script code itself, to operate OP_CHECKSIG needs to know the current transaction, the current transaction input, and the current hashtype (discussed later)&lt;br /&gt;
&lt;br /&gt;
== How it works ==&lt;br /&gt;
&lt;br /&gt;
# the public key and the signature are popped from the stack, in that order.&lt;br /&gt;
# A new subscript is created from the instruction from the most recent OP_CODESEPARATOR to the end of the script. If there is no OP_CODESEPARATOR the entire script becomes the subscript (hereby referred to as subScript)&lt;br /&gt;
# The sig is deleted from subScript.&lt;br /&gt;
# The hashtype is removed from the last byte of the sig and stored&lt;br /&gt;
# A deep copy is made of the current transaction (hereby referred to txCopy)&lt;br /&gt;
# All OP_CODESEPARATORS are removed from subScript&lt;br /&gt;
# The scripts for all transaction inputs in txCopy are set to empty scripts&lt;br /&gt;
# The script for the current transaction input in txCopy is set to subScript&lt;br /&gt;
&lt;br /&gt;
Now depending on the hashtype various things can happen to txCopy, these will be discussed individually&lt;br /&gt;
&lt;br /&gt;
=== Hashtype SIGHASH_ALL (default) ===&lt;br /&gt;
&lt;br /&gt;
No special handling occurs in the default case&lt;br /&gt;
&lt;br /&gt;
=== Hashtype SIGHASH_NONE ===&lt;br /&gt;
&lt;br /&gt;
# The output of txCopy is set to a vector of zero size.&lt;br /&gt;
# All other inputs aside from the current input in txCopy have their nSequence index set to zero&lt;br /&gt;
&lt;br /&gt;
=== Hashtype SIGHASH_SINGLE ===&lt;br /&gt;
&lt;br /&gt;
# The output of txCopy is resized to the size of the current input index+1&lt;br /&gt;
# All other txCopy outputs aside from the output that is the same as the current input index are set to a blank script and a value of (long) -1;&lt;br /&gt;
# All other txCopy inputs aside from the current input are set to have an nSequence index of zero&lt;br /&gt;
&lt;br /&gt;
=== Hashtype SIGHASH_ANYONECANPAY ===&lt;br /&gt;
&lt;br /&gt;
# The txCopy input vector is resized to a length of one&lt;br /&gt;
# The current input is set as the first and only member of this vector&lt;br /&gt;
&lt;br /&gt;
== Final signature ==&lt;br /&gt;
&lt;br /&gt;
An array of bytes is constructed from the serialized txCopy + four bytes for the hash type. This array is sha256 hashed twice, then the public key is used to to check the supplied signature against the hash. &lt;br /&gt;
&lt;br /&gt;
== Return values ==&lt;br /&gt;
&lt;br /&gt;
OP_CHECKSIG will push true to the stack if the check passed, false otherwise.&lt;br /&gt;
OP_CHECKSIG_VERIFY leaves nothing on the stack but will cause the script eval to fail immediately if the check does not pass.&lt;/div&gt;</summary>
		<author><name>Hal</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Protocol_documentation&amp;diff=1223</id>
		<title>Protocol documentation</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Protocol_documentation&amp;diff=1223"/>
		<updated>2011-01-03T23:56:19Z</updated>

		<summary type="html">&lt;p&gt;Hal: /* block */ Add transactions&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Sources:&lt;br /&gt;
* [[Original Bitcoin client]] source&lt;br /&gt;
* [http://www.bitcoin.org/wiki/doku.php?id=bitcoins_draft_spec_0_0_1 Draft spec on bitcoin wiki]&lt;br /&gt;
&lt;br /&gt;
Type names used in this documentation are from the C99 standard.&lt;br /&gt;
&lt;br /&gt;
== Common standards ==&lt;br /&gt;
&lt;br /&gt;
=== Hashes ===&lt;br /&gt;
&lt;br /&gt;
Usually, when a hash is computed within bitcoin, it is computed twice. Most of the time [http://en.wikipedia.org/wiki/SHA-2 SHA-256] hashes are used, however [http://en.wikipedia.org/wiki/RIPEMD RIPEMD-160] is also used when a shorter hash is desireable (for example when creating a bitcoin address).&lt;br /&gt;
&lt;br /&gt;
Example of double-SHA-256 encoding of string &amp;quot;hello&amp;quot;:&lt;br /&gt;
&lt;br /&gt;
 hello&lt;br /&gt;
 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 (first round of sha-256)&lt;br /&gt;
 9595c9df90075148eb06860365df33584b75bff782a510c6cd4883a419833d50 (second round of sha-256)&lt;br /&gt;
&lt;br /&gt;
For bitcoin addresses (RIPEMD-160) this would give:&lt;br /&gt;
&lt;br /&gt;
 hello&lt;br /&gt;
 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 (first round is sha-256)&lt;br /&gt;
 b6a9c8c230722b7c748331a8b450f05566dc7d0f (with ripemd-160)&lt;br /&gt;
&lt;br /&gt;
=== Signatures ===&lt;br /&gt;
&lt;br /&gt;
Bitcoin uses [http://en.wikipedia.org/wiki/Elliptic_curve_cryptography Elliptic Curve] [http://en.wikipedia.org/wiki/Digital_Signature_Algorithm Digital Signature Algorithm] (ECDSA) to sign transactions. &lt;br /&gt;
&lt;br /&gt;
For ECDSA the secp256k1 curve from http://www.secg.org/collateral/sec2_final.pdf is used.&lt;br /&gt;
Public keys (in scripts) are given as 04 &amp;lt;x&amp;gt; &amp;lt;y&amp;gt; where x and y are 32 byte strings representing the coordinates of a point on the curve.&lt;br /&gt;
&lt;br /&gt;
=== Transaction Verification ===&lt;br /&gt;
&lt;br /&gt;
The first transaction of a block is usually the generating transaction, which do not include any &amp;quot;in&amp;quot; transaction, and generate bitcoins (from fees for example) usually received by whoever solved the block containing this transaction.&lt;br /&gt;
Such transactions are called a &amp;quot;coinbase transaction&amp;quot; and are accepted by bitcoin clients without any need to execute scripts, provided there is only one per block.&lt;br /&gt;
&lt;br /&gt;
If a transaction is not a coinbase, it references previous transaction hashes as input, and the index of the other transaction&#039;s output used as input for this transaction.&lt;br /&gt;
The script from the in part of this transaction is executed.&lt;br /&gt;
Then the script from the out part of the referenced transaction is executed.&lt;br /&gt;
It is considered valid if the top element of the stack is true.&lt;br /&gt;
&lt;br /&gt;
=== Addresses ===&lt;br /&gt;
&lt;br /&gt;
A bitcoin address is in fact the hash of a ECDSA public key, computed this way:&lt;br /&gt;
&lt;br /&gt;
 Version = 1 byte of 0 (zero); on the test network, this is 1 byte of 111&lt;br /&gt;
 Key hash = Version concatenated with RIPEMD-160(SHA-256(public key))&lt;br /&gt;
 Checksum = 1st 4 bytes of SHA-256(SHA-256(Key hash))&lt;br /&gt;
 Bitcoin Address = Base58Encode(Key hash concatenated with Checksum)&lt;br /&gt;
&lt;br /&gt;
The Base58 encoding used is home made, and has some differences. Especially, leading zeroes are kept as single zeroes when conversion happens.&lt;br /&gt;
&lt;br /&gt;
== Common structures ==&lt;br /&gt;
&lt;br /&gt;
Almost all integers are encoded in little endian. Only IP or port number are encoded big endian.&lt;br /&gt;
&lt;br /&gt;
=== Message structure ===&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 4 || magic || uint32_t || Magic value indicating message origin network, and used to seek to next message when stream state is unknown&lt;br /&gt;
|-&lt;br /&gt;
| 12 || command || char[12] || ASCII string identifying the packet content, NULL padded (non-NULL padding results in packet rejected)&lt;br /&gt;
|-&lt;br /&gt;
| 4 || length || uint32_t || Length of payload&lt;br /&gt;
|-&lt;br /&gt;
| 4 || checksum || uint32_t || First 4 bytes of sha256(sha256(payload))&lt;br /&gt;
|-&lt;br /&gt;
| ? || payload || char[] || The actual data (can be empty, in which case checksum is excluded, and length is set to 0&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Known magic values:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Network !! Magic value&lt;br /&gt;
|-&lt;br /&gt;
| main || F9BEB4D9&lt;br /&gt;
|-&lt;br /&gt;
| testnet || FABFB5DA&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Variable length integer ===&lt;br /&gt;
&lt;br /&gt;
Integer can be encoded depending on the represented value to save space.&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Value !! Storage length !! Format&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt; 0xfd || 1 || uint8_t&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;= 0xffff || 3 || 0xfd + uint16_t&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;= 0xffffffff || 5 || 0xfe + uint32_t&lt;br /&gt;
|-&lt;br /&gt;
| - || 9 || 0xff + uint64_t&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Variable length string ===&lt;br /&gt;
&lt;br /&gt;
Variable length string can be stored using a variable length integer followed by the string itself.&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| ? || length || var_int || Length of the string&lt;br /&gt;
|-&lt;br /&gt;
| ? || string || char[] || The string itself (can be empty)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Network address ===&lt;br /&gt;
&lt;br /&gt;
When a network address is needed somewhere, this structure is used (currently only supports ipv4).&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 8 || services || uint64_t || ?&lt;br /&gt;
|-&lt;br /&gt;
| 12 || reserved || char[12] || Reserved for ipv6 support&lt;br /&gt;
|-&lt;br /&gt;
| 4 || ipv4 || uint32_t || IPv4 address, network byte order&lt;br /&gt;
|-&lt;br /&gt;
| 2 || port || uint16_t || port number, network byte order&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Inventory Vectors ===&lt;br /&gt;
&lt;br /&gt;
Inventory vectors are used for notifying other nodes about objects they have or data which is being requested.&lt;br /&gt;
&lt;br /&gt;
Inventory vectors consist of the following data format:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 4 || type || uint32_t || Identifies the object type linked to this inventory&lt;br /&gt;
|-&lt;br /&gt;
| 32 || hash || char[32] || Hash of the object&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The object type is currently defined as one of the following possibilities:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Value !! Name !! Description&lt;br /&gt;
|-&lt;br /&gt;
| 0 || ERROR || Any data of with this number may be ignored&lt;br /&gt;
|-&lt;br /&gt;
| 1 || MSG_TX || Hash is related to a transaction&lt;br /&gt;
|-&lt;br /&gt;
| 2 || MSG_BLOCK || Hash is related to a data block&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Other Data Type values are considered reserved for future implementations.&lt;br /&gt;
&lt;br /&gt;
== Message types ==&lt;br /&gt;
&lt;br /&gt;
=== version ===&lt;br /&gt;
&lt;br /&gt;
When a node receives an incoming connection, it will immediatly advertise its version. No futher communication is possible until both peers have exchanged their version.&lt;br /&gt;
&lt;br /&gt;
Payload:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 4 || version || uint32_t || Identifies protocol version being used by the node&lt;br /&gt;
|-&lt;br /&gt;
| 8 || services || uint64_t || bitfield of features to be enabled for this connection&lt;br /&gt;
|-&lt;br /&gt;
| 8 || timestamp || uint64_t || standard UNIX timestamp in seconds&lt;br /&gt;
|-&lt;br /&gt;
| 26 || addr_me || net_addr || The network address of the node emitting this message&lt;br /&gt;
|-&lt;br /&gt;
|colspan=&amp;quot;4&amp;quot;| version &amp;gt;= 106&lt;br /&gt;
|-&lt;br /&gt;
| 26 || addr_you || net_addr || The network address seen by the node emitting this message (ie, the address of the receiving node)&lt;br /&gt;
|-&lt;br /&gt;
| 8 || nonce || uint64_t || Node random unique id. This id is used to detect connections to self&lt;br /&gt;
|-&lt;br /&gt;
| ? || sub_version_num || var_str || Secondary Version information (null terminated?)&lt;br /&gt;
|-&lt;br /&gt;
|colspan=&amp;quot;4&amp;quot;| version &amp;gt;= 209&lt;br /&gt;
|-&lt;br /&gt;
| 4 || start_height || uint32_t || The last block received by the emitting node&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
If the emitter of the packet has version &amp;gt;= 209, a &amp;quot;verack&amp;quot; packet shall be sent if the version packet was accepted.&lt;br /&gt;
&lt;br /&gt;
The following services are currently assigned:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Value !! Name !! Description&lt;br /&gt;
|-&lt;br /&gt;
| 1 || NODE_NETWORK || This node can be asked for full blocks instead of just headers.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== verack ===&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;verack&#039;&#039; packet is sent in reply to &#039;&#039;version&#039;&#039; for clients &amp;gt;= 209.&lt;br /&gt;
&lt;br /&gt;
=== addr ===&lt;br /&gt;
&lt;br /&gt;
Provide informations on known nodes of the network. Non-advertised nodes should be forgotten after typically 3 hours&lt;br /&gt;
&lt;br /&gt;
Payload (maximum payload length: 1000 bytes):&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| ? || count || var_int || Number of address entries&lt;br /&gt;
|-&lt;br /&gt;
| 26x? || addr_list || net_addr[] || Address of other nodes on the network. version &amp;lt; 209 will only read the first one&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note&#039;&#039;&#039;: Starting version 31402, addresses are prefixed with a timestamp. If no timestamp is present, the addresses should not be relayed to other peers, unless it is indeed confirmed they are up.&lt;br /&gt;
&lt;br /&gt;
=== inv ===&lt;br /&gt;
&lt;br /&gt;
Allows a node to advertise its knowledge of one or more objects. It can be received unsolicited, or in reply to &#039;&#039;getblocks&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Payload (maximum payload length: 50000 bytes):&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| ? || count || var_int || Number of inventory entries&lt;br /&gt;
|-&lt;br /&gt;
| 36x? || inventory || inv_vect[] || Inventory vectors&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== getdata ===&lt;br /&gt;
&lt;br /&gt;
getdata is used in response to inv, to retrieve the content of a specific object, and is usually sent after receiving an &#039;&#039;inv&#039;&#039; packet, after filtering known elements.&lt;br /&gt;
&lt;br /&gt;
Payload (maximum payload length: 50000 bytes):&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| ? || count || var_int || Number of inventory entries&lt;br /&gt;
|-&lt;br /&gt;
| 36x? || inventory || inv_vect[] || Inventory vectors&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== getblocks ===&lt;br /&gt;
&lt;br /&gt;
Return an &#039;&#039;inv&#039;&#039; packet containing the list of blocks starting at hash_start, up to hash_stop or 500 blocks, whichever comes first. To receive the next blocks hashes, one needs to issue getblocks again with the last known hash.&lt;br /&gt;
&lt;br /&gt;
Payload:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 4 || version || uint32_t || only present if nType has SER_GETHASH set (purpose unknown)&lt;br /&gt;
|-&lt;br /&gt;
| 1+ || start count || variable length integer || number of hash_start entries&lt;br /&gt;
|-&lt;br /&gt;
| 32+ || hash_start || char[32] || hash of the last known block of the emitting node&lt;br /&gt;
|-&lt;br /&gt;
| 32 || hash_stop || char[32] || hash of the last desired block. Set to zero to get as many blocks as possible (500)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== tx ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;tx&#039;&#039; describes a bitcoin transaction, in reply to &#039;&#039;getdata&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 4 || version || uint32_t || Transaction data format version&lt;br /&gt;
|-&lt;br /&gt;
| 41+ || tx_in || tx_in[] || A list of 1 or more transaction inputs or sources for coins&lt;br /&gt;
|-&lt;br /&gt;
| 8+ || tx_out || tx_out[] || A list of 1 or more transaction outputs or destinations for coins&lt;br /&gt;
|-&lt;br /&gt;
| 4 || lock_time || uint32_t || The block number or timestamp at which this transaction is locked, or 0 if the transaction is always locked. A non-locked transaction must not be included in blocks, and it can be modified by broadcasting a new version before the time has expired (replacement is currently disabled in Bitcoin, however, so this is useless).&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
TxIn consists of the following fields:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 36 || previous_output || outpoint || The previous output transaction reference, as an OutPoint structure&lt;br /&gt;
|-&lt;br /&gt;
| ? || signature || script || Computational Script for confirming transaction authorization&lt;br /&gt;
|-&lt;br /&gt;
| 4 || sequence || uint32_t || Transaction version as defined by the sender. Intended for &amp;quot;replacement&amp;quot; of transactions when information is updated before inclusion into a block.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The OutPoint structure consists of the following fields:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 32 || hash || char[32] || The hash of the referenced transaction.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || index || uint32_t || The index of the specific output in the transaction. The first output is 0, etc.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Script structure consists of a series of pieces of information and operations related to the value of the transaction.&lt;br /&gt;
&lt;br /&gt;
(Structure to be expanded in the future… see script.h and script.cpp for more information)&lt;br /&gt;
&lt;br /&gt;
The TxOut structure consists of the following fields:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 8 || value || uint64_t || Transaction Value&lt;br /&gt;
|-&lt;br /&gt;
| ? || pk_script || script || Usually contains the public key as a Bitcoin script setting up conditions to claim this output.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== block ===&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;block&#039;&#039;&#039; message is sent in response to a getdata message which requests transaction information from a block hash.&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 4 || version || uint32_t || Block version information, based upon the software version creating this block&lt;br /&gt;
|-&lt;br /&gt;
| 32 || prev_block || char[32] || The hash value of the previous block this particular block references&lt;br /&gt;
|-&lt;br /&gt;
| 32 || merkle_root || char[32] || The reference to a Merkle tree collection which is a hash of all transactions related to this block&lt;br /&gt;
|-&lt;br /&gt;
| 4 || timestamp || uint32_t || A timestamp recording when this block was created (Limited to 2038!)&lt;br /&gt;
|-&lt;br /&gt;
| 4 || bits || uint32_t || The calculated difficulty target being used for this block&lt;br /&gt;
|-&lt;br /&gt;
| 4 || nonce || uint32_t || The nonce used to generate this block… to allow variations of the header and compute different hashes&lt;br /&gt;
|-&lt;br /&gt;
| ? || txn_count || var_int || Number of transaction entries&lt;br /&gt;
|-&lt;br /&gt;
| ? || txns || tx[] || Block transactions, in format of &amp;quot;tx&amp;quot; command&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== getaddr ===&lt;br /&gt;
&lt;br /&gt;
The getaddr message sends a request to a node asking for information about known active peers to help with identifying potential nodes in the network. The response to receiving this message is to transmit an addr message with one or more peers from a database of known active peers. The typical presumption is that a node is likely to be active if it has been sending a message within the last three hours.&lt;br /&gt;
&lt;br /&gt;
No additional data is transmitted with this message.&lt;br /&gt;
&lt;br /&gt;
=== checkorder ===&lt;br /&gt;
&lt;br /&gt;
This message is used for [[IP Transactions]], to ask the peer if it accepts such transactions and allow it to look at the content of the order.&lt;br /&gt;
&lt;br /&gt;
It contains a CWalletTx object&lt;br /&gt;
&lt;br /&gt;
Payload:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
|colspan=&amp;quot;4&amp;quot;| Fields from CMerkleTx&lt;br /&gt;
|-&lt;br /&gt;
| ? || hashBlock&lt;br /&gt;
|-&lt;br /&gt;
| ? || vMerkleBranch&lt;br /&gt;
|-&lt;br /&gt;
| ? || nIndex&lt;br /&gt;
|-&lt;br /&gt;
|colspan=&amp;quot;4&amp;quot;| Fields from CWalletTx&lt;br /&gt;
|-&lt;br /&gt;
| ? || vtxPrev&lt;br /&gt;
|-&lt;br /&gt;
| ? || mapValue&lt;br /&gt;
|-&lt;br /&gt;
| ? || vOrderForm&lt;br /&gt;
|-&lt;br /&gt;
| ? || fTimeReceivedIsTxTime&lt;br /&gt;
|-&lt;br /&gt;
| ? || nTimeReceived&lt;br /&gt;
|-&lt;br /&gt;
| ? || fFromMe&lt;br /&gt;
|-&lt;br /&gt;
| ? || fSpent&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== submitorder ===&lt;br /&gt;
&lt;br /&gt;
Confirms an order has been submitted. &lt;br /&gt;
&lt;br /&gt;
Payload:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 32 || hash || char[32] || Hash of the transaction&lt;br /&gt;
|-&lt;br /&gt;
| ? || wallet_entry || CWalletTx || Same payload as checkorder&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== reply ===&lt;br /&gt;
&lt;br /&gt;
Generic reply for [[IP Transactions]]&lt;br /&gt;
&lt;br /&gt;
Payload:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 4 || reply || uint32_t || reply code&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Possible values:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Value !! Name !! Description&lt;br /&gt;
|-&lt;br /&gt;
| 0 || SUCCESS || The IP Transaction can proceed (&#039;&#039;checkorder&#039;&#039;), or has been accepted (&#039;&#039;submitorder&#039;&#039;)&lt;br /&gt;
|-&lt;br /&gt;
| 1 || WALLET_ERROR || AcceptWalletTransaction() failed&lt;br /&gt;
|-&lt;br /&gt;
| 2 || DENIED || IP Transactions are not accepted by this node&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== ping ===&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;ping&#039;&#039; message is sent primarily to confirm that the TCP/IP connection is still valid. An error in transmission is presumed to be a closed connection and the address is removed as a current peer. No reply is expected as a result of this message being sent nor any sort of action expected on the part of a client when it is used.&lt;br /&gt;
&lt;br /&gt;
=== alert ===&lt;br /&gt;
&lt;br /&gt;
An &#039;&#039;&#039;alert&#039;&#039;&#039; is sent between nodes to send a general notification message throughout the network. If the alert can be confirmed with the signature as having come from the the core development group of the Bitcoin software, the message is suggested to be displayed for end-users. Attempts to perform transactions, particularly automated transactions through the client, are suggested to be halted. The text in the Message string should be relayed to log files and any user interfaces.&lt;br /&gt;
&lt;br /&gt;
Payload:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| ? || message || var_str || System message which is coded to convey some information to all nodes in the network&lt;br /&gt;
|-&lt;br /&gt;
| ? || signature || var_str || A signature which can be confirmed with a public key verifying that it is Satoshi (the originator of Bitcoins) who has &amp;quot;authorized&amp;quot; or created the message&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The signature is to be compared to this ECDSA public key:&lt;br /&gt;
&lt;br /&gt;
 04fc9702847840aaf195de8442ebecedf5b095cdbb9bc716bda9110971b28a49e0ead8564ff0db22209e0374782c093bb899692d524e9d6a6956e7c5ecbcd68284&lt;br /&gt;
 (hash) 1AGRxqDa5WjUKBwHB9XYEjmkv1ucoUUy1s&lt;br /&gt;
&lt;br /&gt;
Source: [http://www.bitcoin.org/smf/index.php?topic=898.0]&lt;br /&gt;
&lt;br /&gt;
== Scripting ==&lt;br /&gt;
&lt;br /&gt;
See [http://www.bitcoin.org/wiki/doku.php?id=script] (TODO)&lt;br /&gt;
&lt;br /&gt;
[[Category:Technical]]&lt;br /&gt;
[[Category:Developer]]&lt;/div&gt;</summary>
		<author><name>Hal</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Protocol_documentation&amp;diff=1222</id>
		<title>Protocol documentation</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Protocol_documentation&amp;diff=1222"/>
		<updated>2011-01-03T23:50:12Z</updated>

		<summary type="html">&lt;p&gt;Hal: /* addr */ Preceded by count&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Sources:&lt;br /&gt;
* [[Original Bitcoin client]] source&lt;br /&gt;
* [http://www.bitcoin.org/wiki/doku.php?id=bitcoins_draft_spec_0_0_1 Draft spec on bitcoin wiki]&lt;br /&gt;
&lt;br /&gt;
Type names used in this documentation are from the C99 standard.&lt;br /&gt;
&lt;br /&gt;
== Common standards ==&lt;br /&gt;
&lt;br /&gt;
=== Hashes ===&lt;br /&gt;
&lt;br /&gt;
Usually, when a hash is computed within bitcoin, it is computed twice. Most of the time [http://en.wikipedia.org/wiki/SHA-2 SHA-256] hashes are used, however [http://en.wikipedia.org/wiki/RIPEMD RIPEMD-160] is also used when a shorter hash is desireable (for example when creating a bitcoin address).&lt;br /&gt;
&lt;br /&gt;
Example of double-SHA-256 encoding of string &amp;quot;hello&amp;quot;:&lt;br /&gt;
&lt;br /&gt;
 hello&lt;br /&gt;
 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 (first round of sha-256)&lt;br /&gt;
 9595c9df90075148eb06860365df33584b75bff782a510c6cd4883a419833d50 (second round of sha-256)&lt;br /&gt;
&lt;br /&gt;
For bitcoin addresses (RIPEMD-160) this would give:&lt;br /&gt;
&lt;br /&gt;
 hello&lt;br /&gt;
 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 (first round is sha-256)&lt;br /&gt;
 b6a9c8c230722b7c748331a8b450f05566dc7d0f (with ripemd-160)&lt;br /&gt;
&lt;br /&gt;
=== Signatures ===&lt;br /&gt;
&lt;br /&gt;
Bitcoin uses [http://en.wikipedia.org/wiki/Elliptic_curve_cryptography Elliptic Curve] [http://en.wikipedia.org/wiki/Digital_Signature_Algorithm Digital Signature Algorithm] (ECDSA) to sign transactions. &lt;br /&gt;
&lt;br /&gt;
For ECDSA the secp256k1 curve from http://www.secg.org/collateral/sec2_final.pdf is used.&lt;br /&gt;
Public keys (in scripts) are given as 04 &amp;lt;x&amp;gt; &amp;lt;y&amp;gt; where x and y are 32 byte strings representing the coordinates of a point on the curve.&lt;br /&gt;
&lt;br /&gt;
=== Transaction Verification ===&lt;br /&gt;
&lt;br /&gt;
The first transaction of a block is usually the generating transaction, which do not include any &amp;quot;in&amp;quot; transaction, and generate bitcoins (from fees for example) usually received by whoever solved the block containing this transaction.&lt;br /&gt;
Such transactions are called a &amp;quot;coinbase transaction&amp;quot; and are accepted by bitcoin clients without any need to execute scripts, provided there is only one per block.&lt;br /&gt;
&lt;br /&gt;
If a transaction is not a coinbase, it references previous transaction hashes as input, and the index of the other transaction&#039;s output used as input for this transaction.&lt;br /&gt;
The script from the in part of this transaction is executed.&lt;br /&gt;
Then the script from the out part of the referenced transaction is executed.&lt;br /&gt;
It is considered valid if the top element of the stack is true.&lt;br /&gt;
&lt;br /&gt;
=== Addresses ===&lt;br /&gt;
&lt;br /&gt;
A bitcoin address is in fact the hash of a ECDSA public key, computed this way:&lt;br /&gt;
&lt;br /&gt;
 Version = 1 byte of 0 (zero); on the test network, this is 1 byte of 111&lt;br /&gt;
 Key hash = Version concatenated with RIPEMD-160(SHA-256(public key))&lt;br /&gt;
 Checksum = 1st 4 bytes of SHA-256(SHA-256(Key hash))&lt;br /&gt;
 Bitcoin Address = Base58Encode(Key hash concatenated with Checksum)&lt;br /&gt;
&lt;br /&gt;
The Base58 encoding used is home made, and has some differences. Especially, leading zeroes are kept as single zeroes when conversion happens.&lt;br /&gt;
&lt;br /&gt;
== Common structures ==&lt;br /&gt;
&lt;br /&gt;
Almost all integers are encoded in little endian. Only IP or port number are encoded big endian.&lt;br /&gt;
&lt;br /&gt;
=== Message structure ===&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 4 || magic || uint32_t || Magic value indicating message origin network, and used to seek to next message when stream state is unknown&lt;br /&gt;
|-&lt;br /&gt;
| 12 || command || char[12] || ASCII string identifying the packet content, NULL padded (non-NULL padding results in packet rejected)&lt;br /&gt;
|-&lt;br /&gt;
| 4 || length || uint32_t || Length of payload&lt;br /&gt;
|-&lt;br /&gt;
| 4 || checksum || uint32_t || First 4 bytes of sha256(sha256(payload))&lt;br /&gt;
|-&lt;br /&gt;
| ? || payload || char[] || The actual data (can be empty, in which case checksum is excluded, and length is set to 0&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Known magic values:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Network !! Magic value&lt;br /&gt;
|-&lt;br /&gt;
| main || F9BEB4D9&lt;br /&gt;
|-&lt;br /&gt;
| testnet || FABFB5DA&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Variable length integer ===&lt;br /&gt;
&lt;br /&gt;
Integer can be encoded depending on the represented value to save space.&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Value !! Storage length !! Format&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt; 0xfd || 1 || uint8_t&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;= 0xffff || 3 || 0xfd + uint16_t&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;= 0xffffffff || 5 || 0xfe + uint32_t&lt;br /&gt;
|-&lt;br /&gt;
| - || 9 || 0xff + uint64_t&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Variable length string ===&lt;br /&gt;
&lt;br /&gt;
Variable length string can be stored using a variable length integer followed by the string itself.&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| ? || length || var_int || Length of the string&lt;br /&gt;
|-&lt;br /&gt;
| ? || string || char[] || The string itself (can be empty)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Network address ===&lt;br /&gt;
&lt;br /&gt;
When a network address is needed somewhere, this structure is used (currently only supports ipv4).&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 8 || services || uint64_t || ?&lt;br /&gt;
|-&lt;br /&gt;
| 12 || reserved || char[12] || Reserved for ipv6 support&lt;br /&gt;
|-&lt;br /&gt;
| 4 || ipv4 || uint32_t || IPv4 address, network byte order&lt;br /&gt;
|-&lt;br /&gt;
| 2 || port || uint16_t || port number, network byte order&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Inventory Vectors ===&lt;br /&gt;
&lt;br /&gt;
Inventory vectors are used for notifying other nodes about objects they have or data which is being requested.&lt;br /&gt;
&lt;br /&gt;
Inventory vectors consist of the following data format:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 4 || type || uint32_t || Identifies the object type linked to this inventory&lt;br /&gt;
|-&lt;br /&gt;
| 32 || hash || char[32] || Hash of the object&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The object type is currently defined as one of the following possibilities:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Value !! Name !! Description&lt;br /&gt;
|-&lt;br /&gt;
| 0 || ERROR || Any data of with this number may be ignored&lt;br /&gt;
|-&lt;br /&gt;
| 1 || MSG_TX || Hash is related to a transaction&lt;br /&gt;
|-&lt;br /&gt;
| 2 || MSG_BLOCK || Hash is related to a data block&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Other Data Type values are considered reserved for future implementations.&lt;br /&gt;
&lt;br /&gt;
== Message types ==&lt;br /&gt;
&lt;br /&gt;
=== version ===&lt;br /&gt;
&lt;br /&gt;
When a node receives an incoming connection, it will immediatly advertise its version. No futher communication is possible until both peers have exchanged their version.&lt;br /&gt;
&lt;br /&gt;
Payload:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 4 || version || uint32_t || Identifies protocol version being used by the node&lt;br /&gt;
|-&lt;br /&gt;
| 8 || services || uint64_t || bitfield of features to be enabled for this connection&lt;br /&gt;
|-&lt;br /&gt;
| 8 || timestamp || uint64_t || standard UNIX timestamp in seconds&lt;br /&gt;
|-&lt;br /&gt;
| 26 || addr_me || net_addr || The network address of the node emitting this message&lt;br /&gt;
|-&lt;br /&gt;
|colspan=&amp;quot;4&amp;quot;| version &amp;gt;= 106&lt;br /&gt;
|-&lt;br /&gt;
| 26 || addr_you || net_addr || The network address seen by the node emitting this message (ie, the address of the receiving node)&lt;br /&gt;
|-&lt;br /&gt;
| 8 || nonce || uint64_t || Node random unique id. This id is used to detect connections to self&lt;br /&gt;
|-&lt;br /&gt;
| ? || sub_version_num || var_str || Secondary Version information (null terminated?)&lt;br /&gt;
|-&lt;br /&gt;
|colspan=&amp;quot;4&amp;quot;| version &amp;gt;= 209&lt;br /&gt;
|-&lt;br /&gt;
| 4 || start_height || uint32_t || The last block received by the emitting node&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
If the emitter of the packet has version &amp;gt;= 209, a &amp;quot;verack&amp;quot; packet shall be sent if the version packet was accepted.&lt;br /&gt;
&lt;br /&gt;
The following services are currently assigned:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Value !! Name !! Description&lt;br /&gt;
|-&lt;br /&gt;
| 1 || NODE_NETWORK || This node can be asked for full blocks instead of just headers.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== verack ===&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;verack&#039;&#039; packet is sent in reply to &#039;&#039;version&#039;&#039; for clients &amp;gt;= 209.&lt;br /&gt;
&lt;br /&gt;
=== addr ===&lt;br /&gt;
&lt;br /&gt;
Provide informations on known nodes of the network. Non-advertised nodes should be forgotten after typically 3 hours&lt;br /&gt;
&lt;br /&gt;
Payload (maximum payload length: 1000 bytes):&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| ? || count || var_int || Number of address entries&lt;br /&gt;
|-&lt;br /&gt;
| 26x? || addr_list || net_addr[] || Address of other nodes on the network. version &amp;lt; 209 will only read the first one&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note&#039;&#039;&#039;: Starting version 31402, addresses are prefixed with a timestamp. If no timestamp is present, the addresses should not be relayed to other peers, unless it is indeed confirmed they are up.&lt;br /&gt;
&lt;br /&gt;
=== inv ===&lt;br /&gt;
&lt;br /&gt;
Allows a node to advertise its knowledge of one or more objects. It can be received unsolicited, or in reply to &#039;&#039;getblocks&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Payload (maximum payload length: 50000 bytes):&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| ? || count || var_int || Number of inventory entries&lt;br /&gt;
|-&lt;br /&gt;
| 36x? || inventory || inv_vect[] || Inventory vectors&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== getdata ===&lt;br /&gt;
&lt;br /&gt;
getdata is used in response to inv, to retrieve the content of a specific object, and is usually sent after receiving an &#039;&#039;inv&#039;&#039; packet, after filtering known elements.&lt;br /&gt;
&lt;br /&gt;
Payload (maximum payload length: 50000 bytes):&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| ? || count || var_int || Number of inventory entries&lt;br /&gt;
|-&lt;br /&gt;
| 36x? || inventory || inv_vect[] || Inventory vectors&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== getblocks ===&lt;br /&gt;
&lt;br /&gt;
Return an &#039;&#039;inv&#039;&#039; packet containing the list of blocks starting at hash_start, up to hash_stop or 500 blocks, whichever comes first. To receive the next blocks hashes, one needs to issue getblocks again with the last known hash.&lt;br /&gt;
&lt;br /&gt;
Payload:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 4 || version || uint32_t || only present if nType has SER_GETHASH set (purpose unknown)&lt;br /&gt;
|-&lt;br /&gt;
| 1+ || start count || variable length integer || number of hash_start entries&lt;br /&gt;
|-&lt;br /&gt;
| 32+ || hash_start || char[32] || hash of the last known block of the emitting node&lt;br /&gt;
|-&lt;br /&gt;
| 32 || hash_stop || char[32] || hash of the last desired block. Set to zero to get as many blocks as possible (500)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== tx ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;tx&#039;&#039; describes a bitcoin transaction, in reply to &#039;&#039;getdata&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 4 || version || uint32_t || Transaction data format version&lt;br /&gt;
|-&lt;br /&gt;
| 41+ || tx_in || tx_in[] || A list of 1 or more transaction inputs or sources for coins&lt;br /&gt;
|-&lt;br /&gt;
| 8+ || tx_out || tx_out[] || A list of 1 or more transaction outputs or destinations for coins&lt;br /&gt;
|-&lt;br /&gt;
| 4 || lock_time || uint32_t || The block number or timestamp at which this transaction is locked, or 0 if the transaction is always locked. A non-locked transaction must not be included in blocks, and it can be modified by broadcasting a new version before the time has expired (replacement is currently disabled in Bitcoin, however, so this is useless).&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
TxIn consists of the following fields:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 36 || previous_output || outpoint || The previous output transaction reference, as an OutPoint structure&lt;br /&gt;
|-&lt;br /&gt;
| ? || signature || script || Computational Script for confirming transaction authorization&lt;br /&gt;
|-&lt;br /&gt;
| 4 || sequence || uint32_t || Transaction version as defined by the sender. Intended for &amp;quot;replacement&amp;quot; of transactions when information is updated before inclusion into a block.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The OutPoint structure consists of the following fields:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 32 || hash || char[32] || The hash of the referenced transaction.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || index || uint32_t || The index of the specific output in the transaction. The first output is 0, etc.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Script structure consists of a series of pieces of information and operations related to the value of the transaction.&lt;br /&gt;
&lt;br /&gt;
(Structure to be expanded in the future… see script.h and script.cpp for more information)&lt;br /&gt;
&lt;br /&gt;
The TxOut structure consists of the following fields:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 8 || value || uint64_t || Transaction Value&lt;br /&gt;
|-&lt;br /&gt;
| ? || pk_script || script || Usually contains the public key as a Bitcoin script setting up conditions to claim this output.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== block ===&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;block&#039;&#039;&#039; message is sent in response to a getdata message which requests transaction information from a block hash.&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 4 || version || uint32_t || Block version information, based upon the software version creating this block&lt;br /&gt;
|-&lt;br /&gt;
| 32 || prev_block || char[32] || The hash value of the previous block this particular block references&lt;br /&gt;
|-&lt;br /&gt;
| 32 || merkle_root || char[32] || The reference to a Merkle tree collection which is a hash of all transactions related to this block&lt;br /&gt;
|-&lt;br /&gt;
| 4 || timestamp || uint32_t || A timestamp recording when this block was created (Limited to 2038!)&lt;br /&gt;
|-&lt;br /&gt;
| 4 || bits || uint32_t || The calculated difficulty target being used for this block&lt;br /&gt;
|-&lt;br /&gt;
| 4 || nonce || uint32_t || The nonce used to generate this block… to allow variations of the header and compute different hashes&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== getaddr ===&lt;br /&gt;
&lt;br /&gt;
The getaddr message sends a request to a node asking for information about known active peers to help with identifying potential nodes in the network. The response to receiving this message is to transmit an addr message with one or more peers from a database of known active peers. The typical presumption is that a node is likely to be active if it has been sending a message within the last three hours.&lt;br /&gt;
&lt;br /&gt;
No additional data is transmitted with this message.&lt;br /&gt;
&lt;br /&gt;
=== checkorder ===&lt;br /&gt;
&lt;br /&gt;
This message is used for [[IP Transactions]], to ask the peer if it accepts such transactions and allow it to look at the content of the order.&lt;br /&gt;
&lt;br /&gt;
It contains a CWalletTx object&lt;br /&gt;
&lt;br /&gt;
Payload:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
|colspan=&amp;quot;4&amp;quot;| Fields from CMerkleTx&lt;br /&gt;
|-&lt;br /&gt;
| ? || hashBlock&lt;br /&gt;
|-&lt;br /&gt;
| ? || vMerkleBranch&lt;br /&gt;
|-&lt;br /&gt;
| ? || nIndex&lt;br /&gt;
|-&lt;br /&gt;
|colspan=&amp;quot;4&amp;quot;| Fields from CWalletTx&lt;br /&gt;
|-&lt;br /&gt;
| ? || vtxPrev&lt;br /&gt;
|-&lt;br /&gt;
| ? || mapValue&lt;br /&gt;
|-&lt;br /&gt;
| ? || vOrderForm&lt;br /&gt;
|-&lt;br /&gt;
| ? || fTimeReceivedIsTxTime&lt;br /&gt;
|-&lt;br /&gt;
| ? || nTimeReceived&lt;br /&gt;
|-&lt;br /&gt;
| ? || fFromMe&lt;br /&gt;
|-&lt;br /&gt;
| ? || fSpent&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== submitorder ===&lt;br /&gt;
&lt;br /&gt;
Confirms an order has been submitted. &lt;br /&gt;
&lt;br /&gt;
Payload:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 32 || hash || char[32] || Hash of the transaction&lt;br /&gt;
|-&lt;br /&gt;
| ? || wallet_entry || CWalletTx || Same payload as checkorder&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== reply ===&lt;br /&gt;
&lt;br /&gt;
Generic reply for [[IP Transactions]]&lt;br /&gt;
&lt;br /&gt;
Payload:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 4 || reply || uint32_t || reply code&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Possible values:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Value !! Name !! Description&lt;br /&gt;
|-&lt;br /&gt;
| 0 || SUCCESS || The IP Transaction can proceed (&#039;&#039;checkorder&#039;&#039;), or has been accepted (&#039;&#039;submitorder&#039;&#039;)&lt;br /&gt;
|-&lt;br /&gt;
| 1 || WALLET_ERROR || AcceptWalletTransaction() failed&lt;br /&gt;
|-&lt;br /&gt;
| 2 || DENIED || IP Transactions are not accepted by this node&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== ping ===&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;ping&#039;&#039; message is sent primarily to confirm that the TCP/IP connection is still valid. An error in transmission is presumed to be a closed connection and the address is removed as a current peer. No reply is expected as a result of this message being sent nor any sort of action expected on the part of a client when it is used.&lt;br /&gt;
&lt;br /&gt;
=== alert ===&lt;br /&gt;
&lt;br /&gt;
An &#039;&#039;&#039;alert&#039;&#039;&#039; is sent between nodes to send a general notification message throughout the network. If the alert can be confirmed with the signature as having come from the the core development group of the Bitcoin software, the message is suggested to be displayed for end-users. Attempts to perform transactions, particularly automated transactions through the client, are suggested to be halted. The text in the Message string should be relayed to log files and any user interfaces.&lt;br /&gt;
&lt;br /&gt;
Payload:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| ? || message || var_str || System message which is coded to convey some information to all nodes in the network&lt;br /&gt;
|-&lt;br /&gt;
| ? || signature || var_str || A signature which can be confirmed with a public key verifying that it is Satoshi (the originator of Bitcoins) who has &amp;quot;authorized&amp;quot; or created the message&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The signature is to be compared to this ECDSA public key:&lt;br /&gt;
&lt;br /&gt;
 04fc9702847840aaf195de8442ebecedf5b095cdbb9bc716bda9110971b28a49e0ead8564ff0db22209e0374782c093bb899692d524e9d6a6956e7c5ecbcd68284&lt;br /&gt;
 (hash) 1AGRxqDa5WjUKBwHB9XYEjmkv1ucoUUy1s&lt;br /&gt;
&lt;br /&gt;
Source: [http://www.bitcoin.org/smf/index.php?topic=898.0]&lt;br /&gt;
&lt;br /&gt;
== Scripting ==&lt;br /&gt;
&lt;br /&gt;
See [http://www.bitcoin.org/wiki/doku.php?id=script] (TODO)&lt;br /&gt;
&lt;br /&gt;
[[Category:Technical]]&lt;br /&gt;
[[Category:Developer]]&lt;/div&gt;</summary>
		<author><name>Hal</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Protocol_documentation&amp;diff=1099</id>
		<title>Protocol documentation</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Protocol_documentation&amp;diff=1099"/>
		<updated>2011-01-01T20:09:15Z</updated>

		<summary type="html">&lt;p&gt;Hal: /* Addresses */ Add version byte&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Sources:&lt;br /&gt;
* [[Original Bitcoin client]] source&lt;br /&gt;
* [http://www.bitcoin.org/wiki/doku.php?id=bitcoins_draft_spec_0_0_1 Draft spec on bitcoin wiki]&lt;br /&gt;
&lt;br /&gt;
Type names used in this documentation are from the C99 standard.&lt;br /&gt;
&lt;br /&gt;
== Common standards ==&lt;br /&gt;
&lt;br /&gt;
=== Hashes ===&lt;br /&gt;
&lt;br /&gt;
Usually, when a hash is computed within bitcoin, it is computed twice. Most of the time [http://en.wikipedia.org/wiki/SHA-2 SHA-256] hashes are used, however [http://en.wikipedia.org/wiki/RIPEMD RIPEMD-160] is also used when a shorter hash is desireable (for example when creating a bitcoin address).&lt;br /&gt;
&lt;br /&gt;
Example of double-SHA-256 encoding of string &amp;quot;hello&amp;quot;:&lt;br /&gt;
&lt;br /&gt;
 hello&lt;br /&gt;
 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 (first round of sha-256)&lt;br /&gt;
 9595c9df90075148eb06860365df33584b75bff782a510c6cd4883a419833d50 (second round of sha-256)&lt;br /&gt;
&lt;br /&gt;
For bitcoin addresses (RIPEMD-160) this would give:&lt;br /&gt;
&lt;br /&gt;
 hello&lt;br /&gt;
 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 (first round is sha-256)&lt;br /&gt;
 b6a9c8c230722b7c748331a8b450f05566dc7d0f (with ripemd-160)&lt;br /&gt;
&lt;br /&gt;
=== Signatures ===&lt;br /&gt;
&lt;br /&gt;
Bitcoin uses [http://en.wikipedia.org/wiki/Elliptic_curve_cryptography Elliptic Curve] [http://en.wikipedia.org/wiki/Digital_Signature_Algorithm Digital Signature Algorithm] (ECDSA) to sign transactions. &lt;br /&gt;
&lt;br /&gt;
For ECDSA the secp256k1 curve from http://www.secg.org/collateral/sec2_final.pdf is used.&lt;br /&gt;
Public keys (in scripts) are given as 04 &amp;lt;x&amp;gt; &amp;lt;y&amp;gt; where x and y are 32 byte strings representing the coordinates of a point on the curve.&lt;br /&gt;
&lt;br /&gt;
=== Transaction Verification ===&lt;br /&gt;
&lt;br /&gt;
The first transaction of a block is usually the generating transaction, which do not include any &amp;quot;in&amp;quot; transaction, and generate bitcoins (from fees for example) usually received by whoever solved the block containing this transaction.&lt;br /&gt;
Such transactions are called a &amp;quot;coinbase transaction&amp;quot; and are accepted by bitcoin clients without any need to execute scripts, provided there is only one per block.&lt;br /&gt;
&lt;br /&gt;
If a transaction is not a coinbase, it references previous transaction hashes as input, and the index of the other transaction&#039;s output used as input for this transaction.&lt;br /&gt;
The script from the in part of this transaction is executed.&lt;br /&gt;
Then the script from the out part of the referenced transaction is executed.&lt;br /&gt;
It is considered valid if the top element of the stack is true.&lt;br /&gt;
&lt;br /&gt;
=== Addresses ===&lt;br /&gt;
&lt;br /&gt;
A bitcoin address is in fact the hash of a ECDSA public key, computed this way:&lt;br /&gt;
&lt;br /&gt;
 Version = 1 byte of 0 (zero); on the test network, this is 1 byte of 111&lt;br /&gt;
 Key hash = Version concatenated with RIPEMD-160(SHA-256(public key))&lt;br /&gt;
 Checksum = 1st 4 bytes of SHA-256(SHA-256(Key hash))&lt;br /&gt;
 Bitcoin Address = Base58Encode(Key hash concatenated with Checksum)&lt;br /&gt;
&lt;br /&gt;
The Base58 encoding used is home made, and has some differences. Especially, leading zeroes are kept as single zeroes when conversion happens.&lt;br /&gt;
&lt;br /&gt;
== Common structures ==&lt;br /&gt;
&lt;br /&gt;
Almost all integers are encoded in little endian. Only IP or port number are encoded big endian.&lt;br /&gt;
&lt;br /&gt;
=== Message structure ===&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 4 || magic || uint32_t || Magic value indicating message origin network, and used to seek to next message when stream state is unknown&lt;br /&gt;
|-&lt;br /&gt;
| 12 || command || char[12] || ASCII string identifying the packet content, NULL padded (non-NULL padding results in packet rejected)&lt;br /&gt;
|-&lt;br /&gt;
| 4 || length || uint32_t || Length of payload&lt;br /&gt;
|-&lt;br /&gt;
| 4 || checksum || uint32_t || First 4 bytes of sha256(sha256(payload))&lt;br /&gt;
|-&lt;br /&gt;
| ? || payload || char[] || The actual data (can be empty, in which case checksum is excluded, and length is set to 0&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Known magic values:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Network !! Magic value&lt;br /&gt;
|-&lt;br /&gt;
| main || F9BEB4D9&lt;br /&gt;
|-&lt;br /&gt;
| testnet || FABFB5DA&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Variable length integer ===&lt;br /&gt;
&lt;br /&gt;
Integer can be encoded depending on the represented value to save space.&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Value !! Storage length !! Format&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt; 0xfd || 1 || uint8_t&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;= 0xffff || 3 || 0xfd + uint16_t&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;= 0xffffffff || 5 || 0xfe + uint32_t&lt;br /&gt;
|-&lt;br /&gt;
| - || 9 || 0xff + uint64_t&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Variable length string ===&lt;br /&gt;
&lt;br /&gt;
Variable length string can be stored using a variable length integer followed by the string itself.&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| ? || length || var_int || Length of the string&lt;br /&gt;
|-&lt;br /&gt;
| ? || string || char[] || The string itself (can be empty)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Network address ===&lt;br /&gt;
&lt;br /&gt;
When a network address is needed somewhere, this structure is used (currently only supports ipv4).&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 8 || services || uint64_t || ?&lt;br /&gt;
|-&lt;br /&gt;
| 12 || reserved || char[12] || Reserved for ipv6 support&lt;br /&gt;
|-&lt;br /&gt;
| 4 || ipv4 || uint32_t || IPv4 address, network byte order&lt;br /&gt;
|-&lt;br /&gt;
| 2 || port || uint16_t || port number, network byte order&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Inventory Vectors ===&lt;br /&gt;
&lt;br /&gt;
Inventory vectors are used for notifying other nodes about objects they have or data which is being requested.&lt;br /&gt;
&lt;br /&gt;
Inventory vectors consist of the following data format:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 4 || type || uint32_t || Identifies the object type linked to this inventory&lt;br /&gt;
|-&lt;br /&gt;
| 32 || hash || char[32] || Hash of the object&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The object type is currently defined as one of the following possibilities:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Value !! Name !! Description&lt;br /&gt;
|-&lt;br /&gt;
| 0 || ERROR || Any data of with this number may be ignored&lt;br /&gt;
|-&lt;br /&gt;
| 1 || MSG_TX || Hash is related to a transaction&lt;br /&gt;
|-&lt;br /&gt;
| 2 || MSG_BLOCK || Hash is related to a data block&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Other Data Type values are considered reserved for future implementations.&lt;br /&gt;
&lt;br /&gt;
== Message types ==&lt;br /&gt;
&lt;br /&gt;
=== version ===&lt;br /&gt;
&lt;br /&gt;
When a node receives an incoming connection, it will immediatly advertise its version. No futher communication is possible until both peers have exchanged their version.&lt;br /&gt;
&lt;br /&gt;
Payload:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 4 || version || uint32_t || Identifies protocol version being used by the node&lt;br /&gt;
|-&lt;br /&gt;
| 8 || services || uint64_t || bitfield of features to be enabled for this connection&lt;br /&gt;
|-&lt;br /&gt;
| 8 || timestamp || uint64_t || standard UNIX timestamp in seconds&lt;br /&gt;
|-&lt;br /&gt;
| 26 || addr_me || net_addr || The network address of the node emitting this message&lt;br /&gt;
|-&lt;br /&gt;
|colspan=&amp;quot;4&amp;quot;| version &amp;gt;= 106&lt;br /&gt;
|-&lt;br /&gt;
| 26 || addr_you || net_addr || The network address seen by the node emitting this message (ie, the address of the receiving node)&lt;br /&gt;
|-&lt;br /&gt;
| 8 || nonce || uint64_t || Node random unique id. This id is used to detect connections to self&lt;br /&gt;
|-&lt;br /&gt;
| ? || sub_version_num || var_str || Secondary Version information (null terminated?)&lt;br /&gt;
|-&lt;br /&gt;
|colspan=&amp;quot;4&amp;quot;| version &amp;gt;= 209&lt;br /&gt;
|-&lt;br /&gt;
| 4 || start_height || uint32_t || The last block received by the emitting node&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
If the emitter of the packet has version &amp;gt;= 209, a &amp;quot;verack&amp;quot; packet shall be sent if the version packet was accepted.&lt;br /&gt;
&lt;br /&gt;
The following services are currently assigned:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Value !! Name !! Description&lt;br /&gt;
|-&lt;br /&gt;
| 1 || NODE_NETWORK || This node can be asked for full blocks instead of just headers.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== verack ===&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;verack&#039;&#039; packet is sent in reply to &#039;&#039;version&#039;&#039; for clients &amp;gt;= 209.&lt;br /&gt;
&lt;br /&gt;
=== addr ===&lt;br /&gt;
&lt;br /&gt;
Provide informations on known nodes of the network. Non-advertised nodes should be forgotten after typically 3 hours&lt;br /&gt;
&lt;br /&gt;
Payload (maximum payload length: 1000 bytes):&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 26x? || addr_list || net_addr[] || Address of other nodes on the network. version &amp;lt; 209 will only read the first one&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note&#039;&#039;&#039;: Starting version 31402, addresses are prefixed with a timestamp. If no timestamp is present, the addresses should not be relayed to other peers, unless it is indeed confirmed they are up.&lt;br /&gt;
&lt;br /&gt;
=== inv ===&lt;br /&gt;
&lt;br /&gt;
Allows a node to advertise its knowledge of one or more objects. It can be received unsolicited, or in reply to &#039;&#039;getblocks&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Payload (maximum payload length: 50000 bytes):&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| ? || count || var_int || Number of inventory entries&lt;br /&gt;
|-&lt;br /&gt;
| 36x? || inventory || inv_vect[] || Inventory vectors&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== getdata ===&lt;br /&gt;
&lt;br /&gt;
getdata is used in response to inv, to retrieve the content of a specific object, and is usually sent after receiving an &#039;&#039;inv&#039;&#039; packet, after filtering known elements.&lt;br /&gt;
&lt;br /&gt;
Payload (maximum payload length: 50000 bytes):&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| ? || count || var_int || Number of inventory entries&lt;br /&gt;
|-&lt;br /&gt;
| 36x? || inventory || inv_vect[] || Inventory vectors&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== getblocks ===&lt;br /&gt;
&lt;br /&gt;
Return an &#039;&#039;inv&#039;&#039; packet containing the list of blocks starting at hash_start, up to hash_stop or 500 blocks, whichever comes first. To receive the next blocks hashes, one needs to issue getblocks again with the last known hash.&lt;br /&gt;
&lt;br /&gt;
Payload:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 4 || version || uint32_t || only present if nType has SER_GETHASH set (purpose unknown)&lt;br /&gt;
|-&lt;br /&gt;
| 1+ || start count || variable length integer || number of hash_start entries&lt;br /&gt;
|-&lt;br /&gt;
| 32+ || hash_start || char[32] || hash of the last known block of the emitting node&lt;br /&gt;
|-&lt;br /&gt;
| 32 || hash_stop || char[32] || hash of the last desired block. Set to zero to get as many blocks as possible (500)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== tx ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;tx&#039;&#039; describes a bitcoin transaction, in reply to &#039;&#039;getdata&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 4 || version || uint32_t || Transaction data format version&lt;br /&gt;
|-&lt;br /&gt;
| 41+ || tx_in || tx_in[] || A list of 1 or more transaction inputs or sources for coins&lt;br /&gt;
|-&lt;br /&gt;
| 8+ || tx_out || tx_out[] || A list of 1 or more transaction outputs or destinations for coins&lt;br /&gt;
|-&lt;br /&gt;
| 4 || lock_time || uint32_t || The block number or timestamp at which this transaction is locked, or 0 if the transaction is always locked. A non-locked transaction must not be included in blocks, and it can be modified by broadcasting a new version before the time has expired (replacement is currently disabled in Bitcoin, however, so this is useless).&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
TxIn consists of the following fields:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 36 || previous_output || outpoint || The previous output transaction reference, as an OutPoint structure&lt;br /&gt;
|-&lt;br /&gt;
| ? || signature || script || Computational Script for confirming transaction authorization&lt;br /&gt;
|-&lt;br /&gt;
| 4 || sequence || uint32_t || Transaction version as defined by the sender. Intended for &amp;quot;replacement&amp;quot; of transactions when information is updated before inclusion into a block.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The OutPoint structure consists of the following fields:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 32 || hash || char[32] || The hash of the referenced transaction.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || index || uint32_t || The index of the specific output in the transaction. The first output is 0, etc.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Script structure consists of a series of pieces of information and operations related to the value of the transaction.&lt;br /&gt;
&lt;br /&gt;
(Structure to be expanded in the future… see script.h and script.cpp for more information)&lt;br /&gt;
&lt;br /&gt;
The TxOut structure consists of the following fields:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 8 || value || uint64_t || Transaction Value&lt;br /&gt;
|-&lt;br /&gt;
| ? || pk_script || script || Usually contains the public key as a Bitcoin script setting up conditions to claim this output.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== block ===&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;block&#039;&#039;&#039; message is sent in response to a getdata message which requests transaction information from a block hash.&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 4 || version || uint32_t || Block version information, based upon the software version creating this block&lt;br /&gt;
|-&lt;br /&gt;
| 32 || prev_block || char[32] || The hash value of the previous block this particular block references&lt;br /&gt;
|-&lt;br /&gt;
| 32 || merkle_root || char[32] || The reference to a Merkle tree collection which is a hash of all transactions related to this block&lt;br /&gt;
|-&lt;br /&gt;
| 4 || timestamp || uint32_t || A timestamp recording when this block was created (Limited to 2038!)&lt;br /&gt;
|-&lt;br /&gt;
| 4 || bits || uint32_t || The calculated difficulty target being used for this block&lt;br /&gt;
|-&lt;br /&gt;
| 4 || nonce || uint32_t || The nonce used to generate this block… to allow variations of the header and compute different hashes&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== getaddr ===&lt;br /&gt;
&lt;br /&gt;
The getaddr message sends a request to a node asking for information about known active peers to help with identifying potential nodes in the network. The response to receiving this message is to transmit an addr message with one or more peers from a database of known active peers. The typical presumption is that a node is likely to be active if it has been sending a message within the last three hours.&lt;br /&gt;
&lt;br /&gt;
No additional data is transmitted with this message.&lt;br /&gt;
&lt;br /&gt;
=== checkorder ===&lt;br /&gt;
&lt;br /&gt;
This message is used for [[IP Transactions]], to ask the peer if it accepts such transactions and allow it to look at the content of the order.&lt;br /&gt;
&lt;br /&gt;
It contains a CWalletTx object&lt;br /&gt;
&lt;br /&gt;
Payload:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
|colspan=&amp;quot;4&amp;quot;| Fields from CMerkleTx&lt;br /&gt;
|-&lt;br /&gt;
| ? || hashBlock&lt;br /&gt;
|-&lt;br /&gt;
| ? || vMerkleBranch&lt;br /&gt;
|-&lt;br /&gt;
| ? || nIndex&lt;br /&gt;
|-&lt;br /&gt;
|colspan=&amp;quot;4&amp;quot;| Fields from CWalletTx&lt;br /&gt;
|-&lt;br /&gt;
| ? || vtxPrev&lt;br /&gt;
|-&lt;br /&gt;
| ? || mapValue&lt;br /&gt;
|-&lt;br /&gt;
| ? || vOrderForm&lt;br /&gt;
|-&lt;br /&gt;
| ? || fTimeReceivedIsTxTime&lt;br /&gt;
|-&lt;br /&gt;
| ? || nTimeReceived&lt;br /&gt;
|-&lt;br /&gt;
| ? || fFromMe&lt;br /&gt;
|-&lt;br /&gt;
| ? || fSpent&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== submitorder ===&lt;br /&gt;
&lt;br /&gt;
Confirms an order has been submitted. &lt;br /&gt;
&lt;br /&gt;
Payload:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 32 || hash || char[32] || Hash of the transaction&lt;br /&gt;
|-&lt;br /&gt;
| ? || wallet_entry || CWalletTx || Same payload as checkorder&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== reply ===&lt;br /&gt;
&lt;br /&gt;
Generic reply for [[IP Transactions]]&lt;br /&gt;
&lt;br /&gt;
Payload:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 4 || reply || uint32_t || reply code&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Possible values:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Value !! Name !! Description&lt;br /&gt;
|-&lt;br /&gt;
| 0 || SUCCESS || The IP Transaction can proceed (&#039;&#039;checkorder&#039;&#039;), or has been accepted (&#039;&#039;submitorder&#039;&#039;)&lt;br /&gt;
|-&lt;br /&gt;
| 1 || WALLET_ERROR || AcceptWalletTransaction() failed&lt;br /&gt;
|-&lt;br /&gt;
| 2 || DENIED || IP Transactions are not accepted by this node&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== ping ===&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;ping&#039;&#039; message is sent primarily to confirm that the TCP/IP connection is still valid. An error in transmission is presumed to be a closed connection and the address is removed as a current peer. No reply is expected as a result of this message being sent nor any sort of action expected on the part of a client when it is used.&lt;br /&gt;
&lt;br /&gt;
=== alert ===&lt;br /&gt;
&lt;br /&gt;
An &#039;&#039;&#039;alert&#039;&#039;&#039; is sent between nodes to send a general notification message throughout the network. If the alert can be confirmed with the signature as having come from the the core development group of the Bitcoin software, the message is suggested to be displayed for end-users. Attempts to perform transactions, particularly automated transactions through the client, are suggested to be halted. The text in the Message string should be relayed to log files and any user interfaces.&lt;br /&gt;
&lt;br /&gt;
Payload:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| ? || message || var_str || System message which is coded to convey some information to all nodes in the network&lt;br /&gt;
|-&lt;br /&gt;
| ? || signature || var_str || A signature which can be confirmed with a public key verifying that it is Satoshi (the originator of Bitcoins) who has &amp;quot;authorized&amp;quot; or created the message&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The signature is to be compared to this ECDSA public key:&lt;br /&gt;
&lt;br /&gt;
 04fc9702847840aaf195de8442ebecedf5b095cdbb9bc716bda9110971b28a49e0ead8564ff0db22209e0374782c093bb899692d524e9d6a6956e7c5ecbcd68284&lt;br /&gt;
 (hash) 1AGRxqDa5WjUKBwHB9XYEjmkv1ucoUUy1s&lt;br /&gt;
&lt;br /&gt;
Source: [http://www.bitcoin.org/smf/index.php?topic=898.0]&lt;br /&gt;
&lt;br /&gt;
== Scripting ==&lt;br /&gt;
&lt;br /&gt;
See [http://www.bitcoin.org/wiki/doku.php?id=script] (TODO)&lt;br /&gt;
&lt;br /&gt;
[[Category:Technical]]&lt;/div&gt;</summary>
		<author><name>Hal</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Protocol_documentation&amp;diff=1097</id>
		<title>Protocol documentation</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Protocol_documentation&amp;diff=1097"/>
		<updated>2011-01-01T19:54:32Z</updated>

		<summary type="html">&lt;p&gt;Hal: /* Addresses */ Describe checksum&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Sources:&lt;br /&gt;
* [[Original Bitcoin client]] source&lt;br /&gt;
* [http://www.bitcoin.org/wiki/doku.php?id=bitcoins_draft_spec_0_0_1 Draft spec on bitcoin wiki]&lt;br /&gt;
&lt;br /&gt;
Type names used in this documentation are from the C99 standard.&lt;br /&gt;
&lt;br /&gt;
== Common standards ==&lt;br /&gt;
&lt;br /&gt;
=== Hashes ===&lt;br /&gt;
&lt;br /&gt;
Usually, when a hash is computed within bitcoin, it is computed twice. Most of the time [http://en.wikipedia.org/wiki/SHA-2 SHA-256] hashes are used, however [http://en.wikipedia.org/wiki/RIPEMD RIPEMD-160] is also used when a shorter hash is desireable (for example when creating a bitcoin address).&lt;br /&gt;
&lt;br /&gt;
Example of double-SHA-256 encoding of string &amp;quot;hello&amp;quot;:&lt;br /&gt;
&lt;br /&gt;
 hello&lt;br /&gt;
 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 (first round of sha-256)&lt;br /&gt;
 9595c9df90075148eb06860365df33584b75bff782a510c6cd4883a419833d50 (second round of sha-256)&lt;br /&gt;
&lt;br /&gt;
For bitcoin addresses (RIPEMD-160) this would give:&lt;br /&gt;
&lt;br /&gt;
 hello&lt;br /&gt;
 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 (first round is sha-256)&lt;br /&gt;
 b6a9c8c230722b7c748331a8b450f05566dc7d0f (with ripemd-160)&lt;br /&gt;
&lt;br /&gt;
=== Signatures ===&lt;br /&gt;
&lt;br /&gt;
Bitcoin uses [http://en.wikipedia.org/wiki/Elliptic_curve_cryptography Elliptic Curve] [http://en.wikipedia.org/wiki/Digital_Signature_Algorithm Digital Signature Algorithm] (ECDSA) to sign transactions. &lt;br /&gt;
&lt;br /&gt;
For ECDSA the secp256k1 curve from http://www.secg.org/collateral/sec2_final.pdf is used.&lt;br /&gt;
Public keys (in scripts) are given as 04 &amp;lt;x&amp;gt; &amp;lt;y&amp;gt; where x and y are 32 byte strings representing the coordinates of a point on the curve.&lt;br /&gt;
&lt;br /&gt;
=== Transaction Verification ===&lt;br /&gt;
&lt;br /&gt;
The first transaction of a block is usually the generating transaction, which do not include any &amp;quot;in&amp;quot; transaction, and generate bitcoins (from fees for example) usually received by whoever solved the block containing this transaction.&lt;br /&gt;
Such transactions are called a &amp;quot;coinbase transaction&amp;quot; and are accepted by bitcoin clients without any need to execute scripts, provided there is only one per block.&lt;br /&gt;
&lt;br /&gt;
If a transaction is not a coinbase, it references previous transaction hashes as input, and the index of the other transaction&#039;s output used as input for this transaction.&lt;br /&gt;
The script from the in part of this transaction is executed.&lt;br /&gt;
Then the script from the out part of the referenced transaction is executed.&lt;br /&gt;
It is considered valid if the top element of the stack is true.&lt;br /&gt;
&lt;br /&gt;
=== Addresses ===&lt;br /&gt;
&lt;br /&gt;
A bitcoin address is in fact the hash of a ECDSA public key, computed this way:&lt;br /&gt;
&lt;br /&gt;
 Key hash = RIPEMD-160(SHA-256(public key))&lt;br /&gt;
 Checksum = 1st 4 bytes of SHA-256(SHA-256(Key hash))&lt;br /&gt;
 Bitcoin Address = Base58Encode(Key hash concatenated with Checksum)&lt;br /&gt;
&lt;br /&gt;
The Base58 encoding used is home made, and has some differences. Especially, leading zeroes are kept as single zeroes when conversion happens.&lt;br /&gt;
&lt;br /&gt;
== Common structures ==&lt;br /&gt;
&lt;br /&gt;
Almost all integers are encoded in little endian. Only IP or port number are encoded big endian.&lt;br /&gt;
&lt;br /&gt;
=== Message structure ===&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 4 || magic || uint32_t || Magic value indicating message origin network, and used to seek to next message when stream state is unknown&lt;br /&gt;
|-&lt;br /&gt;
| 12 || command || char[12] || ASCII string identifying the packet content, NULL padded (non-NULL padding results in packet rejected)&lt;br /&gt;
|-&lt;br /&gt;
| 4 || length || uint32_t || Length of payload&lt;br /&gt;
|-&lt;br /&gt;
| 4 || checksum || uint32_t || First 4 bytes of sha256(sha256(payload))&lt;br /&gt;
|-&lt;br /&gt;
| ? || payload || char[] || The actual data (can be empty, in which case checksum is excluded, and length is set to 0&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Known magic values:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Network !! Magic value&lt;br /&gt;
|-&lt;br /&gt;
| main || F9BEB4D9&lt;br /&gt;
|-&lt;br /&gt;
| testnet || FABFB5DA&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Variable length integer ===&lt;br /&gt;
&lt;br /&gt;
Integer can be encoded depending on the represented value to save space.&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Value !! Storage length !! Format&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt; 0xfd || 1 || uint8_t&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;= 0xffff || 3 || 0xfd + uint16_t&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;= 0xffffffff || 5 || 0xfe + uint32_t&lt;br /&gt;
|-&lt;br /&gt;
| - || 9 || 0xff + uint64_t&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Variable length string ===&lt;br /&gt;
&lt;br /&gt;
Variable length string can be stored using a variable length integer followed by the string itself.&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| ? || length || var_int || Length of the string&lt;br /&gt;
|-&lt;br /&gt;
| ? || string || char[] || The string itself (can be empty)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Network address ===&lt;br /&gt;
&lt;br /&gt;
When a network address is needed somewhere, this structure is used (currently only supports ipv4).&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 8 || services || uint64_t || ?&lt;br /&gt;
|-&lt;br /&gt;
| 12 || reserved || char[12] || Reserved for ipv6 support&lt;br /&gt;
|-&lt;br /&gt;
| 4 || ipv4 || uint32_t || IPv4 address, network byte order&lt;br /&gt;
|-&lt;br /&gt;
| 2 || port || uint16_t || port number, network byte order&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Inventory Vectors ===&lt;br /&gt;
&lt;br /&gt;
Inventory vectors are used for notifying other nodes about objects they have or data which is being requested.&lt;br /&gt;
&lt;br /&gt;
Inventory vectors consist of the following data format:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 4 || type || uint32_t || Identifies the object type linked to this inventory&lt;br /&gt;
|-&lt;br /&gt;
| 32 || hash || char[32] || Hash of the object&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The object type is currently defined as one of the following possibilities:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Value !! Name !! Description&lt;br /&gt;
|-&lt;br /&gt;
| 0 || ERROR || Any data of with this number may be ignored&lt;br /&gt;
|-&lt;br /&gt;
| 1 || MSG_TX || Hash is related to a transaction&lt;br /&gt;
|-&lt;br /&gt;
| 2 || MSG_BLOCK || Hash is related to a data block&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Other Data Type values are considered reserved for future implementations.&lt;br /&gt;
&lt;br /&gt;
== Message types ==&lt;br /&gt;
&lt;br /&gt;
=== version ===&lt;br /&gt;
&lt;br /&gt;
When a node receives an incoming connection, it will immediatly advertise its version. No futher communication is possible until both peers have exchanged their version.&lt;br /&gt;
&lt;br /&gt;
Payload:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 4 || version || uint32_t || Identifies protocol version being used by the node&lt;br /&gt;
|-&lt;br /&gt;
| 8 || services || uint64_t || bitfield of features to be enabled for this connection&lt;br /&gt;
|-&lt;br /&gt;
| 8 || timestamp || uint64_t || standard UNIX timestamp in seconds&lt;br /&gt;
|-&lt;br /&gt;
| 26 || addr_me || net_addr || The network address of the node emitting this message&lt;br /&gt;
|-&lt;br /&gt;
|colspan=&amp;quot;4&amp;quot;| version &amp;gt;= 106&lt;br /&gt;
|-&lt;br /&gt;
| 26 || addr_you || net_addr || The network address seen by the node emitting this message (ie, the address of the receiving node)&lt;br /&gt;
|-&lt;br /&gt;
| 8 || nonce || uint64_t || Node random unique id. This id is used to detect connections to self&lt;br /&gt;
|-&lt;br /&gt;
| ? || sub_version_num || var_str || Secondary Version information (null terminated?)&lt;br /&gt;
|-&lt;br /&gt;
|colspan=&amp;quot;4&amp;quot;| version &amp;gt;= 209&lt;br /&gt;
|-&lt;br /&gt;
| 4 || start_height || uint32_t || The last block received by the emitting node&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
If the emitter of the packet has version &amp;gt;= 209, a &amp;quot;verack&amp;quot; packet shall be sent if the version packet was accepted.&lt;br /&gt;
&lt;br /&gt;
The following services are currently assigned:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Value !! Name !! Description&lt;br /&gt;
|-&lt;br /&gt;
| 1 || NODE_NETWORK || This node can be asked for full blocks instead of just headers.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== verack ===&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;verack&#039;&#039; packet is sent in reply to &#039;&#039;version&#039;&#039; for clients &amp;gt;= 209.&lt;br /&gt;
&lt;br /&gt;
=== addr ===&lt;br /&gt;
&lt;br /&gt;
Provide informations on known nodes of the network. Non-advertised nodes should be forgotten after typically 3 hours&lt;br /&gt;
&lt;br /&gt;
Payload (maximum payload length: 1000 bytes):&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 26x? || addr_list || net_addr[] || Address of other nodes on the network. version &amp;lt; 209 will only read the first one&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note&#039;&#039;&#039;: Starting version 31402, addresses are prefixed with a timestamp. If no timestamp is present, the addresses should not be relayed to other peers, unless it is indeed confirmed they are up.&lt;br /&gt;
&lt;br /&gt;
=== inv ===&lt;br /&gt;
&lt;br /&gt;
Allows a node to advertise its knowledge of one or more objects. It can be received unsolicited, or in reply to &#039;&#039;getblocks&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Payload (maximum payload length: 50000 bytes):&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| ? || count || var_int || Number of inventory entries&lt;br /&gt;
|-&lt;br /&gt;
| 36x? || inventory || inv_vect[] || Inventory vectors&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== getdata ===&lt;br /&gt;
&lt;br /&gt;
getdata is used in response to inv, to retrieve the content of a specific object, and is usually sent after receiving an &#039;&#039;inv&#039;&#039; packet, after filtering known elements.&lt;br /&gt;
&lt;br /&gt;
Payload (maximum payload length: 50000 bytes):&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| ? || count || var_int || Number of inventory entries&lt;br /&gt;
|-&lt;br /&gt;
| 36x? || inventory || inv_vect[] || Inventory vectors&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== getblocks ===&lt;br /&gt;
&lt;br /&gt;
Return an &#039;&#039;inv&#039;&#039; packet containing the list of blocks starting at hash_start, up to hash_stop or 500 blocks, whichever comes first. To receive the next blocks hashes, one needs to issue getblocks again with the last known hash.&lt;br /&gt;
&lt;br /&gt;
Payload:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 4 || version || uint32_t || only present if nType has SER_GETHASH set (purpose unknown)&lt;br /&gt;
|-&lt;br /&gt;
| 1+ || start count || variable length integer || number of hash_start entries&lt;br /&gt;
|-&lt;br /&gt;
| 32+ || hash_start || char[32] || hash of the last known block of the emitting node&lt;br /&gt;
|-&lt;br /&gt;
| 32 || hash_stop || char[32] || hash of the last desired block. Set to zero to get as many blocks as possible (500)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== tx ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;tx&#039;&#039; describes a bitcoin transaction, in reply to &#039;&#039;getdata&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 4 || version || uint32_t || Transaction data format version&lt;br /&gt;
|-&lt;br /&gt;
| 41+ || tx_in || tx_in[] || A list of 1 or more transaction inputs or sources for coins&lt;br /&gt;
|-&lt;br /&gt;
| 8+ || tx_out || tx_out[] || A list of 1 or more transaction outputs or destinations for coins&lt;br /&gt;
|-&lt;br /&gt;
| 4 || lock_time || uint32_t || The block number or timestamp at which this transaction is locked, or 0 if the transaction is always locked. A non-locked transaction must not be included in blocks, and it can be modified by broadcasting a new version before the time has expired (replacement is currently disabled in Bitcoin, however, so this is useless).&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
TxIn consists of the following fields:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 36 || previous_output || outpoint || The previous output transaction reference, as an OutPoint structure&lt;br /&gt;
|-&lt;br /&gt;
| ? || signature || script || Computational Script for confirming transaction authorization&lt;br /&gt;
|-&lt;br /&gt;
| 4 || sequence || uint32_t || Transaction version as defined by the sender. Intended for &amp;quot;replacement&amp;quot; of transactions when information is updated before inclusion into a block.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The OutPoint structure consists of the following fields:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 32 || hash || char[32] || The hash of the referenced transaction.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || index || uint32_t || The index of the specific output in the transaction. The first output is 0, etc.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Script structure consists of a series of pieces of information and operations related to the value of the transaction.&lt;br /&gt;
&lt;br /&gt;
(Structure to be expanded in the future… see script.h and script.cpp for more information)&lt;br /&gt;
&lt;br /&gt;
The TxOut structure consists of the following fields:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 8 || value || uint64_t || Transaction Value&lt;br /&gt;
|-&lt;br /&gt;
| ? || pk_script || script || Usually contains the public key as a Bitcoin script setting up conditions to claim this output.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== block ===&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;block&#039;&#039;&#039; message is sent in response to a getdata message which requests transaction information from a block hash.&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 4 || version || uint32_t || Block version information, based upon the software version creating this block&lt;br /&gt;
|-&lt;br /&gt;
| 32 || prev_block || char[32] || The hash value of the previous block this particular block references&lt;br /&gt;
|-&lt;br /&gt;
| 32 || merkle_root || char[32] || The reference to a Merkle tree collection which is a hash of all transactions related to this block&lt;br /&gt;
|-&lt;br /&gt;
| 4 || timestamp || uint32_t || A timestamp recording when this block was created (Limited to 2038!)&lt;br /&gt;
|-&lt;br /&gt;
| 4 || bits || uint32_t || The calculated difficulty target being used for this block&lt;br /&gt;
|-&lt;br /&gt;
| 4 || nonce || uint32_t || The nonce used to generate this block… to allow variations of the header and compute different hashes&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== getaddr ===&lt;br /&gt;
&lt;br /&gt;
The getaddr message sends a request to a node asking for information about known active peers to help with identifying potential nodes in the network. The response to receiving this message is to transmit an addr message with one or more peers from a database of known active peers. The typical presumption is that a node is likely to be active if it has been sending a message within the last three hours.&lt;br /&gt;
&lt;br /&gt;
No additional data is transmitted with this message.&lt;br /&gt;
&lt;br /&gt;
=== checkorder ===&lt;br /&gt;
&lt;br /&gt;
This message is used for [[IP Transactions]], to ask the peer if it accepts such transactions and allow it to look at the content of the order.&lt;br /&gt;
&lt;br /&gt;
It contains a CWalletTx object&lt;br /&gt;
&lt;br /&gt;
Payload:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
|colspan=&amp;quot;4&amp;quot;| Fields from CMerkleTx&lt;br /&gt;
|-&lt;br /&gt;
| ? || hashBlock&lt;br /&gt;
|-&lt;br /&gt;
| ? || vMerkleBranch&lt;br /&gt;
|-&lt;br /&gt;
| ? || nIndex&lt;br /&gt;
|-&lt;br /&gt;
|colspan=&amp;quot;4&amp;quot;| Fields from CWalletTx&lt;br /&gt;
|-&lt;br /&gt;
| ? || vtxPrev&lt;br /&gt;
|-&lt;br /&gt;
| ? || mapValue&lt;br /&gt;
|-&lt;br /&gt;
| ? || vOrderForm&lt;br /&gt;
|-&lt;br /&gt;
| ? || fTimeReceivedIsTxTime&lt;br /&gt;
|-&lt;br /&gt;
| ? || nTimeReceived&lt;br /&gt;
|-&lt;br /&gt;
| ? || fFromMe&lt;br /&gt;
|-&lt;br /&gt;
| ? || fSpent&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== submitorder ===&lt;br /&gt;
&lt;br /&gt;
Confirms an order has been submitted. &lt;br /&gt;
&lt;br /&gt;
Payload:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 32 || hash || char[32] || Hash of the transaction&lt;br /&gt;
|-&lt;br /&gt;
| ? || wallet_entry || CWalletTx || Same payload as checkorder&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== reply ===&lt;br /&gt;
&lt;br /&gt;
Generic reply for [[IP Transactions]]&lt;br /&gt;
&lt;br /&gt;
Payload:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| 4 || reply || uint32_t || reply code&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Possible values:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Value !! Name !! Description&lt;br /&gt;
|-&lt;br /&gt;
| 0 || SUCCESS || The IP Transaction can proceed (&#039;&#039;checkorder&#039;&#039;), or has been accepted (&#039;&#039;submitorder&#039;&#039;)&lt;br /&gt;
|-&lt;br /&gt;
| 1 || WALLET_ERROR || AcceptWalletTransaction() failed&lt;br /&gt;
|-&lt;br /&gt;
| 2 || DENIED || IP Transactions are not accepted by this node&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== ping ===&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;ping&#039;&#039; message is sent primarily to confirm that the TCP/IP connection is still valid. An error in transmission is presumed to be a closed connection and the address is removed as a current peer. No reply is expected as a result of this message being sent nor any sort of action expected on the part of a client when it is used.&lt;br /&gt;
&lt;br /&gt;
=== alert ===&lt;br /&gt;
&lt;br /&gt;
An &#039;&#039;&#039;alert&#039;&#039;&#039; is sent between nodes to send a general notification message throughout the network. If the alert can be confirmed with the signature as having come from the the core development group of the Bitcoin software, the message is suggested to be displayed for end-users. Attempts to perform transactions, particularly automated transactions through the client, are suggested to be halted. The text in the Message string should be relayed to log files and any user interfaces.&lt;br /&gt;
&lt;br /&gt;
Payload:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Field Size !! Description !! Data type !! Comments&lt;br /&gt;
|-&lt;br /&gt;
| ? || message || var_str || System message which is coded to convey some information to all nodes in the network&lt;br /&gt;
|-&lt;br /&gt;
| ? || signature || var_str || A signature which can be confirmed with a public key verifying that it is Satoshi (the originator of Bitcoins) who has &amp;quot;authorized&amp;quot; or created the message&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The signature is to be compared to this ECDSA public key:&lt;br /&gt;
&lt;br /&gt;
 04fc9702847840aaf195de8442ebecedf5b095cdbb9bc716bda9110971b28a49e0ead8564ff0db22209e0374782c093bb899692d524e9d6a6956e7c5ecbcd68284&lt;br /&gt;
 (hash) 1AGRxqDa5WjUKBwHB9XYEjmkv1ucoUUy1s&lt;br /&gt;
&lt;br /&gt;
Source: [http://www.bitcoin.org/smf/index.php?topic=898.0]&lt;br /&gt;
&lt;br /&gt;
== Scripting ==&lt;br /&gt;
&lt;br /&gt;
See [http://www.bitcoin.org/wiki/doku.php?id=script] (TODO)&lt;br /&gt;
&lt;br /&gt;
[[Category:Technical]]&lt;/div&gt;</summary>
		<author><name>Hal</name></author>
	</entry>
</feed>