Search in sources :

Example 21 with Account

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());
}
Also used : Account(io.nuls.account.entity.Account) ValidateResult(io.nuls.core.validate.ValidateResult) Result(io.nuls.core.chain.entity.Result)

Example 22 with Account

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");
}
Also used : Account(io.nuls.account.entity.Account) AliasTransaction(io.nuls.account.entity.tx.AliasTransaction) TransactionEvent(io.nuls.ledger.event.TransactionEvent) CoinTransferData(io.nuls.ledger.entity.params.CoinTransferData) Alias(io.nuls.account.entity.Alias) ValidateResult(io.nuls.core.validate.ValidateResult) NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) ValidateResult(io.nuls.core.validate.ValidateResult) Result(io.nuls.core.chain.entity.Result)

Example 23 with Account

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");
}
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 24 with Account

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");
}
Also used : Account(io.nuls.account.entity.Account) AccountPo(io.nuls.db.entity.AccountPo) NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) ValidateResult(io.nuls.core.validate.ValidateResult) Result(io.nuls.core.chain.entity.Result)

Example 25 with Account

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;
}
Also used : Account(io.nuls.account.entity.Account) Alias(io.nuls.account.entity.Alias)

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