
io.nem.sdk.infrastructure.AliasServiceImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sdk-core Show documentation
Show all versions of sdk-core Show documentation
sdk-core lib for NEM2 sdk Java
The newest version!
/*
* Copyright 2019 NEM
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.nem.sdk.infrastructure;
import io.nem.sdk.api.AliasService;
import io.nem.sdk.api.NamespaceRepository;
import io.nem.sdk.api.RepositoryFactory;
import io.nem.sdk.model.account.Address;
import io.nem.sdk.model.account.UnresolvedAddress;
import io.nem.sdk.model.mosaic.MosaicId;
import io.nem.sdk.model.mosaic.UnresolvedMosaicId;
import io.nem.sdk.model.namespace.AliasType;
import io.nem.sdk.model.namespace.NamespaceId;
import io.reactivex.Observable;
import org.apache.commons.lang3.Validate;
/**
* Implementation of the alias service.
*/
public class AliasServiceImpl implements AliasService {
private final NamespaceRepository namespaceRepository;
/**
* Constructor
*
* @param repositoryFactory repository factory.
*/
public AliasServiceImpl(RepositoryFactory repositoryFactory) {
this.namespaceRepository = repositoryFactory.createNamespaceRepository();
}
@Override
public Observable resolveMosaicId(UnresolvedMosaicId unresolvedMosaicId) {
if (unresolvedMosaicId.isAlias()) {
NamespaceId alias = (NamespaceId) unresolvedMosaicId;
return namespaceRepository.getNamespace(alias)
.map(namespaceInfo -> {
Validate.isTrue(namespaceInfo.getAlias().getType() == AliasType.MOSAIC,
"Alias is not Mosaic");
return (MosaicId) namespaceInfo.getAlias().getAliasValue();
})
.onErrorResumeNext(e -> {
return Observable.error(new IllegalArgumentException(
"MosaicId could not be resolved from alias " + alias.getIdAsHex(), e));
});
} else {
return Observable.just((MosaicId) unresolvedMosaicId);
}
}
@Override
public Observable resolveAddress(UnresolvedAddress unresolvedAddress) {
if (unresolvedAddress.isAlias()) {
NamespaceId alias = (NamespaceId) unresolvedAddress;
return namespaceRepository.getNamespace(alias)
.map(namespaceInfo -> {
Validate.isTrue(namespaceInfo.getAlias().getType() == AliasType.ADDRESS,
"Alias is not address");
return (Address) namespaceInfo.getAlias().getAliasValue();
})
.onErrorResumeNext(e -> {
return Observable.error(new IllegalArgumentException(
"Address could not be resolved from alias " + alias.getIdAsHex(), e));
});
} else {
return Observable.just((Address) unresolvedAddress);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy