use of io.nuls.ledger.entity.UtxoOutput in project nuls by nuls-io.
the class UtxoTransferTool method toInput.
public static UtxoInput toInput(UtxoInputPo po) {
UtxoInput input = new UtxoInput();
input.setTxHash(new NulsDigestData(Hex.decode(po.getTxHash())));
input.setIndex(po.getInIndex());
input.setFromHash(new NulsDigestData(Hex.decode(po.getFromHash())));
input.setFromIndex(po.getFromIndex());
UtxoOutput output = new UtxoOutput();
output.setTxHash(new NulsDigestData(Hex.decode(po.getFromOutPut().getTxHash())));
output.setIndex(po.getFromOutPut().getOutIndex());
output.setLockTime(po.getFromOutPut().getLockTime());
output.setValue(po.getFromOutPut().getValue());
output.setAddress(po.getFromOutPut().getAddress());
input.setFrom(output);
return input;
}
use of io.nuls.ledger.entity.UtxoOutput in project nuls by nuls-io.
the class UtxoTxOutputsValidator method validate.
@Override
public ValidateResult validate(AbstractCoinTransaction tx) {
UtxoData utxoData = (UtxoData) tx.getCoinData();
List<UtxoOutput> outputs = utxoData.getOutputs();
if (null != outputs && outputs.size() > MAX_OUTPUT_COUNT) {
return ValidateResult.getFailedResult(ERROR_MESSAGE);
}
return ValidateResult.getSuccessResult();
}
use of io.nuls.ledger.entity.UtxoOutput in project nuls by nuls-io.
the class AccountResource method getUtxo.
@GET
@Path("/utxo/")
@Produces(MediaType.APPLICATION_JSON)
public RpcResult getUtxo(@QueryParam("address") String address, @QueryParam("amount") long amount) {
if (!Address.validAddress(address) || amount <= 0 || amount > Na.MAX_NA_VALUE) {
return RpcResult.getFailed(ErrorCode.PARAMETER_ERROR);
}
UtxoBalance balance = (UtxoBalance) ledgerService.getBalance(address);
if (balance == null || balance.getUnSpends() == null) {
return RpcResult.getFailed("balance not enough");
}
amount += this.ledgerService.getTxFee(Integer.MAX_VALUE).getValue();
long usable = 0;
boolean enough = false;
List<OutputDto> dtoList = new ArrayList<>();
for (int i = 0; i < balance.getUnSpends().size(); i++) {
UtxoOutput output = balance.getUnSpends().get(i);
if (output.isUsable()) {
usable += output.getValue();
dtoList.add(new OutputDto(output));
}
if (usable > amount) {
enough = true;
break;
}
}
if (!enough) {
return RpcResult.getFailed("balance not enough");
}
return RpcResult.getSuccess().setData(dtoList);
}
use of io.nuls.ledger.entity.UtxoOutput in project nuls by nuls-io.
the class UtxoTransferTool method toOutput.
public static UtxoOutput toOutput(UtxoOutputPo po) {
UtxoOutput output = new UtxoOutput();
output.setTxHash(new NulsDigestData(Hex.decode(po.getTxHash())));
output.setIndex(po.getOutIndex());
output.setLockTime(po.getLockTime());
output.setValue(po.getValue());
output.setAddress(po.getAddress());
try {
output.setP2PKHScript(new P2PKHScript(po.getScript()));
} catch (Exception e) {
// todo
Log.error(e);
}
long currentTime = TimeService.currentTimeMillis();
long genesisTime = NulsContext.getInstance().getGenesisBlock().getHeader().getTime();
long bestHeight = NulsContext.getInstance().getNetBestBlockHeight();
if (po.getStatus() == UtxoOutputPo.USABLE) {
if (po.getLockTime() > 0) {
if (po.getLockTime() >= genesisTime && po.getLockTime() > currentTime) {
output.setStatus(OutPutStatusEnum.UTXO_CONFIRM_TIME_LOCK);
} else if (po.getLockTime() < genesisTime && po.getLockTime() > bestHeight) {
output.setStatus(OutPutStatusEnum.UTXO_CONFIRM_TIME_LOCK);
} else {
output.setStatus(OutPutStatusEnum.UTXO_CONFIRM_UNSPEND);
}
} else {
output.setStatus(OutPutStatusEnum.UTXO_CONFIRM_UNSPEND);
}
} else if (po.getStatus() == UtxoOutputPo.LOCKED) {
output.setStatus(OutPutStatusEnum.UTXO_CONFIRM_CONSENSUS_LOCK);
} else if (po.getStatus() == UtxoOutputPo.SPENT) {
output.setStatus(OutPutStatusEnum.UTXO_SPENT);
}
if (po.getCreateTime() != null) {
output.setCreateTime(po.getCreateTime());
}
if (po.getTxType() != null) {
output.setTxType(po.getTxType());
}
return output;
}
use of io.nuls.ledger.entity.UtxoOutput in project nuls by nuls-io.
the class UtxoTxInputsValidator method validate.
@Override
public ValidateResult validate(AbstractCoinTransaction tx) {
UtxoData data = (UtxoData) tx.getCoinData();
for (int i = 0; i < data.getInputs().size(); i++) {
UtxoInput input = data.getInputs().get(i);
UtxoOutput output = input.getFrom();
if (output == null && tx.getStatus() == TxStatusEnum.CACHED) {
return ValidateResult.getFailedResult(ErrorCode.ORPHAN_TX);
}
if (tx.getStatus() == TxStatusEnum.CACHED) {
if (!output.isUsable()) {
return ValidateResult.getFailedResult(ErrorCode.UTXO_STATUS_CHANGE);
}
} else if (tx.getStatus() == TxStatusEnum.AGREED) {
if (!output.isSpend()) {
return ValidateResult.getFailedResult(ErrorCode.UTXO_STATUS_CHANGE);
}
}
byte[] owner = output.getOwner();
P2PKHScriptSig p2PKHScriptSig = null;
try {
p2PKHScriptSig = P2PKHScriptSig.createFromBytes(tx.getScriptSig());
} catch (NulsException e) {
return ValidateResult.getFailedResult(ErrorCode.DATA_ERROR);
}
byte[] user = p2PKHScriptSig.getSignerHash160();
if (!Arrays.equals(owner, user)) {
return ValidateResult.getFailedResult(ErrorCode.INVALID_OUTPUT);
}
return ValidateResult.getSuccessResult();
}
return ValidateResult.getSuccessResult();
}
Aggregations