Search in sources :

Example 46 with NulsException

use of io.nuls.core.exception.NulsException in project nuls by nuls-io.

the class MicroKernelBootstrap method init.

@Override
public void init() {
    try {
        NulsContext.NULS_CONFIG = ConfigLoader.loadIni(NulsConstant.USER_CONFIG_FILE);
        NulsContext.MODULES_CONFIG = ConfigLoader.loadIni(NulsConstant.MODULES_CONFIG_FILE);
    } catch (IOException e) {
        Log.error("Client start failed", e);
        throw new NulsRuntimeException(ErrorCode.FAILED, "Client start failed");
    }
    // set system language
    try {
        NulsContext.DEFAULT_ENCODING = NulsContext.NULS_CONFIG.getCfgValue(NulsConstant.CFG_SYSTEM_SECTION, NulsConstant.CFG_SYSTEM_DEFAULT_ENCODING);
        String language = NulsContext.NULS_CONFIG.getCfgValue(NulsConstant.CFG_SYSTEM_SECTION, NulsConstant.CFG_SYSTEM_LANGUAGE);
        I18nUtils.setLanguage(language);
    } catch (NulsException e) {
        Log.error(e);
    }
    SpringLiteContext.init("io.nuls", new ModularServiceMethodInterceptor());
    try {
        VersionManager.start();
    } catch (NulsException e) {
        Log.error(e);
    }
}
Also used : ModularServiceMethodInterceptor(io.nuls.core.utils.spring.lite.core.ModularServiceMethodInterceptor) NulsException(io.nuls.core.exception.NulsException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) IOException(java.io.IOException)

Example 47 with NulsException

use of io.nuls.core.exception.NulsException in project nuls by nuls-io.

the class AccountServiceImpl method unlockAccounts.

@Override
public Result unlockAccounts(final String password, int seconds) {
    List<Account> accounts = this.getAccountList();
    if (accounts == null || accounts.isEmpty()) {
        return new Result(false, "No account was found");
    }
    Account acct = accounts.get(0);
    if (!acct.isEncrypted()) {
        return new Result(false, "No password has been set up yet");
    }
    if (!StringUtils.validPassword(password)) {
        return new Result(false, "Length between 8 and 20, the combination of characters and numbers");
    }
    for (Account account : accounts) {
        try {
            if (!account.unlock(password)) {
                return Result.getFailed(ErrorCode.PASSWORD_IS_WRONG);
            }
        } catch (NulsException e) {
            return Result.getFailed(ErrorCode.PASSWORD_IS_WRONG);
        }
    }
    isLockNow = false;
    TaskManager.createAndRunThread(NulsConstant.MODULE_ID_ACCOUNT, "unlockAccountThread", new Runnable() {

        @Override
        public void run() {
            isLockNow = true;
            try {
                Thread.sleep(seconds * 1000);
            } catch (InterruptedException e) {
                Log.error(e);
            }
            try {
                resetKeys(password);
            } catch (NulsException e) {
                Log.error("unlockAccounts resetKey error", e);
            }
            isLockNow = false;
        }
    });
    return new Result(true, "OK");
}
Also used : Account(io.nuls.account.entity.Account) NulsException(io.nuls.core.exception.NulsException) ValidateResult(io.nuls.core.validate.ValidateResult) Result(io.nuls.core.chain.entity.Result)

Example 48 with NulsException

use of io.nuls.core.exception.NulsException in project nuls by nuls-io.

the class Account method decrypt.

public boolean decrypt(String password) throws NulsException {
    try {
        byte[] unencryptedPrivateKey = AESEncrypt.decrypt(this.getEncryptedPriKey(), password);
        BigInteger newPriv = new BigInteger(1, unencryptedPrivateKey);
        ECKey key = ECKey.fromPrivate(newPriv);
        // todo  pub key compress?
        if (!Arrays.equals(key.getPubKey(), getPubKey())) {
            return false;
        }
        key.setEncryptedPrivateKey(new EncryptedData(this.getEncryptedPriKey()));
        this.setPriKey(key.getPrivKeyBytes());
        this.setEcKey(key);
    } catch (Exception e) {
        throw new NulsException(ErrorCode.PASSWORD_IS_WRONG);
    }
    return true;
}
Also used : NulsException(io.nuls.core.exception.NulsException) BigInteger(java.math.BigInteger) NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 49 with NulsException

use of io.nuls.core.exception.NulsException in project nuls by nuls-io.

the class NewTxEventHandler method onEvent.

@Override
public void onEvent(TransactionEvent event, String fromId) {
    Transaction tx = event.getEventBody();
    if (null == tx) {
        return;
    }
    boolean isMine = false;
    try {
        isMine = ledgerService.checkTxIsMine(tx);
    } catch (NulsException e) {
        Log.error(e);
    }
    ValidateResult result = tx.verify();
    if (result.isFailed()) {
        if (result.getErrorCode() == ErrorCode.ORPHAN_TX) {
            orphanTxCacheManager.putTx(tx);
            eventBroadcaster.broadcastHashAndCacheAysn(event, false, fromId);
            return;
        }
        if (result.getLevel() == SeverityLevelEnum.NORMAL_FOUL) {
            Log.info("-----------------------------------------------newTxHandler remove node:" + fromId);
            networkService.removeNode(fromId);
        } else if (result.getLevel() == SeverityLevelEnum.FLAGRANT_FOUL) {
            networkService.blackNode(fromId, NodePo.BLACK);
        }
        return;
    }
    try {
        if (isMine) {
            ledgerService.approvalTx(tx);
        }
        cacheManager.putTx(tx);
        eventBroadcaster.broadcastHashAndCacheAysn(event, false, fromId);
    } catch (Exception e) {
        Log.error(e);
    }
}
Also used : Transaction(io.nuls.core.chain.entity.Transaction) NulsException(io.nuls.core.exception.NulsException) ValidateResult(io.nuls.core.validate.ValidateResult) NulsException(io.nuls.core.exception.NulsException)

Example 50 with NulsException

use of io.nuls.core.exception.NulsException in project nuls by nuls-io.

the class CoinbaseValidator method validate.

@Override
public ValidateResult validate(Block block) {
    if (null == block || block.getHeader() == null || null == block.getTxs() || block.getTxs().isEmpty()) {
        return ValidateResult.getFailedResult(ErrorCode.DATA_FIELD_CHECK_ERROR);
    }
    Transaction tx = block.getTxs().get(0);
    if (tx.getType() != TransactionConstant.TX_TYPE_COIN_BASE) {
        return ValidateResult.getFailedResult("Coinbase transaction order wrong!");
    }
    for (int i = 1; i < block.getTxs().size(); i++) {
        Transaction transaction = block.getTxs().get(i);
        if (transaction.getType() == TransactionConstant.TX_TYPE_COIN_BASE) {
            ValidateResult result = ValidateResult.getFailedResult("Coinbase transaction more than one!");
            result.setLevel(SeverityLevelEnum.FLAGRANT_FOUL);
            return result;
        }
    }
    BlockRoundData blockRound = null;
    try {
        blockRound = new BlockRoundData(block.getHeader().getExtend());
    } catch (NulsException e) {
        Log.error(e);
    }
    if (null == blockRound) {
        return ValidateResult.getFailedResult("Cann't get the round data!");
    }
    return ValidateResult.getSuccessResult();
}
Also used : Transaction(io.nuls.core.chain.entity.Transaction) NulsException(io.nuls.core.exception.NulsException) ValidateResult(io.nuls.core.validate.ValidateResult) BlockRoundData(io.nuls.consensus.entity.block.BlockRoundData)

Aggregations

NulsException (io.nuls.core.exception.NulsException)69 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)32 IOException (java.io.IOException)17 ValidateResult (io.nuls.core.validate.ValidateResult)12 Account (io.nuls.account.entity.Account)11 Transaction (io.nuls.core.chain.entity.Transaction)8 BlockRoundData (io.nuls.consensus.entity.block.BlockRoundData)7 Block (io.nuls.core.chain.entity.Block)6 DbSession (io.nuls.db.transactional.annotation.DbSession)6 Coin (io.nuls.ledger.entity.params.Coin)6 CoinTransferData (io.nuls.ledger.entity.params.CoinTransferData)6 Result (io.nuls.core.chain.entity.Result)5 TransactionEvent (io.nuls.ledger.event.TransactionEvent)5 BlockHeader (io.nuls.core.chain.entity.BlockHeader)4 NulsDigestData (io.nuls.core.chain.entity.NulsDigestData)4 Address (io.nuls.account.entity.Address)3 PocMeetingRound (io.nuls.consensus.entity.meeting.PocMeetingRound)3 P2PKHScriptSig (io.nuls.core.script.P2PKHScriptSig)3 NulsByteBuffer (io.nuls.core.utils.io.NulsByteBuffer)3 AccountPo (io.nuls.db.entity.AccountPo)3