-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
97881c2
commit a039997
Showing
3 changed files
with
99 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package org.bitcorej.chain.stellar; | ||
|
||
import com.google.common.io.BaseEncoding; | ||
import org.stellar.sdk.FormatException; | ||
|
||
import java.util.Arrays; | ||
|
||
public class StrKey { | ||
public enum VersionByte { | ||
ACCOUNT_ID((byte)(6 << 3)), // G | ||
SEED((byte)(18 << 3)), // S | ||
PRE_AUTH_TX((byte)(19 << 3)), // T | ||
SHA256_HASH((byte)(23 << 3)); // X | ||
private final byte value; | ||
VersionByte(byte value) { | ||
this.value = value; | ||
} | ||
public int getValue() { | ||
return value; | ||
} | ||
} | ||
|
||
private static BaseEncoding base32Encoding = BaseEncoding.base32().upperCase().omitPadding(); | ||
|
||
public static byte[] decodeStellarSecretSeed(char[] data) { | ||
return decodeCheck(VersionByte.SEED, data); | ||
} | ||
|
||
static byte[] decodeCheck(VersionByte versionByte, char[] encoded) { | ||
byte[] bytes = new byte[encoded.length]; | ||
for (int i = 0; i < encoded.length; i++) { | ||
if (encoded[i] > 127) { | ||
throw new IllegalArgumentException("Illegal characters in encoded char array."); | ||
} | ||
bytes[i] = (byte) encoded[i]; | ||
} | ||
|
||
byte[] decoded = base32Encoding.decode(java.nio.CharBuffer.wrap(encoded)); | ||
byte decodedVersionByte = decoded[0]; | ||
byte[] payload = Arrays.copyOfRange(decoded, 0, decoded.length-2); | ||
byte[] data = Arrays.copyOfRange(payload, 1, payload.length); | ||
byte[] checksum = Arrays.copyOfRange(decoded, decoded.length-2, decoded.length); | ||
|
||
if (decodedVersionByte != versionByte.getValue()) { | ||
throw new FormatException("Version byte is invalid"); | ||
} | ||
|
||
byte[] expectedChecksum = calculateChecksum(payload); | ||
|
||
if (!Arrays.equals(expectedChecksum, checksum)) { | ||
throw new FormatException("Checksum invalid"); | ||
} | ||
|
||
if (VersionByte.SEED.getValue() == decodedVersionByte) { | ||
Arrays.fill(bytes, (byte) 0); | ||
Arrays.fill(decoded, (byte) 0); | ||
Arrays.fill(payload, (byte) 0); | ||
} | ||
|
||
return data; | ||
} | ||
|
||
static byte[] calculateChecksum(byte[] bytes) { | ||
// This code calculates CRC16-XModem checksum | ||
// Ported from https://github.com/alexgorbatchev/node-crc | ||
int crc = 0x0000; | ||
int count = bytes.length; | ||
int i = 0; | ||
int code; | ||
|
||
while (count > 0) { | ||
code = crc >>> 8 & 0xFF; | ||
code ^= bytes[i++] & 0xFF; | ||
code ^= code >>> 4; | ||
crc = crc << 8 & 0xFFFF; | ||
crc ^= code; | ||
code = code << 5 & 0xFFFF; | ||
crc ^= code; | ||
code = code << 7 & 0xFFFF; | ||
crc ^= code; | ||
count--; | ||
} | ||
|
||
// little-endian | ||
return new byte[] { | ||
(byte)crc, | ||
(byte)(crc >>> 8)}; | ||
} | ||
} |