Java代码对波场交易签名

1.由于时间关系,没有自己编写,而是借用了java-tron的代码
首先maven引用

    com.github.tronprotocol
    java-tron
    Odyssey-v3.2

代码如下

public static String sign(String transactionStr) throws JsonFormat.ParseException {
    JSONObject input = JSONObject.parseObject(transactionStr);
    String strTransaction = input.getJSONObject("transaction").toJSONString();
    Protocol.Transaction transaction = Util.packTransaction(strTransaction);
    JSONObject jsonTransaction = JSONObject.parseObject(JsonFormat.printToString(transaction));
    input.put("transaction", jsonTransaction);
    Protocol.TransactionSign.Builder build = Protocol.TransactionSign.newBuilder();
    JsonFormat.merge(input.toJSONString(), build);
    TransactionCapsule reply = getTransactionSign(build.build());
    return Util.printTransaction(reply.getInstance());
}
public static TransactionCapsule getTransactionSign(Protocol.TransactionSign transactionSign) {
    byte[] privateKey = transactionSign.getPrivateKey().toByteArray();
    TransactionCapsule trx = new TransactionCapsule(transactionSign.getTransaction());
    trx.sign(privateKey);
    return trx;
}

2.Base58和HEX地址转换

public static String tryToHexAddr(String addr){
        if(isValid(addr)){
            addr = addr.replace("0x","");
            String regPattern = "[0-9a-fA-F]{42}";
            if(Pattern.matches(regPattern, addr)) {
                return addr;
            }
            return decodeBase58Address(addr);

        }
        return null;
    }

    public static String tryToBase58Addr(String hexAddr){
        if(isValid(hexAddr)){
//            String base58 = "[123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ]{34}";
            hexAddr = hexAddr.replace("0x","");
            String regPattern = "[0-9a-fA-F]{42}";
            if(Pattern.matches(regPattern, hexAddr)) {
                return encode58Check(ByteArray.fromHexString(hexAddr));
            }
            return hexAddr;
        }
        return null;
    }

    public static void main(String[] args){
        System.out.println(tryToHexAddr("TXGc1WLYQgDaiGgC7RcqvMwfqq7T41WR6L"));
        System.out.println(tryToBase58Addr("TNqsh3tHYszgkw99Jhyw7rQGCDwynrxGDo"));
    }
    public static String decodeBase58Address(String addr){
        if(isValid(addr) && addr.length() >= 4){
            byte[] bytes = decode58Check(addr);
            return ByteUtil.toHexString(bytes);
        }
        return null;
    }

    private static String encode58Check(byte[] input) {
        byte[] hash0 = Sha256Hash.hash(input);
        byte[] hash1 = Sha256Hash.hash(hash0);
        byte[] inputCheck = new byte[input.length + 4];
        System.arraycopy(input, 0, inputCheck, 0, input.length);
        System.arraycopy(hash1, 0, inputCheck, input.length, 4);
        return Base58.encode(inputCheck);
    }

    private static byte[] decode58Check(String input) {
        byte[] decodeCheck = Base58.decode(input);
        if (decodeCheck.length <= 4) {
            return null;
        }
        byte[] decodeData = new byte[decodeCheck.length - 4];
        System.arraycopy(decodeCheck, 0, decodeData, 0, decodeData.length);
        byte[] hash0 = Sha256Hash.hash(decodeData);
        byte[] hash1 = Sha256Hash.hash(hash0);
        if (hash1[0] == decodeCheck[decodeData.length] &&
                hash1[1] == decodeCheck[decodeData.length + 1] &&
                hash1[2] == decodeCheck[decodeData.length + 2] &&
                hash1[3] == decodeCheck[decodeData.length + 3]) {
            return decodeData;
        }
        return null;
    }

你可能感兴趣的:(波场)