public static String toHexString(byte[] input) { return toHexString(input, 0, input.length, true); }
public static String toHexStringNoPrefix(byte[] input) { return toHexString(input, 0, input.length, false); }
public String asString() { return Numeric.toHexString(value); }
static String buildMethodId(String methodSignature) { byte[] input = methodSignature.getBytes(); byte[] hash = Hash.sha3(input); return Numeric.toHexString(hash).substring(0, 10); } }
/** * Keccak-256 hash function that operates on a UTF-8 encoded String. * * @param utf8String UTF-8 encoded string * @return hash value as hex encoded string */ public static String sha3String(String utf8String) { return Numeric.toHexString(sha3(utf8String.getBytes(StandardCharsets.UTF_8))); }
/** * Utility method to provide the transaction hash for a given transaction. * * @param rawTransaction we wish to send * @param chainId of the intended chain * @param credentials of the sender * @return transaction hash as a hex encoded string */ public static String generateTransactionHashHexEncoded( RawTransaction rawTransaction, byte chainId, Credentials credentials) { return Numeric.toHexString(generateTransactionHash(rawTransaction, chainId, credentials)); } }
public static String buildEventSignature(String methodSignature) { byte[] input = methodSignature.getBytes(); byte[] hash = Hash.sha3(input); return Numeric.toHexString(hash); } }
/** * Utility method to provide the transaction hash for a given transaction. * * @param rawTransaction we wish to send * @param credentials of the sender * @return transaction hash as a hex encoded string */ public static String generateTransactionHashHexEncoded( RawTransaction rawTransaction, Credentials credentials) { return Numeric.toHexString(generateTransactionHash(rawTransaction, credentials)); }
public static String nameHash(String ensName) { String normalisedEnsName = normalise(ensName); return Numeric.toHexString(nameHash(normalisedEnsName.split("\\."))); }
/** * Keccak-256 hash function. * * @param hexInput hex encoded input data with optional 0x prefix * @return hash value as hex encoded string */ public static String sha3(String hexInput) { byte[] bytes = Numeric.hexStringToByteArray(hexInput); byte[] result = sha3(bytes); return Numeric.toHexString(result); }
public String sign(RawTransaction rawTransaction) { byte[] signedMessage; if (chainId > ChainId.NONE) { signedMessage = TransactionEncoder.signMessage(rawTransaction, chainId, credentials); } else { signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials); } return Numeric.toHexString(signedMessage); }
public static String generateContractAddress(String address, BigInteger nonce) { byte[] result = generateContractAddress(Numeric.hexStringToByteArray(address), nonce); return Numeric.toHexString(result); } }
@Test public void testToHexString() { assertThat(Numeric.toHexString(new byte[] {}), is("0x")); assertThat(Numeric.toHexString(new byte[] { 0x1 }), is("0x01")); assertThat(Numeric.toHexString(HEX_RANGE_ARRAY), is(HEX_RANGE_STRING)); }
@Test public void testSignMessage() { byte[] signedMessage = TransactionEncoder.signMessage( createEtherTransaction(), SampleKeys.CREDENTIALS); String hexMessage = Numeric.toHexString(signedMessage); assertThat(hexMessage, is("0xf85580010a840add5355887fffffffffffffff80" + "1c" + "a046360b50498ddf5566551ce1ce69c46c565f1f478bb0ee680caf31fbc08ab727" + "a01b2f1432de16d110407d544f519fc91b84c8e16d3b6ec899592d486a94974cd0")); }
@Test public void testTransferEther() throws Exception { BigInteger nonce = getNonce(ALICE.getAddress()); RawTransaction rawTransaction = createEtherTransaction( nonce, BOB.getAddress()); byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, ALICE); String hexValue = Numeric.toHexString(signedMessage); EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get(); String transactionHash = ethSendTransaction.getTransactionHash(); assertFalse(transactionHash.isEmpty()); TransactionReceipt transactionReceipt = waitForTransactionReceipt(transactionHash); assertThat(transactionReceipt.getTransactionHash(), is(transactionHash)); }
@Test public void testDeploySmartContract() throws Exception { BigInteger nonce = getNonce(ALICE.getAddress()); RawTransaction rawTransaction = createSmartContractTransaction(nonce); byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, ALICE); String hexValue = Numeric.toHexString(signedMessage); EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get(); String transactionHash = ethSendTransaction.getTransactionHash(); assertFalse(transactionHash.isEmpty()); TransactionReceipt transactionReceipt = waitForTransactionReceipt(transactionHash); assertThat(transactionReceipt.getTransactionHash(), is(transactionHash)); assertFalse("Contract execution ran out of gas", rawTransaction.getGasLimit().equals(transactionReceipt.getGasUsed())); }
private String execute( Credentials credentials, Function function, String contractAddress) throws Exception { BigInteger nonce = getNonce(credentials.getAddress()); String encodedFunction = FunctionEncoder.encode(function); RawTransaction rawTransaction = RawTransaction.createTransaction( nonce, GAS_PRICE, GAS_LIMIT, contractAddress, encodedFunction); byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials); String hexValue = Numeric.toHexString(signedMessage); EthSendTransaction transactionResponse = web3j.ethSendRawTransaction(hexValue) .sendAsync().get(); return transactionResponse.getTransactionHash(); }
@Test public void testSignTransaction() throws Exception { boolean accountUnlocked = unlockAccount(); assertTrue(accountUnlocked); RawTransaction rawTransaction = createTransaction(); byte[] encoded = TransactionEncoder.encode(rawTransaction); byte[] hashed = Hash.sha3(encoded); EthSign ethSign = web3j.ethSign(ALICE.getAddress(), Numeric.toHexString(hashed)) .sendAsync().get(); String signature = ethSign.getSignature(); assertNotNull(signature); assertFalse(signature.isEmpty()); }
private String sendCreateContractTransaction( Credentials credentials, BigInteger initialSupply) throws Exception { BigInteger nonce = getNonce(credentials.getAddress()); String encodedConstructor = FunctionEncoder.encodeConstructor( Arrays.asList( new Uint256(initialSupply), new Utf8String("web3j tokens"), new Uint8(BigInteger.TEN), new Utf8String("w3j$"))); RawTransaction rawTransaction = RawTransaction.createContractTransaction( nonce, GAS_PRICE, GAS_LIMIT, BigInteger.ZERO, getHumanStandardTokenBinary() + encodedConstructor); byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials); String hexValue = Numeric.toHexString(signedMessage); EthSendTransaction transactionResponse = web3j.ethSendRawTransaction(hexValue) .sendAsync().get(); return transactionResponse.getTransactionHash(); }
@Test public void testDecoding() throws Exception { BigInteger nonce = BigInteger.ZERO; BigInteger gasPrice = BigInteger.ONE; BigInteger gasLimit = BigInteger.TEN; String to = "0x0add5355"; BigInteger value = BigInteger.valueOf(Long.MAX_VALUE); RawTransaction rawTransaction = RawTransaction.createEtherTransaction( nonce, gasPrice, gasLimit, to, value); byte[] encodedMessage = TransactionEncoder.encode(rawTransaction); String hexMessage = Numeric.toHexString(encodedMessage); RawTransaction result = TransactionDecoder.decode(hexMessage); assertNotNull(result); assertEquals(nonce, result.getNonce()); assertEquals(gasPrice, result.getGasPrice()); assertEquals(gasLimit, result.getGasLimit()); assertEquals(to, result.getTo()); assertEquals(value, result.getValue()); assertEquals("", result.getData()); }