From a6969762b24a08d485ce9fb99f80b39f43477456 Mon Sep 17 00:00:00 2001 From: springrain Date: Mon, 10 Jan 2022 11:16:42 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20=20xchainAKToEVMAddres?= =?UTF-8?q?s=20=E6=96=B9=E6=B3=95,=E5=AE=9E=E7=8E=B0=20AK=20=E8=BD=AC=20EV?= =?UTF-8?q?M=20address?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/baidu/xuper/api/Account.java | 47 ++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/baidu/xuper/api/Account.java b/src/main/java/com/baidu/xuper/api/Account.java index fa9107c..5be26ca 100644 --- a/src/main/java/com/baidu/xuper/api/Account.java +++ b/src/main/java/com/baidu/xuper/api/Account.java @@ -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; @@ -174,6 +179,44 @@ 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 + * + * @return + * @throws Exception + */ + + public String xchainAKToEVMAddress() throws Exception { + if (getAKAddress() == null) { + throw new RuntimeException("getAKAddress() is null"); + } + byte[] rawAddr = Base58.decode(getAKAddress()); + + if (rawAddr.length < 21) { + throw new RuntimeException("bad address"); + } + + byte[] ripemd160Hash = Arrays.copyOfRange(rawAddr, 1, 21); + return hexEncodeUpperToString(ripemd160Hash); + } + /** * @return 公私钥相关。 */ @@ -229,6 +272,8 @@ public String getAuthRequireId() { return this.address; } + + class privatePubKey { String CurvName; BigInteger D; From 144191943ed58f183413634a95fe30b0ece9879b Mon Sep 17 00:00:00 2001 From: springrain Date: Mon, 17 Jan 2022 17:05:43 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20=20xchainAKToEVMAddres?= =?UTF-8?q?s=20=E6=96=B9=E6=B3=95,=E5=AE=9E=E7=8E=B0=20AK=20=E8=BD=AC=20EV?= =?UTF-8?q?M=20address?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/baidu/xuper/api/Account.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/baidu/xuper/api/Account.java b/src/main/java/com/baidu/xuper/api/Account.java index 5be26ca..2d706a7 100644 --- a/src/main/java/com/baidu/xuper/api/Account.java +++ b/src/main/java/com/baidu/xuper/api/Account.java @@ -198,16 +198,16 @@ public static String hexEncodeUpperToString(byte[] bytes) { /** * AK 转 EVM Address - * + * @param akAddress * @return * @throws Exception */ - public String xchainAKToEVMAddress() throws Exception { - if (getAKAddress() == null) { + public static String xchainAKToEVMAddress(String akAddress) throws Exception { + if (akAddress == null) { throw new RuntimeException("getAKAddress() is null"); } - byte[] rawAddr = Base58.decode(getAKAddress()); + byte[] rawAddr = Base58.decode(akAddress); if (rawAddr.length < 21) { throw new RuntimeException("bad address"); From 5a0f2e85ede04083981d139144e99457b10f22c4 Mon Sep 17 00:00:00 2001 From: springrain Date: Fri, 25 Mar 2022 23:43:16 +0800 Subject: [PATCH 3/3] =?UTF-8?q?EVM=20Address=20=E8=BD=AC=20AK?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/baidu/xuper/api/Account.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/main/java/com/baidu/xuper/api/Account.java b/src/main/java/com/baidu/xuper/api/Account.java index 2d706a7..4d630d5 100644 --- a/src/main/java/com/baidu/xuper/api/Account.java +++ b/src/main/java/com/baidu/xuper/api/Account.java @@ -217,6 +217,46 @@ public static String xchainAKToEVMAddress(String akAddress) throws Exception { 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 公私钥相关。 */