1

Given an arbitrary signed raw transaction, how can we easily verify if all inputs are correctly signed (assuming all inputs are existent/unspent and the fee is higher than zero)? Bitcoin core's RPC command testmempoolaccept will check if all inputs are available to be spent in the mempool/blockchain so it's impossible to test transactions that have parents not yet broadcasted.

I am aware that for this kind of check, the scriptPubKeys of all inputs needs to be known and therefore only the signed raw transaction by itself is not enough for this kind of check. Still, the scriptPubKeys could be passed to the transaction instance or verify method. I was looking for some nice way to do this in python/javascript but was surprised how difficult this task is:

2

Pubkey script alone is not enough to verify a transaction, you'll need:

  1. Pubkey script, to evaluate the scripts
  2. Amount, in order to check if the sum of inputs is bigger than or equal to sum of outputs
  3. Index (in TxOut list) and tx hash of the transaction being spent
  4. Block height so that you can set the consensus rules

After gathering all the above (should be found in the node's UTXO database) you can start evaluating if the provided script is valid which isn't limited to signature verification but running/evaluating the script that includes checking correctness of the script, OP codes, OP count, script size,...
A lot of the verification can be found in validation.cpp file.
This is also how I do it in Bitcoin.Net library.

| improve this answer | |
  • Forgot about block height, which is important for nLockTime... Thank you for sharing code from your project but I would prefer using some more battle-tested code for now, Seems like this task is more easily accomplished in C derived languages (although I was trying to steer away from them), just reminded me that it might also be possible with libbitcoin: github.com/libbitcoin/libbitcoin-system/tree/master/include/… – Pedro Jul 1 at 11:27

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.