Error validating generated transaction: Error running script for input 0 referencing
skullpunks opened this issue · 0 comments
I have exported the extended private key from mobile wallet and tried to convert to get the private the key, private key wif and address with following code
const ecc = require('tiny-secp256k1')
const { BIP32Factory } = require('bip32')
const { ECPairFactory } = require('ecpair');
// You must wrap a tiny-secp256k1 compatible implementation
const bip32 = BIP32Factory(ecc)
const bitcoin = require('bitcoinjs-lib');
const ECPair = ECPairFactory(ecc);
// Extended private key
const xprv = 'xprv_MY_EXTEND_PRIVATE_KEY';
// Derive the private key for a specific address using path
const node = bip32.fromBase58(xprv, bitcoin.networks.mainnet).derivePath("m/44'/0'/0'/0/0");
//const node = bip32.fromBase58(xprv, bitcoin.networks.mainnet);
const privateKey = node.privateKey;
// Convert the private key to a private WIF
const privateWif = ECPair.fromPrivateKey(privateKey).toWIF();
// Derive the public key and Bech32 address
const publicKey = ECPair.fromPrivateKey(privateKey).publicKey;
const { address } = bitcoin.payments.p2wpkh({ pubkey: publicKey });
console.log("Private Key: ", privateKey.toString('hex'));
console.log("Private WIF: ", privateWif);
console.log("Public Key: ", publicKey.toString('hex'));
console.log("Bech32 Address: ", address);
Then I have tried to send a transaction using the following code
`const fromAddress = keys.BITCOIN_PUBLIC_KEY.key;
const ECPair = ECPairFactory(ecc);
const MAINNNET = bitcoin.networks.mainnet;
const keyPair = ECPair.fromWIF(keys.BITCOIN_PRIVATE_KEY.key, MAINNNET);
console.log("fromAddress", fromAddress);
const payload = {
inputs: [{ addresses: [fromAddress] }],
outputs: [
{
addresses: [toAddress],
value: parseInt(Math.floor(amount * Math.pow(10, 8)).toString()),
},
],
};
const response = await axios.post(
`https://api.blockcypher.com/v1/btc/main/txs/new`,
JSON.stringify(payload)
);
let unsignedTx = response.data;
unsignedTx.pubkeys = [];
unsignedTx.signatures = unsignedTx.tosign.map((tosign: any, n: any) => {
unsignedTx.pubkeys.push(keyPair.publicKey.toString("hex"));
let signature = keyPair.sign(Buffer.from(tosign, "hex"));
let encodedSignature = bitcoin.script.signature.encode(
signature,
bitcoin.Transaction.SIGHASH_ALL
);
let hexStr = encodedSignature.toString("hex")
.slice(0, -2);
return hexStr;
});
console.log("unsignedTx i", unsignedTx.tx.inputs);
console.log("unsignedTx o", unsignedTx.tx.outputs);
console.log("unsignedTx", unsignedTx);
const signedTx = await axios.post(
`https://api.blockcypher.com/v1/btc/main/txs/send`,
JSON.stringify(unsignedTx)
);`
But my transaction fails with follow error
I am using bech32 address.
tx: { block_height: -1, block_index: -1, hash: 'c8b1d893d886abbdff2ec305c802adfa9f6147c8543b040e6cf0959865790e67', addresses: [ 'bc1qqhgpzdlzxcls989vhrpedhmwjeycmy3hcm5d59', '1JhtjhzorSGQ7t8KxoctGRog3VLWgxt9mp' ], total: 18095, fees: 3800, size: 119, vsize: 119, preference: 'low', relayed_by: '49.207.209.98', received: '2023-02-03T15:31:40.606564955Z', ver: 1, double_spend: false, vin_sz: 1, vout_sz: 2, confirmations: 0, inputs: [ [Object] ], outputs: [ [Object], [Object] ] }, tosign: [ '249967a28396e33b633d7bdd8e6a3aac5b4f4e9a1dd2ec1fc5bb86bd2cf7190f' ], pubkeys: [ '032656281655b3413444770fd33d3b84bf7f044e293822bac732941fbe1731ed66' ], signatures: [ '304402202773b77d472311817510752984658f05f053ccefa5dc4c598ed876fbc2ef10d402201ab0da62a494cf6daf66cc25ee9a514eb458a05637e7c30e1f3264b3e3631ab2' ] }
Below is the error
{ error: 'Error validating generated transaction: Error running script for input 0 referencing f58c711f9fce2b1417503d0f21d356c637961515a0b32838bfeacccec629 5a25 at 14: Script was NOT verified successfully.'
Any help appreciated.