Bitcoin Forum

Economy => Web Wallets => Topic started by: WingTsun on August 27, 2015, 12:04:15 AM



Title: [Solved] Blockchain API Callback
Post by: WingTsun on August 27, 2015, 12:04:15 AM
I just stumbled across a problem, creating a PHP "CALLBACK". Everything is working fine, except I can not find how can I grab the SENDERS Wallet. All other parameters are provided.

I have red the BlockChain API, and it shows only how to grab receivers wallet (Your Own, which received the transaction). Here are the parameters they provide:

  value - The value of the payment received in satoshi (not including fees). Divide by 100000000 to get the value in BTC.
  transaction_hash - The transaction hash.
  input_address - The bitcoin address that received the transaction.
  confirmations - The number of confirmations of this transaction.
  {Custom Parameters} - Any parameters included in the callback URL will be past back to the callback URL in the notification.

That does not make any sense for me, why there is no example how to grab the senders wallet, since my script needs to save the senders wallet for further use.

Any help would be appreciated.
Thank you, WingTsun.



Title: Re: Blockchain API Callback
Post by: achow101 on August 27, 2015, 12:11:49 AM
Since the sender could be sending from multiple addresses or through a shared sending transaction, it is possible that blockchain omitted this feature because there is no guarantee (except for 1 input 1 output transactions) which input is going to you.

A rather roundabout way would be to take the transaction hash they provide and query for all of the inputs in that transaction and pull the sending addresses from there.


Title: Re: Blockchain API Callback
Post by: DannyHamilton on August 27, 2015, 12:20:11 AM
There is no such thing as a "sending address" at the technical level of bitcoin.

The sender might not even be using a wallet to send the transaction.

If you want to know what address to use to send bitcoins to someone, ASK THEM.  Any other method is at best a guess, and at worst completely wrong.



Title: Re: Blockchain API Callback
Post by: coinableS on August 27, 2015, 01:25:01 AM
Danny and Knight are right, there is no sure way to do this, since there can be multiple input addresses that sent you the funds.
However there is a way to get the 1st input sending address of the most recent transaction to a particular address.

I did a video tutorial on how to do this a few months back with blockchain.info.

https://youtu.be/_K270ufQ6ho?t=1m

Go here -> https://blockchain.info/api/blockchain_api
Scroll down to the single address area:
Then you'll take your $input_address and plug it in: https://blockchain.info/address/$bitcoin_address?format=json
Replace $bitcoin_address with the input_address that received the funds.
and then you can pull out the most recent transaction and then pull out the address from the inputs array.
Again watch the video ^^

Your code will look something like:

Code:
<?php
$input_address 
"1someBitcoinAddress";
$url "https://blockchain.info/address/$input_address?format=json";
$jsonData json_decode(file_get_contents($url), true);
$sendingAddress $jsonData["txs"][0]["inputs"][0]["prev_out"]["addr"];
echo 
"This address ".$sendingAddress." sent the most recent transaction";
//or you can enter the $sendingAddress to a DB
?>


If you want to get all the input addresses that sent for the most recent transaction you will just create a loop that counts the number of inputs
something like:
Code:
        $n_inputs = count($jsonData["txs"][0]["inputs"]);	
for($ii = 0; $ii < $n_inputs; $ii++){
$inValue = $jsonData["txs"][0]["inputs"][$ii]["prev_out"]["addr"];
        }

I also did a video on making the loops but with chain.com API
https://youtu.be/OUdGJK-sJ2k?t=2m33s  start video at 2:33


Title: Re: Blockchain API Callback
Post by: WingTsun on August 27, 2015, 02:12:05 AM
Thank you guys for the replies, now it's clear to me.  ;)


Title: Re: Blockchain API Callback
Post by: WingTsun on August 29, 2015, 07:17:12 PM
So, the only way I could come up with, in order to keep system max easy for users is:

- They click a "Start" button;
- Window pops-up asking for the users BTC Wallet;
- After user enters his Wallet, system generates a new one, and provides it for the user in the same Window;
- Further, system enters the details in the DataBase, and the Callback waits for this specific transaction in the newly generated wallet by the system.

Will this approach work? I mean, By this method I should be able to see in the Callback from which Wallet is the coins sent, right?

Thank you, WingTsun.


Title: Re: Blockchain API Callback
Post by: coinableS on August 29, 2015, 07:28:37 PM
So, the only way I could come up with, in order to keep system max easy for users is:

- They click a "Start" button;
- Window pops-up asking for the users BTC Wallet;
- After user enters his Wallet, system generates a new one, and provides it for the user in the same Window;
- Further, system enters the details in the DataBase, and the Callback waits for this specific transaction in the newly generated wallet by the system.

Will this approach work? I mean, By this method I should be able to see in the Callback from which Wallet is the coins sent, right?

Thank you, WingTsun.

Yes that would work and that's how a lot of sites that accept bitcoin as payments online track orders. Generate a new deposit address for each user then you are easily able to tell who sent in the funds. The above would work by asking them for their receiving address and then assigning it to a unique deposit address. That way you will know what address to send the funds back to. 


Title: Re: Blockchain API Callback
Post by: WingTsun on August 29, 2015, 07:33:28 PM
Ok, thank you. I am now checking the Documentation, now I just need to confirm the correct way of obtaining the senders wallet (In this case it is the newly generated wallet) in the CallBack.

The CallBack response looks like this:

Code:
{
    "fee_percent":0,
    "destination":"1A8JiWcwvpY7tAopUkSnGuEYHmzGYfZPiq",
    "input_address":"1KZoUuPWFAeyVySHAGqvTUDoX6P3ntuLNF",
    "callback_url":"http://yoururl.com"
}

So, I should be able to get he senders wallet (the generated one) like this right?

Code:
$input_address = $_GET['input_address'];


Title: Re: Blockchain API Callback
Post by: coinableS on August 29, 2015, 07:53:18 PM
Correct, the input_address parameter is automatically provided with blockchain.info's callback as a GET.

So you can use that input address to run a database query and see who that address belongs to.
Something like   SELECT * FROM mytable WHERE depositaddress = '$input_address'


Title: Re: Blockchain API Callback
Post by: WingTsun on August 29, 2015, 07:56:16 PM
Thank you, now that you confirmed that this parameter will be the generated wallet, I have everything i need to implement this solution. Huge thanks to you. I am marking this as "[Solved]".