xuperchain / xuper-java-sdk

The java sdk of xuperunion https://github.com/xuperchain/xuperunion
Apache License 2.0
27 stars 22 forks source link

调用 invokeEVMContract 时候,argss 需要传递 evmaddress ,没找到对应的 XchainToEVMAddress 方法 #36

Open LKCoinOne opened 2 years ago

LKCoinOne commented 2 years ago

调用 invokeEVMContract 时候,argss 需要传递 evmaddress ,没找到对应的 XchainToEVMAddress 方法

superlitian commented 2 years ago

Java sdk 目前还没有地址转换功能,可以使用go sdk 或 xchain-cli 进行转换,java sdk 的地址转换在不久后会上线

LKCoinOne commented 2 years ago

目前解决方法,从sdk分支中找到(搬砖)

/**
 * AKAddress 转 EVMAddress
 *
 * @return
 * @throws Exception
 */
public static String getAkToEVMAddress(String akAddress) {
    if (akAddress == null) {
        throw new RuntimeException("getAKAddress() is null");
    }
    byte[] rawAddr = new byte[0];
    try {
        rawAddr = Base58.decode(akAddress);
    } catch (Exception e) {
        throw new RuntimeException("getAKAddress() Base58 Error");
    }

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

    byte[] ripemd160Hash = Arrays.copyOfRange(rawAddr, 1, 21);
    StringBuilder result = new StringBuilder();
    for (int index = 0, len = ripemd160Hash.length; index <= len - 1; index += 1) {
        String invalue1 = Integer.toHexString((ripemd160Hash[index] >> 4) & 0xF);
        String intValue2 = Integer.toHexString(ripemd160Hash[index] & 0xF);
        result.append(invalue1);
        result.append(intValue2);
    }
    return result.toString().toUpperCase(Locale.ROOT);
}

/**
 * 使用方法
 */
public static void main(String[] args) {
   Account account = Account.create(1,2);
// getAkToEVMAddress(account.getAddress());
   getAkToEVMAddress(account.getAKAddress());
}