2 * Novacoin classes library
3 * Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
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.
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.
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/>.
20 using System.Collections.Generic;
21 using System.Diagnostics.Contracts;
28 public class COutPoint : IComparable<COutPoint>, IEquatable<COutPoint>, IEqualityComparer<COutPoint>
31 /// Hash of parent transaction.
36 /// Parent input number.
41 /// Out reference is always 36 bytes long.
43 public const int Size = 36;
51 public COutPoint(uint256 hashIn, uint nIn)
57 public COutPoint(COutPoint o)
63 public COutPoint(byte[] bytes)
65 Contract.Requires<ArgumentException>(bytes.Length == 36, "Any valid outpoint reference data item is exactly 36 bytes long.");
67 hash = bytes.Take(32).ToArray();
68 n = BitConverter.ToUInt32(bytes, 32);
73 get { return !hash && n == uint.MaxValue; }
76 public static implicit operator byte[] (COutPoint o)
78 var stream = new MemoryStream();
79 var writer = new BinaryWriter(stream);
84 var outBytes = stream.ToArray();
91 public static implicit operator COutPoint(byte[] b)
93 return new COutPoint(b);
96 public override string ToString()
98 var sb = new StringBuilder();
99 sb.AppendFormat("COutPoint({0}, {1})", hash, n);
101 return sb.ToString();
105 /// Compare this outpoint with some other.
107 /// <param name="o">Other outpoint.</param>
108 /// <returns>Result of comparison.</returns>
109 public int CompareTo(COutPoint o)
125 /// Equality comparer for outpoints.
127 /// <param name="o">Other outpoint.</param>
128 /// <returns>Result of comparison.</returns>
129 public bool Equals(COutPoint o)
131 if (object.ReferenceEquals(o, null))
136 return (o.n == n) && (o.hash == hash);
140 /// Equality comparer for outpoints.
142 /// <param name="x">First outpoint.</param>
143 /// <param name="y">Second outpoint.</param>
144 /// <returns>Result of comparison.</returns>
145 public bool Equals(COutPoint x, COutPoint y)
147 return (x.n == y.n) && (x.hash == y.hash);
150 public override int GetHashCode()
152 return n.GetHashCode() ^ hash.GetHashCode();
155 public int GetHashCode(COutPoint obj)
157 return obj.GetHashCode();