Esempio n. 1
0
 private bool CheckStack(OpData stack, out string error)
 {
     if (stack.ItemCount > 0 && IsNotZero(stack.Pop()))
     {
         error = null;
         return(true);
     }
     else
     {
         error = stack.ItemCount == 0 ? "Emtpy stack" : "Top stack item is not true.";
         return(false);
     }
 }
Esempio n. 2
0
        private bool VerifyP2wsh(ITransaction tx, int index, IRedeemScript redeem, ReadOnlySpan <byte> expectedHash,
                                 ulong amount, out string error)
        {
            ReadOnlySpan <byte> actualHash = sha256.ComputeHash(redeem.Data);

            if (!actualHash.SequenceEqual(expectedHash))
            {
                error = "Invalid hash.";
                return(false);
            }

            if (!redeem.TryEvaluate(out IOperation[] redeemOps, out int redeemOpCount, out error))
            {
                error = $"Script evaluation failed." +
                        $"{Environment.NewLine}TxId: {tx.GetTransactionId()}" +
                        $"{Environment.NewLine}More info: {error}";
                return(false);
            }

            // Note that there is no *Constants.WitnessScaleFactor here anymore
            TotalSigOpCount += redeem.CountSigOps(redeemOps);

            var stack = new OpData()
            {
                Tx        = tx,
                TxInIndex = index,

                ForceLowS            = ForceLowS,
                StrictNumberEncoding = StrictNumberEncoding,

                IsBip65Enabled  = consensus.IsBip65Enabled(BlockHeight),
                IsBip112Enabled = consensus.IsBip112Enabled(BlockHeight),
                IsStrictDerSig  = consensus.IsStrictDerSig(BlockHeight),
                IsBip147Enabled = consensus.IsBip147Enabled(BlockHeight),
            };

            for (int j = 0; j < tx.WitnessList[index].Items.Length - 1; j++)
            {
                if (!tx.WitnessList[index].Items[j].Run(stack, out error))
                {
                    error = $"Script evaluation failed." +
                            $"{Environment.NewLine}TxId: {tx.GetTransactionId()}" +
                            $"{Environment.NewLine}More info: {error}";
                    return(false);
                }
            }

            stack.AmountBeingSpent = amount;
            stack.IsSegWit         = true;
            stack.OpCount          = redeemOpCount;
            stack.ExecutingScript  = redeemOps;

            foreach (var op in redeemOps)
            {
                if (!op.Run(stack, out error))
                {
                    error = $"Script evaluation failed." +
                            $"{Environment.NewLine}TxId: {tx.GetTransactionId()}" +
                            $"{Environment.NewLine}More info: {error}";
                    return(false);
                }
            }

            // Stack has to only have 1 item left
            if (stack.ItemCount == 1 && IsNotZero(stack.Pop()))
            {
                error = null;
                return(true);
            }
            else
            {
                error = stack.ItemCount != 1 ?
                        "Stack has to have only 1 item after witness execution." :
                        "Top stack item is not true";
                return(false);
            }
        }