Send Bitcoin Transaction In Legacy, Segwit, And Native Segwit(Bech32) In JavaScript
When I started working in crypto, the most difficult blockchain I found to work with was bitcoin due to lack of documentation on various…
Send Bitcoin Transaction In Legacy, Segwit, And Native Segwit(Bech32) In JavaScript
Photo by Kanchanara on Unsplash
When I started working in crypto, the most difficult blockchain I found to work with was bitcoin due to lack of documentation on various topics as compared to ethereum etc. I struggled with basic topics like what utxos were, miner fee, gas fee etc. Today, I am going to show you how you can send bitcoin transaction on other addresses in different standards of bitcoin i.e. Legacy, segwit and native segwit.
If you want to see the major differences between these standards, check out the following url: https://thebitcoinmanual.com/articles/btc-bech32-address/
Some of the steps are same in all the standards, so we will go through them first. We will be using blockcypher for getting address related details.
The libraries and services that we would be using are:
Get Address UTXOs In simple words, UTXO is the amount of digital currency remaining after a cryptocurrency transaction is made. Here is how we can get utxos for a particular address.
[embed]
In the first function, we are using blockcypher api to get the total number of utxos for any given address. We are also checking if there is any unconfirmed balance for any given address. The transaction that has not been confirmed yet comes under under unconfirmed transaction and balance transacted in this transaction is unconfirmed.
In the second function, we are looping over all utxos to check if user has sufficient balance to send the transaction or not. If he doesn’t have sufficient balance, an error is being thrown.
Getting Miner Fee, Network Fee And Total Balance On An Address
When we make a transaction on blockchain, there are a number of computers that go through a process to confirm this transaction. As a result, those computers get reward. The reward is called miner fee or gas fee. Let’s see how can we calculate miner fee and network fee for a transaction.
[embed]
In the first function above, we are calculating the miner fee for the transaction. In the second function, we are calculating the total amount that would be transacted including miner fee.
Now that we have established the basics for sending transaction, let’s see how can we send transaction in different bitcoin standards.
Legacy In legacy, bitcoin addresses start with “1…”. This is the most old standard in bitcoin blockchain. These are the original BTC addresses. There are some downsides to this standard which led the community to build more efficient standards like segwit and bech32. But still, not all exchanges and wallets have shifted to new standards. So, it’s good to know the working of this standard as well.
Now, let’s see how we can send bitcoin from one address to another in legacy.
[embed]
Segwit Segwit short for segregated witness is a relatively new standard in bitcoin which was introduces to improve transaction throughput on a blockchain network. SegWit reduces the weight of transactions in a block on the blockchain by segregating a transaction into two sections; effectively increasing the amount of transactions one can include in a block of the same size. This reduced the transaction confirmation time and overall agility of the network. Segwit addresses start from “3…”.
Now let’s see how can we send segwit transaction from one address to another in Javascript.
[embed]
Native Segwit(Bech32) Bech32 is a segwit address format specified by BIP 0173. This address format is also known as “bc1 addresses”. Bech32 is more efficient with block space. It gives a faster transaction speed at low fees with improved scalability. The user gets improved prospects for error detection.
There is not much of a difference between transaction sending mechanism of segwit and bech32. We can send bech32 transaction as follows.
[embed]
Now that we have seen how can we send transaction in different bitcoin standards, last step is to submit the transaction on blockchain. We will be using blockcypher api for this.
[embed]
This will submit the transaction on blockchain.
All the code that we have written until now is as follows:
[embed]
Hope you liked the article. If you have any more suggestions or improvements that I can make in this article please mention. If you find this article helpful, please like and share it with your friends. Thanks.
Stackademic 🎓
Thank you for reading until the end. Before you go:
- Please consider clapping and following the writer! 👏
- Follow us **X | LinkedIn | YouTube | Discord**
- Visit our other platforms: **In Plain English | CoFeed | Venture | Cubed**
- More content at **Stackademic.com**
메타데이터
- post_id
- 56ebd93d2c91
- slug
- send-bitcoin-transaction-in-legacy-segwit-and-native-segwit-bech32-in-javascript-56ebd93d2c91
- url
- https://blog.stackademic.com/send-bitcoin-transaction-in-legacy-segwit-and-native-segwit-bech32-in-javascript-56ebd93d2c91
- canonical_url
- https://blog.stackademic.com/send-bitcoin-transaction-in-legacy-segwit-and-native-segwit-bech32-in-javascript-56ebd93d2c91
- author_url
- https://medium.com/@waqarilyas
- status
- ok
- fetched_at
- 2026-07-26 08:09:12