Search in sources :

Example 6 with P2PKHScriptSig

use of io.nuls.core.script.P2PKHScriptSig in project nuls by nuls-io.

the class ConsensusTool method createBlock.

public static Block createBlock(BlockData blockData, Account account) throws NulsException {
    if (null == account) {
        throw new NulsRuntimeException(ErrorCode.ACCOUNT_NOT_EXIST);
    }
    Block block = new Block();
    block.setTxs(blockData.getTxList());
    BlockHeader header = new BlockHeader();
    block.setHeader(header);
    try {
        block.getHeader().setExtend(blockData.getRoundData().serialize());
    } catch (IOException e) {
        Log.error(e);
    }
    header.setHeight(blockData.getHeight());
    header.setTime(TimeService.currentTimeMillis());
    header.setPreHash(blockData.getPreHash());
    header.setTxCount(blockData.getTxList().size());
    List<NulsDigestData> txHashList = new ArrayList<>();
    for (int i = 0; i < blockData.getTxList().size(); i++) {
        Transaction tx = blockData.getTxList().get(i);
        txHashList.add(tx.getHash());
    }
    header.setPackingAddress(account.getAddress().toString());
    header.setMerkleHash(NulsDigestData.calcMerkleDigestData(txHashList));
    header.setHash(NulsDigestData.calcDigestData(block.getHeader()));
    P2PKHScriptSig scriptSig = new P2PKHScriptSig();
    NulsSignData signData = accountService.signDigest(header.getHash(), account, NulsContext.CACHED_PASSWORD_OF_WALLET);
    scriptSig.setSignData(signData);
    scriptSig.setPublicKey(account.getPubKey());
    header.setScriptSig(scriptSig);
    return block;
}
Also used : P2PKHScriptSig(io.nuls.core.script.P2PKHScriptSig) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) IOException(java.io.IOException)

Example 7 with P2PKHScriptSig

use of io.nuls.core.script.P2PKHScriptSig in project nuls by nuls-io.

the class AccountServiceImpl method createP2PKHScriptSigFromDigest.

@Override
public P2PKHScriptSig createP2PKHScriptSigFromDigest(NulsDigestData nulsDigestData, Account account, String password) throws NulsException {
    P2PKHScriptSig p2PKHScriptSig = new P2PKHScriptSig();
    p2PKHScriptSig.setSignData(signDigest(nulsDigestData, account, password));
    p2PKHScriptSig.setPublicKey(account.getPubKey());
    return p2PKHScriptSig;
}
Also used : P2PKHScriptSig(io.nuls.core.script.P2PKHScriptSig)

Example 8 with P2PKHScriptSig

use of io.nuls.core.script.P2PKHScriptSig in project nuls by nuls-io.

the class GenesisBlock method initGengsisTxs.

private void initGengsisTxs(Map<String, Object> jsonMap) {
    List<Map<String, Object>> list = (List<Map<String, Object>>) jsonMap.get(CONFIG_FILED_TXS);
    if (null == list || list.isEmpty()) {
        throw new NulsRuntimeException(ErrorCode.CONFIG_ERROR);
    }
    CoinTransferData data = new CoinTransferData(OperationType.COIN_BASE, Na.ZERO);
    data.setPriKey(Hex.decode(priKey));
    Na total = Na.ZERO;
    for (Map<String, Object> map : list) {
        String address = (String) map.get(CONFIG_FILED_ADDRESS);
        AssertUtil.canNotEmpty(address, ErrorCode.NULL_PARAMETER);
        Integer nuls = (Integer) map.get(CONFIG_FILED_NULS);
        AssertUtil.canNotEmpty(nuls, ErrorCode.NULL_PARAMETER);
        Integer height = (Integer) map.get(CONFIG_FILED_UNLOCK_HEIGHT);
        Coin coin = new Coin();
        coin.setNa(Na.parseNuls(nuls));
        coin.setUnlockTime(0);
        if (height == null) {
            coin.setUnlockTime(0);
        } else {
            coin.setUnlockHeight(height.longValue());
        }
        data.addTo(address, coin);
        total = total.add(coin.getNa());
    }
    data.setTotalNa(total);
    CoinBaseTransaction tx = null;
    try {
        tx = new CoinBaseTransaction(data, null);
    } catch (NulsException e) {
        Log.error(e);
        throw new NulsRuntimeException(e);
    }
    tx.setTime(this.blockTime);
    tx.setFee(Na.ZERO);
    try {
        tx.setHash(NulsDigestData.calcDigestData(tx.serialize()));
    } catch (IOException e) {
        Log.error(e);
        throw new NulsRuntimeException(e);
    }
    P2PKHScriptSig p2PKHScriptSig = new P2PKHScriptSig();
    Account account = null;
    try {
        account = AccountTool.createAccount(priKey);
    } catch (NulsException e) {
        e.printStackTrace();
    }
    AccountService accountService = NulsContext.getServiceBean(AccountService.class);
    P2PKHScriptSig scriptSig = null;
    try {
        scriptSig = accountService.createP2PKHScriptSigFromDigest(tx.getHash(), account, "");
    } catch (NulsException e) {
        e.printStackTrace();
    }
    try {
        tx.setScriptSig(scriptSig.serialize());
    } catch (IOException e) {
        e.printStackTrace();
    }
    List<Transaction> txlist = new ArrayList<>();
    // tx.setStatus(TxStatusEnum.AGREED);
    txlist.add(tx);
    setTxs(txlist);
}
Also used : Account(io.nuls.account.entity.Account) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) IOException(java.io.IOException) BigInteger(java.math.BigInteger) Coin(io.nuls.ledger.entity.params.Coin) P2PKHScriptSig(io.nuls.core.script.P2PKHScriptSig) CoinTransferData(io.nuls.ledger.entity.params.CoinTransferData) CoinBaseTransaction(io.nuls.ledger.entity.tx.CoinBaseTransaction) CoinBaseTransaction(io.nuls.ledger.entity.tx.CoinBaseTransaction) NulsException(io.nuls.core.exception.NulsException) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) AccountService(io.nuls.account.service.intf.AccountService)

Example 9 with P2PKHScriptSig

use of io.nuls.core.script.P2PKHScriptSig 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

P2PKHScriptSig (io.nuls.core.script.P2PKHScriptSig)9 IOException (java.io.IOException)4 NulsException (io.nuls.core.exception.NulsException)3 ArrayList (java.util.ArrayList)3 NulsDigestData (io.nuls.core.chain.entity.NulsDigestData)2 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)2 NulsByteBuffer (io.nuls.core.utils.io.NulsByteBuffer)2 CoinBaseTransaction (io.nuls.ledger.entity.tx.CoinBaseTransaction)2 BigInteger (java.math.BigInteger)2 Account (io.nuls.account.entity.Account)1 AccountService (io.nuls.account.service.intf.AccountService)1 BlockRoundData (io.nuls.consensus.entity.block.BlockRoundData)1 BlockHeader (io.nuls.core.chain.entity.BlockHeader)1 ValidateResult (io.nuls.core.validate.ValidateResult)1 UtxoData (io.nuls.ledger.entity.UtxoData)1 UtxoInput (io.nuls.ledger.entity.UtxoInput)1 UtxoOutput (io.nuls.ledger.entity.UtxoOutput)1 Coin (io.nuls.ledger.entity.params.Coin)1 CoinTransferData (io.nuls.ledger.entity.params.CoinTransferData)1 List (java.util.List)1