use of io.nuls.core.exception.NulsException 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();
}
use of io.nuls.core.exception.NulsException 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();
}
}
use of io.nuls.core.exception.NulsException 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;
}
use of io.nuls.core.exception.NulsException 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);
}
use of io.nuls.core.exception.NulsException 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);
}
}
}
Aggregations