Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: finales on April 06, 2017, 05:09:09 PM



Title: Create a transaction with BitcoinJS
Post by: finales on April 06, 2017, 05:09:09 PM
Do you need to be running that code on a server that also acts as a Bitcoin Core Node in order to broadcast the transaction? Or am I misthinking how this works? Thanks!


Title: Re: Create a transaction with BitcoinJS
Post by: dannon on April 10, 2017, 12:03:02 PM
You do not need to be running a node to create and sign transactions. You need to be running a node to have the data to put in the transaction or be able to get that data from somewhere else.


Title: Re: Create a transaction with BitcoinJS
Post by: finales on April 11, 2017, 06:45:58 PM
What data are you referring to? Thanks


Title: Re: Create a transaction with BitcoinJS
Post by: dannon on April 12, 2017, 11:04:52 AM
What data are you referring to? Thanks

Blockchain data. Information about previous transactions.


Title: Re: Create a transaction with BitcoinJS
Post by: nemgun on April 12, 2017, 12:31:29 PM
Do you need to be running that code on a server that also acts as a Bitcoin Core Node in order to broadcast the transaction? Or am I misthinking how this works? Thanks!


I suppose that you did this :

Code:
var tx = new bitcoin.TransactionBuilder()

// Add the input (who is paying):
// [previous transaction hash, index of the output to use]
var txId = 'aa94ab02c182214f090e99a0d57021caffd0f195a81c24602b1028b130b63e31' //
tx.addInput(txId, 0)

// Add the output (who to pay to):
// [payee's address, amount in satoshis]
tx.addOutput("1Gokm82v6DmtwKEB8AiVhm82hyFSsEvBDK", 15000)

// Initialize a private key using WIF
var privateKeyWIF = 'L1uyy5qTuGrVXrmrsvHWHgVzW9kKdrp27wBC7Vs6nZDTF2BRUVwy'
var keyPair = bitcoin.ECPair.fromWIF(privateKeyWIF)

// Sign the first input with the new key
tx.sign(0, keyPair);

/*-------------------------------------------Look here ---------------------------------------------*/
// Print transaction serialized as hex
var hexTX = tx.build().toHex();

$.post('http://btc.blockr.io/api/v1/tx/push',{"hex":hexTX},function(data){
console.log(data);
});

You just need to push it like in the example, but you should use $.Ajax instead of $.post because you iwll have more options to manage the transmission and secure it.


Title: Re: Create a transaction with BitcoinJS
Post by: dannon on April 14, 2017, 07:25:21 PM
Do you need to be running that code on a server that also acts as a Bitcoin Core Node in order to broadcast the transaction? Or am I misthinking how this works? Thanks!


I suppose that you did this :

Code:
var tx = new bitcoin.TransactionBuilder()

// Add the input (who is paying):
// [previous transaction hash, index of the output to use]
var txId = 'aa94ab02c182214f090e99a0d57021caffd0f195a81c24602b1028b130b63e31' //
tx.addInput(txId, 0)

// Add the output (who to pay to):
// [payee's address, amount in satoshis]
tx.addOutput("1Gokm82v6DmtwKEB8AiVhm82hyFSsEvBDK", 15000)

// Initialize a private key using WIF
var privateKeyWIF = 'L1uyy5qTuGrVXrmrsvHWHgVzW9kKdrp27wBC7Vs6nZDTF2BRUVwy'
var keyPair = bitcoin.ECPair.fromWIF(privateKeyWIF)

// Sign the first input with the new key
tx.sign(0, keyPair);

/*-------------------------------------------Look here ---------------------------------------------*/
// Print transaction serialized as hex
var hexTX = tx.build().toHex();

$.post('http://btc.blockr.io/api/v1/tx/push',{"hex":hexTX},function(data){
console.log(data);
});

You just need to push it like in the example, but you should use $.Ajax instead of $.post because you iwll have more options to manage the transmission and secure it.

You don't need jQuery. Just make an xmlhttprequest


Title: Re: Create a transaction with BitcoinJS
Post by: nemgun on April 15, 2017, 10:18:55 AM
Do you need to be running that code on a server that also acts as a Bitcoin Core Node in order to broadcast the transaction? Or am I misthinking how this works? Thanks!


I suppose that you did this :

Code:
var tx = new bitcoin.TransactionBuilder()

// Add the input (who is paying):
// [previous transaction hash, index of the output to use]
var txId = 'aa94ab02c182214f090e99a0d57021caffd0f195a81c24602b1028b130b63e31' //
tx.addInput(txId, 0)

// Add the output (who to pay to):
// [payee's address, amount in satoshis]
tx.addOutput("1Gokm82v6DmtwKEB8AiVhm82hyFSsEvBDK", 15000)

// Initialize a private key using WIF
var privateKeyWIF = 'L1uyy5qTuGrVXrmrsvHWHgVzW9kKdrp27wBC7Vs6nZDTF2BRUVwy'
var keyPair = bitcoin.ECPair.fromWIF(privateKeyWIF)

// Sign the first input with the new key
tx.sign(0, keyPair);

/*-------------------------------------------Look here ---------------------------------------------*/
// Print transaction serialized as hex
var hexTX = tx.build().toHex();

$.post('http://btc.blockr.io/api/v1/tx/push',{"hex":hexTX},function(data){
console.log(data);
});

You just need to push it like in the example, but you should use $.Ajax instead of $.post because you iwll have more options to manage the transmission and secure it.

You don't need jQuery. Just make an xmlhttprequest

I said he "should" use jquery, because it is easier then using xhr, because is can be tricky, while using jQuery you can simply send Data, debug, customise the result, use promises or callbacks. While doing these using xhr will end with a 100 line of code, which is not the aim of developers i guess.


Title: Re: Create a transaction with BitcoinJS
Post by: Kolder on April 15, 2017, 10:24:31 AM
Anybody here can help me on installing bitcoinjs on my laptop. I'm using windows 10 OS. I succesfully installed the bitcoinjs exe file but i can't install the npm by using the command
Code:
npm install npm

I hope somebody can help me.  :-\


Title: Re: Create a transaction with BitcoinJS
Post by: nemgun on April 15, 2017, 10:30:27 AM
Anybody here can help me on installing bitcoinjs on my laptop. I'm using windows 10 OS. I succesfully installed the bitcoinjs exe file but i can't install the npm by using the command
Code:
npm install npm

I hope somebody can help me.  :-\

yes, it is normall as you are asking npm to install npm, which is not really possible.
use : npm install bitcoinjs-lib

By the way, have you installed nodeJS ?


Title: Re: Create a transaction with BitcoinJS
Post by: Kolder on April 15, 2017, 01:13:30 PM
Anybody here can help me on installing bitcoinjs on my laptop. I'm using windows 10 OS. I succesfully installed the bitcoinjs exe file but i can't install the npm by using the command
Code:
npm install npm

I hope somebody can help me.  :-\

yes, it is normall as you are asking npm to install npm, which is not really possible.
use : npm install bitcoinjs-lib

By the way, have you installed nodeJS ?

Yeah. I watch the tutorial on youtube regarding creating vanity address using bitcoinJS. In the tutorial. He used that code to install npm which is necessary to do your your suggested code. I saw that code on instruction on BitcoinJS but it has a $ symbol at the beginning. I try that code with $ sign at beginning but it didn't work out.


Title: Re: Create a transaction with BitcoinJS
Post by: nemgun on April 15, 2017, 02:51:35 PM
Anybody here can help me on installing bitcoinjs on my laptop. I'm using windows 10 OS. I succesfully installed the bitcoinjs exe file but i can't install the npm by using the command
Code:
npm install npm

I hope somebody can help me.  :-\

yes, it is normall as you are asking npm to install npm, which is not really possible.
use : npm install bitcoinjs-lib

By the way, have you installed nodeJS ?

Yeah. I watch the tutorial on youtube regarding creating vanity address using bitcoinJS. In the tutorial. He used that code to install npm which is necessary to do your your suggested code. I saw that code on instruction on BitcoinJS but it has a $ symbol at the beginning. I try that code with $ sign at beginning but it didn't work out.

ok, so, you are new to coding.

$ is displayed in a terminal, these terminals are used bu Unix and MacOSX, so you don't have it in cmd, so i suppose you are using windows.
When you have installed nodejs, create a directory and open a cmd inside the directory.
then do
Code:
npm init
then enter the informations, next to this do
Code:
npm install --save bitcoinjs-lib jquery
create a .txt file and rename it to main.js or index.js as you specified when npm init.
Then open this file with a software like notepad2 or sublimetext, and add :
Code:
var bitcoin = require('bitcoinjs-lib');
var $ = require('jquery'); // yes, we are in 2017, it had been solved

var tx = new bitcoin.TransactionBuilder();

// Add the input (who is paying):
// [previous transaction hash, index of the output to use]
var txId = 'aa94ab02c182214f090e99a0d57021caffd0f195a81c24602b1028b130b63e31' ;
tx.addInput(txId, 0);

// Add the output (who to pay to):
// [payee's address, amount in satoshis]
tx.addOutput("1Gokm82v6DmtwKEB8AiVhm82hyFSsEvBDK", 15000);

// Initialize a private key using WIF
var privateKeyWIF = 'L1uyy5qTuGrVXrmrsvHWHgVzW9kKdrp27wBC7Vs6nZDTF2BRUVwy';
var keyPair = bitcoin.ECPair.fromWIF(privateKeyWIF);

// Sign the first input with the new key
tx.sign(0, keyPair);

/*-------------------------------------------Look here ---------------------------------------------*/
// Print transaction serialized as hex
var hexTX = tx.build().toHex();

$.post('http://btc.blockr.io/api/v1/tx/push',{"hex":hexTX},function(data){
console.log(data);
});

And you are done.


Title: Re: Create a transaction with BitcoinJS
Post by: finales on April 16, 2017, 06:19:03 PM
Is there a way to know on that same server when the transaction has been processed and added to the Blockchain? Thanks!


Title: Re: Create a transaction with BitcoinJS
Post by: nemgun on April 17, 2017, 08:45:24 AM
Is there a way to know on that same server when the transaction has been processed and added to the Blockchain? Thanks!

It is not related to the thread, but you can get these informations using APIs from block explorers, if you want to have a live feed of transaction confirmations, or reception, you  can use blockcypher's websocket, subscribe and you will get all of these informations.


Title: Re: Create a transaction with BitcoinJS
Post by: alexsamudra on April 17, 2017, 09:05:30 AM
enough with the data that you've got it, do not need to sign the transaction.
you simply enter the data and any further instructions ikutin into the transaction


Title: Re: Create a transaction with BitcoinJS
Post by: nemgun on April 18, 2017, 09:41:39 AM
enough with the data that you've got it, do not need to sign the transaction.
you simply enter the data and any further instructions ikutin into the transaction

Signing a transaction is an important security measure, unsigned transactions are maleable. You should check this : https://bitcoin.org/en/developer-guide#transactions
And revise some of the informations you have about bitcoin transactions.
If you are too lazy to read the Transaction of the developper guide, maybe this post will interest you : https://bitcoin.stackexchange.com/questions/7253/signing-bitcoin-transactions


Title: Re: Create a transaction with BitcoinJS
Post by: iDice_io on April 27, 2017, 12:44:31 AM
Do you need to be running that code on a server that also acts as a Bitcoin Core Node in order to broadcast the transaction? Or am I misthinking how this works? Thanks!
Create the transaction in BitcoinJS, which can then be broadcasted to a node (local, or not local). I believe Blockchain has a service for this.


Title: Re: Create a transaction with BitcoinJS
Post by: Casy on April 27, 2017, 09:06:18 AM
As you are new to coding, whatever you do, try it on the testnet first in order to prevent losing btc.


Title: Re: Create a transaction with BitcoinJS
Post by: BluePoppy on April 28, 2017, 07:36:48 AM
Do you need to be running that code on a server that also acts as a Bitcoin Core Node in order to broadcast the transaction? Or am I misthinking how this works? Thanks!
Create the transaction in BitcoinJS, which can then be broadcasted to a node (local, or not local). I believe Blockchain has a service for this.

Here's the link: https://blockchain.info/pushtx

Yup, BitcoinJS should be used here and then you can paste it manually. Or you can use Blockchain's API, which will be helpful if you're creating an application for users.