bitcoin-php icon indicating copy to clipboard operation
bitcoin-php copied to clipboard

Error broadcasting transaction: Code: -26, Error: non-mandatory-script-verify-flag (Witness requires empty scriptSig)

Open hussainSheikh123 opened this issue 1 year ago • 0 comments

hey @afk11 I am stuck in an error in main, can you help me? This is my code. $oldTransactions = file_get_contents('https://blockchain.info/rawaddr/15716aoSwq1VuqZgxbTSEXg7XAknmRVmE7'); $oldTransactions = json_decode($oldTransactions, true); $wif = 'xxxxx'; $txid = $oldTransactions['txs'][0]['hash']; $address = '1LDQAktLU5Wcw55CU3nzNE6RqVnCPFWzdQ';

$privKeyFactory = new PrivateKeyFactory;
$key = $privKeyFactory->fromWif($wif);

$witnessScript = new WitnessScript(
    ScriptFactory::scriptPubKey()->payToPubKeyHash($key->getPubKeyHash())
);

$dest = new SegwitAddress(
    WitnessProgram::v0(
        (new AddressCreator())->fromString($address)->getHash()
    )
);

$outpoint = new OutPoint(Buffer::hex($txid, 32), 0);
$txOut = new TransactionOutput(100000, $witnessScript);

$builder = (new TxBuilder())
    ->spendOutPoint($outpoint)
    ->payToAddress(9000, $dest);

$signer = new Signer($builder->get(), Bitcoin::getEcAdapter());
$input = $signer->input(0, $txOut);
$input->sign($key);

$signed = $signer->get();

$consensus = ScriptFactory::consensus();
echo "Script validation result: " . ($input->verify(I::VERIFY_P2SH | I::VERIFY_WITNESS) ? "yay\n" : "nay\n");

echo PHP_EOL;
echo "Witness serialized transaction: " . $signed->getHex() . PHP_EOL. PHP_EOL;
echo "Base serialized transaction: " . $signed->getBaseSerialization()->getHex() . PHP_EOL;

$apiUrl = 'https://blockchain.info/pushtx';
$postData = [
    'tx' => $signed->getHex(),
];
$ch = curl_init($apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
$response = curl_exec($ch);
if ($response === false) {
    die('Error: ' . curl_error($ch));
}
curl_close($ch);
$responseArray = json_decode($response, true);
if (isset($responseArray['status']) && $responseArray['status'] === 'success') {
    echo 'Transaction broadcasted successfully!';
} else {
    echo 'Error broadcasting transaction: ' . $response;
}

hussainSheikh123 avatar Feb 12 '24 07:02 hussainSheikh123