bitpay / bitcore

A full stack for bitcoin and blockchain-based applications
https://bitcore.io/
MIT License
4.8k stars 2.07k forks source link

Can this library be used in. bitcoinabc? Signature transaction return error'mandatory-script-verify-flag-failed'(Signature must use SIGHASH_FORKID)' #2048

Open 77381110 opened 5 years ago

77381110 commented 5 years ago

The code :const alice = bitcoin.ECPair.fromPrivateKey(Buffer.from(child.__d, 'hex')) const txb = new bitcoin.TransactionBuilder() txb.setVersion(1); txb.addInput('64d3df9413f527fd86181ac7877330ad2393068033ba41dd1469d489db5b0dcf', 1) txb.addOutput('1PzSzJmdCuTbu27dYxjhWAdGcdya75vMmv', 500000) txb.addOutput('1H3xhcX2N4GEhWdN89vktEWkkS2mjpgG7m', 6141829) txb.sign(0, alice)

The result :0100000001cf0d5bdb89d46914dd41ba3380069323ad307387c71a1886fd27f51394dfd364010000006b483045022100f4c9f4d6b21afc4bd5d22b62a487a78d6b434d7e94aa9dfcc1a32d16f0a4032c022079739b9b9afcec6835b0d45541cb6ce6daaa2911ff7055776b23bf7fcd981f2101210201a7bf096af54f1c325a3ae306e9cfcd5d138f2630aee1182bf981a820250837ffffffff0220a10700000000001976a914fc302bb8d5a04b9dc72b4c1f5c53fe9e5484cad588ac85b75d00000000001976a914b01157358816f433c560162ffea92f29f3bc96a488ac00000000

then sendrawtransaction, Return error: { code: -26, message: '16: mandatory-script-verify-flag-failed (Signature must use SIGHASH_FORKID)' }

How can I use this class library to sign transactions in abc? Thank you.

ljluestc commented 6 months ago

const bitcoin = require('bitcoinjs-lib'); // Import the BitcoinJS library

// Your private key and transaction data
const privateKeyHex = 'your_private_key_hex';
const txHashToSpend = '64d3df9413f527fd86181ac7877330ad2393068033ba41dd1469d489db5b0dcf';

// Create an ECPair from the private key
const alice = bitcoin.ECPair.fromPrivateKey(Buffer.from(privateKeyHex, 'hex'));

// Create a TransactionBuilder
const txb = new bitcoin.TransactionBuilder(bitcoin.networks.bitcoin); // Use the appropriate network

// Add input
txb.addInput(txHashToSpend, 1);

// Add outputs
txb.addOutput('1PzSzJmdCuTbu27dYxjhWAdGcdya75vMmv', 500000);
txb.addOutput('1H3xhcX2N4GEhWdN89vktEWkkS2mjpgG7m', 6141829);

// Sign the transaction with SIGHASH_FORKID flag
txb.sign(0, alice, null, bitcoin.Transaction.SIGHASH_FORKID | bitcoin.Transaction.SIGHASH_ALL);

// Build the transaction
const tx = txb.build();

// Serialize the transaction
const txHex = tx.toHex();

console.log(txHex); // This is the signed transaction in hexadecimal format