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

org.bcos.web3j.protocol.parity.JsonRpc2_0Parity Maven / Gradle / Ivy

There is a newer version: 2.6.6
Show newest version
package org.bcos.web3j.protocol.parity;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.bcos.web3j.crypto.WalletFile;
import org.bcos.web3j.protocol.Web3jService;
import org.bcos.web3j.protocol.core.JsonRpc2_0Web3j;
import org.bcos.web3j.protocol.core.Request;
import org.bcos.web3j.protocol.core.methods.request.Transaction;
import org.bcos.web3j.protocol.core.methods.response.EthSendTransaction;
import org.bcos.web3j.protocol.core.methods.response.VoidResponse;
import org.bcos.web3j.protocol.parity.methods.response.NewAccountIdentifier;
import org.bcos.web3j.protocol.parity.methods.response.PersonalAccountsInfo;
import org.bcos.web3j.protocol.parity.methods.response.PersonalEcRecover;
import org.bcos.web3j.protocol.parity.methods.response.PersonalListAccounts;
import org.bcos.web3j.protocol.parity.methods.response.PersonalRejectRequest;
import org.bcos.web3j.protocol.parity.methods.response.PersonalRequestsToConfirm;
import org.bcos.web3j.protocol.parity.methods.response.PersonalSign;
import org.bcos.web3j.protocol.parity.methods.response.PersonalSignerEnabled;
import org.bcos.web3j.protocol.parity.methods.response.PersonalUnlockAccount;

/**
 * JSON-RPC 2.0 factory implementation for Parity.
 */
public class JsonRpc2_0Parity extends JsonRpc2_0Web3j implements Parity {

    public JsonRpc2_0Parity(Web3jService web3jService) {
        super(web3jService);
    }

    @Override
    public Request personalSignerEnabled() {
        return new Request<>(
                "personal_signerEnabled",
                Collections.emptyList(),
                ID,
                web3jService,
                PersonalSignerEnabled.class);
    }

    @Override
    public Request personalSign(
            String message, String accountId, String password) {
        return new Request<>(
                "personal_sign",
                Arrays.asList(message,accountId,password),
                ID,
                web3jService,
                PersonalSign.class);
    }

    @Override
    public Request personalEcRecover(
            String hexMessage, String signedMessage) {
        return new Request<>(
                "personal_ecRecover",
                Arrays.asList(hexMessage,signedMessage),
                ID,
                web3jService,
                PersonalEcRecover.class);
    }

    @Override
    public Request personalListAccounts() {
        return new Request<>(
                "personal_listAccounts",
                Collections.emptyList(),
                ID,
                web3jService,
                PersonalListAccounts.class);
    }

    @Override
    public Request personalNewAccount(String password) {
        return new Request<>(
                "personal_newAccount",
                Arrays.asList(password),
                ID,
                web3jService,
                NewAccountIdentifier.class);
    }

    @Override
    public Request personalNewAccountFromPhrase(
            String phrase, String password) {
        return new Request<>(
                "personal_newAccountFromPhrase",
                Arrays.asList(phrase, password),
                ID,
                web3jService,
                NewAccountIdentifier.class);
    }

    @Override
    public Request personalNewAccountFromWallet(
            WalletFile walletFile, String password) {
        return new Request<>(
                "personal_newAccountFromWallet",
                Arrays.asList(walletFile, password),
                ID,
                web3jService,
                NewAccountIdentifier.class);
    }

    @Override
    public Request personalUnlockAccount(
            String accountId, String password, BigInteger duration) {
        List attributes = new ArrayList<>(3);
        attributes.add(accountId);
        attributes.add(password);

        if (duration != null) {
            // Parity has a bug where it won't support a duration
            // See https://github.com/ethcore/parity/issues/1215
            attributes.add(duration.longValue());
        } else {
            // we still need to include the null value, otherwise Parity rejects request
            attributes.add(null);
        }

        return new Request<>(
                "personal_unlockAccount",
                attributes,
                ID,
                web3jService,
                PersonalUnlockAccount.class);
    }

    @Override
    public Request personalUnlockAccount(
            String accountId, String password) {

        return personalUnlockAccount(accountId, password, null);
    }

    @Override
    public Request personalSignAndSendTransaction(
            Transaction transaction, String password) {
        return new Request<>(
                "personal_signAndSendTransaction",
                Arrays.asList(transaction, password),
                ID,
                web3jService,
                EthSendTransaction.class);
    }

    @Override
    public Request personalSetAccountName(
            String accountId, String newAccountName) {

        return new Request<>(
                "personal_setAccountName",
                Arrays.asList(accountId, newAccountName),
                ID,
                web3jService,
                VoidResponse.class);
    }

    @Override
    public Request personalSetAccountMeta(
            String accountId, Map metadata) {
        return new Request<>(
                "personal_setAccountMeta",
                Arrays.asList(accountId, metadata),
                ID,
                web3jService,
                VoidResponse.class);
    }

    @Override
    public Request personalAccountsInfo() {
        return new Request<>(
                "personal_accountsInfo",
                Collections.emptyList(),
                ID,
                web3jService,
                PersonalAccountsInfo.class);
    }

    @Override
    public Request personalRequestsToConfirm() {
        return new Request<>(
                "personal_requestsToConfirm",
                Collections.emptyList(),
                ID,
                web3jService,
                PersonalRequestsToConfirm.class);
    }

    @Override
    public Request personalConfirmRequest(
            String requestId, Transaction transaction, String password) {
        return new Request<>(
                "personal_confirmRequest",
                Arrays.asList(requestId, transaction, password),
                ID,
                web3jService,
                EthSendTransaction.class);
    }

    @Override
    public Request personalRejectRequest(String requestId) {
        return new Request<>(
                "personal_rejectRequest",
                Arrays.asList(requestId),
                ID,
                web3jService,
                PersonalRejectRequest.class);
    }
}