All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xwc1125.chain5j.crypto.TransactionEncoder Maven / Gradle / Ivy

There is a newer version: 4.3.9
Show newest version
package com.xwc1125.chain5j.crypto;

import java.util.ArrayList;
import java.util.List;

import com.xwc1125.chain5j.rlp.RlpEncoder;
import com.xwc1125.chain5j.rlp.RlpList;
import com.xwc1125.chain5j.rlp.RlpString;
import com.xwc1125.chain5j.rlp.RlpType;
import com.xwc1125.chain5j.utils.Bytes;
import com.xwc1125.chain5j.utils.Numeric;

/**
 * Create RLP encoded transaction, implementation as per p4 of the
 * yellow paper.
 */
public class TransactionEncoder {

    public static byte[] signMessage(RawTransaction rawTransaction, Credentials credentials) {
        byte[] encodedTransaction = encode(rawTransaction);
        Sign.SignatureData signatureData = Sign.signMessage(
                encodedTransaction, credentials.getEcKeyPair());

        return encode(rawTransaction, signatureData);
    }

    public static byte[] signMessage(
            RawTransaction rawTransaction, int chainId, Credentials credentials) {
        byte[] encodedTransaction = encode(rawTransaction, chainId);
        Sign.SignatureData signatureData = Sign.signMessage(
                encodedTransaction, credentials.getEcKeyPair());

        Sign.SignatureData eip155SignatureData = createEip155SignatureData(signatureData, chainId);
        return encode(rawTransaction, eip155SignatureData);
    }

    public static Sign.SignatureData createEip155SignatureData(
            Sign.SignatureData signatureData, int chainId) {
        int v = (signatureData.getV() + (chainId << 1) + 8);

        return new Sign.SignatureData(
                v, signatureData.getR(), signatureData.getS());
    }

    public static byte[] encode(RawTransaction rawTransaction) {
        return encode(rawTransaction, null);
    }

    public static byte[] encode(RawTransaction rawTransaction, int chainId) {
        Sign.SignatureData signatureData = new Sign.SignatureData(
                chainId, new byte[]{}, new byte[]{});
        return encode(rawTransaction, signatureData);
    }

    private static byte[] encode(RawTransaction rawTransaction, Sign.SignatureData signatureData) {
        List values = asRlpValues(rawTransaction, signatureData);
        RlpList rlpList = new RlpList(values);
        return RlpEncoder.encode(rlpList);
    }

    static List asRlpValues(
            RawTransaction rawTransaction, Sign.SignatureData signatureData) {
        List result = new ArrayList<>();

        result.add(RlpString.create(rawTransaction.getNonce()));
        result.add(RlpString.create(rawTransaction.getGasPrice()));
        result.add(RlpString.create(rawTransaction.getGasLimit()));

        // an empty to address (contract creation) should not be encoded as a numeric 0 value
        String to = rawTransaction.getTo();
        if (to != null && to.length() > 0) {
            // addresses that start with zeros should be encoded with the zeros included, not
            // as numeric values
            if (to.startsWith("0x")) {
                result.add(RlpString.create(Numeric.hexStringToByteArray(to)));
            } else {
                result.add(RlpString.create(to.getBytes()));
            }
        } else {
            result.add(RlpString.create(""));
        }

        // token (if it is not,does not add to rlp)
        String token = rawTransaction.getToken();
        if (token != null && token.length() > 0) {
            // addresses that start with zeros should be encoded with the zeros included, not
            // as numeric values
            if (token.startsWith("0x")) {
                result.add(RlpString.create(Numeric.hexStringToByteArray(token)));
            } else {
                result.add(RlpString.create(token.getBytes()));
            }
        } else {
            if (rawTransaction.getHasToken()) {
                result.add(RlpString.create(""));
            }
        }

        result.add(RlpString.create(rawTransaction.getValue()));

        // value field will already be hex encoded, so we need to convert into binary first
        byte[] data = Numeric.hexStringToByteArray(rawTransaction.getData());
        result.add(RlpString.create(data));

        if (signatureData != null) {
            result.add(RlpString.create(signatureData.getV()));
            result.add(RlpString.create(Bytes.trimLeadingZeroes(signatureData.getR())));
            result.add(RlpString.create(Bytes.trimLeadingZeroes(signatureData.getS())));
        }

        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy