private int CountP2SHSigOps(ValidatableTx validatableTx, int inputIndex)
{
if (validatableTx.IsCoinbase)
return 0;
var prevTxOutput = validatableTx.PrevTxOutputs[inputIndex];
if (prevTxOutput.IsPayToScriptHash())
{
var script = validatableTx.Transaction.Inputs[inputIndex].ScriptSignature;
ImmutableArray<byte>? data = null;
// find the last item pushed onto the stack in the P2SH script signature...
var index = 0;
while (index < script.Length)
{
ScriptOp op;
if (!GetOp(script, ref index, out op, out data))
return 0;
else if (op > ScriptOp.OP_16)
return 0;
}
if (data == null)
return 0;
// ...and count its sig ops
return CountLegacySigOps(data.Value, accurate: true);
}
else
return 0;
}