本文整理了Java中org.web3j.utils.Numeric.cleanHexPrefix()
方法的一些代码示例,展示了Numeric.cleanHexPrefix()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Numeric.cleanHexPrefix()
方法的具体详情如下:
包路径:org.web3j.utils.Numeric
类名称:Numeric
方法名:cleanHexPrefix
暂无
代码示例来源:origin: web3j/web3j
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);
}
}
代码示例来源:origin: web3j/web3j
public static boolean isValidPrivateKey(String privateKey) {
String cleanPrivateKey = Numeric.cleanHexPrefix(privateKey);
return cleanPrivateKey.length() == PRIVATE_KEY_LENGTH_IN_HEX;
}
代码示例来源:origin: web3j/web3j
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;
}
}
代码示例来源:origin: web3j/web3j
public static BigInteger toBigInt(String hexValue) {
String cleanValue = cleanHexPrefix(hexValue);
return toBigIntNoPrefix(cleanValue);
}
代码示例来源:origin: web3j/web3j
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;
}
代码示例来源:origin: web3j/web3j
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
}
代码示例来源:origin: web3j/web3j
/**
* 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();
}
代码示例来源:origin: web3j/web3j
/**
* 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);
}
}
代码示例来源:origin: web3j/web3j
@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"));
}
代码示例来源:origin: web3j/web3j
String code = Numeric.cleanHexPrefix(ethGetCode.getCode());
代码示例来源:origin: web3j/web3j
public static <T extends Type> Type decodeIndexedValue(
String rawInput, TypeReference<T> typeReference) {
String input = Numeric.cleanHexPrefix(rawInput);
代码示例来源:origin: web3j/web3j
/**
* 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);
}
}
代码示例来源:origin: org.web3j/crypto
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);
}
}
代码示例来源:origin: io.daonomic.scalether/util
public static BigInteger toBigInt(String hexValue) {
String cleanValue = cleanHexPrefix(hexValue);
return toBigIntNoPrefix(cleanValue);
}
代码示例来源:origin: io.daonomic.scalether/util
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
}
}
代码示例来源:origin: org.web3j/crypto
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
}
代码示例来源:origin: io.daonomic.scalether/util
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;
}
代码示例来源:origin: org.web3j/crypto
/**
* 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();
}
代码示例来源:origin: org.web3j/abi
/**
* 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);
}
}
代码示例来源:origin: TrustWallet/trust-wallet-android-source
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));
}
}
内容来源于网络,如有侵权,请联系作者删除!