1

I need to receive bitcoins from the customers, and then add to theirs virtual account some credits. I use BitcoinJ wallet and I want to give each customer the unique Address created by

walletAppKit.wallet().freshReceiveAddress()

But when I receive coins I can't define customer who send me them.

walletAppKit.wallet().addCoinsReceivedEventListener((w, tx, prevBalance, newBalance) contains all information but I don't see any Address info to define customer.

0

There can be many recipients (in the same transaction), so you need to iterate for the correct ones. E.g. for incoming transactions, you can do

for (TransactionOutput output : tx.getOutputs())
{
        try
        {
                if (output.isMine(wallet))
                {
                        Script script = output.getScriptPubKey();
                        Address address = script.getToAddress(Constants.NETWORK_PARAMETERS,true);
                        // do something with the address
                }
        }
        catch (final ScriptException x){}
}
0

I'm not very familiar with bitcoinj but it appears to me that you need to do the following.

You're passed the transaction that was received (tx). Call getOutputs to get a list of the transaction's outputs, and for each one, call getAddressFromP2PKHScript to determine the address credited by that output.

You should account for the possibility that there might be multiple outputs paying the same address, or multiple outputs paying different addresses that are in your wallet.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.