Search in sources :

Example 6 with TopicCreateTransaction

use of com.hedera.hashgraph.sdk.TopicCreateTransaction in project hedera-sdk-java by hashgraph.

the class TopicMessageSubmitIntegrationTest method canSubmitALargeTopicMessage.

@Test
@DisplayName("Can submit a large topic message")
void canSubmitALargeTopicMessage() {
    // Skip if using PreviewNet
    Assumptions.assumeTrue(!System.getProperty("HEDERA_NETWORK").equals("previewnet"));
    assertThatNoException().isThrownBy(() -> {
        var testEnv = new IntegrationTestEnv(2);
        var response = new TopicCreateTransaction().setAdminKey(testEnv.operatorKey).setTopicMemo("[e2e::TopicCreateTransaction]").execute(testEnv.client);
        var topicId = Objects.requireNonNull(response.getReceipt(testEnv.client).topicId);
        Thread.sleep(5000);
        @Var var info = new TopicInfoQuery().setTopicId(topicId).execute(testEnv.client);
        assertThat(info.topicId).isEqualTo(topicId);
        assertThat(info.topicMemo).isEqualTo("[e2e::TopicCreateTransaction]");
        assertThat(info.sequenceNumber).isEqualTo(0);
        assertThat(info.adminKey).isEqualTo(testEnv.operatorKey);
        var responses = new TopicMessageSubmitTransaction().setTopicId(topicId).setMaxChunks(15).setMessage(Contents.BIG_CONTENTS).executeAll(testEnv.client);
        for (var resp : responses) {
            resp.getReceipt(testEnv.client);
        }
        info = new TopicInfoQuery().setTopicId(topicId).execute(testEnv.client);
        assertThat(info.topicId).isEqualTo(topicId);
        assertThat(info.topicMemo).isEqualTo("[e2e::TopicCreateTransaction]");
        assertThat(info.sequenceNumber).isEqualTo(14);
        assertThat(info.adminKey).isEqualTo(testEnv.operatorKey);
        new TopicDeleteTransaction().setTopicId(topicId).execute(testEnv.client).getReceipt(testEnv.client);
        testEnv.close();
    });
}
Also used : TopicCreateTransaction(com.hedera.hashgraph.sdk.TopicCreateTransaction) TopicInfoQuery(com.hedera.hashgraph.sdk.TopicInfoQuery) Var(com.google.errorprone.annotations.Var) TopicMessageSubmitTransaction(com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction) TopicDeleteTransaction(com.hedera.hashgraph.sdk.TopicDeleteTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 7 with TopicCreateTransaction

use of com.hedera.hashgraph.sdk.TopicCreateTransaction in project hedera-sdk-java by hashgraph.

the class ScheduleCreateIntegrationTest method canScheduleTopicMessage.

@Test
@DisplayName("Can schedule topic message")
void canScheduleTopicMessage() throws Exception {
    var testEnv = new IntegrationTestEnv(1);
    // Generate 3 random keys
    var key1 = PrivateKey.generateED25519();
    // This is the submit key
    var key2 = PrivateKey.generateED25519();
    var key3 = PrivateKey.generateED25519();
    var keyList = new KeyList();
    keyList.add(key1.getPublicKey());
    keyList.add(key2.getPublicKey());
    keyList.add(key3.getPublicKey());
    var response = new AccountCreateTransaction().setInitialBalance(new Hbar(100)).setKey(keyList).execute(testEnv.client);
    assertThat(response.getReceipt(testEnv.client).accountId).isNotNull();
    var topicId = Objects.requireNonNull(new TopicCreateTransaction().setAdminKey(testEnv.operatorKey).setAutoRenewAccountId(testEnv.operatorId).setTopicMemo("HCS Topic_").setSubmitKey(key2.getPublicKey()).execute(testEnv.client).getReceipt(testEnv.client).topicId);
    var transaction = new TopicMessageSubmitTransaction().setTopicId(topicId).setMessage("scheduled hcs message".getBytes(StandardCharsets.UTF_8));
    // create schedule
    var scheduledTx = transaction.schedule().setAdminKey(testEnv.operatorKey).setPayerAccountId(testEnv.operatorId).setScheduleMemo("mirror scheduled E2E signature on create and sign_" + Instant.now());
    var scheduled = scheduledTx.freezeWith(testEnv.client);
    var scheduleId = Objects.requireNonNull(scheduled.execute(testEnv.client).getReceipt(testEnv.client).scheduleId);
    // verify schedule has been created and has 1 of 2 signatures
    @Var var info = new ScheduleInfoQuery().setScheduleId(scheduleId).execute(testEnv.client);
    assertThat(info).isNotNull();
    assertThat(info.scheduleId).isEqualTo(scheduleId);
    var infoTransaction = (TopicMessageSubmitTransaction) info.getScheduledTransaction();
    assertThat(transaction.getTopicId()).isEqualTo(infoTransaction.getTopicId());
    assertThat(transaction.getNodeAccountIds()).isEqualTo(infoTransaction.getNodeAccountIds());
    var scheduleSign = new ScheduleSignTransaction().setScheduleId(scheduleId).freezeWith(testEnv.client);
    scheduleSign.sign(key2).execute(testEnv.client).getReceipt(testEnv.client);
    info = new ScheduleInfoQuery().setScheduleId(scheduleId).execute(testEnv.client);
    assertThat(info.executedAt).isNotNull();
    testEnv.close();
}
Also used : TopicCreateTransaction(com.hedera.hashgraph.sdk.TopicCreateTransaction) Var(com.google.errorprone.annotations.Var) ScheduleSignTransaction(com.hedera.hashgraph.sdk.ScheduleSignTransaction) KeyList(com.hedera.hashgraph.sdk.KeyList) Hbar(com.hedera.hashgraph.sdk.Hbar) ScheduleInfoQuery(com.hedera.hashgraph.sdk.ScheduleInfoQuery) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) TopicMessageSubmitTransaction(com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 8 with TopicCreateTransaction

use of com.hedera.hashgraph.sdk.TopicCreateTransaction in project hedera-sdk-java by hashgraph.

the class TopicWithAdminKeyExample method createTopicWithAdminKey.

private void createTopicWithAdminKey() throws TimeoutException, PrecheckStatusException, ReceiptStatusException {
    // Generate the initial keys that are part of the adminKey's thresholdKey.
    // 3 ED25519 keys part of a 2-of-3 threshold key.
    initialAdminKeys = new PrivateKey[3];
    J8Arrays.setAll(initialAdminKeys, i -> PrivateKey.generate());
    KeyList thresholdKey = KeyList.withThreshold(2);
    Collections.addAll(thresholdKey, initialAdminKeys);
    Transaction<?> transaction = new TopicCreateTransaction().setTopicMemo("demo topic").setAdminKey(thresholdKey).freezeWith(hapiClient);
    // Sign the transaction with 2 of 3 keys that are part of the adminKey threshold key.
    J8Arrays.stream(initialAdminKeys, 0, 2).forEach(k -> {
        System.out.println("Signing ConsensusTopicCreateTransaction with key " + k);
        transaction.sign(k);
    });
    TransactionResponse transactionResponse = transaction.execute(hapiClient);
    topicId = transactionResponse.getReceipt(hapiClient).topicId;
    System.out.println("Created new topic " + topicId + " with 2-of-3 threshold key as adminKey.");
}
Also used : TopicCreateTransaction(com.hedera.hashgraph.sdk.TopicCreateTransaction) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) KeyList(com.hedera.hashgraph.sdk.KeyList)

Example 9 with TopicCreateTransaction

use of com.hedera.hashgraph.sdk.TopicCreateTransaction in project hedera-sdk-java by hashgraph.

the class ConsensusPubSubChunkedExample method main.

public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException, InterruptedException, InvalidProtocolBufferException {
    Client client = Client.forName(HEDERA_NETWORK);
    // Defaults the operator account ID and key such that all generated transactions will be paid for
    // by this account and be signed by this key
    client.setOperator(OPERATOR_ID, OPERATOR_KEY);
    // generate a submit key to use with the topic
    PrivateKey submitKey = PrivateKey.generateED25519();
    // make a new topic ID to use
    TopicId newTopicId = new TopicCreateTransaction().setTopicMemo("hedera-sdk-java/ConsensusPubSubChunkedExample").setSubmitKey(submitKey).execute(client).getReceipt(client).topicId;
    assert newTopicId != null;
    System.out.println("for topic " + newTopicId);
    // Let's wait a bit
    System.out.println("wait 10s to propagate to the mirror ...");
    Thread.sleep(10000);
    // setup a mirror client to print out messages as we receive them
    new TopicMessageQuery().setTopicId(newTopicId).subscribe(client, topicMessage -> {
        System.out.println("at " + topicMessage.consensusTimestamp + " ( seq = " + topicMessage.sequenceNumber + " ) received topic message of " + topicMessage.contents.length + " bytes");
    });
    // get a large file to send
    String bigContents = readResources("large_message.txt");
    System.out.println("about to prepare a transaction to send a message of " + bigContents.length() + " bytes");
    // prepare a message send transaction that requires a submit key from "somewhere else"
    @Var Transaction<?> transaction = new TopicMessageSubmitTransaction().setMaxChunks(// this is 10 by default
    15).setTopicId(newTopicId).setMessage(bigContents).signWithOperator(client);
    // serialize to bytes so we can be signed "somewhere else" by the submit key
    byte[] transactionBytes = transaction.toBytes();
    // now pretend we sent those bytes across the network
    // parse them into a transaction so we can sign as the submit key
    transaction = Transaction.fromBytes(transactionBytes);
    // view out the message size from the parsed transaction
    // this can be useful to display what we are about to sign
    long transactionMessageSize = ((TopicMessageSubmitTransaction) transaction).getMessage().size();
    System.out.println("about to send a transaction with a message of " + transactionMessageSize + " bytes");
    // sign with that submit key
    transaction.sign(submitKey);
    // now actually submit the transaction
    // get the receipt to ensure there were no errors
    transaction.execute(client).getReceipt(client);
    // noinspection InfiniteLoopStatement
    while (true) {
        System.out.println("waiting ...");
        // noinspection BusyWait
        Thread.sleep(2500);
    }
}
Also used : TopicCreateTransaction(com.hedera.hashgraph.sdk.TopicCreateTransaction) PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) Var(com.google.errorprone.annotations.Var) TopicId(com.hedera.hashgraph.sdk.TopicId) Client(com.hedera.hashgraph.sdk.Client) TopicMessageQuery(com.hedera.hashgraph.sdk.TopicMessageQuery) TopicMessageSubmitTransaction(com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction)

Example 10 with TopicCreateTransaction

use of com.hedera.hashgraph.sdk.TopicCreateTransaction in project hedera-sdk-java by hashgraph.

the class ConsensusPubSubExample method main.

public static void main(String[] args) throws TimeoutException, InterruptedException, PrecheckStatusException, ReceiptStatusException {
    Client client = Client.forName(HEDERA_NETWORK);
    // Defaults the operator account ID and key such that all generated transactions will be paid for
    // by this account and be signed by this key
    client.setOperator(OPERATOR_ID, OPERATOR_KEY);
    TransactionResponse transactionResponse = new TopicCreateTransaction().execute(client);
    TransactionReceipt transactionReceipt = transactionResponse.getReceipt(client);
    TopicId topicId = Objects.requireNonNull(transactionReceipt.topicId);
    System.out.println("New topic created: " + topicId);
    Thread.sleep(5000);
    new TopicMessageQuery().setTopicId(topicId).subscribe(client, resp -> {
        String messageAsString = new String(resp.contents, StandardCharsets.UTF_8);
        System.out.println(resp.consensusTimestamp + " received topic message: " + messageAsString);
    });
    // noinspection InfiniteLoopStatement
    for (int i = 0; ; i++) {
        new TopicMessageSubmitTransaction().setTopicId(topicId).setMessage("hello, HCS! " + i).execute(client).getReceipt(client);
        Thread.sleep(2500);
    }
}
Also used : TopicCreateTransaction(com.hedera.hashgraph.sdk.TopicCreateTransaction) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) TopicId(com.hedera.hashgraph.sdk.TopicId) Client(com.hedera.hashgraph.sdk.Client) TopicMessageQuery(com.hedera.hashgraph.sdk.TopicMessageQuery) TopicMessageSubmitTransaction(com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction)

Aggregations

TopicCreateTransaction (com.hedera.hashgraph.sdk.TopicCreateTransaction)26 Test (org.junit.jupiter.api.Test)19 DisplayName (org.junit.jupiter.api.DisplayName)17 TopicDeleteTransaction (com.hedera.hashgraph.sdk.TopicDeleteTransaction)15 TopicInfoQuery (com.hedera.hashgraph.sdk.TopicInfoQuery)10 TopicMessageSubmitTransaction (com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction)10 Var (com.google.errorprone.annotations.Var)4 TopicMessageQuery (com.hedera.hashgraph.sdk.TopicMessageQuery)4 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)4 Client (com.hedera.hashgraph.sdk.Client)3 PublicKey (com.hedera.hashgraph.sdk.PublicKey)3 TopicId (com.hedera.hashgraph.sdk.TopicId)3 Hbar (com.hedera.hashgraph.sdk.Hbar)2 KeyList (com.hedera.hashgraph.sdk.KeyList)2 TransactionReceipt (com.hedera.hashgraph.sdk.TransactionReceipt)2 AbstractTransactionSupplierTest (com.hedera.mirror.monitor.publish.transaction.AbstractTransactionSupplierTest)2 AccountCreateTransaction (com.hedera.hashgraph.sdk.AccountCreateTransaction)1 MaxQueryPaymentExceededException (com.hedera.hashgraph.sdk.MaxQueryPaymentExceededException)1 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)1 ScheduleInfoQuery (com.hedera.hashgraph.sdk.ScheduleInfoQuery)1