Search in sources :

Example 1 with MerkleHashBuilder

use of io.nem.symbol.core.crypto.MerkleHashBuilder in project nem2-sdk-java by nemtech.

the class AggregateTransactionFactory method calculateTransactionsHash.

/**
 * It generates the hash of the transactions that are going to be included in the {@link
 * AggregateTransaction}
 *
 * @param transactions the inner transaction
 * @return the added transaction hash.
 */
private static String calculateTransactionsHash(final List<Transaction> transactions) {
    final MerkleHashBuilder transactionsHashBuilder = new MerkleHashBuilder();
    final BinarySerializationImpl transactionSerialization = new BinarySerializationImpl();
    Hasher hasher = Hashes::sha3_256;
    for (final Transaction transaction : transactions) {
        final byte[] bytes = transactionSerialization.serializeEmbedded(transaction);
        byte[] transactionHash = hasher.hash(bytes);
        transactionsHashBuilder.update(transactionHash);
    }
    final byte[] hash = transactionsHashBuilder.getRootHash();
    return ConvertUtils.toHex(hash);
}
Also used : Hasher(io.nem.symbol.core.crypto.Hasher) MerkleHashBuilder(io.nem.symbol.core.crypto.MerkleHashBuilder) BinarySerializationImpl(io.nem.symbol.sdk.infrastructure.BinarySerializationImpl)

Aggregations

Hasher (io.nem.symbol.core.crypto.Hasher)1 MerkleHashBuilder (io.nem.symbol.core.crypto.MerkleHashBuilder)1 BinarySerializationImpl (io.nem.symbol.sdk.infrastructure.BinarySerializationImpl)1