org.web3j.utils.Numeric.toHexString()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(8.0k)|赞(0)|评价(0)|浏览(197)

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

Numeric.toHexString介绍

暂无

代码示例

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

  1. public static String toHexString(byte[] input) {
  2. return toHexString(input, 0, input.length, true);
  3. }

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

  1. public static String toHexStringNoPrefix(byte[] input) {
  2. return toHexString(input, 0, input.length, false);
  3. }

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

  1. public String asString() {
  2. return Numeric.toHexString(value);
  3. }

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

  1. static String buildMethodId(String methodSignature) {
  2. byte[] input = methodSignature.getBytes();
  3. byte[] hash = Hash.sha3(input);
  4. return Numeric.toHexString(hash).substring(0, 10);
  5. }
  6. }

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

  1. /**
  2. * Keccak-256 hash function that operates on a UTF-8 encoded String.
  3. *
  4. * @param utf8String UTF-8 encoded string
  5. * @return hash value as hex encoded string
  6. */
  7. public static String sha3String(String utf8String) {
  8. return Numeric.toHexString(sha3(utf8String.getBytes(StandardCharsets.UTF_8)));
  9. }

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

  1. /**
  2. * Utility method to provide the transaction hash for a given transaction.
  3. *
  4. * @param rawTransaction we wish to send
  5. * @param chainId of the intended chain
  6. * @param credentials of the sender
  7. * @return transaction hash as a hex encoded string
  8. */
  9. public static String generateTransactionHashHexEncoded(
  10. RawTransaction rawTransaction, byte chainId, Credentials credentials) {
  11. return Numeric.toHexString(generateTransactionHash(rawTransaction, chainId, credentials));
  12. }
  13. }

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

  1. public static String buildEventSignature(String methodSignature) {
  2. byte[] input = methodSignature.getBytes();
  3. byte[] hash = Hash.sha3(input);
  4. return Numeric.toHexString(hash);
  5. }
  6. }

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

  1. /**
  2. * Utility method to provide the transaction hash for a given transaction.
  3. *
  4. * @param rawTransaction we wish to send
  5. * @param credentials of the sender
  6. * @return transaction hash as a hex encoded string
  7. */
  8. public static String generateTransactionHashHexEncoded(
  9. RawTransaction rawTransaction, Credentials credentials) {
  10. return Numeric.toHexString(generateTransactionHash(rawTransaction, credentials));
  11. }

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

  1. public static String nameHash(String ensName) {
  2. String normalisedEnsName = normalise(ensName);
  3. return Numeric.toHexString(nameHash(normalisedEnsName.split("\\.")));
  4. }

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

  1. /**
  2. * Keccak-256 hash function.
  3. *
  4. * @param hexInput hex encoded input data with optional 0x prefix
  5. * @return hash value as hex encoded string
  6. */
  7. public static String sha3(String hexInput) {
  8. byte[] bytes = Numeric.hexStringToByteArray(hexInput);
  9. byte[] result = sha3(bytes);
  10. return Numeric.toHexString(result);
  11. }

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

  1. public String sign(RawTransaction rawTransaction) {
  2. byte[] signedMessage;
  3. if (chainId > ChainId.NONE) {
  4. signedMessage = TransactionEncoder.signMessage(rawTransaction, chainId, credentials);
  5. } else {
  6. signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
  7. }
  8. return Numeric.toHexString(signedMessage);
  9. }

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

  1. public static String generateContractAddress(String address, BigInteger nonce) {
  2. byte[] result = generateContractAddress(Numeric.hexStringToByteArray(address), nonce);
  3. return Numeric.toHexString(result);
  4. }
  5. }

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

  1. @Test
  2. public void testToHexString() {
  3. assertThat(Numeric.toHexString(new byte[] {}), is("0x"));
  4. assertThat(Numeric.toHexString(new byte[] { 0x1 }), is("0x01"));
  5. assertThat(Numeric.toHexString(HEX_RANGE_ARRAY), is(HEX_RANGE_STRING));
  6. }

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

  1. @Test
  2. public void testSignMessage() {
  3. byte[] signedMessage = TransactionEncoder.signMessage(
  4. createEtherTransaction(), SampleKeys.CREDENTIALS);
  5. String hexMessage = Numeric.toHexString(signedMessage);
  6. assertThat(hexMessage,
  7. is("0xf85580010a840add5355887fffffffffffffff80"
  8. + "1c"
  9. + "a046360b50498ddf5566551ce1ce69c46c565f1f478bb0ee680caf31fbc08ab727"
  10. + "a01b2f1432de16d110407d544f519fc91b84c8e16d3b6ec899592d486a94974cd0"));
  11. }

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

  1. @Test
  2. public void testTransferEther() throws Exception {
  3. BigInteger nonce = getNonce(ALICE.getAddress());
  4. RawTransaction rawTransaction = createEtherTransaction(
  5. nonce, BOB.getAddress());
  6. byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, ALICE);
  7. String hexValue = Numeric.toHexString(signedMessage);
  8. EthSendTransaction ethSendTransaction =
  9. web3j.ethSendRawTransaction(hexValue).sendAsync().get();
  10. String transactionHash = ethSendTransaction.getTransactionHash();
  11. assertFalse(transactionHash.isEmpty());
  12. TransactionReceipt transactionReceipt =
  13. waitForTransactionReceipt(transactionHash);
  14. assertThat(transactionReceipt.getTransactionHash(), is(transactionHash));
  15. }

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

  1. @Test
  2. public void testDeploySmartContract() throws Exception {
  3. BigInteger nonce = getNonce(ALICE.getAddress());
  4. RawTransaction rawTransaction = createSmartContractTransaction(nonce);
  5. byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, ALICE);
  6. String hexValue = Numeric.toHexString(signedMessage);
  7. EthSendTransaction ethSendTransaction =
  8. web3j.ethSendRawTransaction(hexValue).sendAsync().get();
  9. String transactionHash = ethSendTransaction.getTransactionHash();
  10. assertFalse(transactionHash.isEmpty());
  11. TransactionReceipt transactionReceipt =
  12. waitForTransactionReceipt(transactionHash);
  13. assertThat(transactionReceipt.getTransactionHash(), is(transactionHash));
  14. assertFalse("Contract execution ran out of gas",
  15. rawTransaction.getGasLimit().equals(transactionReceipt.getGasUsed()));
  16. }

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

  1. private String execute(
  2. Credentials credentials, Function function, String contractAddress) throws Exception {
  3. BigInteger nonce = getNonce(credentials.getAddress());
  4. String encodedFunction = FunctionEncoder.encode(function);
  5. RawTransaction rawTransaction = RawTransaction.createTransaction(
  6. nonce,
  7. GAS_PRICE,
  8. GAS_LIMIT,
  9. contractAddress,
  10. encodedFunction);
  11. byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
  12. String hexValue = Numeric.toHexString(signedMessage);
  13. EthSendTransaction transactionResponse = web3j.ethSendRawTransaction(hexValue)
  14. .sendAsync().get();
  15. return transactionResponse.getTransactionHash();
  16. }

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

  1. @Test
  2. public void testSignTransaction() throws Exception {
  3. boolean accountUnlocked = unlockAccount();
  4. assertTrue(accountUnlocked);
  5. RawTransaction rawTransaction = createTransaction();
  6. byte[] encoded = TransactionEncoder.encode(rawTransaction);
  7. byte[] hashed = Hash.sha3(encoded);
  8. EthSign ethSign = web3j.ethSign(ALICE.getAddress(), Numeric.toHexString(hashed))
  9. .sendAsync().get();
  10. String signature = ethSign.getSignature();
  11. assertNotNull(signature);
  12. assertFalse(signature.isEmpty());
  13. }

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

  1. private String sendCreateContractTransaction(
  2. Credentials credentials, BigInteger initialSupply) throws Exception {
  3. BigInteger nonce = getNonce(credentials.getAddress());
  4. String encodedConstructor =
  5. FunctionEncoder.encodeConstructor(
  6. Arrays.asList(
  7. new Uint256(initialSupply),
  8. new Utf8String("web3j tokens"),
  9. new Uint8(BigInteger.TEN),
  10. new Utf8String("w3j$")));
  11. RawTransaction rawTransaction = RawTransaction.createContractTransaction(
  12. nonce,
  13. GAS_PRICE,
  14. GAS_LIMIT,
  15. BigInteger.ZERO,
  16. getHumanStandardTokenBinary() + encodedConstructor);
  17. byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
  18. String hexValue = Numeric.toHexString(signedMessage);
  19. EthSendTransaction transactionResponse = web3j.ethSendRawTransaction(hexValue)
  20. .sendAsync().get();
  21. return transactionResponse.getTransactionHash();
  22. }

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

  1. @Test
  2. public void testDecoding() throws Exception {
  3. BigInteger nonce = BigInteger.ZERO;
  4. BigInteger gasPrice = BigInteger.ONE;
  5. BigInteger gasLimit = BigInteger.TEN;
  6. String to = "0x0add5355";
  7. BigInteger value = BigInteger.valueOf(Long.MAX_VALUE);
  8. RawTransaction rawTransaction = RawTransaction.createEtherTransaction(
  9. nonce, gasPrice, gasLimit, to, value);
  10. byte[] encodedMessage = TransactionEncoder.encode(rawTransaction);
  11. String hexMessage = Numeric.toHexString(encodedMessage);
  12. RawTransaction result = TransactionDecoder.decode(hexMessage);
  13. assertNotNull(result);
  14. assertEquals(nonce, result.getNonce());
  15. assertEquals(gasPrice, result.getGasPrice());
  16. assertEquals(gasLimit, result.getGasLimit());
  17. assertEquals(to, result.getTo());
  18. assertEquals(value, result.getValue());
  19. assertEquals("", result.getData());
  20. }

相关文章