Search in sources :

Example 1 with AccountPo

use of io.nuls.db.entity.AccountPo in project nuls by nuls-io.

the class AccountServiceImpl method changePassword.

@Override
public Result changePassword(String oldPassword, String newPassword) {
    if (!StringUtils.validPassword(oldPassword)) {
        return new Result(false, "Length between 8 and 20, the combination of characters and numbers");
    }
    if (!StringUtils.validPassword(newPassword)) {
        return new Result(false, "Length between 8 and 20, the combination of characters and numbers");
    }
    List<Account> accounts = this.getAccountList();
    if (accounts == null || accounts.isEmpty()) {
        new Result(false, "No account was found");
    }
    try {
        Account acct = accounts.get(0);
        if (!acct.isEncrypted()) {
            return new Result(false, "No password has been set up yet");
        }
        List<AccountPo> accountPoList = new ArrayList<>();
        for (Account account : accounts) {
            if (!account.unlock(oldPassword)) {
                return new Result(false, "old password error");
            }
            account.encrypt(newPassword, true);
            AccountPo po = new AccountPo();
            AccountTool.toPojo(account, po);
            accountPoList.add(po);
        }
        if (accountPoList.size() > 0) {
            accountDao.update(accountPoList);
        }
        accountCacheService.putAccountList(accounts);
    } catch (Exception e) {
        Log.error(e);
        return new Result(false, "change password failed");
    }
    this.eventBroadcaster.publishToLocal(new PasswordChangeNotice());
    return new Result(true, "OK");
}
Also used : Account(io.nuls.account.entity.Account) AccountPo(io.nuls.db.entity.AccountPo) 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)

Example 2 with AccountPo

use of io.nuls.db.entity.AccountPo 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 AccountPo

use of io.nuls.db.entity.AccountPo 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 AccountPo

use of io.nuls.db.entity.AccountPo 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 AccountPo

use of io.nuls.db.entity.AccountPo in project nuls by nuls-io.

the class AccountServiceImpl method importSave.

private void importSave(List<Account> accounts) throws Exception {
    List<AccountPo> accountPoList = new ArrayList<>();
    for (Account account : accounts) {
        AccountPo accountPo = new AccountPo();
        AccountTool.toPojo(account, accountPo);
        List<TransactionLocalPo> transactionPos = new ArrayList<>();
        for (Transaction tx : account.getMyTxs()) {
            TransactionLocalPo po = UtxoTransferTool.toLocalTransactionPojo(tx);
            transactionPos.add(po);
        }
        accountPo.setMyTxs(transactionPos);
        accountPoList.add(accountPo);
    }
    accountAliasDBService.importAccount(accountPoList);
}
Also used : Account(io.nuls.account.entity.Account) Transaction(io.nuls.core.chain.entity.Transaction) AliasTransaction(io.nuls.account.entity.tx.AliasTransaction) AccountPo(io.nuls.db.entity.AccountPo) TransactionLocalPo(io.nuls.db.entity.TransactionLocalPo)

Aggregations

AccountPo (io.nuls.db.entity.AccountPo)10 Account (io.nuls.account.entity.Account)7 DbSession (io.nuls.db.transactional.annotation.DbSession)6 Result (io.nuls.core.chain.entity.Result)5 NulsException (io.nuls.core.exception.NulsException)5 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)5 ValidateResult (io.nuls.core.validate.ValidateResult)4 AliasPo (io.nuls.db.entity.AliasPo)3 IOException (java.io.IOException)3 TransactionLocalPo (io.nuls.db.entity.TransactionLocalPo)2 AliasTransaction (io.nuls.account.entity.tx.AliasTransaction)1 Transaction (io.nuls.core.chain.entity.Transaction)1