Add SignatureOK property, init block signature with zero lengh byte array
[NovacoinLibrary.git] / Novacoin / CBlock.cs
1 \feffusing System;
2 using System.Text;
3 using System.Collections.Generic;
4
5 namespace Novacoin
6 {
7         /// <summary>
8         /// Represents the block. Block consists of header, transaction array and header signature.
9         /// </summary>
10         public class CBlock
11         {
12                 /// <summary>
13                 /// Block header.
14                 /// </summary>
15                 public CBlockHeader header;
16
17                 /// <summary>
18                 /// Transactions array.
19                 /// </summary>
20                 public CTransaction[] vtx;
21
22         /// <summary>
23         /// Block header signature.
24         /// </summary>
25         public byte[] signature = new byte[0];
26
27         public CBlock(CBlock b)
28         {
29             header = new CBlockHeader(b.header);
30
31             for (int i = 0; i < b.vtx.Length; i++)
32             {
33                 vtx[i] = new CTransaction(b.vtx[i]);
34             }
35
36             b.signature.CopyTo(signature, 0);
37         }
38
39         /// <summary>
40         /// Parse byte sequence and initialize new block instance
41         /// </summary>
42         /// <param name="blockBytes"></param>
43                 public CBlock (IList<byte> blockBytes)
44                 {
45             ByteQueue wBytes = new ByteQueue(blockBytes);
46
47             // Fill the block header fields
48             header = new CBlockHeader(wBytes.Get(80));
49
50             // Parse transactions list
51             vtx = CTransaction.ReadTransactionsList(ref wBytes);
52
53             // Read block signature
54             signature = wBytes.Get((int)wBytes.GetVarInt());
55                 }
56
57         public CBlock()
58         {
59             // Initialize empty array of transactions. Please note that such 
60             // configuration is not valid real block since it has to provide 
61             // at least one transaction.
62             vtx = new CTransaction[0];
63         }
64
65         /// <summary>
66         /// Is this a Proof-of-Stake block?
67         /// </summary>
68         public bool IsProofOfStake
69         {
70             get
71             {
72                 return (vtx.Length > 1 && vtx[1].IsCoinStake);
73             }
74         }
75
76         public bool SignatureOK
77         {
78             get
79             {
80                 IList<IEnumerable<byte>> solutions;
81                 txnouttype whichType;
82
83                 if (IsProofOfStake)
84                 {
85                     if (signature.Length == 0)
86                     {
87                         return false; // No signature
88                     }
89
90                     if (!ScriptCode.Solver(vtx[1].vout[1].scriptPubKey, out whichType, out solutions))
91                     {
92                         return false; // No solutions found
93                     }
94
95                     if (whichType == txnouttype.TX_PUBKEY)
96                     {
97                         CPubKey pubkey;
98
99                         try
100                         {
101                             pubkey = new CPubKey(solutions[0]);
102                         }
103                         catch (Exception)
104                         {
105                             return false; // Error while loading public key
106                         }
107
108                         return pubkey.VerifySignature(header.Hash, signature);
109                     }
110                 }
111                 else
112                 {
113                     // Proof-of-Work blocks have no signature
114
115                     return true;
116                 }
117
118                 return false;
119             }
120         }
121
122         /// <summary>
123         /// Convert current instance into sequence of bytes
124         /// </summary>
125         /// <returns>Byte sequence</returns>
126         public IList<byte> Bytes 
127         {
128             get
129             {
130                 List<byte> r = new List<byte>();
131
132                 r.AddRange(header.Bytes);
133                 r.AddRange(VarInt.EncodeVarInt(vtx.LongLength)); // transactions count
134
135                 foreach (CTransaction tx in vtx)
136                 {
137                     r.AddRange(tx.Bytes);
138                 }
139
140                 r.AddRange(VarInt.EncodeVarInt(signature.LongLength));
141                 r.AddRange(signature);
142
143                 return r;
144             }
145         }
146
147         public override string ToString()
148         {
149             StringBuilder sb = new StringBuilder();
150
151             sb.AppendFormat("CBlock(\n header={0},\n", header.ToString());
152
153             foreach(CTransaction tx in vtx)
154             {
155                 sb.AppendFormat("{0},\n", tx.ToString());
156             }
157
158             sb.AppendFormat("signature={0})\n", Interop.ToHex(signature));
159             sb.AppendFormat("signatureOK={0})\n", SignatureOK);
160
161
162             // TODO
163             return sb.ToString();
164         }
165         }
166 }
167