0

I'm trying to modify the simple wallettemplate to get the sender's address in the transaction list. However since TransactionInput.getFromAddress() doesn't exist now and getConnectedOutput().getAddressFromP2PKHScript() is deprecated, I don't know which is the right way to do so.

  • It's not entirely clear to me why TransactionOutput.getAddressFromP2PKHScript() has been deprecated. It seems to me that perhaps Andreas Schildbach (who deprecated the method) is trying to discourage people trying to determine a "sender" of a transaction in such a way because of the ambiguity involved (for instance if a transaction has multiple "senders". (See, e.g., here) – Vecna Feb 23 at 22:35
0
public Address getFromAddress(TransactionInput txIn) {
    // get script from connected output
    TransactionOutput txOut = txIn.getConnectedOutput();
    Script txOutScript = txOut.getScriptPubKey();

    // get address from script
    Address fromAddress = txOutScript.getToAddress(MainNetParams.get());

    return fromAddress;
}
  • thank you! which TransactionInput should I check? if I use tx.getInput(0) I get a NullPointerException. Should I iterate over all the inputs or is it something else I'm missing? – Johnny Feb 23 at 22:45
  • This is the problem with trying to find the sender. A "sender" isn't a clear idea for a Bitcoin transaction. A transaction's inputs could come from multiple sources. Ultimately, determining which input(s) to consider the "sender" is up to you. As for the NullPointerException, coinbase transactions have no inputs, and I'm guessing that's probably where you're getting the exception. You'll want to skip over the transaction if tx.isCoinBase(). – Vecna Feb 25 at 23:09

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.