5 using System.Collections;
6 using System.Collections.Generic;
10 public class CScriptException : Exception
12 public CScriptException()
16 public CScriptException(string message)
21 public CScriptException(string message, Exception inner)
22 : base(message, inner)
28 /// Representation of script code
32 private List<byte> codeBytes;
35 /// Initializes an empty instance of CScript
39 codeBytes = new List<byte>();
43 /// Initializes new instance of CScript and fills it with supplied bytes
45 /// <param name="bytes">List of bytes</param>
46 public CScript(IList<byte> bytes)
48 codeBytes = new List<byte>(bytes);
52 /// Initializes new instance of CScript and fills it with supplied bytes
54 /// <param name="bytes">Array of bytes</param>
55 public CScript(byte[] bytes)
57 codeBytes = new List<byte>(bytes);
61 /// Adds specified operation to opcode bytes list
63 /// <param name="opcode"></param>
64 public void AddOp(opcodetype opcode)
66 if (opcode < opcodetype.OP_0 || opcode > opcodetype.OP_INVALIDOPCODE)
68 throw new CScriptException("CScript::AddOp() : invalid opcode");
71 codeBytes.Add((byte)opcode);
75 /// Adds hash to opcode bytes list.
76 /// New items are added in this format:
77 /// hash_length_byte hash_bytes
79 /// <param name="hash">Hash160 instance</param>
80 public void AddHash(Hash160 hash)
82 codeBytes.Add((byte)hash.hashSize);
83 codeBytes.AddRange(hash.hashBytes);
87 /// Adds hash to opcode bytes list.
88 /// New items are added in this format:
89 /// hash_length_byte hash_bytes
91 /// <param name="hash">Hash256 instance</param>
92 public void AddHash(Hash256 hash)
94 codeBytes.Add((byte)hash.hashSize);
95 codeBytes.AddRange(hash.hashBytes);
99 /// Create new OP_PUSHDATAn operator and add it to opcode bytes list
101 /// <param name="dataBytes">List of data bytes</param>
102 public void PushData(IList<byte> dataBytes)
104 if (dataBytes.Count < (int)opcodetype.OP_PUSHDATA1)
107 codeBytes.Add((byte)dataBytes.Count);
109 else if (dataBytes.Count < 0xff)
111 // OP_PUSHDATA1 0x01 [0x5a]
112 codeBytes.Add((byte)opcodetype.OP_PUSHDATA1);
113 codeBytes.Add((byte)dataBytes.Count);
115 else if (dataBytes.Count < 0xffff)
117 // OP_PUSHDATA1 0x00 0x01 [0x5a]
118 codeBytes.Add((byte)opcodetype.OP_PUSHDATA2);
120 byte[] szBytes = BitConverter.GetBytes((short)dataBytes.Count);
121 if (BitConverter.IsLittleEndian)
123 Array.Reverse(szBytes);
125 codeBytes.AddRange(szBytes);
127 else if ((uint)dataBytes.Count < 0xffffffff)
129 // OP_PUSHDATA1 0x00 0x00 0x00 0x01 [0x5a]
130 codeBytes.Add((byte)opcodetype.OP_PUSHDATA4);
132 byte[] szBytes = BitConverter.GetBytes((uint)dataBytes.Count);
133 if (BitConverter.IsLittleEndian)
135 Array.Reverse(szBytes);
137 codeBytes.AddRange(szBytes);
141 codeBytes.AddRange(dataBytes);
145 /// Scan code bytes for pattern
147 /// <param name="pattern">Pattern sequence</param>
148 /// <returns>Matches enumerator</returns>
149 private IEnumerable<int> FindPattern(IList<byte> pattern)
151 for (int i = 0; i < codeBytes.Count; i++)
153 if (codeBytes.Skip(i).Take(pattern.Count).SequenceEqual(pattern))
161 /// Scan code bytes for pattern and remove it
163 /// <param name="pattern">Pattern sequence</param>
164 /// <returns>Matches number</returns>
165 public int RemovePattern(IList<byte> pattern)
167 List<byte> resultBytes = new List<byte>(codeBytes);
169 int patternLen = pattern.Count;
171 foreach (int i in FindPattern(pattern))
173 resultBytes.RemoveRange(i - count * patternLen, patternLen);
177 codeBytes = resultBytes;
183 /// Disassemble current script code
185 /// <returns>Code listing</returns>
186 public override string ToString()
188 StringBuilder sb = new StringBuilder();
189 WrappedList<byte> wCodeBytes = new WrappedList<byte>(codeBytes);
191 opcodetype opcode; // Current opcode
192 IEnumerable<byte> pushArgs; // OP_PUSHDATAn argument
193 while (ScriptOpcode.GetOp(ref wCodeBytes, out opcode, out pushArgs))
200 if (0 <= opcode && opcode <= opcodetype.OP_PUSHDATA4)
202 sb.Append(ScriptOpcode.ValueString(pushArgs));
206 sb.Append(ScriptOpcode.GetOpName(opcode));
210 return sb.ToString();