Search in sources :

Example 1 with UtxoInput

use of io.nuls.ledger.entity.UtxoInput 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;
}
Also used : NulsDigestData(io.nuls.core.chain.entity.NulsDigestData) UtxoInput(io.nuls.ledger.entity.UtxoInput) UtxoOutput(io.nuls.ledger.entity.UtxoOutput)

Example 2 with UtxoInput

use of io.nuls.ledger.entity.UtxoInput 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();
}
Also used : P2PKHScriptSig(io.nuls.core.script.P2PKHScriptSig) NulsException(io.nuls.core.exception.NulsException) UtxoData(io.nuls.ledger.entity.UtxoData) UtxoInput(io.nuls.ledger.entity.UtxoInput) UtxoOutput(io.nuls.ledger.entity.UtxoOutput)

Aggregations

UtxoInput (io.nuls.ledger.entity.UtxoInput)2 UtxoOutput (io.nuls.ledger.entity.UtxoOutput)2 NulsDigestData (io.nuls.core.chain.entity.NulsDigestData)1 NulsException (io.nuls.core.exception.NulsException)1 P2PKHScriptSig (io.nuls.core.script.P2PKHScriptSig)1 UtxoData (io.nuls.ledger.entity.UtxoData)1