Example #1
0
        /// <summary>
        /// Calculate sum.
        /// </summary>
        /// <returns></returns>
        public int Sum()
        {
            int firstNumber  = Stack.Pop();
            int secondNumber = Stack.Pop();
            int sum          = firstNumber + secondNumber;

            return(sum);
        }
Example #2
0
        private void OpLeft(ParsedOpCode op)
        {
            var high = MainStack.PopInt32();
            var data = MainStack.Pop();

            if (data.Length == 0 || high == 0)
            {
                MainStack.Push(new byte[0]);
            }
            else if (high < 0)
            {
                throw new ScriptIndexOutOfRangeException(op.Code, "upper boundary less than zero");
            }
            else if (high > data.Length)
            {
                throw new ScriptIndexOutOfRangeException(op.Code, "upper boundary greater than array length");
            }
            else if (high < 0)
            {
                throw new ScriptIndexOutOfRangeException(op.Code, "upper boundary less than zero");
            }
            else
            {
                var bytes = data.Take(high).ToArray();
                MainStack.Push(bytes);
            }
        }
Example #3
0
        private void OpEqual(ParsedOpCode op)
        {
            var a = MainStack.Pop();
            var b = MainStack.Pop();

            MainStack.Push(a.SequenceEqual(b));
        }
Example #4
0
        private void OpHash160(ParsedOpCode obj)
        {
            var value = MainStack.Pop();
            var hash  = HashUtil.Ripemd160(HashUtil.Blake256(value));

            MainStack.Push(hash);
        }
Example #5
0
        public void OpCheckSig(ParsedOpCode op, MsgTx transaction)
        {
            try
            {
                var rawPublicKey = MainStack.Pop();
                var rawSignature = MainStack.Pop();

                if (rawSignature.Length < 1)
                {
                    MainStack.Push(false);
                    return;
                }

                var signature     = rawSignature.Take(rawSignature.Length - 1).ToArray();
                var signatureType = (SignatureHashType)rawSignature.Last();

                AssertSignatureHashType(signatureType);
                AssertSignatureEncoding(signature);
                AssertPublicKeyEncoding(rawPublicKey);

                var subScript = _script.GetOpCodesWithoutData(rawSignature);
                var hash      = CalculateSignatureHash(subScript, signatureType, (MsgTx)transaction.Clone(), _txIndex);

                var ecSignature      = new ECSignature(signature);
                var securityService  = new ECPublicSecurityService(rawPublicKey);
                var isValidSignature = securityService.VerifySignature(hash, ecSignature);

                MainStack.Push(isValidSignature);
            }
            catch (ScriptException)
            {
                MainStack.Push(false);
            }
        }
Example #6
0
        private void OpRoll()
        {
            var n   = MainStack.PopInt32();
            var val = MainStack.Pop(n);

            MainStack.Push(val);
        }
Example #7
0
        private void OpSubStr(ParsedOpCode op)
        {
            var startIndex        = MainStack.PopInt32();
            var endIndexExclusive = MainStack.PopInt32();
            var array             = MainStack.Pop();

            if (array.Length == 0)
            {
                MainStack.Push(new byte[0]);
            }
            else if (startIndex < 0 || endIndexExclusive < 0)
            {
                throw new ScriptIndexOutOfRangeException(op.Code, "Negative substring index");
            }
            else if (startIndex > array.Length || endIndexExclusive > array.Length)
            {
                throw new ScriptIndexOutOfRangeException(op.Code, "Substring index out of bounds");
            }
            else if (startIndex > endIndexExclusive)
            {
                throw new ScriptIndexOutOfRangeException(op.Code, "Start index is greater than end index");
            }
            else if (startIndex == endIndexExclusive)
            {
                MainStack.Push(new byte[0]);
            }
            else
            {
                var substr = array.Skip(startIndex).Take(endIndexExclusive - startIndex).ToArray();
                MainStack.Push(substr);
            }
        }
Example #8
0
        private void Op2Rot()
        {
            var a = MainStack.Pop(4);
            var b = MainStack.Pop(4);

            MainStack.Push(b);
            MainStack.Push(a);
        }
Example #9
0
        private void OpSwap()
        {
            var a = MainStack.Pop();
            var b = MainStack.Pop();

            MainStack.Push(a);
            MainStack.Push(b);
        }
Example #10
0
        private void OpCat(ParsedOpCode op)
        {
            var first  = MainStack.Pop();
            var second = MainStack.Pop();

            var bytes = second.Concat(first).ToArray();

            MainStack.Push(bytes);
        }
Example #11
0
        private void OpRot()
        {
            var a = MainStack.Pop();
            var b = MainStack.Pop();
            var c = MainStack.Pop();

            MainStack.Push(b);
            MainStack.Push(a);
            MainStack.Push(c);
        }
Example #12
0
        private void Op2Swap()
        {
            var a = MainStack.Pop();
            var b = MainStack.Pop();
            var c = MainStack.Pop();
            var d = MainStack.Pop();

            MainStack.Push(b);
            MainStack.Push(a);
            MainStack.Push(d);
            MainStack.Push(c);
        }
Example #13
0
        private void OpSha256(ParsedOpCode op)
        {
            if (!Options.EnableSha256)
            {
                OpNop(op);
                return;
            }

            var data = MainStack.Pop();
            var hash = HashUtil.Sha256(data);

            MainStack.Push(hash);
        }
Example #14
0
        private void OpRight(ParsedOpCode op)
        {
            var index = MainStack.PopInt32();
            var data  = MainStack.Pop();

            if (data.Length == 0 || index == data.Length)
            {
                MainStack.Push(new byte[0]);
            }
            else if (index < 0)
            {
                throw new ScriptIndexOutOfRangeException(op.Code, "upper boundary less than zero");
            }
            else if (index > data.Length)
            {
                throw new ScriptIndexOutOfRangeException(op.Code, "upper boundary greater than array length");
            }
            else
            {
                MainStack.Push(data.Skip(index).ToArray());
            }
        }
Example #15
0
        private void OpHash256(ParsedOpCode obj)
        {
            var value = MainStack.Pop();

            MainStack.Push(HashUtil.Blake256D(value));
        }
Example #16
0
 private void Op2Drop()
 {
     MainStack.Pop();
     MainStack.Pop();
 }
Example #17
0
        private void OpSha1(ParsedOpCode obj)
        {
            var value = MainStack.Pop();

            MainStack.Push(HashUtil.Sha1(value));
        }
Example #18
0
        private void OpRipemd160(ParsedOpCode obj)
        {
            var value = MainStack.Pop();

            MainStack.Push(HashUtil.Ripemd160(value));
        }
Example #19
0
 public void Pop()
 {
     AuxiliaryStack.Pop();
     MainStack.Pop();
 }
Example #20
0
        /// <summary>
        /// Method splitting an index and adding it to the BTree
        /// </summary>
        public void SplitIndex()
        {
            if (MainStack.Peek().Items.Count <= NodeSize)
            {
                //Do nothing since all splitting is done
            }

            //Continue to Split
            else
            {
                #region Initialize values

                //Values to be set and used
                Index       CurrentIndex = new Index(MainStack.Peek());
                Index       LeftIndex;
                Index       CenterIndex  = new Index(NodeSize);
                Index       RightIndex   = new Index(NodeSize);
                List <Leaf> RightLeaves  = new List <Leaf>();
                int         half         = CurrentIndex.Items.Count / 2;
                int         newIndexItem = CurrentIndex.Items[half];

                //Values to handle being the first root or
                //a non-inner index
                bool isFirstRootSplit = false;
                if (CurrentIndex.IndexList.Count == 0)
                {
                    isFirstRootSplit = true;
                }

                bool needToGetLeaves = false;
                if (CurrentIndex.LeafList != null)
                {
                    needToGetLeaves = true;
                }

                #endregion

                #region If split at Root

                if (MainStack.Count == 1)
                {
                    //Root = CenterIndex

                    #region Set Right LeafList

                    //Set start to half if the Root doesn't
                    //reference any indexes
                    if (isFirstRootSplit)
                    {
                        for (int rightCount = half; rightCount < CurrentIndex.LeafList.Count; rightCount++)
                        {
                            RightLeaves.Add(CurrentIndex.LeafList[rightCount]);
                        }
                    }

                    #endregion

                    #region Set and Reference RightIndex's Indexes/Leaves

                    for (int i = half + 1; i < CurrentIndex.Items.Count; i++)
                    {
                        RightIndex.Items.Add(CurrentIndex.Items[i]);
                    }

                    if (isFirstRootSplit)
                    {
                        for (int i = 0; i < RightIndex.Items.Count; i++)
                        {
                            RightIndex.LeafList.Add(RightLeaves[i]);
                        }
                    }
                    else
                    {
                        for (int i = half; i < CurrentIndex.Items.Count; i++)
                        {
                            RightIndex.IndexList.Add(CurrentIndex.IndexList[i]);
                        }
                    }

                    #endregion

                    //Dispose values
                    int disposeCount = CurrentIndex.Items.Count;
                    for (int i = half; i < disposeCount; i++)
                    {
                        CurrentIndex.Items.RemoveAt(half);
                        if (isFirstRootSplit)
                        {
                            CurrentIndex.LeafList.RemoveAt(half);
                        }
                        else
                        {
                            CurrentIndex.IndexList.RemoveAt(half);
                        }
                    }

                    //Set Left Index
                    LeftIndex = new Index(CurrentIndex);

                    //Set Right Index Level
                    LeftIndex.IndexLevel  = CurrentIndex.IndexLevel;
                    RightIndex.IndexLevel = CurrentIndex.IndexLevel;

                    //Set and Reference CenterIndex
                    CenterIndex.IndexList.Add(LeftIndex);
                    CenterIndex.Insert(newIndexItem, RightIndex);
                    CenterIndex.IndexLevel = 0;
                    Root = new Index(CenterIndex);

                    //Set Index Levels
                    IncrementAllTreeLevels(Root);

                    //Increment Count
                    TreeIndexes += 2;
                    NodeCount   += 2;

                    //Pop stack and move up tree
                    MainStack.Pop();
                }

                #endregion

                #region If split elsewhere

                else
                {
                    #region Set Right LeafList

                    if (needToGetLeaves)
                    {
                        //Enter Leaves into RightIndex
                        for (int rightCount = half; rightCount < CurrentIndex.LeafList.Count; rightCount++)
                        {
                            RightLeaves.Add(CurrentIndex.LeafList[rightCount]);
                        }
                    }

                    #endregion

                    #region Set and Reference RightIndex's Indexes/Leaves

                    for (int i = half + 1; i < CurrentIndex.Items.Count; i++)
                    {
                        RightIndex.Items.Add(CurrentIndex.Items[i]);
                    }

                    if (needToGetLeaves)
                    {
                        for (int i = 0; i < RightIndex.Items.Count; i++)
                        {
                            RightIndex.LeafList.Add(RightLeaves[i]);
                        }
                    }
                    else
                    {
                        for (int i = half; i < CurrentIndex.Items.Count; i++)
                        {
                            RightIndex.IndexList.Add(CurrentIndex.IndexList[i]);
                        }
                    }

                    #endregion

                    //Set Right Index Level
                    RightIndex.IndexLevel = CurrentIndex.IndexLevel;

                    //Dispose values
                    int disposeCount = CurrentIndex.Items.Count;
                    for (int i = half; i < disposeCount; i++)
                    {
                        MainStack.Peek().Items.RemoveAt(half);
                        if (needToGetLeaves)
                        {
                            MainStack.Peek().LeafList.RemoveAt(half);
                        }
                        else
                        {
                            MainStack.Peek().IndexList.RemoveAt(half);
                        }
                    }

                    //Pop stack and move up tree
                    MainStack.Pop();

                    //Add RightIndex to the Index up the tree
                    //with references
                    MainStack.Peek().Insert(newIndexItem, RightIndex);

                    //Reset Root if needed
                    if (MainStack.Count == 1)
                    {
                        Root = new Index(MainStack.Peek());
                    }

                    //Increment Count
                    TreeIndexes++;
                    NodeCount++;

                    //Recursively call SplitIndex until all
                    //Indexes are split that need it
                    SplitIndex();
                }

                #endregion
            }
        }
Example #21
0
 private void OpToAltStack()
 {
     AltStack.Push(MainStack.Pop());
 }
Example #22
0
 public void Pop()
 {
     MainStack.Pop();
     MStack.Pop();
 }
Example #23
0
 private void OpNip()
 {
     MainStack.Pop(1);
 }