Use code contracts.
[NovacoinLibrary.git] / Novacoin / COutPoint.cs
1 \feff/**
2  *  Novacoin classes library
3  *  Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
4
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 using System;
20 using System.Collections.Generic;
21 using System.Diagnostics.Contracts;
22 using System.Text;
23
24 namespace Novacoin
25 {
26     public class COutPoint
27     {
28         /// <summary>
29         /// Hash of parent transaction.
30         /// </summary>
31         public Hash256 hash;
32
33         /// <summary>
34         /// Parent input number.
35         /// </summary>
36         public uint n;
37
38         public COutPoint()
39         {
40             hash = new Hash256();
41             n = uint.MaxValue;
42         }
43
44         public COutPoint(Hash256 hashIn, uint nIn)
45         {
46             hash = hashIn;
47             n = nIn;
48         }
49
50         public COutPoint(COutPoint o)
51         {
52             hash = new Hash256(o.hash);
53             n = o.n;
54         }
55
56         public COutPoint(byte[] bytes)
57         {
58             Contract.Requires<ArgumentException>(bytes.Length == 36, "Any valid outpoint reference data item is exactly 36 bytes long.");
59
60             hash = new Hash256(bytes);
61             n = BitConverter.ToUInt32(bytes, 32);
62         }
63
64         public bool IsNull
65         {
66             get { return hash.IsZero && n == uint.MaxValue; }
67         }
68
69         public static implicit operator byte[] (COutPoint o)
70         {
71             var r = new List<byte>();
72             r.AddRange((byte[])o.hash);
73             r.AddRange(BitConverter.GetBytes(o.n));
74
75             return r.ToArray();
76         }
77
78         public override string ToString()
79         {
80             var sb = new StringBuilder();
81             sb.AppendFormat("COutPoint({0}, {1})", hash.ToString(), n);
82
83             return sb.ToString();
84         }
85
86         
87     }
88
89 }