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 long nCount = dataBytes.LongCount();
106 if (nCount < (int)opcodetype.OP_PUSHDATA1)
109 codeBytes.Add((byte)nCount);
111 else if (nCount < 0xff)
113 // OP_PUSHDATA1 0x01 [0x5a]
114 codeBytes.Add((byte)opcodetype.OP_PUSHDATA1);
115 codeBytes.Add((byte)nCount);
117 else if (nCount < 0xffff)
119 // OP_PUSHDATA1 0x00 0x01 [0x5a]
120 codeBytes.Add((byte)opcodetype.OP_PUSHDATA2);
122 byte[] szBytes = Interop.BEBytes((ushort)nCount);
123 codeBytes.AddRange(szBytes);
125 else if (nCount < 0xffffffff)
127 // OP_PUSHDATA1 0x00 0x00 0x00 0x01 [0x5a]
128 codeBytes.Add((byte)opcodetype.OP_PUSHDATA4);
130 byte[] szBytes = Interop.BEBytes((uint)nCount);
131 codeBytes.AddRange(szBytes);
135 codeBytes.AddRange(dataBytes);
139 /// Scan code bytes for pattern
141 /// <param name="pattern">Pattern sequence</param>
142 /// <returns>Matches enumerator</returns>
143 private IEnumerable<int> FindPattern(IList<byte> pattern)
145 for (int i = 0; i < codeBytes.Count; i++)
147 if (codeBytes.Skip(i).Take(pattern.Count).SequenceEqual(pattern))
155 /// Scan code bytes for pattern and remove it
157 /// <param name="pattern">Pattern sequence</param>
158 /// <returns>Matches number</returns>
159 public int RemovePattern(IList<byte> pattern)
161 List<byte> resultBytes = new List<byte>(codeBytes);
163 int patternLen = pattern.Count;
165 foreach (int i in FindPattern(pattern))
167 resultBytes.RemoveRange(i - count * patternLen, patternLen);
171 codeBytes = resultBytes;
177 /// Is it true that script doesn't contain anything except push value operations?
179 /// <returns>Checking result</returns>
180 public bool IsPushonly()
182 WrappedList<byte> wCodeBytes = new WrappedList<byte>(codeBytes);
184 opcodetype opcode; // Current opcode
185 IEnumerable<byte> pushArgs; // OP_PUSHDATAn argument
187 // Scan opcodes sequence
188 while (ScriptOpcode.GetOp(ref wCodeBytes, out opcode, out pushArgs))
190 if (opcode > opcodetype.OP_16)
192 // We don't allow control opcodes here
201 /// Is it true that script doesn't contain non-canonical push operations?
203 /// <returns>Checking result</returns>
204 public bool HashOnlyCanonicalPushes()
206 WrappedList<byte> wCodeBytes = new WrappedList<byte>(codeBytes);
208 opcodetype opcode; // Current opcode
209 IEnumerable<byte> pushArgs; // OP_PUSHDATAn argument
211 // Scan opcodes sequence
212 while (ScriptOpcode.GetOp(ref wCodeBytes, out opcode, out pushArgs))
214 byte[] data = pushArgs.ToArray();
216 if (opcode < opcodetype.OP_PUSHDATA1 && opcode > opcodetype.OP_0 && (data.Length == 1 && data[0] <= 16))
218 // Could have used an OP_n code, rather than a 1-byte push.
221 if (opcode == opcodetype.OP_PUSHDATA1 && data.Length < (int)opcodetype.OP_PUSHDATA1)
223 // Could have used a normal n-byte push, rather than OP_PUSHDATA1.
226 if (opcode == opcodetype.OP_PUSHDATA2 && data.Length <= 0xFF)
228 // Could have used an OP_PUSHDATA1.
231 if (opcode == opcodetype.OP_PUSHDATA4 && data.LongLength <= 0xFFFF)
233 // Could have used an OP_PUSHDATA2.
242 /// Quick test for pay-to-script-hash CScripts
244 /// <returns>Checking result</returns>
245 public bool IsPayToScriptHash()
247 return (codeBytes.Count() == 23 &&
248 codeBytes[0] == (byte)opcodetype.OP_HASH160 &&
249 codeBytes[1] == 0x14 &&
250 codeBytes[22] == (byte)opcodetype.OP_EQUAL);
254 /// Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs
255 /// as 20 sigops. With pay-to-script-hash, that changed:
256 /// CHECKMULTISIGs serialized in scriptSigs are
257 /// counted more accurately, assuming they are of the form
258 /// ... OP_N CHECKMULTISIG ...
260 /// <param name="fAccurate">Legacy mode flag</param>
261 /// <returns>Amount of sigops</returns>
262 public int GetSigOpCount(bool fAccurate)
264 WrappedList<byte> wCodeBytes = new WrappedList<byte>(codeBytes);
266 opcodetype opcode; // Current opcode
267 IEnumerable<byte> pushArgs; // OP_PUSHDATAn argument
270 opcodetype lastOpcode = opcodetype.OP_INVALIDOPCODE;
272 // Scan opcodes sequence
273 while (ScriptOpcode.GetOp(ref wCodeBytes, out opcode, out pushArgs))
275 if (opcode == opcodetype.OP_CHECKSIG || opcode == opcodetype.OP_CHECKSIGVERIFY)
279 else if (opcode == opcodetype.OP_CHECKMULTISIG || opcode == opcodetype.OP_CHECKMULTISIGVERIFY)
281 if (fAccurate && lastOpcode >= opcodetype.OP_1 && lastOpcode <= opcodetype.OP_16)
283 nCount += ScriptOpcode.DecodeOP_N(lastOpcode);
296 /// Accurately count sigOps, including sigOps in
297 /// pay-to-script-hash transactions
299 /// <param name="scriptSig">pay-to-script-hash scriptPubKey</param>
300 /// <returns>SigOps count</returns>
301 public int GetSigOpCount(CScript scriptSig)
303 if (!IsPayToScriptHash())
305 return GetSigOpCount(true);
308 // This is a pay-to-script-hash scriptPubKey;
309 // get the last item that the scriptSig
310 // pushes onto the stack:
311 WrappedList<byte> wScriptSig = scriptSig.GetWrappedList();
313 opcodetype opcode; // Current opcode
314 IEnumerable<byte> pushArgs; // OP_PUSHDATAn argument
316 while (ScriptOpcode.GetOp(ref wScriptSig, out opcode, out pushArgs))
318 if (opcode > opcodetype.OP_16)
324 /// ... and return its opcount:
325 CScript subScript = new CScript(pushArgs);
327 return subScript.GetSigOpCount(true);
331 public void SetDestination(CKeyID ID)
334 AddOp(opcodetype.OP_DUP);
335 AddOp(opcodetype.OP_HASH160);
337 AddOp(opcodetype.OP_EQUAL);
340 public void SetDestination(CScriptID ID)
343 AddOp(opcodetype.OP_HASH160);
345 AddOp(opcodetype.OP_EQUAL);
348 public void SetMultiSig(int nRequired, IEnumerable<CKey> keys)
351 AddOp(ScriptOpcode.EncodeOP_N(nRequired));
353 foreach (CKey key in keys)
355 PushData(key.GetPubKey().Raw);
357 AddOp(ScriptOpcode.EncodeOP_N(keys.Count()));
358 AddOp(opcodetype.OP_CHECKMULTISIG);
362 /// Disassemble current script code
364 /// <returns>Code listing</returns>
365 public override string ToString()
367 StringBuilder sb = new StringBuilder();
368 WrappedList<byte> wCodeBytes = new WrappedList<byte>(codeBytes);
370 opcodetype opcode; // Current opcode
371 IEnumerable<byte> pushArgs; // OP_PUSHDATAn argument
372 while (ScriptOpcode.GetOp(ref wCodeBytes, out opcode, out pushArgs))
379 if (0 <= opcode && opcode <= opcodetype.OP_PUSHDATA4)
381 sb.Append(ScriptOpcode.ValueString(pushArgs));
385 sb.Append(ScriptOpcode.GetOpName(opcode));
389 return sb.ToString();