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