-
Notifications
You must be signed in to change notification settings - Fork 159
/
Util.java
170 lines (154 loc) · 5.53 KB
/
Util.java
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package org.stellar.sdk;
import com.google.common.io.BaseEncoding;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import org.stellar.sdk.xdr.ClaimableBalanceID;
import org.stellar.sdk.xdr.SorobanTransactionData;
import org.stellar.sdk.xdr.XdrDataInputStream;
import org.stellar.sdk.xdr.XdrDataOutputStream;
public class Util {
public static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
}
return new String(hexChars);
}
public static byte[] hexToBytes(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] =
(byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
}
return data;
}
/**
* Returns SHA-256 hash of <code>data</code>.
*
* @param data
*/
public static byte[] hash(byte[] data) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(data);
return md.digest();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("SHA-256 not implemented");
}
}
/**
* Pads <code>bytes</code> array to <code>length</code> with zeros.
*
* @param bytes
* @param length
*/
static byte[] paddedByteArray(byte[] bytes, int length) {
byte[] finalBytes = new byte[length];
Arrays.fill(finalBytes, (byte) 0);
System.arraycopy(bytes, 0, finalBytes, 0, bytes.length);
return finalBytes;
}
/**
* Pads <code>string</code> to <code>length</code> with zeros.
*
* @param string
* @param length
*/
static byte[] paddedByteArray(String string, int length) {
return Util.paddedByteArray(string.getBytes(), length);
}
/**
* Remove zeros from the end of <code>bytes</code> array.
*
* @param bytes
*/
static String paddedByteArrayToString(byte[] bytes) {
String[] strArray = new String(bytes).split("\0");
if (strArray.length > 0) {
return strArray[0];
}
return "";
}
/**
* Get the version of the SDK
*
* @return version
*/
public static String getSdkVersion() {
String clientVersion = "dev";
String implementationVersion = Util.class.getPackage().getImplementationVersion();
if (implementationVersion != null) {
clientVersion = implementationVersion;
}
return clientVersion;
}
public static ClaimableBalanceID claimableBalanceIdToXDR(String balanceId) {
byte[] balanceIdBytes = BaseEncoding.base16().lowerCase().decode(balanceId.toLowerCase());
XdrDataInputStream balanceIdXdrDataInputStream =
new XdrDataInputStream(new ByteArrayInputStream(balanceIdBytes));
try {
return ClaimableBalanceID.decode(balanceIdXdrDataInputStream);
} catch (IOException e) {
throw new IllegalArgumentException("invalid balanceId: " + balanceId, e);
}
}
public static String xdrToClaimableBalanceId(ClaimableBalanceID balanceId) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
XdrDataOutputStream xdrDataOutputStream = new XdrDataOutputStream(byteArrayOutputStream);
try {
balanceId.encode(xdrDataOutputStream);
} catch (IOException e) {
throw new IllegalArgumentException("invalid claimClaimableBalanceOp.", e);
}
return BaseEncoding.base16().lowerCase().encode(byteArrayOutputStream.toByteArray());
}
/**
* Convert {@link SorobanTransactionData} XDR String representation to {@link
* SorobanTransactionData} object.
*
* @param sorobanData XDR String representation of a {@link SorobanTransactionData}
* @return {@link SorobanTransactionData}
*/
public static SorobanTransactionData sorobanTransactionDataToXDR(String sorobanData) {
BaseEncoding base64Encoding = BaseEncoding.base64();
byte[] bytes = base64Encoding.decode(sorobanData);
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
XdrDataInputStream xdrInputStream = new XdrDataInputStream(inputStream);
try {
return SorobanTransactionData.decode(xdrInputStream);
} catch (IOException e) {
throw new IllegalArgumentException("invalid sorobanData: " + sorobanData, e);
}
}
/**
* Convert {@link SorobanTransactionData} object to XDR String representation.
*
* @param sorobanData {@link SorobanTransactionData} object
* @return XDR String representation of a {@link SorobanTransactionData}
*/
public static String xdrToSorobanTransactionData(SorobanTransactionData sorobanData) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
XdrDataOutputStream xdrDataOutputStream = new XdrDataOutputStream(byteArrayOutputStream);
try {
sorobanData.encode(xdrDataOutputStream);
} catch (IOException e) {
throw new IllegalArgumentException("invalid sorobanData.", e);
}
BaseEncoding base64Encoding = BaseEncoding.base64();
return base64Encoding.encode(byteArrayOutputStream.toByteArray());
}
public static AssetTypeCreditAlphaNum assertNonNativeAsset(Asset asset) {
if (asset instanceof AssetTypeCreditAlphaNum) {
return (AssetTypeCreditAlphaNum) asset;
}
throw new IllegalArgumentException("native assets are not supported");
}
}