Search in sources :

Example 16 with CoinDataResult

use of io.nuls.account.ledger.model.CoinDataResult in project nuls by nuls-io.

the class PocConsensusResource method depositToAgent.

@POST
@Path("/deposit")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "deposit nuls to a bank! 申请参与共识 ", notes = "返回申请成功交易hash")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = String.class) })
public RpcClientResult depositToAgent(@ApiParam(name = "form", value = "申请参与共识表单数据", required = true) DepositForm form) throws NulsException {
    AssertUtil.canNotEmpty(form);
    AssertUtil.canNotEmpty(form.getAddress());
    AssertUtil.canNotEmpty(form.getAgentHash());
    if (!NulsDigestData.validHash(form.getAgentHash())) {
        return Result.getFailed(PocConsensusErrorCode.AGENT_NOT_EXIST).toRpcClientResult();
    }
    AssertUtil.canNotEmpty(form.getDeposit());
    if (!AddressTool.validAddress(form.getAddress())) {
        throw new NulsRuntimeException(KernelErrorCode.PARAMETER_ERROR);
    }
    Account account = accountService.getAccount(form.getAddress()).getData();
    if (null == account) {
        return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST).toRpcClientResult();
    }
    if (account.isEncrypted() && account.isLocked()) {
        AssertUtil.canNotEmpty(form.getPassword(), "password is wrong");
        if (!account.validatePassword(form.getPassword())) {
            return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG).toRpcClientResult();
        }
    }
    if (!account.isOk()) {
        return Result.getFailed(AccountErrorCode.IMPORTING_ACCOUNT).toRpcClientResult();
    }
    DepositTransaction tx = new DepositTransaction();
    Deposit deposit = new Deposit();
    deposit.setAddress(AddressTool.getAddress(form.getAddress()));
    deposit.setAgentHash(NulsDigestData.fromDigestHex(form.getAgentHash()));
    deposit.setDeposit(Na.valueOf(form.getDeposit()));
    tx.setTxData(deposit);
    CoinData coinData = new CoinData();
    List<Coin> toList = new ArrayList<>();
    toList.add(new Coin(deposit.getAddress(), deposit.getDeposit(), PocConsensusConstant.CONSENSUS_LOCK_TIME));
    coinData.setTo(toList);
    tx.setCoinData(coinData);
    CoinDataResult result = accountLedgerService.getCoinData(deposit.getAddress(), deposit.getDeposit(), tx.size(), TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES);
    RpcClientResult result1 = this.txProcessing(tx, result, account, form.getPassword());
    if (!result1.isSuccess()) {
        return result1;
    }
    Map<String, String> valueMap = new HashMap<>();
    valueMap.put("value", tx.getHash().getDigestHex());
    return Result.getSuccess().setData(valueMap).toRpcClientResult();
}
Also used : Account(io.nuls.account.model.Account) MultiSigAccount(io.nuls.account.model.MultiSigAccount) CancelDepositTransaction(io.nuls.consensus.poc.protocol.tx.CancelDepositTransaction) DepositTransaction(io.nuls.consensus.poc.protocol.tx.DepositTransaction) Deposit(io.nuls.consensus.poc.protocol.entity.Deposit) CancelDeposit(io.nuls.consensus.poc.protocol.entity.CancelDeposit) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) CoinDataResult(io.nuls.account.ledger.model.CoinDataResult)

Example 17 with CoinDataResult

use of io.nuls.account.ledger.model.CoinDataResult in project nuls by nuls-io.

the class PocConsensusResource method getMultiDepositFee.

@GET
@Path("/multiAccount/deposit/fee")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "get the fee of create agent! 获取加入共识的手续费", notes = "返回加入共识交易手续费")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = String.class) })
public RpcClientResult getMultiDepositFee(@BeanParam() GetDepositFeeForm form) throws Exception {
    AssertUtil.canNotEmpty(form);
    AssertUtil.canNotEmpty(form.getAddress(), "address can not be null");
    AssertUtil.canNotEmpty(form.getAgentHash(), "agent hash can not be null");
    AssertUtil.canNotEmpty(form.getDeposit(), "deposit can not be null");
    DepositTransaction tx = new DepositTransaction();
    Deposit deposit = new Deposit();
    deposit.setAddress(AddressTool.getAddress(form.getAddress()));
    deposit.setAgentHash(NulsDigestData.fromDigestHex(form.getAgentHash()));
    deposit.setDeposit(Na.valueOf(form.getDeposit()));
    tx.setTxData(deposit);
    Result<MultiSigAccount> sigAccountResult = accountService.getMultiSigAccount(form.getAddress());
    MultiSigAccount multiSigAccount = sigAccountResult.getData();
    Script redeemScript = accountLedgerService.getRedeemScript(multiSigAccount);
    if (redeemScript == null) {
        return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST).toRpcClientResult();
    }
    CoinData coinData = new CoinData();
    List<Coin> toList = new ArrayList<>();
    toList.add(new Coin(deposit.getAddress(), deposit.getDeposit(), -1));
    coinData.setTo(toList);
    tx.setCoinData(coinData);
    CoinDataResult result = accountLedgerService.getCoinData(deposit.getAddress(), deposit.getDeposit(), tx.size(), TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES);
    tx.getCoinData().setFrom(result.getCoinList());
    if (null != result.getChange()) {
        tx.getCoinData().getTo().add(result.getChange());
    }
    Na fee = TransactionFeeCalculator.getMaxFee(108 + tx.size());
    // 交易签名的长度为m*单个签名长度+赎回脚本长度
    int scriptSignLenth = redeemScript.getProgram().length + ((int) multiSigAccount.getM()) * 72;
    Result rs = accountLedgerService.getMultiMaxAmountOfOnce(AddressTool.getAddress(form.getAddress()), tx, TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES, scriptSignLenth);
    Map<String, Long> map = new HashMap<>();
    Long maxAmount = null;
    if (rs.isSuccess()) {
        maxAmount = ((Na) rs.getData()).getValue();
    }
    map.put("fee", fee.getValue());
    map.put("maxAmount", maxAmount);
    rs.setData(map);
    return Result.getSuccess().setData(rs).toRpcClientResult();
}
Also used : MultiSigAccount(io.nuls.account.model.MultiSigAccount) CancelDepositTransaction(io.nuls.consensus.poc.protocol.tx.CancelDepositTransaction) DepositTransaction(io.nuls.consensus.poc.protocol.tx.DepositTransaction) Deposit(io.nuls.consensus.poc.protocol.entity.Deposit) CancelDeposit(io.nuls.consensus.poc.protocol.entity.CancelDeposit) CoinDataResult(io.nuls.account.ledger.model.CoinDataResult) CoinDataResult(io.nuls.account.ledger.model.CoinDataResult)

Example 18 with CoinDataResult

use of io.nuls.account.ledger.model.CoinDataResult in project nuls by nuls-io.

the class PocConsensusResource method getCreateMultiAgentFee.

@GET
@Path("/multiAccount/Agent/fee")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "get the fee of create agent! 获取创建共识(代理)节点的手续费", notes = "返回创建的节点成功的交易手续费")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = String.class) })
public RpcClientResult getCreateMultiAgentFee(@BeanParam() GetCreateAgentFeeForm form) throws Exception {
    AssertUtil.canNotEmpty(form);
    AssertUtil.canNotEmpty(form.getAgentAddress(), "agent address can not be null");
    AssertUtil.canNotEmpty(form.getCommissionRate(), "commission rate can not be null");
    AssertUtil.canNotEmpty(form.getDeposit(), "deposit can not be null");
    AssertUtil.canNotEmpty(form.getPackingAddress(), "packing address can not be null");
    if (StringUtils.isBlank(form.getRewardAddress())) {
        form.setRewardAddress(form.getAgentAddress());
    }
    CreateAgentTransaction tx = new CreateAgentTransaction();
    tx.setTime(TimeService.currentTimeMillis());
    Agent agent = new Agent();
    agent.setAgentAddress(AddressTool.getAddress(form.getAgentAddress()));
    agent.setPackingAddress(AddressTool.getAddress(form.getPackingAddress()));
    if (StringUtils.isBlank(form.getRewardAddress())) {
        agent.setRewardAddress(agent.getAgentAddress());
    } else {
        agent.setRewardAddress(AddressTool.getAddress(form.getRewardAddress()));
    }
    agent.setDeposit(Na.valueOf(form.getDeposit()));
    agent.setCommissionRate(form.getCommissionRate());
    tx.setTxData(agent);
    Result<MultiSigAccount> sigAccountResult = accountService.getMultiSigAccount(form.getAgentAddress());
    MultiSigAccount multiSigAccount = sigAccountResult.getData();
    Script redeemScript = accountLedgerService.getRedeemScript(multiSigAccount);
    if (redeemScript == null) {
        return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST).toRpcClientResult();
    }
    CoinData coinData = new CoinData();
    List<Coin> toList = new ArrayList<>();
    if (agent.getAgentAddress()[2] == 3) {
        Script scriptPubkey = SignatureUtil.createOutputScript(agent.getAgentAddress());
        toList.add(new Coin(scriptPubkey.getProgram(), agent.getDeposit(), -1));
    } else {
        toList.add(new Coin(agent.getAgentAddress(), agent.getDeposit(), -1));
    }
    coinData.setTo(toList);
    tx.setCoinData(coinData);
    CoinDataResult result = accountLedgerService.getMutilCoinData(agent.getAgentAddress(), agent.getDeposit(), tx.size(), TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES);
    tx.getCoinData().setFrom(result.getCoinList());
    if (null != result.getChange()) {
        tx.getCoinData().getTo().add(result.getChange());
    }
    Na fee = TransactionFeeCalculator.getMaxFee(108 + tx.size());
    // 交易签名的长度为m*单个签名长度+赎回脚本长度
    int scriptSignLenth = redeemScript.getProgram().length + ((int) multiSigAccount.getM()) * 72;
    Result rs = accountLedgerService.getMultiMaxAmountOfOnce(AddressTool.getAddress(form.getAgentAddress()), tx, TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES, scriptSignLenth);
    Map<String, Long> map = new HashMap<>();
    Long maxAmount = null;
    if (rs.isSuccess()) {
        maxAmount = ((Na) rs.getData()).getValue();
    }
    map.put("fee", fee.getValue());
    map.put("maxAmount", maxAmount);
    rs.setData(map);
    return Result.getSuccess().setData(rs).toRpcClientResult();
}
Also used : MultiSigAccount(io.nuls.account.model.MultiSigAccount) StopAgent(io.nuls.consensus.poc.protocol.entity.StopAgent) Agent(io.nuls.consensus.poc.protocol.entity.Agent) CoinDataResult(io.nuls.account.ledger.model.CoinDataResult) CreateAgentTransaction(io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction) CoinDataResult(io.nuls.account.ledger.model.CoinDataResult)

Example 19 with CoinDataResult

use of io.nuls.account.ledger.model.CoinDataResult in project nuls by nuls-io.

the class PocConsensusResource method createAgent.

@POST
@Path("/agent")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Create an agent for consensus! 创建共识(代理)节点 [3.6.3]", notes = "返回创建的节点成功的交易hash")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = String.class) })
public RpcClientResult createAgent(@ApiParam(name = "form", value = "创建节点表单数据", required = true) CreateAgentForm form) throws NulsException {
    AssertUtil.canNotEmpty(form);
    AssertUtil.canNotEmpty(form.getAgentAddress(), "agent address can not be null");
    AssertUtil.canNotEmpty(form.getCommissionRate(), "commission rate can not be null");
    AssertUtil.canNotEmpty(form.getDeposit(), "deposit can not be null");
    AssertUtil.canNotEmpty(form.getPackingAddress(), "packing address can not be null");
    if (!AddressTool.isPackingAddress(form.getPackingAddress()) || !AddressTool.validAddress(form.getAgentAddress())) {
        throw new NulsRuntimeException(AccountErrorCode.ADDRESS_ERROR);
    }
    Account account = accountService.getAccount(form.getAgentAddress()).getData();
    if (null == account) {
        return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST).toRpcClientResult();
    }
    if (!account.isOk()) {
        return Result.getFailed(AccountErrorCode.IMPORTING_ACCOUNT).toRpcClientResult();
    }
    if (account.isEncrypted() && account.isLocked()) {
        AssertUtil.canNotEmpty(form.getPassword(), "password is wrong");
        if (!account.validatePassword(form.getPassword())) {
            return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG).toRpcClientResult();
        }
    }
    CreateAgentTransaction tx = new CreateAgentTransaction();
    tx.setTime(TimeService.currentTimeMillis());
    Agent agent = new Agent();
    agent.setAgentAddress(AddressTool.getAddress(form.getAgentAddress()));
    agent.setPackingAddress(AddressTool.getAddress(form.getPackingAddress()));
    if (StringUtils.isBlank(form.getRewardAddress())) {
        agent.setRewardAddress(agent.getAgentAddress());
    } else {
        agent.setRewardAddress(AddressTool.getAddress(form.getRewardAddress()));
    }
    agent.setDeposit(Na.valueOf(form.getDeposit()));
    agent.setCommissionRate(form.getCommissionRate());
    tx.setTxData(agent);
    CoinData coinData = new CoinData();
    List<Coin> toList = new ArrayList<>();
    toList.add(new Coin(agent.getAgentAddress(), agent.getDeposit(), PocConsensusConstant.CONSENSUS_LOCK_TIME));
    coinData.setTo(toList);
    tx.setCoinData(coinData);
    CoinDataResult result = accountLedgerService.getCoinData(agent.getAgentAddress(), agent.getDeposit(), tx.size(), TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES);
    RpcClientResult result1 = this.txProcessing(tx, result, account, form.getPassword());
    if (!result1.isSuccess()) {
        return result1;
    }
    Map<String, String> valueMap = new HashMap<>();
    valueMap.put("value", tx.getHash().getDigestHex());
    return Result.getSuccess().setData(valueMap).toRpcClientResult();
}
Also used : Account(io.nuls.account.model.Account) MultiSigAccount(io.nuls.account.model.MultiSigAccount) StopAgent(io.nuls.consensus.poc.protocol.entity.StopAgent) Agent(io.nuls.consensus.poc.protocol.entity.Agent) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) CreateAgentTransaction(io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction) CoinDataResult(io.nuls.account.ledger.model.CoinDataResult)

Example 20 with CoinDataResult

use of io.nuls.account.ledger.model.CoinDataResult in project nuls by nuls-io.

the class PocConsensusResource method getDepositFee.

@GET
@Path("/deposit/fee")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "get the fee of create agent! 获取加入共识的手续费", notes = "返回加入共识交易手续费")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = String.class) })
public RpcClientResult getDepositFee(@BeanParam() GetDepositFeeForm form) throws NulsException {
    AssertUtil.canNotEmpty(form);
    AssertUtil.canNotEmpty(form.getAddress(), "address can not be null");
    AssertUtil.canNotEmpty(form.getAgentHash(), "agent hash can not be null");
    AssertUtil.canNotEmpty(form.getDeposit(), "deposit can not be null");
    Account account = accountService.getAccount(form.getAddress()).getData();
    if (null == account) {
        return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST).toRpcClientResult();
    }
    if (!account.isOk()) {
        return Result.getFailed(AccountErrorCode.IMPORTING_ACCOUNT).toRpcClientResult();
    }
    DepositTransaction tx = new DepositTransaction();
    Deposit deposit = new Deposit();
    deposit.setAddress(AddressTool.getAddress(form.getAddress()));
    deposit.setAgentHash(NulsDigestData.fromDigestHex(form.getAgentHash()));
    deposit.setDeposit(Na.valueOf(form.getDeposit()));
    tx.setTxData(deposit);
    CoinData coinData = new CoinData();
    List<Coin> toList = new ArrayList<>();
    toList.add(new Coin(deposit.getAddress(), deposit.getDeposit(), -1));
    coinData.setTo(toList);
    tx.setCoinData(coinData);
    CoinDataResult result = accountLedgerService.getCoinData(deposit.getAddress(), deposit.getDeposit(), tx.size(), TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES);
    tx.getCoinData().setFrom(result.getCoinList());
    if (null != result.getChange()) {
        tx.getCoinData().getTo().add(result.getChange());
    }
    Na fee = TransactionFeeCalculator.getMaxFee(108 + tx.size());
    Map<String, Long> map = new HashMap<>();
    map.put("fee", fee.getValue());
    map.put("maxAmount", getMaxAmount(fee, form.getAddress(), tx));
    return Result.getSuccess().setData(map).toRpcClientResult();
}
Also used : Account(io.nuls.account.model.Account) MultiSigAccount(io.nuls.account.model.MultiSigAccount) CancelDepositTransaction(io.nuls.consensus.poc.protocol.tx.CancelDepositTransaction) DepositTransaction(io.nuls.consensus.poc.protocol.tx.DepositTransaction) Deposit(io.nuls.consensus.poc.protocol.entity.Deposit) CancelDeposit(io.nuls.consensus.poc.protocol.entity.CancelDeposit) CoinDataResult(io.nuls.account.ledger.model.CoinDataResult)

Aggregations

CoinDataResult (io.nuls.account.ledger.model.CoinDataResult)35 Account (io.nuls.account.model.Account)22 NulsException (io.nuls.kernel.exception.NulsException)22 MultiSigAccount (io.nuls.account.model.MultiSigAccount)20 IOException (java.io.IOException)20 UnsupportedEncodingException (java.io.UnsupportedEncodingException)15 ECKey (io.nuls.core.tools.crypto.ECKey)14 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)10 TransferTransaction (io.nuls.protocol.model.tx.TransferTransaction)8 TransactionDataResult (io.nuls.account.ledger.model.TransactionDataResult)7 AliasTransaction (io.nuls.account.tx.AliasTransaction)7 ValidateResult (io.nuls.kernel.validate.ValidateResult)7 MultipleAddressTransferModel (io.nuls.account.ledger.model.MultipleAddressTransferModel)6 ArrayList (java.util.ArrayList)6 Alias (io.nuls.account.model.Alias)5 NulsByteBuffer (io.nuls.kernel.utils.NulsByteBuffer)5 DataTransaction (io.nuls.protocol.model.tx.DataTransaction)4 LogicData (io.nuls.protocol.model.tx.LogicData)4 AccountConstant (io.nuls.account.constant.AccountConstant)3 AccountErrorCode (io.nuls.account.constant.AccountErrorCode)3