<p>"fundrawtransaction" and "walletcreatefundedpsbt" are <em>wallet</em> RPCs. They add inputs and change outputs <em>from your wallet</em> to aim for a specific feerate for the transaction.</p>

<p>These operations inherently require the relevant wallet. Change addresses are generated by the sender's wallet, and inputs have to come from the wallet of the sender. If you don't have that wallet, you simply do not have access to that information.</p>

<p>I don't know what advice to give you except to run these RPCs on a  instance of Bitcoin Core that actually has access to the relevant wallet.</p>

<p>If you do things entirely manually, the fee is the difference between the inputs and outputs of the transaction. If that fee value for the transaction you have is not desirable, you'll need to create a different transaction, with either different inputs, or by adding change yourself back to your own wallet.</p>

CC BY-SA 4.0
2019-12-30T15:11:34.007Z
2019-12-30T15:34:00.577Z
2019-12-30T15:34:00.577Z
Pieter Wuille
<blockquote>
  <p>So how we can calculate txn fee before signing and sending txn for external addresses?</p>
</blockquote>

<p>As a reference:</p>

<pre><code>           P2PKH   P2SH-P2WPKH  P2WPKH
        -------------------------------
 Input :  592 WU     364 WU     272 WU
Output :  136 WU     128 WU     124 WU
</code></pre>

<p>All transactions include 40 WU by default. If your transaction spends a P2SH-P2WPKH or P2WPKH output, you will need to add an additional 2 WU. To convert from Weight Units to virtual size, divide by 4 and round up.</p>

<ul>
<li>For a standard transaction spending a P2PKH input and having 2 P2PKH outputs, the weight is 40+592+136+136 = 904 WU. A standard P2PKH transaction is 904/4 = <strong>226 vB</strong>.</li>
<li>For a standard transaction spending a P2SH-P2WPKH input and having 2 P2SH-P2WPKH outputs, the weight is 40+2+364+128+128 = 662 WU. A standard P2SH-P2WPKH transaction is 662/4 = <strong>166 vB</strong>.</li>
<li>For a standard transaction spending a P2WPKH input and having 2 P2WPKH outputs, the weight is 40+2+272+124+124 = 562 WU. A standard P2WPKH transaction is 562/4 = <strong>141 vB</strong>.</li>
</ul>

<p>These amounts are calculated based on pre-segwit data having a size of 4 WU/byte and segwit data having a size of 1 WU/byte. It is best to assume that signatures will be the largest they can be (72 bytes) since their size may vary by 1 byte. We overestimate the vB size so that a fee of 1 sat/vB is enough in case the signature is in fact +1 byte larger.</p>

<h3>P2PKH Addresses</h3>

<pre><code>    INPUT (592 WU)
Outpoint   (144 WU): [32-byte txhash] [4-byte txindex]
ScriptLength (4 WU): 0x6b
ScriptSig  (428 WU): OP_PUSHBYTES_72 [71-byte signature] SIGHASH_TYPE OP_PUSHBYTES_33 [33-byte pubkey]
Sequence    (16 WU): [4-byte value]

    OUTPUT (136 WU)
Value         (32 WU): [8-byte value]
ScriptLength   (4 WU): 0x19
ScriptPubKey (100 WU): OP_DUP OP_HASH160 OP_PUSHBYTES_20 [20-byte pubkey] OP_EQUALVERIFY OP_CHECKSIG
</code></pre>

<h3>P2SH-P2WPKH Addresses</h3>

<pre><code>    INPUT (364 WU)
Outpoint     (144 WU): [32-byte txhash] [4-byte txindex]
ScriptLength   (4 WU): 0x17
ScriptSig     (92 WU): OP_PUSHBYTES_22 [22-byte redeemscript]
Sequence      (16 WU): [4-byte value]
Witness Data (108 WU): NUM_PUSHES_2 DATA_PUSH_72 [71-byte signature] SIGHASH_TYPE DATA_PUSH_33 [33-byte pubkey]

    OUTPUT (128 WU)
Value        (32 WU): [8-byte value]
ScriptLength  (4 WU): 0x17
ScriptPubKey (92 WU): OP_HASH160 OP_PUSHBYTES_20 [20-byte scripthash] OP_EQUAL
</code></pre>

<h3>P2WPKH Addresses</h3>

<pre><code>    INPUT (272 WU)
Outpoint     (144 WU): [32-byte txhash] [4-byte txindex]
ScriptLength   (4 WU): 0x00
ScriptSig      (0 WU): N/A
Sequence      (16 WU): [4-byte value]
Witness Data (108 WU): NUM_PUSHES_2 DATA_PUSH_72 [71-byte signature] SIGHASH_TYPE DATA_PUSH_33 [33-byte pubkey]

    OUTPUT (124 WU)
Value        (32 WU): [8-byte value]
ScriptLength  (4 WU): 0x16
ScriptPubKey (88 WU): OP_0 OP_PUSHBYTES_20 [20-byte pubkeyhash]
</code></pre>

CC BY-SA 4.0
2019-12-31T23:10:57.357Z
2019-12-31T23:10:57.357Z
Septem151
<p>I am doing transaction on addresses which are created outside of my local bitcoin-core server using third party lib. I am having its private/public key. I am 
 able to create raw transaction using "createrawtransaction" and also able to sign and send txn using "signrawtransactionwithkey" &amp; "sendrawtransaction" but when i am trying to calculate txn fee and adding change address using "fundrawtransaction" or  "walletcreatefundedpsbt", am getting error as "insufficient fund" but i have enough unspent btc on my addresses. So how we can calculate txn fee before signing and sending txn for external addresses? </p>

<bitcoin-core><transactions><address><transaction-fees>
Calculate transaction fee for external addresses which doesn't belong to my local bitcoin-core server?
Jvd
# Calculate transaction fee for external addresses which doesn't belong to my local bitcoin-core server?

(Asked by: username_0 on 2019-12-30)

I am doing transaction on addresses which are created outside of my local bitcoin-core server using third party lib. I am having its private/public key. I am
able to create raw transaction using "createrawtransaction" and also able to sign and send txn using "signrawtransactionwithkey" & "sendrawtransaction" but when i am trying to calculate txn fee and adding change address using "fundrawtransaction" or "walletcreatefundedpsbt", am getting error as "insufficient fund" but i have enough unspent btc on my addresses. So how we can calculate txn fee before signing and sending txn for external addresses?

**Tags:** bitcoin-core, transactions, address, transaction-fees


---

## 2 answers

### Answer by username_1 on 2019-12-30. Score: 1

"fundrawtransaction" and "walletcreatefundedpsbt" are *wallet* RPCs. They add inputs and change outputs *from your wallet* to aim for a specific feerate for the transaction.

These operations inherently require the relevant wallet. Change addresses are generated by the sender's wallet, and inputs have to come from the wallet of the sender. If you don't have that wallet, you simply do not have access to that information.

I don't know what advice to give you except to run these RPCs on a instance of Bitcoin Core that actually has access to the relevant wallet.

If you do things entirely manually, the fee is the difference between the inputs and outputs of the transaction. If that fee value for the transaction you have is not desirable, you'll need to create a different transaction, with either different inputs, or by adding change yourself back to your own wallet.


---

### Answer by username_2 on 2019-12-31. Score: 1

> So how we can calculate txn fee before signing and sending txn for external addresses?

As a reference:

```
           P2PKH   P2SH-P2WPKH  P2WPKH
        -------------------------------
 Input :  592 WU     364 WU     272 WU
Output :  136 WU     128 WU     124 WU

```

All transactions include 40 WU by default. If your transaction spends a P2SH-P2WPKH or P2WPKH output, you will need to add an additional 2 WU. To convert from Weight Units to virtual size, divide by 4 and round up.

* For a standard transaction spending a P2PKH input and having 2 P2PKH outputs, the weight is 40+592+136+136 = 904 WU. A standard P2PKH transaction is 904/4 = **226 vB**.
* For a standard transaction spending a P2SH-P2WPKH input and having 2 P2SH-P2WPKH outputs, the weight is 40+2+364+128+128 = 662 WU. A standard P2SH-P2WPKH transaction is 662/4 = **166 vB**.
* For a standard transaction spending a P2WPKH input and having 2 P2WPKH outputs, the weight is 40+2+272+124+124 = 562 WU. A standard P2WPKH transaction is 562/4 = **141 vB**.

These amounts are calculated based on pre-segwit data having a size of 4 WU/byte and segwit data having a size of 1 WU/byte. It is best to assume that signatures will be the largest they can be (72 bytes) since their size may vary by 1 byte. We overestimate the vB size so that a fee of 1 sat/vB is enough in case the signature is in fact +1 byte larger.

### P2PKH Addresses

```
    INPUT (592 WU)
Outpoint   (144 WU): [32-byte txhash] [4-byte txindex]
ScriptLength (4 WU): 0x6b
ScriptSig  (428 WU): OP_PUSHBYTES_72 [71-byte signature] SIGHASH_TYPE OP_PUSHBYTES_33 [33-byte pubkey]
Sequence    (16 WU): [4-byte value]

    OUTPUT (136 WU)
Value         (32 WU): [8-byte value]
ScriptLength   (4 WU): 0x19
ScriptPubKey (100 WU): OP_DUP OP_HASH160 OP_PUSHBYTES_20 [20-byte pubkey] OP_EQUALVERIFY OP_CHECKSIG

```

### P2SH-P2WPKH Addresses

```
    INPUT (364 WU)
Outpoint     (144 WU): [32-byte txhash] [4-byte txindex]
ScriptLength   (4 WU): 0x17
ScriptSig     (92 WU): OP_PUSHBYTES_22 [22-byte redeemscript]
Sequence      (16 WU): [4-byte value]
Witness Data (108 WU): NUM_PUSHES_2 DATA_PUSH_72 [71-byte signature] SIGHASH_TYPE DATA_PUSH_33 [33-byte pubkey]

    OUTPUT (128 WU)
Value        (32 WU): [8-byte value]
ScriptLength  (4 WU): 0x17
ScriptPubKey (92 WU): OP_HASH160 OP_PUSHBYTES_20 [20-byte scripthash] OP_EQUAL

```

### P2WPKH Addresses

```
    INPUT (272 WU)
Outpoint     (144 WU): [32-byte txhash] [4-byte txindex]
ScriptLength   (4 WU): 0x00
ScriptSig      (0 WU): N/A
Sequence      (16 WU): [4-byte value]
Witness Data (108 WU): NUM_PUSHES_2 DATA_PUSH_72 [71-byte signature] SIGHASH_TYPE DATA_PUSH_33 [33-byte pubkey]

    OUTPUT (124 WU)
Value        (32 WU): [8-byte value]
ScriptLength  (4 WU): 0x16
ScriptPubKey (88 WU): OP_0 OP_PUSHBYTES_20 [20-byte pubkeyhash]

```


---

