Skip to content

Commit

Permalink
fix sol secret format
Browse files Browse the repository at this point in the history
  • Loading branch information
pengpengliu committed Dec 28, 2020
1 parent 97881c2 commit a039997
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@
import org.bitcorej.utils.NumericUtil;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

public class SOLStateProvider implements ChainState {
@Override
public KeyPair generateKeyPair(String secret) {
Ed25519 keyPair = new Ed25519(NumericUtil.hexToBytes(secret));
return new KeyPair(keyPair.B58SecKey(), keyPair.B58PubKey());
return new KeyPair(NumericUtil.bytesToHex(keyPair.seckey()), keyPair.B58PubKey());
}

@Override
public KeyPair generateKeyPair() {
return this.generateKeyPair(new Ed25519().B58SecKey());
return this.generateKeyPair(NumericUtil.bytesToHex(new Ed25519().seckey()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.bitcorej.chain.stellar;

import org.bitcorej.chain.sol.SOLStateProvider;
import org.bitcorej.core.Network;
import org.bitcorej.utils.NumericUtil;
import org.json.JSONObject;
Expand All @@ -13,6 +14,12 @@ public KINStateProvider(Network network) {
super(network);
}

public static org.bitcorej.chain.KeyPair toSol(String secret) {
char[] charSeed = secret.toCharArray();
byte[] decoded = StrKey.decodeStellarSecretSeed(charSeed);
return new SOLStateProvider().generateKeyPair(NumericUtil.bytesToHex(decoded));
}

@Override
protected void switchNetwork() {
org.stellar.sdk.Network.use(new org.stellar.sdk.Network("Kin Mainnet ; December 2018"));
Expand Down
89 changes: 89 additions & 0 deletions core/src/main/java/org/bitcorej/chain/stellar/StrKey.java
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)};
}
}

0 comments on commit a039997

Please sign in to comment.