Search in sources :

Example 11 with Result

use of io.nuls.core.chain.entity.Result in project nuls by nuls-io.

the class AccountServiceImpl method exportAccount.

private Result exportAccount(Account account, File backupFile) {
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(backupFile);
        // account length
        fos.write(1);
        fos.write(account.serialize());
    } catch (Exception e) {
        Log.error(e);
        return new Result(false, "export failed");
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                Log.error(e);
            }
        }
    }
    return new Result(true, "OK");
}
Also used : FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) 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 12 with Result

use of io.nuls.core.chain.entity.Result 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 13 with Result

use of io.nuls.core.chain.entity.Result 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 14 with Result

use of io.nuls.core.chain.entity.Result 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 15 with Result

use of io.nuls.core.chain.entity.Result 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)

Aggregations

Result (io.nuls.core.chain.entity.Result)19 ValidateResult (io.nuls.core.validate.ValidateResult)12 NulsException (io.nuls.core.exception.NulsException)10 Account (io.nuls.account.entity.Account)9 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)7 IOException (java.io.IOException)7 AccountPo (io.nuls.db.entity.AccountPo)5 RpcResult (io.nuls.rpc.entity.RpcResult)4 DbSession (io.nuls.db.transactional.annotation.DbSession)3 FileOutputStream (java.io.FileOutputStream)2 Alias (io.nuls.account.entity.Alias)1 AliasTransaction (io.nuls.account.entity.tx.AliasTransaction)1 NodeDownloadingStatus (io.nuls.consensus.entity.NodeDownloadingStatus)1 Block (io.nuls.core.chain.entity.Block)1 AliasPo (io.nuls.db.entity.AliasPo)1 TransactionPo (io.nuls.db.entity.TransactionPo)1 CoinTransferData (io.nuls.ledger.entity.params.CoinTransferData)1 TransactionEvent (io.nuls.ledger.event.TransactionEvent)1 List (java.util.List)1