<p>Regarding first question,</p>
<p>No, you don't have to run your own node. What I know is blockstream esplora provides public API with <a href="https://github.com/Blockstream/esplora/blob/master/API.md" rel="nofollow noreferrer">documentation</a> you can work with.</p>
<p>Regarding second question,</p>
<p>It depends on what kind of segwit address(p2wpkh, p2wsh, p2tr) it is, but I guess what you want to spend would be the most common segwit transaction, which is p2wpkh(single signature with public key).</p>
<p>For p2wpkh segwit spend, you can refer below code working for nodejs, which you can find <a href="https://bitcoin.stackexchange.com/questions/123028/how-to-create-and-sign-a-segwit-transaction-using-any-npm-pacakge/123030#123030">here</a>, too.</p>
<pre class="lang-javascript prettyprint-override"><code>import * as bitcoin from 'bitcoin-sdk-js';

const privkey =
  'e6c58327b4bf1314296bba7adb66f94c36ea5611ac07830c229aa7ee6f9caac3';
const toAddress = 'tb1qmjqyknun9umlwlle5nuy8jz9ms7v85cp7k2nfe';
const toValue = 300000; // unit is satoshi
const txHash =
  '8bcd197873dbff5cc67cb3e0b1cbbdf097c7af94b0e9365adecb424e408294bf';
const inputValue = 310981; // fee is input value - total value

const tx = new bitcoin.Transaction();

await tx.addInput({
  txHash: txHash,
  index: n, // it should be vout of utxo, index of utxo in transation
  value: inputValue,
});
await tx.addOutput({
  address: toAddress,
  value: toValue,
});
await tx.signInput(privkey, 0);

const txHex = await tx.getSignedHex();

</code></pre>
<p>For other type of segwit address, it really varies on unlocking script, so there's not really one way solution for it.</p>

CC BY-SA 4.0
2024-06-16T05:47:14.617Z
2024-06-16T07:28:52.860Z
2024-06-16T07:28:52.860Z
Hyunhum Cho
<p>I have a segwit address (starts with bc...) and its private key, that holds some BTC value.
Using <code>node.js</code> I would like to transfer all my funds to another address.
This should be pretty straight forward - create a transaction  and broadcast to the network.
I've found a few libraries that does it but for some reason was not able to successfully execute.</p>
<p>2 questions:</p>
<ol>
<li>Must I have a running Bitcoin node for that or is there some &quot;public&quot; node which I can work with?</li>
<li>Is there a working code reference for this simple transaction? Suppose that my wallet has 0.001 (unspent) BTC, how do I transfer this amount to another address? How does the fee calculation come to place?</li>
</ol>

<transaction-fees><raw-transaction>
Transfer all funds to another wallet with node.js
forhas
# Transfer all funds to another wallet with node.js

(Asked by: username_0 on 2022-01-02)

I have a segwit address (starts with bc...) and its private key, that holds some BTC value.
Using `node.js` I would like to transfer all my funds to another address.
This should be pretty straight forward - create a transaction and broadcast to the network.
I've found a few libraries that does it but for some reason was not able to successfully execute.

2 questions:

1. Must I have a running Bitcoin node for that or is there some "public" node which I can work with?
2. Is there a working code reference for this simple transaction? Suppose that my wallet has 0.001 (unspent) BTC, how do I transfer this amount to another address? How does the fee calculation come to place?

**Tags:** transaction-fees, raw-transaction


---

## 1 answers

### Answer by username_1 on 2024-06-16. Score: 0

Regarding first question,
No, you don't have to run your own node. What I know is blockstream esplora provides public API with documentation you can work with.
Regarding second question,
It depends on what kind of segwit address(p2wpkh, p2wsh, p2tr) it is, but I guess what you want to spend would be the most common segwit transaction, which is p2wpkh(single signature with public key).
For p2wpkh segwit spend, you can refer below code working for nodejs, which you can find here, too.
import * as bitcoin from 'bitcoin-sdk-js';

const privkey =
  'e6c58327b4bf1314296bba7adb66f94c36ea5611ac07830c229aa7ee6f9caac3';
const toAddress = 'tb1qmjqyknun9umlwlle5nuy8jz9ms7v85cp7k2nfe';
const toValue = 300000; // unit is satoshi
const txHash =
  '8bcd197873dbff5cc67cb3e0b1cbbdf097c7af94b0e9365adecb424e408294bf';
const inputValue = 310981; // fee is input value - total value

const tx = new bitcoin.Transaction();

await tx.addInput({
  txHash: txHash,
  index: n, // it should be vout of utxo, index of utxo in transation
  value: inputValue,
});
await tx.addOutput({
  address: toAddress,
  value: toValue,
});
await tx.signInput(privkey, 0);

const txHex = await tx.getSignedHex();


For other type of segwit address, it really varies on unlocking script, so there's not really one way solution for it.


---

