use of io.nuls.kernel.exception.NulsException in project nuls by nuls-io.
the class AliasService method signMultiAliasTransaction.
/**
* A transfers NULS to B 多签交易
*
* @param signAddr 签名地址
* @param password password of A
* @param txdata 需要签名的数据
* @return Result
*/
public Result signMultiAliasTransaction(String signAddr, String password, String txdata) {
try {
Result<Account> accountResult = accountService.getAccount(signAddr);
if (accountResult.isFailed()) {
return accountResult;
}
Account account = accountResult.getData();
if (account.isEncrypted() && account.isLocked()) {
AssertUtil.canNotEmpty(password, "the password can not be empty");
if (!account.validatePassword(password)) {
return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG);
}
}
AliasTransaction tx = new AliasTransaction();
TransactionSignature transactionSignature = new TransactionSignature();
byte[] txByte = Hex.decode(txdata);
tx.parse(new NulsByteBuffer(txByte));
transactionSignature.parse(new NulsByteBuffer(tx.getTransactionSignature()));
return accountLedgerService.txMultiProcess(tx, transactionSignature, account, password);
} catch (NulsException e) {
Log.error(e);
return Result.getFailed(e.getErrorCode());
} catch (Exception e) {
Log.error(e);
return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST);
}
}
use of io.nuls.kernel.exception.NulsException in project nuls by nuls-io.
the class Account method getPriKey.
public byte[] getPriKey(String password) throws NulsException {
if (!StringUtils.validPassword(password)) {
throw new NulsException(AccountErrorCode.PASSWORD_IS_WRONG);
}
byte[] unencryptedPrivateKey;
try {
unencryptedPrivateKey = AESEncrypt.decrypt(this.getEncryptedPriKey(), password);
} catch (CryptoException e) {
throw new NulsException(AccountErrorCode.PASSWORD_IS_WRONG);
}
BigInteger newPriv = new BigInteger(1, unencryptedPrivateKey);
ECKey key = ECKey.fromPrivate(newPriv);
if (!Arrays.equals(key.getPubKey(), getPubKey())) {
throw new NulsException(AccountErrorCode.PASSWORD_IS_WRONG);
}
return unencryptedPrivateKey;
}
use of io.nuls.kernel.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(1, Hex.decode(prikey)));
} catch (Exception e) {
throw new NulsException(AccountErrorCode.PRIVATE_KEY_WRONG, e);
}
}
Address address = new Address(NulsContext.getInstance().getDefaultChainId(), NulsContext.DEFAULT_ADDRESS_TYPE, SerializeUtils.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;
}
use of io.nuls.kernel.exception.NulsException in project nuls by nuls-io.
the class Account method getEcKey.
/**
* 根据密码获取ECKey
*/
public ECKey getEcKey(String password) throws NulsException {
ECKey eckey = null;
byte[] unencryptedPrivateKey;
// 判断当前账户是否存在私钥,如果不存在私钥这为锁定账户
BigInteger newPriv = null;
if (this.isLocked()) {
AssertUtil.canNotEmpty(password, "the password can not be empty");
if (!validatePassword(password)) {
throw new NulsException(AccountErrorCode.PASSWORD_IS_WRONG);
}
try {
unencryptedPrivateKey = AESEncrypt.decrypt(this.getEncryptedPriKey(), password);
newPriv = new BigInteger(1, unencryptedPrivateKey);
} catch (CryptoException e) {
throw new NulsException(AccountErrorCode.PASSWORD_IS_WRONG);
}
} else {
newPriv = new BigInteger(1, this.getPriKey());
}
eckey = ECKey.fromPrivate(newPriv);
if (!Arrays.equals(eckey.getPubKey(), getPubKey())) {
throw new NulsException(AccountErrorCode.PASSWORD_IS_WRONG);
}
return eckey;
}
use of io.nuls.kernel.exception.NulsException in project nuls by nuls-io.
the class Account method decrypt.
/**
* 根据解密账户, 包括生成账户明文私钥
* According to the decryption account, including generating the account plaintext private key
*/
private boolean decrypt(String password) throws NulsException {
try {
byte[] unencryptedPrivateKey = AESEncrypt.decrypt(this.getEncryptedPriKey(), password);
BigInteger newPriv = new BigInteger(1, unencryptedPrivateKey);
ECKey key = ECKey.fromPrivate(newPriv);
if (!Arrays.equals(key.getPubKey(), getPubKey())) {
return false;
}
key.setEncryptedPrivateKey(new EncryptedData(this.getEncryptedPriKey()));
this.setPriKey(key.getPrivKeyBytes());
this.setEcKey(key);
} catch (Exception e) {
throw new NulsException(AccountErrorCode.PASSWORD_IS_WRONG);
}
return true;
}
Aggregations