forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompactEncoder.java
More file actions
138 lines (119 loc) · 4.32 KB
/
CompactEncoder.java
File metadata and controls
138 lines (119 loc) · 4.32 KB
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
/*
* java-tron is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* java-tron is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.tron.utils;
import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import static java.util.Arrays.copyOf;
import static java.util.Arrays.copyOfRange;
import static org.spongycastle.util.Arrays.concatenate;
import static org.spongycastle.util.encoders.Hex.encode;
import static org.tron.utils.ByteUtil.appendByte;
public class CompactEncoder {
private final static byte TERMINATOR = 16;
private final static Map<Character, Byte> hexMap = new HashMap<>();
static {
hexMap.put('0', (byte) 0x0);
hexMap.put('1', (byte) 0x1);
hexMap.put('2', (byte) 0x2);
hexMap.put('3', (byte) 0x3);
hexMap.put('4', (byte) 0x4);
hexMap.put('5', (byte) 0x5);
hexMap.put('6', (byte) 0x6);
hexMap.put('7', (byte) 0x7);
hexMap.put('8', (byte) 0x8);
hexMap.put('9', (byte) 0x9);
hexMap.put('a', (byte) 0xa);
hexMap.put('b', (byte) 0xb);
hexMap.put('c', (byte) 0xc);
hexMap.put('d', (byte) 0xd);
hexMap.put('e', (byte) 0xe);
hexMap.put('f', (byte) 0xf);
}
/**
* Pack nibbles to binary
*
* @param nibbles sequence. may have a terminator
* @return hex-encoded byte array
*/
public static byte[] packNibbles(byte[] nibbles) {
int terminator = 0;
if (nibbles[nibbles.length - 1] == TERMINATOR) {
terminator = 1;
nibbles = copyOf(nibbles, nibbles.length - 1);
}
int oddlen = nibbles.length % 2;
int flag = 2 * terminator + oddlen;
if (oddlen != 0) {
byte[] flags = new byte[]{(byte) flag};
nibbles = concatenate(flags, nibbles);
} else {
byte[] flags = new byte[]{(byte) flag, 0};
nibbles = concatenate(flags, nibbles);
}
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
for (int i = 0; i < nibbles.length; i += 2) {
buffer.write(16 * nibbles[i] + nibbles[i + 1]);
}
return buffer.toByteArray();
}
public static boolean hasTerminator(byte[] packedKey) {
return ((packedKey[0] >> 4) & 2) != 0;
}
/**
* Unpack a binary string to its nibbles equivalent
*
* @param str of binary data
* @return array of nibbles in byte-format
*/
public static byte[] unpackToNibbles(byte[] str) {
byte[] base = binToNibbles(str);
base = copyOf(base, base.length - 1);
if (base[0] >= 2) {
base = appendByte(base, TERMINATOR);
}
if (base[0] % 2 == 1) {
base = copyOfRange(base, 1, base.length);
} else {
base = copyOfRange(base, 2, base.length);
}
return base;
}
/**
* Transforms a binary array to hexadecimal format + terminator
*
* @param str byte[]
* @return array with each individual nibble adding a terminator at the end
*/
public static byte[] binToNibbles(byte[] str) {
byte[] hexEncoded = encode(str);
byte[] hexEncodedTerminated = Arrays.copyOf(hexEncoded, hexEncoded.length + 1);
for (int i = 0; i < hexEncoded.length; ++i){
byte b = hexEncodedTerminated[i];
hexEncodedTerminated[i] = hexMap.get((char) b);
}
hexEncodedTerminated[hexEncodedTerminated.length - 1] = TERMINATOR;
return hexEncodedTerminated;
}
public static byte[] binToNibblesNoTerminator(byte[] str) {
byte[] hexEncoded = encode(str);
for (int i = 0; i < hexEncoded.length; ++i){
byte b = hexEncoded[i];
hexEncoded[i] = hexMap.get((char) b);
}
return hexEncoded;
}
}