tantonj
tantonj

Reputation: 444

How to find senders Bitcoin Address in BitcoinJ after receiving a transaction

So in my app I have the following functionality for receiving bitcoins

kit.wallet().addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {
            @Override
            public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
                txtLog.append("-----> coins resceived: " + tx.getHashAsString() + "\n");
                txtLog.append("received: " + tx.getValue(wallet) + "\n");  

            Futures.addCallback(tx.getConfidence().getDepthFuture(1), new FutureCallback<TransactionConfidence>() {
                @Override
                public void onSuccess(TransactionConfidence result) {
                    txtLog.append("\nSuccess! Recieved: " + tx.getValue(wallet) + "\n");
                    //Find address of sender here
                }

                @Override
                public void onFailure(Throwable t) {
                    throw new RuntimeException(t);
                }
            });
        }
    });

This works great, OnSuccess triggers properly once a transaction is confirmed and added to my wallet. txtLog is just a textArea in my java frame which displays some text output for me. What I need to do now is find the address of the sender at this point, can i do this with the Transaction object tx? Any help would be appreciated.

Upvotes: 1

Views: 808

Answers (1)

tantonj
tantonj

Reputation: 444

Found the solution! Unfortunately it uses a depreciated method. I just added the following in the appropriate spot.

String address = "";
for (TransactionInput txin : tx.getInputs()){
    address = txin.getFromAddress().toString();
}

Upvotes: 1

Related Questions