|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +#include "AccountCreateTransaction.h" |
| 3 | +#include "AccountId.h" |
| 4 | +#include "AccountUpdateTransaction.h" |
| 5 | +#include "Client.h" |
| 6 | +#include "ContractCreateTransaction.h" |
| 7 | +#include "ContractId.h" |
| 8 | +#include "ED25519PrivateKey.h" |
| 9 | +#include "Hbar.h" |
| 10 | +#include "PublicKey.h" |
| 11 | +#include "TransactionReceipt.h" |
| 12 | +#include "TransactionResponse.h" |
| 13 | +#include "hooks/EvmHook.h" |
| 14 | +#include "hooks/EvmHookSpec.h" |
| 15 | +#include "hooks/HookCreationDetails.h" |
| 16 | +#include "hooks/HookExtensionPoint.h" |
| 17 | +#include "impl/HexConverter.h" |
| 18 | +#include "impl/Utilities.h" |
| 19 | + |
| 20 | +#include <dotenv.h> |
| 21 | +#include <filesystem> |
| 22 | +#include <fstream> |
| 23 | +#include <iostream> |
| 24 | + |
| 25 | +using namespace Hiero; |
| 26 | + |
| 27 | +static const std::string kContractBytecodeHex = |
| 28 | + "608060405234801561001057600080fd5b50600436106100365760003560e01c8063c29855781461003b578063f2fde38b14610059575b60" |
| 29 | + "0080fd5b610043610075565b60405161005091906100a1565b60405180910390f35b610073600480360381019061006e91906100ed565b61" |
| 30 | + "007b565b005b60005481565b8073ffffffffffffffffffffffffffffffffffffffff1660008054906101000a900473ffffffffffffffffff" |
| 31 | + "ffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167f8be0079c531659141344cd1fd0a4f28419497f9722" |
| 32 | + "a3daafe3b4186f6b6457e060405160405180910390a3806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff0219" |
| 33 | + "16908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b6000819050919050565b61009b81610088565b82525050" |
| 34 | + "565b60006020820190506100b66000830184610092565b92915050565b600080fd5b600073ffffffffffffffffffffffffffffffffffffff" |
| 35 | + "ff82169050919050565b60006100e7826100bc565b9050919050565b6100f7816100dc565b811461010257600080fd5b50565b6000813590" |
| 36 | + "50610114816100ee565b92915050565b6000602082840312156101305761012f6100bc565b5b600061013e84828501610105565b91505092" |
| 37 | + "91505056fea2646970667358221220"; |
| 38 | + |
| 39 | +ContractId createHookContract(Client& client, const std::shared_ptr<PrivateKey>& operatorKey) |
| 40 | +{ |
| 41 | + std::cout << "Creating hook contract..." << std::endl; |
| 42 | + const TransactionReceipt txReceipt = ContractCreateTransaction() |
| 43 | + .setAdminKey(operatorKey->getPublicKey()) |
| 44 | + .setGas(500000ULL) |
| 45 | + .setBytecode(internal::HexConverter::hexToBytes(kContractBytecodeHex)) |
| 46 | + .execute(client) |
| 47 | + .getReceipt(client); |
| 48 | + |
| 49 | + if (!txReceipt.mContractId.has_value()) |
| 50 | + { |
| 51 | + throw std::runtime_error("Failed to create hook contract!"); |
| 52 | + } |
| 53 | + |
| 54 | + std::cout << "Hook contract created with ID: " << txReceipt.mContractId.value().toString() << std::endl; |
| 55 | + return txReceipt.mContractId.value(); |
| 56 | +} |
| 57 | + |
| 58 | +std::pair<AccountId, std::shared_ptr<PrivateKey>> createAccountWithHook(Client& client, |
| 59 | + const ContractId& contractId, |
| 60 | + const std::shared_ptr<PublicKey>& adminKey) |
| 61 | +{ |
| 62 | + const std::shared_ptr<PrivateKey> accountKey = ED25519PrivateKey::generatePrivateKey(); |
| 63 | + EvmHookSpec evmHookSpec; |
| 64 | + evmHookSpec.setContractId(contractId); |
| 65 | + EvmHook evmHook; |
| 66 | + evmHook.setEvmHookSpec(evmHookSpec); |
| 67 | + HookCreationDetails hookWithId1002; |
| 68 | + hookWithId1002.setExtensionPoint(HookExtensionPoint::ACCOUNT_ALLOWANCE_HOOK); |
| 69 | + hookWithId1002.setHookId(1002LL); |
| 70 | + hookWithId1002.setEvmHook(evmHook); |
| 71 | + hookWithId1002.setAdminKey(adminKey); |
| 72 | + const TransactionReceipt txReceipt = AccountCreateTransaction() |
| 73 | + .setKeyWithoutAlias(accountKey->getPublicKey()) |
| 74 | + .setInitialBalance(Hbar(1ULL)) |
| 75 | + .addHook(hookWithId1002) |
| 76 | + .execute(client) |
| 77 | + .getReceipt(client); |
| 78 | + if (!txReceipt.mAccountId.has_value()) |
| 79 | + { |
| 80 | + throw std::runtime_error("Failed to create account with hook!"); |
| 81 | + } |
| 82 | + std::cout << "account id = " << txReceipt.mAccountId.value().toString() << std::endl; |
| 83 | + std::cout << "Successfully created account with hook!" << std::endl; |
| 84 | + return { txReceipt.mAccountId.value(), accountKey }; |
| 85 | +} |
| 86 | + |
| 87 | +void addHooksToAccount(Client& client, |
| 88 | + const AccountId& accountId, |
| 89 | + const std::shared_ptr<PrivateKey>& accountKey, |
| 90 | + const EvmHookSpec& evmHookSpec, |
| 91 | + const std::shared_ptr<PublicKey>& adminKey) |
| 92 | +{ |
| 93 | + std::cout << "\n=== Adding Hooks to Existing Account ===" << std::endl; |
| 94 | + EvmHook hook1; |
| 95 | + hook1.setEvmHookSpec(evmHookSpec); |
| 96 | + HookCreationDetails hookWithId1; |
| 97 | + hookWithId1.setExtensionPoint(HookExtensionPoint::ACCOUNT_ALLOWANCE_HOOK); |
| 98 | + hookWithId1.setHookId(1LL); |
| 99 | + hookWithId1.setEvmHook(hook1); |
| 100 | + hookWithId1.setAdminKey(adminKey); |
| 101 | + EvmHook hook2; |
| 102 | + hook2.setEvmHookSpec(evmHookSpec); |
| 103 | + HookCreationDetails hookWithId2; |
| 104 | + hookWithId2.setExtensionPoint(HookExtensionPoint::ACCOUNT_ALLOWANCE_HOOK); |
| 105 | + hookWithId2.setHookId(2LL); |
| 106 | + hookWithId2.setEvmHook(hook2); |
| 107 | + hookWithId2.setAdminKey(adminKey); |
| 108 | + try |
| 109 | + { |
| 110 | + AccountUpdateTransaction() |
| 111 | + .setAccountId(accountId) |
| 112 | + .addHookToCreate(hookWithId1) |
| 113 | + .addHookToCreate(hookWithId2) |
| 114 | + .freezeWith(&client) |
| 115 | + .sign(accountKey) |
| 116 | + .execute(client) |
| 117 | + .getReceipt(client); |
| 118 | + std::cout << "Successfully added hooks to account!" << std::endl; |
| 119 | + } |
| 120 | + catch (const std::exception& error) |
| 121 | + { |
| 122 | + std::cerr << "Failed to execute hook transaction: " << error.what() << std::endl; |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +void deleteHooksFromAccount(Client& client, const AccountId& accountId, const std::shared_ptr<PrivateKey>& accountKey) |
| 127 | +{ |
| 128 | + std::cout << "\n=== Deleting Hooks from Account ===" << std::endl; |
| 129 | + try |
| 130 | + { |
| 131 | + AccountUpdateTransaction() |
| 132 | + .setAccountId(accountId) |
| 133 | + .addHookToDelete(1LL) |
| 134 | + .addHookToDelete(2LL) |
| 135 | + .freezeWith(&client) |
| 136 | + .sign(accountKey) |
| 137 | + .execute(client) |
| 138 | + .getReceipt(client); |
| 139 | + std::cout << "Successfully deleted hooks (IDs: 1, 2)" << std::endl; |
| 140 | + } |
| 141 | + catch (const std::exception& error) |
| 142 | + { |
| 143 | + std::cerr << "Failed to execute hook deletion: " << error.what() << std::endl; |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +void run(Client& client, const std::shared_ptr<PrivateKey>& operatorKey) |
| 148 | +{ |
| 149 | + std::cout << "Account Hooks Example Start!" << std::endl; |
| 150 | + const ContractId contractId = createHookContract(client, operatorKey); |
| 151 | + std::cout << "\n=== Creating Account with Hooks ===" << std::endl; |
| 152 | + const std::shared_ptr<PublicKey> adminKey = client.getOperatorPublicKey(); |
| 153 | + const auto [accountId, accountKey] = createAccountWithHook(client, contractId, adminKey); |
| 154 | + EvmHookSpec evmHookSpec; |
| 155 | + evmHookSpec.setContractId(contractId); |
| 156 | + addHooksToAccount(client, accountId, accountKey, evmHookSpec, adminKey); |
| 157 | + deleteHooksFromAccount(client, accountId, accountKey); |
| 158 | + std::cout << "Account Hooks Example Complete!" << std::endl; |
| 159 | +} |
| 160 | + |
| 161 | +int main(int argc, char** argv) |
| 162 | +{ |
| 163 | + dotenv::init(); |
| 164 | + if (std::getenv("OPERATOR_ID") == nullptr || std::getenv("OPERATOR_KEY") == nullptr || |
| 165 | + std::getenv("NETWORK_NAME") == nullptr) |
| 166 | + { |
| 167 | + std::cerr << "Environment variables OPERATOR_ID, NETWORK_NAME, and OPERATOR_KEY are required." << std::endl; |
| 168 | + return 1; |
| 169 | + } |
| 170 | + Client client = Client::forName(std::getenv("NETWORK_NAME")); |
| 171 | + client.setOperator(AccountId::fromString(std::getenv("OPERATOR_ID")), |
| 172 | + ED25519PrivateKey::fromString(std::getenv("OPERATOR_KEY"))); |
| 173 | + try |
| 174 | + { |
| 175 | + run(client, ED25519PrivateKey::fromString(std::getenv("OPERATOR_KEY"))); |
| 176 | + } |
| 177 | + catch (const std::exception& error) |
| 178 | + { |
| 179 | + std::cerr << "Error occurred: " << error.what() << std::endl; |
| 180 | + return 1; |
| 181 | + } |
| 182 | + client.close(); |
| 183 | + return 0; |
| 184 | +} |
0 commit comments