Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加 xchainAKToEVMAddress 方法,实现 AK 转 EVM address #30

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 86 additions & 1 deletion src/main/java/com/baidu/xuper/api/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;

import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Locale;

public class Account {
private final ECKeyPair ecKeyPair;
Expand Down Expand Up @@ -174,6 +179,84 @@ public static Account getAccountFromPlainFile(String path) {
}
}

/**
* 字节数组转16进制大写字符串
*
* @param bytes
* @return
*/
public static String hexEncodeUpperToString(byte[] bytes) {
StringBuilder result = new StringBuilder();
for (int index = 0, len = bytes.length; index <= len - 1; index += 1) {
String invalue1 = Integer.toHexString((bytes[index] >> 4) & 0xF);
String intValue2 = Integer.toHexString(bytes[index] & 0xF);
result.append(invalue1);
result.append(intValue2);
}
return result.toString().toUpperCase(Locale.ROOT);
}

/**
* AK 转 EVM Address
* @param akAddress
* @return
* @throws Exception
*/

public static String xchainAKToEVMAddress(String akAddress) throws Exception {
if (akAddress == null) {
throw new RuntimeException("getAKAddress() is null");
}
byte[] rawAddr = Base58.decode(akAddress);

if (rawAddr.length < 21) {
throw new RuntimeException("bad address");
}

byte[] ripemd160Hash = Arrays.copyOfRange(rawAddr, 1, 21);
return hexEncodeUpperToString(ripemd160Hash);
}


/**
* EVM Address 转 AK
* @param evmAddress
* @return
* @throws Exception
*/
public static String evmAddressToXchainAK(String evmAddress) throws Exception {
if (evmAddress == null) {
throw new RuntimeException("evmAddress is null");
}
byte[] outputRipemd160 = hexStringToBytes(evmAddress);
if (outputRipemd160.length != 20) {
throw new RuntimeException("bad address");
}

byte[] bufVersion = new byte[]{(byte) 1};
byte[] strSlice = new byte[outputRipemd160.length + bufVersion.length];
System.arraycopy(bufVersion, 0, strSlice, 0, bufVersion.length);
System.arraycopy(outputRipemd160, 0, strSlice, 1, outputRipemd160.length);
byte[] checkCode = Hash.doubleSha256(strSlice);

byte[] slice = new byte[strSlice.length + 4];
System.arraycopy(strSlice, 0, slice, 0, strSlice.length);
System.arraycopy(checkCode, 0, slice, strSlice.length, 4);
String encode = Base58.encode(slice);

return encode;
}


private static byte[] hexStringToBytes(String hex) {
byte[] bytes = new byte[hex.length() / 2];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) Integer.parseInt(hex.substring(i * 2, i * 2 + 2), 16);
}
return bytes;
}


/**
* @return 公私钥相关。
*/
Expand Down Expand Up @@ -229,6 +312,8 @@ public String getAuthRequireId() {
return this.address;
}



class privatePubKey {
String CurvName;
BigInteger D;
Expand Down