Creating Segwit Transactions with Bitcore-Node: A Troubleshooting Guide
As a Node.js developer, you have likely encountered issues when using the bitcore-lib
package to create and sign Bitcoin transactions. One of the most common challenges is trying to use the Segwit feature introduced in Bitcoin Core 26.1. In this article, we’ll explore how to overcome these limitations and successfully create Segwit transactions using the same NPM package.
The Issue: Legacy Transactions
When you’re creating legacy Bitcoin transactions with bitcore-lib
, it seems that they don’t support Segwit by default. This is because the package relies on the older Bitcoin Core protocol, which doesn’t natively support Segwit. However, some npm packages have been created to bridge this gap.
The Solution: Using bitcore-segwit'
To create and sign Segwit transactions using any npm package that supports it, you can use thebitcore-segwitpackage (available at [ This package provides a simple way to work with Segwit-compatible Bitcoin nodes and wallets.
Step by Step Guide
Here's a step-by-step guide on how to create and sign Segwit transactions usingbitcore-liband
bitcore-segwit:
- Install the required packages: Run the following command in your terminal:
npm install bitcore libseaweed
- Create a new transaction: Create a new transaction using the newTransaction
method provided by
libseaweed:
const { Transaction } = require('libseaweed');
const bitcoreLib = require('./bitcore-lib');
// Create a new wallet (e.g., from a private key)
let wallet;
try {
const privateKey = 'your_private_key_here';
wallet = await bitcoreLib.createWallet(privateKey);
} catch (error) {
console.error(error);
}
// Create a new transaction
const tx = new Transaction(
wallet.address,
[new BitcoindAddress('1.2.3.4:1234'), // sender's address]
);
// Set the Segwit flag for the transaction
tx.setSegwits([
{ type: 'publicKey', privateKey: privateKey },
]);
// Sign the transaction with a new private key (or use an existing one)
const signature = await bitcoreLib.signTransaction(tx, 'your_new_private_key_here');
In this example:
- We create a wallet using bitcore-lib
and store it in the
walletvariable.
- We create a new transaction usinglibseaweed
.
- We set the Segwit flag for the transaction by adding an object with two properties:type = ‘publicKey’
and
privateKey = privateKey. This tells
libseaweed` to use your private key as a public key in the transaction.
- Finally, we sign the transaction using the new private key.
Verify the Transaction
To verify that the Segwit transaction was created successfully, you can use the following code:
const { Transaction } = require('libseaweed');
const bitcoreLib = require('./bitcore-lib');
// Create a new wallet (e.g., from a private key)
let wallet;
try {
const privateKey = 'your_private_key_here';
wallet = await bitcoreLib.createWallet(privateKey);
} catch (error) {
console.error(error);
}
// Create a new transaction
const tx = new Transaction(
wallet.address,
[new BitcoindAddress('1.2.3.4:1234'), // sender's address]
);
// Set the Segwit flag for the transaction
tx.setSegwits([
{ type: 'publicKey', privateKey: privateKey },
]);
// Sign the transaction with a new private key (or use an existing one)
const signature = await bitcoreLib.signTransaction(tx, 'your_new_private_key_here');
// Verify the transaction
const verificationTx = new Transaction(
wallet.address,
tx.hash,
);
await verificationTx.verify(signature);
If the transaction is verified successfully, you should see a message indicating that the transaction has been confirmed.