Skip to content

Error with creating a signed bitcoin transaction #916

Open
@code2expert

Description

Kindly help with this issue.

I am trying to create a bitcoin transaction but keep getting error bad request or Invalid transaction. Error: non-mandatory-script-verify-flag (Script failed an OP_EQUALVERIFY operation) . I believe I'm using the correct keys.

Below is my code:

My signed tranaction hex is:
010000000001012e1d32ebc3fa508fc740c6b589b32787e7c16132093e49008386ab59118c76f00000000000ffffffff028089000000000000160014facb445a65e273a1475b46f279a6d2bcbacf87c59a420000000000001600142865223582201a799e229c19a15ef4d7ec72097202483045022100ab0d9a9e02a8bc5907d49e4e21edbabd2a47e0eabdd97506f364bd7d9b443bd202205586775ac5ceec73a5d226c11e4076a7ca8b96a432408cc885c14af65b047ce901210353825959e0f36eff9a1257fa07ad919406a9bba4d100c6df601bc44c63d0ab1100000000

Below is my code:
$privateKey = (new BitWasp\Bitcoin\Key\Factory\PrivateKeyFactory)->fromWif('XXXXXXXXXXXXXXXXXXXXXXXXXX');

// Utxo is: outpoint.txid, outpoint.vout, txout.scriptPubKey, txout.amount
$outpoint = new OutPoint(Buffer::hex('f0768c1159ab868300493e093261c1e78727b389b5c640c78f50fac3eb321d2e'), 0);
$outputScript = ScriptFactory::scriptPubKey()->payToPubKeyHash($privateKey->getPubKeyHash());

$satoshi = 100000000; //one bitcoin in satoshi ie 100 million
$send_wallet_amount = 0.0005425 * $satoshi;
$send = 0.0003520 * $satoshi; // satoshis
$fee = 0.00002 * $satoshi; // satoshis
//calculate change
$change = $send_wallet_amount - $send - $fee;

$txOut = new TransactionOutput($send_wallet_amount, $outputScript);

$transaction = TransactionFactory::build()
    ->input('f0768c1159ab868300493e093261c1e78727b389b5c640c78f50fac3eb321d2e', 0 )
    //->spendOutPoint($outpoint)
    //address to send to
    ->payToAddress($send, (new BitWasp\Bitcoin\Address\AddressCreator)->fromString('bc1qlt95gkn9ufe6z36mgme8nfkjhjavlp79ynjrdz'))
    //change
    ->payToAddress($change, (new BitWasp\Bitcoin\Address\AddressCreator)->fromString('bc1q9pjjydvzyqd8n83znsv6zhh56lk8yztj3m7cjn'))
    ->get();

$tx_hex = $transaction->getHex();

$redeemScript = ScriptFactory::scriptPubKey()->p2wkh($privateKey->getPubKeyHash());
$p2shScript = new P2shScript($redeemScript);
$signData = new SignData();
$signData->p2sh($redeemScript);

$transactionOutputs = [];
foreach ($transaction->getInputs() as $idx => $input) {
    //$transactionOutput = new TransactionOutput(0, ScriptFactory::fromHex($input->getScript()->getBuffer()->getHex()));
    $transactionOutput = new TransactionOutput(0, ScriptFactory::fromHex($p2shScript->getBuffer()->getHex()));
    array_push($transactionOutputs, $transactionOutput);
}
$signer = new Signer($transaction, Bitcoin::getEcAdapter());
foreach ($transactionOutputs as $idx => $transactionOutput) {
    $signer->sign($idx, $privateKey, $transactionOutput, $signData);
}
$signedTx = $signer->get();
$hex = $signedTx->getHex();

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions