Search in sources :

Example 6 with NulsException

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

the class AccountServiceImpl method importAccounts.

@Override
@DbSession
public Result importAccounts(List<String> keys, String password) {
    Account account = null;
    AccountPo accountPo = null;
    AliasPo aliasPo = null;
    Account defaultAcct = getDefaultAccount();
    if (defaultAcct != null) {
        try {
            if (!defaultAcct.decrypt(password)) {
                return Result.getFailed(ErrorCode.PASSWORD_IS_WRONG);
            }
            defaultAcct.encrypt(password);
        } catch (NulsException e) {
        }
    }
    for (String priKey : keys) {
        try {
            account = AccountTool.createAccount(priKey);
            account.encrypt(password);
        } catch (NulsException e) {
            return Result.getFailed("invalid prikey");
        }
        accountPo = accountDao.get(account.getAddress().getBase58());
        if (accountPo != null) {
            continue;
        } else {
            accountPo = new AccountPo();
        }
        // save db
        AccountTool.toPojo(account, accountPo);
        aliasPo = aliasDataService.getByAddress(accountPo.getAddress());
        if (aliasPo != null) {
            account.setAlias(aliasPo.getAlias());
            accountPo.setAlias(aliasPo.getAlias());
        }
        accountDao.save(accountPo);
        ledgerService.saveTxInLocal(accountPo.getAddress());
        // save cache
        accountCacheService.putAccount(account);
        NulsContext.LOCAL_ADDRESS_LIST.add(accountPo.getAddress());
        ledgerService.getBalance(accountPo.getAddress());
    }
    return Result.getSuccess();
}
Also used : Account(io.nuls.account.entity.Account) NulsException(io.nuls.core.exception.NulsException) AccountPo(io.nuls.db.entity.AccountPo) AliasPo(io.nuls.db.entity.AliasPo) DbSession(io.nuls.db.transactional.annotation.DbSession)

Example 7 with NulsException

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

the class AliasTxService method onApproval.

@Override
public void onApproval(AliasTransaction tx) throws NulsException {
    Alias alias = tx.getTxData();
    AliasPo po = getAliasDataService().getAlias(alias.getAlias());
    if (po != null) {
        throw new NulsException(ErrorCode.ALIAS_EXIST);
    }
}
Also used : Alias(io.nuls.account.entity.Alias) NulsException(io.nuls.core.exception.NulsException) AliasPo(io.nuls.db.entity.AliasPo)

Example 8 with NulsException

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

the class Account method encrypt.

/**
 * @param password
 */
public void encrypt(String password, boolean isForce) throws NulsException {
    if (this.isEncrypted() && !isForce) {
        if (!unlock(password)) {
            throw new NulsException(ErrorCode.ACCOUNT_IS_ALREADY_ENCRYPTED);
        }
    }
    ECKey eckey = this.getEcKey();
    byte[] privKeyBytes = eckey.getPrivKeyBytes();
    EncryptedData encryptedPrivateKey = AESEncrypt.encrypt(privKeyBytes, EncryptedData.DEFAULT_IV, new KeyParameter(Sha256Hash.hash(password.getBytes())));
    eckey.setEncryptedPrivateKey(encryptedPrivateKey);
    ECKey result = ECKey.fromEncrypted(encryptedPrivateKey, getPubKey());
    this.setPriKey(new byte[0]);
    this.setEcKey(result);
    this.setEncryptedPriKey(encryptedPrivateKey.getEncryptedBytes());
}
Also used : NulsException(io.nuls.core.exception.NulsException) KeyParameter(org.spongycastle.crypto.params.KeyParameter)

Example 9 with NulsException

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

the class Alias method parse.

@Override
protected void parse(NulsByteBuffer byteBuffer) throws NulsException {
    try {
        this.address = new String(byteBuffer.readByLengthByte(), NulsContext.DEFAULT_ENCODING);
        this.alias = new String(byteBuffer.readByLengthByte(), NulsContext.DEFAULT_ENCODING);
    } catch (UnsupportedEncodingException e) {
        Log.error(e);
        throw new NulsException(ErrorCode.DATA_ERROR);
    }
}
Also used : NulsException(io.nuls.core.exception.NulsException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 10 with NulsException

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

the class AccountTool method createAccount.

public static Account createAccount(String prikey) throws NulsException {
    ECKey key = null;
    if (StringUtils.isBlank(prikey)) {
        key = new ECKey();
    } else {
        try {
            key = ECKey.fromPrivate(new BigInteger(Hex.decode(prikey)));
        } catch (Exception e) {
            throw new NulsException(ErrorCode.DATA_PARSE_ERROR);
        }
    }
    Address address = new Address(NulsContext.getInstance().getChainId(NulsContext.CHAIN_ID), Utils.sha256hash160(key.getPubKey()));
    Account account = new Account();
    account.setEncryptedPriKey(new byte[0]);
    account.setAddress(address);
    account.setPubKey(key.getPubKey());
    account.setEcKey(key);
    account.setPriKey(key.getPrivKeyBytes());
    account.setCreateTime(TimeService.currentTimeMillis());
    return account;
}
Also used : Account(io.nuls.account.entity.Account) Address(io.nuls.account.entity.Address) NulsException(io.nuls.core.exception.NulsException) BigInteger(java.math.BigInteger) ECKey(io.nuls.core.crypto.ECKey) NulsException(io.nuls.core.exception.NulsException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

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