use of io.nuls.core.exception.NulsException in project nuls by nuls-io.
the class AccountServiceImpl method importAccounts.
@Override
@DbSession
public Result importAccounts(List<String> keys, String password) {
Account account = null;
AccountPo accountPo = null;
AliasPo aliasPo = null;
Account defaultAcct = getDefaultAccount();
if (defaultAcct != null) {
try {
if (!defaultAcct.decrypt(password)) {
return Result.getFailed(ErrorCode.PASSWORD_IS_WRONG);
}
defaultAcct.encrypt(password);
} catch (NulsException e) {
}
}
for (String priKey : keys) {
try {
account = AccountTool.createAccount(priKey);
account.encrypt(password);
} catch (NulsException e) {
return Result.getFailed("invalid prikey");
}
accountPo = accountDao.get(account.getAddress().getBase58());
if (accountPo != null) {
continue;
} else {
accountPo = new AccountPo();
}
// save db
AccountTool.toPojo(account, accountPo);
aliasPo = aliasDataService.getByAddress(accountPo.getAddress());
if (aliasPo != null) {
account.setAlias(aliasPo.getAlias());
accountPo.setAlias(aliasPo.getAlias());
}
accountDao.save(accountPo);
ledgerService.saveTxInLocal(accountPo.getAddress());
// save cache
accountCacheService.putAccount(account);
NulsContext.LOCAL_ADDRESS_LIST.add(accountPo.getAddress());
ledgerService.getBalance(accountPo.getAddress());
}
return Result.getSuccess();
}
use of io.nuls.core.exception.NulsException in project nuls by nuls-io.
the class AliasTxService method onApproval.
@Override
public void onApproval(AliasTransaction tx) throws NulsException {
Alias alias = tx.getTxData();
AliasPo po = getAliasDataService().getAlias(alias.getAlias());
if (po != null) {
throw new NulsException(ErrorCode.ALIAS_EXIST);
}
}
use of io.nuls.core.exception.NulsException in project nuls by nuls-io.
the class Account method encrypt.
/**
* @param password
*/
public void encrypt(String password, boolean isForce) throws NulsException {
if (this.isEncrypted() && !isForce) {
if (!unlock(password)) {
throw new NulsException(ErrorCode.ACCOUNT_IS_ALREADY_ENCRYPTED);
}
}
ECKey eckey = this.getEcKey();
byte[] privKeyBytes = eckey.getPrivKeyBytes();
EncryptedData encryptedPrivateKey = AESEncrypt.encrypt(privKeyBytes, EncryptedData.DEFAULT_IV, new KeyParameter(Sha256Hash.hash(password.getBytes())));
eckey.setEncryptedPrivateKey(encryptedPrivateKey);
ECKey result = ECKey.fromEncrypted(encryptedPrivateKey, getPubKey());
this.setPriKey(new byte[0]);
this.setEcKey(result);
this.setEncryptedPriKey(encryptedPrivateKey.getEncryptedBytes());
}
use of io.nuls.core.exception.NulsException in project nuls by nuls-io.
the class Alias method parse.
@Override
protected void parse(NulsByteBuffer byteBuffer) throws NulsException {
try {
this.address = new String(byteBuffer.readByLengthByte(), NulsContext.DEFAULT_ENCODING);
this.alias = new String(byteBuffer.readByLengthByte(), NulsContext.DEFAULT_ENCODING);
} catch (UnsupportedEncodingException e) {
Log.error(e);
throw new NulsException(ErrorCode.DATA_ERROR);
}
}
use of io.nuls.core.exception.NulsException in project nuls by nuls-io.
the class AccountTool method createAccount.
public static Account createAccount(String prikey) throws NulsException {
ECKey key = null;
if (StringUtils.isBlank(prikey)) {
key = new ECKey();
} else {
try {
key = ECKey.fromPrivate(new BigInteger(Hex.decode(prikey)));
} catch (Exception e) {
throw new NulsException(ErrorCode.DATA_PARSE_ERROR);
}
}
Address address = new Address(NulsContext.getInstance().getChainId(NulsContext.CHAIN_ID), Utils.sha256hash160(key.getPubKey()));
Account account = new Account();
account.setEncryptedPriKey(new byte[0]);
account.setAddress(address);
account.setPubKey(key.getPubKey());
account.setEcKey(key);
account.setPriKey(key.getPrivKeyBytes());
account.setCreateTime(TimeService.currentTimeMillis());
return account;
}
Aggregations