Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Blockchain by (4.1k points)

I'm trying to make a little web app (just for learning purposes), where I can push on a button, and it will send some bitcoin from (my own) bitcoin wallet A to bitcoin wallet B. I've managed to learn some stuff about bitcoin, the blockchain, and figured out how to make a raw bitcoin transaction like so:

var bitcoin = require('bitcoinjs-lib');

var keyPair = bitcoin.ECPair.fromWIF('****************************');
var tx = new bitcoin.TransactionBuilder();

tx.addInput('****************************', 0);
tx.addOutput('****************************', 546);
tx.sign(0, keyPair);

console.log(tx.build().toHex());
generatedhash = tx.build().toHex();

If I take the generatedhash to a website like https://blockr.io/tx/push it will succesfully do the bitcoin transaction, but I'd like to know if its possible to automate pushing the raw transaction using js as well? Thanks for reading :)

1 Answer

0 votes
by (14.4k points)
edited by

Use these lines of code for pushing the raw transaction using js: 

var Insight = require('bitcore-explorers').Insight;

var insight = new Insight();

insight.broadcast(tx, function(err, returnedTxId) {

  if (err) {

    // Handle errors...

  } else {

    // Mark the transaction as broadcasted

  }

});

Know more about Blockchain in detail. Enroll in IBM Blockchain Certification now to take that first step.

Browse Categories

...