bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

saimrahdari opened this issue · comments

I am using Blockcypher API's which uses bitcoinjs-lib at the backend and i am getting this error when i am broadcasting my transaction to blockchain.

Created an express server for the backend.

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("TMP TX INPUTS" + JSON.stringify(tmptx.tx.inputs[0]));
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 });
}
};

This is not a support forum for BlockCypher.

How can I possibly know if your code is correct if the most important parts are hidden behind an API?

Ask BlockCypher support.