use of com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction in project hedera-sdk-java by hashgraph.
the class ConsensusPubSubWithSubmitKeyExample method publishMessagesToTopic.
/**
* Publish a list of messages to a topic, signing each transaction with the topic's submitKey.
*/
private void publishMessagesToTopic() throws TimeoutException, InterruptedException, PrecheckStatusException, ReceiptStatusException {
Random r = new Random();
for (int i = 0; i < messagesToPublish; i++) {
String message = "random message " + r.nextLong();
System.out.println("Publishing message: " + message);
new TopicMessageSubmitTransaction().setTopicId(topicId).setMessage(message).freezeWith(client).sign(submitKey).execute(client).transactionId.getReceipt(client);
Thread.sleep(millisBetweenMessages);
}
Thread.sleep(10000);
}
use of com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction 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.TopicMessageSubmitTransaction 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.TopicMessageSubmitTransaction in project hedera-mirror-node by hashgraph.
the class TopicClient method publishMessageToTopic.
public TransactionId publishMessageToTopic(TopicId topicId, byte[] message, KeyList submitKeys) {
TopicMessageSubmitTransaction consensusMessageSubmitTransaction = new TopicMessageSubmitTransaction().setTopicId(topicId).setMessage(message).setTransactionMemo(getMemo("Publish topic message"));
TransactionId transactionId = executeTransaction(consensusMessageSubmitTransaction, submitKeys);
TransactionRecord transactionRecord = getTransactionRecord(transactionId);
// get only the 1st sequence number
if (recordPublishInstants.size() == 0) {
recordPublishInstants.put(0L, transactionRecord.consensusTimestamp);
}
if (log.isTraceEnabled()) {
log.trace("Published message : '{}' to topicId : {} with consensusTimestamp: {}", new String(message, StandardCharsets.UTF_8), topicId, transactionRecord.consensusTimestamp);
}
return transactionId;
}
use of com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction in project hedera-mirror-node by hashgraph.
the class ConsensusSubmitMessageTransactionSupplierTest method createWithCustomMessageSize.
@Test
void createWithCustomMessageSize() {
ConsensusSubmitMessageTransactionSupplier consensusSubmitMessageTransactionSupplier = new ConsensusSubmitMessageTransactionSupplier();
consensusSubmitMessageTransactionSupplier.setMaxTransactionFee(1);
consensusSubmitMessageTransactionSupplier.setMessageSize(14);
consensusSubmitMessageTransactionSupplier.setTopicId(TOPIC_ID.toString());
TopicMessageSubmitTransaction actual = consensusSubmitMessageTransactionSupplier.get();
assertThat(actual).returns(ONE_TINYBAR, TopicMessageSubmitTransaction::getMaxTransactionFee).returns(TOPIC_ID, TopicMessageSubmitTransaction::getTopicId).satisfies(a -> assertThat(a.getMessage()).isNotNull()).extracting(a -> a.getMessage().toStringUtf8(), STRING).hasSize(14);
}
Aggregations