Search in sources :

Example 1 with Account

use of io.nuls.account.model.Account in project nuls by nuls-io.

the class AccountLedgerServiceImpl method sendToAddress.

@Override
public Result sendToAddress(byte[] from, byte[] to, Na values, String password, byte[] remark, Na price) {
    try {
        Result<Account> accountResult = accountService.getAccount(from);
        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);
            }
        }
        // 检查to是否为合约地址,如果是合约地址,则返回错误
        if (contractService.isContractAddress(to)) {
            return Result.getFailed(ContractErrorCode.NON_CONTRACTUAL_TRANSACTION_NO_TRANSFER);
        }
        if (price == null) {
            price = TransactionFeeCalculator.MIN_PRICE_PRE_1024_BYTES;
        }
        TransactionDataResult txResult = createTransferTx(from, values, price, to, remark);
        if (!txResult.isEnough()) {
            return Result.getFailed(AccountLedgerErrorCode.INSUFFICIENT_BALANCE);
        }
        Transaction tx = txResult.getTransaction();
        tx.setHash(NulsDigestData.calcDigestData(tx.serializeForHash()));
        // 生成签名
        List<ECKey> signEckeys = new ArrayList<>();
        List<ECKey> scriptEckeys = new ArrayList<>();
        ECKey eckey = account.getEcKey(password);
        // 如果最后一位为1则表示该交易包含普通签名
        if ((txResult.getSignType() & 0x01) == 0x01) {
            signEckeys.add(eckey);
        }
        // 如果倒数第二位位为1则表示该交易包含脚本签名
        if ((txResult.getSignType() & 0x02) == 0x02) {
            scriptEckeys.add(eckey);
        }
        SignatureUtil.createTransactionSignture(tx, scriptEckeys, signEckeys);
        // 保存未确认交易到本地账户
        Result saveResult = verifyAndSaveUnconfirmedTransaction(tx);
        if (saveResult.isFailed()) {
            if (KernelErrorCode.DATA_SIZE_ERROR.getCode().equals(saveResult.getErrorCode().getCode())) {
                // 重新算一次交易(不超出最大交易数据大小下)的最大金额
                Result rs = getMaxAmountOfOnce(from, tx, price);
                if (rs.isSuccess()) {
                    Na maxAmount = (Na) rs.getData();
                    rs = Result.getFailed(KernelErrorCode.DATA_SIZE_ERROR_EXTEND);
                    rs.setMsg(rs.getMsg() + maxAmount.toDouble());
                }
                return rs;
            }
            return saveResult;
        }
        // transactionService.newTx(tx);
        Result sendResult = transactionService.broadcastTx(tx);
        if (sendResult.isFailed()) {
            this.deleteTransaction(tx);
            return sendResult;
        }
        return Result.getSuccess().setData(tx.getHash().getDigestHex());
    } catch (IOException e) {
        Log.error(e);
        return Result.getFailed(KernelErrorCode.IO_ERROR);
    } catch (NulsException e) {
        Log.error(e);
        return Result.getFailed(e.getErrorCode());
    }
}
Also used : Account(io.nuls.account.model.Account) MultiSigAccount(io.nuls.account.model.MultiSigAccount) TransferTransaction(io.nuls.protocol.model.tx.TransferTransaction) DataTransaction(io.nuls.protocol.model.tx.DataTransaction) NulsException(io.nuls.kernel.exception.NulsException) ECKey(io.nuls.core.tools.crypto.ECKey) TransactionDataResult(io.nuls.account.ledger.model.TransactionDataResult) IOException(java.io.IOException) TransactionDataResult(io.nuls.account.ledger.model.TransactionDataResult) ValidateResult(io.nuls.kernel.validate.ValidateResult) CoinDataResult(io.nuls.account.ledger.model.CoinDataResult)

Example 2 with Account

use of io.nuls.account.model.Account in project nuls by nuls-io.

the class AccountLedgerServiceImpl method multipleAddressTransfer.

@Override
public Result multipleAddressTransfer(Set<String> addressSet, List<MultipleAddressTransferModel> fromList, List<MultipleAddressTransferModel> toList, Na amount, String remark, Na price) {
    try {
        for (MultipleAddressTransferModel from : fromList) {
            Result<Account> accountResult = accountService.getAccount(from.getAddress());
            if (accountResult.isFailed()) {
                return accountResult;
            }
            Account account = accountResult.getData();
            if (account.isEncrypted() && account.isLocked()) {
                AssertUtil.canNotEmpty(from.getPassword(), "the password can not be empty");
                if (!account.validatePassword(from.getPassword())) {
                    Result result = Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG);
                    result.setMsg(result.getErrorCode().getMsg() + ",address :" + AddressTool.getStringAddressByBytes(from.getAddress()));
                    return result;
                }
            }
        }
        for (MultipleAddressTransferModel to : toList) {
            // 检查to是否为合约地址,如果是合约地址,则返回错误
            if (contractService.isContractAddress(to.getAddress())) {
                return Result.getFailed(ContractErrorCode.NON_CONTRACTUAL_TRANSACTION_NO_TRANSFER);
            }
        }
        TransferTransaction tx = new TransferTransaction();
        if (StringUtils.isNotBlank(remark)) {
            try {
                tx.setRemark(remark.getBytes(NulsConfig.DEFAULT_ENCODING));
            } catch (UnsupportedEncodingException e) {
                Log.error(e);
            }
        }
        tx.setTime(TimeService.currentTimeMillis());
        CoinData coinData = new CoinData();
        for (MultipleAddressTransferModel to : toList) {
            // 如果为多签地址
            Coin toCoin = null;
            if (to.getAddress()[2] == 3) {
                Script scriptPubkey = SignatureUtil.createOutputScript(to.getAddress());
                toCoin = new Coin(scriptPubkey.getProgram(), Na.valueOf(to.getAmount()));
            } else {
                toCoin = new Coin(to.getAddress(), Na.valueOf(to.getAmount()));
            }
            coinData.getTo().add(toCoin);
        }
        if (price == null) {
            price = TransactionFeeCalculator.MIN_PRICE_PRE_1024_BYTES;
        }
        CoinDataResult coinDataResult = getCoinDataMultipleAdresses(fromList, amount, tx.size() + coinData.size() + addressSet.size() * P2PHKSignature.SERIALIZE_LENGTH, price);
        // 从多个地址中获取币 from
        List<Coin> fromCoinList = new ArrayList<>();
        List<Coin> changeCoinList = new ArrayList<>();
        if (!coinDataResult.isEnough()) {
            // 验证utxo是否足够
            return Result.getFailed(AccountLedgerErrorCode.INSUFFICIENT_BALANCE);
        }
        // 把每个地址获取的币放到list里面
        fromCoinList.addAll(coinDataResult.getCoinList());
        if (coinDataResult.getChange() != null) {
            changeCoinList.add(coinDataResult.getChange());
        }
        // 每个地址from获取的utxo list
        coinData.setFrom(fromCoinList);
        // 找零钱
        coinData.getTo().addAll(changeCoinList);
        tx.setCoinData(coinData);
        tx.setHash(NulsDigestData.calcDigestData(tx.serializeForHash()));
        // 生成签名
        List<ECKey> signEckeys = new ArrayList<>();
        List<ECKey> scriptEckeys = new ArrayList<>();
        for (int index = 0; index < fromList.size(); index++) {
            Result<Account> accountResult = accountService.getAccount(fromList.get(index).getAddress());
            Account account = accountResult.getData();
            // 用于生成ECKey
            ECKey ecKey = account.getEcKey(fromList.get(index).getPassword());
            // 如果最后一位为1则表示该交易包含普通签名
            if ((coinDataResult.getSignType() & 0x01) == 0x01) {
                signEckeys.add(ecKey);
            }
            // 如果倒数第二位位为1则表示该交易包含脚本签名
            if ((coinDataResult.getSignType() & 0x02) == 0x02) {
                scriptEckeys.add(ecKey);
            }
        }
        SignatureUtil.createTransactionSignture(tx, scriptEckeys, signEckeys);
        // 保存未确认交易到本地账户
        Result saveResult = verifyAndSaveUnconfirmedTransaction(tx);
        if (saveResult.isFailed()) {
            for (MultipleAddressTransferModel from : fromList) {
                if (KernelErrorCode.DATA_SIZE_ERROR.getCode().equals(saveResult.getErrorCode().getCode())) {
                    // 重新算一次交易(不超出最大交易数据大小下)的最大金额
                    Na maxAmount = getMaxAmountOfOnce(from.getAddress(), tx, price).getData();
                    Result rs = Result.getFailed(KernelErrorCode.DATA_SIZE_ERROR_EXTEND);
                    rs.setMsg(rs.getMsg() + maxAmount.toDouble());
                    return rs;
                }
            }
            return saveResult;
        }
        Result sendResult = transactionService.broadcastTx(tx);
        if (sendResult.isFailed()) {
            this.deleteTransaction(tx);
            return sendResult;
        }
        return Result.getSuccess().setData(tx.getHash().getDigestHex());
    } catch (IOException e) {
        Log.error(e);
        return Result.getFailed(KernelErrorCode.IO_ERROR);
    } catch (NulsException e) {
        Log.error(e);
        return Result.getFailed(e.getErrorCode());
    }
}
Also used : Account(io.nuls.account.model.Account) MultiSigAccount(io.nuls.account.model.MultiSigAccount) MultipleAddressTransferModel(io.nuls.account.ledger.model.MultipleAddressTransferModel) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ECKey(io.nuls.core.tools.crypto.ECKey) IOException(java.io.IOException) TransactionDataResult(io.nuls.account.ledger.model.TransactionDataResult) ValidateResult(io.nuls.kernel.validate.ValidateResult) CoinDataResult(io.nuls.account.ledger.model.CoinDataResult) NulsException(io.nuls.kernel.exception.NulsException) TransferTransaction(io.nuls.protocol.model.tx.TransferTransaction) CoinDataResult(io.nuls.account.ledger.model.CoinDataResult)

Example 3 with Account

use of io.nuls.account.model.Account in project nuls by nuls-io.

the class AccountLedgerServiceImpl method filterLocalTransaction.

protected List<Transaction> filterLocalTransaction(List<Transaction> txs) {
    List<Transaction> resultTxs = new ArrayList<>();
    if (txs == null || txs.size() == 0) {
        return resultTxs;
    }
    Collection<Account> localAccountList = accountService.getAccountList().getData();
    if (localAccountList == null || localAccountList.size() == 0) {
        return resultTxs;
    }
    Transaction tmpTx;
    for (int i = 0; i < txs.size(); i++) {
        tmpTx = txs.get(i);
        if (AccountLegerUtils.isLocalTransaction(tmpTx)) {
            resultTxs.add(tmpTx);
        }
    }
    return resultTxs;
}
Also used : Account(io.nuls.account.model.Account) MultiSigAccount(io.nuls.account.model.MultiSigAccount) TransferTransaction(io.nuls.protocol.model.tx.TransferTransaction) DataTransaction(io.nuls.protocol.model.tx.DataTransaction)

Example 4 with Account

use of io.nuls.account.model.Account in project nuls by nuls-io.

the class AccountLedgerServiceImpl method transferFee.

@Override
public Result transferFee(byte[] from, byte[] to, Na values, String remark, Na price) {
    Result<Account> accountResult = accountService.getAccount(from);
    if (accountResult.isFailed()) {
        return accountResult;
    }
    if (!accountResult.getData().isOk()) {
        return Result.getFailed(AccountErrorCode.IMPORTING_ACCOUNT);
    }
    TransferTransaction tx = new TransferTransaction();
    try {
        tx.setRemark(remark.getBytes(NulsConfig.DEFAULT_ENCODING));
    } catch (UnsupportedEncodingException e) {
        return Result.getFailed(LedgerErrorCode.PARAMETER_ERROR);
    }
    tx.setTime(TimeService.currentTimeMillis());
    CoinData coinData = new CoinData();
    Script scriptPubkey = SignatureUtil.createOutputScript(to);
    Coin toCoin = new Coin(scriptPubkey.getProgram(), values);
    coinData.getTo().add(toCoin);
    tx.setCoinData(coinData);
    Na fee = getTxFee(from, values, tx.size() + P2PHKSignature.SERIALIZE_LENGTH, price);
    Result result = Result.getSuccess().setData(fee);
    return result;
}
Also used : Account(io.nuls.account.model.Account) MultiSigAccount(io.nuls.account.model.MultiSigAccount) UnsupportedEncodingException(java.io.UnsupportedEncodingException) TransferTransaction(io.nuls.protocol.model.tx.TransferTransaction) TransactionDataResult(io.nuls.account.ledger.model.TransactionDataResult) ValidateResult(io.nuls.kernel.validate.ValidateResult) CoinDataResult(io.nuls.account.ledger.model.CoinDataResult)

Example 5 with Account

use of io.nuls.account.model.Account in project nuls by nuls-io.

the class AccountLegerUtils method getRelatedAddresses.

public static List<byte[]> getRelatedAddresses(Transaction tx) {
    List<byte[]> result = new ArrayList<>();
    if (tx == null) {
        return result;
    }
    Collection<Account> localAccountList = accountService.getAccountList().getData();
    if (localAccountList == null || localAccountList.size() == 0) {
        return result;
    }
    List<byte[]> destAddresses = new ArrayList<>();
    for (Account account : localAccountList) {
        destAddresses.add(account.getAddress().getAddressBytes());
    }
    return getRelatedAddresses(tx, destAddresses);
}
Also used : Account(io.nuls.account.model.Account) ArrayList(java.util.ArrayList)

Aggregations

Account (io.nuls.account.model.Account)65 NulsException (io.nuls.kernel.exception.NulsException)38 MultiSigAccount (io.nuls.account.model.MultiSigAccount)30 CoinDataResult (io.nuls.account.ledger.model.CoinDataResult)28 IOException (java.io.IOException)28 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)16 UnsupportedEncodingException (java.io.UnsupportedEncodingException)13 ECKey (io.nuls.core.tools.crypto.ECKey)12 ContractResult (io.nuls.contract.dto.ContractResult)11 Test (org.junit.Test)11 TransferTransaction (io.nuls.protocol.model.tx.TransferTransaction)9 TransactionDataResult (io.nuls.account.ledger.model.TransactionDataResult)7 ValidateResult (io.nuls.kernel.validate.ValidateResult)7 Agent (io.nuls.consensus.poc.protocol.entity.Agent)6 CancelDeposit (io.nuls.consensus.poc.protocol.entity.CancelDeposit)6 StopAgent (io.nuls.consensus.poc.protocol.entity.StopAgent)6 CancelDepositTransaction (io.nuls.consensus.poc.protocol.tx.CancelDepositTransaction)6 DepositTransaction (io.nuls.consensus.poc.protocol.tx.DepositTransaction)6 Result (io.nuls.kernel.model.Result)6 ArrayList (java.util.ArrayList)6