org.bcos.web3j.crypto.TransactionEncoder Maven / Gradle / Ivy
package org.bcos.web3j.crypto;
import java.util.ArrayList;
import java.util.List;
import org.bcos.web3j.crypto.sm2.util.encoders.Hex;
import org.bcos.web3j.protocol.core.methods.request.RawTransaction;
import org.bcos.web3j.rlp.RlpEncoder;
import org.bcos.web3j.rlp.RlpList;
import org.bcos.web3j.rlp.RlpString;
import org.bcos.web3j.rlp.RlpType;
import org.bcos.web3j.utils.Bytes;
import org.bcos.web3j.utils.Numeric;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Create RLP encoded transaction, implementation as per p4 of the
* yellow paper.
*/
public class TransactionEncoder {
private static Logger logger = LoggerFactory.getLogger(TransactionEncoder.class);
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, byte 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, byte chainId) {
byte v = (byte) (signatureData.getV() + (chainId << 1) + 8);
return new Sign.SignatureData(
v, signatureData.getR(), signatureData.getS() ,signatureData.getPub());
}
public static byte[] encode(RawTransaction rawTransaction) {
return encode(rawTransaction, null);
}
public static byte[] encode(RawTransaction rawTransaction, byte 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.getRandomid()));
result.add(RlpString.create(rawTransaction.getGasPrice()));
result.add(RlpString.create(rawTransaction.getGasLimit()));
result.add(RlpString.create(rawTransaction.getBlockLimit()));
// 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
result.add(RlpString.create(Numeric.hexStringToByteArray(to)));
} else {
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));
String contractName = rawTransaction.getContractName();
if (contractName != null && contractName.length() > 0) {
result.add(RlpString.create(rawTransaction.getContractName()));
result.add(RlpString.create(rawTransaction.getVersion()));
result.add(RlpString.create(rawTransaction.getType()));
}
if (signatureData != null) {
if (EncryptType.encryptType == 1) {
result.add(RlpString.create(Bytes.trimLeadingZeroes(signatureData.getPub())));
//logger.debug("RLP-Pub:{},RLP-PubLen:{}",Hex.toHexString(signatureData.getPub()),signatureData.getPub().length);
result.add(RlpString.create(Bytes.trimLeadingZeroes(signatureData.getR())));
//logger.debug("RLP-R:{},RLP-RLen:{}",Hex.toHexString(signatureData.getR()),signatureData.getR().length);
result.add(RlpString.create(Bytes.trimLeadingZeroes(signatureData.getS())));
//logger.debug("RLP-S:{},RLP-SLen:{}",Hex.toHexString(signatureData.getS()),signatureData.getS().length);
}
else{
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 - 2025 Weber Informatics LLC | Privacy Policy