Search in sources :

Example 31 with Result

use of io.nuls.kernel.model.Result in project nuls by nuls-io.

the class AccountServiceImpl method updatePasswordByAccountKeyStore.

@Override
public Result<Account> updatePasswordByAccountKeyStore(AccountKeyStore keyStore, String password) {
    AssertUtil.canNotEmpty(keyStore, AccountErrorCode.PARAMETER_ERROR.getMsg());
    AssertUtil.canNotEmpty(keyStore.getAddress(), AccountErrorCode.PARAMETER_ERROR.getMsg());
    AssertUtil.canNotEmpty(password, AccountErrorCode.PARAMETER_ERROR.getMsg());
    Account account;
    byte[] priKey = null;
    if (null != keyStore.getPrikey() && keyStore.getPrikey().length > 0) {
        if (!ECKey.isValidPrivteHex(Hex.encode(keyStore.getPrikey()))) {
            return Result.getFailed(AccountErrorCode.PARAMETER_ERROR);
        }
        priKey = keyStore.getPrikey();
        try {
            account = AccountTool.createAccount(Hex.encode(priKey));
        } catch (NulsException e) {
            return Result.getFailed(AccountErrorCode.FAILED);
        }
    } else {
        try {
            account = AccountTool.createAccount();
        } catch (NulsException e) {
            return Result.getFailed(AccountErrorCode.FAILED);
        }
        account.setAddress(new Address(keyStore.getAddress()));
    }
    try {
        account.encrypt(password);
    } catch (NulsException e) {
        Log.error(e);
        return Result.getFailed(e.getErrorCode());
    }
    if (StringUtils.isNotBlank(keyStore.getAlias())) {
        Alias aliasDb = aliasService.getAlias(keyStore.getAlias());
        if (null != aliasDb && account.getAddress().toString().equals(AddressTool.getStringAddressByBytes(aliasDb.getAddress()))) {
            account.setAlias(aliasDb.getAlias());
        } else {
            List<AliasPo> list = aliasStorageService.getAliasList().getData();
            for (AliasPo aliasPo : list) {
                // 如果全网别名中的地址有和当前导入的账户地址相同,则赋值别名到账户中
                if (AddressTool.getStringAddressByBytes(aliasPo.getAddress()).equals(account.getAddress().toString())) {
                    account.setAlias(aliasPo.getAlias());
                    break;
                }
            }
        }
    }
    account.setOk(false);
    AccountPo po = new AccountPo(account);
    Result result = accountStorageService.saveAccount(po);
    if (result.isFailed()) {
        return result;
    }
    accountCacheService.localAccountMaps.put(account.getAddress().getBase58(), account);
    TaskManager.asynExecuteRunnable(new Runnable() {

        @Override
        public void run() {
            String address = account.getAddress().getBase58();
            Result res = accountLedgerService.importLedger(address);
            if (res.isFailed()) {
                AccountServiceImpl.this.removeAccount(address, password);
            } else {
                AccountServiceImpl.this.finishImport(account);
            }
        }
    });
    return Result.getSuccess().setData(account);
}
Also used : Address(io.nuls.kernel.model.Address) NulsException(io.nuls.kernel.exception.NulsException) AccountPo(io.nuls.account.storage.po.AccountPo) AliasPo(io.nuls.account.storage.po.AliasPo) Result(io.nuls.kernel.model.Result)

Example 32 with Result

use of io.nuls.kernel.model.Result in project nuls by nuls-io.

the class AliasServiceTest method setAlias.

@Test
public void setAlias() {
    List<Account> accounts = accountService.createAccount(1, "nuls123456").getData();
    Account account = accounts.get(0);
    Result result = aliasService.setAlias(account.getAddress().toString(), "nuls123456", "Charlie555");
    assertTrue(result.isSuccess());
}
Also used : Account(io.nuls.account.model.Account) Result(io.nuls.kernel.model.Result) Test(org.junit.Test)

Example 33 with Result

use of io.nuls.kernel.model.Result in project nuls by nuls-io.

the class AccountServiceImpl method createAccount.

@Override
public Result<List<Account>> createAccount(int count, String password) {
    if (count <= 0 || count > AccountTool.CREATE_MAX_SIZE) {
        return Result.getFailed(AccountErrorCode.PARAMETER_ERROR);
    }
    if (StringUtils.isNotBlank(password) && !StringUtils.validPassword(password)) {
        return Result.getFailed(AccountErrorCode.PASSWORD_FORMAT_WRONG);
    }
    locker.lock();
    try {
        List<Account> accounts = new ArrayList<>();
        List<AccountPo> accountPos = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            Account account = AccountTool.createAccount();
            if (StringUtils.isNotBlank(password)) {
                account.encrypt(password);
            }
            accounts.add(account);
            AccountPo po = new AccountPo(account);
            accountPos.add(po);
        }
        Result result = accountStorageService.saveAccountList(accountPos);
        if (result.isFailed()) {
            return result;
        }
        for (Account account : accounts) {
            accountCacheService.localAccountMaps.put(account.getAddress().getBase58(), account);
        }
        return Result.getSuccess().setData(accounts);
    } catch (Exception e) {
        Log.error(e);
        throw new NulsRuntimeException(KernelErrorCode.FAILED);
    } finally {
        locker.unlock();
    }
}
Also used : AccountPo(io.nuls.account.storage.po.AccountPo) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) CryptoException(io.nuls.core.tools.crypto.Exception.CryptoException) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) NulsException(io.nuls.kernel.exception.NulsException) Result(io.nuls.kernel.model.Result)

Example 34 with Result

use of io.nuls.kernel.model.Result in project nuls by nuls-io.

the class LocalUtxoStorageServiceImpl method batchSaveUTXO.

@Override
public Result<Integer> batchSaveUTXO(Map<byte[], byte[]> utxos) {
    BatchOperation batch = dbService.createWriteBatch(AccountLedgerStorageConstant.DB_NAME_ACCOUNT_LEDGER_COINDATA);
    Set<Map.Entry<byte[], byte[]>> utxosToSaveEntries = utxos.entrySet();
    for (Map.Entry<byte[], byte[]> entry : utxosToSaveEntries) {
        batch.put(entry.getKey(), entry.getValue());
    }
    Result batchResult = batch.executeBatch();
    if (batchResult.isFailed()) {
        return batchResult;
    }
    Result result = Result.getSuccess().setData(utxos.size());
    if (result.isSuccess() && cacheMap != null) {
        for (Map.Entry<byte[], byte[]> entry : utxosToSaveEntries) {
            cacheMap.put(new String(entry.getKey()), new Entry(entry.getKey(), entry.getValue()));
        }
    }
    return result;
}
Also used : Entry(io.nuls.db.model.Entry) BatchOperation(io.nuls.db.service.BatchOperation) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Result(io.nuls.kernel.model.Result)

Example 35 with Result

use of io.nuls.kernel.model.Result in project nuls by nuls-io.

the class LocalUtxoStorageServiceImpl method batchDeleteUTXO.

@Override
public Result batchDeleteUTXO(Set<byte[]> utxos) {
    BatchOperation batch = dbService.createWriteBatch(AccountLedgerStorageConstant.DB_NAME_ACCOUNT_LEDGER_COINDATA);
    for (byte[] key : utxos) {
        batch.delete(key);
    }
    Result batchResult = batch.executeBatch();
    if (batchResult.isFailed()) {
        return batchResult;
    }
    Result result = Result.getSuccess().setData(new Integer(utxos.size()));
    if (result.isSuccess() && cacheMap != null) {
        for (byte[] key : utxos) {
            cacheMap.remove(new String(key));
        }
    }
    return result;
}
Also used : BatchOperation(io.nuls.db.service.BatchOperation) Result(io.nuls.kernel.model.Result)

Aggregations

Result (io.nuls.kernel.model.Result)70 NulsException (io.nuls.kernel.exception.NulsException)16 IOException (java.io.IOException)15 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)12 ValidateResult (io.nuls.kernel.validate.ValidateResult)11 AccountPo (io.nuls.account.storage.po.AccountPo)7 RpcClientResult (io.nuls.kernel.model.RpcClientResult)7 ArrayList (java.util.ArrayList)7 Account (io.nuls.account.model.Account)6 NulsDigestData (io.nuls.kernel.model.NulsDigestData)5 Test (org.junit.Test)5 CryptoException (io.nuls.core.tools.crypto.Exception.CryptoException)4 BatchOperation (io.nuls.db.service.BatchOperation)4 Address (io.nuls.kernel.model.Address)4 Block (io.nuls.kernel.model.Block)4 BlockHeader (io.nuls.kernel.model.BlockHeader)4 Transaction (io.nuls.kernel.model.Transaction)4 Node (io.nuls.network.model.Node)4 NotFound (io.nuls.protocol.model.NotFound)4 ApiOperation (io.swagger.annotations.ApiOperation)4