Search in sources :

Example 11 with Account

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

Example 12 with Account

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

the class UtxoCoinDataProvider method createByTransferData.

@Override
public CoinData createByTransferData(Transaction tx, CoinTransferData coinParam, String password) throws NulsException {
    UtxoData utxoData = new UtxoData();
    List<UtxoInput> inputs = new ArrayList<>();
    List<UtxoOutput> outputs = new ArrayList<>();
    if (coinParam.getTotalNa().equals(Na.ZERO)) {
        utxoData.setInputs(inputs);
        utxoData.setOutputs(outputs);
        return utxoData;
    }
    long inputValue = 0;
    if (!coinParam.getFrom().isEmpty()) {
        // find unSpends to create inputs for this tx
        Na totalFee = Na.ZERO;
        if (tx instanceof UnlockNulsTransaction) {
            totalFee = coinParam.getFee();
        } else {
            totalFee = coinParam.getTotalNa().add(coinParam.getFee());
        }
        List<UtxoOutput> unSpends = coinManager.getAccountsUnSpend(coinParam.getFrom(), totalFee);
        if (unSpends.isEmpty()) {
            throw new NulsException(ErrorCode.BALANCE_NOT_ENOUGH);
        }
        for (int i = 0; i < unSpends.size(); i++) {
            UtxoOutput output = unSpends.get(i);
            UtxoInput input = new UtxoInput();
            input.setFrom(output);
            input.setFromHash(output.getTxHash());
            input.setFromIndex(output.getIndex());
            input.setTxHash(tx.getHash());
            input.setIndex(i);
            inputValue += output.getValue();
            inputs.add(input);
        }
    }
    // get EcKey for output's script
    Account account = null;
    byte[] priKey = null;
    if (coinParam.getPriKey() != null) {
        priKey = coinParam.getPriKey();
    } else if (!coinParam.getFrom().isEmpty()) {
        account = accountService.getAccount(coinParam.getFrom().get(0));
        if (account == null) {
            throw new NulsException(ErrorCode.ACCOUNT_NOT_EXIST);
        }
        if (account.isEncrypted() && account.isLocked()) {
            if (!account.unlock(password)) {
                throw new NulsException(ErrorCode.PASSWORD_IS_WRONG);
            }
            priKey = account.getPriKey();
            account.lock();
        } else {
            priKey = account.getPriKey();
        }
    }
    // create outputs
    int i = 0;
    long outputValue = 0;
    for (Map.Entry<String, List<Coin>> entry : coinParam.getToMap().entrySet()) {
        String address = entry.getKey();
        List<Coin> coinList = entry.getValue();
        for (Coin coin : coinList) {
            UtxoOutput output = new UtxoOutput();
            output.setAddress(address);
            output.setValue(coin.getNa().getValue());
            if (output.getLockTime() > 0) {
                output.setStatus(OutPutStatusEnum.UTXO_UNCONFIRM_TIME_LOCK);
            } else if (tx instanceof LockNulsTransaction) {
                output.setStatus(OutPutStatusEnum.UTXO_UNCONFIRM_CONSENSUS_LOCK);
            } else {
                output.setStatus(OutPutStatusEnum.UTXO_UNCONFIRM_UNSPEND);
            }
            output.setIndex(i);
            P2PKHScript p2PKHScript = new P2PKHScript(new NulsDigestData(NulsDigestData.DIGEST_ALG_SHA160, new Address(address).getHash160()));
            output.setP2PKHScript(p2PKHScript);
            if (coin.getUnlockHeight() > 0) {
                output.setLockTime(coin.getUnlockHeight());
            } else if (coin.getUnlockTime() > 0) {
                output.setLockTime(coin.getUnlockTime());
            } else {
                output.setLockTime(0L);
            }
            output.setTxHash(tx.getHash());
            outputValue += output.getValue();
            outputs.add(output);
            i++;
        }
    }
    // the balance leave to myself
    long balance = inputValue - outputValue - coinParam.getFee().getValue();
    if (balance > 0) {
        UtxoOutput output = new UtxoOutput();
        // todo script
        output.setAddress(inputs.get(0).getFrom().getAddress());
        output.setValue(balance);
        output.setIndex(i);
        output.setTxHash(tx.getHash());
        output.setStatus(OutPutStatusEnum.UTXO_UNCONFIRM_UNSPEND);
        P2PKHScript p2PKHScript = new P2PKHScript(new NulsDigestData(NulsDigestData.DIGEST_ALG_SHA160, account.getHash160()));
        output.setP2PKHScript(p2PKHScript);
        outputs.add(output);
    }
    utxoData.setInputs(inputs);
    utxoData.setOutputs(outputs);
    return utxoData;
}
Also used : P2PKHScript(io.nuls.core.script.P2PKHScript) Account(io.nuls.account.entity.Account) Address(io.nuls.account.entity.Address) Coin(io.nuls.ledger.entity.params.Coin) LockNulsTransaction(io.nuls.ledger.entity.tx.LockNulsTransaction) Na(io.nuls.core.chain.entity.Na) NulsException(io.nuls.core.exception.NulsException) UnlockNulsTransaction(io.nuls.ledger.entity.tx.UnlockNulsTransaction) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData)

Example 13 with Account

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

the class UtxoTransactionTool method createTransferTx.

public TransferTransaction createTransferTx(CoinTransferData transferData, String password, String remark) throws Exception {
    if (transferData.getFrom().isEmpty()) {
        throw new NulsRuntimeException(ErrorCode.DATA_ERROR);
    }
    TransferTransaction tx = new TransferTransaction(transferData, password);
    if (StringUtils.isNotBlank(remark)) {
        tx.setRemark(remark.getBytes(NulsContext.DEFAULT_ENCODING));
    }
    tx.setHash(NulsDigestData.calcDigestData(tx.serialize()));
    AccountService accountService = getAccountService();
    Account account = accountService.getAccount(transferData.getFrom().get(0));
    tx.setScriptSig(accountService.createP2PKHScriptSigFromDigest(tx.getHash(), account, password).serialize());
    return tx;
}
Also used : Account(io.nuls.account.entity.Account) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) TransferTransaction(io.nuls.ledger.entity.tx.TransferTransaction) AccountService(io.nuls.account.service.intf.AccountService)

Example 14 with Account

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

the class UtxoTransactionTool method createLockNulsTx.

public LockNulsTransaction createLockNulsTx(CoinTransferData transferData, String password, String remark) throws Exception {
    LockNulsTransaction tx = new LockNulsTransaction(transferData, password);
    if (StringUtils.isNotBlank(remark)) {
        tx.setRemark(remark.getBytes(NulsContext.DEFAULT_ENCODING));
    }
    tx.setHash(NulsDigestData.calcDigestData(tx.serialize()));
    AccountService accountService = getAccountService();
    if (transferData.getFrom().isEmpty()) {
        throw new NulsRuntimeException(ErrorCode.DATA_ERROR);
    }
    Account account = accountService.getAccount(transferData.getFrom().get(0));
    tx.setScriptSig(accountService.createP2PKHScriptSigFromDigest(tx.getHash(), account, password).serialize());
    return tx;
}
Also used : LockNulsTransaction(io.nuls.ledger.entity.tx.LockNulsTransaction) Account(io.nuls.account.entity.Account) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) AccountService(io.nuls.account.service.intf.AccountService)

Example 15 with Account

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

the class PocConsensusServiceImpl method getConsensusInfo.

@Override
public Map<String, Object> getConsensusInfo(String address) {
    if (StringUtils.isNotBlank(address)) {
        return getConsensusInfoByAddress(address);
    }
    List<Account> accountList = this.accountService.getAccountList();
    if (accountList == null || accountList.isEmpty()) {
        return null;
    }
    long lastDayTime = TimeService.currentTimeMillis() - DateUtil.DATE_TIME;
    int agentCount = 0;
    long totalDeposit = 0;
    long reward = 0;
    long rewardOfDay = 0;
    long usableBalance = 0;
    Set<String> joinedAgent = new HashSet<>();
    for (Account account : accountList) {
        Consensus<Agent> agent = this.consensusCacheManager.getCachedAgentByAddress(account.getAddress().toString());
        List<Consensus<Deposit>> depositList = this.consensusCacheManager.getCachedDepositListByAddress(account.getAddress().toString());
        for (Consensus<Deposit> cd : depositList) {
            totalDeposit += cd.getExtend().getDeposit().getValue();
            joinedAgent.add(cd.getExtend().getAgentHash());
        }
        if (null != agent) {
            agentCount++;
            totalDeposit += agent.getExtend().getDeposit().getValue();
        }
        reward += ledgerService.getAccountReward(account.getAddress().getBase58(), 0);
        rewardOfDay += ledgerService.getAccountReward(account.getAddress().getBase58(), lastDayTime);
        Balance balance = ledgerService.getBalance(account.getAddress().getBase58());
        if (balance != null) {
            usableBalance += balance.getUsable().getValue();
        }
    }
    Map<String, Object> map = new HashMap<>();
    map.put("agentCount", agentCount);
    map.put("totalDeposit", totalDeposit);
    map.put("joinAccountCount", joinedAgent.size());
    map.put("usableBalance", usableBalance);
    map.put("reward", reward);
    map.put("rewardOfDay", rewardOfDay);
    return map;
}
Also used : Account(io.nuls.account.entity.Account) Agent(io.nuls.consensus.entity.member.Agent) Deposit(io.nuls.consensus.entity.member.Deposit) Consensus(io.nuls.consensus.entity.Consensus) Balance(io.nuls.ledger.entity.Balance)

Aggregations

Account (io.nuls.account.entity.Account)28 NulsException (io.nuls.core.exception.NulsException)14 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)11 Result (io.nuls.core.chain.entity.Result)9 ValidateResult (io.nuls.core.validate.ValidateResult)9 AccountPo (io.nuls.db.entity.AccountPo)7 IOException (java.io.IOException)7 DbSession (io.nuls.db.transactional.annotation.DbSession)4 AccountService (io.nuls.account.service.intf.AccountService)3 Agent (io.nuls.consensus.entity.member.Agent)3 CoinTransferData (io.nuls.ledger.entity.params.CoinTransferData)3 Address (io.nuls.account.entity.Address)2 Alias (io.nuls.account.entity.Alias)2 AliasTransaction (io.nuls.account.entity.tx.AliasTransaction)2 Consensus (io.nuls.consensus.entity.Consensus)2 Deposit (io.nuls.consensus.entity.member.Deposit)2 Transaction (io.nuls.core.chain.entity.Transaction)2 AliasPo (io.nuls.db.entity.AliasPo)2 Coin (io.nuls.ledger.entity.params.Coin)2 LockNulsTransaction (io.nuls.ledger.entity.tx.LockNulsTransaction)2