Search in sources :

Example 6 with TransferTransaction

use of io.nuls.protocol.model.tx.TransferTransaction in project nuls by nuls-io.

the class AccountLedgerResource method createMultipleTx.

@POST
@Path("/createMultipleTx")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "创建多账户转账交易", notes = "result.data: resultJson 返回交易Hex")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success") })
public RpcClientResult createMultipleTx(@ApiParam(name = "form", value = "交易输入输出", required = true) TransactionForm form) {
    if (form.getInputs() == null || form.getInputs().isEmpty()) {
        return RpcClientResult.getFailed("inputs error");
    }
    if (form.getOutputs() == null || form.getOutputs().isEmpty()) {
        return RpcClientResult.getFailed("outputs error");
    }
    byte[] remark = null;
    if (!StringUtils.isBlank(form.getRemark())) {
        try {
            remark = form.getRemark().getBytes(NulsConfig.DEFAULT_ENCODING);
        } catch (UnsupportedEncodingException e) {
            return RpcClientResult.getFailed("remark error");
        }
    }
    List<Coin> outputList = new ArrayList<>();
    for (int i = 0; i < form.getOutputs().size(); i++) {
        OutputDto outputDto = form.getOutputs().get(i);
        Coin to = new Coin();
        try {
            if (!AddressTool.validAddress(outputDto.getAddress())) {
                return Result.getFailed(AccountErrorCode.ADDRESS_ERROR).toRpcClientResult();
            }
            byte[] owner = AddressTool.getAddress(outputDto.getAddress());
            if (owner[2] == 3) {
                Script scriptPubkey = SignatureUtil.createOutputScript(to.getAddress());
                to.setOwner(scriptPubkey.getProgram());
            } else {
                to.setOwner(AddressTool.getAddress(outputDto.getAddress()));
            }
        } catch (Exception e) {
            return Result.getFailed(AccountErrorCode.ADDRESS_ERROR).toRpcClientResult();
        }
        try {
            to.setNa(Na.valueOf(outputDto.getValue()));
        } catch (Exception e) {
            return Result.getFailed(AccountErrorCode.DATA_PARSE_ERROR).toRpcClientResult();
        }
        if (outputDto.getLockTime() < 0) {
            return RpcClientResult.getFailed("lockTime error");
        }
        to.setLockTime(outputDto.getLockTime());
        outputList.add(to);
    }
    List<Coin> inputsList = new ArrayList<>();
    Set<String> addressSet = new HashSet<>();
    for (int i = 0; i < form.getInputs().size(); i++) {
        InputDto inputDto = form.getInputs().get(i);
        if (inputDto.getAddress() == null) {
            return Result.getFailed(AccountErrorCode.ADDRESS_ERROR).toRpcClientResult();
        }
        addressSet.add(inputDto.getAddress());
        byte[] key = Arrays.concatenate(Hex.decode(inputDto.getFromHash()), new VarInt(inputDto.getFromIndex()).encode());
        Coin coin = new Coin();
        coin.setOwner(key);
        coin.setNa(Na.valueOf(inputDto.getValue()));
        coin.setLockTime(inputDto.getLockTime());
        inputsList.add(coin);
    }
    TransferTransaction tx = new TransferTransaction();
    CoinData coinData = new CoinData();
    coinData.setFrom(inputsList);
    coinData.setTo(outputList);
    tx.setCoinData(coinData);
    tx.setTime(TimeService.currentTimeMillis());
    tx.setRemark(remark);
    if (!isFeeEnough(tx, P2PHKSignature.SERIALIZE_LENGTH * addressSet.size())) {
        return Result.getFailed(TransactionErrorCode.FEE_NOT_RIGHT).toRpcClientResult();
    }
    try {
        String txHex = Hex.encode(tx.serialize());
        Map<String, String> map = new HashMap<>();
        map.put("value", txHex);
        return Result.getSuccess().setData(map).toRpcClientResult();
    } catch (IOException e) {
        Log.error(e);
        return RpcClientResult.getFailed(e.getMessage());
    }
}
Also used : Script(io.nuls.kernel.script.Script) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) CryptoException(io.nuls.core.tools.crypto.Exception.CryptoException) NulsException(io.nuls.kernel.exception.NulsException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) IOException(java.io.IOException) TransferTransaction(io.nuls.protocol.model.tx.TransferTransaction)

Example 7 with TransferTransaction

use of io.nuls.protocol.model.tx.TransferTransaction in project nuls by nuls-io.

the class InventoryFilterTest method test.

@Test
public void test() throws IOException {
    BloomFilter<byte[]> filter = BloomFilter.create(Funnels.byteArrayFunnel(), 1000000, 0.00001);
    List<String> list = new ArrayList<>();
    Set<NulsDigestData> set = new HashSet<>();
    ArrayList<Transaction> txList = new ArrayList<>();
    for (int i = 0; i < 1000000; i++) {
        Transaction tx = new TransferTransaction();
        tx.setTime(i);
        tx.setRemark("sdfsdfsdfsdfsdfsdfaaadsfasdfsadfsdfasdfasdfasdfasdfasdfsadfaaaaaaaaaaaaaaaaaaaaaabsdsadfsadfsdfsdfsdfsdfsdfsdfsdfaaadsfasdfsadfsdfasdfasdfasdfasdfasdfsadfaaaaaaaaaaaaaaaaaaaaaabsdsadfsadfsdfsdfsdfsdfsdfsdfsdfaa".getBytes());
        tx.setHash(NulsDigestData.calcDigestData(tx.serializeForHash()));
        txList.add(tx);
    }
    for (int i = 0; i < 2; i++) {
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                for (Transaction tx : txList) {
                    NulsDigestData hash = tx.getHash();
                    if (!filter.mightContain(hash.getDigestBytes())) {
                        filter.put(hash.getDigestBytes());
                        set.add(hash);
                        int num = count.incrementAndGet();
                        if (num % 1000 == 0) {
                            System.out.println("count::::::" + num);
                        }
                    }
                }
                list.add("done");
            }
        });
        t.start();
    }
    while (list.size() < 5) {
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    System.out.println("count====" + count.get());
    System.out.println("real-size====" + set.size());
}
Also used : ArrayList(java.util.ArrayList) Transaction(io.nuls.kernel.model.Transaction) TransferTransaction(io.nuls.protocol.model.tx.TransferTransaction) NulsDigestData(io.nuls.kernel.model.NulsDigestData) TransferTransaction(io.nuls.protocol.model.tx.TransferTransaction) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 8 with TransferTransaction

use of io.nuls.protocol.model.tx.TransferTransaction in project nuls by nuls-io.

the class TransactionServiceImplTest method createTransferTransaction.

private TransferTransaction createTransferTransaction(ECKey ecKey1, byte[] coinKey, ECKey ecKey2, Na na) {
    TransferTransaction tx = new TransferTransaction();
    setCommonFields(tx);
    CoinData coinData = new CoinData();
    List<Coin> fromList = new ArrayList<>();
    fromList.add(new Coin(coinKey, Na.parseNuls(10001), 0));
    coinData.setFrom(fromList);
    List<Coin> toList = new ArrayList<>();
    toList.add(new Coin(AddressTool.getAddress(ecKey2.getPubKey()), Na.parseNuls(10000), 1000));
    coinData.setTo(toList);
    tx.setCoinData(coinData);
    signTransaction(tx, ecKey1);
    return tx;
}
Also used : ArrayList(java.util.ArrayList) TransferTransaction(io.nuls.protocol.model.tx.TransferTransaction)

Example 9 with TransferTransaction

use of io.nuls.protocol.model.tx.TransferTransaction in project nuls by nuls-io.

the class TransactionServiceImplTest method initTxList.

private void initTxList() {
    List<Transaction> list = new ArrayList<>();
    ECKey ecKey1 = new ECKey();
    ECKey ecKey2 = new ECKey();
    ECKey ecKey3 = new ECKey();
    ECKey ecKey4 = new ECKey();
    ECKey ecKey5 = new ECKey();
    ECKey ecKey6 = new ECKey();
    Transaction tx = createCoinBaseTransaction(ecKey1, ecKey2, ecKey3, ecKey4, ecKey5, ecKey6);
    list.add(tx);
    Transaction yellowPunishTx = createYellowPunishTx(ecKey1, ecKey2, ecKey3, ecKey4, ecKey5, ecKey6);
    list.add(yellowPunishTx);
    // RedPunishTransaction redPunishTransaction = createRedPunishTx(ecKey1, ecKey4, ecKey5, ecKey6);
    // list.add(redPunishTransaction);
    TransferTransaction transferTransaction1 = createTransferTransaction(ecKey1, null, ecKey2, Na.ZERO);
    TransferTransaction transferTransaction2 = createTransferTransaction(ecKey1, null, ecKey3, Na.ZERO);
    list.add(transferTransaction1);
    list.add(transferTransaction2);
    createSetAliasTransaction(ecKey1, "alias");
    // createSetAliasTransaction(ecKey1, "alias1");
    // createSetAliasTransaction(ecKey2, "alias");
    CreateAgentTransaction tx1 = createRegisterAgentTransaction(ecKey1, ecKey2, "agentName");
    CreateAgentTransaction tx2 = createRegisterAgentTransaction(ecKey2, ecKey3, "agentName");
    CreateAgentTransaction tx3 = createRegisterAgentTransaction(ecKey4, ecKey5, "agentName2");
    CreateAgentTransaction tx4 = createRegisterAgentTransaction(ecKey1, ecKey3, "agentName3");
    list.add(tx1);
    list.add(tx2);
    list.add(tx3);
    list.add(tx4);
    DepositTransaction join1 = createDepositTransaction(ecKey1, tx1.getHash(), Na.parseNuls(200000));
    DepositTransaction join2 = createDepositTransaction(ecKey1, tx2.getHash(), Na.parseNuls(200000));
    DepositTransaction join3 = createDepositTransaction(ecKey1, tx3.getHash(), Na.parseNuls(200000));
    DepositTransaction join4 = createDepositTransaction(ecKey1, tx4.getHash(), Na.parseNuls(200000));
    DepositTransaction join5 = createDepositTransaction(ecKey1, tx3.getHash(), Na.parseNuls(200000));
    DepositTransaction join6 = createDepositTransaction(ecKey1, tx3.getHash(), Na.parseNuls(200000));
    DepositTransaction join7 = createDepositTransaction(ecKey1, tx3.getHash(), Na.parseNuls(200000));
    list.add(join1);
    list.add(join3);
    list.add(join2);
    list.add(join4);
    list.add(join5);
    list.add(join6);
    list.add(join7);
    try {
        createCancelDepositTransaction(ecKey1, NulsDigestData.fromDigestHex("txHash"));
    } catch (NulsException e) {
        Log.error(e);
    }
    StopAgentTransaction stop1 = createStopAgentTransaction(ecKey1, tx1.getHash());
    StopAgentTransaction stop2 = createStopAgentTransaction(ecKey1, tx2.getHash());
    StopAgentTransaction stop3 = createStopAgentTransaction(ecKey4, tx3.getHash());
    StopAgentTransaction stop4 = createStopAgentTransaction(ecKey1, tx4.getHash());
    list.add(stop1);
    list.add(stop2);
    list.add(stop3);
    list.add(stop4);
    this.allList = list;
}
Also used : TransferTransaction(io.nuls.protocol.model.tx.TransferTransaction) CoinBaseTransaction(io.nuls.protocol.model.tx.CoinBaseTransaction) NulsException(io.nuls.kernel.exception.NulsException) ArrayList(java.util.ArrayList) ECKey(io.nuls.core.tools.crypto.ECKey) TransferTransaction(io.nuls.protocol.model.tx.TransferTransaction)

Example 10 with TransferTransaction

use of io.nuls.protocol.model.tx.TransferTransaction in project nuls by nuls-io.

the class ScriptTransactionTestTool method test1.

// @Test
public void test1() throws IOException {
    NulsContext.MAIN_NET_VERSION = 2;
    TransferTransaction tx = new TransferTransaction();
    tx.setRemark("test script".getBytes());
    CoinData data = new CoinData();
    Coin coin = new Coin();
    coin.setOwner(ArraysTool.concatenate(Hex.decode("0020dab71b3cd376e2ccf2f290e384d2917cc0929f8de582f63a01fc15144fe38371"), new byte[] { 0 }));
    coin.setNa(Na.parseNuls(9997));
    coin.setLockTime(0);
    List<Coin> from = new ArrayList<>();
    from.add(coin);
    data.setFrom(from);
    Coin toCoin = new Coin();
    toCoin.setLockTime(0);
    Script script = ScriptBuilder.createOutputScript(AddressTool.getAddress("NsdvuzHyQJEJkz4LEKweDeCs97845xN9"), 1);
    toCoin.setOwner(script.getProgram());
    toCoin.setNa(Na.parseNuls(9994));
    List<Coin> to = new ArrayList<>();
    to.add(toCoin);
    data.setTo(to);
    tx.setCoinData(data);
    // ECKey ecKey = ECKey.fromPrivate(new BigInteger(1,Hex.decode("00b491621168dffd80c4684f7445ef378ba4d381b2fe2a7b1fbf905864ed8fbeb9")));
    ECKey ecKey = ECKey.fromPrivate(new BigInteger(1, Hex.decode("4b19caef601a45531b7068430a5b0e380a004001f14bfec025ddf16d5d87fa8e")));
    List<ECKey> signEckeys = new ArrayList<>();
    signEckeys.add(ecKey);
    List<ECKey> scriptEckeys = new ArrayList<>();
    SignatureUtil.createTransactionSignture(tx, scriptEckeys, signEckeys);
    String param = "{\"txHex\": \"" + Hex.encode(tx.serialize()) + "\"}";
    String res = post("http://127.0.0.1:7001/api/accountledger/transaction/valiTransaction", param, "utf-8");
    System.out.println(res);
    res = post("http://127.0.0.1:7001/api/accountledger/transaction/broadcast", param, "utf-8");
    System.out.println(res);
}
Also used : Coin(io.nuls.kernel.model.Coin) Script(io.nuls.kernel.script.Script) CoinData(io.nuls.kernel.model.CoinData) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) ECKey(io.nuls.core.tools.crypto.ECKey) TransferTransaction(io.nuls.protocol.model.tx.TransferTransaction)

Aggregations

TransferTransaction (io.nuls.protocol.model.tx.TransferTransaction)22 NulsException (io.nuls.kernel.exception.NulsException)11 IOException (java.io.IOException)11 UnsupportedEncodingException (java.io.UnsupportedEncodingException)10 CoinDataResult (io.nuls.account.ledger.model.CoinDataResult)9 ECKey (io.nuls.core.tools.crypto.ECKey)8 ArrayList (java.util.ArrayList)8 Account (io.nuls.account.model.Account)7 MultiSigAccount (io.nuls.account.model.MultiSigAccount)7 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)7 ValidateResult (io.nuls.kernel.validate.ValidateResult)7 TransactionDataResult (io.nuls.account.ledger.model.TransactionDataResult)6 MultipleAddressTransferModel (io.nuls.account.ledger.model.MultipleAddressTransferModel)4 NulsDigestData (io.nuls.kernel.model.NulsDigestData)4 Transaction (io.nuls.kernel.model.Transaction)4 Script (io.nuls.kernel.script.Script)4 Test (org.junit.Test)4 CryptoException (io.nuls.core.tools.crypto.Exception.CryptoException)2 Coin (io.nuls.kernel.model.Coin)2 CoinData (io.nuls.kernel.model.CoinData)2