org.web3j.crypto.WalletUtils类的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(7.1k)|赞(0)|评价(0)|浏览(234)

本文整理了Java中org.web3j.crypto.WalletUtils类的一些代码示例,展示了WalletUtils类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。WalletUtils类的具体详情如下:
包路径:org.web3j.crypto.WalletUtils
类名称:WalletUtils

WalletUtils介绍

[英]Utility functions for working with Wallet files.
[中]用于处理钱包文件的实用功能。

代码示例

代码示例来源:origin: web3j/web3j

public static Credentials loadCredentials(String password, String source)
    throws IOException, CipherException {
  return loadCredentials(password, new File(source));
}

代码示例来源:origin: web3j/web3j

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);
}

代码示例来源:origin: web3j/web3j

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;
  }
}

代码示例来源:origin: web3j/web3j

@Test
public void testGenerateLightWalletFile() throws Exception {
  String fileName = WalletUtils.generateWalletFile(PASSWORD, KEY_PAIR, tempDir, false);
  testGenerateWalletFile(fileName);
}

代码示例来源:origin: web3j/web3j

@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)));
  
}

代码示例来源:origin: web3j/web3j

/**
 * 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);
}

代码示例来源:origin: web3j/web3j

public static boolean isValidEnsName(String input) {
    return input != null  // will be set to null on new Contract creation
        && (input.contains(".") || !WalletUtils.isValidAddress(input));
  }
}

代码示例来源:origin: web3j/web3j

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);
    }
  }
}

代码示例来源:origin: web3j/web3j

public static String generateLightNewWalletFile(String password, File destinationDirectory)
    throws NoSuchAlgorithmException, NoSuchProviderException,
    InvalidAlgorithmParameterException, CipherException, IOException {
  return generateNewWalletFile(password, destinationDirectory, false);
}

代码示例来源:origin: web3j/web3j

@Test
public void testGenerateFullNewWalletFile() throws Exception {
  String fileName = WalletUtils.generateFullNewWalletFile(PASSWORD, tempDir);
  testGeneratedNewWalletFile(fileName);
}

代码示例来源:origin: web3j/web3j

@Test
public void testGenerateFullWalletFile() throws Exception {
  String fileName = WalletUtils.generateWalletFile(PASSWORD, KEY_PAIR, tempDir, true);
  testGenerateWalletFile(fileName);
}

代码示例来源:origin: web3j/web3j

public static String getTestnetKeyDirectory() {
  return String.format(
      "%s%stestnet%skeystore", getDefaultKeyDirectory(), File.separator, File.separator);
}

代码示例来源:origin: web3j/web3j

@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)));
  }
}

代码示例来源:origin: web3j/web3j

public static String generateFullNewWalletFile(String password, File destinationDirectory)
    throws NoSuchAlgorithmException, NoSuchProviderException,
    InvalidAlgorithmParameterException, CipherException, IOException {
  return generateNewWalletFile(password, destinationDirectory, true);
}

代码示例来源:origin: web3j/web3j

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);
    }
  }
}

代码示例来源:origin: web3j/web3j

private void testGeneratedNewWalletFile(String fileName) throws Exception {
  WalletUtils.loadCredentials(PASSWORD, new File(tempDir, fileName));
}

代码示例来源:origin: web3j/web3j

/**
 * 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);
}

代码示例来源:origin: web3j/web3j

public static String getDefaultKeyDirectory() {
  return getDefaultKeyDirectory(System.getProperty("os.name"));
}

代码示例来源:origin: adridadou/eth-contract-api

public static List<SecureKey> listRopstenKeystores() {
  return listKeystores(new File(WalletUtils.getTestnetKeyDirectory()));
}

代码示例来源:origin: web3j/web3j

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;
  }
}

相关文章