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

The following examples show how to use org.web3j.crypto.WalletUtils#isValidPrivateKey() . 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: KeyImporter.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
private void createWalletFile(String privateKey) {
    if (!WalletUtils.isValidPrivateKey(privateKey)) {
        exitError("Invalid private key specified, must be "
                + PRIVATE_KEY_LENGTH_IN_HEX
                + " digit hex value");
    }

    Credentials credentials = Credentials.create(privateKey);
    String password = getPassword("Please enter a wallet file password: ");

    String destinationDir = getDestinationDir();
    File destination = createDir(destinationDir);

    try {
        String walletFileName = WalletUtils.generateWalletFile(
                password, credentials.getEcKeyPair(), destination, true);
        console.printf("Wallet file " + walletFileName
                + " successfully created in: " + destinationDir + "\n");
    } catch (CipherException | IOException e) {
        exitError(e);
    }
}
 
Example 2
Source File: KeyImporter.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
private void createWalletFile(String privateKey) {
    if (!WalletUtils.isValidPrivateKey(privateKey)) {
        exitError("Invalid private key specified, must be "
                + PRIVATE_KEY_LENGTH_IN_HEX
                + " digit hex value");
    }

    Credentials credentials = Credentials.create(privateKey);
    String password = getPassword("Please enter a wallet file password: ");

    String destinationDir = getDestinationDir();
    File destination = createDir(destinationDir);

    try {
        String walletFileName = WalletUtils.generateWalletFile(
                password, credentials.getEcKeyPair(), destination, true);
        console.printf("Wallet file " + walletFileName
                + " successfully created in: " + destinationDir + "\n");
    } catch (CipherException | IOException e) {
        exitError(e);
    }
}
 
Example 3
Source File: ImportWalletActivity.java    From alpha-wallet-android with MIT License 6 votes vote down vote up
@Override
public void onPrivateKey(String privateKey)
{
    try
    {
        BigInteger key = new BigInteger(privateKey, 16);
        if (!WalletUtils.isValidPrivateKey(privateKey)) throw new Exception(getString(R.string.invalid_private_key));
        ECKeyPair keypair = ECKeyPair.create(key);
        String address = Numeric.prependHexPrefix(Keys.getAddress(keypair));

        if (importWalletViewModel.keystoreExists(address))
        {
            queryReplaceWalletPrivateKey(address);
        }
        else
        {
            importWalletViewModel.importPrivateKeyWallet(address, this, this);
        }
    }
    catch (Exception e)
    {
        keyImportError(e.getMessage());
    }
}