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);
}
}
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);
}
}
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);
}
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;
}
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);
}
Aggregations