I managed to create a raw tx using Bitcoinj but not able to figure out how to verify its signatures. Assuming I know the addresses corresponding to the (supposedly) unspent outputs used as inputs to the Tx, what is the easiest way to verify the Tx?


You're interested in the following API calls:

public List<TransactionInput> Transaction.getInputs()
public Script TransactionInput.getScriptSig() throws ScriptException
public void Script.correctlySpends(Transaction txContainingThis, long scriptSigIndex, Script scriptPubKey)

Use them like so:

Transaction tx = ...
List<TransactionInput> inputs = tx.getInputs();
for(int i = 0; i < inputs.size(); i++) {
    TransactionInput input = inputs.get(i);
    Script scriptSig = input.getScriptSig();
    Script scriptPubKey = ...
    scriptSig.correctlySpends(tx, i, scriptPubKey);

The tricky bit is getting scriptPubKey. If you have the transactions you're spending in memory, it's as simple as input.getConnectedOutput().getScriptPubKey();

If it isn't, then you can make an educated guess by converting the address to a P2PKH scriptPubKey.

public static Script ScriptBuilder.createOutputScript(Address to)

This creates an edge case where if someone tries to spend a P2PK output, it will appear to be signed incorrectly to your program. (Both are represented by addresses.)

  • Seems to answer the question. Will check and revert. Thanks for mentioning the P2PK outputs. The transactions are created by me and are standard so I can probably ignore this. – Jus12 Jan 8 '15 at 5:22
  • correctlySpends requires a 4th boolean parameter (enforceP2SH). What is that? – Jus12 Jan 11 '15 at 5:07
  • @Jus12 It's for implementing a rare edge case of BIP0016. Set it to true. P.S. You are using an old version of BitcoinJ. You should update it. – Nick ODell Jan 11 '15 at 5:17
  • Thanks. I set it to true. There are significant changes from 0.11 to 0.12. I tried to upgrade but it breaks my code (requires some refactoring). Will get to it eventually. Are there any serious issues in using 0.11? – Jus12 Jan 12 '15 at 5:18
  • 1
    As an update, there are actually some serious in using 0.11 based on this bug report: github.com/bitcoinj/bitcoinj/issues/1082 – Jus12 Oct 15 '15 at 7:07

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.