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);
}
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);
}
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);
}
}
}
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();
}
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();
}
Aggregations