diff --git a/sdk/examples/CMakeLists.txt b/sdk/examples/CMakeLists.txt index 2c61bcb13..5766b7f25 100644 --- a/sdk/examples/CMakeLists.txt +++ b/sdk/examples/CMakeLists.txt @@ -5,6 +5,7 @@ set(ACCOUNT_CREATION_WAYS_EXAMPLE_NAME ${PROJECT_NAME}-account-creation-ways-exa set(AUTO_CREATE_ACCOUNT_TRANSFER_TRANSACTION_EXAMPLE_NAME ${PROJECT_NAME}-auto-create-account-transfer-transaction-example) set(CONSENSUS_PUB_SUB_EXAMPLE_NAME ${PROJECT_NAME}-consensus-pub-sub-example) set(CONSENSUS_PUB_SUB_CHUNKED_EXAMPLE_NAME ${PROJECT_NAME}-consensus-pub-sub-chunked-example) +set(CONSENSUS_PUB_SUB_WITH_SUBMIT_KEY_EXAMPLE_NAME ${PROJECT_NAME}-consensus-pub-sub-with-submit-key-example) set(CREATE_ACCOUNT_EXAMPLE_NAME ${PROJECT_NAME}-create-account-example) set(CREATE_SIMPLE_CONTRACT_EXAMPLE_NAME ${PROJECT_NAME}-create-simple-contract-example) set(CREATE_STATEFUL_CONTRACT_EXAMPLE_NAME ${PROJECT_NAME}-create-stateful-contract-example) @@ -37,6 +38,7 @@ add_executable(${ACCOUNT_CREATION_WAYS_EXAMPLE_NAME} AccountCreationWaysExample. add_executable(${AUTO_CREATE_ACCOUNT_TRANSFER_TRANSACTION_EXAMPLE_NAME} AutoCreateAccountTransferTransactionExample.cc) add_executable(${CONSENSUS_PUB_SUB_EXAMPLE_NAME} ConsensusPubSubExample.cc) add_executable(${CONSENSUS_PUB_SUB_CHUNKED_EXAMPLE_NAME} ConsensusPubSubChunkedExample.cc) +add_executable(${CONSENSUS_PUB_SUB_WITH_SUBMIT_KEY_EXAMPLE_NAME} ConsensusPubSubWithSubmitKeyExample.cc) add_executable(${CREATE_ACCOUNT_EXAMPLE_NAME} CreateAccountExample.cc) add_executable(${CREATE_SIMPLE_CONTRACT_EXAMPLE_NAME} CreateSimpleContractExample.cc) add_executable(${CREATE_STATEFUL_CONTRACT_EXAMPLE_NAME} CreateStatefulContractExample.cc) @@ -87,6 +89,7 @@ target_link_libraries(${ACCOUNT_CREATION_WAYS_EXAMPLE_NAME} PUBLIC ${PROJECT_NAM target_link_libraries(${AUTO_CREATE_ACCOUNT_TRANSFER_TRANSACTION_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME}) target_link_libraries(${CONSENSUS_PUB_SUB_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME}) target_link_libraries(${CONSENSUS_PUB_SUB_CHUNKED_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME}) +target_link_libraries(${CONSENSUS_PUB_SUB_WITH_SUBMIT_KEY_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME}) target_link_libraries(${CREATE_ACCOUNT_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME}) target_link_libraries(${CREATE_SIMPLE_CONTRACT_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME}) target_link_libraries(${CREATE_STATEFUL_CONTRACT_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME}) @@ -122,6 +125,7 @@ install(TARGETS ${AUTO_CREATE_ACCOUNT_TRANSFER_TRANSACTION_EXAMPLE_NAME} ${CONSENSUS_PUB_SUB_EXAMPLE_NAME} ${CONSENSUS_PUB_SUB_CHUNKED_EXAMPLE_NAME} + ${CONSENSUS_PUB_SUB_WITH_SUBMIT_KEY_EXAMPLE_NAME} ${CREATE_ACCOUNT_EXAMPLE_NAME} ${CREATE_SIMPLE_CONTRACT_EXAMPLE_NAME} ${CREATE_STATEFUL_CONTRACT_EXAMPLE_NAME} diff --git a/sdk/examples/ConsensusPubSubWithSubmitKeyExample.cc b/sdk/examples/ConsensusPubSubWithSubmitKeyExample.cc new file mode 100644 index 000000000..dd382a09c --- /dev/null +++ b/sdk/examples/ConsensusPubSubWithSubmitKeyExample.cc @@ -0,0 +1,89 @@ +/*- + * + * Hedera C++ SDK + * + * Copyright (C) 2020 - 2023 Hedera Hashgraph, LLC + * + * Licensed under the Apache License, Version 2.0 (the "License") + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#include "Client.h" +#include "ED25519PrivateKey.h" +#include "SubscriptionHandle.h" +#include "TopicCreateTransaction.h" +#include "TopicId.h" +#include "TopicMessage.h" +#include "TopicMessageQuery.h" +#include "TopicMessageSubmitTransaction.h" +#include "TransactionReceipt.h" +#include "TransactionResponse.h" +#include "impl/Utilities.h" + +#include +#include +#include + +using namespace Hedera; + +int main(int argc, char** argv) +{ + if (argc < 3) + { + std::cout << "Please input account ID and private key" << std::endl; + return 1; + } + + // Get a client for the Hedera testnet, and set 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 client = Client::forTestnet(); + client.setOperator(AccountId::fromString(argv[1]), ED25519PrivateKey::fromString(argv[2])); + + // Generate a submit key. + const std::shared_ptr submitKey = ED25519PrivateKey::generatePrivateKey(); + + // Create a topic with the submit key. + const TopicId topicId = + TopicCreateTransaction().setSubmitKey(submitKey).execute(client).getReceipt(client).mTopicId.value(); + std::cout << "Created topic " << topicId.toString() << " with submit key " << submitKey->toStringRaw() << std::endl; + + // Wait for topic to propagate to the mirror nodes. + std::cout << "Waiting to propagate to mirror nodes"; + for (int i = 0; i < 5; ++i) + { + std::this_thread::sleep_for(std::chrono::seconds(1)); + std::cout << '.'; + } + std::cout << std::endl; + + // Subscribe to the topic. + TopicMessageQuery query = TopicMessageQuery().setTopicId(topicId); + SubscriptionHandle handle = query.subscribe( + client, + [](const TopicMessage& message) + { std::cout << "Received message: " << internal::Utilities::byteVectorToString(message.mContents) << std::endl; }); + + for (int i = 0; true; ++i) + { + const TransactionReceipt txReceipt = TopicMessageSubmitTransaction() + .setTopicId(topicId) + .setMessage("Hello from HCS " + std::to_string(i)) + .freezeWith(&client) + // Due to the topic having a submit key, the submit key must sign. + .sign(submitKey) + .execute(client) + .getReceipt(client); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + } + + return 0; +}