Search in sources :

Example 6 with CoinTransferData

use of io.nuls.ledger.entity.params.CoinTransferData in project nuls by nuls-io.

the class AccountServiceImpl method setAlias.

@Override
public Result setAlias(String address, String password, String alias) {
    Account account = getAccount(address);
    if (account == null) {
        return new Result(false, ErrorCode.ACCOUNT_NOT_EXIST, null);
    }
    if (StringUtils.isNotBlank(account.getAlias())) {
        return new Result(false, "Alias has been set up");
    }
    if (!StringUtils.validAlias(alias)) {
        return new Result(false, "The alias is between 3 to 20 characters");
    }
    try {
        TransactionEvent event = new TransactionEvent();
        CoinTransferData coinData = new CoinTransferData(OperationType.TRANSFER, AccountConstant.ALIAS_NA, address, ledgerService.getTxFee(TransactionConstant.TX_TYPE_SET_ALIAS));
        AliasTransaction aliasTx = new AliasTransaction(coinData, password, new Alias(address, alias));
        aliasTx.setHash(NulsDigestData.calcDigestData(aliasTx.serialize()));
        aliasTx.setScriptSig(createP2PKHScriptSigFromDigest(aliasTx.getHash(), account, password).serialize());
        ValidateResult validate = aliasTx.verify();
        if (validate.isFailed()) {
            return new Result(false, validate.getMessage());
        }
        event.setEventBody(aliasTx);
        eventBroadcaster.broadcastAndCache(event, true);
        SetAliasNotice notice = new SetAliasNotice();
        notice.setEventBody(aliasTx);
        eventBroadcaster.publishToLocal(notice);
    } catch (Exception e) {
        Log.error(e);
        return new Result(false, e.getMessage());
    }
    return new Result(true, "OK");
}
Also used : Account(io.nuls.account.entity.Account) AliasTransaction(io.nuls.account.entity.tx.AliasTransaction) TransactionEvent(io.nuls.ledger.event.TransactionEvent) CoinTransferData(io.nuls.ledger.entity.params.CoinTransferData) Alias(io.nuls.account.entity.Alias) ValidateResult(io.nuls.core.validate.ValidateResult) NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) ValidateResult(io.nuls.core.validate.ValidateResult) Result(io.nuls.core.chain.entity.Result)

Example 7 with CoinTransferData

use of io.nuls.ledger.entity.params.CoinTransferData in project nuls by nuls-io.

the class GenesisBlock method initGengsisTxs.

private void initGengsisTxs(Map<String, Object> jsonMap) {
    List<Map<String, Object>> list = (List<Map<String, Object>>) jsonMap.get(CONFIG_FILED_TXS);
    if (null == list || list.isEmpty()) {
        throw new NulsRuntimeException(ErrorCode.CONFIG_ERROR);
    }
    CoinTransferData data = new CoinTransferData(OperationType.COIN_BASE, Na.ZERO);
    data.setPriKey(Hex.decode(priKey));
    Na total = Na.ZERO;
    for (Map<String, Object> map : list) {
        String address = (String) map.get(CONFIG_FILED_ADDRESS);
        AssertUtil.canNotEmpty(address, ErrorCode.NULL_PARAMETER);
        Integer nuls = (Integer) map.get(CONFIG_FILED_NULS);
        AssertUtil.canNotEmpty(nuls, ErrorCode.NULL_PARAMETER);
        Integer height = (Integer) map.get(CONFIG_FILED_UNLOCK_HEIGHT);
        Coin coin = new Coin();
        coin.setNa(Na.parseNuls(nuls));
        coin.setUnlockTime(0);
        if (height == null) {
            coin.setUnlockTime(0);
        } else {
            coin.setUnlockHeight(height.longValue());
        }
        data.addTo(address, coin);
        total = total.add(coin.getNa());
    }
    data.setTotalNa(total);
    CoinBaseTransaction tx = null;
    try {
        tx = new CoinBaseTransaction(data, null);
    } catch (NulsException e) {
        Log.error(e);
        throw new NulsRuntimeException(e);
    }
    tx.setTime(this.blockTime);
    tx.setFee(Na.ZERO);
    try {
        tx.setHash(NulsDigestData.calcDigestData(tx.serialize()));
    } catch (IOException e) {
        Log.error(e);
        throw new NulsRuntimeException(e);
    }
    P2PKHScriptSig p2PKHScriptSig = new P2PKHScriptSig();
    Account account = null;
    try {
        account = AccountTool.createAccount(priKey);
    } catch (NulsException e) {
        e.printStackTrace();
    }
    AccountService accountService = NulsContext.getServiceBean(AccountService.class);
    P2PKHScriptSig scriptSig = null;
    try {
        scriptSig = accountService.createP2PKHScriptSigFromDigest(tx.getHash(), account, "");
    } catch (NulsException e) {
        e.printStackTrace();
    }
    try {
        tx.setScriptSig(scriptSig.serialize());
    } catch (IOException e) {
        e.printStackTrace();
    }
    List<Transaction> txlist = new ArrayList<>();
    // tx.setStatus(TxStatusEnum.AGREED);
    txlist.add(tx);
    setTxs(txlist);
}
Also used : Account(io.nuls.account.entity.Account) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) IOException(java.io.IOException) BigInteger(java.math.BigInteger) Coin(io.nuls.ledger.entity.params.Coin) P2PKHScriptSig(io.nuls.core.script.P2PKHScriptSig) CoinTransferData(io.nuls.ledger.entity.params.CoinTransferData) CoinBaseTransaction(io.nuls.ledger.entity.tx.CoinBaseTransaction) CoinBaseTransaction(io.nuls.ledger.entity.tx.CoinBaseTransaction) NulsException(io.nuls.core.exception.NulsException) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) AccountService(io.nuls.account.service.intf.AccountService)

Aggregations

NulsException (io.nuls.core.exception.NulsException)7 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)7 CoinTransferData (io.nuls.ledger.entity.params.CoinTransferData)7 Coin (io.nuls.ledger.entity.params.Coin)5 TransactionEvent (io.nuls.ledger.event.TransactionEvent)5 IOException (java.io.IOException)4 Account (io.nuls.account.entity.Account)3 ValidateResult (io.nuls.core.validate.ValidateResult)3 PocJoinConsensusTransaction (io.nuls.consensus.entity.tx.PocJoinConsensusTransaction)2 RegisterAgentTransaction (io.nuls.consensus.entity.tx.RegisterAgentTransaction)2 CoinBaseTransaction (io.nuls.ledger.entity.tx.CoinBaseTransaction)2 Alias (io.nuls.account.entity.Alias)1 AliasTransaction (io.nuls.account.entity.tx.AliasTransaction)1 AccountService (io.nuls.account.service.intf.AccountService)1 ConsensusAgentImpl (io.nuls.consensus.entity.ConsensusAgentImpl)1 ConsensusDepositImpl (io.nuls.consensus.entity.ConsensusDepositImpl)1 ConsensusReward (io.nuls.consensus.entity.meeting.ConsensusReward)1 Agent (io.nuls.consensus.entity.member.Agent)1 Deposit (io.nuls.consensus.entity.member.Deposit)1 PocExitConsensusTransaction (io.nuls.consensus.entity.tx.PocExitConsensusTransaction)1