Author

Topic: Looking for a code to Create, Sign & Broadcast Transaction solely using PHP. (Read 160 times)

member
Activity: 84
Merit: 22


ps. There is also a missing " in your "address2 => "address" line... Wink


Nice catch Smiley you have a good eye! I've added the 'code' tags and i will use them in the future, thank you for the merits Smiley


I suggest OP invest in a bitcoin node rather than rely on other services online its much safer to a certain extent
HCP
legendary
Activity: 2086
Merit: 4361
Yes, I'm a newbie but I'm a bitcoin expert Smiley
Solid effort. But I suspect that the reason the OP specified that his sendTX() function takes a private key is that he doesn't have access to a Bitcoin Core node to be able to make use of RPC. I believe this is also why he mentioned using APIs at blockcypher/sochain etc to retrieve UTXOs.

And one small piece of advice... next time, when posting code, please use [ code ][ /code ] formatting tags (without the spaces)... it makes code a LOT easier to read when it is indented properly in monospaced font, and it also prevents your posts from getting excessively long Wink


Code:
require("jsonRPCClient.php");
$bitcoin = new jsonRPCClient('http://username:[email protected]:port');

$addresses = array (

"address1" => "address", //fill in address you want to spend from
"address2 => "address"  //fill in address you want to spend from

);


$unspentTransactions = $bitcoin->listunspent(0, 99999, $addresses);



 $collectedTransactions = [];

    foreach ($unspentTransactions as $unspentTransaction) {

        if ($collectedAmount < $amountToSend) {
            $collectedTransactions[] = [
                'txid'  => $unspentTransaction['txid'],
                'vout'  => $unspentTransaction['vout'],
               
            ];

           $collectedAmount += $unspentTransaction['amount']; //this shows all balance collected from addesses you wish to spend from
        } else {

            break;
        }
    }


$recipient = "address";  //this where you want to send payment to
$amount = 0.000000; //amount you wish to send in BTC value
$change_address = "your_change_Address";
$change_amount = number_format($collectedAmount-$fee-$amount, 8);

$output =
    array(


"".$recipient."" =>"".$amount."",

//this is where the change money will go back to... set your address

"".$change_addr."" =>"".$change_amount-$fee."",

);


// Now let's create the transaction

 $rawTransaction = $bitcoin->createrawtransaction($collectedTransactions, $output);

// now lets sign the transaction using bitcoin core signwithwallet function

$sign_raw_txn = $bitcoin->signrawtransactionwithwallet($rawTransaction);
$shex = $sign_raw_txn['hex']; // this is the signed hex you can do anything you want with it... if you want to send payment follow the next step.


echo $bitcoin->sendrawtransaction($shex); // this will return txn id and you're all done.


//I haven't tested this code so if you want me to code it for you I can do in my spare time, I hope this helps.

ps. There is also a missing " in your "address2 => "address" line... Wink
member
Activity: 84
Merit: 22
Hi,


Yes, I'm a newbie but I'm a bitcoin expert Smiley

I've coded this for you i can make it better if you want, but if you want to do it yourself here is how it should be done.


Code:
Json RPC can be found here: https://github.com/thinkphp/json-rpc-php/blob/master/jsonRPCClient.php

require("jsonRPCClient.php");
$bitcoin = new jsonRPCClient('http://username:[email protected]:port');

$addresses = array (

"address1" => "address", //fill in address you want to spend from
"address2 => "address"  //fill in address you want to spend from

);


$unspentTransactions = $bitcoin->listunspent(0, 99999, $addresses);



 $collectedTransactions = [];

    foreach ($unspentTransactions as $unspentTransaction) {

        if ($collectedAmount < $amountToSend) {
            $collectedTransactions[] = [
                'txid'  => $unspentTransaction['txid'],
                'vout'  => $unspentTransaction['vout'],
              
            ];

           $collectedAmount += $unspentTransaction['amount']; //this shows all balance collected from addesses you wish to spend from
        } else {

            break;
        }
    }


$recipient = "address";  //this where you want to send payment to
$amount = 0.000000; //amount you wish to send in BTC value
$change_address = "your_change_Address";
$change_amount = number_format($collectedAmount-$fee-$amount, 8);

$output =
    array(


"".$recipient."" =>"".$amount."",

//this is where the change money will go back to... set your address

"".$change_addr."" =>"".$change_amount-$fee."",

);


// Now let's create the transaction

 $rawTransaction = $bitcoin->createrawtransaction($collectedTransactions, $output);

// now lets sign the transaction using bitcoin core signwithwallet function

$sign_raw_txn = $bitcoin->signrawtransactionwithwallet($rawTransaction);
$shex = $sign_raw_txn['hex']; // this is the signed hex you can do anything you want with it... if you want to send payment follow the next step.


echo $bitcoin->sendrawtransaction($shex); // this will return txn id and you're all done.


//I haven't tested this code so if you want me to code it for you I can do in my spare time, I hope this helps.

HCP
legendary
Activity: 2086
Merit: 4361
So, what you want is for someone to use Bit-Wasp's PHP implementation and to actually write you some code for a function called sendTX()? Huh

All the documentation for creating/signing transactions is available here: https://github.com/Bit-Wasp/bitcoin-php/blob/1.0/doc/documentation/Transaction.md

In fact, their "Pay to pubkey hash" example seems to do pretty much everything that you're asking for. You'd just need to figure out how to retrieve the UTXO data that you're after and pass that into the transaction builder.
full member
Activity: 214
Merit: 278
Coinb.in is mentioned by a lot of people here and is written in java script afaik: http://coinb.in/#about - so it would run on a php server and within php afaik.
I'm thorough with Coinb.in. It's JS based and works client side. Wont serve my purpose. I need PHP code.

It's compatible with calls from bitcoin core so you may need to edit it to work with external explorers.
Bit-Wasp? Yes. I'm looking for that edited code.
copper member
Activity: 2856
Merit: 3071
https://bit.ly/387FXHi lightning theory
Coinb.in is mentioned by a lot of people here and is written in java script afaik: http://coinb.in/#about - so it would run on a php server and within php afaik.

It's compatible with calls from bitcoin core so you may need to edit it to work with external explorers.
full member
Activity: 214
Merit: 278
Desired PHP function can be of following structure...

sendTX($fromAddress, $fromAddressPrivateKey, $toAddress, $amountToSend, $changeAddress)

$fromAddress = Legacy address support mandatory

$fromAddressPrivateKey = WIF/HEX Private Key for legacy address

$toAddress = Can be any of the all available address format, i.e. legacy/multisig/segwit

$amountToSend = Always an integer in Satoshi

$changeAddress = Legacy address support mandatory



Following APIs can be used to get list of UTXO and to broadcast the signed transaction...

1. https://www.blockcypher.com/dev/bitcoin/

2. https://blockchair.com/api/docs

3. https://sochain.com/api/



Reference: https://github.com/Bit-Wasp/bitcoin-php

Note: Must run on shared hosting with PHP 7.4 support.
Jump to: