Search in sources :

Example 11 with NulsRuntimeException

use of io.nuls.kernel.exception.NulsRuntimeException 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();
    }
}
Also used : Script(io.nuls.kernel.script.Script) Address(io.nuls.kernel.model.Address) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) CryptoException(io.nuls.core.tools.crypto.Exception.CryptoException) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) NulsException(io.nuls.kernel.exception.NulsException) Result(io.nuls.kernel.model.Result)

Example 12 with NulsRuntimeException

use of io.nuls.kernel.exception.NulsRuntimeException 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();
    }
}
Also used : AccountPo(io.nuls.account.storage.po.AccountPo) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) CryptoException(io.nuls.core.tools.crypto.Exception.CryptoException) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) NulsException(io.nuls.kernel.exception.NulsException) Result(io.nuls.kernel.model.Result)

Example 13 with NulsRuntimeException

use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.

the class LocalUtxoServiceImpl method unlockCoinData.

@Override
public Result<List<byte[]>> unlockCoinData(Transaction tx, long newLockTime) {
    List<byte[]> addresses = new ArrayList<>();
    CoinData coinData = tx.getCoinData();
    if (coinData != null) {
        List<Coin> tos = coinData.getTo();
        Coin to;
        for (int i = 0, length = tos.size(); i < length; i++) {
            to = tos.get(i);
            if (to.getLockTime() == -1L) {
                Coin needUnLockUtxoNew = new Coin(to.getOwner(), to.getNa(), newLockTime);
                needUnLockUtxoNew.setFrom(to.getFrom());
                try {
                    byte[] outKey = ArraysTool.concatenate(tx.getHash().serialize(), new VarInt(i).encode());
                    saveUTXO(outKey, needUnLockUtxoNew.serialize());
                    addresses.add(to.getAddress());
                } catch (IOException e) {
                    throw new NulsRuntimeException(e);
                }
                // todo , think about weather to add a transaction history
                break;
            }
        }
    }
    return Result.getSuccess().setData(addresses);
}
Also used : VarInt(io.nuls.kernel.utils.VarInt) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) IOException(java.io.IOException)

Example 14 with NulsRuntimeException

use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.

the class LocalUtxoServiceImpl method rollbackUnlockTxCoinData.

@Override
public Result<List<byte[]>> rollbackUnlockTxCoinData(Transaction tx) {
    List<byte[]> addresses = new ArrayList<>();
    CoinData coinData = tx.getCoinData();
    if (coinData != null) {
        // lock utxo - to
        List<Coin> tos = coinData.getTo();
        for (int i = 0, length = tos.size(); i < length; i++) {
            Coin to = tos.get(i);
            if (to.getLockTime() == -1L) {
                try {
                    byte[] outKey = ArraysTool.concatenate(tx.getHash().serialize(), new VarInt(i).encode());
                    saveUTXO(outKey, to.serialize());
                    addresses.add(to.getAddress());
                } catch (IOException e) {
                    throw new NulsRuntimeException(e);
                }
                break;
            }
        }
    }
    return Result.getSuccess().setData(addresses);
}
Also used : VarInt(io.nuls.kernel.utils.VarInt) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) IOException(java.io.IOException)

Example 15 with NulsRuntimeException

use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.

the class LocalUtxoServiceImpl method saveUtxoForAccount.

@Override
public Result saveUtxoForAccount(Transaction tx, List<byte[]> addressesList) {
    if (tx == null || addressesList == null || addressesList.size() == 0) {
        return Result.getFailed(KernelErrorCode.NULL_PARAMETER);
    }
    CoinData coinData = tx.getCoinData();
    if (coinData != null) {
        // delete - from
        List<Coin> froms = coinData.getFrom();
        List<byte[]> fromsList = new ArrayList<>();
        byte[] fromSource;
        byte[] utxoFromSource;
        byte[] fromIndex;
        Transaction sourceTx;
        Coin fromOfFromCoin;
        for (Coin from : froms) {
            fromSource = from.getOwner();
            fromOfFromCoin = from.getFrom();
            if (fromOfFromCoin == null) {
                utxoFromSource = new byte[tx.getHash().size()];
                fromIndex = new byte[fromSource.length - utxoFromSource.length];
                System.arraycopy(fromSource, 0, utxoFromSource, 0, tx.getHash().size());
                System.arraycopy(fromSource, tx.getHash().size(), fromIndex, 0, fromIndex.length);
                try {
                    sourceTx = ledgerService.getTx(NulsDigestData.fromDigestHex(Hex.encode(utxoFromSource)));
                } catch (Exception e) {
                    continue;
                }
                if (sourceTx == null) {
                    return Result.getFailed(TransactionErrorCode.TX_NOT_EXIST);
                }
                int index = (int) new VarInt(fromIndex, 0).value;
                fromOfFromCoin = sourceTx.getCoinData().getTo().get(index);
                from.setFrom(fromOfFromCoin);
            }
            if (fromOfFromCoin == null) {
                Log.warn("from coin not found!");
                continue;
            }
            byte[] toAddress = fromOfFromCoin.getAddress();
            boolean addressIsMatch = false;
            for (byte[] addresses : addressesList) {
                if (Arrays.equals(toAddress, addresses)) {
                    addressIsMatch = true;
                    break;
                }
            }
            if (!addressIsMatch) {
                continue;
            }
            fromsList.add(fromSource);
        }
        // save utxo - to
        List<Coin> tos = coinData.getTo();
        List<Entry<byte[], byte[]>> toList = new ArrayList<>();
        byte[] txHashBytes = null;
        try {
            txHashBytes = tx.getHash().serialize();
        } catch (IOException e) {
            throw new NulsRuntimeException(e);
        }
        byte[] outKey;
        Coin to;
        byte[] toAddress;
        for (int i = 0, length = tos.size(); i < length; i++) {
            to = tos.get(i);
            toAddress = to.getAddress();
            boolean addressIsMatch = false;
            for (byte[] addresses : addressesList) {
                if (Arrays.equals(toAddress, addresses)) {
                    addressIsMatch = true;
                    break;
                }
            }
            if (!addressIsMatch) {
                continue;
            }
            try {
                outKey = ArraysTool.concatenate(txHashBytes, new VarInt(i).encode());
                toList.add(new Entry<>(outKey, to.serialize()));
            } catch (IOException e) {
                Log.error(e);
            }
        }
        Result result = localUtxoStorageService.batchSaveAndDeleteUTXO(toList, fromsList);
        if (result.isFailed() || result.getData() == null || (int) result.getData() != toList.size() + fromsList.size()) {
            return Result.getFailed();
        }
    }
    return Result.getSuccess();
}
Also used : VarInt(io.nuls.kernel.utils.VarInt) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) IOException(java.io.IOException) Entry(io.nuls.db.model.Entry)

Aggregations

NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)68 IOException (java.io.IOException)35 NulsException (io.nuls.kernel.exception.NulsException)26 ArrayList (java.util.ArrayList)21 CoinDataResult (io.nuls.account.ledger.model.CoinDataResult)10 Result (io.nuls.kernel.model.Result)9 Account (io.nuls.account.model.Account)8 MultiSigAccount (io.nuls.account.model.MultiSigAccount)8 Entry (io.nuls.db.model.Entry)8 Agent (io.nuls.consensus.poc.protocol.entity.Agent)7 VarInt (io.nuls.kernel.utils.VarInt)7 CreateAgentTransaction (io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction)6 ValidateResult (io.nuls.kernel.validate.ValidateResult)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 Deposit (io.nuls.consensus.poc.protocol.entity.Deposit)5 DepositTransaction (io.nuls.consensus.poc.protocol.tx.DepositTransaction)5 DepositPo (io.nuls.consensus.poc.storage.po.DepositPo)5 PunishLogPo (io.nuls.consensus.poc.storage.po.PunishLogPo)5 TransferTransaction (io.nuls.protocol.model.tx.TransferTransaction)5 StopAgent (io.nuls.consensus.poc.protocol.entity.StopAgent)4