<p>You're interested in the following API calls:</p>

<pre><code>public List&lt;TransactionInput&gt; Transaction.getInputs()
public Script TransactionInput.getScriptSig() throws ScriptException
public void Script.correctlySpends(Transaction txContainingThis, long scriptSigIndex, Script scriptPubKey)
</code></pre>

<p>Use them like so:</p>

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

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

<p>If it isn't, then you can make an educated guess by converting the address to a <a href="https://en.bitcoin.it/wiki/Script#Standard_Transaction_to_Bitcoin_address_.28pay-to-pubkey-hash.29" rel="nofollow">P2PKH</a> scriptPubKey.</p>

<pre><code>public static Script ScriptBuilder.createOutputScript(Address to)
</code></pre>

<p>This creates an edge case where if someone tries to spend a <a href="https://en.bitcoin.it/wiki/Script#Obsolete_pay-to-pubkey_transaction" rel="nofollow">P2PK output</a>, it will appear to be signed incorrectly to your program. (Both are represented by addresses.)</p>

CC BY-SA 3.0
2015-01-08T05:22:19.333Z
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
CC BY-SA 3.0
2015-01-11T05:07:31.767Z
`correctlySpends` requires a 4th boolean parameter (`enforceP2SH`). What is that?
Jus12
CC BY-SA 3.0
2015-01-11T05:17:57.123Z
@Jus12 It's for implementing [a rare edge case of BIP0016](https://github.com/bitcoin/bips/blame/master/bip-0016.mediawiki#L40). Set it to true. P.S. You are using an old version of BitcoinJ. You should update it.
Nick ODell
CC BY-SA 3.0
2015-01-12T05:18:58.400Z
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
CC BY-SA 3.0
2015-01-12T05:22:49.597Z
@Jus12 `I tried to upgrade but it breaks my code (requires some refactoring)` Ah, fair enough. `Are there any serious issues in using 0.11?` Well, people on the internet will assume you're using the latest version. :) But there's nothing specific I can think of.
Nick ODell
CC BY-SA 3.0
2015-10-15T07:07:41.680Z
As an update, there *are* actually some serious in using 0.11 based on this bug report: https://github.com/bitcoinj/bitcoinj/issues/1082
Jus12
CC BY-SA 3.0
2015-01-07T18:35:06.347Z
2015-01-11T05:41:06.607Z
2015-01-11T05:41:06.607Z
Nick ODell
<p>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?</p>

CC BY-SA 3.0
2015-01-07T15:15:36.617Z
http://bitcoin.stackexchange.com/questions/32305
amaclin
<transactions><signature><bitcoinj>
How do we verify the signature on a raw transaction using Bitcoinj?
Jus12
# How do we verify the signature on a raw transaction using Bitcoinj?

(Asked by: username_0 on 2015-01-07)

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?

**Tags:** transactions, signature, bitcoinj

> **Comments:**
> **username_1**: http://bitcoin.stackexchange.com/questions/32305  

---

## 1 answers

### Answer by username_2 on 2015-01-07. Score: 4 | Accepted answer

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](https://en.bitcoin.it/wiki/Script#Standard_Transaction_to_Bitcoin_address_.28pay-to-pubkey-hash.29) scriptPubKey.

```
public static Script ScriptBuilder.createOutputScript(Address to)

```

This creates an edge case where if someone tries to spend a [P2PK output](https://en.bitcoin.it/wiki/Script#Obsolete_pay-to-pubkey_transaction), it will appear to be signed incorrectly to your program. (Both are represented by addresses.)

> **Comments:**
> **username_0**: 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.  
> **username_0**: `correctlySpends` requires a 4th boolean parameter (`enforceP2SH`). What is that?  
> **username_2**: @username_0 It's for implementing [a rare edge case of BIP0016](https://github.com/bitcoin/bips/blame/master/bip-0016.mediawiki#L40). Set it to true. P.S. You are using an old version of BitcoinJ. You should update it.  
> **username_0**: 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?  
> **username_2**: @username_0 `I tried to upgrade but it breaks my code (requires some refactoring)` Ah, fair enough. `Are there any serious issues in using 0.11?` Well, people on the internet will assume you're using the latest version. :) But there's nothing specific I can think of.  
> **username_0**: As an update, there *are* actually some serious in using 0.11 based on this bug report: https://github.com/bitcoinj/bitcoinj/issues/1082  

---

