Search in sources :

Example 1 with RlpString

use of org.web3j.rlp.RlpString in project web3j by web3j.

the class TransactionDecoder method decodeEIP1559Transaction.

private static RawTransaction decodeEIP1559Transaction(final byte[] transaction) {
    final byte[] encodedTx = Arrays.copyOfRange(transaction, 1, transaction.length);
    final RlpList rlpList = RlpDecoder.decode(encodedTx);
    final RlpList values = (RlpList) rlpList.getValues().get(0);
    final long chainId = ((RlpString) values.getValues().get(0)).asPositiveBigInteger().longValue();
    final BigInteger nonce = ((RlpString) values.getValues().get(1)).asPositiveBigInteger();
    final BigInteger maxPriorityFeePerGas = ((RlpString) values.getValues().get(2)).asPositiveBigInteger();
    final BigInteger maxFeePerGas = ((RlpString) values.getValues().get(3)).asPositiveBigInteger();
    final BigInteger gasLimit = ((RlpString) values.getValues().get(4)).asPositiveBigInteger();
    final String to = ((RlpString) values.getValues().get(5)).asString();
    final BigInteger value = ((RlpString) values.getValues().get(6)).asPositiveBigInteger();
    final String data = ((RlpString) values.getValues().get(7)).asString();
    final RawTransaction rawTransaction = RawTransaction.createTransaction(chainId, nonce, gasLimit, to, value, data, maxPriorityFeePerGas, maxFeePerGas);
    if (values.getValues().size() == UNSIGNED_EIP1559TX_RLP_LIST_SIZE) {
        return rawTransaction;
    } else {
        final byte[] v = Sign.getVFromRecId(Numeric.toBigInt(((RlpString) values.getValues().get(9)).getBytes()).intValue());
        final byte[] r = Numeric.toBytesPadded(Numeric.toBigInt(((RlpString) values.getValues().get(10)).getBytes()), 32);
        final byte[] s = Numeric.toBytesPadded(Numeric.toBigInt(((RlpString) values.getValues().get(11)).getBytes()), 32);
        final Sign.SignatureData signatureData = new Sign.SignatureData(v, r, s);
        return new SignedRawTransaction(rawTransaction.getTransaction(), signatureData);
    }
}
Also used : BigInteger(java.math.BigInteger) RlpString(org.web3j.rlp.RlpString) RlpString(org.web3j.rlp.RlpString) RlpList(org.web3j.rlp.RlpList)

Example 2 with RlpString

use of org.web3j.rlp.RlpString in project web3j by web3j.

the class TransactionDecoder method decodeLegacyTransaction.

private static RawTransaction decodeLegacyTransaction(final byte[] transaction) {
    final RlpList rlpList = RlpDecoder.decode(transaction);
    final RlpList values = (RlpList) rlpList.getValues().get(0);
    final BigInteger nonce = ((RlpString) values.getValues().get(0)).asPositiveBigInteger();
    final BigInteger gasPrice = ((RlpString) values.getValues().get(1)).asPositiveBigInteger();
    final BigInteger gasLimit = ((RlpString) values.getValues().get(2)).asPositiveBigInteger();
    final String to = ((RlpString) values.getValues().get(3)).asString();
    final BigInteger value = ((RlpString) values.getValues().get(4)).asPositiveBigInteger();
    final String data = ((RlpString) values.getValues().get(5)).asString();
    if (values.getValues().size() == 6 || (values.getValues().size() == 8 && ((RlpString) values.getValues().get(7)).getBytes().length == 10) || (values.getValues().size() == 9 && ((RlpString) values.getValues().get(8)).getBytes().length == 10)) {
        // representation of "restricted" for private transactions
        return RawTransaction.createTransaction(nonce, gasPrice, gasLimit, to, value, data);
    } else {
        final byte[] v = ((RlpString) values.getValues().get(6)).getBytes();
        final byte[] r = Numeric.toBytesPadded(Numeric.toBigInt(((RlpString) values.getValues().get(7)).getBytes()), 32);
        final byte[] s = Numeric.toBytesPadded(Numeric.toBigInt(((RlpString) values.getValues().get(8)).getBytes()), 32);
        final Sign.SignatureData signatureData = new Sign.SignatureData(v, r, s);
        return new SignedRawTransaction(nonce, gasPrice, gasLimit, to, value, data, signatureData);
    }
}
Also used : BigInteger(java.math.BigInteger) RlpString(org.web3j.rlp.RlpString) RlpString(org.web3j.rlp.RlpString) RlpList(org.web3j.rlp.RlpList)

Aggregations

BigInteger (java.math.BigInteger)2 RlpList (org.web3j.rlp.RlpList)2 RlpString (org.web3j.rlp.RlpString)2