35 使用bitcoinjs-lib 进行PSBT创建与广播的问题

在nodejs 中创建psbt 使用mempool的方法进行广播时返回: 'sendrawtransaction RPC error: {"code":-22,"message":"TX decode failed. Make sure the tx has at least one input."}错误:

当前钱包地址类型为:Native Segwit

代码: `import bitcoin from 'bitcoinjs-lib'; import ecc from "@bitcoinerlab/secp256k1"; import {ECPair} from "@unisat/wallet-sdk/lib/bitcoin-core.js"; import mempoolJS from "@mempool/mempool.js";

bitcoin.initEccLib(ecc);

async function start_p2trktr(key,scriptPubKey1) {

const abss = '....';    //从本地获取私钥
const signer1 = ECPair.fromPrivateKey(Buffer.from(abss, 'hex'))

const scriptPubKey = await findValueByKey(scriptPubKey1);   //ScriptPubKey
const scriptPubKeyBuffer = Buffer.from(scriptPubKey, 'hex');
const network = bitcoin.networks.bitcoin; 

const psbt = new bitcoin.Psbt({ network });
psbt.addInput({
    hash : '50b49b0892382ed3e9c8727355e8a8fbedca1dc10773c159f4ec99ff25baf8ea',
    index : 0,
    witnessUtxo: {
        script: scriptPubKeyBuffer,
        value: 702655
    }
});
psbt.addOutput({
    address: 'bc1qqw9mj873usx0v28yaszx3jppqdkach7g2fkfvh',
    value: 702000
});
//签名
psbt.signInput(0, signer1);

console.log(psbt.toBase64());
psbt.finalizeAllInputs();
console.log(psbt.toBase64());

transactionPSBT(psbt.toString('hex'))

}

async function transactionPSBT(txHex) {

const { bitcoin: { transactions } } = mempoolJS({
    hostname: 'mempool.space'
});
const txid = await transactions.postTx({ txHex });
console.log(txid);

};`

返回错误: data: 'sendrawtransaction RPC error: {"code":-22,"message":"TX decode failed. Make sure the tx has at least one input."}'

请问问题出现在哪里?我应该如何更正呢

请先 登录 后评论

3 个回答

Wade - Footprint Analytics CTO
  擅长:数据分析,GameFi,NFT
请先 登录 后评论
Wade - Footprint Analytics CTO
  擅长:数据分析,GameFi,NFT
请先 登录 后评论
NPC.李括
请先 登录 后评论
  • 2 关注
  • 0 收藏,1740 浏览
  • 用户_20097 提出于 2024-05-07 15:42