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">Enumerator interface for byte sequence</param>
46 public CScript(IEnumerable<byte> bytes)
48 codeBytes = new List<byte>(bytes);
52 /// Return a new instance of WrappedList object for current code bytes
54 /// <returns></returns>
55 public WrappedList<byte> GetWrappedList()
57 return new WrappedList<byte>(codeBytes);
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 /// Is it true that script doesn't contain anything except push value operations?
185 /// <returns>Checking result</returns>
186 public bool IsPushonly()
188 WrappedList<byte> wCodeBytes = new WrappedList<byte>(codeBytes);
190 opcodetype opcode; // Current opcode
191 IEnumerable<byte> pushArgs; // OP_PUSHDATAn argument
193 // Scan opcodes sequence
194 while (ScriptOpcode.GetOp(ref wCodeBytes, out opcode, out pushArgs))
196 if (opcode > opcodetype.OP_16)
198 // We don't allow control opcodes here
207 /// Is it true that script doesn't contain non-canonical push operations?
209 /// <returns>Checking result</returns>
210 public bool HashOnlyCanonicalPushes()
212 WrappedList<byte> wCodeBytes = new WrappedList<byte>(codeBytes);
214 opcodetype opcode; // Current opcode
215 IEnumerable<byte> pushArgs; // OP_PUSHDATAn argument
217 // Scan opcodes sequence
218 while (ScriptOpcode.GetOp(ref wCodeBytes, out opcode, out pushArgs))
220 byte[] data = pushArgs.ToArray();
222 if (opcode < opcodetype.OP_PUSHDATA1 && opcode > opcodetype.OP_0 && (data.Length == 1 && data[0] <= 16))
223 // Could have used an OP_n code, rather than a 1-byte push.
225 if (opcode == opcodetype.OP_PUSHDATA1 && data.Length < (int)opcodetype.OP_PUSHDATA1)
226 // Could have used a normal n-byte push, rather than OP_PUSHDATA1.
228 if (opcode == opcodetype.OP_PUSHDATA2 && data.Length <= 0xFF)
229 // Could have used an OP_PUSHDATA1.
231 if (opcode == opcodetype.OP_PUSHDATA4 && data.Length <= 0xFFFF)
232 // Could have used an OP_PUSHDATA2.
240 /// Quick test for pay-to-script-hash CScripts
242 /// <returns>Checking result</returns>
243 public bool IsPayToScriptHash()
245 return (codeBytes.Count() == 23 &&
246 codeBytes[0] == (byte)opcodetype.OP_HASH160 &&
247 codeBytes[1] == 0x14 &&
248 codeBytes[22] == (byte)opcodetype.OP_EQUAL);
252 /// Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs
253 /// as 20 sigops. With pay-to-script-hash, that changed:
254 /// CHECKMULTISIGs serialized in scriptSigs are
255 /// counted more accurately, assuming they are of the form
256 /// ... OP_N CHECKMULTISIG ...
258 /// <param name="fAccurate">Legacy mode flag</param>
259 /// <returns>Amount of sigops</returns>
260 public int GetSigOpCount(bool fAccurate)
262 WrappedList<byte> wCodeBytes = new WrappedList<byte>(codeBytes);
264 opcodetype opcode; // Current opcode
265 IEnumerable<byte> pushArgs; // OP_PUSHDATAn argument
268 opcodetype lastOpcode = opcodetype.OP_INVALIDOPCODE;
270 // Scan opcodes sequence
271 while (ScriptOpcode.GetOp(ref wCodeBytes, out opcode, out pushArgs))
273 if (opcode == opcodetype.OP_CHECKSIG || opcode == opcodetype.OP_CHECKSIGVERIFY)
277 else if (opcode == opcodetype.OP_CHECKMULTISIG || opcode == opcodetype.OP_CHECKMULTISIGVERIFY)
279 if (fAccurate && lastOpcode >= opcodetype.OP_1 && lastOpcode <= opcodetype.OP_16)
281 nCount += ScriptOpcode.DecodeOP_N(lastOpcode);
294 /// Accurately count sigOps, including sigOps in
295 /// pay-to-script-hash transactions
297 /// <param name="scriptSig">pay-to-script-hash scriptPubKey</param>
298 /// <returns>SigOps count</returns>
299 public int GetSigOpCount(CScript scriptSig)
301 if (!IsPayToScriptHash())
303 return GetSigOpCount(true);
306 // This is a pay-to-script-hash scriptPubKey;
307 // get the last item that the scriptSig
308 // pushes onto the stack:
309 WrappedList<byte> wScriptSig = scriptSig.GetWrappedList();
311 opcodetype opcode; // Current opcode
312 IEnumerable<byte> pushArgs; // OP_PUSHDATAn argument
314 while (ScriptOpcode.GetOp(ref wScriptSig, out opcode, out pushArgs))
316 if (opcode > opcodetype.OP_16)
320 /// ... and return its opcount:
321 CScript subScript = new CScript(pushArgs);
323 return subScript.GetSigOpCount(true);
327 public void SetDestination(CKeyID ID)
330 AddOp(opcodetype.OP_DUP);
331 AddOp(opcodetype.OP_HASH160);
333 AddOp(opcodetype.OP_EQUAL);
336 public void SetDestination(CScriptID ID)
339 AddOp(opcodetype.OP_HASH160);
341 AddOp(opcodetype.OP_EQUAL);
344 public void SetMultiSig(int nRequired, IEnumerable<CKey> keys)
347 AddOp(ScriptOpcode.EncodeOP_N(nRequired));
349 foreach (CKey key in keys)
351 PushData(key.GetPubKey().Raw);
353 AddOp(ScriptOpcode.EncodeOP_N(keys.Count()));
354 AddOp(opcodetype.OP_CHECKMULTISIG);
358 /// Disassemble current script code
360 /// <returns>Code listing</returns>
361 public override string ToString()
363 StringBuilder sb = new StringBuilder();
364 WrappedList<byte> wCodeBytes = new WrappedList<byte>(codeBytes);
366 opcodetype opcode; // Current opcode
367 IEnumerable<byte> pushArgs; // OP_PUSHDATAn argument
368 while (ScriptOpcode.GetOp(ref wCodeBytes, out opcode, out pushArgs))
375 if (0 <= opcode && opcode <= opcodetype.OP_PUSHDATA4)
377 sb.Append(ScriptOpcode.ValueString(pushArgs));
381 sb.Append(ScriptOpcode.GetOpName(opcode));
385 return sb.ToString();