-
Notifications
You must be signed in to change notification settings - Fork 1
/
Utils.cs
142 lines (127 loc) · 4.56 KB
/
Utils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.IO;
using System.Text;
namespace WavesCS.Core
{
public static class Utils
{
public static void WriteLong(this BinaryWriter writer, long n)
{
n = System.Net.IPAddress.HostToNetworkOrder(n);
writer.Write(n);
}
public static void WriteInt(this BinaryWriter writer, int n)
{
n = System.Net.IPAddress.HostToNetworkOrder(n);
writer.Write(n);
}
public static void WriteByte(this BinaryWriter writer, byte n)
{
writer.Write(n);
}
public static void Write(this BinaryWriter writer, TransactionType n)
{
writer.Write((byte) n);
}
public static void WriteShort(this BinaryWriter writer, int n)
{
byte[] shortN = BitConverter.GetBytes((short)n);
Array.Reverse(shortN);
writer.Write(shortN);
}
public static void WriteObject(this BinaryWriter writer, object o)
{
const byte INTEGER = 0;
const byte BOOLEAN = 1;
const byte BINARY = 2;
const byte STRING = 3;
switch (o)
{
case long value:
writer.Write(INTEGER);
writer.WriteLong(value);
break;
case bool value:
writer.Write(BOOLEAN);
writer.Write(value ? (byte)1 : (byte)0);
break;
case byte[] value:
writer.Write(BINARY);
writer.WriteShort((short)value.Length);
writer.Write(value);
break;
case string value:
writer.Write(STRING);
var encoded = Encoding.UTF8.GetBytes(value);
writer.WriteShort((short)encoded.Length);
writer.Write(encoded);
break;
default:
throw new ArgumentException("Only long, bool and byte[] entry values supported");
}
}
public static void WriteEvaluatedExpression(this BinaryWriter writer, object o)
{
const byte E_LONG = 0;
const byte E_BYTES = 1;
const byte E_STRING = 2;
const byte E_TRUE = 6;
const byte E_FALSE = 7;
switch (o)
{
case long value:
writer.Write(E_LONG);
writer.WriteLong(value);
break;
case bool value:
writer.WriteByte(value ? E_TRUE : E_FALSE);
break;
case byte[] value:
writer.Write(E_BYTES);
writer.WriteInt(value.Length);
writer.Write(value);
break;
case string value:
writer.Write(E_STRING);
var encoded = Encoding.UTF8.GetBytes(value);
writer.WriteInt(encoded.Length);
writer.Write(encoded);
break;
default:
throw new ArgumentException("Only long, bool and byte[] entry values supported");
}
}
public static long CurrentTimestamp()
{
long epochTicks = new DateTime(1970, 1, 1).Ticks;
return (DateTime.UtcNow.Ticks - epochTicks) / (TimeSpan.TicksPerSecond / 1000);
}
public static long ToLong(this DateTime date)
{
return (date - new DateTime(1970, 1, 1)).Ticks / (TimeSpan.TicksPerSecond / 1000);
}
public static string ToBase64(this byte[] data)
{
return "base64:" + Convert.ToBase64String(data);
}
public static byte[] FromBase64(this string data)
{
if (data.StartsWith("base64:"))
data = data.Substring(7);
return Convert.FromBase64CharArray(data.ToCharArray(), 0, data.Length);
}
public static void WriteAsset(this BinaryWriter stream, string assetId)
{
if (string.IsNullOrEmpty(assetId) || assetId == "WAVES")
{
stream.WriteByte(0);
}
else
{
stream.WriteByte(1);
var decoded = Base58.Decode(assetId);
stream.Write(decoded, 0, decoded.Length);
}
}
}
}