Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hussainSheikh123 opened this issue Feb 12, 2024 · 0 comments

Comments

@hussainSheikh123
Copy link

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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant