<?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=Rising</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=Rising"/>
	<link rel="alternate" type="text/html" href="https://en.bitcoin.it/wiki/Special:Contributions/Rising"/>
	<updated>2026-05-15T18:58:50Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.8</generator>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Block&amp;diff=35015</id>
		<title>Block</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Block&amp;diff=35015"/>
		<updated>2013-01-15T18:36:53Z</updated>

		<summary type="html">&lt;p&gt;Rising: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
Data is permanently recorded in the Bitcoin network through files called &#039;&#039;&#039;blocks&#039;&#039;&#039;.  A block is a record of some or all of the most recent Bitcoin transactions that have not yet been recorded in any prior blocks.  They could be thought of like the individual pages of a city recorder&#039;s recordbook (where changes to title to real estate are recorded) or a stock transaction ledger.  In all but a few exceptional cases, new blocks are added to the end of the record (known in Bitcoin as the [[block chain]]), and once written, are never changed or removed.  Each block memorializes what took place immediately before it was created.&lt;br /&gt;
===Block structure===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Field&lt;br /&gt;
! Description&lt;br /&gt;
! Size&lt;br /&gt;
|-&lt;br /&gt;
|Magic no&lt;br /&gt;
|value always 0xD9B4BEF9&lt;br /&gt;
|4 bytes&lt;br /&gt;
|-&lt;br /&gt;
|Blocksize&lt;br /&gt;
|number of bytes following up to end of block&lt;br /&gt;
|4 bytes&lt;br /&gt;
|-&lt;br /&gt;
|Blockheader&lt;br /&gt;
|[[Block hashing algorithm| consists of 6 items]]&lt;br /&gt;
|80 bytes&lt;br /&gt;
|-&lt;br /&gt;
|Transaction counter&lt;br /&gt;
| positive integer&lt;br /&gt;
| 1 - 9 bytes&lt;br /&gt;
|-&lt;br /&gt;
|[[transactions]]&lt;br /&gt;
|the (non empty) list of transactions&lt;br /&gt;
|&amp;lt;Transaction counter&amp;gt;-many transactions&lt;br /&gt;
|}&lt;br /&gt;
==Description==&lt;br /&gt;
Each block contains, among other things, in its block header a record of some or all recent [[transactions]], and a reference to the block that came immediately before it.  It also contains an answer to a difficult-to-solve mathematical puzzle - the answer to which is unique to each block.  New blocks can&#039;t be submitted to the network without the correct answer - the process of &amp;quot;[[Mining]]&amp;quot; is essentially the process of competing to be the next to find the answer that &amp;quot;solves&amp;quot; the current block.  The mathematical problem in each block is difficult to solve, but once a valid solution is found, it is very easy for the rest of the network to confirm that the solution is correct.  There are multiple valid solutions for any given block - only one of the solutions needs to be found for the block to be solved.&lt;br /&gt;
&lt;br /&gt;
Because there is a reward of brand new Bitcoins for solving each block, every block also contains a record of which [[Bitcoin address]] is entitled to receive the reward.  This record is known as a generation transaction, or a [[coinbase]] transaction, and is always the first transaction appearing in every block.  The number of [[Bitcoins]] generated per block starts at 50 and is halved every 210,000 blocks (about four years).&lt;br /&gt;
&lt;br /&gt;
Bitcoin transactions are broadcast to the [[network]] by the sender, and all peers trying to solve blocks collect the transaction records and add them to the block they&#039;re working to solve. &lt;br /&gt;
&lt;br /&gt;
The difficulty of the mathematical problem is automatically adjusted by the network, such that it targets a goal of solving an average of 6 blocks per hour.  Every 2016 blocks (about two weeks), all Bitcoin clients compare the actual number created with this goal and modify the target by the percentage that it varied.  This increases (or decreases) the difficulty of generating blocks.&lt;br /&gt;
&lt;br /&gt;
Because each block contains a reference to the prior block, the collection of all blocks in existence can be said to form a chain.  However, it&#039;s possible for the chain to have temporary splits - for example, if two miners arrive at two different valid solutions for the same block at the same time, unbeknownst to one another.  The peer-to-peer network is designed to resolve these splits within a short period of time, so that only one branch of the chain survives.&lt;br /&gt;
&lt;br /&gt;
The client accepts the &#039;longest&#039; chain of blocks as valid. The &#039;length&#039; of the entire [[block chain]] refers to the chain with the most combined difficulty, not the one with the most blocks. This prevents someone from forking the chain and creating a large number of low-difficulty blocks, and having it accepted by the network as &#039;longest&#039;.&lt;br /&gt;
&lt;br /&gt;
== Common Questions about Blocks ==&lt;br /&gt;
&lt;br /&gt;
=== How many blocks are there? ===&lt;br /&gt;
[http://blockexplorer.com/q/getblockcount Current block count]&lt;br /&gt;
&lt;br /&gt;
=== What is the maximum number of blocks? ===&lt;br /&gt;
There is no maximum number, blocks just keep getting added to the end of the chain at an average rate of one every 10 minutes.&lt;br /&gt;
&lt;br /&gt;
==== Even when all 21 million coins have been generated? ====&lt;br /&gt;
Yes. The blocks are for proving that transactions existed at a particular time. Transactions will still occur once all the coins have been generated, so blocks will still be created as long as people are trading Bitcoins.&lt;br /&gt;
&lt;br /&gt;
=== How long will it take me to generate a block? ===&lt;br /&gt;
No-one can say exactly. There is a [[Generation Calculator|generation calculator]] that will tell you how long it &#039;&#039;&#039;might&#039;&#039;&#039; take.&lt;br /&gt;
&lt;br /&gt;
=== What if I&#039;m 1% towards calculating a block and...? ===&lt;br /&gt;
There&#039;s no such thing as being 1% towards solving a block.  You don&#039;t make progress towards solving it.  After working on it for 24 hours, your chances of solving it are equal to what your chances were at the start or at any moment. Believing otherwise is what&#039;s known as the Gambler&#039;s fallacy [http://en.wikipedia.org/wiki/Gambler&#039;s_fallacy].&lt;br /&gt;
&lt;br /&gt;
It&#039;s like trying to flip 53 coins at once and have them all come up heads.  Each time you try, your chances of success are the same.&lt;br /&gt;
&lt;br /&gt;
=== Where can I find more technical detail? ===&lt;br /&gt;
There is more technical detail on the [[block hashing algorithm]] page.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[https://bitcointalk.org/index.php?topic=101514.0 Format of the BDB-style block files]&lt;br /&gt;
* [http://en.bitcoin.it/wiki/File:Total_bitcoins_over_time_graph.png Total Bitcoins Over Time]&lt;br /&gt;
&lt;br /&gt;
[[Category:Technical]]&lt;br /&gt;
[[Category:Vocabulary]]&lt;br /&gt;
&lt;br /&gt;
[[fr:Blocs]][[pl:Bloki]][[zh-cn:Block]]&lt;br /&gt;
[[de:Block]]&lt;/div&gt;</summary>
		<author><name>Rising</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Irreversible_Transactions&amp;diff=34180</id>
		<title>Irreversible Transactions</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Irreversible_Transactions&amp;diff=34180"/>
		<updated>2012-12-28T15:52:31Z</updated>

		<summary type="html">&lt;p&gt;Rising: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Double-spending is the result of successfully spending some money more than once.  Bitcoin protects against double spending by verifying each transaction added to the [[block chain]] to ensure that the inputs for the transaction had not previously already been spent.&lt;br /&gt;
&lt;br /&gt;
Other electronic systems prevent double-spending by having a master authoritative source that follows business rules for authorizing each transaction.  Bitcoin uses a decentralized system, where a consensus among nodes following the same protocol is substituted for a central authority.  &lt;br /&gt;
&lt;br /&gt;
Bitcoin has some exposure to fraudulent double-spending when a transaction is first made, with less and less risk as a transaction gains [[confirmation|confirmations]].&lt;br /&gt;
&lt;br /&gt;
==Attack vectors==&lt;br /&gt;
&lt;br /&gt;
===Race attack===&lt;br /&gt;
&lt;br /&gt;
Traders and merchants who accept a payment immediately on seeing &amp;quot;0/unconfirmed&amp;quot; are exposed to a double-spend occurring is there was a fraudulent attempt that successfully communicated one transaction to the merchant yet communicated a different transaction that spends the same coin that was first to eventually make it into the block chain.&lt;br /&gt;
&lt;br /&gt;
Merchants can take precautions (e.g., disable incoming connections, only connect to well connected nodes) to lessen the risk of a race attack but the risk cannot be eliminated.  Therefore, the cost/benefit of the risk needs to be considered when accepting payment on 0/unconfirmed when there is no recourse against the attacker.&lt;br /&gt;
&lt;br /&gt;
The [[research]] paper [http://eprint.iacr.org/2012/248.pdf Two Bitcoins at the Price of One] finds that the protocol allows a high degree of success by an attacker in performing race attacks.  The method studied in the research paper depends on access to the merchant&#039;s Bitcoin node which is why that even prior to this paper, recommendations for merchants include disabling incoming connections and to choose specific outgoing connections&amp;lt;ref&amp;gt;[http://bitcointalk.org/index.php?topic=79090.msg881283#msg881283 BitcoinTalk Thread - Two Bitcoins at the Price of One]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Finney attack===&lt;br /&gt;
&lt;br /&gt;
Another attack the trader or merchant is exposed to when accepting payment on 0/unconfirmed.  The Finney attack is a fraudulent double-spend that requires the participation of a miner once a block has been mined&amp;lt;ref&amp;gt;[http://www.bitcointalk.org/index.php?topic=3441.msg48384#msg48384 Best practice for fast transaction acceptance - how high is the risk?]&amp;lt;/ref&amp;gt;.  The risk of a Finney attack cannot be eliminated regardless of the precautions taken by the merchant, but the participation of a miner is required and a specific sequence of events must occur.  Thus the attack is not trivial nor inexpensive to perform and only makes sense for the attacker when the gains from the attack are significant.  Just like with the race attack, a trader or merchant should consider the cost / benefit when accepting payment on just one confirmation when there is no recourse against the attacker.&lt;br /&gt;
&lt;br /&gt;
===Vector76 attack===&lt;br /&gt;
&lt;br /&gt;
Also referred to as a one-confirmation attack, is a combination of the race attack and the Finney attack such that a transaction that even has one confirmation can still be double-spent.  The same protective action for the race attack (no incoming connections, explicit outgoing connection to a well-connected node) significantly reduces the risk of this occurring.&lt;br /&gt;
&lt;br /&gt;
===Brute force attack===&lt;br /&gt;
&lt;br /&gt;
This attack has a chance to work even if the merchant waits for some confirmations, but requires relatively high hashrate.&lt;br /&gt;
&lt;br /&gt;
The attacker submits to the merchant/network a transaction which pays the merchant, while privately mining a blockchain fork in which a double-spending transaction is included instead. After waiting for &#039;&#039;n&#039;&#039; confirmations, the merchant sends the product. If the attacker happened to find more than &#039;&#039;n&#039;&#039; blocks at this point, he releases his fork and regains his coins; otherwise, he can try to continue extending his fork with the hope of being able to catch up with the network. If he never manages to do this, the attack fails and the payment to the merchant will go through.&lt;br /&gt;
&lt;br /&gt;
The probability of success is a function of the attacker&#039;s hashrate (as a proportion of the total network hashrate) and the number of confirmations the merchant waits for. For example, if the attacker controls 10% of the network hashrate but the merchant waits for 6 confirmations, the success probability is on the order of 0.1%.&lt;br /&gt;
&lt;br /&gt;
===&amp;gt;50% attack===&lt;br /&gt;
&lt;br /&gt;
If the attacker controls more than half of the network hashrate, the previous attack has a probability of 100% to succeed. Since the attacker can generate blocks faster than the rest of the network, he can simply persevere with his private fork until it becomes longer than the branch built by the honest network, from whatever disadvantage.&lt;br /&gt;
&lt;br /&gt;
No amount of confirmations can prevent this attack; however, waiting for confirmations does increase the aggregate resource cost of performing the attack, which could make it unprofitable or delay it long enough for the circumstances to change or slower-acting synchronization methods to kick in.&lt;br /&gt;
&lt;br /&gt;
==Risk management==&lt;br /&gt;
&lt;br /&gt;
There are third-party services to assist traders and merchants to help manage the risk or to insure against losses.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
* [[Weaknesses]]&lt;br /&gt;
* [[How bitcoin works]]&lt;br /&gt;
* [http://codinginmysleep.com/bitcoin-attacks-in-plain-english Bitcoin Attacks in Plain English] by David Perry&lt;br /&gt;
&lt;br /&gt;
[[de:Doppelausgaben]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Technical]]&lt;/div&gt;</summary>
		<author><name>Rising</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Irreversible_Transactions&amp;diff=34179</id>
		<title>Irreversible Transactions</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Irreversible_Transactions&amp;diff=34179"/>
		<updated>2012-12-28T15:52:22Z</updated>

		<summary type="html">&lt;p&gt;Rising: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Double-spending is the result of successfully spending some money more than once.  Bitcoin protects against double spending by verifying each transaction added to the [[block chain]] to ensure that the inputs for the transaction had not previously already been spent.&lt;br /&gt;
&lt;br /&gt;
Other electronic systems prevent double-spending by having a master authoritative source that follows business rules for authorizing each transaction.  Bitcoin uses a decentralized system, where a consensus among nodes following the same protocol is substituted for a central authority.  &lt;br /&gt;
&lt;br /&gt;
Bitcoin has some exposure to fraudulent double-spending when a transaction is first made, with less and less risk as a transaction gains [[confirmation|confirmations]].&lt;br /&gt;
&lt;br /&gt;
==Attack vectors==&lt;br /&gt;
&lt;br /&gt;
===Race attack===&lt;br /&gt;
&lt;br /&gt;
Traders and merchants who accept a payment immediately on seeing &amp;quot;0/unconfirmed&amp;quot; are exposed to a double-spend occurring is there was a fraudulent attempt that successfully communicated one transaction to the merchant yet communicated a different transaction that spends the same coin that was first to eventually make it into the block chain.&lt;br /&gt;
&lt;br /&gt;
Merchants can take precautions (e.g., disable incoming connections, only connect to well connected nodes) to lessen the risk of a race attack but the risk cannot be eliminated.  Therefore, the cost/benefit of the risk needs to be considered when accepting payment on 0/unconfirmed when there is no recourse against the attacker.&lt;br /&gt;
&lt;br /&gt;
The [[research]] paper [http://eprint.iacr.org/2012/248.pdf Two Bitcoins at the Price of One] finds that the protocol allows a high degree of success by an attacker in performing race attacks.  The method studied in the research paper depends on access to the merchant&#039;s Bitcoin node which is why that even prior to this paper, recommendations for merchants include disabling incoming connections and to choose specific outgoing connections&amp;lt;ref&amp;gt;[http://bitcointalk.org/index.php?topic=79090.msg881283#msg881283 BitcoinTalk Thread - Two Bitcoins at the Price of One]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Finney attack===&lt;br /&gt;
&lt;br /&gt;
Another attack the trader or merchant is exposed to when accepting payment on 0/unconfirmed.  The Finney attack is a fraudulent double-spend that requires the participation of a miner once a block has been mined&amp;lt;ref&amp;gt;[http://www.bitcointalk.org/index.php?topic=3441.msg48384#msg48384 Best practice for fast transaction acceptance - how high is the risk?]&amp;lt;/ref&amp;gt;.  The risk of a Finney attack cannot be eliminated regardless of the precautions taken by the merchant, but the participation of a miner is required and a specific sequence of events must occur.  Thus the attack is not trivial nor inexpensive to perform and only makes sense for the attacker when the gains from the attack are significant.  Just like with the race attack, a trader or merchant should consider the cost / benefit when accepting payment on just one confirmation when there is no recourse against the attacker.&lt;br /&gt;
&lt;br /&gt;
===Vector76 attack===&lt;br /&gt;
&lt;br /&gt;
Also referred to as a one-confirmation attack, is a combination of the race attack and the Finney attack such that a transaction that even has one confirmation can still be double-spent.  The same protective action for the race attack (no incoming connections, explicit outgoing connection to a well-connected node) significantly reduces the risk of this occurring.&lt;br /&gt;
&lt;br /&gt;
===Brute force attack===&lt;br /&gt;
&lt;br /&gt;
This attack has a chance to work even if the merchant waits for some confirmations, but requires relatively high hashrate.&lt;br /&gt;
&lt;br /&gt;
The attacker submits to the merchant/network a transaction which pays the merchant, while privately mining a blockchain fork in which a double-spending transaction is included instead. After waiting for &#039;&#039;n&#039;&#039; confirmations, the merchant sends the product. If the attacker happened to find more than &#039;&#039;n&#039;&#039; blocks at this point, he releases his fork and regains his coins; otherwise, he can try to continue extending his fork with the hope of being able to catch up with the network. If he never manages to do this, the attack fails and the payment to the merchant will go through.&lt;br /&gt;
&lt;br /&gt;
The probability of success is a function of the attacker&#039;s hashrate (as a proportion of the total network hashrate) and the number of confirmations the merchant waits for. For example, if the attacker controls 10% of the network hashrate but the merchant waits for 6 confirmations, the success probability is on the order of 0.1%.&lt;br /&gt;
&lt;br /&gt;
===&amp;gt;50% attack===&lt;br /&gt;
&lt;br /&gt;
If the attacker controls more than half of the network hashrate, the previous attack has a probability of 100% to succeed. Since the attacker can generate blocks faster than the rest of the network, he can simply persevere with his private fork until it becomes longer than the branch built by the honest network, from whatever disadvantage.&lt;br /&gt;
&lt;br /&gt;
No amount of confirmations can prevent this attack; however, waiting for confirmations does increase the aggregate resource cost of performing the attack, which could make it unprofitable or delay it long enough for the circumstances to change or slower-acting synchronization methods to kick in.&lt;br /&gt;
&lt;br /&gt;
==Risk management==&lt;br /&gt;
&lt;br /&gt;
There are third-party services to assist traders and merchants to help manage the risk or to insure against losses.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
* [[Weaknesses]]&lt;br /&gt;
* [[How bitcoin works]]&lt;br /&gt;
* [http://codinginmysleep.com/bitcoin-attacks-in-plain-english Bitcoin Attacks in Plain English] by David Perry&lt;br /&gt;
&lt;br /&gt;
[[en:Doppelausgaben]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Technical]]&lt;/div&gt;</summary>
		<author><name>Rising</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Lazy_API&amp;diff=34178</id>
		<title>Lazy API</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Lazy_API&amp;diff=34178"/>
		<updated>2012-12-28T15:41:17Z</updated>

		<summary type="html">&lt;p&gt;Rising: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;For the incredibly lazy and/or incompetent web developer, present is 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;
==Problem==&lt;br /&gt;
&lt;br /&gt;
Lazy web designer wants to use bitcoins without dealing with installing bitcoin on a server, installing a shopping cart interface, or using ugly merchant services with callbacks.&lt;br /&gt;
&lt;br /&gt;
==Solution for sending bitcoins==&lt;br /&gt;
&lt;br /&gt;
Use the [https://mtgox.com/support/tradeAPI MtGox API]&lt;br /&gt;
&lt;br /&gt;
==Solution for receiving bitcoins==&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 (you can choose a number higher than 1 if you are worried about double-spending)&lt;br /&gt;
# Check blockexplorer to see if they sent the right amount (i.e. http://blockexplorer.com/q/getreceivedbyaddress/19hMEAaRMbEhfSkeU4GT8mgSuyR4t4M6TH/1) - the /1 is the number of confirmations you require&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 by getting a new address via API call from one of the wallet services that support this feature (e.g. https://instawallet.org/static/api).&lt;br /&gt;
&lt;br /&gt;
==Risks==&lt;br /&gt;
&lt;br /&gt;
===External Service===&lt;br /&gt;
&lt;br /&gt;
BlockExplorer is a service that is provided by a private party.  There is no guarantee that the information provided by BlockExplorer matches the blockchain.&lt;br /&gt;
&lt;br /&gt;
There have not been any reports that BlockExplorer has reported transaction data incorrectly.&lt;br /&gt;
&lt;br /&gt;
===Double Spending===&lt;br /&gt;
&lt;br /&gt;
A merchant is exposed to a [[double-spending]] attack when recognizing a payment before it has been [[confirmation|confirmed]] with a sufficient number of blocks.&lt;br /&gt;
&lt;br /&gt;
For an attacker to be successful with this double spend tactic a significant effort is required and thus the risk of this attack being made against the typical retail merchant is pretty minimal.  It would not be advisable for a merchant with little to no recourse against an attacker to accept payment without a sufficient number of confirmations however.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
* [[BitAddress]] Generate address and private key pairs for an offline wallet&lt;br /&gt;
* [[BitcoinNotify]] Register addresses and receive email or SMS alerts when a payment to that address occurs&lt;br /&gt;
&lt;br /&gt;
[[de:API_für_Faule]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer]]&lt;/div&gt;</summary>
		<author><name>Rising</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Proper_Money_Handling_(JSON-RPC)&amp;diff=33998</id>
		<title>Proper Money Handling (JSON-RPC)</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Proper_Money_Handling_(JSON-RPC)&amp;diff=33998"/>
		<updated>2012-12-23T19:07:32Z</updated>

		<summary type="html">&lt;p&gt;Rising: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Overview ==&lt;br /&gt;
The original bitcoin client stores all bitcoin values as 64-bit integers, with 1 BTC stored as 100,000,000 (one-hundred-million of the smallest possible bitcoin unit).  Values are expressed as double-precision Numbers in the JSON API, with 1 BTC expressed as 1.00000000&lt;br /&gt;
&lt;br /&gt;
If you are writing software that uses the JSON-RPC interface you need to be aware of possible floating-point conversion issues.  You, or the JSON library you are using, should convert amounts to either a fixed-point Decimal representation (with 8 digits after the decimal point) or ideally a 64-bit integer representation. In either case, rounding values is required.&lt;br /&gt;
&lt;br /&gt;
Improper value handling can lead to embarrassing errors; for example, if you truncate instead of doing proper rounding and your software will display the value &amp;quot;0.1 BTC&amp;quot; as &amp;quot;0.09999999 BTC&amp;quot; (or, worse, &amp;quot;0.09 BTC&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
The original bitcoin client does proper, full-precision rounding for all values passed to it via the RPC interface.  So, for example, if the value 0.1 is converted to the value &amp;quot;0.099999999999&amp;quot; by your JSON-RPC library, that value will be rounded to the nearest 0.00000001 bitcoin and will be treated as exactly 0.10 BTC.&lt;br /&gt;
&lt;br /&gt;
The rest of this page gives sample code for various JSON libraries and programming languages.&lt;br /&gt;
&lt;br /&gt;
== BASH ==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
 function JSONtoAmount() {&lt;br /&gt;
     printf &#039;%.8f&#039; &amp;quot;$1&amp;quot; | tr -d &#039;.&#039;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== C/C++ ==&lt;br /&gt;
C/C++ JSON libraries return the JavaScript Number type as type &#039;double&#039;.  To convert, without loss of precision, from a double to a 64-bit integer multiply by 100,000,000 and round to the nearest integer:&lt;br /&gt;
 int64_t JSONtoAmount(double value) {&lt;br /&gt;
     return (int64_t)(value * 1e8 + (value &amp;lt; 0.0 ? -.5 : .5));&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
To convert to a JSON value divide by 100,000,000.0, and make sure your JSON implementation outputs doubles with 8 or more digits after the decimal point:&lt;br /&gt;
  double forJSON = (double)amount / 1e8;&lt;br /&gt;
&lt;br /&gt;
== ECMAScript ==&lt;br /&gt;
&lt;br /&gt;
 function JSONtoAmount(value) {&lt;br /&gt;
     return Math.round(1e8 * value);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Perl ==&lt;br /&gt;
 sub JSONtoAmount {&lt;br /&gt;
     return sprintf &#039;%.0f&#039;, 1e8 * shift;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== PHP ==&lt;br /&gt;
 function JSONtoAmount($value) {&lt;br /&gt;
     return round($value * 1e8);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Python ==&lt;br /&gt;
 def JSONtoAmount(value):&lt;br /&gt;
     return long(round(value * 1e8))&lt;br /&gt;
 def AmountToJSON(amount):&lt;br /&gt;
     return float(amount / 1e8)&lt;br /&gt;
&lt;br /&gt;
== Common Lisp ==&lt;br /&gt;
  (defun json-to-amount (n)&lt;br /&gt;
    (coerce (round (* n 1e8)) &#039;integer))&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;CAUTION&#039;&#039;&#039;: The CL-JSON library parses numbers as &#039;&#039;single&#039;&#039; precision floating-point by&lt;br /&gt;
default. The default parsing behavior can be overridden as follows:&lt;br /&gt;
&lt;br /&gt;
   (set-custom-vars :real (lambda (n)&lt;br /&gt;
                             (json::parse-number (concatenate &#039;string n &amp;quot;d0&amp;quot;))))&lt;br /&gt;
[[Category:Technical]]&lt;br /&gt;
[[Category:Developer]]&lt;br /&gt;
&lt;br /&gt;
[[de:Korrektes_Handling_von_Geldbeträgen_(JSON-RPC)]]&lt;/div&gt;</summary>
		<author><name>Rising</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=PHP_developer_intro&amp;diff=33997</id>
		<title>PHP developer intro</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=PHP_developer_intro&amp;diff=33997"/>
		<updated>2012-12-23T18:57:54Z</updated>

		<summary type="html">&lt;p&gt;Rising: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;L&#039;&#039;&#039;inux &#039;&#039;&#039;A&#039;&#039;&#039;pache &#039;&#039;&#039;M&#039;&#039;&#039;ySQL &#039;&#039;&#039;P&#039;&#039;&#039;HP + Bitcoin tutorial.&lt;br /&gt;
&lt;br /&gt;
For this introduction we assume that you have GNU/Linux server with Apache and PHP and that you wish to interact with the Bitcoin network from a web application. We assume some knowledge of Bitcoin and experience in PHP.&lt;br /&gt;
&lt;br /&gt;
While this is written for PHP, the same principles apply for other languages. See the associated [[API reference (JSON-RPC)|API reference]] pages for info on other languages.&lt;br /&gt;
&lt;br /&gt;
The easiest way to get started is to run Bitcoin in daemon mode with which PHP communicates via local HTTP requests. A library called [http://jsonrpcphp.org/ JSON-RPC] is used to call the various functions of bitcoind, which will respond back with a [http://en.wikipedia.org/wiki/Json JSON object].&lt;br /&gt;
&lt;br /&gt;
== Setting up Bitcoin ==&lt;br /&gt;
&lt;br /&gt;
You can download the Bitcoin daemon from the [[Main_Page|homepage]] and run one of the included binaries or compile your own from the included source code. See [[Running Bitcoin]] for details on configuring bitcoind.&lt;br /&gt;
&lt;br /&gt;
Before running bitcoind you will need to create a configuration file in the Bitcoin data directory (~/.bitcoin/bitcoin.conf on Linux):&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
rpcuser=user&lt;br /&gt;
rpcpassword={you MUST pick a unique password to be secure}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
If you miss this step, bitcoind will remind you.&lt;br /&gt;
&lt;br /&gt;
Now run bitcoind:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ./bitcoind&lt;br /&gt;
# wait a few seconds for it to start up&lt;br /&gt;
$ ./bitcoind getinfo&lt;br /&gt;
# various information will be shown. If you get an error, try again until you see some useful output.&lt;br /&gt;
$ ./bitcoind help&lt;br /&gt;
# get help on commands, note no dash before help&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Bitcoin will begin synchronizing with the network and downloading a complete copy of the block chain. As of August 2012, more than 2gb of data must be downloaded and verified during this process. It may take two or more hours to complete. You will know when it&#039;s done when the block count reaches the [http://blockexplorer.com/q/getblockcount current count].&lt;br /&gt;
&lt;br /&gt;
== Getinfo (Bitcoind&#039;s version of Hello World) ==&lt;br /&gt;
&lt;br /&gt;
Assuming Bitcoin has finished the initialisation process; download the file jsonRPCClient.php from [http://jsonrpcphp.org/ JSON-RPC PHP] and place it in a web-accessible location.&lt;br /&gt;
&lt;br /&gt;
Second, create a PHP file with the following and visit it with your browser to test.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
  require_once &#039;jsonRPCClient.php&#039;;&lt;br /&gt;
  &lt;br /&gt;
  $bitcoin = new jsonRPCClient(&#039;http://user:password@127.0.0.1:8332/&#039;);&lt;br /&gt;
   &lt;br /&gt;
  echo &amp;quot;&amp;lt;pre&amp;gt;\n&amp;quot;;&lt;br /&gt;
  print_r($bitcoin-&amp;gt;getinfo());&lt;br /&gt;
  echo &amp;quot;&amp;lt;/pre&amp;gt;&amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Precision ==&lt;br /&gt;
&lt;br /&gt;
Bitcoin amounts can range from 1 (0.00000001 BTC) to nearly 2,100,000,000,000,000 (21,000,000 BTC).  To avoid rounding errors, you must make sure your PHP implementation supports the full range of Bitcoin values without losing precision.  Most PHP implementations use IEEE 64-bit double-precision floating point numbers with 53 bits of precision, which is enough to correctly represent the full range of bitcoin values.&lt;br /&gt;
&lt;br /&gt;
See [[Proper Money Handling (JSON-RPC)]] for more information.&lt;br /&gt;
&lt;br /&gt;
If your PHP implementation does not support 64-bit numbers (again, this is very rare), you must use a version of bitcoind that sends values as strings (genjix maintains a fork at http://github.com/genjix/bitcoin) and use the  [http://php.net/manual/en/ref.gmp.php GMP] and [http://php.net/manual/en/ref.bc.php BC Math] libraries for all calculations involving bitcoin amounts.&lt;br /&gt;
&lt;br /&gt;
== Accounts ==&lt;br /&gt;
&lt;br /&gt;
In Bitcoin, money is sent to addresses and many addresses can be held by one wallet. The balance shown by default in bitcoind is the sum of the bitcoins in all the addresses in the wallet.&lt;br /&gt;
&lt;br /&gt;
Bitcoin goes another step. You can have [[Accounts explained|accounts]]. Each account holds multiple addresses and acts like a mini-bitcoind. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ./bitcoind listaccounts&lt;br /&gt;
# show list of accounts and various info for each one&lt;br /&gt;
$ ./bitcoind getaccountaddress user889&lt;br /&gt;
# get an address to receive money to that is unique for the account user889&lt;br /&gt;
$ ./bitcoind getbalance user889&lt;br /&gt;
# get the sum of all the money in the addresses owned by the account user889&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In your application, each user should have a unique username. You may then query bitcoind for a unique address using $bitcoin-&amp;gt;getaccountaddress(&amp;quot;user889&amp;quot;); [gets the first address for user889] or $bitcoin-&amp;gt;getnewaddress(&amp;quot;user889&amp;quot;); [creates a new address for user889].&lt;br /&gt;
&lt;br /&gt;
The customer then deposits to this address.&lt;br /&gt;
&lt;br /&gt;
You can check the funds for that customer by doing $bitcoin-&amp;gt;getbalance(&amp;quot;user889&amp;quot;, 4);. The 4 indicates the minimum number of confirmations we will accept before assuming this payment is valid.&lt;br /&gt;
&lt;br /&gt;
If you will be using accounts for multiple deposits and withdrawals long-term, you may want to consider tracking user balances in your own database. This simplifies transfers between your application&#039;s accounts and decouples your accounts from the Bitcoin wallet.&lt;br /&gt;
&lt;br /&gt;
=== getnewaddress vs getaccountaddress ===&lt;br /&gt;
&lt;br /&gt;
Using getnewaddress helps increase maintain anonymity of your users by making it hard for a malicious agent to track payments flowing through your application. Running getnewaddress too often, however, will cause your wallet to become filled with many empty addresses.&lt;br /&gt;
&lt;br /&gt;
It is therefore recommended to in some way limit the number of unfunded addresses each user can request. Here is an example using sessions:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
    require_once(&#039;jsonRPCClient.php&#039;);&lt;br /&gt;
    $bitcoin = new jsonRPCClient(&#039;http://root:root@127.0.0.1:8332/&#039;); &lt;br /&gt;
    # now check for appropriate funds in user account&lt;br /&gt;
    try {&lt;br /&gt;
        $username = ...&lt;br /&gt;
        if(isset($_SESSION[&#039;sendaddress&#039;]))&lt;br /&gt;
            $sendaddress = $_SESSION[&#039;sendaddress&#039;];&lt;br /&gt;
        else {&lt;br /&gt;
            $sendaddress = $bitcoin-&amp;gt;getnewaddress($username);&lt;br /&gt;
            $_SESSION[&#039;sendaddress&#039;] = $sendaddress;&lt;br /&gt;
        }&lt;br /&gt;
        $balance = $bitcoin-&amp;gt;getbalance($username);&lt;br /&gt;
    }&lt;br /&gt;
    catch (Exception $e) {&lt;br /&gt;
        die(&amp;quot;&amp;lt;p&amp;gt;Server error! Please contact the admin.&amp;lt;/p&amp;gt;&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This creates a new address at the beginning of every new session, and stores it in the session variable.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
* [[API reference (JSON-RPC)]]&lt;br /&gt;
* [[Lazy_API]]&lt;br /&gt;
* [[Merchant Howto]]&lt;br /&gt;
&lt;br /&gt;
[[es:Introducción para desarrolladores de PHP]]&lt;br /&gt;
[[de:Einführung_für_PHP-Entwickler]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer]]&lt;/div&gt;</summary>
		<author><name>Rising</name></author>
	</entry>
</feed>