Monday, November 25, 2024
HomeBitcointransactions - Lacking Witness subject in p2wpkh decoded tx hex

transactions – Lacking Witness subject in p2wpkh decoded tx hex


I’ve positioned collectively a uncooked tx hex however when it is unable to be broadcasted as I maintain getting an error under

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

I then decoded the tx hex utilizing https://reside.blockcypher.com/btc/decodetx/ to see that I appear to be lacking the Witness subject as I’m utilizing native segwit addresses:

{
    "addresses": [
        "bc1qj3rjxzhlqm6fqesd5fwphjnf457zkww3s5qrv5",
        "bc1qjp3cch0hdvdr0hy99cs76kl9kekmuxv3k9nr6l"
    ],
    "block_height": -1,
    "block_index": -1,
    "confirmations": 0,
    "double_spend": false,
    "charges": 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",
            "worth": 60000
        }
    ],
    "choice": "low",
    "acquired": "2021-09-05T10:17:14.395062568Z",
    "relayed_by": "3.82.154.159",
    "dimension": 188,
    "complete": 60000,
    "ver": 2,
    "vin_sz": 1,
    "vout_sz": 1,
    "vsize": 188
}

I then thought maybe I did not add a scriptPubKey into the addInput however this code did not appear to work for me:

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

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

async perform getPubAddInfo() {
  const response = await fetch(url + pubAdd)
  const information = await response.json()

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

async perform 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.signal(0, keypairSpend, null, null, 60200)

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

getTXHash()

In the mean time I get the error TypeError: Can not learn property 'output' of undefined on the road the place i create var scriptPubKey.

Any recommendations as to how I can add the Witness subject into the tx? Is it as a result of I’m improperly including the scriptPubKey? I’m utilizing bitcoinjs 5.2.0

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments