Search in sources :

Example 1 with MetadataTransactionFactory

use of io.nem.symbol.sdk.model.transaction.MetadataTransactionFactory in project nem2-sdk-java by nemtech.

the class MetadataTransactionServiceImpl method processMetadata.

/**
 * Generic way of processing a metadata entity and creating a new metadata transaction factory
 * depending on the existing metadata value. This works for Account, Mosaic and Namespace
 * metadata.
 *
 * @param criteria the criteria
 * @param transactionFactory the function that creates a transaction factory
 * @param newValue the new value you want to set.
 * @param <T> the type of the transaction factory.
 * @return an Observable of a transaction factory.
 */
private <T extends MetadataTransactionFactory> Observable<T> processMetadata(MetadataSearchCriteria criteria, TriFunction<byte[], NetworkType, Deadline, T> transactionFactory, String newValue) {
    return Observable.combineLatest(networkTypeObservable, epochAdjustmentObservable, (networkType, epochAdjustment) -> metadataRepository.search(criteria).map(page -> {
        Deadline deadline = Deadline.create(epochAdjustment);
        if (page.getData().isEmpty()) {
            return transactionFactory.apply(StringEncoder.getBytes(newValue), networkType, deadline);
        } else {
            byte[] originalValue = page.getData().get(0).getValue();
            byte[] newValueBytes = StringEncoder.getBytes(newValue);
            byte[] xoredBytes = ConvertUtils.xor(originalValue, newValueBytes);
            T factory = transactionFactory.apply(xoredBytes, networkType, deadline);
            factory.valueSizeDelta(newValueBytes.length - originalValue.length);
            return factory;
        }
    })).flatMap(f -> f);
}
Also used : UnresolvedMosaicId(io.nem.symbol.sdk.model.mosaic.UnresolvedMosaicId) NamespaceId(io.nem.symbol.sdk.model.namespace.NamespaceId) MetadataTransactionService(io.nem.symbol.sdk.api.MetadataTransactionService) MosaicMetadataTransactionFactory(io.nem.symbol.sdk.model.transaction.MosaicMetadataTransactionFactory) NamespaceMetadataTransactionFactory(io.nem.symbol.sdk.model.transaction.NamespaceMetadataTransactionFactory) ConvertUtils(io.nem.symbol.core.utils.ConvertUtils) AccountMetadataTransactionFactory(io.nem.symbol.sdk.model.transaction.AccountMetadataTransactionFactory) RepositoryFactory(io.nem.symbol.sdk.api.RepositoryFactory) MetadataType(io.nem.symbol.sdk.model.metadata.MetadataType) Function(java.util.function.Function) StringEncoder(io.nem.symbol.core.utils.StringEncoder) MetadataRepository(io.nem.symbol.sdk.api.MetadataRepository) Objects(java.util.Objects) Deadline(io.nem.symbol.sdk.model.transaction.Deadline) MetadataTransactionFactory(io.nem.symbol.sdk.model.transaction.MetadataTransactionFactory) Duration(java.time.Duration) AliasService(io.nem.symbol.sdk.api.AliasService) MetadataSearchCriteria(io.nem.symbol.sdk.api.MetadataSearchCriteria) Observable(io.reactivex.Observable) BigInteger(java.math.BigInteger) Address(io.nem.symbol.sdk.model.account.Address) NetworkType(io.nem.symbol.sdk.model.network.NetworkType) Deadline(io.nem.symbol.sdk.model.transaction.Deadline)

Aggregations

ConvertUtils (io.nem.symbol.core.utils.ConvertUtils)1 StringEncoder (io.nem.symbol.core.utils.StringEncoder)1 AliasService (io.nem.symbol.sdk.api.AliasService)1 MetadataRepository (io.nem.symbol.sdk.api.MetadataRepository)1 MetadataSearchCriteria (io.nem.symbol.sdk.api.MetadataSearchCriteria)1 MetadataTransactionService (io.nem.symbol.sdk.api.MetadataTransactionService)1 RepositoryFactory (io.nem.symbol.sdk.api.RepositoryFactory)1 Address (io.nem.symbol.sdk.model.account.Address)1 MetadataType (io.nem.symbol.sdk.model.metadata.MetadataType)1 UnresolvedMosaicId (io.nem.symbol.sdk.model.mosaic.UnresolvedMosaicId)1 NamespaceId (io.nem.symbol.sdk.model.namespace.NamespaceId)1 NetworkType (io.nem.symbol.sdk.model.network.NetworkType)1 AccountMetadataTransactionFactory (io.nem.symbol.sdk.model.transaction.AccountMetadataTransactionFactory)1 Deadline (io.nem.symbol.sdk.model.transaction.Deadline)1 MetadataTransactionFactory (io.nem.symbol.sdk.model.transaction.MetadataTransactionFactory)1 MosaicMetadataTransactionFactory (io.nem.symbol.sdk.model.transaction.MosaicMetadataTransactionFactory)1 NamespaceMetadataTransactionFactory (io.nem.symbol.sdk.model.transaction.NamespaceMetadataTransactionFactory)1 Observable (io.reactivex.Observable)1 BigInteger (java.math.BigInteger)1 Duration (java.time.Duration)1