How to test if a transaction can spend an existing output?

0

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.

robermann

Posted 2018-09-04T07:09:18.440

Reputation: 135

Answers

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.

Raghav Sood

Posted 2018-09-04T07:09:18.440

Reputation: 10 897

does it test also if sigScript + ScriptPubKey return true, so if the transaction really can spend that input?robermann 2018-09-04T07:50:45.453

It should, yes. A bad script will not be accepted into the mempoolRaghav Sood 2018-09-04T08:02:52.467

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

Mike D

Posted 2018-09-04T07:09:18.440

Reputation: 1 571

Thanks. It does not look like what I'm asking for, I edited the quesiton.robermann 2018-09-04T09:00:27.600

1@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 2018-09-04T09:23:10.787

I tested it, it works (but doesn't recognize OP_RETURN data)robermann 2018-09-04T19:40:30.147

0

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

public static bool VerifyScript

robermann

Posted 2018-09-04T07:09:18.440

Reputation: 135