Java Code Examples for org.web3j.crypto.Hash#sha3()

The following examples show how to use org.web3j.crypto.Hash#sha3() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NameHash.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
private static byte[] nameHash(String[] labels) {
    if (labels.length == 0 || labels[0].equals("")) {
        return EMPTY;
    } else {
        String[] tail;
        if (labels.length == 1) {
            tail = new String[] { };
        } else {
            tail = Arrays.copyOfRange(labels, 1, labels.length);
        }

        byte[] remainderHash = nameHash(tail);
        byte[] result = Arrays.copyOf(remainderHash, 64);

        byte[] labelHash = Hash.sha3(labels[0].getBytes(StandardCharsets.UTF_8));
        System.arraycopy(labelHash, 0, result, 32, labelHash.length);

        return Hash.sha3(result);
    }
}
 
Example 2
Source File: SignTransactionIT.java    From web3j with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example 3
Source File: RawTransactionManager.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
public PlatonSendTransaction signAndSend(RawTransaction rawTransaction)
        throws IOException {

    byte[] signedMessage;

    if (chainId > ChainId.NONE) {
        signedMessage = TransactionEncoder.signMessage(rawTransaction, chainId, credentials);
    } else {
        signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
    }

    String hexValue = Numeric.toHexString(signedMessage);
    PlatonSendTransaction ethSendTransaction = web3j.platonSendRawTransaction(hexValue).send();

    if (ethSendTransaction != null && !ethSendTransaction.hasError()) {
        String txHashLocal = Hash.sha3(hexValue);
        String txHashRemote = ethSendTransaction.getTransactionHash();
        if (!txHashVerifier.verify(txHashLocal, txHashRemote)) {
            throw new TxHashMismatchException(txHashLocal, txHashRemote);
        }
    }

    return ethSendTransaction;
}
 
Example 4
Source File: LWallet.java    From dapp-wallet-demo with Apache License 2.0 5 votes vote down vote up
private static byte[] generateMac(byte[] derivedKey, byte[] cipherText) {
    byte[] result = new byte[16 + cipherText.length];

    System.arraycopy(derivedKey, 16, result, 0, 16);
    System.arraycopy(cipherText, 0, result, 16, cipherText.length);

    return Hash.sha3(result);
}
 
Example 5
Source File: TransactionMonitorIT.java    From eventeum with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadFilterFromConfig() throws Exception {
    final String rawTx = createRawSignedTransactionHex(TO_ADDRESS);
    final String txHash = Hash.sha3(rawTx);

    sendRawTransaction(rawTx);

    waitForTransactionMessages(1);

    assertEquals(1, getBroadcastTransactionMessages().size());

    final TransactionDetails txDetails = getBroadcastTransactionMessages().get(0);
    assertEquals(txHash, txDetails.getHash());
    assertEquals(TransactionStatus.UNCONFIRMED, txDetails.getStatus());
}
 
Example 6
Source File: FunctionReturnDecoderTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testDecodeIndexedDynamicArrayValue() {
    DynamicArray<Uint256> array = new DynamicArray<>(new Uint256(BigInteger.TEN));
    String encoded = TypeEncoder.encodeDynamicArray(array);
    String hash = Hash.sha3(encoded);

    assertThat(FunctionReturnDecoder.decodeIndexedValue(
            hash,
            new TypeReference<DynamicArray>() {}),
            equalTo(new Bytes32(Numeric.hexStringToByteArray(hash))));
}
 
Example 7
Source File: FunctionReturnDecoderTest.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecodeIndexedDynamicBytesValue() {
    DynamicBytes bytes = new DynamicBytes(new byte[]{ 1, 2, 3, 4, 5});
    String encoded = TypeEncoder.encodeDynamicBytes(bytes);
    String hash = Hash.sha3(encoded);

    assertThat(FunctionReturnDecoder.decodeIndexedValue(
            hash,
            new TypeReference<DynamicBytes>() {}),
            equalTo(new Bytes32(Numeric.hexStringToByteArray(hash))));
}
 
Example 8
Source File: FunctionReturnDecoderTest.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecodeIndexedDynamicArrayValue() {
    DynamicArray<Uint256> array = new DynamicArray<>(new Uint256(BigInteger.TEN));
    String encoded = TypeEncoder.encodeDynamicArray(array);
    String hash = Hash.sha3(encoded);

    assertThat(FunctionReturnDecoder.decodeIndexedValue(
            hash,
            new TypeReference<DynamicArray>() {}),
            equalTo(new Bytes32(Numeric.hexStringToByteArray(hash))));
}
 
Example 9
Source File: RawTransactionManager.java    From web3j with Apache License 2.0 5 votes vote down vote up
public EthSendTransaction signAndSend(RawTransaction rawTransaction) throws IOException {
    String hexValue = sign(rawTransaction);
    EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).send();

    if (ethSendTransaction != null && !ethSendTransaction.hasError()) {
        String txHashLocal = Hash.sha3(hexValue);
        String txHashRemote = ethSendTransaction.getTransactionHash();
        if (!txHashVerifier.verify(txHashLocal, txHashRemote)) {
            throw new TxHashMismatchException(txHashLocal, txHashRemote);
        }
    }

    return ethSendTransaction;
}
 
Example 10
Source File: FunctionReturnDecoderTest.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecodeIndexedDynamicArrayValue() {
    DynamicArray<Uint256> array =
            new DynamicArray<>(Uint256.class, new Uint256(BigInteger.TEN));

    String encoded = TypeEncoder.encodeDynamicArray(array);
    String hash = Hash.sha3(encoded);

    assertEquals(
            FunctionReturnDecoder.decodeIndexedValue(
                    hash, new TypeReference<DynamicArray>() {}),
            (new Bytes32(Numeric.hexStringToByteArray(hash))));
}
 
Example 11
Source File: WasmEventEncoder.java    From client-sdk-java with Apache License 2.0 4 votes vote down vote up
public static String encode(WasmEvent event) {
	byte[] hash = Hash.sha3(RLPCodec.encode(event.getName()));
	return Numeric.toHexString(hash);
}
 
Example 12
Source File: FunctionEncoder.java    From web3j with Apache License 2.0 4 votes vote down vote up
protected static String buildMethodId(final String methodSignature) {
    final byte[] input = methodSignature.getBytes();
    final byte[] hash = Hash.sha3(input);
    return Numeric.toHexString(hash).substring(0, 10);
}
 
Example 13
Source File: FunctionEncoder.java    From etherscan-explorer with GNU General Public License v3.0 4 votes vote down vote up
static String buildMethodId(String methodSignature) {
    byte[] input = methodSignature.getBytes();
    byte[] hash = Hash.sha3(input);
    return Numeric.toHexString(hash).substring(0, 10);
}
 
Example 14
Source File: EventEncoder.java    From etherscan-explorer with GNU General Public License v3.0 4 votes vote down vote up
public static String buildEventSignature(String methodSignature) {
    byte[] input = methodSignature.getBytes();
    byte[] hash = Hash.sha3(input);
    return Numeric.toHexString(hash);
}
 
Example 15
Source File: NodeIdTool.java    From client-sdk-java with Apache License 2.0 4 votes vote down vote up
private static byte[] getMsgHash(PlatonBlock.Block block) {
    byte[] signData = encode(block);
    return Hash.sha3(signData);
}
 
Example 16
Source File: FunctionEncoder.java    From client-sdk-java with Apache License 2.0 4 votes vote down vote up
static String buildMethodId(String methodSignature) {
    byte[] input = methodSignature.getBytes();
    byte[] hash = Hash.sha3(input);
    return Numeric.toHexString(hash).substring(0, 10);
}
 
Example 17
Source File: EventEncoder.java    From client-sdk-java with Apache License 2.0 4 votes vote down vote up
public static String buildEventSignature(String methodSignature) {
	byte[] input = methodSignature.getBytes();
	byte[] hash = Hash.sha3(input);
	return Numeric.toHexString(hash);
}
 
Example 18
Source File: WasmEventEncoder.java    From client-sdk-java with Apache License 2.0 4 votes vote down vote up
public static String encodeIndexParameter(Object o) {
	byte[] hash = Hash.sha3(RLPCodec.encode(o));
	return Numeric.toHexString(hash);
}
 
Example 19
Source File: PlatOnEventEncoder.java    From client-sdk-java with Apache License 2.0 4 votes vote down vote up
private static String buildEventSignature(String methodSignature) {
    byte[] input = methodSignature.getBytes();
    byte[] hash = Hash.sha3(input);
    return Numeric.toHexString(hash);
}
 
Example 20
Source File: ServiceRestartRecoveryTests.java    From eventeum with Apache License 2.0 2 votes vote down vote up
protected void doBroadcastTransactionUnconfirmedAfterFailureTest() throws Exception {

        triggerBlocks(1);

        waitForBlockMessages(1);

        //We're going to send 10 transactions in front to trigger blocks so nonce should be 10 higher
        final BigInteger nonce = getNonce().add(BigInteger.TEN);

        final String signedHex = createRawSignedTransactionHex(nonce);

        final String txHash = Hash.sha3(signedHex);

        TransactionMonitoringSpec monitorSpec = new TransactionMonitoringSpec(TransactionIdentifierType.HASH, txHash, Constants.DEFAULT_NODE_NAME);

        monitorTransaction(monitorSpec);

        txRepo.findAll();

        restartEventeum(() -> {
            try {
                triggerBlocks(10);
                final String actualTxHash = sendRawTransaction(signedHex);
                assertEquals(txHash, actualTxHash);
                waitForBroadcast();

                triggerBlocks(10);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        txRepo.findAll();

        waitForTransactionMessages(1);

        assertEquals(1, getBroadcastTransactionMessages().size());

        final TransactionDetails txDetails = getBroadcastTransactionMessages().get(0);
        assertEquals(txHash, txDetails.getHash());
        assertEquals(TransactionStatus.UNCONFIRMED, txDetails.getStatus());
    }