<p>If you build Bitcoin Core from master, you can use the new <code>testmempoolaccept</code> rpc method to check if your tx would have made it into the mempool. This will include checking the signature, and ensuring that your inputs are actually unspent. It does not add broadcast the tx into the mempool, but simply checks if it would have been accepted.</p>

CC BY-SA 4.0
2018-09-04T07:50:45.453Z
does it test also if sigScript + ScriptPubKey return true, so if the transaction really can spend that input?
robermann
CC BY-SA 4.0
2018-09-04T08:02:52.467Z
It should, yes. A bad script will not be accepted into the mempool
Raghav Sood
CC BY-SA 4.0
2018-09-04T07:17:25.960Z
2018-09-04T07:17:25.960Z
Raghav Sood
<p><a href="https://github.com/mcdallas/cryptotools" rel="nofollow noreferrer">This</a> Python library has a <code>verify()</code> method that will run the script and perform a few trivial checks.</p>

<pre><code>from btctools import Transaction
&gt;&gt;&gt; tx = Transaction.from_hex('0100000000010153159b2a077d8dfbe4dca4b6b3a9e0ccedb4962ca73280526e6a1eeb1e8e9adc2a00000000ffffffff04a08f3e00000000001976a914de755835002260891962f1e671a2bf7605788d0f88ac0046c3230000000017a914c7c9b5f51244f39f81ec01146eb0d1d98d4bbd4c872052a6000000000017a91469f376599f0ffcaacd6a79854b1ee99513bb7b35870006f21600000000220020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d04004830450221009360af3ec3f9d4ae8acc3ab2213f3f9344cb322b4ea447490bb297a9a4c1d207022067027b032e54b3efb9e885604349ec4f1b6446eed8ed208c6fe6eeae9ace921a0148304502210097cf932eb37513c201b577472d70f1553f338d6439e9181607ec75f2e83fa148022052368c88085838521efc64cf509b95dd53d8c825089acd3ff681e74d3a2b4959016952210266edd4ef2953675faf0662c088a7f620935807d200d65387290b31648e51e253210372ce38027ee95c98cdc54172964fa3aecf9f24b85c139d3d203365d6b691d0502103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae00000000')
&gt;&gt;&gt; tx
Transaction(inputs=1, outputs=4)
&gt;&gt;&gt; tx.verify()
True
&gt;&gt;&gt; tx.outputs[0].json()
{'scriptPubKey': {'address': '3KuQ2pyr5LHwiU1a2Rwrn9kGGMpUmHxLdm',
 'asm': 'OP_HASH160 c7c9b5f51244f39f81ec01146eb0d1d98d4bbd4c OP_EQUAL',
 'hex': 'a914c7c9b5f51244f39f81ec01146eb0d1d98d4bbd4c87',
 'type': 'P2SH'},
 'value': 6.0}
&gt;&gt;&gt; tx.outputs[0].value += 1
&gt;&gt;&gt; tx.verify()
False
</code></pre>

CC BY-SA 4.0
2018-09-04T09:00:27.600Z
Thanks. It does not look like what I'm asking for, I edited the quesiton.
robermann
CC BY-SA 4.0
2018-09-04T09:23:10.787Z
@robermann I read your edit. You want something to validate that the script executes and results in a single True on the stack. That is what the library does. Maybe I didn't show a good example in my post.
Mike D
CC BY-SA 4.0
2018-09-04T19:40:30.147Z
I tested it, it works (but doesn't recognize OP_RETURN data)
robermann
CC BY-SA 4.0
2018-09-04T08:49:14.203Z
2018-09-04T08:49:14.203Z
Mike D
<p>Just for reference, there is also a static method in NBitcoin in class <em>Script</em>: </p>

<blockquote>
  <p>public static bool <a href="https://github.com/MetacoSA/NBitcoin/blob/master/NBitcoin/Script.cs#L879" rel="nofollow noreferrer">VerifyScript</a></p>
</blockquote>

CC BY-SA 4.0
2018-09-04T19:39:35.140Z
2018-09-04T19:39:35.140Z
robermann
<p>Suppose I'm writing a transaction by hand, that spends an existing uspent output sent by myself from another address. Is there a way to test if the transaction can really spend that output, before brodcasting? The reason is, I want to be sure if I'm correctly generating the sigScript.</p>

<p>EDIT: I know it is possible to syntactically verify transactions, but I'm asking if do exists a method such as <code>canSpend(myTransaction, unspentOutput)</code>. Ideally that would execute the combination of scripts <em>myTransaction.sigScript</em> and <em>unspentOutput.ScriptPubKey</em> and check if the result is true.</p>

<spending>
How to test if a transaction can spend an existing output?
robermann
# How to test if a transaction can spend an existing output?

(Asked by: username_0 on 2018-09-04)

Suppose I'm writing a transaction by hand, that spends an existing uspent output sent by myself from another address. Is there a way to test if the transaction can really spend that output, before brodcasting? The reason is, I want to be sure if I'm correctly generating the sigScript.

EDIT: I know it is possible to syntactically verify transactions, but I'm asking if do exists a method such as `canSpend(myTransaction, unspentOutput)`. Ideally that would execute the combination of scripts *myTransaction.sigScript* and *unspentOutput.ScriptPubKey* and check if the result is true.

**Tags:** spending


---

## 3 answers

### Answer by username_1 on 2018-09-04. Score: 1

If you build Bitcoin Core from master, you can use the new `testmempoolaccept` rpc method to check if your tx would have made it into the mempool. This will include checking the signature, and ensuring that your inputs are actually unspent. It does not add broadcast the tx into the mempool, but simply checks if it would have been accepted.

> **Comments:**
> **username_0**: does it test also if sigScript + ScriptPubKey return true, so if the transaction really can spend that input?  
> **username_1**: It should, yes. A bad script will not be accepted into the mempool  

---

### Answer by username_2 on 2018-09-04. Score: 1

This Python library has a verify() method that will run the script and perform a few trivial checks.
from btctools import Transaction
>>> tx = Transaction.from_hex('0100000000010153159b2a077d8dfbe4dca4b6b3a9e0ccedb4962ca73280526e6a1eeb1e8e9adc2a00000000ffffffff04a08f3e00000000001976a914de755835002260891962f1e671a2bf7605788d0f88ac0046c3230000000017a914c7c9b5f51244f39f81ec01146eb0d1d98d4bbd4c872052a6000000000017a91469f376599f0ffcaacd6a79854b1ee99513bb7b35870006f21600000000220020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d04004830450221009360af3ec3f9d4ae8acc3ab2213f3f9344cb322b4ea447490bb297a9a4c1d207022067027b032e54b3efb9e885604349ec4f1b6446eed8ed208c6fe6eeae9ace921a0148304502210097cf932eb37513c201b577472d70f1553f338d6439e9181607ec75f2e83fa148022052368c88085838521efc64cf509b95dd53d8c825089acd3ff681e74d3a2b4959016952210266edd4ef2953675faf0662c088a7f620935807d200d65387290b31648e51e253210372ce38027ee95c98cdc54172964fa3aecf9f24b85c139d3d203365d6b691d0502103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae00000000')
>>> tx
Transaction(inputs=1, outputs=4)
>>> tx.verify()
True
>>> tx.outputs[0].json()
{'scriptPubKey': {'address': '3KuQ2pyr5LHwiU1a2Rwrn9kGGMpUmHxLdm',
 'asm': 'OP_HASH160 c7c9b5f51244f39f81ec01146eb0d1d98d4bbd4c OP_EQUAL',
 'hex': 'a914c7c9b5f51244f39f81ec01146eb0d1d98d4bbd4c87',
 'type': 'P2SH'},
 'value': 6.0}
>>> tx.outputs[0].value += 1
>>> tx.verify()
False

> **Comments:**
> **username_0**: Thanks. It does not look like what I'm asking for, I edited the quesiton.  
> **username_2**: @username_0 I read your edit. You want something to validate that the script executes and results in a single True on the stack. That is what the library does. Maybe I didn't show a good example in my post.  
> **username_0**: I tested it, it works (but doesn't recognize OP_RETURN data)  

---

### Answer by username_0 on 2018-09-04. Score: 0

Just for reference, there is also a static method in NBitcoin in class *Script*:

> public static bool [VerifyScript](https://github.com/MetacoSA/NBitcoin/blob/master/NBitcoin/Script.cs#L879)


---

