Blockcypher API Transaction Error 400 Status Code

25 Views Asked by At

I am getting 400 Status Code error while doing a transaction from a BTC Wallet to another BTC Wallet.

Error validating generated transaction: Error running script for input 0 referencing 5a500347f5e57b5c3ad5efde8fdacdae9eee819c561ae59b4a38e33943d4b561 at 0: Script was NOT verified successfully.

I am using exactly the same procedure and the code provided by the official documentation. Here is my backend server code.

It's not working on mainnet but it's completely working fine on testnet.


const sendBtcTransaction = async (
  fromAddress,
  toAddress,
  amount,
  privateKey,
  res
) => {
  console.log("Private Key", privateKey);
  try {
    const keyPair = await ECPair.fromPrivateKey(Buffer.from(privateKey, "hex"));

    const amountIn = amount * satoshi;
    var newtx = {
      inputs: [{ addresses: [fromAddress] }],
      outputs: [{ addresses: [toAddress], value: amountIn }],
    };

    const transactionDetail = await axios.post(
      `${process.env.BLOCKCYPHER_URL}txs/new?token=${process.env.BLOCKCYPHER_TOKEN}`,
      JSON.stringify(newtx)
    );
    const tmptx = transactionDetail.data;
    tmptx.pubkeys = [];

    tmptx.signatures = tmptx.tosign.map(function (tosign, n) {
      tmptx.pubkeys.push(keyPair.publicKey.toString("hex"));
      return bitcoin.script.signature
        .encode(keyPair.sign(Buffer.from(tosign, "hex")), 0x01)
        .toString("hex")
        .slice(0, -2);
    });
    console.log(tmptx);

    const finalTransaction = await axios.post(
      `${process.env.BLOCKCYPHER_URL}txs/send?token=${process.env.BLOCKCYPHER_TOKEN}`,
      JSON.stringify(tmptx)
    );

    console.log("Final Transactions Error");

    const transactionData = finalTransaction.data;
    const TransactionHash = transactionData.tx.hash;

    return res.status(200).json({ transactionHash: TransactionHash });
  } catch (error) {
    console.log(
      " ~ file: bitcoin.controller.js ~ line 111 ~ exports.createBTCTransaction= ~ error",
      error.response.data.errors
    );
    return res.status(400).json({ error: error.message });
  }
};

I tried changing it to testnet and it is working on the testnet but it is giving me error on mainnet.

0

There are 0 best solutions below