CTxIn and CTxOut constructors, some interoperability improvements
[NovacoinLibrary.git] / Novacoin / Interop.cs
1 \feffusing System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace Novacoin
8 {
9     public class InteropException : Exception
10     {
11         public InteropException()
12         {
13         }
14
15         public InteropException(string message)
16             : base(message)
17         {
18         }
19
20         public InteropException(string message, Exception inner)
21             : base(message, inner)
22         {
23         }
24     }
25
26     class Interop
27     {
28         public static byte[] LEBytes(ushort n)
29         {
30             byte[] resultBytes = BitConverter.GetBytes(n);
31
32             if (!BitConverter.IsLittleEndian)
33             {
34                 Array.Reverse(resultBytes);
35             }
36
37             return resultBytes;
38         }
39
40         public static byte[] LEBytes(uint n)
41         {
42             byte[] resultBytes = BitConverter.GetBytes(n);
43
44             if (!BitConverter.IsLittleEndian)
45             {
46                 Array.Reverse(resultBytes);
47             }
48
49             return resultBytes;
50         }
51
52         public static byte[] LEBytes(ulong n)
53         {
54             byte[] resultBytes = BitConverter.GetBytes(n);
55
56             if (!BitConverter.IsLittleEndian)
57             {
58                 Array.Reverse(resultBytes);
59             }
60
61             return resultBytes;
62         }
63
64         public static byte[] BEBytes(ushort n)
65         {
66             byte[] resultBytes = BitConverter.GetBytes(n);
67
68             if (BitConverter.IsLittleEndian)
69             {
70                 Array.Reverse(resultBytes);
71             }
72
73             return resultBytes;
74         }
75
76         public static byte[] BEBytes(uint n)
77         {
78             byte[] resultBytes = BitConverter.GetBytes(n);
79
80             if (BitConverter.IsLittleEndian)
81             {
82                 Array.Reverse(resultBytes);
83             }
84
85             return resultBytes;
86         }
87
88         public static byte[] BEBytes(ulong n)
89         {
90             byte[] resultBytes = BitConverter.GetBytes(n);
91
92             if (BitConverter.IsLittleEndian)
93             {
94                 Array.Reverse(resultBytes);
95             }
96
97             return resultBytes;
98         }
99
100         public static ushort LEBytesToUInt16(byte[] bytes)
101         {
102             if (bytes.Length != sizeof(ushort))
103             {
104                 throw new InteropException("Array size doesn't match the ushort data type.");
105             }
106
107             if (!BitConverter.IsLittleEndian)
108             {
109                 Array.Reverse(bytes);
110             }
111
112             return BitConverter.ToUInt16(bytes, 0);
113         }
114
115         public static uint LEBytesToUInt32(byte[] bytes)
116         {
117             if (bytes.Length != sizeof(ushort))
118             {
119                 throw new InteropException("Array size doesn't match the uint data type.");
120             }
121
122             if (!BitConverter.IsLittleEndian)
123             {
124                 Array.Reverse(bytes);
125             }
126
127             return BitConverter.ToUInt32(bytes, 0);
128         }
129
130         public static ulong LEBytesToUInt64(byte[] bytes)
131         {
132             if (bytes.Length != sizeof(ushort))
133             {
134                 throw new InteropException("Array size doesn't match the ulong data type.");
135             }
136
137             if (!BitConverter.IsLittleEndian)
138             {
139                 Array.Reverse(bytes);
140             }
141
142             return BitConverter.ToUInt64(bytes, 0);
143         }
144
145
146     }
147 }