Search in sources :

Example 1 with AliasPo

use of io.nuls.db.entity.AliasPo 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 2 with AliasPo

use of io.nuls.db.entity.AliasPo 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 3 with AliasPo

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

the class AccountServiceImpl method getAlias.

@Override
public Alias getAlias(String address) {
    AliasPo aliasPo = aliasDataService.getByAddress(address);
    if (aliasPo == null) {
        return null;
    }
    Alias alias = new Alias(aliasPo.getAddress(), aliasPo.getAlias());
    return alias;
}
Also used : Alias(io.nuls.account.entity.Alias) AliasPo(io.nuls.db.entity.AliasPo)

Example 4 with AliasPo

use of io.nuls.db.entity.AliasPo 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 5 with AliasPo

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

the class AliasTxService method onRollback.

@Override
public void onRollback(AliasTransaction tx) throws NulsException {
    AliasPo po = AccountTool.toAliasPojo(tx.getTxData());
    getAliasDataService().rollbackAlias(po);
}
Also used : AliasPo(io.nuls.db.entity.AliasPo)

Aggregations

AliasPo (io.nuls.db.entity.AliasPo)8 Alias (io.nuls.account.entity.Alias)3 NulsException (io.nuls.core.exception.NulsException)3 AccountPo (io.nuls.db.entity.AccountPo)3 DbSession (io.nuls.db.transactional.annotation.DbSession)3 Account (io.nuls.account.entity.Account)2 Address (io.nuls.account.entity.Address)1 Result (io.nuls.core.chain.entity.Result)1 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)1 ValidateResult (io.nuls.core.validate.ValidateResult)1