Java Code Examples for org.fisco.bcos.web3j.utils.Numeric#cleanHexPrefix()

The following examples show how to use org.fisco.bcos.web3j.utils.Numeric#cleanHexPrefix() . 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: Contract.java    From web3sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Check that the contract deployed at the address associated with this smart contract wrapper
 * is in fact the contract you believe it is.
 *
 * <p>This method uses the <a href=
 * "https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getcode">eth_getCode</a> method to get
 * the contract byte code and validates it against the byte code stored in this smart contract
 * wrapper.
 *
 * @return true if the contract is valid
 * @throws IOException if unable to connect to web3j node
 */
public boolean isValid() throws IOException {
    if (contractBinary.equals(BIN_NOT_PROVIDED)) {
        throw new UnsupportedOperationException(
                "Contract binary not present in contract wrapper, "
                        + "please generate your wrapper using -abiFile=<file>");
    }

    if (contractAddress.equals("")) {
        throw new UnsupportedOperationException(
                "Contract binary not present, you will need to regenerate your smart "
                        + "contract wrapper with web3j v2.2.0+");
    }

    Code gcode = web3j.getCode(contractAddress, DefaultBlockParameterName.LATEST).send();
    if (gcode.hasError()) {
        return false;
    }

    String code = Numeric.cleanHexPrefix(gcode.getCode());
    // There may be multiple contracts in the Solidity bytecode, hence we only
    // check for a match with a subset
    return !code.isEmpty() && contractBinary.contains(code);
}
 
Example 2
Source File: RawTransaction.java    From web3sdk with Apache License 2.0 6 votes vote down vote up
protected RawTransaction(
        BigInteger randomid,
        BigInteger gasPrice,
        BigInteger gasLimit,
        BigInteger blockLimit,
        String to,
        BigInteger value,
        String data) {
    this.randomid = randomid;
    this.gasPrice = gasPrice;
    this.gasLimit = gasLimit;
    this.blockLimit = blockLimit;

    this.to = to;

    this.value = value;

    if (data != null) {
        this.data = Numeric.cleanHexPrefix(data);
    }
}
 
Example 3
Source File: Keys.java    From web3sdk with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example 4
Source File: ExtendedRawTransaction.java    From web3sdk with Apache License 2.0 6 votes vote down vote up
protected ExtendedRawTransaction(
        BigInteger randomid,
        BigInteger gasPrice,
        BigInteger gasLimit,
        BigInteger blockLimit,
        String to,
        BigInteger value,
        String data,
        BigInteger fiscoChainId,
        BigInteger groupId,
        String extraData) {
    this.randomid = randomid;
    this.gasPrice = gasPrice;
    this.gasLimit = gasLimit;
    this.blockLimit = blockLimit;
    this.fiscoChainId = fiscoChainId;
    this.groupId = groupId;
    this.extraData = extraData;
    this.to = to;

    this.value = value;

    if (data != null) {
        this.data = Numeric.cleanHexPrefix(data);
    }
}
 
Example 5
Source File: FunctionInputDecode.java    From WeBASE-Collect-Bee with Apache License 2.0 5 votes vote down vote up
/**
 * decode function input, return List<Type>.
 * 
 * @param rawInput
 * @param inputParameters
 * @return
 * @return List<Type>
 */
public static List<Type> decode(String rawInput, List<TypeReference<Type>> inputParameters) {
    String input = Numeric.cleanHexPrefix(rawInput);
    log.info("input without Prefix : {}", input);

    if (input == null || input.length() == 0) {
        return Collections.emptyList();
    } else {
        return build(input, inputParameters);
    }
}
 
Example 6
Source File: AddressUtils.java    From WeBASE-Sign with Apache License 2.0 5 votes vote down vote up
/**
 * @param publicKeyHex String in hex
 * @param encryptType 1: guomi, 0: standard
 * @return
 */
public String getAddressByType(String publicKeyHex, int encryptType) {
	String publicKeyHexNoPrefix = Numeric.cleanHexPrefix(publicKeyHex);

	if (publicKeyHexNoPrefix.length() < PUBLIC_KEY_LENGTH_IN_HEX) {
		publicKeyHexNoPrefix =
				Strings.zeros(PUBLIC_KEY_LENGTH_IN_HEX - publicKeyHexNoPrefix.length())
						+ publicKeyHexNoPrefix;
	}
	String hash = hashByType(publicKeyHexNoPrefix, encryptType);
	// right most 160 bits
	return hash.substring(hash.length() - ADDRESS_LENGTH_IN_HEX);
}
 
Example 7
Source File: TopicTools.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
public static String addressToTopic(String s) {

        if (!WalletUtils.isValidAddress(s)) {
            throw new IllegalArgumentException("invalid address");
        }

        return "0x000000000000000000000000" + Numeric.cleanHexPrefix(s);
    }
 
Example 8
Source File: FunctionReturnDecoder.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 9
Source File: Keys.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
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
}
 
Example 10
Source File: GenGmAccount.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
private static String deduceAccountFromPublic(BigInteger publicKey) {
    try {
        SM3Digest sm3Digest = new SM3Digest();
        System.out.println("===GEN COUNT :" + publicKey.toString(16));
        String publicKeyNoPrefix = Numeric.cleanHexPrefix(publicKey.toString(16));
        String hashSM3String = sm3Digest.hash(publicKeyNoPrefix);
        String account = hashSM3String.substring(24);

        return "0x" + account;
    } catch (Exception e) {
        System.out.println("DeduceAccountFromPublic failed, error message:" + e.getMessage());
        return null;
    }
}
 
Example 11
Source File: FunctionReturnDecoder.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
/**
 * Decodes an indexed parameter associated with an event. Indexed parameters are individually
 * encoded, unlike non-indexed parameters which are encoded as per ABI-encoded function
 * parameters and return values.
 *
 * <p>If any of the following types are indexed, the Keccak-256 hashes of the values are
 * returned instead. These are returned as a bytes32 value.
 *
 * <ul>
 *   <li>Arrays
 *   <li>Strings
 *   <li>Bytes
 * </ul>
 *
 * <p>See the <a href="http://solidity.readthedocs.io/en/latest/contracts.html#events">Solidity
 * documentation</a> for further information.
 *
 * @param rawInput ABI encoded input
 * @param typeReference of expected result type
 * @param <T> type of TypeReference
 * @return the decode value
 */
@SuppressWarnings("unchecked")
public static <T extends Type> Type decodeIndexedValue(
        String rawInput, TypeReference<T> typeReference) {
    String input = Numeric.cleanHexPrefix(rawInput);

    try {
        Class<T> type = typeReference.getClassType();

        if (Bytes.class.isAssignableFrom(type)) {
            return TypeDecoder.decodeBytes(input, (Class<Bytes>) Class.forName(type.getName()));
        } else if (Array.class.isAssignableFrom(type)
                || BytesType.class.isAssignableFrom(type)
                || Utf8String.class.isAssignableFrom(type)) {
            return TypeDecoder.decodeBytes(input, Bytes32.class);
        } else {
            return TypeDecoder.decode(input, 0, type);
        }
    } catch (ClassNotFoundException e) {
        throw new UnsupportedOperationException("Invalid class reference provided", e);
    }
}
 
Example 12
Source File: WalletUtils.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
public static boolean isValidPrivateKey(String privateKey) {
    String cleanPrivateKey = Numeric.cleanHexPrefix(privateKey);
    return cleanPrivateKey.length() == PRIVATE_KEY_LENGTH_IN_HEX;
}
 
Example 13
Source File: WalletUtils.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
public static boolean isValidAddress(String address) {
    String addressNoPrefix = Numeric.cleanHexPrefix(address);
    return addressNoPrefix.length() == ADDRESS_LENGTH_IN_HEX;
}