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:
- Peter Todd's python-bitcoinlib requires each input to be validated manually with some flags indicating the type of ScriptPubKey.
- 1200wd bitcoinlib, has a transaction.verify() method but it seems to only verify the basic transaction types: coinbase, P2PKH, P2SH(P2PKH), P2SH(multisig), ...
- BitcoinJS has a method for this, psbt.validateSignaturesOfAllInputs() but converting a raw transaction to PSBT is not possible/easy.
- Bitcore's transaction.verify() does mostly some basic sanity checks and doesn't verify the signatures.
- BitcoinJ's transaction.verify() does also basic verification and the script execution, as python-bitcoinlib, has also to be done manually with verification flags.