Check bitcoin balance blockchain
This step can and should be executed only once, and the secret key can be copied to the configuration file in your application or kept in another safe place. Now it is time to generate new random and unique address for our user. As I mentioned before, Bitcoin does not allow adding additional fields or information to the transaction. Thus we can recognize the payer only by this unique address. This address will be connected with our Wallet, which we created a moment ago, but this connection will not be visible to anybody else.
Creating an address is an action that saves something to the blockchain, so we need to unblock our wallet first. The same happens when a transaction is sent. In the 14th line, we call the joinWallet method, which authenticates the application with the secret key. That is the same key we generated in the previous example. Another method, openWallet , in line 24, opens this Wallet, and from now on, we will be able to write to the blockchain. As the only argument, it accepts the callback function where we will create a new Address.
Line 30 is responsible for creating the new address. Thus, we will receive an Address object with the field address , which should be presented to the user and saved in the database for future reference. We can just improve the way how or when we present payment information. At this point, the user scans the QR code or copies the address to his wallet and makes the payment.
When that happens, the payment is published to the Blockchain but has not been included in any block yet. It is on the pending list, and we can get information about its status.
Until the block with this transaction is mined, the payment is not secure, so we cannot trust it. But, we can give the user the feedback that we noticed the payment.
To do it, we can call an API method that delivers a full list of all pending transactions. Once, the transaction is mined, it will be moved from this list to the block. At this point, we can check if any new pending transaction for the address generated for our user has appeared.
When the Steam platform allowed Bitcoin payments, they did the same. The first step was to present a unique address for the user, and if she did not close the page and transferred coins to this address, the Steam platform displayed the current payment status as Pending. To approach the same behavior, we can list all transactions for the address, and before being mined, they will have 0 confirmations.
At this point, the transaction is Pending and cannot be treated as safe. However, we can always inform the user that we immediately noticed the payment. This pair of tools allows us to review the blockchain without having a wallet. I strongly recommend you connect and open the Wallet only and only when it is necessary. In this piece of code, I used an address where I transferred some amount of coins, but they have not been mined yet.
Therefore, I can see that the txs method returns a list of transactions with only one element which has no confirmations. There are two ways of getting information about the transaction now — the first one is to save the transaction id from the pending list and track its status with a cronjob or another system, or listen to the last published block and get a list of transactions included in it.
It does not mean it is better, just different. This is one of the approaches for getting information about the current payment status. If you already got the transaction ID, then you can omit this step and go directly to step four. But sometimes, it happens that you are not able to observe all addresses.
When you run a really successful business, you probably open hundreds of addresses every day, and checking pending transactions for all of them may be a time- and resource-consuming process. That's why I prefer to observe the latest block in the blockchain. The block includes a list of mined transactions. We will review those transactions and compare receivers with our saved addresses. If we find any of the addresses in our database, we have a match; we can save the transaction ID and just wait for confirmations.
This will return a full set of data including a list of transactions. Unfortunately, the list of transactions includes only the IDs, so we need to go through all those IDs and get the transaction data from another method called tx.
I prepared the code: As you can see, the latest block has two transactions, but the receivers are pretty well hidden. This is because they are stored in the vout section. You can notice that the response includes two sections — vin value in , and vout value out. Those are arrays because Bitcoin does not make a transfer from one address to another, but from a bunch of addresses in the same wallet to whatever is specified.
You can create your own transaction and send it to how many receivers you want to. To do so, you need to create a raw transaction, sign it with your private key, and publish to the blockchain. This operation requires three different steps, or four if you want to verify the generated hash. In this case, you can compose your own transaction. You need to specify which addresses you want to use to send money from, and where exactly it will be sent — you can transfer funds to one or many addresses.
The funny thing here is that there could be 5 recipient addresses, all of them the same. Bitcoin cannot stop you. The code below lists all receiver addresses with the amount of funds that were transferred to it. This one list is included into one specific transaction: Now we can get information about the block, but we need to know the block hash, which is pretty hard to guess. That is why blocks also have order numbers. My approach is to save the number of the last block that I checked to the database and increase it every time I run the cronjob.
In this case, I can check transactions in many blocks, one after another, not only in the last one. The API provides a method that returns a block hash for a specific order number. It is called block-index: All Bitcoin transactions are grouped in files called blocks. Bitcoin adds a new block of transactions every 10 minutes. Once a new block is added to the blockchain, it becomes immutable and can't be deleted or modified.
A special group of participants in the network called miners computers connected to the blockchain are responsible for creating new blocks of transactions. A miner has to authenticate each transaction using the sender's public key, confirm that the sender has enough balance for the requested transaction, and add the transaction to the block.
Miners are completely free to choose which transactions to include in the blocks, therefore the senders need to include a transaction fee to incentivise the miners to add their transactions to the blocks. For a block to be accepted by the blockchain, it needs to be "mined". To mine a block, miners need to find an extremely rare solution to a cryptographic puzzle.
If a mined block is accepted by the blockchain, the miner receive a reward in bitcoins which is an additional incentive to transaction fees. The mining process is also referred to as Proof of Work PoW , and it's the main mechanism that enables the blockchain to be trustless and secure more on blockchain security later. To understand the blockchain's cryptographic puzzle, we need to start with hash functions. A hash function is any function that can be used to map data of arbitrary size to data of fixed size.
The values returned by a hash function are called hashes. Hash functions are usually used to accelerate database lookup by detecting duplicated records, and they are also widely used in cryptography. A cryptographic hash function allows one to easily verify that some input data maps to a given hash value, but if the input data is unknown, it is deliberately difficult to reconstruct it by knowing the stored hash value. Bitcoins uses a cryptographic hash function called SHA SHA is applied to a combination of the block's data bitcoin transactions and a number called nonce.
By changing the block data or the nonce, we get completely different hashes. For a block to be considered valid or "mined", the hash value of the block and the nonce needs to meet a certain condition. For example, the four leading digits of the hash needs to be equal to "". We can increase the mining complexity by making the condition more complex, for example we can increase the number of 0s that the hash value needs to start with.
The cryptograhic puzzle that miners need to solve is to find a nonce value that makes the hash value satisfies the mining condition.
You can use the app below to simulate block mining. When you type in the "Data" text box or change the nonce value, you can notice the change in the hash value.
When you click the "Mine" button, the app starts with a nonce equals to zero, computes the hash value and checks if the leading four digits of the hash value is equal to "". If the leading four digits are not equal to "", it increments the nonce by one and repeats the whole process until it finds a nonce value that satisify the condition. If the block is considered mined, the background color turns green.
As discussed in the previous section, transactions are grouped in blocks and blocks are appended to the blockchain. Any changes to the data in any block will affect all the hash values of the blocks that come after it and they will become invalid. This give the blockchain its immutability characteristic. You can use the app below to simulate a blockchain with 3 blocks.
When you type in the "Data" text box or change the nonce value, you can notice the change in the hash value and the "Prev" value previous hash of the next block. After mining the 3 blocks, try changing the data in block 1 or 2, and you will notice that all the blocks that come after become invalid. Both mining simulators above were adapted from Anders Brownworth's excellent Blockchain Demo. SpectroCoin blockchain wallet security is assured by advanced authentication methods, so only you have access to your blockchain wallet.
Create a SpectroCoin cryptocurrency wallet and start receiving bitcoins. Use SpectroCoin as your gateway to blockchain. SpectroCoin mobile wallet reduce cryptocurrency price risk by allowing clients to buy exact amount of coins after initiating a transaction, and it happens instantly! Get a Free Blockchain Wallet Sign up. Use a secure application programming interface to integrate the SpectroCoin blockchain wallet directly into your applications. Manage blockchain wallet through API to check balances and view transaction histories.