Wednesday, December 25, 2024

segregated witness – How you can create and signal a segwit transaction utilizing any npm pacakge

So I’m utilizing this NPM bundle ( https://www.npmjs.com/bundle/bitcore-lib )
to create and signal a Bitcoin legacy transaction regionally and it was working effective for legacy.
However once I tried to do the identical for segwit addresses I began getting this error when i submit that uncooked into sendrawtransasction node methodology.

mandatory-script-verify-flag-failed (Witness requires empty scriptSig)

So I feel the difficulty is with this bundle because the construction of this cost will not be what’s required for a SegWit transaction.

So my major query is is there any bundle in javascript I can use to create and signal a transaction? the principle payload I’ve is

receipentAddress, senderAddress, quantity ( in satoshis ), and privateKey in WIF. 

any assist or clue is very appreciated.

//edit:

rawhex:

02000000017d155e9418b60ee26fe189d4381a1fcd5aac1d8eed5216c210e6f6e7a6d86b37010000006a47304402204d6b08238b2eafbf18b7b940a4bbb59c1cc610a1d5e05d2ed72f78f9a6035b890220513a33b2461f12f711776063a54d62913adef76eb40f62ad0eac3f16577c572e01210312bc181f50c9dff7d5ebe77d4df32a9ae2e5b0530b5a4a9c4b8b88647911a883ffffffff029065000000000000160014dc804b4f932f37f77ff9a4f843c845dc3cc3d30164f20300000000001600149518ea8b88e82e82d9003fa3f7518e15180cc58500000000

edit: added code beneath

const axios = require('axios');
const bitcore = require('bitcore-lib');

async operate generateAndSignTransaction(payload) {
  let { deal with, privateKey, recipientAddress, satoshis, chain } = payload;

  attempt {
    async operate charges() {
      attempt {
        let key;
        if (chain === "testnet"){
          key = 'test3'
        }else {
          key = 'major'
        }

        let r = await axios.get(`https://api.blockcypher.com/v1/btc/${key}`);
        let feeObj = new Object;
        feeObj["High_fee"] = r.knowledge.high_fee_per_kb;
        feeObj["Medium_fee"] = r.knowledge.medium_fee_per_kb;
        feeObj["Low_fee"] = r.knowledge.low_fee_per_kb;
        return feeObj;
      } catch (error) {
        console.error(error);
        throw error; // Propagate error
      }
    }


    let res = await charges();
    let charge = res.Low_fee;


    let url = `https://blockstream.information/${chain}/api/deal with/${deal with}/utxo`;
    res = await axios.get(url);

    console.log('UTXOS: ', res.knowledge);


    let i = 0;
    let utxos = [];
    let balance_utxos = 0;

    whereas (balance_utxos <= satoshis + charge && i < res.knowledge.size) {
      let utxo = {
        "txId": res.knowledge[i].txid,
        "outputIndex": res.knowledge[i].vout,
        "script": bitcore.Script.buildPublicKeyHashOut(deal with).toString(),
        "satoshis": res.knowledge[i].worth
      };
      utxos.push(utxo);
      balance_utxos += res.knowledge[i].worth;
      i++;
    }
    console.log('UTXOS 02: ', utxos);

    let tx = new bitcore.Transaction();
    tx.from(utxos);
    tx.to(recipientAddress, satoshis);
    tx.change(deal with);
    tx.charge(charge);
    tx.inputs.forEach(enter => {
      enter.setScript(bitcore.Script.empty());
    });
    tx.signal(privateKey);


    console.log('Tramsactopnm: ', tx);
    console.log('Tramsactopnm knowledge: ', tx.serialize());

    let txnHex = tx.toString();

    return { txid: txnHex }; // Return the serialized transaction
  } catch (error) {
    console.error('Error:', error);
    throw error; // Propagate error
  }
}


module.exports = { generateAndSignTransaction };

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles