use of io.nuls.account.ledger.model.CoinDataResult in project nuls by nuls-io.
the class AccountResource method aliasFee.
@GET
@Path("/alias/fee")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("[别名手续费] 获取设置别名手续 ")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = RpcClientResult.class) })
public RpcClientResult aliasFee(@BeanParam() AccountAliasFeeForm form) {
if (!AddressTool.validAddress(form.getAddress())) {
return Result.getFailed(AccountErrorCode.ADDRESS_ERROR).toRpcClientResult();
}
if (StringUtils.isBlank(form.getAlias())) {
return Result.getFailed(AccountErrorCode.PARAMETER_ERROR).toRpcClientResult();
}
Result result = aliasService.getAliasFee(form.getAddress(), form.getAlias());
Long fee = null;
Long maxAmount = null;
Map<String, Long> map = new HashMap<>();
if (result.isSuccess()) {
fee = ((Na) result.getData()).getValue();
// 如果手续费大于理论最大值,则说明交易过大,需要计算最大交易金额
long feeMax = TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES.multiply(TxMaxSizeValidator.MAX_TX_BYTES).getValue();
if (fee > feeMax) {
AliasTransaction tx = new AliasTransaction();
tx.setTime(TimeService.currentTimeMillis());
Alias alias = new Alias(AddressTool.getAddress(form.getAddress()), form.getAlias());
tx.setTxData(alias);
try {
CoinDataResult coinDataResult = accountLedgerService.getCoinData(AddressTool.getAddress(form.getAddress()), AccountConstant.ALIAS_NA, tx.size(), TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES);
if (!coinDataResult.isEnough()) {
return Result.getFailed(AccountErrorCode.INSUFFICIENT_BALANCE).toRpcClientResult();
}
CoinData coinData = new CoinData();
coinData.setFrom(coinDataResult.getCoinList());
Coin change = coinDataResult.getChange();
if (null != change) {
// 创建toList
List<Coin> toList = new ArrayList<>();
toList.add(change);
coinData.setTo(toList);
}
Coin coin = new Coin(NulsConstant.BLACK_HOLE_ADDRESS, Na.parseNuls(1), 0);
coinData.addTo(coin);
tx.setCoinData(coinData);
} catch (Exception e) {
Log.error(e);
return Result.getFailed(KernelErrorCode.SYS_UNKOWN_EXCEPTION).toRpcClientResult();
}
Result rs = accountLedgerService.getMaxAmountOfOnce(AddressTool.getAddress(form.getAddress()), tx, TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES);
if (rs.isSuccess()) {
maxAmount = ((Na) rs.getData()).getValue();
}
}
map.put("fee", fee);
map.put("maxAmount", maxAmount);
result.setData(map);
}
return result.toRpcClientResult();
}
use of io.nuls.account.ledger.model.CoinDataResult in project nuls by nuls-io.
the class AliasService method setMutilAlias.
/**
* 多签账户设置别名
* Initiate a transaction to set alias.
*
* @param addr Address of account
* @param signAddr Address of account
* @param password password of account
* @param aliasName the alias to set
* @return Result
*/
public Result<String> setMutilAlias(String addr, String signAddr, String aliasName, String password) {
// 签名账户
Account account = accountService.getAccount(signAddr).getData();
if (null == account) {
return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST);
}
if (account.isEncrypted() && account.isLocked()) {
if (!account.validatePassword(password)) {
return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG);
}
}
try {
byte[] addressBytes = AddressTool.getAddress(addr);
Result<MultiSigAccount> sigAccountResult = accountService.getMultiSigAccount(addr);
MultiSigAccount multiSigAccount = sigAccountResult.getData();
if (!AddressTool.validSignAddress(multiSigAccount.getPubKeyList(), account.getPubKey())) {
return Result.getFailed(AccountErrorCode.SIGN_ADDRESS_NOT_MATCH);
}
Script redeemScript = accountLedgerService.getRedeemScript(multiSigAccount);
if (redeemScript == null) {
return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST);
}
AliasTransaction tx = new AliasTransaction();
TransactionSignature transactionSignature = new TransactionSignature();
List<P2PHKSignature> p2PHKSignatures = new ArrayList<>();
List<Script> scripts = new ArrayList<>();
tx.setTime(TimeService.currentTimeMillis());
Alias alias = new Alias(addressBytes, aliasName);
tx.setTxData(alias);
// 交易签名的长度为m*单个签名长度+赎回脚本长度
int scriptSignLenth = redeemScript.getProgram().length + ((int) multiSigAccount.getM()) * 72;
CoinDataResult coinDataResult = accountLedgerService.getMutilCoinData(addressBytes, AccountConstant.ALIAS_NA, tx.size() + scriptSignLenth, TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES);
if (!coinDataResult.isEnough()) {
return Result.getFailed(AccountErrorCode.INSUFFICIENT_BALANCE);
}
CoinData coinData = new CoinData();
coinData.setFrom(coinDataResult.getCoinList());
Coin change = coinDataResult.getChange();
if (null != change) {
// 创建toList
List<Coin> toList = new ArrayList<>();
toList.add(change);
coinData.setTo(toList);
}
Coin coin = new Coin(NulsConstant.BLACK_HOLE_ADDRESS, Na.parseNuls(1), 0);
coinData.addTo(coin);
tx.setCoinData(coinData);
tx.setHash(NulsDigestData.calcDigestData(tx.serializeForHash()));
// 将赎回脚本先存储在签名脚本中
scripts.add(redeemScript);
transactionSignature.setScripts(scripts);
// 使用签名账户对交易进行签名
P2PHKSignature p2PHKSignature = new P2PHKSignature();
ECKey eckey = account.getEcKey(password);
p2PHKSignature.setPublicKey(eckey.getPubKey());
// 用当前交易的hash和账户的私钥账户
p2PHKSignature.setSignData(accountService.signDigest(tx.getHash().getDigestBytes(), eckey));
p2PHKSignatures.add(p2PHKSignature);
Result result = txMutilProcessing(tx, p2PHKSignatures, scripts, transactionSignature, addressBytes);
return result;
} catch (Exception e) {
Log.error(e);
return Result.getFailed(KernelErrorCode.SYS_UNKOWN_EXCEPTION);
}
}
use of io.nuls.account.ledger.model.CoinDataResult in project nuls by nuls-io.
the class AccountResource method multiAliasFee.
@GET
@Path("multiAccount/alias/fee")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("[别名手续费] 获取设置别名手续 ")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = RpcClientResult.class) })
public RpcClientResult multiAliasFee(@BeanParam() MultiAliasFeeForm form) throws Exception {
if (!AddressTool.validAddress(form.getAddress())) {
return Result.getFailed(AccountErrorCode.ADDRESS_ERROR).toRpcClientResult();
}
if (StringUtils.isBlank(form.getAlias())) {
return Result.getFailed(AccountErrorCode.PARAMETER_ERROR).toRpcClientResult();
}
Result result = aliasService.getMultiAliasFee(form.getAddress(), form.getAlias());
AliasTransaction tx = new AliasTransaction();
tx.setTime(TimeService.currentTimeMillis());
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();
}
Alias alias = new Alias(AddressTool.getAddress(form.getAddress()), form.getAlias());
tx.setTxData(alias);
try {
CoinDataResult coinDataResult = accountLedgerService.getMutilCoinData(AddressTool.getAddress(form.getAddress()), AccountConstant.ALIAS_NA, tx.size(), TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES);
if (!coinDataResult.isEnough()) {
return Result.getFailed(AccountErrorCode.INSUFFICIENT_BALANCE).toRpcClientResult();
}
CoinData coinData = new CoinData();
coinData.setFrom(coinDataResult.getCoinList());
Coin change = coinDataResult.getChange();
if (null != change) {
// 创建toList
List<Coin> toList = new ArrayList<>();
toList.add(change);
coinData.setTo(toList);
}
Coin coin = new Coin(NulsConstant.BLACK_HOLE_ADDRESS, Na.parseNuls(1), 0);
coinData.addTo(coin);
tx.setCoinData(coinData);
} catch (Exception e) {
Log.error(e);
return Result.getFailed(KernelErrorCode.SYS_UNKOWN_EXCEPTION).toRpcClientResult();
}
// 交易签名的长度为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 fee = null;
Long maxAmount = null;
if (result.isSuccess()) {
fee = ((Na) result.getData()).getValue();
}
if (rs.isSuccess()) {
maxAmount = ((Na) rs.getData()).getValue();
}
map.put("fee", fee);
map.put("maxAmount", maxAmount);
result.setData(map);
return result.toRpcClientResult();
}
use of io.nuls.account.ledger.model.CoinDataResult in project nuls by nuls-io.
the class PocConsensusResource method createMutilAgent.
@POST
@Path("/multiAccount/createMultiAgent")
@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 createMutilAgent(@ApiParam(name = "form", value = "多签地址创建节点表单数据", required = true) CreateMultiAgentForm form) throws Exception {
if (NulsContext.MAIN_NET_VERSION <= 1) {
return Result.getFailed(KernelErrorCode.VERSION_TOO_LOW).toRpcClientResult();
}
AssertUtil.canNotEmpty(form);
AssertUtil.canNotEmpty(form.getAgentAddress(), "agent address can not be null");
AssertUtil.canNotEmpty(form.getSignAddress(), "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()) || !AddressTool.validAddress(form.getSignAddress())) {
throw new NulsRuntimeException(AccountErrorCode.ADDRESS_ERROR);
}
Account account = accountService.getAccount(form.getSignAddress()).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();
}
}
Result<MultiSigAccount> sigAccountResult = accountService.getMultiSigAccount(form.getAgentAddress());
MultiSigAccount multiSigAccount = sigAccountResult.getData();
// 验证签名账户是否属于多签账户,如果不是多签账户下的地址则提示错误
if (!AddressTool.validSignAddress(multiSigAccount.getPubKeyList(), account.getPubKey())) {
return Result.getFailed(AccountErrorCode.SIGN_ADDRESS_NOT_MATCH).toRpcClientResult();
}
Script redeemScript = accountLedgerService.getRedeemScript(multiSigAccount);
if (redeemScript == null) {
return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST).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()));
}
TransactionSignature transactionSignature = new TransactionSignature();
List<Script> scripts = new ArrayList<>();
agent.setDeposit(Na.valueOf(form.getDeposit()));
agent.setCommissionRate(form.getCommissionRate());
tx.setTxData(agent);
CoinData coinData = new CoinData();
List<Coin> toList = new ArrayList<>();
if (agent.getAgentAddress()[2] == NulsContext.P2SH_ADDRESS_TYPE) {
Script scriptPubkey = SignatureUtil.createOutputScript(agent.getAgentAddress());
toList.add(new Coin(scriptPubkey.getProgram(), agent.getDeposit(), PocConsensusConstant.CONSENSUS_LOCK_TIME));
} else {
toList.add(new Coin(agent.getAgentAddress(), agent.getDeposit(), PocConsensusConstant.CONSENSUS_LOCK_TIME));
}
coinData.setTo(toList);
tx.setCoinData(coinData);
// 交易签名的长度为m*单个签名长度+赎回脚本长度
int scriptSignLenth = redeemScript.getProgram().length + ((int) multiSigAccount.getM()) * 72;
CoinDataResult result = accountLedgerService.getMutilCoinData(agent.getAgentAddress(), agent.getDeposit(), tx.size() + scriptSignLenth, TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES);
if (null != result) {
if (result.isEnough()) {
tx.getCoinData().setFrom(result.getCoinList());
if (null != result.getChange()) {
tx.getCoinData().getTo().add(result.getChange());
}
} else {
return Result.getFailed(TransactionErrorCode.INSUFFICIENT_BALANCE).toRpcClientResult();
}
}
tx.setHash(NulsDigestData.calcDigestData(tx.serializeForHash()));
// 将赎回脚本先存储在签名脚本中
scripts.add(redeemScript);
transactionSignature.setScripts(scripts);
Result finalResult = accountLedgerService.txMultiProcess(tx, transactionSignature, account, form.getPassword());
if (finalResult.isSuccess()) {
Map<String, String> valueMap = new HashMap<>();
valueMap.put("txData", (String) finalResult.getData());
return Result.getSuccess().setData(valueMap).toRpcClientResult();
}
return finalResult.toRpcClientResult();
}
use of io.nuls.account.ledger.model.CoinDataResult in project nuls by nuls-io.
the class PocConsensusResource method getCreateAgentFee.
@GET
@Path("/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 getCreateAgentFee(@BeanParam() GetCreateAgentFeeForm 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 (StringUtils.isBlank(form.getRewardAddress())) {
form.setRewardAddress(form.getAgentAddress());
}
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();
}
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<>();
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.getCoinData(agent.getAgentAddress(), agent.getDeposit(), tx.size() - P2PHKSignature.SERIALIZE_LENGTH, 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.getAgentAddress(), tx));
return Result.getSuccess().setData(map).toRpcClientResult();
}
Aggregations