Search in sources :

Example 31 with NulsException

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);
    }
}
Also used : Account(io.nuls.account.model.Account) MultiSigAccount(io.nuls.account.model.MultiSigAccount) AliasTransaction(io.nuls.account.tx.AliasTransaction) NulsException(io.nuls.kernel.exception.NulsException) IOException(java.io.IOException) NulsException(io.nuls.kernel.exception.NulsException) NulsByteBuffer(io.nuls.kernel.utils.NulsByteBuffer)

Example 32 with NulsException

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;
}
Also used : NulsException(io.nuls.kernel.exception.NulsException) BigInteger(java.math.BigInteger) ECKey(io.nuls.core.tools.crypto.ECKey) CryptoException(io.nuls.core.tools.crypto.Exception.CryptoException)

Example 33 with NulsException

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;
}
Also used : Account(io.nuls.account.model.Account) Address(io.nuls.kernel.model.Address) NulsException(io.nuls.kernel.exception.NulsException) BigInteger(java.math.BigInteger) ECKey(io.nuls.core.tools.crypto.ECKey) NulsException(io.nuls.kernel.exception.NulsException)

Example 34 with NulsException

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;
}
Also used : NulsException(io.nuls.kernel.exception.NulsException) BigInteger(java.math.BigInteger) ECKey(io.nuls.core.tools.crypto.ECKey) CryptoException(io.nuls.core.tools.crypto.Exception.CryptoException)

Example 35 with NulsException

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;
}
Also used : NulsException(io.nuls.kernel.exception.NulsException) BigInteger(java.math.BigInteger) ECKey(io.nuls.core.tools.crypto.ECKey) EncryptedData(io.nuls.core.tools.crypto.EncryptedData) CryptoException(io.nuls.core.tools.crypto.Exception.CryptoException) NulsException(io.nuls.kernel.exception.NulsException)

Aggregations

NulsException (io.nuls.kernel.exception.NulsException)109 IOException (java.io.IOException)35 Account (io.nuls.account.model.Account)25 ValidateResult (io.nuls.kernel.validate.ValidateResult)23 ECKey (io.nuls.core.tools.crypto.ECKey)20 CoinDataResult (io.nuls.account.ledger.model.CoinDataResult)18 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)16 UnsupportedEncodingException (java.io.UnsupportedEncodingException)14 TransferTransaction (io.nuls.protocol.model.tx.TransferTransaction)13 Coin (io.nuls.kernel.model.Coin)12 MultiSigAccount (io.nuls.account.model.MultiSigAccount)11 ContractResult (io.nuls.contract.dto.ContractResult)9 NulsByteBuffer (io.nuls.kernel.utils.NulsByteBuffer)9 ArrayList (java.util.ArrayList)9 TransactionSignature (io.nuls.kernel.script.TransactionSignature)8 BigInteger (java.math.BigInteger)8 TransactionDataResult (io.nuls.account.ledger.model.TransactionDataResult)7 AccountPo (io.nuls.account.storage.po.AccountPo)7 AliasPo (io.nuls.account.storage.po.AliasPo)7 CryptoException (io.nuls.core.tools.crypto.Exception.CryptoException)7