Bitcoin Forum
May 21, 2024, 03:15:53 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Looking for a code to Create, Sign & Broadcast Transaction solely using PHP.  (Read 144 times)
CounterEntropy (OP)
Full Member
***
Offline Offline

Activity: 214
Merit: 278


View Profile
May 05, 2020, 07:30:25 PM
 #1

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.
jackg
Copper Member
Legendary
*
Offline Offline

Activity: 2856
Merit: 3071


https://bit.ly/387FXHi lightning theory


View Profile
May 05, 2020, 10:57:21 PM
 #2

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.
CounterEntropy (OP)
Full Member
***
Offline Offline

Activity: 214
Merit: 278


View Profile
May 06, 2020, 04:14:57 PM
 #3

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.
HCP
Legendary
*
Offline Offline

Activity: 2086
Merit: 4316

<insert witty quote here>


View Profile
May 06, 2020, 10:50:56 PM
 #4

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.

█████████████████████████
████▐██▄█████████████████
████▐██████▄▄▄███████████
████▐████▄█████▄▄████████
████▐█████▀▀▀▀▀███▄██████
████▐███▀████████████████
████▐█████████▄█████▌████
████▐██▌█████▀██████▌████
████▐██████████▀████▌████
█████▀███▄█████▄███▀█████
███████▀█████████▀███████
██████████▀███▀██████████
█████████████████████████
.
BC.GAME
▄▄░░░▄▀▀▄████████
▄▄▄
██████████████
█████░░▄▄▄▄████████
▄▄▄▄▄▄▄▄▄██▄██████▄▄▄▄████
▄███▄█▄▄██████████▄████▄████
███████████████████████████▀███
▀████▄██▄██▄░░░░▄████████████
▀▀▀█████▄▄▄███████████▀██
███████████████████▀██
███████████████████▄██
▄███████████████████▄██
█████████████████████▀██
██████████████████████▄
.
..CASINO....SPORTS....RACING..
█░░░░░░█░░░░░░█
▀███▀░░▀███▀░░▀███▀
▀░▀░░░░▀░▀░░░░▀░▀
░░░░░░░░░░░░
▀██████████
░░░░░███░░░░
░░█░░░███▄█░░░
░░██▌░░███░▀░░██▌
░█░██░░███░░░█░██
░█▀▀▀█▌░███░░█▀▀▀█▌
▄█▄░░░██▄███▄█▄░░▄██▄
▄███▄
░░░░▀██▄▀


▄▄████▄▄
▄███▀▀███▄
██████████
▀███▄░▄██▀
▄▄████▄▄░▀█▀▄██▀▄▄████▄▄
▄███▀▀▀████▄▄██▀▄███▀▀███▄
███████▄▄▀▀████▄▄▀▀███████
▀███▄▄███▀░░░▀▀████▄▄▄███▀
▀▀████▀▀████████▀▀████▀▀
cryptoworld99
Member
**
Offline Offline

Activity: 84
Merit: 22


View Profile
May 08, 2020, 01:24:30 PM
Last edit: May 08, 2020, 10:34:42 PM by cryptoworld99
Merited by HCP (2)
 #5

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:password@127.0.0.1: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
*
Offline Offline

Activity: 2086
Merit: 4316

<insert witty quote here>


View Profile
May 08, 2020, 10:06:48 PM
 #6

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:password@127.0.0.1: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

█████████████████████████
████▐██▄█████████████████
████▐██████▄▄▄███████████
████▐████▄█████▄▄████████
████▐█████▀▀▀▀▀███▄██████
████▐███▀████████████████
████▐█████████▄█████▌████
████▐██▌█████▀██████▌████
████▐██████████▀████▌████
█████▀███▄█████▄███▀█████
███████▀█████████▀███████
██████████▀███▀██████████
█████████████████████████
.
BC.GAME
▄▄░░░▄▀▀▄████████
▄▄▄
██████████████
█████░░▄▄▄▄████████
▄▄▄▄▄▄▄▄▄██▄██████▄▄▄▄████
▄███▄█▄▄██████████▄████▄████
███████████████████████████▀███
▀████▄██▄██▄░░░░▄████████████
▀▀▀█████▄▄▄███████████▀██
███████████████████▀██
███████████████████▄██
▄███████████████████▄██
█████████████████████▀██
██████████████████████▄
.
..CASINO....SPORTS....RACING..
█░░░░░░█░░░░░░█
▀███▀░░▀███▀░░▀███▀
▀░▀░░░░▀░▀░░░░▀░▀
░░░░░░░░░░░░
▀██████████
░░░░░███░░░░
░░█░░░███▄█░░░
░░██▌░░███░▀░░██▌
░█░██░░███░░░█░██
░█▀▀▀█▌░███░░█▀▀▀█▌
▄█▄░░░██▄███▄█▄░░▄██▄
▄███▄
░░░░▀██▄▀


▄▄████▄▄
▄███▀▀███▄
██████████
▀███▄░▄██▀
▄▄████▄▄░▀█▀▄██▀▄▄████▄▄
▄███▀▀▀████▄▄██▀▄███▀▀███▄
███████▄▄▀▀████▄▄▀▀███████
▀███▄▄███▀░░░▀▀████▄▄▄███▀
▀▀████▀▀████████▀▀████▀▀
cryptoworld99
Member
**
Offline Offline

Activity: 84
Merit: 22


View Profile
May 08, 2020, 10:38:43 PM
 #7



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
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!