use of io.nuls.kernel.model.Result in project nuls by nuls-io.
the class AccountServiceImpl method removeAccount.
@Override
public Result removeAccount(String address, String password) {
if (!AddressTool.validAddress(address)) {
return Result.getFailed(AccountErrorCode.ADDRESS_ERROR);
}
Account account = getAccountByAddress(address);
if (account == null) {
return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST);
}
// 加过密(有密码)并且没有解锁, 就验证密码 Already encrypted(Added password) and did not unlock, verify password
if (account.isEncrypted() && account.isLocked()) {
if (!account.validatePassword(password)) {
return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG);
}
}
Result result = accountStorageService.removeAccount(account.getAddress());
if (result.isFailed()) {
return result;
}
accountLedgerService.deleteUnconfirmedTx(account.getAddress().getAddressBytes());
accountCacheService.localAccountMaps.remove(account.getAddress().getBase58());
return Result.getSuccess().setData(true);
}
use of io.nuls.kernel.model.Result in project nuls by nuls-io.
the class AccountServiceImpl method importAccountFormKeyStore.
@Override
public Result<Account> importAccountFormKeyStore(AccountKeyStore keyStore, String password) {
if (null == keyStore || StringUtils.isBlank(keyStore.getAddress())) {
return Result.getFailed(AccountErrorCode.PARAMETER_ERROR);
}
if (!AddressTool.validAddress(keyStore.getAddress())) {
return Result.getFailed(AccountErrorCode.ADDRESS_ERROR);
}
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(e.getErrorCode());
}
// 如果私钥生成的地址和keystore的地址不相符,说明私钥错误
if (!account.getAddress().getBase58().equals(keyStore.getAddress())) {
return Result.getFailed(AccountErrorCode.PRIVATE_KEY_WRONG);
}
} else if (null == keyStore.getPrikey() && null != keyStore.getEncryptedPrivateKey()) {
if (!StringUtils.validPassword(password)) {
return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG);
}
try {
priKey = AESEncrypt.decrypt(Hex.decode(keyStore.getEncryptedPrivateKey()), password);
account = AccountTool.createAccount(Hex.encode(priKey));
} catch (CryptoException e) {
return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG);
} catch (NulsException e) {
return Result.getFailed(e.getErrorCode());
}
// 如果私钥生成的地址和keystore的地址不相符,说明密码错误
if (!account.getAddress().getBase58().equals(keyStore.getAddress())) {
return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG);
}
} else {
return Result.getFailed(AccountErrorCode.PARAMETER_ERROR);
}
Alias aliasDb = null;
if (StringUtils.isNotBlank(keyStore.getAlias())) {
aliasDb = aliasService.getAlias(keyStore.getAlias());
}
if (null != aliasDb && AddressTool.getStringAddressByBytes(aliasDb.getAddress()).equals(account.getAddress().toString())) {
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;
}
}
}
if (StringUtils.validPassword(password)) {
try {
account.encrypt(password);
} catch (NulsException e) {
Log.error(e);
return Result.getFailed(e.getErrorCode());
}
}
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 AccountServiceImpl method importAccount.
@Override
public Result<Account> importAccount(String prikey, String password) {
if (!ECKey.isValidPrivteHex(prikey)) {
return Result.getFailed(AccountErrorCode.PARAMETER_ERROR);
}
Account account;
try {
account = AccountTool.createAccount(prikey);
} catch (NulsException e) {
return Result.getFailed(AccountErrorCode.PRIVATE_KEY_WRONG);
}
if (StringUtils.validPassword(password)) {
try {
account.encrypt(password);
} catch (NulsException e) {
Log.error(e);
return Result.getFailed(e.getErrorCode());
}
}
// 扫所全网别名对比地址符合就设置
// String alias = null;
Account acc = getAccountByAddress(account.getAddress().toString());
if (null == acc) {
List<AliasPo> list = aliasStorageService.getAliasList().getData();
for (AliasPo aliasPo : list) {
// 如果全网别名中的地址有和当前导入的账户地址相同,则赋值别名到账户中
if (AddressTool.getStringAddressByBytes(aliasPo.getAddress()).equals(account.getAddress().toString())) {
account.setAlias(aliasPo.getAlias());
break;
}
}
} else {
account.setAlias(acc.getAlias());
}
// Result res = accountLedgerService.importLedgerByAddress(account.getAddress().getBase58());
// if (res.isFailed()) {
// return res;
// }
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 AccountServiceImpl method createMultiAccount.
@Override
public Result<Address> createMultiAccount(List<String> pubkeys, int m) {
locker.lock();
try {
Script redeemScript = ScriptBuilder.createNulsRedeemScript(m, pubkeys);
Address address = new Address(NulsContext.getInstance().getDefaultChainId(), NulsContext.P2SH_ADDRESS_TYPE, SerializeUtils.sha256hash160(redeemScript.getProgram()));
MultiSigAccount account = new MultiSigAccount();
account.setAddress(address);
account.setM(m);
account.addPubkeys(pubkeys);
Result result = this.multiSigAccountStorageService.saveAccount(account.getAddress(), account.serialize());
if (result.isFailed()) {
return result;
}
return result.setData(account);
} 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 AccountServiceImpl method isEncrypted.
@Override
public Result isEncrypted(String address) {
if (!AddressTool.validAddress(address)) {
return Result.getFailed(AccountErrorCode.ADDRESS_ERROR);
}
Account account = getAccountByAddress(address);
if (null == account) {
return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST);
}
Result result = new Result();
boolean rs = account.isEncrypted();
result.setSuccess(true);
result.setData(rs);
return result;
}
Aggregations