Search in sources :

Example 6 with Account

use of io.nuls.account.entity.Account in project nuls by nuls-io.

the class AccountServiceImpl method exportAccounts.

@Override
public Result exportAccounts(String password) {
    List<Account> accounts = accountCacheService.getAccountList();
    if (null == accounts || accounts.isEmpty()) {
        return Result.getFailed("no account can export");
    }
    List<String> prikeyList = new ArrayList<>();
    for (Account account : accounts) {
        try {
            account.decrypt(password);
            prikeyList.add(Hex.encode(account.getPriKey()));
            account.encrypt(password);
        } catch (NulsException e) {
            return Result.getFailed(ErrorCode.PASSWORD_IS_WRONG);
        }
    }
    Map<String, Object> map = new HashMap<>();
    map.put("prikeys", prikeyList);
    map.put("password", MD5Util.md5(password));
    return new Result(true, "OK", map);
}
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 7 with Account

use of io.nuls.account.entity.Account in project nuls by nuls-io.

the class AccountServiceImpl method setDefaultAccount.

@Override
public void setDefaultAccount(String id) {
    if (id == null) {
        return;
    }
    Account account = accountCacheService.getAccountById(id);
    if (null != account) {
        NulsContext.DEFAULT_ACCOUNT_ID = id;
    } else {
        throw new NulsRuntimeException(ErrorCode.FAILED, "The account not exist,id:" + id);
    }
    DefaultAccountChangeNotice notice = new DefaultAccountChangeNotice();
    notice.setEventBody(account);
    eventBroadcaster.publishToLocal(notice);
}
Also used : Account(io.nuls.account.entity.Account) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 8 with Account

use of io.nuls.account.entity.Account in project nuls by nuls-io.

the class AccountServiceImpl method getPrivateKey.

@Override
public Result getPrivateKey(String address, String password) {
    AssertUtil.canNotEmpty(address, "");
    Account account = accountCacheService.getAccountByAddress(address);
    if (account == null) {
        return Result.getFailed(ErrorCode.ACCOUNT_NOT_EXIST);
    }
    if (!account.isLocked()) {
        Result result = new Result(true, "OK", Hex.encode(account.getPriKey()));
        return result;
    } else {
        try {
            if (!account.unlock(password)) {
                return Result.getFailed(ErrorCode.PASSWORD_IS_WRONG);
            }
            byte[] publicKeyBytes = account.getPriKey();
            account.lock();
            return new Result(true, "OK", Hex.encode(publicKeyBytes));
        } catch (NulsException e) {
            return Result.getFailed(ErrorCode.PASSWORD_IS_WRONG);
        }
    }
}
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 9 with Account

use of io.nuls.account.entity.Account 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 10 with Account

use of io.nuls.account.entity.Account in project nuls by nuls-io.

the class AccountServiceImpl method isEncrypted.

@Override
public boolean isEncrypted() {
    if (!isLockNow) {
        return false;
    }
    List<Account> accounts = this.getAccountList();
    if (accounts == null || accounts.size() == 0) {
        return false;
    }
    Account account = accounts.get(0);
    return account.isEncrypted();
}
Also used : Account(io.nuls.account.entity.Account)

Aggregations

Account (io.nuls.account.entity.Account)28 NulsException (io.nuls.core.exception.NulsException)14 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)11 Result (io.nuls.core.chain.entity.Result)9 ValidateResult (io.nuls.core.validate.ValidateResult)9 AccountPo (io.nuls.db.entity.AccountPo)7 IOException (java.io.IOException)7 DbSession (io.nuls.db.transactional.annotation.DbSession)4 AccountService (io.nuls.account.service.intf.AccountService)3 Agent (io.nuls.consensus.entity.member.Agent)3 CoinTransferData (io.nuls.ledger.entity.params.CoinTransferData)3 Address (io.nuls.account.entity.Address)2 Alias (io.nuls.account.entity.Alias)2 AliasTransaction (io.nuls.account.entity.tx.AliasTransaction)2 Consensus (io.nuls.consensus.entity.Consensus)2 Deposit (io.nuls.consensus.entity.member.Deposit)2 Transaction (io.nuls.core.chain.entity.Transaction)2 AliasPo (io.nuls.db.entity.AliasPo)2 Coin (io.nuls.ledger.entity.params.Coin)2 LockNulsTransaction (io.nuls.ledger.entity.tx.LockNulsTransaction)2