Search in sources :

Example 1 with RlpType

use of org.fisco.bcos.web3j.rlp.RlpType in project web3sdk by FISCO-BCOS.

the class ContractUtils method generateContractAddress.

/**
 * Generate a smart contract address. This enables you to identify what address a smart contract
 * will be deployed to on the network.
 *
 * @param address of sender
 * @param nonce of transaction
 * @return the generated smart contract address
 */
public static byte[] generateContractAddress(byte[] address, BigInteger nonce) {
    List<RlpType> values = new ArrayList<>();
    values.add(RlpString.create(address));
    values.add(RlpString.create(nonce));
    RlpList rlpList = new RlpList(values);
    byte[] encoded = RlpEncoder.encode(rlpList);
    byte[] hashed = Hash.sha3(encoded);
    return Arrays.copyOfRange(hashed, 12, hashed.length);
}
Also used : ArrayList(java.util.ArrayList) RlpType(org.fisco.bcos.web3j.rlp.RlpType) RlpList(org.fisco.bcos.web3j.rlp.RlpList)

Example 2 with RlpType

use of org.fisco.bcos.web3j.rlp.RlpType in project web3sdk by FISCO-BCOS.

the class TransactionEncoder method asRlpValues.

static List<RlpType> asRlpValues(RawTransaction rawTransaction, Sign.SignatureData signatureData) {
    List<RlpType> 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));
    if (signatureData != null) {
        if (EncryptType.encryptType == 1) {
            result.add(RlpString.create(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;
}
Also used : ArrayList(java.util.ArrayList) RlpString(org.fisco.bcos.web3j.rlp.RlpString) RlpType(org.fisco.bcos.web3j.rlp.RlpType)

Example 3 with RlpType

use of org.fisco.bcos.web3j.rlp.RlpType in project web3sdk by FISCO-BCOS.

the class TransactionEncoder method encode.

public static byte[] encode(RawTransaction rawTransaction, Sign.SignatureData signatureData) {
    List<RlpType> values = asRlpValues(rawTransaction, signatureData);
    RlpList rlpList = new RlpList(values);
    return RlpEncoder.encode(rlpList);
}
Also used : RlpType(org.fisco.bcos.web3j.rlp.RlpType) RlpList(org.fisco.bcos.web3j.rlp.RlpList)

Example 4 with RlpType

use of org.fisco.bcos.web3j.rlp.RlpType in project web3sdk by FISCO-BCOS.

the class ExtendedTransactionEncoder method encode.

public static byte[] encode(ExtendedRawTransaction rawTransaction, Sign.SignatureData signatureData) {
    List<RlpType> values = asRlpValues(rawTransaction, signatureData);
    RlpList rlpList = new RlpList(values);
    return RlpEncoder.encode(rlpList);
}
Also used : RlpType(org.fisco.bcos.web3j.rlp.RlpType) RlpList(org.fisco.bcos.web3j.rlp.RlpList)

Example 5 with RlpType

use of org.fisco.bcos.web3j.rlp.RlpType in project web3sdk by FISCO-BCOS.

the class ExtendedTransactionEncoder method asRlpValues.

static List<RlpType> asRlpValues(ExtendedRawTransaction rawTransaction, Sign.SignatureData signatureData) {
    List<RlpType> 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));
    // add extra data!!!
    result.add(RlpString.create(rawTransaction.getFiscoChainId()));
    result.add(RlpString.create(rawTransaction.getGroupId()));
    if (rawTransaction.getExtraData() == null) {
        result.add(RlpString.create(""));
    } else {
        result.add(RlpString.create(Numeric.hexStringToByteArray(rawTransaction.getExtraData())));
    }
    if (signatureData != null) {
        if (EncryptType.encryptType == 1) {
            // Note: shouldn't trimLeadingZeroes here for the Pub must be with the length of 64
            // Bytes
            result.add(RlpString.create(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;
}
Also used : ArrayList(java.util.ArrayList) RlpString(org.fisco.bcos.web3j.rlp.RlpString) RlpType(org.fisco.bcos.web3j.rlp.RlpType)

Aggregations

RlpType (org.fisco.bcos.web3j.rlp.RlpType)7 RlpList (org.fisco.bcos.web3j.rlp.RlpList)5 ArrayList (java.util.ArrayList)4 RlpString (org.fisco.bcos.web3j.rlp.RlpString)3 Log (org.fisco.bcos.web3j.protocol.core.methods.response.Log)1