Java Code Examples for org.web3j.utils.Numeric#toBigInt()

The following examples show how to use org.web3j.utils.Numeric#toBigInt() . 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: TypeDecoder.java    From web3j with Apache License 2.0 6 votes vote down vote up
static BigInteger asBigInteger(Object arg) {
    if (arg instanceof BigInteger) {
        return (BigInteger) arg;
    } else if (arg instanceof BigDecimal) {
        return ((BigDecimal) arg).toBigInteger();
    } else if (arg instanceof String) {
        return Numeric.toBigInt((String) arg);
    } else if (arg instanceof byte[]) {
        return Numeric.toBigInt((byte[]) arg);
    } else if (arg instanceof Double
            || arg instanceof Float
            || arg instanceof java.lang.Double
            || arg instanceof java.lang.Float) {
        return BigDecimal.valueOf(((Number) arg).doubleValue()).toBigInteger();
    } else if (arg instanceof Number) {
        return BigInteger.valueOf(((Number) arg).longValue());
    }
    return null;
}
 
Example 2
Source File: Token.java    From alpha-wallet-android with MIT License 6 votes vote down vote up
/**
 * Convert a CSV string of Hex values into a BigInteger List
 * @param integerString CSV string of hex ticket id's
 * @return
 */
public List<BigInteger> stringHexToBigIntegerList(String integerString)
{
    List<BigInteger> idList = new ArrayList<>();

    try
    {
        String[] ids = integerString.split(",");

        for (String id : ids)
        {
            //remove whitespace
            String trim = id.trim();
            BigInteger val = Numeric.toBigInt(trim);
            idList.add(val);
        }
    }
    catch (Exception e)
    {
        idList = new ArrayList<>();
    }

    return idList;
}
 
Example 3
Source File: Ticket.java    From alpha-wallet-android with MIT License 6 votes vote down vote up
/**
 * Convert a String list of ticket IDs into a list of ticket indices
 * @param userList
 * @return
 */
@Override
public List<BigInteger> ticketIdStringToIndexList(String userList)
{
    List<BigInteger> idList = new ArrayList<>();

    String[] ids = userList.split(",");

    for (String id : ids)
    {
        //remove whitespace
        String     trim   = id.trim();
        BigInteger thisId = Numeric.toBigInt(trim);
        idList.add(thisId);
    }

    return tokenIdsToTokenIndices(idList);
}
 
Example 4
Source File: SignatureDataOperations.java    From web3j with Apache License 2.0 5 votes vote down vote up
default String getFrom() throws SignatureException {
    byte[] encodedTransaction = getEncodedTransaction(getChainId());
    BigInteger v = Numeric.toBigInt(getSignatureData().getV());
    byte[] r = getSignatureData().getR();
    byte[] s = getSignatureData().getS();
    Sign.SignatureData signatureDataV = new Sign.SignatureData(getRealV(v), r, s);
    BigInteger key = Sign.signedMessageToKey(encodedTransaction, signatureDataV);
    return "0x" + Keys.getAddress(key);
}
 
Example 5
Source File: Keys.java    From web3j with Apache License 2.0 5 votes vote down vote up
public static ECKeyPair deserialize(byte[] input) {
    if (input.length != PRIVATE_KEY_SIZE + PUBLIC_KEY_SIZE) {
        throw new RuntimeException("Invalid input key size");
    }

    BigInteger privateKey = Numeric.toBigInt(input, 0, PRIVATE_KEY_SIZE);
    BigInteger publicKey = Numeric.toBigInt(input, PRIVATE_KEY_SIZE, PUBLIC_KEY_SIZE);

    return new ECKeyPair(privateKey, publicKey);
}
 
Example 6
Source File: TradeInstance.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
public TradeInstance(BigInteger price, BigInteger expiry, int[] tickets, String contractAddress, BigInteger publicKey, BigInteger ticketStartId) {
    this.price = price;
    this.expiry = expiry;
    this.tickets = tickets;
    byte[] keyBytes = publicKey.toByteArray();
    this.publicKey = padLeft(Numeric.toHexString(keyBytes, 0, keyBytes.length, false), 128);
    this.ticketStart = ticketStartId;
    this.contractAddress = Numeric.toBigInt(contractAddress);
}
 
Example 7
Source File: Keys.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
public static ECKeyPair deserialize(byte[] input) {
    if (input.length != PRIVATE_KEY_SIZE + PUBLIC_KEY_SIZE) {
        throw new RuntimeException("Invalid input key size");
    }

    BigInteger privateKey = Numeric.toBigInt(input, 0, PRIVATE_KEY_SIZE);
    BigInteger publicKey = Numeric.toBigInt(input, PRIVATE_KEY_SIZE, PUBLIC_KEY_SIZE);

    return new ECKeyPair(privateKey, publicKey);
}
 
Example 8
Source File: SignatureDataOperations.java    From web3j with Apache License 2.0 5 votes vote down vote up
default Long getChainId() {
    BigInteger bv = Numeric.toBigInt(getSignatureData().getV());
    long v = bv.longValue();
    if (v == LOWER_REAL_V || v == (LOWER_REAL_V + 1)) {
        return null;
    }
    return (v - CHAIN_ID_INC) / 2;
}
 
Example 9
Source File: SignedRawTransaction.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
public Long getChainId() {
    BigInteger bv = Numeric.toBigInt(getSignatureData().getV());
    long v = bv.longValue();
    if (v == LOWER_REAL_V || v == (LOWER_REAL_V + 1)) {
        return null;
    }
    return (v - CHAIN_ID_INC) / 2;
}
 
Example 10
Source File: SignedRawTransaction.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
public String getFrom() throws SignatureException {
    Long chainId = getChainId();
    byte[] encodedTransaction;
    if (null == chainId) {
        encodedTransaction = TransactionEncoder.encode(this);
    } else {
        encodedTransaction = TransactionEncoder.encode(this, chainId);
    }
    BigInteger v = Numeric.toBigInt(getSignatureData().getV());
    byte[] r = signatureData.getR();
    byte[] s = signatureData.getS();
    Sign.SignatureData signatureDataV = new Sign.SignatureData(getRealV(v), r, s);
    BigInteger key = Sign.signedMessageToKey(encodedTransaction, signatureDataV);
    return "0x" + Keys.getAddress(key);
}
 
Example 11
Source File: Keys.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
public static ECKeyPair deserialize(byte[] input) {
    if (input.length != PRIVATE_KEY_SIZE + PUBLIC_KEY_SIZE) {
        throw new RuntimeException("Invalid input key size");
    }

    BigInteger privateKey = Numeric.toBigInt(input, 0, PRIVATE_KEY_SIZE);
    BigInteger publicKey = Numeric.toBigInt(input, PRIVATE_KEY_SIZE, PUBLIC_KEY_SIZE);

    return new ECKeyPair(privateKey, publicKey);
}
 
Example 12
Source File: TransactionEncoder.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
public static Sign.SignatureData createEip155SignatureData(
            Sign.SignatureData signatureData, long chainId) {
//        byte v = (byte) (signatureData.getV() + (chainId << 1) + 8);
        BigInteger v =  Numeric.toBigInt(signatureData.getV());
        v = v.subtract(BigInteger.valueOf(LOWER_REAL_V));
        v = v.add(BigInteger.valueOf(chainId * 2));
        v = v.add(BigInteger.valueOf(CHAIN_ID_INC));

        

        return new Sign.SignatureData(
                v.toByteArray(), signatureData.getR(), signatureData.getS());
    }
 
Example 13
Source File: TypeDecoder.java    From etherscan-explorer with GNU General Public License v3.0 4 votes vote down vote up
static Bool decodeBool(String rawInput, int offset) {
    String input = rawInput.substring(offset, offset + MAX_BYTE_LENGTH_FOR_HEX_STRING);
    BigInteger numericValue = Numeric.toBigInt(input);
    boolean value = numericValue.equals(BigInteger.ONE);
    return new Bool(value);
}
 
Example 14
Source File: WasmAddress.java    From client-sdk-java with Apache License 2.0 4 votes vote down vote up
public WasmAddress(byte[] value) {
	this.value = value;
	this.bigIntValue = Numeric.toBigInt(value);
	this.address = Numeric.toHexStringWithPrefixZeroPadded(bigIntValue, LENGTH_IN_HEX);
}
 
Example 15
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 16
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 17
Source File: TypeDecoder.java    From web3j with Apache License 2.0 4 votes vote down vote up
static Bool decodeBool(String rawInput, int offset) {
    String input = rawInput.substring(offset, offset + MAX_BYTE_LENGTH_FOR_HEX_STRING);
    BigInteger numericValue = Numeric.toBigInt(input);
    boolean value = numericValue.equals(BigInteger.ONE);
    return new Bool(value);
}
 
Example 18
Source File: Address.java    From client-sdk-java with Apache License 2.0 4 votes vote down vote up
public Address(String hexValue) {
    this(Numeric.toBigInt(hexValue));
}
 
Example 19
Source File: Address.java    From web3j with Apache License 2.0 4 votes vote down vote up
public Address(int bitSize, String hexValue) {
    this(bitSize, Numeric.toBigInt(hexValue));
}
 
Example 20
Source File: TypeDecoder.java    From client-sdk-java with Apache License 2.0 4 votes vote down vote up
static Bool decodeBool(String rawInput, int offset) {
    String input = rawInput.substring(offset, offset + MAX_BYTE_LENGTH_FOR_HEX_STRING);
    BigInteger numericValue = Numeric.toBigInt(input);
    boolean value = numericValue.equals(BigInteger.ONE);
    return new Bool(value);
}