Search in sources :

Example 21 with NetworkType

use of io.nem.symbol.sdk.model.network.NetworkType 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)

Example 22 with NetworkType

use of io.nem.symbol.sdk.model.network.NetworkType in project nem2-sdk-java by nemtech.

the class AccountOperationRestrictionTransactionMapper method createFactory.

@Override
protected AccountOperationRestrictionTransactionFactory createFactory(NetworkType networkType, Deadline deadline, AccountOperationRestrictionTransactionDTO transaction) {
    AccountOperationRestrictionFlags restrictionFlags = AccountOperationRestrictionFlags.rawValueOf(transaction.getRestrictionFlags().getValue());
    List<TransactionType> additions = transaction.getRestrictionAdditions().stream().map(transactionTypeEnum -> TransactionType.rawValueOf(transactionTypeEnum.getValue())).collect(Collectors.toList());
    List<TransactionType> deletions = transaction.getRestrictionDeletions().stream().map(transactionTypeEnum -> TransactionType.rawValueOf(transactionTypeEnum.getValue())).collect(Collectors.toList());
    return AccountOperationRestrictionTransactionFactory.create(networkType, deadline, restrictionFlags, additions, deletions);
}
Also used : AccountRestrictionFlagsEnum(io.nem.symbol.sdk.openapi.okhttp_gson.model.AccountRestrictionFlagsEnum) Deadline(io.nem.symbol.sdk.model.transaction.Deadline) List(java.util.List) AccountOperationRestrictionTransactionDTO(io.nem.symbol.sdk.openapi.okhttp_gson.model.AccountOperationRestrictionTransactionDTO) JsonHelper(io.nem.symbol.sdk.model.transaction.JsonHelper) AccountOperationRestrictionTransaction(io.nem.symbol.sdk.model.transaction.AccountOperationRestrictionTransaction) TransactionType(io.nem.symbol.sdk.model.transaction.TransactionType) AccountOperationRestrictionFlags(io.nem.symbol.sdk.model.transaction.AccountOperationRestrictionFlags) NetworkType(io.nem.symbol.sdk.model.network.NetworkType) TransactionTypeEnum(io.nem.symbol.sdk.openapi.okhttp_gson.model.TransactionTypeEnum) Collectors(java.util.stream.Collectors) AccountOperationRestrictionTransactionFactory(io.nem.symbol.sdk.model.transaction.AccountOperationRestrictionTransactionFactory) AccountOperationRestrictionFlags(io.nem.symbol.sdk.model.transaction.AccountOperationRestrictionFlags) TransactionType(io.nem.symbol.sdk.model.transaction.TransactionType)

Example 23 with NetworkType

use of io.nem.symbol.sdk.model.network.NetworkType in project nem2-sdk-java by nemtech.

the class Config method loadNemesisAccountsFromBootstrap.

private static List<Account> loadNemesisAccountsFromBootstrap(NetworkType networkType, File generatedAddresses) {
    ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    try {
        if (!generatedAddresses.exists()) {
            System.out.println("Generated addresses could not be found in " + generatedAddresses.getAbsolutePath() + " Nemesis address must bue added manually");
            return Collections.emptyList();
        }
        List<Map<String, String>> bootstrapAddresses;
        List<Map<String, List<Map<String, String>>>> mosaics = (List<Map<String, List<Map<String, String>>>>) mapper.readValue(generatedAddresses, Map.class).get("mosaics");
        bootstrapAddresses = mosaics.get(0).get("accounts");
        return bootstrapAddresses.stream().map(m -> Account.createFromPrivateKey(m.get("privateKey"), networkType)).collect(Collectors.toList());
    } catch (Exception e) {
        throw new IllegalStateException("Nemesis account could not be loaded from Bootstrap: " + ExceptionUtils.getMessage(e), e);
    }
}
Also used : ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Account(io.nem.symbol.sdk.model.account.Account) IOException(java.io.IOException) HashMap(java.util.HashMap) FileInputStream(java.io.FileInputStream) Collectors(java.util.stream.Collectors) File(java.io.File) IOUtils(org.apache.commons.io.IOUtils) Deadline(io.nem.symbol.sdk.model.transaction.Deadline) List(java.util.List) Map(java.util.Map) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) Optional(java.util.Optional) JsonObject(io.vertx.core.json.JsonObject) NetworkType(io.nem.symbol.sdk.model.network.NetworkType) Collections(java.util.Collections) InputStream(java.io.InputStream) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException)

Example 24 with NetworkType

use of io.nem.symbol.sdk.model.network.NetworkType in project nem2-sdk-java by nemtech.

the class NetworkRepositoryVertxImplTest method shouldGetNetworkType.

@Test
void shouldGetNetworkType() throws Exception {
    NodeInfoDTO dto = new NodeInfoDTO();
    dto.setNetworkIdentifier(NetworkType.MIJIN_TEST.getValue());
    mockRemoteCall(dto);
    NetworkType info = repository.getNetworkType().toFuture().get();
    Assertions.assertNotNull(info);
    Assertions.assertEquals(NetworkType.MIJIN_TEST, info);
}
Also used : NetworkType(io.nem.symbol.sdk.model.network.NetworkType) NodeInfoDTO(io.nem.symbol.sdk.openapi.vertx.model.NodeInfoDTO) Test(org.junit.jupiter.api.Test)

Example 25 with NetworkType

use of io.nem.symbol.sdk.model.network.NetworkType in project nem2-sdk-java by nemtech.

the class AccountOperationRestrictionTransactionMapper method createFactory.

@Override
protected AccountOperationRestrictionTransactionFactory createFactory(NetworkType networkType, Deadline deadline, AccountOperationRestrictionTransactionDTO transaction) {
    AccountOperationRestrictionFlags restrictionFlags = AccountOperationRestrictionFlags.rawValueOf(transaction.getRestrictionFlags().getValue());
    List<TransactionType> additions = transaction.getRestrictionAdditions().stream().map(transactionTypeEnum -> TransactionType.rawValueOf(transactionTypeEnum.getValue())).collect(Collectors.toList());
    List<TransactionType> deletions = transaction.getRestrictionDeletions().stream().map(transactionTypeEnum -> TransactionType.rawValueOf(transactionTypeEnum.getValue())).collect(Collectors.toList());
    return AccountOperationRestrictionTransactionFactory.create(networkType, deadline, restrictionFlags, additions, deletions);
}
Also used : Deadline(io.nem.symbol.sdk.model.transaction.Deadline) List(java.util.List) AccountRestrictionFlagsEnum(io.nem.symbol.sdk.openapi.vertx.model.AccountRestrictionFlagsEnum) AccountOperationRestrictionTransactionDTO(io.nem.symbol.sdk.openapi.vertx.model.AccountOperationRestrictionTransactionDTO) JsonHelper(io.nem.symbol.sdk.model.transaction.JsonHelper) AccountOperationRestrictionTransaction(io.nem.symbol.sdk.model.transaction.AccountOperationRestrictionTransaction) TransactionType(io.nem.symbol.sdk.model.transaction.TransactionType) AccountOperationRestrictionFlags(io.nem.symbol.sdk.model.transaction.AccountOperationRestrictionFlags) NetworkType(io.nem.symbol.sdk.model.network.NetworkType) TransactionTypeEnum(io.nem.symbol.sdk.openapi.vertx.model.TransactionTypeEnum) Collectors(java.util.stream.Collectors) AccountOperationRestrictionTransactionFactory(io.nem.symbol.sdk.model.transaction.AccountOperationRestrictionTransactionFactory) AccountOperationRestrictionFlags(io.nem.symbol.sdk.model.transaction.AccountOperationRestrictionFlags) TransactionType(io.nem.symbol.sdk.model.transaction.TransactionType)

Aggregations

NetworkType (io.nem.symbol.sdk.model.network.NetworkType)31 Test (org.junit.jupiter.api.Test)13 Deadline (io.nem.symbol.sdk.model.transaction.Deadline)11 BigInteger (java.math.BigInteger)10 Address (io.nem.symbol.sdk.model.account.Address)9 MosaicId (io.nem.symbol.sdk.model.mosaic.MosaicId)7 Account (io.nem.symbol.sdk.model.account.Account)5 PublicAccount (io.nem.symbol.sdk.model.account.PublicAccount)5 UnresolvedAddress (io.nem.symbol.sdk.model.account.UnresolvedAddress)5 PlainMessage (io.nem.symbol.sdk.model.message.PlainMessage)5 Mosaic (io.nem.symbol.sdk.model.mosaic.Mosaic)5 Duration (java.time.Duration)5 TransactionType (io.nem.symbol.sdk.model.transaction.TransactionType)4 List (java.util.List)4 Collectors (java.util.stream.Collectors)4 BinarySerializationImpl (io.nem.symbol.sdk.infrastructure.BinarySerializationImpl)3 UnresolvedMosaicId (io.nem.symbol.sdk.model.mosaic.UnresolvedMosaicId)3 TransferTransaction (io.nem.symbol.sdk.model.transaction.TransferTransaction)3 Optional (java.util.Optional)3 ConvertUtils (io.nem.symbol.core.utils.ConvertUtils)2