Search in sources :

Example 1 with NetworkType

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

the class AccountHttp method getAccountsInfo.

@Override
public Observable<List<AccountInfo>> getAccountsInfo(List<Address> addresses) {
    JsonObject requestBody = new JsonObject();
    requestBody.put("addresses", addresses.stream().map(address -> address.plain()).collect(Collectors.toList()));
    Observable<NetworkType> networkTypeResolve = getNetworkTypeObservable();
    return networkTypeResolve.flatMap(networkType -> this.client.postAbs(this.url.toString()).as(BodyCodec.jsonArray()).rxSendJson(requestBody).toObservable().map(HttpResponse::body).map(json -> objectMapper.<List<AccountInfoDTO>>readValue(json.toString(), new TypeReference<List<AccountInfoDTO>>() {
    })).flatMapIterable(item -> item).map(AccountInfoDTO::getAccount).map(accountDTO -> new AccountInfo(Address.createFromRawAddress(accountDTO.getAddressEncoded()), accountDTO.getAddressHeight().extractIntArray(), accountDTO.getPublicKey(), accountDTO.getPublicKeyHeight().extractIntArray(), accountDTO.getImportance().extractIntArray(), accountDTO.getImportanceHeight().extractIntArray(), accountDTO.getMosaics().stream().map(mosaicDTO -> new Mosaic(new MosaicId(mosaicDTO.getId().extractIntArray()), mosaicDTO.getAmount().extractIntArray())).collect(Collectors.toList()))).toList().toObservable());
}
Also used : Mosaic(io.nem.sdk.model.mosaic.Mosaic) HttpResponse(io.vertx.reactivex.ext.web.client.HttpResponse) MalformedURLException(java.net.MalformedURLException) AggregateTransaction(io.nem.sdk.model.transaction.AggregateTransaction) HashMap(java.util.HashMap) NetworkType(io.nem.sdk.model.blockchain.NetworkType) Collectors(java.util.stream.Collectors) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) BodyCodec(io.vertx.reactivex.ext.web.codec.BodyCodec) Function(io.reactivex.functions.Function) io.nem.sdk.model.account(io.nem.sdk.model.account) Map(java.util.Map) Optional(java.util.Optional) Transaction(io.nem.sdk.model.transaction.Transaction) Observable(io.reactivex.Observable) JsonObject(io.vertx.core.json.JsonObject) TypeReference(com.fasterxml.jackson.core.type.TypeReference) MosaicId(io.nem.sdk.model.mosaic.MosaicId) NetworkType(io.nem.sdk.model.blockchain.NetworkType) MosaicId(io.nem.sdk.model.mosaic.MosaicId) JsonObject(io.vertx.core.json.JsonObject) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Mosaic(io.nem.sdk.model.mosaic.Mosaic)

Example 2 with NetworkType

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

the class Listener method open.

/**
 * @return a {@link CompletableFuture} that resolves when the websocket connection is opened
 */
public CompletableFuture<Void> open() {
    HttpClient httpClient = Vertx.vertx().createHttpClient();
    CompletableFuture<Void> future = new CompletableFuture<>();
    if (this.webSocket != null) {
        return CompletableFuture.completedFuture(null);
    }
    RequestOptions requestOptions = new RequestOptions();
    requestOptions.setHost(this.url.getHost());
    requestOptions.setPort(this.url.getPort());
    requestOptions.setURI("/ws");
    httpClient.websocket(requestOptions, webSocket -> {
        this.webSocket = webSocket;
        webSocket.handler(handler -> {
            JsonObject message = handler.toJsonObject();
            if (message.containsKey("uid")) {
                this.UID = message.getString("uid");
                future.complete(null);
            } else if (message.containsKey("transaction")) {
                this.messageSubject.onNext(new ListenerMessage(ListenerChannel.rawValueOf(message.getJsonObject("meta").getString("channelName")), new TransactionMapping().apply(message)));
            } else if (message.containsKey("block")) {
                final JsonObject meta = message.getJsonObject("meta");
                final JsonObject block = message.getJsonObject("block");
                int rawNetworkType = (int) Long.parseLong(Integer.toHexString(block.getInteger("version")).substring(0, 2), 16);
                final NetworkType networkType;
                if (rawNetworkType == NetworkType.MIJIN_TEST.getValue())
                    networkType = NetworkType.MIJIN_TEST;
                else if (rawNetworkType == NetworkType.MIJIN.getValue())
                    networkType = NetworkType.MIJIN;
                else if (rawNetworkType == NetworkType.MAIN_NET.getValue())
                    networkType = NetworkType.MAIN_NET;
                else
                    networkType = NetworkType.TEST_NET;
                final int version = (int) Long.parseLong(Integer.toHexString(block.getInteger("version")).substring(2, 4), 16);
                this.messageSubject.onNext(new ListenerMessage(ListenerChannel.BLOCK, new BlockInfo(meta.getString("hash"), meta.getString("generationHash"), Optional.empty(), Optional.empty(), block.getString("signature"), new PublicAccount(block.getString("signer"), networkType), networkType, version, block.getInteger("type"), extractBigInteger(block.getJsonArray("height")), extractBigInteger(block.getJsonArray("timestamp")), extractBigInteger(block.getJsonArray("difficulty")), block.getString("previousBlockHash"), block.getString("blockTransactionsHash"))));
            } else if (message.containsKey("status")) {
                this.messageSubject.onNext(new ListenerMessage(ListenerChannel.STATUS, new TransactionStatusError(message.getString("hash"), message.getString("status"), new Deadline(extractBigInteger(message.getJsonArray("deadline"))))));
            } else if (message.containsKey("meta")) {
                this.messageSubject.onNext(new ListenerMessage(ListenerChannel.rawValueOf(message.getJsonObject("meta").getString("channelName")), message.getJsonObject("meta").getString("hash")));
            } else if (message.containsKey("parentHash")) {
                this.messageSubject.onNext(new ListenerMessage(ListenerChannel.COSIGNATURE, new CosignatureSignedTransaction(message.getString("parenthash"), message.getString("signature"), message.getString("signer"))));
            }
        });
    });
    return future;
}
Also used : RequestOptions(io.vertx.core.http.RequestOptions) JsonObject(io.vertx.core.json.JsonObject) CompletableFuture(java.util.concurrent.CompletableFuture) NetworkType(io.nem.sdk.model.blockchain.NetworkType) BlockInfo(io.nem.sdk.model.blockchain.BlockInfo) HttpClient(io.vertx.core.http.HttpClient) PublicAccount(io.nem.sdk.model.account.PublicAccount)

Example 3 with NetworkType

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

the class NetworkHttpTest method getNetworkType.

@Test
void getNetworkType() throws ExecutionException, InterruptedException {
    NetworkType networkType = networkHttp.getNetworkType().toFuture().get();
    assertEquals(NetworkType.MIJIN_TEST.getValue(), networkType.getValue());
}
Also used : NetworkType(io.nem.sdk.model.blockchain.NetworkType) Test(org.junit.jupiter.api.Test)

Example 4 with NetworkType

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

the class MosaicHttp method getMosaics.

@Override
public Observable<List<MosaicInfo>> getMosaics(List<MosaicId> mosaicIds) {
    JsonObject requestBody = new JsonObject();
    requestBody.put("mosaicIds", mosaicIds.stream().map(id -> UInt64.bigIntegerToHex(id.getId())).collect(Collectors.toList()));
    Observable<NetworkType> networkTypeResolve = getNetworkTypeObservable();
    return networkTypeResolve.flatMap(networkType -> this.client.postAbs(this.url + "/mosaic").as(BodyCodec.jsonArray()).rxSendJson(requestBody).toObservable().map(HttpResponse::body).map(json -> objectMapper.<List<MosaicInfoDTO>>readValue(json.toString(), new TypeReference<List<MosaicInfoDTO>>() {
    })).flatMapIterable(item -> item).map(mosaicInfoDTO -> new MosaicInfo(mosaicInfoDTO.getMeta().isActive(), mosaicInfoDTO.getMeta().getIndex(), mosaicInfoDTO.getMeta().getId(), new NamespaceId(mosaicInfoDTO.getMosaic().getNamespaceId().extractIntArray()), new MosaicId(mosaicInfoDTO.getMosaic().getMosaicId().extractIntArray()), mosaicInfoDTO.getMosaic().getSupply().extractIntArray(), mosaicInfoDTO.getMosaic().getHeight().extractIntArray(), new PublicAccount(mosaicInfoDTO.getMosaic().getOwner(), networkType), extractMosaicProperties(mosaicInfoDTO.getMosaic().getProperties()))).toList().toObservable());
}
Also used : HttpResponse(io.vertx.reactivex.ext.web.client.HttpResponse) MalformedURLException(java.net.MalformedURLException) MosaicName(io.nem.sdk.model.mosaic.MosaicName) MosaicInfo(io.nem.sdk.model.mosaic.MosaicInfo) NetworkType(io.nem.sdk.model.blockchain.NetworkType) Collectors(java.util.stream.Collectors) UInt64(io.nem.sdk.model.transaction.UInt64) List(java.util.List) PublicAccount(io.nem.sdk.model.account.PublicAccount) BodyCodec(io.vertx.reactivex.ext.web.codec.BodyCodec) MosaicProperties(io.nem.sdk.model.mosaic.MosaicProperties) NamespaceId(io.nem.sdk.model.namespace.NamespaceId) Optional(java.util.Optional) Observable(io.reactivex.Observable) JsonObject(io.vertx.core.json.JsonObject) TypeReference(com.fasterxml.jackson.core.type.TypeReference) MosaicId(io.nem.sdk.model.mosaic.MosaicId) MosaicInfo(io.nem.sdk.model.mosaic.MosaicInfo) NetworkType(io.nem.sdk.model.blockchain.NetworkType) MosaicId(io.nem.sdk.model.mosaic.MosaicId) PublicAccount(io.nem.sdk.model.account.PublicAccount) JsonObject(io.vertx.core.json.JsonObject) HttpResponse(io.vertx.reactivex.ext.web.client.HttpResponse) List(java.util.List) NamespaceId(io.nem.sdk.model.namespace.NamespaceId)

Example 5 with NetworkType

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

the class NamespaceHttp method getNamespacesFromAccounts.

private Observable<List<NamespaceInfo>> getNamespacesFromAccounts(List<Address> addresses, Optional<QueryParams> queryParams) {
    JsonObject requestBody = new JsonObject();
    requestBody.put("addresses", addresses.stream().map((address -> address.plain())).collect(Collectors.toList()));
    Observable<NetworkType> networkTypeResolve = getNetworkTypeObservable();
    return networkTypeResolve.flatMap(networkType -> this.client.postAbs(this.url + "/account/namespaces" + (queryParams.isPresent() ? queryParams.get().toUrl() : "")).as(BodyCodec.jsonArray()).rxSendJson(requestBody).toObservable().map(HttpResponse::body).map(json -> objectMapper.<List<NamespaceInfoDTO>>readValue(json.toString(), new TypeReference<List<NamespaceInfoDTO>>() {
    })).flatMapIterable(item -> item).map(namespaceInfoDTO -> new NamespaceInfo(namespaceInfoDTO.getMeta().isActive(), namespaceInfoDTO.getMeta().getIndex(), namespaceInfoDTO.getMeta().getId(), NamespaceType.rawValueOf(namespaceInfoDTO.getNamespace().getType()), namespaceInfoDTO.getNamespace().getDepth(), extractLevels(namespaceInfoDTO), new NamespaceId(namespaceInfoDTO.getNamespace().getParentId().extractIntArray()), new PublicAccount(namespaceInfoDTO.getNamespace().getOwner(), networkType), namespaceInfoDTO.getNamespace().getStartHeight().extractIntArray(), namespaceInfoDTO.getNamespace().getEndHeight().extractIntArray())).toList().toObservable());
}
Also used : NamespaceInfo(io.nem.sdk.model.namespace.NamespaceInfo) NamespaceName(io.nem.sdk.model.namespace.NamespaceName) HttpResponse(io.vertx.reactivex.ext.web.client.HttpResponse) MalformedURLException(java.net.MalformedURLException) NamespaceType(io.nem.sdk.model.namespace.NamespaceType) NetworkType(io.nem.sdk.model.blockchain.NetworkType) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) UInt64(io.nem.sdk.model.transaction.UInt64) List(java.util.List) PublicAccount(io.nem.sdk.model.account.PublicAccount) BodyCodec(io.vertx.reactivex.ext.web.codec.BodyCodec) Address(io.nem.sdk.model.account.Address) NamespaceId(io.nem.sdk.model.namespace.NamespaceId) Optional(java.util.Optional) Observable(io.reactivex.Observable) JsonObject(io.vertx.core.json.JsonObject) TypeReference(com.fasterxml.jackson.core.type.TypeReference) NetworkType(io.nem.sdk.model.blockchain.NetworkType) PublicAccount(io.nem.sdk.model.account.PublicAccount) JsonObject(io.vertx.core.json.JsonObject) HttpResponse(io.vertx.reactivex.ext.web.client.HttpResponse) ArrayList(java.util.ArrayList) List(java.util.List) NamespaceId(io.nem.sdk.model.namespace.NamespaceId) NamespaceInfo(io.nem.sdk.model.namespace.NamespaceInfo)

Aggregations

NetworkType (io.nem.sdk.model.blockchain.NetworkType)5 JsonObject (io.vertx.core.json.JsonObject)4 TypeReference (com.fasterxml.jackson.core.type.TypeReference)3 PublicAccount (io.nem.sdk.model.account.PublicAccount)3 Observable (io.reactivex.Observable)3 HttpResponse (io.vertx.reactivex.ext.web.client.HttpResponse)3 BodyCodec (io.vertx.reactivex.ext.web.codec.BodyCodec)3 MalformedURLException (java.net.MalformedURLException)3 List (java.util.List)3 Optional (java.util.Optional)3 Collectors (java.util.stream.Collectors)3 MosaicId (io.nem.sdk.model.mosaic.MosaicId)2 NamespaceId (io.nem.sdk.model.namespace.NamespaceId)2 UInt64 (io.nem.sdk.model.transaction.UInt64)2 io.nem.sdk.model.account (io.nem.sdk.model.account)1 Address (io.nem.sdk.model.account.Address)1 BlockInfo (io.nem.sdk.model.blockchain.BlockInfo)1 Mosaic (io.nem.sdk.model.mosaic.Mosaic)1 MosaicInfo (io.nem.sdk.model.mosaic.MosaicInfo)1 MosaicName (io.nem.sdk.model.mosaic.MosaicName)1