0

I have a unsigned transaction hex. Now I want to sign this transaction input with my private key in PHP using the BitWasp Library. I have followed some of the examples and searched the forums and cannot get my head around it.

I get the raw transaction from https://counterparty.io/docs/api/

I am getting the error Invalid script Signature - Must be push only.

My Raw Unsigned TX:

010000000147e4efecb198d013f9a938904d560d4c831cb3e7ae84cf899c10c5689ed0c2ed000000001976a91409c4d51c89d31a374706ae80249bb408f02b86ba88acffffffff0200000000000000002a6a2846e58e6bfb7a15bc6cd36b7e545f4c86b49ea57539d2b77b57a822a3ac811d7d6846e3416cccc4c673600000000000001976a91409c4d51c89d31a374706ae80249bb408f02b86ba88ac00000000

My code :

require_once __DIR__ . "/vendor/autoload.php";

use BitWasp\Bitcoin\Script\ScriptFactory;
use BitWasp\Bitcoin\Transaction\OutPoint;
use BitWasp\Bitcoin\Utxo\Utxo;
use BitWasp\Buffertools\Buffer;
use BitWasp\Bitcoin\Transaction\TransactionOutput;
use BitWasp\Bitcoin\Address\AddressCreator;
use BitWasp\Bitcoin\Address\PayToPubKeyHashAddress;
use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\Key\Factory\PrivateKeyFactory;
use BitWasp\Bitcoin\Network\NetworkFactory;
use BitWasp\Bitcoin\Transaction\Factory\Signer;
use BitWasp\Bitcoin\Transaction\TransactionFactory;

Bitcoin::setNetwork(NetworkFactory::bitcoin());
$network = Bitcoin::getNetwork();
$ecAdapter = Bitcoin::getEcAdapter();

$addrCreator = new AddressCreator();
$privFactory = new PrivateKeyFactory($ecAdapter);

$priv = $privFactory->fromWif('MY-PRIVATE-KEY');

$txHex = '010000000147e4efecb198d013f9a938904d560d4c831cb3e7ae84cf899c10c5689ed0c2ed000000001976a91409c4d51c89d31a374706ae80249bb408f02b86ba88acffffffff0200000000000000002a6a2846e58e6bfb7a15bc6cd36b7e545f4c86b49ea57539d2b77b57a822a3ac811d7d6846e3416cccc4c673600000000000001976a91409c4d51c89d31a374706ae80249bb408f02b86ba88ac00000000';
$myTx = TransactionFactory::fromHex($txHex);

$transactionOutputs = [];
foreach ($myTx->getInputs() as $idx => $input) {
    $transactionOutput = new TransactionOutput(0, ScriptFactory::fromHex($input->getScript()->getBuffer()->getHex()));
    array_push($transactionOutputs, $transactionOutput);
}

$signer = new Signer($myTx, $ecAdapter);

foreach ($transactionOutputs as $idx => $transactionOutput) {
    $signer->sign($idx, $priv, $transactionOutput);
}

$signed = $signer->get();
echo $signed->getHex() . PHP_EOL;

Please can someone explain what it is im doing wrong and explain it in layman terms?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.