4 using System.Collections;
5 using System.Collections.Generic;
9 public class CScriptException : Exception
11 public CScriptException()
15 public CScriptException(string message)
20 public CScriptException(string message, Exception inner)
21 : base(message, inner)
27 /// Representation of script code
31 private List<byte> codeBytes;
34 /// Initializes an empty instance of CScript
38 codeBytes = new List<byte>();
42 /// Initializes new instance of CScript and fills it with supplied bytes
44 /// <param name="bytes">List of bytes</param>
45 public CScript(IList<byte> bytes)
47 codeBytes = new List<byte>(bytes);
51 /// Initializes new instance of CScript and fills it with supplied bytes
53 /// <param name="bytes">Array of bytes</param>
54 public CScript(byte[] bytes)
56 codeBytes = new List<byte>(bytes);
60 /// Adds specified operation to opcode bytes list
62 /// <param name="opcode"></param>
63 public void AddOp(opcodetype opcode)
65 if (opcode < opcodetype.OP_0 || opcode > opcodetype.OP_INVALIDOPCODE)
67 throw new CScriptException("CScript::AddOp() : invalid opcode");
70 codeBytes.Add((byte)opcode);
74 /// Adds hash to opcode bytes list.
75 /// New items are added in this format:
76 /// hash_length_byte hash_bytes
78 /// <param name="hash">Hash160 instance</param>
79 public void AddHash(Hash160 hash)
81 codeBytes.Add((byte)hash.hashSize);
82 codeBytes.AddRange(hash.hashBytes);
86 /// Adds hash to opcode bytes list.
87 /// New items are added in this format:
88 /// hash_length_byte hash_bytes
90 /// <param name="hash">Hash256 instance</param>
91 public void AddHash(Hash256 hash)
93 codeBytes.Add((byte)hash.hashSize);
94 codeBytes.AddRange(hash.hashBytes);
97 public void PushData(IList<byte> dataBytes)
99 if (dataBytes.Count < (int)opcodetype.OP_PUSHDATA1)
101 codeBytes.Add((byte)dataBytes.Count);
103 else if (dataBytes.Count < 0xff)
105 codeBytes.Add((byte)opcodetype.OP_PUSHDATA1);
106 codeBytes.Add((byte)dataBytes.Count);
108 else if (dataBytes.Count < 0xffff)
110 codeBytes.Add((byte)opcodetype.OP_PUSHDATA2);
112 byte[] szBytes = BitConverter.GetBytes((short)dataBytes.Count);
113 if (BitConverter.IsLittleEndian)
114 Array.Reverse(szBytes);
116 codeBytes.AddRange(szBytes);
118 else if ((uint)dataBytes.Count < 0xffffffff)
120 codeBytes.Add((byte)opcodetype.OP_PUSHDATA2);
122 byte[] szBytes = BitConverter.GetBytes((uint)dataBytes.Count);
123 if (BitConverter.IsLittleEndian)
124 Array.Reverse(szBytes);
126 codeBytes.AddRange(szBytes);
128 codeBytes.AddRange(dataBytes);
131 public override string ToString()
135 StringBuilder sb = new StringBuilder();
139 return sb.ToString();