org.web3j.rlp.RlpType Java Examples

The following examples show how to use org.web3j.rlp.RlpType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PrivateTransactionEncoder.java    From web3j with Apache License 2.0 6 votes vote down vote up
public static List<RlpType> asRlpValues(
        final RawPrivateTransaction privateTransaction,
        final Sign.SignatureData signatureData) {

    final List<RlpType> result =
            new ArrayList<>(
                    TransactionEncoder.asRlpValues(
                            privateTransaction.asRawTransaction(), signatureData));

    result.add(privateTransaction.getPrivateFrom().asRlp());

    privateTransaction
            .getPrivateFor()
            .ifPresent(privateFor -> result.add(Base64String.unwrapListToRlp(privateFor)));

    privateTransaction.getPrivacyGroupId().map(Base64String::asRlp).ifPresent(result::add);

    result.add(RlpString.create(privateTransaction.getRestriction().getRestriction()));

    return result;
}
 
Example #2
Source File: DelegateContract.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
/**
 *  获得解除委托时所提取的委托收益(当减持/撤销委托成功时调用)
 *
 * @param transactionReceipt
 * @return
 * @throws TransactionException
 */
public BigInteger decodeUnDelegateLog(TransactionReceipt transactionReceipt) throws TransactionException {
    List<Log> logs = transactionReceipt.getLogs();
    if(logs==null||logs.isEmpty()){
        throw new TransactionException("TransactionReceipt logs is empty");
    }

    String logData = logs.get(0).getData();
    if(null == logData || "".equals(logData) ){
        throw new TransactionException("TransactionReceipt logs[0].data is empty");
    }

    RlpList rlp = RlpDecoder.decode(Numeric.hexStringToByteArray(logData));
    List<RlpType> rlpList = ((RlpList)(rlp.getValues().get(0))).getValues();
    String decodedStatus = new String(((RlpString)rlpList.get(0)).getBytes());
    int statusCode = Integer.parseInt(decodedStatus);

    if(statusCode != ErrorCode.SUCCESS){
        throw new TransactionException("TransactionResponse code is 0");
    }

    return  ((RlpString)((RlpList)RlpDecoder.decode(((RlpString)rlpList.get(1)).getBytes())).getValues().get(0)).asPositiveBigInteger();
}
 
Example #3
Source File: BaseContract.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
private TransactionResponse getResponseFromTransactionReceipt(TransactionReceipt transactionReceipt) throws TransactionException {
	List<Log> logs = transactionReceipt.getLogs();
       if(logs==null||logs.isEmpty()){
       	 throw new TransactionException("TransactionReceipt logs is empty");
       }

	String logData = logs.get(0).getData();
	if(null == logData || "".equals(logData) ){
		throw new TransactionException("TransactionReceipt log data is empty");
	}

       RlpList rlp = RlpDecoder.decode(Numeric.hexStringToByteArray(logData));
       List<RlpType> rlpList = ((RlpList)(rlp.getValues().get(0))).getValues();
       String decodedStatus = new String(((RlpString)rlpList.get(0)).getBytes());
       int statusCode = Integer.parseInt(decodedStatus);
       
       TransactionResponse transactionResponse = new TransactionResponse();
       transactionResponse.setCode(statusCode);
       transactionResponse.setTransactionReceipt(transactionReceipt);
       
       return transactionResponse;
}
 
Example #4
Source File: TransferTest.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
public String sendTransaction(String privateKey, String toAddress, BigDecimal amount, long gasPrice, long gasLimit) {

        BigInteger GAS_PRICE = BigInteger.valueOf(gasPrice);
        BigInteger GAS_LIMIT = BigInteger.valueOf(gasLimit);

        Credentials credentials = Credentials.create(privateKey);

        try {

            List<RlpType> result = new ArrayList<>();
            result.add(RlpString.create(Numeric.hexStringToByteArray(PlatOnTypeEncoder.encode(new Int64(0)))));
            String txType = Hex.toHexString(RlpEncoder.encode(new RlpList(result)));

            RawTransaction rawTransaction = RawTransaction.createTransaction(getNonce(), GAS_PRICE, GAS_LIMIT, toAddress, amount.toBigInteger(),
                    txType);

            byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, new Byte("100"), credentials);
            String hexValue = Numeric.toHexString(signedMessage);

            PlatonSendTransaction transaction = web3j.platonSendRawTransaction(hexValue).send();

            return transaction.getTransactionHash();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
Example #5
Source File: TransactionEncoderTest.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testContractAsRlpValues() {
    List<RlpType> rlpStrings =
            TransactionEncoder.asRlpValues(createContractTransaction(), null);
    assertEquals(rlpStrings.size(), (6));
    assertEquals(rlpStrings.get(3), (RlpString.create("")));
}
 
Example #6
Source File: TransactionEncoderTest.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testEtherTransactionAsRlpValues() {
    List<RlpType> rlpStrings =
            TransactionEncoder.asRlpValues(
                    createEtherTransaction(),
                    new Sign.SignatureData((byte) 0, new byte[32], new byte[32]));
    assertEquals(rlpStrings.size(), (9));
    assertEquals(rlpStrings.get(3), (RlpString.create(new BigInteger("add5355", 16))));
}
 
Example #7
Source File: ContractUtils.java    From web3j with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #8
Source File: TransactionEncoder.java    From web3j with Apache License 2.0 5 votes vote down vote up
public static List<RlpType> asRlpValues(
        RawTransaction rawTransaction, Sign.SignatureData signatureData) {
    List<RlpType> 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
        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 gas premium and fee cap if this is an EIP-1559 transaction
    if (rawTransaction.isEIP1559Transaction()) {
        result.add(RlpString.create(rawTransaction.getGasPremium()));
        result.add(RlpString.create(rawTransaction.getFeeCap()));
    }

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

    return result;
}
 
Example #9
Source File: PrivacyGroupUtils.java    From web3j with Apache License 2.0 5 votes vote down vote up
public static Base64String generateLegacyGroup(
        final Base64String privateFrom, final List<Base64String> privateFor) {
    final List<byte[]> stringList = new ArrayList<>();
    stringList.add(Base64.getDecoder().decode(privateFrom.toString()));
    privateFor.forEach(item -> stringList.add(item.raw()));

    final List<RlpType> rlpList =
            stringList.stream()
                    .distinct()
                    .sorted(Comparator.comparing(Arrays::hashCode))
                    .map(RlpString::create)
                    .collect(Collectors.toList());

    return Base64String.wrap(Hash.sha3(RlpEncoder.encode(new RlpList(rlpList))));
}
 
Example #10
Source File: TransactionEncoderTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testContractAsRlpValues() {
    List<RlpType> rlpStrings = TransactionEncoder.asRlpValues(
            createContractTransaction(), null);
    assertThat(rlpStrings.size(), is(6));
    assertThat(rlpStrings.get(3), is(RlpString.create("")));
}
 
Example #11
Source File: TransactionEncoderTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testEtherTransactionAsRlpValues() {
    List<RlpType> rlpStrings = TransactionEncoder.asRlpValues(createEtherTransaction(),
            new Sign.SignatureData((byte) 0, new byte[32], new byte[32]));
    assertThat(rlpStrings.size(), is(9));
    assertThat(rlpStrings.get(3), equalTo(RlpString.create(new BigInteger("add5355", 16))));
}
 
Example #12
Source File: ContractUtils.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
/**
 *  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);
}
 
Example #13
Source File: TransactionEncoder.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
static List<RlpType> asRlpValues(
        RawTransaction rawTransaction, Sign.SignatureData signatureData) {
    List<RlpType> 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
        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) {
        result.add(RlpString.create(signatureData.getV()));
        result.add(RlpString.create(Bytes.trimLeadingZeroes(signatureData.getR())));
        result.add(RlpString.create(Bytes.trimLeadingZeroes(signatureData.getS())));
    }

    return result;
}
 
Example #14
Source File: PrivateTransaction.java    From ethsigner with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] rlpEncode(final SignatureData signatureData) {
  final RawPrivateTransaction rawTransaction = createTransaction();
  final List<RlpType> values =
      PrivateTransactionEncoder.asRlpValues(rawTransaction, signatureData);
  final RlpList rlpList = new RlpList(values);
  return RlpEncoder.encode(rlpList);
}
 
Example #15
Source File: RewardContract.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
/**
 *  获得提取的明细(当提取账户当前所有的可提取的委托奖励成功时调用)
 *
 * @param transactionReceipt
 * @return
 * @throws TransactionException
 */
public List<Reward> decodeWithdrawDelegateRewardLog(TransactionReceipt transactionReceipt) throws TransactionException {
    List<Log> logs = transactionReceipt.getLogs();
    if(logs==null||logs.isEmpty()){
        throw new TransactionException("TransactionReceipt logs is empty");
    }

    String logData = logs.get(0).getData();
    if(null == logData || "".equals(logData) ){
        throw new TransactionException("TransactionReceipt logs[0].data is empty");
    }

    RlpList rlp = RlpDecoder.decode(Numeric.hexStringToByteArray(logData));
    List<RlpType> rlpList = ((RlpList)(rlp.getValues().get(0))).getValues();
    String decodedStatus = new String(((RlpString)rlpList.get(0)).getBytes());
    int statusCode = Integer.parseInt(decodedStatus);

    if(statusCode != ErrorCode.SUCCESS){
        throw new TransactionException("TransactionResponse code is 0");
    }

    List<Reward> rewards = new ArrayList<>();
    ((RlpList)((RlpList)RlpDecoder.decode(((RlpString)rlpList.get(1)).getBytes())).getValues().get(0)).getValues()
            .stream()
            .forEach(rl -> {
                RlpList rlpL = (RlpList)rl;
                Reward reward = new Reward();
                reward.setNodeId(((RlpString)rlpL.getValues().get(0)).asString());
                reward.setStakingNum(((RlpString)rlpL.getValues().get(1)).asPositiveBigInteger());
                reward.setRewardBigIntegerValue((((RlpString)rlpL.getValues().get(2)).asPositiveBigInteger()));
                rewards.add(reward);
            });

    return  rewards;
}
 
Example #16
Source File: EthTransaction.java    From ethsigner with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] rlpEncode(final SignatureData signatureData) {
  final RawTransaction rawTransaction = createTransaction();
  final List<RlpType> values = TransactionEncoder.asRlpValues(rawTransaction, signatureData);
  final RlpList rlpList = new RlpList(values);
  return RlpEncoder.encode(rlpList);
}
 
Example #17
Source File: CustomStaticArray.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
public RlpType getRlpEncodeData() {
    if (list != null ) {
        List<RlpType> rlpListList = new ArrayList<>();
        for (T t : list) {
            rlpListList.add(t.getRlpEncodeData());
        }
        return RlpString.create(RlpEncoder.encode(new RlpList(rlpListList)));
    }
    throw new RuntimeException("unsupported types");
}
 
Example #18
Source File: EncoderUtils.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
public static String functionEncoder(Function function){
    List<RlpType> result = new ArrayList<>();

    result.add(RlpString.create(RlpEncoder.encode(RlpString.create(function.getType()))));

    List<Type> parameters = function.getInputParameters();

    if (parameters != null && parameters.size() > 0) {

        for (Type parameter : parameters) {
            if (parameter instanceof IntType) {
                result.add(RlpString.create(RlpEncoder.encode(RlpString.create(((IntType) parameter).getValue()))));
            } else if (parameter instanceof BytesType) {
                result.add(RlpString.create(RlpEncoder.encode(RlpString.create(((BytesType) parameter).getValue()))));
            } else if (parameter instanceof Utf8String) {
                result.add(RlpString.create(RlpEncoder.encode(RlpString.create(((Utf8String) parameter).getValue()))));
            } else if (parameter instanceof CustomStaticArray) {
                result.add(((CustomStaticArray) parameter).getRlpEncodeData());
            } else if (parameter instanceof CustomType) {
                result.add(((CustomType) parameter).getRlpEncodeData());
            }
        }
    }

    String data = Hex.toHexString(RlpEncoder.encode(new RlpList(result)));
    return data;
}
 
Example #19
Source File: CreateTransactionInteract.java    From Upchain-wallet with GNU Affero General Public License v3.0 5 votes vote down vote up
public Single<byte[]> getSignature(ETHWallet wallet, byte[] message, String password) {
    return  Single.fromCallable(() -> {
        Credentials credentials = WalletUtils.loadCredentials(password, wallet.getKeystorePath());
        Sign.SignatureData signatureData = Sign.signMessage(
                message, credentials.getEcKeyPair());

        List<RlpType> result = new ArrayList<>();
        result.add(RlpString.create(message));

        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())));
        }

        RlpList rlpList = new RlpList(result);
        return RlpEncoder.encode(rlpList);
    });
}
 
Example #20
Source File: NodeIdTool.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
static List<RlpType> asRlpValues(PlatonBlock.Block block) {
    List<RlpType> result = new ArrayList<>();
    //ParentHash  common.Hash    `json:"parentHash"       gencodec:"required"`
    result.add(RlpString.create(decodeHash(block.getParentHash())));
    //Coinbase    common.Address `json:"miner"            gencodec:"required"`
    result.add(RlpString.create(decodeHash(block.getMiner())));
    //Root        common.Hash    `json:"stateRoot"        gencodec:"required"`
    result.add(RlpString.create(decodeHash(block.getStateRoot())));
    //TxHash      common.Hash    `json:"transactionsRoot" gencodec:"required"`
    result.add(RlpString.create(decodeHash(block.getTransactionsRoot())));
    //ReceiptHash common.Hash    `json:"receiptsRoot"     gencodec:"required"`
    result.add(RlpString.create(decodeHash(block.getReceiptsRoot())));
    //Bloom       Bloom          `json:"logsBloom"        gencodec:"required"`
    result.add(RlpString.create(decodeHash(block.getLogsBloom())));
    //Number      *big.Int       `json:"number"           gencodec:"required"`
    result.add(RlpString.create(block.getNumber()));
    //GasLimit    uint64         `json:"gasLimit"         gencodec:"required"`
    result.add(RlpString.create(block.getGasLimit()));
    //GasUsed     uint64         `json:"gasUsed"          gencodec:"required"`
    result.add(RlpString.create(block.getGasUsed()));
    //Time        *big.Int       `json:"timestamp"        gencodec:"required"`
    result.add(RlpString.create(block.getTimestamp()));
    //Extra       []byte         `json:"extraData"        gencodec:"required"`
    result.add(RlpString.create(decodeHash(block.getExtraData().substring(0, 66))));
    //Nonce       BlockNonce     `json:"nonce"`
    result.add(RlpString.create(decodeHash(block.getNonceRaw())));
    return result;
}
 
Example #21
Source File: TransactionEncoder.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
static List<RlpType> asRlpValues(
        RawTransaction rawTransaction, Sign.SignatureData signatureData) {
    List<RlpType> 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
        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) {
        result.add(RlpString.create(Bytes.trimLeadingZeroes(signatureData.getV())));
        result.add(RlpString.create(Bytes.trimLeadingZeroes(signatureData.getR())));
        result.add(RlpString.create(Bytes.trimLeadingZeroes(signatureData.getS())));
    }

    return result;
}
 
Example #22
Source File: TransactionEncoderTest.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testContractAsRlpValues() {
    List<RlpType> rlpStrings = TransactionEncoder.asRlpValues(
            createContractTransaction(), null);
    assertThat(rlpStrings.size(), is(6));
    assertThat(rlpStrings.get(3), is(RlpString.create("")));
}
 
Example #23
Source File: ContractUtils.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
/**
 *  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);
}
 
Example #24
Source File: TransactionEncoderTest.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testEtherTransactionAsRlpValues() {
    List<RlpType> rlpStrings = TransactionEncoder.asRlpValues(createEtherTransaction(),
            new Sign.SignatureData((byte) 0, new byte[32], new byte[32]));
    assertThat(rlpStrings.size(), is(9));
    assertThat(rlpStrings.get(3), equalTo(RlpString.create(new BigInteger("add5355", 16))));
}
 
Example #25
Source File: PrivateTransactionDecoder.java    From web3j with Apache License 2.0 4 votes vote down vote up
private static Restriction extractRestriction(final RlpType value) {
    return Restriction.fromString(new String(((RlpString) value).getBytes(), UTF_8));
}
 
Example #26
Source File: TransactionEncoder.java    From client-sdk-java with Apache License 2.0 4 votes vote down vote up
private static byte[] encode(RawTransaction rawTransaction, Sign.SignatureData signatureData) {
    List<RlpType> values = asRlpValues(rawTransaction, signatureData);
    RlpList rlpList = new RlpList(values);
    return RlpEncoder.encode(rlpList);
}
 
Example #27
Source File: TransactionEncoder.java    From web3j with Apache License 2.0 4 votes vote down vote up
private static byte[] encode(RawTransaction rawTransaction, Sign.SignatureData signatureData) {
    List<RlpType> values = asRlpValues(rawTransaction, signatureData);
    RlpList rlpList = new RlpList(values);
    return RlpEncoder.encode(rlpList);
}
 
Example #28
Source File: PrivateTransactionEncoder.java    From web3j with Apache License 2.0 4 votes vote down vote up
private static byte[] encode(
        final RawPrivateTransaction rawTransaction, final Sign.SignatureData signatureData) {
    final List<RlpType> values = asRlpValues(rawTransaction, signatureData);
    final RlpList rlpList = new RlpList(values);
    return RlpEncoder.encode(rlpList);
}
 
Example #29
Source File: PrivateTransactionDecoder.java    From web3j with Apache License 2.0 4 votes vote down vote up
private static List<Base64String> extractBase64List(final RlpType values) {
    return ((RlpList) values)
            .getValues().stream()
                    .map(PrivateTransactionDecoder::extractBase64)
                    .collect(Collectors.toList());
}
 
Example #30
Source File: PrivateTransactionDecoder.java    From web3j with Apache License 2.0 4 votes vote down vote up
private static Base64String extractBase64(final RlpType value) {
    return Base64String.wrap(((RlpString) value).getBytes());
}