Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: vicknick on January 26, 2020, 02:35:53 PM



Title: How to Create Bitcoin Transactions from a Server
Post by: vicknick on January 26, 2020, 02:35:53 PM
I am exploring ways to create a bitcoin transaction on a website server. When a user has typed in his bitcoin address and the amount to be sent, how do you create a bitcoin transaction to send the specified amount to his/her address on a server? I tried searching online but so far I can only find minimal info about this.

Some says you can install bitcoin core on the server, but the storage needed to hold the blockchain exceeds my capacity. Electrum seems to be a good option since it doesn't download the entire blockchain, but there is no info on how to make a transaction from the server code (I am writing in JavaScript).

Any recommendations are highly appreciated.


Title: Re: How to Create Bitcoin Transactions from a Server
Post by: DaveF on January 26, 2020, 02:43:18 PM
I think you are just looking to create the raw transaction and send that?
If so:
https://en.bitcoin.it/wiki/Raw_Transactions (https://en.bitcoin.it/wiki/Raw_Transactions)

If you are looking to do something else you are going to have to define what you are looking to do a bit more.
Also, if the server you are working on can't hold the blockchain it's probably time to get a new one.

-Dave


Title: Re: How to Create Bitcoin Transactions from a Server
Post by: ranochigo on January 26, 2020, 02:43:24 PM
You don't have to keep the entire blockchain on your computer for the whole time. You can delete the older blocks as you go with pruning and keep it from exceeding a certain size.

That being said, Electrum offers JSONRPC and you can easily integrate that with your code. There's some examples here[1].


[1] https://electrum.readthedocs.io/en/latest/merchant.html


Title: Re: How to Create Bitcoin Transactions from a Server
Post by: vicknick on January 26, 2020, 02:48:52 PM
Quote
That being said, Electrum offers JSONRPC and you can easily integrate that with your code. There's some examples here[1].

That's accepting bitcoin, however what I want is to send bitcoin to users.


Title: Re: How to Create Bitcoin Transactions from a Server
Post by: vicknick on January 26, 2020, 02:58:04 PM
I think you are just looking to create the raw transaction and send that?
If so:
https://en.bitcoin.it/wiki/Raw_Transactions (https://en.bitcoin.it/wiki/Raw_Transactions)

If you are looking to do something else you are going to have to define what you are looking to do a bit more.
Also, if the server you are working on can't hold the blockchain it's probably time to get a new one.

-Dave

Does it require bitcoin core to be installed? Is there other ways to send bitcoin from server without installing bitcoin core wallet?


Title: Re: How to Create Bitcoin Transactions from a Server
Post by: ranochigo on January 26, 2020, 03:06:40 PM
Does it require bitcoin core to be installed? Is there other ways to send bitcoin from server without installing bitcoin core wallet?
Would pruning not work for you? It's possible for you to use a code to script your own raw transaction and then directly broadcast the raw transaction to the other nodes. But that would require you to be able to obtain the information about your UTXO in the first place and it would preferably be from your Bitcoin node.


Title: Re: How to Create Bitcoin Transactions from a Server
Post by: BrewMaster on January 26, 2020, 03:34:10 PM
Quote
That being said, Electrum offers JSONRPC and you can easily integrate that with your code. There's some examples here[1].

That's accepting bitcoin, however what I want is to send bitcoin to users.

you can do both with Electrum JSONRPC. the commands support both creation and signing of transactions from your keys with commands such as "signtransaction()" and then you can broadcast them using "broadcast(tx)".


Title: Re: How to Create Bitcoin Transactions from a Server
Post by: vicknick on January 26, 2020, 04:00:45 PM
Would pruning not work for you? It's possible for you to use a code to script your own raw transaction and then directly broadcast the raw transaction to the other nodes. But that would require you to be able to obtain the information about your UTXO in the first place and it would preferably be from your Bitcoin node.
However when users deposit bitcoin to the website, each of them have different addresses, which are generated in deterministic manner. The reason I prefer to use a wallet is because when the server send bitcoin to external address given by the user, the wallet is able to send bitcoin by combining from different addresses. I think writing own script to implement this feature would be harder?


Title: Re: How to Create Bitcoin Transactions from a Server
Post by: vicknick on January 26, 2020, 04:15:10 PM
Quote
That being said, Electrum offers JSONRPC and you can easily integrate that with your code. There's some examples here[1].

That's accepting bitcoin, however what I want is to send bitcoin to users.

you can do both with Electrum JSONRPC. the commands support both creation and signing of transactions from your keys with commands such as "signtransaction()" and then you can broadcast them using "broadcast(tx)".
Is there any examples/documentations for this? Electrum's documentation doesn't seem to have the info.


Title: Re: How to Create Bitcoin Transactions from a Server
Post by: BrewMaster on January 26, 2020, 04:29:53 PM
{snip}
Is there any examples/documentations for this? Electrum's documentation doesn't seem to have the info.

there is the electrum docs here: https://electrum.readthedocs.io/en/latest/merchant.html that contain the needed information to start accepting bitcoin as a merchant using Electrum. it mainly explains accepting payment but the principle is the same for sending payments too.
there are also some articles on the internet that you can find in google search for keyword "bitcoin" + "electrum" + "merchants". i haven't used any of them so i can't recommend any.


Title: Re: How to Create Bitcoin Transactions from a Server
Post by: PrimeNumber7 on January 27, 2020, 06:44:52 AM
Quote
That being said, Electrum offers JSONRPC and you can easily integrate that with your code. There's some examples here[1].

That's accepting bitcoin, however what I want is to send bitcoin to users.

you can do both with Electrum JSONRPC. the commands support both creation and signing of transactions from your keys with commands such as "signtransaction()" and then you can broadcast them using "broadcast(tx)".
Is there any examples/documentations for this? Electrum's documentation doesn't seem to have the info.
You would need to use a series of RPC commands (https://electrum.readthedocs.io/en/latest/protocol.html#server-version), such as listunspent and signtransaction. You would also need to craft a transaction outside of electrum if you were automating sending transactions.

If you absolutely do not wish to use a full node, you can also use the blockchain.com wallet API (https://www.blockchain.com/api/blockchain_wallet_api). Blockchain.com/info does have a history of screwups that has resulted in the loss of customer money, but I believe this to be less common in recent years.

I do believe running a full node and using the sendtoaddress (https://bitcoin.org/en/developer-reference#sendtoaddress) RPC command would probably be your best solution. The cost to run a fully validating full node is not extraordinary. 


Title: Re: How to Create Bitcoin Transactions from a Server
Post by: MrFreeDragon on January 29, 2020, 12:52:32 PM
Have a look at this service:

https://github.com/OutCast3k/coinbin/
https://coinb.in/

This tool helps to create transactions online and broadcast them. If you go to the source code on github, you can find the useful information for you and use it on your web server.

Actually there are 2 steps: (1) create raw transaction, (2) broadcast the transaction. Both steps are realized in the tool.

EDIT: The tool uses JavaScript (as you asked in the OP)


Title: Re: How to Create Bitcoin Transactions from a Server
Post by: Theb on January 29, 2020, 07:55:50 PM
there is the electrum docs here: https://electrum.readthedocs.io/en/latest/merchant.html that contain the needed information to start accepting bitcoin as a merchant using Electrum. it mainly explains accepting payment but the principle is the same for sending payments too.
there are also some articles on the internet that you can find in google search for keyword "bitcoin" + "electrum" + "merchants". i haven't used any of them so i can't recommend any.

Like what the OP stated in his other post he would also like to send crypto payments automatically om his side. Electrum's merchant function in their document only accept payments as a business and doesn't have any documentation for them sending the payments. @OP on this part I think it will be harder to find a sample script on what you are asking since even e-commerce crypto payment processors like Bitpay and Coinbase commerce only has a function to receive payments for your business. You would need to consider the balance your wallet have, the fees you will be handling, and also the conversion of the prices in USD all of which if it will go wrong your funds will be instantly flushed.