public static Credentials loadCredentials(String password, String source) throws IOException, CipherException { return loadCredentials(password, new File(source)); }
public static String generateNewWalletFile( String password, File destinationDirectory, boolean useFullScrypt) throws CipherException, IOException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException { ECKeyPair ecKeyPair = Keys.createEcKeyPair(); return generateWalletFile(password, ecKeyPair, destinationDirectory, useFullScrypt); }
String getDestinationDir() { String defaultDir = WalletUtils.getTestnetKeyDirectory(); String destinationDir = console.readLine( "Please enter a destination directory location [" + defaultDir + "]: "); if (destinationDir.equals("")) { return defaultDir; } else if (destinationDir.startsWith("~")) { return System.getProperty("user.home") + destinationDir.substring(1); } else { return destinationDir; } }
@Test public void testGenerateLightWalletFile() throws Exception { String fileName = WalletUtils.generateWalletFile(PASSWORD, KEY_PAIR, tempDir, false); testGenerateWalletFile(fileName); }
@Test public void testGetTestnetKeyDirectory() { assertTrue(WalletUtils.getMainnetKeyDirectory() .endsWith(String.format("%skeystore", File.separator))); assertTrue(WalletUtils.getTestnetKeyDirectory() .endsWith(String.format("%stestnet%skeystore", File.separator, File.separator))); assertTrue(WalletUtils.getRinkebyKeyDirectory() .endsWith(String.format("%srinkeby%skeystore", File.separator, File.separator))); }
/** * Get keystore destination directory for a Rinkeby network. * @return a String containing destination directory */ public static String getRinkebyKeyDirectory() { return String.format( "%s%srinkeby%skeystore", getDefaultKeyDirectory(), File.separator, File.separator); }
public static boolean isValidEnsName(String input) { return input != null // will be set to null on new Contract creation && (input.contains(".") || !WalletUtils.isValidAddress(input)); } }
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); } } }
public static String generateLightNewWalletFile(String password, File destinationDirectory) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, CipherException, IOException { return generateNewWalletFile(password, destinationDirectory, false); }
@Test public void testGenerateFullNewWalletFile() throws Exception { String fileName = WalletUtils.generateFullNewWalletFile(PASSWORD, tempDir); testGeneratedNewWalletFile(fileName); }
@Test public void testGenerateFullWalletFile() throws Exception { String fileName = WalletUtils.generateWalletFile(PASSWORD, KEY_PAIR, tempDir, true); testGenerateWalletFile(fileName); }
public static String getTestnetKeyDirectory() { return String.format( "%s%stestnet%skeystore", getDefaultKeyDirectory(), File.separator, File.separator); }
@Test public void testIsValidAddress() { assertTrue(isValidAddress(SampleKeys.ADDRESS)); assertTrue(isValidAddress(SampleKeys.ADDRESS_NO_PREFIX)); assertFalse(isValidAddress("")); assertFalse(isValidAddress(SampleKeys.ADDRESS + 'a')); assertFalse(isValidAddress(SampleKeys.ADDRESS.substring(1))); } }
public static String generateFullNewWalletFile(String password, File destinationDirectory) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, CipherException, IOException { return generateNewWalletFile(password, destinationDirectory, true); }
private void run() { String password = getPassword("Please enter a wallet file password: "); String destinationDir = getDestinationDir(); File destination = createDir(destinationDir); try { String walletFileName = WalletUtils.generateFullNewWalletFile(password, destination); console.printf("Wallet file " + walletFileName + " successfully created in: " + destinationDir + "\n"); } catch (CipherException | IOException | InvalidAlgorithmParameterException | NoSuchAlgorithmException | NoSuchProviderException e) { Console.exitError(e); } } }
private void testGeneratedNewWalletFile(String fileName) throws Exception { WalletUtils.loadCredentials(PASSWORD, new File(tempDir, fileName)); }
/** * Generates a BIP-39 compatible Ethereum wallet. The private key for the wallet can * be calculated using following algorithm: * <pre> * Key = SHA-256(BIP_39_SEED(mnemonic, password)) * </pre> * * @param password Will be used for both wallet encryption and passphrase for BIP-39 seed * @param destinationDirectory The directory containing the wallet * @return A BIP-39 compatible Ethereum wallet * @throws CipherException if the underlying cipher is not available * @throws IOException if the destination cannot be written to */ public static Bip39Wallet generateBip39Wallet(String password, File destinationDirectory) throws CipherException, IOException { byte[] initialEntropy = new byte[16]; secureRandom.nextBytes(initialEntropy); String mnemonic = MnemonicUtils.generateMnemonic(initialEntropy); byte[] seed = MnemonicUtils.generateSeed(mnemonic, password); ECKeyPair privateKey = ECKeyPair.create(sha256(seed)); String walletFile = generateWalletFile(password, privateKey, destinationDirectory, false); return new Bip39Wallet(walletFile, mnemonic); }
public static String getDefaultKeyDirectory() { return getDefaultKeyDirectory(System.getProperty("os.name")); }
public static List<SecureKey> listRopstenKeystores() { return listKeystores(new File(WalletUtils.getTestnetKeyDirectory())); }
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; } }