Java Code Examples for org.web3j.crypto.WalletUtils#isValidAddress()

The following examples show how to use org.web3j.crypto.WalletUtils#isValidAddress() . 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: EnsResolver.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
public String resolve(String contractId) {
    if (isValidEnsName(contractId)) {
        PublicResolver resolver = obtainPublicResolver(contractId);

        byte[] nameHash = NameHash.nameHashAsBytes(contractId);
        String contractAddress = null;
        try {
            contractAddress = resolver.addr(nameHash).send();
        } catch (Exception e) {
            throw new RuntimeException("Unable to execute Ethereum request", e);
        }

        if (!WalletUtils.isValidAddress(contractAddress)) {
            throw new RuntimeException("Unable to resolve address for name: " + contractId);
        } else {
            return contractAddress;
        }
    } else {
        return contractId;
    }
}
 
Example 2
Source File: EnsResolver.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 3
Source File: EnsResolver.java    From web3j with Apache License 2.0 6 votes vote down vote up
/**
 * Reverse name resolution as documented in the <a
 * href="https://docs.ens.domains/contract-api-reference/reverseregistrar">specification</a>.
 *
 * @param address an ethereum address, example: "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e"
 * @return a EnsName registered for provided address
 */
public String reverseResolve(String address) {
    if (WalletUtils.isValidAddress(address, addressLength)) {
        String reverseName = Numeric.cleanHexPrefix(address) + REVERSE_NAME_SUFFIX;
        PublicResolver resolver = obtainPublicResolver(reverseName);

        byte[] nameHash = NameHash.nameHashAsBytes(reverseName);
        String name;
        try {
            name = resolver.name(nameHash).send();
        } catch (Exception e) {
            throw new RuntimeException("Unable to execute Ethereum request", e);
        }

        if (!isValidEnsName(name, addressLength)) {
            throw new RuntimeException("Unable to resolve name for address: " + address);
        } else {
            return name;
        }
    } else {
        throw new EnsResolutionException("Address is invalid: " + address);
    }
}
 
Example 4
Source File: EnsResolver.java    From web3j with Apache License 2.0 6 votes vote down vote up
public String resolve(String contractId) {
    if (isValidEnsName(contractId, addressLength)) {
        PublicResolver resolver = obtainPublicResolver(contractId);

        byte[] nameHash = NameHash.nameHashAsBytes(contractId);
        String contractAddress = null;
        try {
            contractAddress = resolver.addr(nameHash).send();
        } catch (Exception e) {
            throw new RuntimeException("Unable to execute Ethereum request", e);
        }

        if (!WalletUtils.isValidAddress(contractAddress)) {
            throw new RuntimeException("Unable to resolve address for name: " + contractId);
        } else {
            return contractAddress;
        }
    } else {
        return contractId;
    }
}
 
Example 5
Source File: ImportWalletActivity.java    From alpha-wallet-android with MIT License 6 votes vote down vote up
@Override
public void onKeystore(String keystore, String password)
{
    String address = extractAddressFromStore(keystore);
    if (address != null && WalletUtils.isValidAddress(address))
    {
        onProgress(true);
        importWalletViewModel.checkKeystorePassword(keystore, address, password)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(result -> keyStoreValid(result, address)).isDisposed();
    }
    else
    {
        keyImportError(getString(R.string.invalid_keystore));
    }
}
 
Example 6
Source File: WalletSendFunds.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
private void run(String walletFileLocation,String chainId, String destinationAddress) {
    File walletFile = new File(walletFileLocation);
    Credentials credentials = getCredentials(walletFile);
    console.printf("Wallet for address " + credentials.getAddress() + " loaded\n");

    if (!WalletUtils.isValidAddress(destinationAddress)) {
        exitError("Invalid destination address specified");
    }

    Web3j web3j = getEthereumClient();

    BigDecimal amountToTransfer = getAmountToTransfer();
    Convert.Unit transferUnit = getTransferUnit();
    BigDecimal amountInWei = Convert.toVon(amountToTransfer, transferUnit);

    confirmTransfer(amountToTransfer, transferUnit, amountInWei, destinationAddress);

    TransactionReceipt transactionReceipt = performTransfer(
            web3j, destinationAddress,chainId, credentials, amountInWei);

    console.printf("Funds have been successfully transferred from %s to %s%n"
                    + "Transaction hash: %s%nMined block number: %s%n",
            credentials.getAddress(),
            destinationAddress,
            transactionReceipt.getTransactionHash(),
            transactionReceipt.getBlockNumber());
}
 
Example 7
Source File: WalletSendFunds.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
private void run(String walletFileLocation, String destinationAddress) {
    File walletFile = new File(walletFileLocation);
    Credentials credentials = getCredentials(walletFile);
    console.printf("Wallet for address " + credentials.getAddress() + " loaded\n");

    if (!WalletUtils.isValidAddress(destinationAddress)
            && !EnsResolver.isValidEnsName(destinationAddress)) {
        exitError("Invalid destination address specified");
    }

    Web3j web3j = getEthereumClient();

    BigDecimal amountToTransfer = getAmountToTransfer();
    Convert.Unit transferUnit = getTransferUnit();
    BigDecimal amountInWei = Convert.toWei(amountToTransfer, transferUnit);

    confirmTransfer(amountToTransfer, transferUnit, amountInWei, destinationAddress);

    TransactionReceipt transactionReceipt = performTransfer(
            web3j, destinationAddress, credentials, amountInWei);

    console.printf("Funds have been successfully transferred from %s to %s%n"
                    + "Transaction hash: %s%nMined block number: %s%n",
            credentials.getAddress(),
            destinationAddress,
            transactionReceipt.getTransactionHash(),
            transactionReceipt.getBlockNumber());
}
 
Example 8
Source File: StringUtil.java    From dapp-wallet-demo with Apache License 2.0 5 votes vote down vote up
public static String shortAddress(String address){
    String shortOfAddress = "unknown";
    if (WalletUtils.isValidAddress(address)){
        int length = address.length();
        shortOfAddress = address.substring(0,10) + "..." + address.substring(length-10,length);
    }
    return shortOfAddress;
}
 
Example 9
Source File: TokensRealmSource.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
private void saveToken(Wallet wallet, Token token, Date currentTime) {
    if (!WalletUtils.isValidAddress(wallet.address)) return;
    try (Realm realm = realmManager.getRealmInstance(wallet))
    {
        TransactionsRealmCache.addRealm();
        realm.beginTransaction();
        saveToken(realm, token);
        realm.commitTransaction();
        TransactionsRealmCache.subRealm();
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
    }
}
 
Example 10
Source File: HomeActivity.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
private void onBackup(String address)
{
    if (address != null && WalletUtils.isValidAddress(address))
    {
        Toast.makeText(this, getString(R.string.postponed_backup_warning), Toast.LENGTH_LONG).show();
    }
}
 
Example 11
Source File: EnsResolver.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
/**
 * Reverse name resolution as documented in the <a
 * href="https://docs.ens.domains/contract-api-reference/reverseregistrar">specification</a>.
 *
 * @param address an ethereum address, example: "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e"
 * @return a EnsName registered for provided address
 */
public String reverseResolve(String address) throws UnableToResolveENS
{
    String name = null;
    if (WalletUtils.isValidAddress(address))
    {
        String reverseName = Numeric.cleanHexPrefix(address) + REVERSE_NAME_SUFFIX;
        try
        {
            String resolverAddress = lookupResolver(reverseName);
            byte[] nameHash = NameHash.nameHashAsBytes(reverseName);
            name = getContractData(EthereumNetworkBase.MAINNET_ID, resolverAddress, getName(nameHash));
        }
        catch (Exception e)
        {
            throw new RuntimeException("Unable to execute Ethereum request", e);
        }

        if (!isValidEnsName(name, addressLength))
        {
            throw new UnableToResolveENS("Unable to resolve name for address: " + address);
        }
        else
        {
            return name;
        }
    }
    else
    {
        throw new EnsResolutionException("Address is invalid: " + address);
    }
}
 
Example 12
Source File: EnsResolver.java    From web3j with Apache License 2.0 4 votes vote down vote up
public static boolean isValidEnsName(String input, int addressLength) {
    return input != null // will be set to null on new Contract creation
            && (input.contains(".") || !WalletUtils.isValidAddress(input, addressLength));
}
 
Example 13
Source File: Utils.java    From alpha-wallet-android with MIT License 4 votes vote down vote up
public static boolean isAddressValid(String address)
{
    return WalletUtils.isValidAddress(address);
}
 
Example 14
Source File: EnsResolver.java    From alpha-wallet-android with MIT License 4 votes vote down vote up
public static boolean isValidEnsName(String input, int addressLength) {
    return input != null // will be set to null on new Contract creation
            && (input.contains(".") || !WalletUtils.isValidAddress(input));
}
 
Example 15
Source File: HomeActivity.java    From alpha-wallet-android with MIT License 4 votes vote down vote up
@Override
public void backupSuccess(String keyAddress)
{
    if (WalletUtils.isValidAddress(keyAddress)) backupWalletSuccess(keyAddress);
}
 
Example 16
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 4 votes vote down vote up
public boolean isValidPublicAddress(String address){
    return  WalletUtils.isValidAddress(address);
}
 
Example 17
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 4 votes vote down vote up
public boolean isValidPublicAddress(String address) {
    return  WalletUtils.isValidAddress(address);
}
 
Example 18
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 4 votes vote down vote up
public boolean isValidPublicAddress(String address) {
    return  WalletUtils.isValidAddress(address);
}
 
Example 19
Source File: EnsResolver.java    From etherscan-explorer with GNU General Public License v3.0 4 votes vote down vote up
public static boolean isValidEnsName(String input) {
    return input != null  // will be set to null on new Contract creation
            && (input.contains(".") || !WalletUtils.isValidAddress(input));
}
 
Example 20
Source File: Address.java    From ETHWallet with GNU General Public License v3.0 4 votes vote down vote up
public static boolean isAddress(String address) {
    return WalletUtils.isValidAddress(address);
}