Search in sources :

Example 1 with RlpList

use of org.web3j.rlp.RlpList in project alpha-wallet-android by AlphaWallet.

the class TransactionRepository method encode.

/**
 * From Web3j to encode a constructor
 * @param rawTransaction
 * @param signatureData
 * @return
 */
private static byte[] encode(RawTransaction rawTransaction, Sign.SignatureData signatureData) {
    List<RlpType> values = TransactionEncoder.asRlpValues(rawTransaction, signatureData);
    RlpList rlpList = new RlpList(values);
    return RlpEncoder.encode(rlpList);
}
Also used : RlpType(org.web3j.rlp.RlpType) RlpList(org.web3j.rlp.RlpList)

Example 2 with RlpList

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

the class PrivateTransactionDecoder method decode.

public static RawPrivateTransaction decode(final String hexTransaction) {
    final byte[] transaction = Numeric.hexStringToByteArray(hexTransaction);
    final RlpList rlpList = RlpDecoder.decode(transaction);
    final RlpList temp = (RlpList) rlpList.getValues().get(0);
    final List<RlpType> values = temp.getValues();
    final RawTransaction rawTransaction = TransactionDecoder.decode(hexTransaction);
    if (values.size() == 9) {
        final Base64String privateFrom = extractBase64(values.get(6));
        final Restriction restriction = extractRestriction(values.get(8));
        if (values.get(7) instanceof RlpList) {
            return new RawPrivateTransaction(rawTransaction, privateFrom, extractBase64List(values.get(7)), restriction);
        } else {
            return new RawPrivateTransaction(rawTransaction, privateFrom, extractBase64(values.get(7)), restriction);
        }
    } else {
        final Base64String privateFrom = extractBase64(values.get(9));
        final Restriction restriction = extractRestriction(values.get(11));
        if (values.get(10) instanceof RlpList) {
            return new SignedRawPrivateTransaction((SignedRawTransaction) rawTransaction, privateFrom, extractBase64List(values.get(10)), restriction);
        } else {
            return new SignedRawPrivateTransaction((SignedRawTransaction) rawTransaction, privateFrom, extractBase64(values.get(10)), restriction);
        }
    }
}
Also used : Restriction(org.web3j.utils.Restriction) RawTransaction(org.web3j.crypto.RawTransaction) SignedRawTransaction(org.web3j.crypto.SignedRawTransaction) RlpList(org.web3j.rlp.RlpList) RlpType(org.web3j.rlp.RlpType) Base64String(org.web3j.utils.Base64String)

Example 3 with RlpList

use of org.web3j.rlp.RlpList in project alpha-wallet-android by AlphaWallet.

the class KeystoreAccountService method encode.

private static byte[] encode(RawTransaction rawTransaction, Sign.SignatureData signatureData) {
    List<RlpType> values = TransactionEncoder.asRlpValues(rawTransaction, signatureData);
    RlpList rlpList = new RlpList(values);
    byte[] encoded = RlpEncoder.encode(rlpList);
    if (!rawTransaction.getType().equals(TransactionType.LEGACY)) {
        return ByteBuffer.allocate(encoded.length + 1).put(rawTransaction.getType().getRlpType()).put(encoded).array();
    }
    return encoded;
}
Also used : RlpType(org.web3j.rlp.RlpType) RlpList(org.web3j.rlp.RlpList)

Example 4 with RlpList

use of org.web3j.rlp.RlpList 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 5 with RlpList

use of org.web3j.rlp.RlpList 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

RlpList (org.web3j.rlp.RlpList)13 RlpType (org.web3j.rlp.RlpType)11 ArrayList (java.util.ArrayList)3 RlpString (org.web3j.rlp.RlpString)3 BigInteger (java.math.BigInteger)2 Sign (org.web3j.crypto.Sign)2 SECPPrivateKey (org.hyperledger.besu.crypto.SECPPrivateKey)1 SECPSignature (org.hyperledger.besu.crypto.SECPSignature)1 Credentials (org.web3j.crypto.Credentials)1 RawTransaction (org.web3j.crypto.RawTransaction)1 SignedRawTransaction (org.web3j.crypto.SignedRawTransaction)1 Base64String (org.web3j.utils.Base64String)1 Restriction (org.web3j.utils.Restriction)1