<p>How are you signing it? Also what version of bitcoinjs are you using? Looks like an older one (v3.3?)prior to PSBTs. It might be a little different depending on version. Spending from segwit addresses (p2sh or native) you must specify <strong>inputvalue</strong>, this is the full value of the unspent output being spent.</p>
<p><code>txb.sign(0, keypairSpend, null, null, inputvalue); </code></p>
<p>As of v.4.03 for an example spending from p2wpkh with the latest transaction builder see their github reference docs at <a href="https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts#L11" rel="nofollow noreferrer">https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts#L11</a></p>

CC BY-SA 4.0
2021-09-07T00:24:59.743Z
I have updated my original question to show you the full code I am using. I am also using bitcoinjs v5.2.0
jiamijiang
CC BY-SA 4.0
2021-09-06T04:17:41.937Z
2021-09-06T04:17:41.937Z
m1xolyd1an
<p>This is likely a problem with the version of bitcoinjs the OP is using.</p>
<p>A similar issue is discussed <a href="https://github.com/bitcoinjs/bitcoinjs-lib/issues/894#issue-258295050" rel="nofollow noreferrer">on Github</a>.</p>
<p>To fix it, the user had to update.</p>
<p>An alternative might be to update the problem line to something like the old pattern:</p>
<pre class="lang-js prettyprint-override"><code>var redeemScript = bitcoin.script.witnessPubKeyHashOutput(pubKeyHash)
var redeemScriptHash = bitcoin.crypto.hash160(redeemScript)
</code></pre>

CC BY-SA 4.0
2024-05-24T07:23:22.947Z
2024-05-24T07:23:22.947Z
Lee
<p>I have placed together a raw tx hex but when it's unable to be broadcasted as I keep getting an error below</p>
<pre><code>sendrawtransaction RPC error: {&quot;code&quot;:-25,&quot;message&quot;:&quot;bad-txns-inputs-missingorspent&quot;}
</code></pre>
<p>I then decoded the tx hex using <a href="https://live.blockcypher.com/btc/decodetx/" rel="nofollow noreferrer">https://live.blockcypher.com/btc/decodetx/</a> to see that I seem to be missing the Witness field as I am using native segwit addresses:</p>
<pre><code>{
    &quot;addresses&quot;: [
        &quot;bc1qj3rjxzhlqm6fqesd5fwphjnf457zkww3s5qrv5&quot;,
        &quot;bc1qjp3cch0hdvdr0hy99cs76kl9kekmuxv3k9nr6l&quot;
    ],
    &quot;block_height&quot;: -1,
    &quot;block_index&quot;: -1,
    &quot;confirmations&quot;: 0,
    &quot;double_spend&quot;: false,
    &quot;fees&quot;: 200,
    &quot;hash&quot;: &quot;88420211f5ea53174fc6a708c00ca2e840d687591573d0ecd2464fae20d139a7&quot;,
    &quot;inputs&quot;: [
        {
            &quot;addresses&quot;: [
                &quot;bc1qj3rjxzhlqm6fqesd5fwphjnf457zkww3s5qrv5&quot;
            ],
            &quot;age&quot;: 696993,
            &quot;output_index&quot;: 0,
            &quot;output_value&quot;: 60200,
            &quot;prev_hash&quot;: &quot;74e24a2d50ba64abc099126c1c208b5f7156e3ad515b86f3edcca3f3d4291f66&quot;,
            &quot;script&quot;: &quot;4730440220516e15b6354f17e3f02711107624005e6ccd26cf61ef21b35e91315dfbd33e9f022049dadb73ca3e814de9da0b35ff20a8ca82147dda138ede948ccac5621a5bc1bb01210384485d43e938f8c68c4ac903d4d5a97fe842f66c12b860d8d2e1242d2b01fc1f&quot;,
            &quot;script_type&quot;: &quot;pay-to-witness-pubkey-hash&quot;,
            &quot;sequence&quot;: 4294967295
        }
    ],
    &quot;outputs&quot;: [
        {
            &quot;addresses&quot;: [
                &quot;bc1qjp3cch0hdvdr0hy99cs76kl9kekmuxv3k9nr6l&quot;
            ],
            &quot;script&quot;: &quot;001490638c5df76b1a37dc852e21ed5be5b66dbe1991&quot;,
            &quot;script_type&quot;: &quot;pay-to-witness-pubkey-hash&quot;,
            &quot;value&quot;: 60000
        }
    ],
    &quot;preference&quot;: &quot;low&quot;,
    &quot;received&quot;: &quot;2021-09-05T10:17:14.395062568Z&quot;,
    &quot;relayed_by&quot;: &quot;3.82.154.159&quot;,
    &quot;size&quot;: 188,
    &quot;total&quot;: 60000,
    &quot;ver&quot;: 2,
    &quot;vin_sz&quot;: 1,
    &quot;vout_sz&quot;: 1,
    &quot;vsize&quot;: 188
}
</code></pre>
<p>I then thought perhaps I didn't add a <code>scriptPubKey</code> into the <code>addInput</code> but this code didn't seem to work for me:</p>
<pre><code>const fetch = require(&quot;node-fetch&quot;)
const bitcoin = require(&quot;bitcoinjs-lib&quot;)
const url = &quot;https://blockchain.info/rawaddr/&quot;
let MAINNET = bitcoin.networks.bitcoin
pubAdd = &quot;bc1qj3rjxzhlqm6fqesd5fwphjnf457zkww3s5qrv5&quot;

let txb = new bitcoin.TransactionBuilder(MAINNET)
var txid = null
var outn = 0
var transactionhex = &quot;&quot;

async function getPubAddInfo() {
  const response = await fetch(url + pubAdd)
  const data = await response.json()

  txid = data[&quot;txs&quot;][0][&quot;hash&quot;]
  outn = data[&quot;txs&quot;][0][&quot;out&quot;][0][&quot;n&quot;]
}

async function getTXHash() {
  await getPubAddInfo()

  let WIF = &quot;xxxxxxxxxxxx&quot;
  let keypairSpend = bitcoin.ECPair.fromWIF(WIF, MAINNET)

  var scriptPubkey = bitcoin.script.witnessPubKeyHash.output.encode(bitcoin.crypto.hash160(keypairSpend.getPublicKeyBuffer()))

  txb.addInput(txid, outn, null, scriptPubkey)

  txb.addOutput(&quot;bc1qjp3cch0hdvdr0hy99cs76kl9kekmuxv3k9nr6l&quot;, 60000)

  txb.sign(0, keypairSpend, null, null, 60200)

  let tx = txb.build()
  transactionhex = tx.toHex()
  console.log(transactionhex)
}

getTXHash()
</code></pre>
<p>At the moment I get the error <code>TypeError: Cannot read property 'output' of undefined</code> on the line where i create <code>var scriptPubKey</code>.</p>
<p>Any suggestions as to how I can add the Witness field into the tx? Is it because I am improperly adding the scriptPubKey? I am using bitcoinjs 5.2.0</p>

CC BY-SA 4.0
2021-09-05T16:08:28.787Z
Are you actually signing the transaction?
Pieter Wuille
CC BY-SA 4.0
2021-09-07T00:15:25.043Z
@PieterWuille Yes, I have it like this: 

`let WIF = "xxxxxxxxx"` , `let keypairSpend = bitcoin.ECPair.fromWIF(WIF, MAINNET)` , `txb.sign(0, keypairSpend, null, null, 60200)`
jiamijiang
CC BY-SA 4.0
2021-09-07T00:24:17.183Z
@PieterWuille I have updated my original question above to show you the full code I am using.
jiamijiang
<transactions><segregated-witness><bitcoinjs><p2wpkh><witness-data>
Missing Witness field in p2wpkh decoded tx hex
jiamijiang
# Missing Witness field in p2wpkh decoded tx hex

(Asked by: username_0 on 2021-09-05)

I have placed together a raw tx hex but when it's unable to be broadcasted as I keep getting an error below

```
sendrawtransaction RPC error: {"code":-25,"message":"bad-txns-inputs-missingorspent"}

```

I then decoded the tx hex using <https://live.blockcypher.com/btc/decodetx/> to see that I seem to be missing the Witness field as I am using native segwit addresses:

```
{
    "addresses": [
        "bc1qj3rjxzhlqm6fqesd5fwphjnf457zkww3s5qrv5",
        "bc1qjp3cch0hdvdr0hy99cs76kl9kekmuxv3k9nr6l"
    ],
    "block_height": -1,
    "block_index": -1,
    "confirmations": 0,
    "double_spend": false,
    "fees": 200,
    "hash": "88420211f5ea53174fc6a708c00ca2e840d687591573d0ecd2464fae20d139a7",
    "inputs": [
        {
            "addresses": [
                "bc1qj3rjxzhlqm6fqesd5fwphjnf457zkww3s5qrv5"
            ],
            "age": 696993,
            "output_index": 0,
            "output_value": 60200,
            "prev_hash": "74e24a2d50ba64abc099126c1c208b5f7156e3ad515b86f3edcca3f3d4291f66",
            "script": "4730440220516e15b6354f17e3f02711107624005e6ccd26cf61ef21b35e91315dfbd33e9f022049dadb73ca3e814de9da0b35ff20a8ca82147dda138ede948ccac5621a5bc1bb01210384485d43e938f8c68c4ac903d4d5a97fe842f66c12b860d8d2e1242d2b01fc1f",
            "script_type": "pay-to-witness-pubkey-hash",
            "sequence": 4294967295
        }
    ],
    "outputs": [
        {
            "addresses": [
                "bc1qjp3cch0hdvdr0hy99cs76kl9kekmuxv3k9nr6l"
            ],
            "script": "001490638c5df76b1a37dc852e21ed5be5b66dbe1991",
            "script_type": "pay-to-witness-pubkey-hash",
            "value": 60000
        }
    ],
    "preference": "low",
    "received": "2021-09-05T10:17:14.395062568Z",
    "relayed_by": "3.82.154.159",
    "size": 188,
    "total": 60000,
    "ver": 2,
    "vin_sz": 1,
    "vout_sz": 1,
    "vsize": 188
}

```

I then thought perhaps I didn't add a `scriptPubKey` into the `addInput` but this code didn't seem to work for me:

```
const fetch = require("node-fetch")
const bitcoin = require("bitcoinjs-lib")
const url = "https://blockchain.info/rawaddr/"
let MAINNET = bitcoin.networks.bitcoin
pubAdd = "bc1qj3rjxzhlqm6fqesd5fwphjnf457zkww3s5qrv5"

let txb = new bitcoin.TransactionBuilder(MAINNET)
var txid = null
var outn = 0
var transactionhex = ""

async function getPubAddInfo() {
  const response = await fetch(url + pubAdd)
  const data = await response.json()

  txid = data["txs"][0]["hash"]
  outn = data["txs"][0]["out"][0]["n"]
}

async function getTXHash() {
  await getPubAddInfo()

  let WIF = "xxxxxxxxxxxx"
  let keypairSpend = bitcoin.ECPair.fromWIF(WIF, MAINNET)

  var scriptPubkey = bitcoin.script.witnessPubKeyHash.output.encode(bitcoin.crypto.hash160(keypairSpend.getPublicKeyBuffer()))

  txb.addInput(txid, outn, null, scriptPubkey)

  txb.addOutput("bc1qjp3cch0hdvdr0hy99cs76kl9kekmuxv3k9nr6l", 60000)

  txb.sign(0, keypairSpend, null, null, 60200)

  let tx = txb.build()
  transactionhex = tx.toHex()
  console.log(transactionhex)
}

getTXHash()

```

At the moment I get the error `TypeError: Cannot read property 'output' of undefined` on the line where i create `var scriptPubKey`.

Any suggestions as to how I can add the Witness field into the tx? Is it because I am improperly adding the scriptPubKey? I am using bitcoinjs 5.2.0

**Tags:** transactions, segregated-witness, bitcoinjs, p2wpkh, witness-data

> **Comments:**
> **username_1**: Are you actually signing the transaction?  
> **username_0**: @username_1 Yes, I have it like this:   `let WIF = "xxxxxxxxx"` , `let keypairSpend = bitcoin.ECPair.fromWIF(WIF, MAINNET)` , `txb.sign(0, keypairSpend, null, null, 60200)`  
> **username_0**: @username_1 I have updated my original question above to show you the full code I am using.  

---

## 2 answers

### Answer by username_2 on 2021-09-06. Score: 0

How are you signing it? Also what version of bitcoinjs are you using? Looks like an older one (v3.3?)prior to PSBTs. It might be a little different depending on version. Spending from segwit addresses (p2sh or native) you must specify **inputvalue**, this is the full value of the unspent output being spent.

`txb.sign(0, keypairSpend, null, null, inputvalue);`

As of v.4.03 for an example spending from p2wpkh with the latest transaction builder see their github reference docs at <https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts#L11>

> **Comments:**
> **username_0**: I have updated my original question to show you the full code I am using. I am also using bitcoinjs v5.2.0  

---

### Answer by username_3 on 2024-05-24. Score: 0

This is likely a problem with the version of bitcoinjs the OP is using.

A similar issue is discussed [on Github](https://github.com/bitcoinjs/bitcoinjs-lib/issues/894#issue-258295050).

To fix it, the user had to update.

An alternative might be to update the problem line to something like the old pattern:

```js
var redeemScript = bitcoin.script.witnessPubKeyHashOutput(pubKeyHash)
var redeemScriptHash = bitcoin.crypto.hash160(redeemScript)

```


---

