Blockchain App Devlp
Blockchain App Devlp
Figure: Application interacting with the Bitcoin blockchain using the Block Explorer API
The diagram illustrates how an application interacts with the Bitcoin blockchain through a Block Explorer
API. Here's a breakdown:
1. Application Code:
o The application code is responsible for performing operations like retrieving blockchain
data, monitoring transactions, or fetching wallet balances.
o It does not directly interact with the blockchain nodes but uses the Block Explorer API as an
intermediary.
2. Block Explorer API:
o The Block Explorer API provides a standardized way to query data from the blockchain.
o This API connects to a specific blockchain node (Node 1 in this diagram) to fetch or push
data.
o Examples of queries include:
Fetching block details.
Retrieving transaction history.
Checking wallet balances.
3. Node 1:
o Node 1 is a blockchain node directly connected to the application through the API.
o It is part of the larger blockchain network and interacts with other nodes to maintain a
consistent and up-to-date ledger.
4. Blockchain Network:
o The blockchain network consists of multiple interconnected nodes (Node 2, Node 3, Node 4,
and Node 5 in this case).
o These nodes share information, validate transactions, and reach consensus on the blockchain
state.
5. Communication Flow:
o The application sends requests via the Block Explorer API to Node 1.
o Node 1 retrieves the necessary information (e.g., transaction data or block details) by
communicating with other nodes in the network as needed.
o Once the information is retrieved, it is sent back to the application through the API.
In summary, the Block Explorer API acts as a bridge between the application and the Bitcoin blockchain,
abstracting the complexity of directly interacting with the network nodes.
Output:
Alice's Address: mqdofsXHpePPGBFXuwwypAqCcXi48Xhb2f
Alice's Private Key: cRzjwUr4Ak7cLk5zL6mSmTSL8u7sKKn7pj9Dj9SkcZ3XxFiUVtk9
Bob's Address: mqEmVRFSnN2Gsb13EY9m4A5BeUFG6Sqtzy
Bob's Private Key: cV6Hn3Zx1QW3R7rZ1KrBR4nB8AUXFChhYGUpeWDC7rfSQFqNzXqs
Enter the Sender Address generated in the previous step and request test bitcoins.
Check the Wallet for the Deposit:
After a few seconds to a minute, your testnet Bitcoin should arrive at Alice's (or Bob's) wallet.
You can check the balance by running a block explorer that supports Bitcoin testnet, like:
https://blockchair.com/bitcoin-testnet
https://live.blockcypher.com/btc-testnet/
You can search for the address there, and it will show you the transaction history and balance.
To get the unspent outputs, we will send an HTTP request to the UTXO endpoint with Bob’s address
Sample Output:
No UTXOs found for this address.
(async () => {
const utxos = await getUTXOs(senderAddress);
psbt.addOutput({
address: receiverAddress,
value: 50000, // Send 0.0005 BTC
});
psbt.addOutput({
address: senderAddress,
value: utxo.value - 50000 - 10000, // Change amount after deducting output and fee
});
console.log("Transaction prepared.");
})();
Sample Output:
Transaction prepared.
if (!psbt.validateSignaturesOfInput(0)) {
throw new Error("Signature validation failed.");
}
psbt.finalizeAllInputs();
console.log("Transaction inputs signed.");
})();
Sample Output:
Transaction inputs signed.
Sample Output:
Raw Transaction Hex: 020000000001...
Sample Output:
Transaction broadcasted. TXID: abcd1234...
Practice Questions
1. Explain how Application interacting with the Bitcoin blockchain using the Block Explorer API with neat
diagram..
2. With code snippet explain how to prepare bitcoin transaction and how to sign transaction inputs.
3. Explain the process of create key pairs for the sender and receiver in bitcoin.
4. With code snippet explain how to broadcast bitcoin transaction to the network.