Thursday, November 24, 2022
HomeBitcoinSigning a RAW transaction utilizing bitcoin-php

Signing a RAW transaction utilizing bitcoin-php


I’ve a unsigned transaction hex. Now I wish to signal this transaction enter with my non-public key in PHP utilizing the BitWasp Library. I’ve adopted a few of the examples and searched the boards and can’t get my head round it.

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

I get the error Invalid script Signature - Have to be push solely.

My Uncooked Unsigned TX:

010000000147e4efecb198d013f9a938904d560d4c831cb3e7ae84cf899c10c5689ed0c2ed000000001976a91409c4d51c89d31a374706ae80249bb408f02b86ba88acffffffff0200000000000000002a6a2846e58e6bfb7a15bc6cd36b7e545f4c86b49ea57539d2b77b57a822a3ac811d7d6846e3416cccc4c673600000000000001976a91409c4d51c89d31a374706ae80249bb408f02b86ba88ac00000000

My code :

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

use BitWaspBitcoinScriptScriptFactory;
use BitWaspBitcoinTransactionOutPoint;
use BitWaspBitcoinUtxoUtxo;
use BitWaspBuffertoolsBuffer;
use BitWaspBitcoinTransactionTransactionOutput;
use BitWaspBitcoinAddressAddressCreator;
use BitWaspBitcoinAddressPayToPubKeyHashAddress;
use BitWaspBitcoinBitcoin;
use BitWaspBitcoinKeyFactoryPrivateKeyFactory;
use BitWaspBitcoinNetworkNetworkFactory;
use BitWaspBitcoinTransactionFactorySigner;
use BitWaspBitcoinTransactionTransactionFactory;

Bitcoin::setNetwork(NetworkFactory::bitcoin());
$community = 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 => $enter) {
    $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->signal($idx, $priv, $transactionOutput);
}

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

Please can somebody clarify what it’s im doing fallacious and clarify it in layman phrases?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments