use of io.nuls.account.entity.Account in project nuls by nuls-io.
the class AccountServiceImpl method exportAccount.
// @Override
public Result exportAccount(String filePath) {
if (StringUtils.isBlank(filePath)) {
return new Result(false, "filePath is required");
}
Account account = getDefaultAccount();
if (account == null) {
return new Result(false, "no account can export");
}
Result result = backUpFile(filePath);
if (!result.isSuccess()) {
return result;
}
return exportAccount(account, (File) result.getObject());
}
use of io.nuls.account.entity.Account in project nuls by nuls-io.
the class AccountServiceImpl method setAlias.
@Override
public Result setAlias(String address, String password, String alias) {
Account account = getAccount(address);
if (account == null) {
return new Result(false, ErrorCode.ACCOUNT_NOT_EXIST, null);
}
if (StringUtils.isNotBlank(account.getAlias())) {
return new Result(false, "Alias has been set up");
}
if (!StringUtils.validAlias(alias)) {
return new Result(false, "The alias is between 3 to 20 characters");
}
try {
TransactionEvent event = new TransactionEvent();
CoinTransferData coinData = new CoinTransferData(OperationType.TRANSFER, AccountConstant.ALIAS_NA, address, ledgerService.getTxFee(TransactionConstant.TX_TYPE_SET_ALIAS));
AliasTransaction aliasTx = new AliasTransaction(coinData, password, new Alias(address, alias));
aliasTx.setHash(NulsDigestData.calcDigestData(aliasTx.serialize()));
aliasTx.setScriptSig(createP2PKHScriptSigFromDigest(aliasTx.getHash(), account, password).serialize());
ValidateResult validate = aliasTx.verify();
if (validate.isFailed()) {
return new Result(false, validate.getMessage());
}
event.setEventBody(aliasTx);
eventBroadcaster.broadcastAndCache(event, true);
SetAliasNotice notice = new SetAliasNotice();
notice.setEventBody(aliasTx);
eventBroadcaster.publishToLocal(notice);
} catch (Exception e) {
Log.error(e);
return new Result(false, e.getMessage());
}
return new Result(true, "OK");
}
use of io.nuls.account.entity.Account in project nuls by nuls-io.
the class AccountServiceImpl method unlockAccounts.
@Override
public Result unlockAccounts(final String password, int seconds) {
List<Account> accounts = this.getAccountList();
if (accounts == null || accounts.isEmpty()) {
return new Result(false, "No account was found");
}
Account acct = accounts.get(0);
if (!acct.isEncrypted()) {
return new Result(false, "No password has been set up yet");
}
if (!StringUtils.validPassword(password)) {
return new Result(false, "Length between 8 and 20, the combination of characters and numbers");
}
for (Account account : accounts) {
try {
if (!account.unlock(password)) {
return Result.getFailed(ErrorCode.PASSWORD_IS_WRONG);
}
} catch (NulsException e) {
return Result.getFailed(ErrorCode.PASSWORD_IS_WRONG);
}
}
isLockNow = false;
TaskManager.createAndRunThread(NulsConstant.MODULE_ID_ACCOUNT, "unlockAccountThread", new Runnable() {
@Override
public void run() {
isLockNow = true;
try {
Thread.sleep(seconds * 1000);
} catch (InterruptedException e) {
Log.error(e);
}
try {
resetKeys(password);
} catch (NulsException e) {
Log.error("unlockAccounts resetKey error", e);
}
isLockNow = false;
}
});
return new Result(true, "OK");
}
use of io.nuls.account.entity.Account in project nuls by nuls-io.
the class AccountServiceImpl method encryptAccount.
@Override
public Result encryptAccount(String password) {
if (!StringUtils.validPassword(password)) {
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()) {
return new Result(false, "No account was found");
}
try {
List<AccountPo> accountPoList = new ArrayList<>();
for (Account account : accounts) {
if (account.isEncrypted()) {
return new Result(false, "password has been set up");
} else {
account.encrypt(password);
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, "set password failed");
}
return new Result(true, "OK");
}
use of io.nuls.account.entity.Account in project nuls by nuls-io.
the class AccountResource method get.
@GET
@Path("/{address}")
@Produces(MediaType.APPLICATION_JSON)
public RpcResult get(@PathParam("address") String address) {
RpcResult result;
if (!Address.validAddress(address)) {
result = RpcResult.getFailed(ErrorCode.PARAMETER_ERROR);
return result;
}
Account account = accountService.getAccount(address);
if (account == null) {
Alias alias = accountService.getAlias(address);
if (alias == null) {
result = RpcResult.getFailed(ErrorCode.DATA_NOT_FOUND);
} else {
AccountDto dto = new AccountDto();
dto.setAddress(address);
dto.setAlias(alias.getAlias());
result = RpcResult.getSuccess().setData(dto);
}
} else {
result = RpcResult.getSuccess().setData(new AccountDto(account));
}
return result;
}
Aggregations