Remove Hash, Hash256, Hash160 and ScryptHash256 classes.
[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.IO;
23 using System.Linq;
24 using System.Text;
25
26 namespace Novacoin
27 {
28     public class COutPoint : IComparable<COutPoint>, IEquatable<COutPoint>
29     {
30         /// <summary>
31         /// Hash of parent transaction.
32         /// </summary>
33         public uint256 hash;
34
35         /// <summary>
36         /// Parent input number.
37         /// </summary>
38         public uint n;
39
40         /// <summary>
41         /// Out reference is always 36 bytes long.
42         /// </summary>
43         public const int Size = 36;
44
45         public COutPoint()
46         {
47             hash = new uint256();
48             n = uint.MaxValue;
49         }
50
51         public COutPoint(uint256 hashIn, uint nIn)
52         {
53             hash = hashIn;
54             n = nIn;
55         }
56
57         public COutPoint(COutPoint o)
58         {
59             hash = o.hash;
60             n = o.n;
61         }
62
63         public COutPoint(byte[] bytes)
64         {
65             Contract.Requires<ArgumentException>(bytes.Length == 36, "Any valid outpoint reference data item is exactly 36 bytes long.");
66
67             hash = bytes.Take(32).ToArray();
68             n = BitConverter.ToUInt32(bytes, 32);
69         }
70
71         public bool IsNull
72         {
73             get { return !hash && n == uint.MaxValue; }
74         }
75
76         public static implicit operator byte[] (COutPoint o)
77         {
78             var stream = new MemoryStream();
79             var writer = new BinaryWriter(stream);
80
81             writer.Write(o.hash);
82             writer.Write(o.n);
83
84             var outBytes = stream.ToArray();
85
86             writer.Close();
87
88             return outBytes;
89         }
90
91         public override string ToString()
92         {
93             var sb = new StringBuilder();
94             sb.AppendFormat("COutPoint({0}, {1})", hash, n);
95
96             return sb.ToString();
97         }
98
99         /// <summary>
100         /// Compare this outpoint with some other.
101         /// </summary>
102         /// <param name="o">Other outpoint.</param>
103         /// <returns>Result of comparison.</returns>
104         public int CompareTo(COutPoint o)
105         {
106             if (n > o.n)
107             {
108                 return 1;
109             }
110             else if (n < o.n)
111             {
112                 return -1;
113             }
114
115             return 0;
116
117         }
118
119         /// <summary>
120         /// Equality comparer for outpoints.
121         /// </summary>
122         /// <param name="o">Other outpoint.</param>
123         /// <returns>Result of comparison.</returns>
124         public bool Equals(COutPoint o)
125         {
126             return (o.n == n) && (o.hash == hash);
127         }
128     }
129
130 }