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 = BitConverter.GetBytes((ushort)nCount);
123 if (BitConverter.IsLittleEndian)
125 Array.Reverse(szBytes);
127 codeBytes.AddRange(szBytes);
129 else if (nCount < 0xffffffff)
131 // OP_PUSHDATA1 0x00 0x00 0x00 0x01 [0x5a]
132 codeBytes.Add((byte)opcodetype.OP_PUSHDATA4);
134 byte[] szBytes = BitConverter.GetBytes((uint)nCount);
135 if (BitConverter.IsLittleEndian)
137 Array.Reverse(szBytes);
139 codeBytes.AddRange(szBytes);
143 codeBytes.AddRange(dataBytes);
147 /// Scan code bytes for pattern
149 /// <param name="pattern">Pattern sequence</param>
150 /// <returns>Matches enumerator</returns>
151 private IEnumerable<int> FindPattern(IList<byte> pattern)
153 for (int i = 0; i < codeBytes.Count; i++)
155 if (codeBytes.Skip(i).Take(pattern.Count).SequenceEqual(pattern))
163 /// Scan code bytes for pattern and remove it
165 /// <param name="pattern">Pattern sequence</param>
166 /// <returns>Matches number</returns>
167 public int RemovePattern(IList<byte> pattern)
169 List<byte> resultBytes = new List<byte>(codeBytes);
171 int patternLen = pattern.Count;
173 foreach (int i in FindPattern(pattern))
175 resultBytes.RemoveRange(i - count * patternLen, patternLen);
179 codeBytes = resultBytes;
185 /// Is it true that script doesn't contain anything except push value operations?
187 /// <returns>Checking result</returns>
188 public bool IsPushonly()
190 WrappedList<byte> wCodeBytes = new WrappedList<byte>(codeBytes);
192 opcodetype opcode; // Current opcode
193 IEnumerable<byte> pushArgs; // OP_PUSHDATAn argument
195 // Scan opcodes sequence
196 while (ScriptOpcode.GetOp(ref wCodeBytes, out opcode, out pushArgs))
198 if (opcode > opcodetype.OP_16)
200 // We don't allow control opcodes here
209 /// Is it true that script doesn't contain non-canonical push operations?
211 /// <returns>Checking result</returns>
212 public bool HashOnlyCanonicalPushes()
214 WrappedList<byte> wCodeBytes = new WrappedList<byte>(codeBytes);
216 opcodetype opcode; // Current opcode
217 IEnumerable<byte> pushArgs; // OP_PUSHDATAn argument
219 // Scan opcodes sequence
220 while (ScriptOpcode.GetOp(ref wCodeBytes, out opcode, out pushArgs))
222 byte[] data = pushArgs.ToArray();
224 if (opcode < opcodetype.OP_PUSHDATA1 && opcode > opcodetype.OP_0 && (data.Length == 1 && data[0] <= 16))
225 // Could have used an OP_n code, rather than a 1-byte push.
227 if (opcode == opcodetype.OP_PUSHDATA1 && data.Length < (int)opcodetype.OP_PUSHDATA1)
228 // Could have used a normal n-byte push, rather than OP_PUSHDATA1.
230 if (opcode == opcodetype.OP_PUSHDATA2 && data.Length <= 0xFF)
231 // Could have used an OP_PUSHDATA1.
233 if (opcode == opcodetype.OP_PUSHDATA4 && data.LongLength <= 0xFFFF)
234 // 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)
322 /// ... and return its opcount:
323 CScript subScript = new CScript(pushArgs);
325 return subScript.GetSigOpCount(true);
329 public void SetDestination(CKeyID ID)
332 AddOp(opcodetype.OP_DUP);
333 AddOp(opcodetype.OP_HASH160);
335 AddOp(opcodetype.OP_EQUAL);
338 public void SetDestination(CScriptID ID)
341 AddOp(opcodetype.OP_HASH160);
343 AddOp(opcodetype.OP_EQUAL);
346 public void SetMultiSig(int nRequired, IEnumerable<CKey> keys)
349 AddOp(ScriptOpcode.EncodeOP_N(nRequired));
351 foreach (CKey key in keys)
353 PushData(key.GetPubKey().Raw);
355 AddOp(ScriptOpcode.EncodeOP_N(keys.Count()));
356 AddOp(opcodetype.OP_CHECKMULTISIG);
360 /// Disassemble current script code
362 /// <returns>Code listing</returns>
363 public override string ToString()
365 StringBuilder sb = new StringBuilder();
366 WrappedList<byte> wCodeBytes = new WrappedList<byte>(codeBytes);
368 opcodetype opcode; // Current opcode
369 IEnumerable<byte> pushArgs; // OP_PUSHDATAn argument
370 while (ScriptOpcode.GetOp(ref wCodeBytes, out opcode, out pushArgs))
377 if (0 <= opcode && opcode <= opcodetype.OP_PUSHDATA4)
379 sb.Append(ScriptOpcode.ValueString(pushArgs));
383 sb.Append(ScriptOpcode.GetOpName(opcode));
387 return sb.ToString();