All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.proximax.sdk.infrastructure.TransactionHttp Maven / Gradle / Ivy

Go to download

The ProximaX Sirius Chain Java SDK is a Java library for interacting with the Sirius Blockchain.

The newest version!
/*
 * Copyright 2018 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.proximax.sdk.infrastructure;

import static io.proximax.sdk.utils.GsonUtils.getJsonArray;
import static io.proximax.sdk.utils.GsonUtils.getJsonPrimitive;
import static io.proximax.sdk.utils.GsonUtils.stream;
import static io.proximax.sdk.utils.dto.UInt64Utils.toBigInt;

import java.lang.reflect.Type;
import java.util.List;
import java.util.stream.Collectors;

import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import io.proximax.sdk.BlockchainApi;
import io.proximax.sdk.TransactionRepository;
import io.proximax.sdk.gen.model.TransactionStatusDTO;
import io.proximax.sdk.model.transaction.CosignatureSignedTransaction;
import io.proximax.sdk.model.transaction.DeadlineRaw;
import io.proximax.sdk.model.transaction.SignedTransaction;
import io.proximax.sdk.model.transaction.Transaction;
import io.proximax.sdk.model.transaction.TransactionAnnounceResponse;
import io.proximax.sdk.model.transaction.TransactionStatus;
import io.proximax.sdk.utils.GsonUtils;
import io.reactivex.Observable;

/**
 * Transaction http repository.
 *
 * @since 1.0
 */
public class TransactionHttp extends Http implements TransactionRepository {
   private static final String ROUTE = "/transaction";
   private static final String KEY_MESSAGE = "message";
   private static final String KEY_PAYLOAD = "payload";
	
   private static final Type TRANSACTION_STATUS_LIST_TYPE = new TypeToken>(){}.getType();

    public TransactionHttp(BlockchainApi api) {
        super(api);
    }

    @Override
    public Observable getTransaction(String transactionHash) {
        return this.client
                .get(ROUTE + SLASH + transactionHash)
                .map(Http::mapStringOrError)
                .map(GsonUtils::mapToJsonObject)
                .map(new TransactionMapping());
    }

    @Override
    public Observable> getTransactions(List transactionHashes) {

       JsonObject requestBody = new JsonObject();
       requestBody.add("transactionIds", getJsonArray(transactionHashes));
        return this.client
                .post(ROUTE, requestBody)
                .map(Http::mapStringOrError)
                .map(GsonUtils::mapToJsonArray)
                .map(arr -> stream(arr).map(s -> (JsonObject) s).collect(Collectors.toList()))
                .flatMapIterable(item -> item)
                .map(new TransactionMapping())
                .toList()
                .toObservable();
    }


    @Override
    public Observable getTransactionStatus(String transactionHash) {
        return this.client
                .get(ROUTE + SLASH + transactionHash + "/status")
                .map(Http::mapStringOrError)
                .map(str -> gson.fromJson(str, TransactionStatusDTO.class))
                .map(transactionStatusDTO -> new TransactionStatus(transactionStatusDTO.getGroup(),
                        transactionStatusDTO.getStatus(),
                        transactionStatusDTO.getHash(),
                        new DeadlineRaw(toBigInt(transactionStatusDTO.getDeadline())),
                        toBigInt(transactionStatusDTO.getHeight())));
    }

    @Override
    public Observable> getTransactionStatuses(List transactionHashes) {
        JsonObject requestBody = new JsonObject();
        requestBody.add("hashes", getJsonArray(transactionHashes));
        return this.client
                .post(ROUTE + "/statuses", requestBody)
                .map(Http::mapStringOrError)
                .map(this::toTransactionStatusList)
                .flatMapIterable(item -> item)
                .map(transactionStatusDTO -> new TransactionStatus(transactionStatusDTO.getGroup(),
                        transactionStatusDTO.getStatus(),
                        transactionStatusDTO.getHash(),
                        new DeadlineRaw(toBigInt(transactionStatusDTO.getDeadline())),
                        toBigInt(transactionStatusDTO.getHeight())))
                .toList()
                .toObservable();
    }

    @Override
    public Observable announce(SignedTransaction signedTransaction) {
        JsonObject requestBody = new JsonObject();
        requestBody.add(KEY_PAYLOAD, getJsonPrimitive(signedTransaction.getPayload()));
        return this.client
                .put(ROUTE, requestBody)
                .map(Http::mapStringOrError)
                .map(GsonUtils::mapToJsonObject)
                .map(json -> new TransactionAnnounceResponse(json.get(KEY_MESSAGE).getAsString()));
    }

    @Override
    public Observable announceAggregateBonded(SignedTransaction signedTransaction) {
        JsonObject requestBody = new JsonObject();
        requestBody.add(KEY_PAYLOAD, getJsonPrimitive(signedTransaction.getPayload()));
        return this.client
                .put(ROUTE + "/partial", requestBody)
                .map(Http::mapStringOrError)
                .map(GsonUtils::mapToJsonObject)
                .map(json -> new TransactionAnnounceResponse(json.get(KEY_MESSAGE).getAsString()));
    }

    @Override
    public Observable announceAggregateBondedCosignature(CosignatureSignedTransaction cosignatureSignedTransaction) {
        JsonObject requestBody = new JsonObject();
        requestBody.add("parentHash", getJsonPrimitive(cosignatureSignedTransaction.getParentHash()));
        requestBody.add("signature", getJsonPrimitive(cosignatureSignedTransaction.getSignature()));
        requestBody.add("signer", getJsonPrimitive(cosignatureSignedTransaction.getSigner()));
        return this.client
                .put(ROUTE + "/cosignature", requestBody)
                .map(Http::mapStringOrError)
                .map(GsonUtils::mapToJsonObject)
                .map(json -> new TransactionAnnounceResponse(json.get(KEY_MESSAGE).getAsString()));
    }
    
    private List toTransactionStatusList(String json) {
       return gson.fromJson(json, TRANSACTION_STATUS_LIST_TYPE);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy