Search in sources :

Example 1 with TopicId

use of com.hedera.hashgraph.sdk.TopicId 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 2 with TopicId

use of com.hedera.hashgraph.sdk.TopicId 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)

Example 3 with TopicId

use of com.hedera.hashgraph.sdk.TopicId in project hedera-mirror-node by hashgraph.

the class ScheduleFeature method createNewHCSSchedule.

@Given("I successfully schedule a topic message submit with {string}'s submit key")
public void createNewHCSSchedule(String accountName) {
    expectedSignersCount = 1;
    currentSignersCount = 0 + signatoryCountOffset;
    ExpandedAccountId submitAdmin = accountClient.getAccount(AccountClient.AccountNameEnum.valueOf(accountName));
    // create topic w submit key
    log.debug("Create new topic with {}'s submit key", accountName);
    networkTransactionResponse = topicClient.createTopic(scheduleClient.getSdkClient().getExpandedOperatorAccountId(), submitAdmin.getPublicKey());
    assertNotNull(networkTransactionResponse.getTransactionId());
    assertNotNull(networkTransactionResponse.getReceipt());
    TopicId topicId = networkTransactionResponse.getReceipt().topicId;
    assertNotNull(topicId);
    scheduledTransaction = topicClient.getTopicMessageSubmitTransaction(topicId, "scheduled hcs message".getBytes(StandardCharsets.UTF_8));
    createNewSchedule(scheduledTransaction, null);
}
Also used : ExpandedAccountId(com.hedera.mirror.test.e2e.acceptance.props.ExpandedAccountId) TopicId(com.hedera.hashgraph.sdk.TopicId) Given(io.cucumber.java.en.Given)

Example 4 with TopicId

use of com.hedera.hashgraph.sdk.TopicId in project hedera-mirror-node by hashgraph.

the class TopicClient method createTopic.

public NetworkTransactionResponse createTopic(ExpandedAccountId adminAccount, PublicKey submitKey) {
    String memo = getMemo("Create Topic");
    TopicCreateTransaction consensusTopicCreateTransaction = new TopicCreateTransaction().setAdminKey(adminAccount.getPublicKey()).setAutoRenewAccountId(sdkClient.getExpandedOperatorAccountId().getAccountId()).setMaxTransactionFee(sdkClient.getMaxTransactionFee()).setTopicMemo(memo).setTransactionMemo(memo).setAutoRenewPeriod(// INSUFFICIENT_TX_FEE, also unsupported
    autoRenewPeriod);
    if (submitKey != null) {
        consensusTopicCreateTransaction.setSubmitKey(submitKey);
    }
    NetworkTransactionResponse networkTransactionResponse = executeTransactionAndRetrieveReceipt(consensusTopicCreateTransaction, KeyList.of(adminAccount.getPrivateKey()));
    TopicId topicId = networkTransactionResponse.getReceipt().topicId;
    log.debug("Created new topic {}", topicId);
    return networkTransactionResponse;
}
Also used : TopicCreateTransaction(com.hedera.hashgraph.sdk.TopicCreateTransaction) NetworkTransactionResponse(com.hedera.mirror.test.e2e.acceptance.response.NetworkTransactionResponse) TopicId(com.hedera.hashgraph.sdk.TopicId)

Example 5 with TopicId

use of com.hedera.hashgraph.sdk.TopicId in project hedera-mirror-node by hashgraph.

the class TopicFeature method createNewOpenTopic.

@Given("I successfully create a new open topic")
public void createNewOpenTopic() {
    testInstantReference = Instant.now();
    NetworkTransactionResponse networkTransactionResponse = topicClient.createTopic(topicClient.getSdkClient().getExpandedOperatorAccountId(), null);
    assertNotNull(networkTransactionResponse.getReceipt());
    TopicId topicId = networkTransactionResponse.getReceipt().topicId;
    assertNotNull(topicId);
    consensusTopicId = topicId;
    topicMessageQuery = new TopicMessageQuery().setTopicId(consensusTopicId).setStartTime(Instant.EPOCH);
    log.debug("Set TopicMessageQuery with topic: {}, startTime: {}", consensusTopicId, Instant.EPOCH);
}
Also used : NetworkTransactionResponse(com.hedera.mirror.test.e2e.acceptance.response.NetworkTransactionResponse) TopicId(com.hedera.hashgraph.sdk.TopicId) TopicMessageQuery(com.hedera.hashgraph.sdk.TopicMessageQuery) Given(io.cucumber.java.en.Given)

Aggregations

TopicId (com.hedera.hashgraph.sdk.TopicId)7 TopicMessageQuery (com.hedera.hashgraph.sdk.TopicMessageQuery)5 Given (io.cucumber.java.en.Given)4 TopicCreateTransaction (com.hedera.hashgraph.sdk.TopicCreateTransaction)3 NetworkTransactionResponse (com.hedera.mirror.test.e2e.acceptance.response.NetworkTransactionResponse)3 Client (com.hedera.hashgraph.sdk.Client)2 TopicMessageSubmitTransaction (com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction)2 Var (com.google.errorprone.annotations.Var)1 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)1 PublicKey (com.hedera.hashgraph.sdk.PublicKey)1 TransactionReceipt (com.hedera.hashgraph.sdk.TransactionReceipt)1 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)1 ExpandedAccountId (com.hedera.mirror.test.e2e.acceptance.props.ExpandedAccountId)1