'How do you make blockchain applications using node?
I've worked with blockchain and bitcoin processing in the past, but it was PHP, everything there seems pretty straightforward. You generate a new address, user sends bitcoin to the address, blockchains' scripts calls your callback php file.
But what if my application does not use PHP and it's made in Node.JS? How do you check if a user sent money to the address generated? What if the application is offline?
Thanks.
Solution 1:[1]
I have not developed any bitcoin related nodejs applications, yet. But since your question is about blockchain applications and generating addresses and transactions, you can use the ethereumjs and web3js modules offered to interact with ethereum nodes.
$ npm install web3
Check if it's available in your app:
window.console.log(web3);
And connect it, for example:
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
Check out the full API documentation on creating accounts and transactions.
Solution 2:[2]
to send money, you create an endpoint, post the "amount" and "recipient" to this endpoint, check the incoming request and then broadcast this transaction.
router.post("/api/transact", async (req, res) => {
const { amount, recipient } = req.body;
// transactionPool is list of transactions that needs to mined
let transaction = transactionPool.existingTransaction({
inputAddress: wallet.publicKey,
});
try {
if (transaction) {
transaction.update({ senderWallet: wallet, recipient, amount });
} else {
transaction = wallet.createTransaction({
recipient,
amount,
chain: blockchain.chain,
});
}
} catch (e) {
return res.status(400).json({ type: "error", message: e.message });
}
transactionPool.setTransaction(transaction);
// to broadcast you need a pub-sub system
await pubsub.broadcastTransaction(transaction);
res.json({ type: "success", transaction });
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | q9f |
| Solution 2 | Yilmaz |
