org.web3j.utils.Bytes Java Examples

The following examples show how to use org.web3j.utils.Bytes. 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: 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 #2
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 #3
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 #4
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 #5
Source File: TokenscriptFunction.java    From alpha-wallet-android with MIT License 4 votes vote down vote up
private String handleTransactionResult(TransactionResult result, Function function, String responseValue, Attribute attr, long lastTransactionTime)
{
    String transResult = null;
    try
    {
        //try to interpret the value. For now, just use the raw return value - this is more reliable until we need to interpret arrays
        List<Type> response = FunctionReturnDecoder.decode(responseValue, function.getOutputParameters());
        if (response.size() > 0)
        {
            result.resultTime = lastTransactionTime;
            Type val = response.get(0);

            BigInteger value;
            byte[] bytes = Bytes.trimLeadingZeroes(Numeric.hexStringToByteArray(responseValue));
            String hexBytes = Numeric.toHexString(bytes);

            switch (attr.syntax)
            {
                case Boolean:
                    value = Numeric.toBigInt(hexBytes);
                    transResult = value.equals(BigDecimal.ZERO) ? "FALSE" : "TRUE";
                    break;
                case Integer:
                    value = Numeric.toBigInt(hexBytes);
                    transResult = value.toString();
                    break;
                case BitString:
                case NumericString:
                    if (val.getTypeAsString().equals("string"))
                    {
                        transResult = (String)val.getValue();
                        if (responseValue.length() > 2 && transResult.length() == 0)
                        {
                            transResult = checkBytesString(responseValue);
                        }
                    }
                    else
                    {
                        //should be a decimal string
                        value = Numeric.toBigInt(hexBytes);
                        transResult = value.toString();
                    }
                    break;
                case IA5String:
                case DirectoryString:
                case GeneralizedTime:
                case CountryString:
                    if (val.getTypeAsString().equals("string"))
                    {
                        transResult = (String)val.getValue();
                        if (responseValue.length() > 2 && transResult.length() == 0)
                        {
                            transResult = checkBytesString(responseValue);
                        }
                    }
                    else if (val.getTypeAsString().equals("address"))
                    {
                        transResult = (String)val.getValue();
                    }
                    else
                    {
                        transResult = hexBytes;
                    }
                    break;
                default:
                    transResult = hexBytes;
                    break;
            }
        }
        else
        {
            result.resultTime = lastTransactionTime == -1 ? -1 : 0;
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return transResult;
}
 
Example #6
Source File: TokenscriptFunction.java    From alpha-wallet-android with MIT License 4 votes vote down vote up
public String convertInputValue(Attribute attr, String valueFromInput)
{
    String convertedValue = "";
    try
    {
        byte[] inputBytes;
        switch (attr.as)
        {
            //UTF8, Unsigned, Signed, Mapping, Boolean, UnsignedInput, TokenId
            case Unsigned:
            case Signed:
            case UnsignedInput:
                inputBytes = TokenscriptFunction.convertArgToBytes(Utils.isolateNumeric(valueFromInput)); //convert cleaned user input
                BigInteger unsignedValue = new BigInteger(inputBytes);
                convertedValue = unsignedValue.toString();
                break;
            case UTF8:
                convertedValue = valueFromInput;
                break;
            case Bytes:
                //apply bitmask to user entry and shift it because bytes is the other way round
                inputBytes = TokenscriptFunction.convertArgToBytes(valueFromInput);
                if (inputBytes.length <= 32)
                {
                    BigInteger val = new BigInteger(1, inputBytes).and(attr.bitmask).shiftRight(attr.bitshift);
                    convertedValue = val.toString(16);
                }
                else
                {
                    convertedValue = com.alphawallet.token.tools.Numeric.toHexString(inputBytes);
                }
                break;
            case e18:
                convertedValue = BalanceUtils.EthToWei(valueFromInput);
                break;
            case e8:
                convertedValue = BalanceUtils.UnitToEMultiplier(valueFromInput, new BigDecimal("100000000"));
                break;
            case e6:
                convertedValue = BalanceUtils.UnitToEMultiplier(valueFromInput, new BigDecimal("1000000"));
                break;
            case e4:
                convertedValue = BalanceUtils.UnitToEMultiplier(valueFromInput, new BigDecimal("1000"));
                break;
            case e2:
                convertedValue = BalanceUtils.UnitToEMultiplier(valueFromInput, new BigDecimal("100"));
                break;
            case Mapping:
                //makes no sense as input
                convertedValue = TOKENSCRIPT_CONVERSION_ERROR + "Mapping in user input params: " + attr.name;
                break;
            case Address:
                convertedValue = valueFromInput;
                break;
            case Boolean:
                //attempt to decode
                if (valueFromInput.equalsIgnoreCase("true") || valueFromInput.equals("1"))
                {
                    convertedValue = "TRUE";
                }
                else
                {
                    convertedValue = "FALSE";
                }
                break;
            case TokenId:
                //Shouldn't get here - tokenId should have been handled before.
                convertedValue = TOKENSCRIPT_CONVERSION_ERROR + "Token ID in user input params: " + attr.name;
                break;
            default:
                convertedValue = valueFromInput;
                break;
        }
    }
    catch (Exception excp)
    {
        excp.printStackTrace();
        convertedValue = TOKENSCRIPT_CONVERSION_ERROR + excp.getMessage();
    }

    return convertedValue;
}
 
Example #7
Source File: TokenscriptFunction.java    From alpha-wallet-android with MIT License 4 votes vote down vote up
private String handleTransactionResult(TransactionResult result, Function function, String responseValue, Attribute attr, long lastTransactionTime)
{
    String transResult = null;
    try
    {
        //try to interpret the value. For now, just use the raw return value - this is more reliable until we need to interpret arrays
        List<Type> response = FunctionReturnDecoder.decode(responseValue, function.getOutputParameters());
        if (response.size() > 0)
        {
            result.resultTime = lastTransactionTime;
            Type val = response.get(0);

            BigInteger value;
            byte[] bytes = Bytes.trimLeadingZeroes(Numeric.hexStringToByteArray(responseValue));
            String hexBytes = Numeric.toHexString(bytes);

            switch (attr.syntax)
            {
                case Boolean:
                    value = Numeric.toBigInt(hexBytes);
                    transResult = value.equals(BigDecimal.ZERO) ? "FALSE" : "TRUE";
                    break;
                case Integer:
                    value = Numeric.toBigInt(hexBytes);
                    transResult = value.toString();
                    break;
                case BitString:
                case NumericString:
                    if (val.getTypeAsString().equals("string"))
                    {
                        transResult = (String)val.getValue();
                        if (responseValue.length() > 2 && transResult.length() == 0)
                        {
                            transResult = checkBytesString(responseValue);
                        }
                    }
                    else
                    {
                        //should be a decimal string
                        value = Numeric.toBigInt(hexBytes);
                        transResult = value.toString();
                    }
                    break;
                case IA5String:
                case DirectoryString:
                case GeneralizedTime:
                case CountryString:
                    if (val.getTypeAsString().equals("string"))
                    {
                        transResult = (String)val.getValue();
                        if (responseValue.length() > 2 && transResult.length() == 0)
                        {
                            transResult = checkBytesString(responseValue);
                        }
                    }
                    else if (val.getTypeAsString().equals("address"))
                    {
                        transResult = (String)val.getValue();
                    }
                    else
                    {
                        transResult = hexBytes;
                    }
                    break;
                default:
                    transResult = hexBytes;
                    break;
            }
        }
        else
        {
            result.resultTime = lastTransactionTime == -1 ? -1 : 0;
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return transResult;
}
 
Example #8
Source File: TokenscriptFunction.java    From alpha-wallet-android with MIT License 4 votes vote down vote up
private String handleTransactionResult(TransactionResult result, Function function, String responseValue, Attribute attr, long lastTransactionTime)
{
    String transResult = null;
    try
    {
        //try to interpret the value. For now, just use the raw return value - this is more reliable until we need to interpret arrays
        List<Type> response = FunctionReturnDecoder.decode(responseValue, function.getOutputParameters());
        if (response.size() > 0)
        {
            result.resultTime = lastTransactionTime;
            Type val = response.get(0);

            BigInteger value;
            byte[] bytes = Bytes.trimLeadingZeroes(Numeric.hexStringToByteArray(responseValue));
            String hexBytes = Numeric.toHexString(bytes);

            switch (attr.syntax)
            {
                case Boolean:
                    value = Numeric.toBigInt(hexBytes);
                    transResult = value.equals(BigDecimal.ZERO) ? "FALSE" : "TRUE";
                    break;
                case Integer:
                    value = Numeric.toBigInt(hexBytes);
                    transResult = value.toString();
                    break;
                case BitString:
                case NumericString:
                    if (val.getTypeAsString().equals("string"))
                    {
                        transResult = (String)val.getValue();
                        if (responseValue.length() > 2 && transResult.length() == 0)
                        {
                            transResult = checkBytesString(responseValue);
                        }
                    }
                    else
                    {
                        //should be a decimal string
                        value = Numeric.toBigInt(hexBytes);
                        transResult = value.toString();
                    }
                    break;
                case IA5String:
                case DirectoryString:
                case GeneralizedTime:
                case CountryString:
                    if (val.getTypeAsString().equals("string"))
                    {
                        transResult = (String)val.getValue();
                        if (responseValue.length() > 2 && transResult.length() == 0)
                        {
                            transResult = checkBytesString(responseValue);
                        }
                    }
                    else if (val.getTypeAsString().equals("address"))
                    {
                        transResult = (String)val.getValue();
                    }
                    else
                    {
                        transResult = hexBytes;
                    }
                    break;
                default:
                    transResult = hexBytes;
                    break;
            }
        }
        else
        {
            result.resultTime = lastTransactionTime == -1 ? -1 : 0;
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return transResult;
}