org.web3j.crypto.WalletUtils.isValidAddress()方法的使用及代码示例

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

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

WalletUtils.isValidAddress介绍

暂无

代码示例

代码示例来源: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

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

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

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

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

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

相关文章