Search in sources :

Example 11 with BinarySerializationImpl

use of io.nem.symbol.sdk.infrastructure.BinarySerializationImpl 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

BinarySerializationImpl (io.nem.symbol.sdk.infrastructure.BinarySerializationImpl)11 Test (org.junit.jupiter.api.Test)8 PlainMessage (io.nem.symbol.sdk.model.message.PlainMessage)6 Address (io.nem.symbol.sdk.model.account.Address)5 Mosaic (io.nem.symbol.sdk.model.mosaic.Mosaic)5 MosaicId (io.nem.symbol.sdk.model.mosaic.MosaicId)5 BinarySerialization (io.nem.symbol.sdk.api.BinarySerialization)4 BigInteger (java.math.BigInteger)4 NetworkType (io.nem.symbol.sdk.model.network.NetworkType)3 PublicAccount (io.nem.symbol.sdk.model.account.PublicAccount)2 Transaction (io.nem.symbol.sdk.model.transaction.Transaction)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)2 AggregateBondedTransactionBuilder (io.nem.symbol.catapult.builders.AggregateBondedTransactionBuilder)1 TransactionBuilder (io.nem.symbol.catapult.builders.TransactionBuilder)1 Hasher (io.nem.symbol.core.crypto.Hasher)1 KeyPair (io.nem.symbol.core.crypto.KeyPair)1 MerkleHashBuilder (io.nem.symbol.core.crypto.MerkleHashBuilder)1