org.web3j.crypto.Hash Java Examples

The following examples show how to use org.web3j.crypto.Hash. 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 web3j with Apache License 2.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: SignTransactionIT.java    From etherscan-explorer with GNU General Public License v3.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 #5
Source File: TransactionMonitoringSpec.java    From eventeum with Apache License 2.0 6 votes vote down vote up
public TransactionMonitoringSpec(TransactionIdentifierType type,
                                 String transactionIdentifierValue,
                                 String nodeName,
                                 List<TransactionStatus> statuses) {
    this.type = type;
    this.transactionIdentifierValue = transactionIdentifierValue;
    this.nodeName = nodeName;

    if (statuses != null && !statuses.isEmpty()) {
        this.statuses = statuses;
    }

    convertToCheckSum();

    this.id = Hash.sha3String(transactionIdentifierValue + type + nodeName + this.statuses.toString()).substring(2);
}
 
Example #6
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 #7
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 #8
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 #9
Source File: PrivacyGroupUtils.java    From web3j with Apache License 2.0 5 votes vote down vote up
public static Base64String generateLegacyGroup(
        final Base64String privateFrom, final List<Base64String> privateFor) {
    final List<byte[]> stringList = new ArrayList<>();
    stringList.add(Base64.getDecoder().decode(privateFrom.toString()));
    privateFor.forEach(item -> stringList.add(item.raw()));

    final List<RlpType> rlpList =
            stringList.stream()
                    .distinct()
                    .sorted(Comparator.comparing(Arrays::hashCode))
                    .map(RlpString::create)
                    .collect(Collectors.toList());

    return Base64String.wrap(Hash.sha3(RlpEncoder.encode(new RlpList(rlpList))));
}
 
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: FunctionReturnDecoderTest.java    From web3j 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);

    assertEquals(
            FunctionReturnDecoder.decodeIndexedValue(
                    hash, new TypeReference<DynamicBytes>() {}),
            (new Bytes32(Numeric.hexStringToByteArray(hash))));
}
 
Example #12
Source File: FunctionReturnDecoderTest.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecodeIndexedStringValue() {
    Utf8String string = new Utf8String("some text");
    String encoded = TypeEncoder.encodeString(string);
    String hash = Hash.sha3(encoded);

    assertEquals(
            FunctionReturnDecoder.decodeIndexedValue(hash, new TypeReference<Utf8String>() {}),
            (new Bytes32(Numeric.hexStringToByteArray(hash))));
}
 
Example #13
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 #14
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 #15
Source File: FunctionReturnDecoderTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testDecodeIndexedStringValue() {
    Utf8String string = new Utf8String("some text");
    String encoded = TypeEncoder.encodeString(string);
    String hash = Hash.sha3(encoded);

    assertThat(FunctionReturnDecoder.decodeIndexedValue(
            hash,
            new TypeReference<Utf8String>() {}),
            equalTo(new Bytes32(Numeric.hexStringToByteArray(hash))));
}
 
Example #16
Source File: FunctionReturnDecoderTest.java    From etherscan-explorer with GNU General Public License v3.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 #17
Source File: FunctionReturnDecoderTest.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecodeIndexedStringValue() {
    Utf8String string = new Utf8String("some text");
    String encoded = TypeEncoder.encodeString(string);
    String hash = Hash.sha3(encoded);

    assertThat(FunctionReturnDecoder.decodeIndexedValue(
            hash,
            new TypeReference<Utf8String>() {}),
            equalTo(new Bytes32(Numeric.hexStringToByteArray(hash))));
}
 
Example #18
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 #19
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 #20
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 #21
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 #22
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 #23
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 #24
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 #25
Source File: EventEncoder.java    From web3j 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 #26
Source File: TransactionDecoder.java    From alpha-wallet-android with MIT License 4 votes vote down vote up
public static String buildMethodId(String methodSignature) {
    byte[] input = methodSignature.getBytes();
    byte[] hash = Hash.sha3(input);
    return Numeric.toHexString(hash).substring(0, 10);
}
 
Example #27
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 #28
Source File: TransactionMonitoringSpec.java    From eventeum with Apache License 2.0 4 votes vote down vote up
public void generateId() {
    this.id = Hash.sha3String(transactionIdentifierValue + type + nodeName + statuses.toString()).substring(2);
}
 
Example #29
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 #30
Source File: EventeumEventConsumingIT.java    From eventeum with Apache License 2.0 4 votes vote down vote up
@Test
public void testTxMonitorRemovedEventRemovesMonitor() throws Exception {

    final String signedTxHex = createRawSignedTransactionHex();
    final String txHash = Hash.sha3(signedTxHex);

    final TransactionMonitoringSpec spec = new TransactionMonitoringSpec();
    spec.setNodeName("default");
    spec.setTransactionIdentifierValue(txHash);
    spec.setType(TransactionIdentifierType.HASH);

    broadcaster.broadcastTransactionMonitorAdded(spec);

    waitForTransactionMonitorEventMessages(1);

    broadcaster.broadcastTransactionMonitorRemoved(spec);

    waitForTransactionMonitorEventMessages(1);

    assertEquals(txHash, sendRawTransaction(signedTxHex));

    waitForBroadcast();

    assertEquals(0, getBroadcastTransactionMessages().size());
}