How to know if a transaction is valid?

7

2

I understand there are a couple things that need to be checked for a transaction to be valid - the previous transaction had to be correct, no OP can invalidate the transaction, etc, but is there a complete list on what one needs to check in order to know if a transaction is valid?

For example, does the transaction need to have a specific structure in order to be spent, like the script from TxIn needs to compile to the encoded message, and the TxOut script needs to properly run OP_Checksig on that particular message? If not, one could simply drop the message from TxIn, push their own malicious message and run OP_Checksig on that.

What steps does the protocol require a client to go through to check if the transaction is valid?

ThePiachu

Posted 2012-01-05T00:00:32.243

Reputation: 41 594

It's not clear what you're asking. By "valid" do you just mean that the client won't consider a block invalid if it contains that transaction? Or by "valid" do you mean the default client will include that transaction in blocks it generates? Or by "valid" you mean the default client will relay it to other nodes? Or something else? There are different checks on transactions to decide if they are "valid" for a number of different purposes. For example, consider a transaction that spends coins not yet available but that could become available. In some senses, that's valid. In some, not yet.David Schwartz 2012-01-05T02:21:33.267

@DavidSchwartz I suppose all of the above would be useful, but mainly I was thinking about including the transactions in the block.ThePiachu 2012-01-05T10:51:51.480

Answers

3

On the bitcoin wiki, there is rather complete list of validation checks that are done by the Satoshi client: Protocol_rules.

There is a section specifically about transaction checking as well.

Pieter Wuille

Posted 2012-01-05T00:00:32.243

Reputation: 54 032

0

I recommend using the bitcore library built by BitPay which has a transaction.verify() function that works well.

You can also attempt to decode a transaction hex using this blockcypher tool and if it decodes properly and all of the components of a valid transaction are there, then your transaction should be good to go

Loourr

Posted 2012-01-05T00:00:32.243

Reputation: 3 022

-1

I understand there are a couple things that need to be checked for a transaction to be valid - the previous transaction had to be correct, no OP can invalidate the transaction, etc, but is there a complete list on what one needs to check in order to know if a transaction is valid?

You check the chain, if it goes all the way back to a generation, it is valid. You must wait for a confirmation to get it set in stone.

What steps does the protocol require a client to go through to check if the transaction is valid?

https://github.com/bitcoin/bitcoin/tree/master/src

Fabián Heredia Montiel

Posted 2012-01-05T00:00:32.243

Reputation: 139

Well, quite more than "a [single] confirmation" to "set [it] in stone".o0'. 2012-04-05T11:08:46.023

Though you're technically correct in saying that you need to check the chain all the way back to generation, this is not useful. Clients only store the part of the chain they've verified to be valid, and the only thing they need to check is whether it correctly connects to the transactions therein. Also, transactions are never entirely set in stone, but each confirmation significantly reduces the chance for reverting further.Pieter Wuille 2012-05-05T12:06:20.973

There has not been and there will likely not be a single Chain Fork attack. It is not worth it, it is unlikely, it has not happened. fThe only ever attack to the Bitcoin Client or Chain was an Integer Overflow back at the beginnings of time.Fabián Heredia Montiel 2012-05-05T16:59:58.050