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