Search in sources :

Example 1 with DbSession

use of io.nuls.db.transactional.annotation.DbSession in project nuls by nuls-io.

the class AccountServiceImpl method removeAccount.

@Override
@DbSession
public Result removeAccount(String address, String password) {
    Account account = getAccount(address);
    if (account == null) {
        return Result.getFailed(ErrorCode.ACCOUNT_NOT_EXIST);
    }
    try {
        if (!account.decrypt(password)) {
            return Result.getFailed(ErrorCode.PASSWORD_IS_WRONG);
        }
    } catch (NulsException e) {
        return Result.getFailed(ErrorCode.PASSWORD_IS_WRONG);
    }
    accountDao.delete(address);
    accountCacheService.removeAccount(address);
    NulsContext.LOCAL_ADDRESS_LIST.remove(address);
    if (NulsContext.DEFAULT_ACCOUNT_ID != null && NulsContext.DEFAULT_ACCOUNT_ID.equals(address)) {
        NulsContext.DEFAULT_ACCOUNT_ID = null;
    }
    return Result.getSuccess();
}
Also used : Account(io.nuls.account.entity.Account) NulsException(io.nuls.core.exception.NulsException) DbSession(io.nuls.db.transactional.annotation.DbSession)

Example 2 with DbSession

use of io.nuls.db.transactional.annotation.DbSession in project nuls by nuls-io.

the class AccountServiceImpl method createAccount.

@Override
@DbSession
public Result<List<String>> createAccount(int count, String password) {
    if (count <= 0 || count > AccountTool.CREATE_MAX_SIZE) {
        return new Result<>(false, "between 0 and 100 can be created at once");
    }
    // todo need to recover the status of the wallet.
    if (!StringUtils.validPassword(password)) {
        return new Result(false, "Length between 8 and 20, the combination of characters and numbers");
    }
    Account defaultAccount = getDefaultAccount();
    if (defaultAccount != null && defaultAccount.isEncrypted()) {
        try {
            if (!defaultAccount.unlock(password)) {
                return Result.getFailed(ErrorCode.PASSWORD_IS_WRONG);
            }
            defaultAccount.lock();
        } catch (NulsException e) {
            return Result.getFailed(ErrorCode.PASSWORD_IS_WRONG);
        }
    }
    locker.lock();
    try {
        List<Account> accounts = new ArrayList<>();
        List<AccountPo> accountPos = new ArrayList<>();
        List<String> resultList = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            Account account = AccountTool.createAccount();
            signAccount(account);
            account.encrypt(password);
            AccountPo po = new AccountPo();
            AccountTool.toPojo(account, po);
            accounts.add(account);
            accountPos.add(po);
            resultList.add(account.getAddress().getBase58());
        }
        accountDao.save(accountPos);
        accountCacheService.putAccountList(accounts);
        NulsContext.LOCAL_ADDRESS_LIST.addAll(resultList);
        for (Account account : accounts) {
            AccountCreateNotice notice = new AccountCreateNotice();
            notice.setEventBody(account);
            eventBroadcaster.publishToLocal(notice);
        }
        return new Result<>(true, "OK", resultList);
    } catch (Exception e) {
        Log.error(e);
        // todo remove newaccounts
        throw new NulsRuntimeException(ErrorCode.FAILED, "create account failed!");
    } finally {
        locker.unlock();
    }
}
Also used : Account(io.nuls.account.entity.Account) AccountPo(io.nuls.db.entity.AccountPo) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) ValidateResult(io.nuls.core.validate.ValidateResult) Result(io.nuls.core.chain.entity.Result) NulsException(io.nuls.core.exception.NulsException) DbSession(io.nuls.db.transactional.annotation.DbSession)

Example 3 with DbSession

use of io.nuls.db.transactional.annotation.DbSession in project nuls by nuls-io.

the class AccountServiceImpl method importAccount.

@Override
@DbSession
public Result importAccount(String priKey, String password) {
    Account account = null;
    try {
        account = AccountTool.createAccount(priKey);
    } catch (NulsException e) {
        return Result.getFailed("invalid prikey");
    }
    // maybe account has been imported
    AccountPo accountPo = accountDao.get(account.getAddress().getBase58());
    if (accountPo != null) {
        return Result.getFailed(ErrorCode.ACCOUNT_EXIST);
    } else {
        accountPo = new AccountPo();
    }
    Account defaultAcct = getDefaultAccount();
    if (defaultAcct != null) {
        try {
            if (!defaultAcct.unlock(password)) {
                return Result.getFailed(ErrorCode.PASSWORD_IS_WRONG);
            }
            defaultAcct.lock();
        } catch (NulsException e) {
            e.printStackTrace();
            return Result.getFailed("ErrorCode.PASSWORD_IS_WRONG");
        }
    }
    try {
        account.encrypt(password);
    } catch (NulsException e) {
        e.printStackTrace();
    }
    // save db
    AccountTool.toPojo(account, accountPo);
    AliasPo 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());
    if (getDefaultAccount() == null) {
        setDefaultAccount(account.getAddress().getBase58());
    }
    AccountImportedNotice notice = new AccountImportedNotice();
    notice.setEventBody(account);
    eventBroadcaster.publishToLocal(notice);
    Result result = Result.getSuccess();
    result.setObject(accountPo.getAddress());
    return result;
}
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) ValidateResult(io.nuls.core.validate.ValidateResult) Result(io.nuls.core.chain.entity.Result) DbSession(io.nuls.db.transactional.annotation.DbSession)

Example 4 with DbSession

use of io.nuls.db.transactional.annotation.DbSession 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 5 with DbSession

use of io.nuls.db.transactional.annotation.DbSession in project nuls by nuls-io.

the class UtxoCoinDataProvider method save.

/**
 * 1. change spending output status  (cache and database)
 * 2. save new input
 * 3. save new unSpend output (cache and database)
 * 4. finally, calc balance
 */
@Override
@DbSession
public void save(CoinData coinData, Transaction tx) throws NulsException {
    UtxoData utxoData = (UtxoData) coinData;
    List<UtxoInputPo> inputPoList = new ArrayList<>();
    List<UtxoOutput> spends = new ArrayList<>();
    List<UtxoOutputPo> spendPoList = new ArrayList<>();
    List<TxAccountRelationPo> txRelations = new ArrayList<>();
    Set<String> addressSet = new HashSet<>();
    try {
        processDataInput(utxoData, inputPoList, spends, spendPoList, addressSet);
        List<UtxoOutputPo> outputPoList = new ArrayList<>();
        for (int i = 0; i < utxoData.getOutputs().size(); i++) {
            UtxoOutput output = utxoData.getOutputs().get(i);
            output = ledgerCacheService.getUtxo(output.getKey());
            if (output == null) {
                throw new NulsRuntimeException(ErrorCode.DATA_NOT_FOUND);
            }
            if (output.isConfirm() || OutPutStatusEnum.UTXO_SPENT == output.getStatus()) {
                Log.error("-----------------------------------save() output status is" + output.getStatus().name());
                throw new NulsRuntimeException(ErrorCode.DATA_ERROR, "use a not legal utxo");
            }
            if (OutPutStatusEnum.UTXO_UNCONFIRM_CONSENSUS_LOCK == output.getStatus()) {
                output.setStatus(OutPutStatusEnum.UTXO_CONFIRM_CONSENSUS_LOCK);
            } else if (OutPutStatusEnum.UTXO_UNCONFIRM_TIME_LOCK == output.getStatus()) {
                output.setStatus(OutPutStatusEnum.UTXO_CONFIRM_TIME_LOCK);
            } else if (OutPutStatusEnum.UTXO_UNCONFIRM_UNSPEND == output.getStatus()) {
                output.setStatus(OutPutStatusEnum.UTXO_CONFIRM_UNSPEND);
            } else if (OutPutStatusEnum.UTXO_UNCONFIRM_SPEND == output.getStatus()) {
                output.setStatus(OutPutStatusEnum.UTXO_CONFIRM_SPEND);
            }
            UtxoOutputPo outputPo = UtxoTransferTool.toOutputPojo(output);
            outputPoList.add(outputPo);
            addressSet.add(Address.fromHashs(output.getAddress()).getBase58());
        }
        for (String address : addressSet) {
            TxAccountRelationPo relationPo = new TxAccountRelationPo(tx.getHash().getDigestHex(), address);
            txRelations.add(relationPo);
        }
        outputDataService.updateStatus(spendPoList);
        inputDataService.save(inputPoList);
        outputDataService.save(outputPoList);
        relationDataService.save(txRelations);
        afterSaveDatabase(spends, utxoData, tx);
        for (String address : addressSet) {
            UtxoTransactionTool.getInstance().calcBalance(address, true);
        }
    } catch (Exception e) {
        // }
        throw e;
    }
}
Also used : TxAccountRelationPo(io.nuls.db.entity.TxAccountRelationPo) UtxoOutputPo(io.nuls.db.entity.UtxoOutputPo) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) NulsException(io.nuls.core.exception.NulsException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) UtxoInputPo(io.nuls.db.entity.UtxoInputPo) DbSession(io.nuls.db.transactional.annotation.DbSession)

Aggregations

DbSession (io.nuls.db.transactional.annotation.DbSession)15 NulsException (io.nuls.core.exception.NulsException)7 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)6 AccountPo (io.nuls.db.entity.AccountPo)6 Account (io.nuls.account.entity.Account)4 Result (io.nuls.core.chain.entity.Result)3 AliasPo (io.nuls.db.entity.AliasPo)3 TransactionLocalPo (io.nuls.db.entity.TransactionLocalPo)3 ValidateResult (io.nuls.core.validate.ValidateResult)2 TransactionPo (io.nuls.db.entity.TransactionPo)2 UtxoInputPo (io.nuls.db.entity.UtxoInputPo)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Block (io.nuls.core.chain.entity.Block)1 Transaction (io.nuls.core.chain.entity.Transaction)1 Searchable (io.nuls.db.dao.impl.mybatis.util.Searchable)1 TxAccountRelationPo (io.nuls.db.entity.TxAccountRelationPo)1 UtxoOutputPo (io.nuls.db.entity.UtxoOutputPo)1 UtxoOutput (io.nuls.ledger.entity.UtxoOutput)1 AbstractCoinTransaction (io.nuls.ledger.entity.tx.AbstractCoinTransaction)1