protected RawTransaction(BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit, String to, BigInteger value, String data) { this.nonce = nonce; this.gasPrice = gasPrice; this.gasLimit = gasLimit; this.to = to; this.value = value; if (data != null) { this.data = Numeric.cleanHexPrefix(data); } }
public static boolean isValidPrivateKey(String privateKey) { String cleanPrivateKey = Numeric.cleanHexPrefix(privateKey); return cleanPrivateKey.length() == PRIVATE_KEY_LENGTH_IN_HEX; }
public static boolean isValidAddress(String input) { String cleanInput = Numeric.cleanHexPrefix(input); try { Numeric.toBigIntNoPrefix(cleanInput); } catch (NumberFormatException e) { return false; } return cleanInput.length() == ADDRESS_LENGTH_IN_HEX; } }
public static BigInteger toBigInt(String hexValue) { String cleanValue = cleanHexPrefix(hexValue); return toBigIntNoPrefix(cleanValue); }
public static byte[] hexStringToByteArray(String input) { String cleanInput = cleanHexPrefix(input); int len = cleanInput.length(); if (len == 0) { return new byte[] {}; } byte[] data; int startIdx; if (len % 2 != 0) { data = new byte[(len / 2) + 1]; data[0] = (byte) Character.digit(cleanInput.charAt(0), 16); startIdx = 1; } else { data = new byte[len / 2]; startIdx = 0; } for (int i = startIdx; i < len; i += 2) { data[(i + 1) / 2] = (byte) ((Character.digit(cleanInput.charAt(i), 16) << 4) + Character.digit(cleanInput.charAt(i + 1), 16)); } return data; }
public static String getAddress(String publicKey) { String publicKeyNoPrefix = Numeric.cleanHexPrefix(publicKey); if (publicKeyNoPrefix.length() < PUBLIC_KEY_LENGTH_IN_HEX) { publicKeyNoPrefix = Strings.zeros( PUBLIC_KEY_LENGTH_IN_HEX - publicKeyNoPrefix.length()) + publicKeyNoPrefix; } String hash = Hash.sha3(publicKeyNoPrefix); return hash.substring(hash.length() - ADDRESS_LENGTH_IN_HEX); // right most 160 bits }
/** * Checksum address encoding as per * <a href="https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md">EIP-55</a>. * * @param address a valid hex encoded address * @return hex encoded checksum address */ public static String toChecksumAddress(String address) { String lowercaseAddress = Numeric.cleanHexPrefix(address).toLowerCase(); String addressHash = Numeric.cleanHexPrefix(Hash.sha3String(lowercaseAddress)); StringBuilder result = new StringBuilder(lowercaseAddress.length() + 2); result.append("0x"); for (int i = 0; i < lowercaseAddress.length(); i++) { if (Integer.parseInt(String.valueOf(addressHash.charAt(i)), 16) >= 8) { result.append(String.valueOf(lowercaseAddress.charAt(i)).toUpperCase()); } else { result.append(lowercaseAddress.charAt(i)); } } return result.toString(); }
/** * Decode ABI encoded return values from smart contract function call. * * @param rawInput ABI encoded input * @param outputParameters list of return types as {@link TypeReference} * @return {@link List} of values returned by function, {@link Collections#emptyList()} if * invalid response */ public static List<Type> decode( String rawInput, List<TypeReference<Type>> outputParameters) { String input = Numeric.cleanHexPrefix(rawInput); if (Strings.isEmpty(input)) { return Collections.emptyList(); } else { return build(input, outputParameters); } }
@Test public void testCleanHexPrefix() { assertThat(Numeric.cleanHexPrefix(""), is("")); assertThat(Numeric.cleanHexPrefix("0123456789abcdef"), is("0123456789abcdef")); assertThat(Numeric.cleanHexPrefix("0x"), is("")); assertThat(Numeric.cleanHexPrefix("0x0123456789abcdef"), is("0123456789abcdef")); }
String code = Numeric.cleanHexPrefix(ethGetCode.getCode());
public static <T extends Type> Type decodeIndexedValue( String rawInput, TypeReference<T> typeReference) { String input = Numeric.cleanHexPrefix(rawInput);
/** * Reverse name resolution as documented in the * <a href="https://docs.ens.domains/en/latest/userguide.html#reverse-name-resolution">specification</a>. * @param address an ethereum address, example: "0x314159265dd8dbb310642f98f50c066173c1259b" * @return a EnsName registered for provided address */ public String reverseResolve(String address) { if (WalletUtils.isValidAddress(address)) { String reverseName = Numeric.cleanHexPrefix(address) + REVERSE_NAME_SUFFIX; PublicResolver resolver = obtainPublicResolver(reverseName); byte[] nameHash = NameHash.nameHashAsBytes(reverseName); String name = null; try { name = resolver.name(nameHash).send(); } catch (Exception e) { throw new RuntimeException("Unable to execute Ethereum request", e); } if (!isValidEnsName(name)) { throw new RuntimeException("Unable to resolve name for address: " + address); } else { return name; } } else { throw new EnsResolutionException("Address is invalid: " + address); } }
protected RawTransaction(BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit, String to, BigInteger value, String data) { this.nonce = nonce; this.gasPrice = gasPrice; this.gasLimit = gasLimit; this.to = to; this.value = value; if (data != null) { this.data = Numeric.cleanHexPrefix(data); } }
public static BigInteger toBigInt(String hexValue) { String cleanValue = cleanHexPrefix(hexValue); return toBigIntNoPrefix(cleanValue); }
public static String getAddress(String publicKey) { String publicKeyNoPrefix = Numeric.cleanHexPrefix(publicKey); if (publicKeyNoPrefix.length() < PUBLIC_KEY_LENGTH_IN_HEX) { publicKeyNoPrefix = Strings.zeros( PUBLIC_KEY_LENGTH_IN_HEX - publicKeyNoPrefix.length()) + publicKeyNoPrefix; } String hash = Hash.sha3(publicKeyNoPrefix); return hash.substring(hash.length() - ADDRESS_LENGTH_IN_HEX); // right most 160 bits } }
public static String getAddress(String publicKey) { String publicKeyNoPrefix = Numeric.cleanHexPrefix(publicKey); if (publicKeyNoPrefix.length() < PUBLIC_KEY_LENGTH_IN_HEX) { publicKeyNoPrefix = Strings.zeros( PUBLIC_KEY_LENGTH_IN_HEX - publicKeyNoPrefix.length()) + publicKeyNoPrefix; } String hash = Hash.sha3(publicKeyNoPrefix); return hash.substring(hash.length() - ADDRESS_LENGTH_IN_HEX); // right most 160 bits }
public static byte[] hexStringToByteArray(String input) { String cleanInput = cleanHexPrefix(input); int len = cleanInput.length(); if (len == 0) { return new byte[] {}; } byte[] data; int startIdx; if (len % 2 != 0) { data = new byte[(len / 2) + 1]; data[0] = (byte) Character.digit(cleanInput.charAt(0), 16); startIdx = 1; } else { data = new byte[len / 2]; startIdx = 0; } for (int i = startIdx; i < len; i += 2) { data[(i + 1) / 2] = (byte) ((Character.digit(cleanInput.charAt(i), 16) << 4) + Character.digit(cleanInput.charAt(i + 1), 16)); } return data; }
/** * Checksum address encoding as per * <a href="https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md">EIP-55</a>. * * @param address a valid hex encoded address * @return hex encoded checksum address */ public static String toChecksumAddress(String address) { String lowercaseAddress = Numeric.cleanHexPrefix(address).toLowerCase(); String addressHash = Numeric.cleanHexPrefix(Hash.sha3String(lowercaseAddress)); StringBuilder result = new StringBuilder(lowercaseAddress.length() + 2); result.append("0x"); for (int i = 0; i < lowercaseAddress.length(); i++) { if (Integer.parseInt(String.valueOf(addressHash.charAt(i)), 16) >= 8) { result.append(String.valueOf(lowercaseAddress.charAt(i)).toUpperCase()); } else { result.append(lowercaseAddress.charAt(i)); } } return result.toString(); }
/** * Decode ABI encoded return values from smart contract function call. * * @param rawInput ABI encoded input * @param outputParameters list of return types as {@link TypeReference} * @return {@link List} of values returned by function, {@link Collections#emptyList()} if * invalid response */ public static List<Type> decode( String rawInput, List<TypeReference<Type>> outputParameters) { String input = Numeric.cleanHexPrefix(rawInput); if (Strings.isEmpty(input)) { return Collections.emptyList(); } else { return build(input, outputParameters); } }
public static byte[] createTokenTransferData(String to, BigInteger tokenAmount) { List<Type> params = Arrays.<Type>asList(new Address(to), new Uint256(tokenAmount)); List<TypeReference<?>> returnTypes = Arrays.<TypeReference<?>>asList(new TypeReference<Bool>() { }); Function function = new Function("transfer", params, returnTypes); String encodedFunction = FunctionEncoder.encode(function); return Numeric.hexStringToByteArray(Numeric.cleanHexPrefix(encodedFunction)); } }