The
stacktop(-1) method is peeking at the topmost element on the stack and interprets it as a public key without removing it from the stack. The
popstack(stack); removes it from the stack later on.
If you want to extract it, you could potentially change the code and add a line right after the
valtype& vchPubKey = stacktop(-1); and get the public key.
CPubKey pubkey(vchPubKey);
if (pubkey.IsValid())
// do whatever!
else
// fail
I'm particularly interested in extracting the public key of "CScript CTxIn::scriptSig" from P2PKH, P2WPKH, P2TR.
2) Legacy scripts (P2PKH and P2SH) will have public key in their signature script or it may already by in the pubkey script (P2PK).
1) SegWit version 0 scripts will have the pubkey (+ everything else) always in their witness and the signature script will be empty for P2WPKH and P2WSH and will contain the witness program for wrapped SegWit ie. P2SH-P2PKH and P2SH-P2WSH
3) Taproot scripts will have a tweaked public key in their pubkey script (the output) and the signature (+ everything else) in their witness
For all cases OP_CHECKSIG(VERIFY) could be run and consumes a public key.
For 1 & 2 OP_CHECKMULTISIG(VERIFY) could be run and consumes one or more public keys.
For 3 OP_CHECKSIGADD could also be run if it is found in the script that consumes a public key.
Why are you trying to extract public keys?