|
| 1 | +""" |
| 2 | +This example demonstrates the admin key privileges for token management using Hiero SDK Python. |
| 3 | +
|
| 4 | +It shows: |
| 5 | +1. Creating a token with an admin key |
| 6 | +2. Demonstrating admin-only operations like updating token memo and deleting the token |
| 7 | +3. Attempting to add a supply key (which fails because admin key cannot add new keys) |
| 8 | +4. Updating existing keys using admin key authorization |
| 9 | +5. Verifying operations using TokenInfoQuery |
| 10 | +
|
| 11 | +Required environment variables: |
| 12 | +- OPERATOR_ID, OPERATOR_KEY |
| 13 | +
|
| 14 | +Usage: |
| 15 | +uv run examples/token_create_transaction_admin_key.py |
| 16 | +python examples/token_create_transaction_admin_key.py |
| 17 | +""" |
| 18 | + |
| 19 | +import os |
| 20 | +import sys |
| 21 | +from dotenv import load_dotenv |
| 22 | + |
| 23 | +from hiero_sdk_python import ( |
| 24 | + Client, |
| 25 | + AccountId, |
| 26 | + PrivateKey, |
| 27 | + Network, |
| 28 | + TokenCreateTransaction, |
| 29 | + TokenUpdateTransaction, |
| 30 | + TokenDeleteTransaction, |
| 31 | + TokenInfoQuery, |
| 32 | +) |
| 33 | + |
| 34 | +from hiero_sdk_python.response_code import ResponseCode |
| 35 | +from hiero_sdk_python.tokens.token_type import TokenType |
| 36 | +from hiero_sdk_python.tokens.supply_type import SupplyType |
| 37 | + |
| 38 | +load_dotenv() |
| 39 | +network_name = os.getenv('NETWORK', 'testnet').lower() |
| 40 | + |
| 41 | + |
| 42 | +def setup_client(): |
| 43 | + """Initialize and set up the client with operator account""" |
| 44 | + network = Network(network_name) |
| 45 | + print(f"Connecting to Hedera {network_name} network!") |
| 46 | + client = Client(network) |
| 47 | + |
| 48 | + try: |
| 49 | + operator_id = AccountId.from_string(os.getenv('OPERATOR_ID', '')) |
| 50 | + operator_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY', '')) |
| 51 | + client.set_operator(operator_id, operator_key) |
| 52 | + print(f"Client set up with operator id {client.operator_account_id}") |
| 53 | + return client, operator_id, operator_key |
| 54 | + |
| 55 | + except (TypeError, ValueError): |
| 56 | + print("Error: Please check OPERATOR_ID and OPERATOR_KEY in your .env file.") |
| 57 | + sys.exit(1) |
| 58 | + |
| 59 | + |
| 60 | +def generate_admin_key(): |
| 61 | + """Generate a new admin key for the token.""" |
| 62 | + print("\nGenerating a new admin key for the token...") |
| 63 | + admin_key = PrivateKey.generate_ed25519() |
| 64 | + print("Admin key generated successfully.") |
| 65 | + return admin_key |
| 66 | + |
| 67 | + |
| 68 | +def create_token_with_admin_key(client, operator_id, operator_key, admin_key): |
| 69 | + """ |
| 70 | + Create a fungible token with only an admin key. |
| 71 | + The admin key grants privileges to update token properties and delete the token. |
| 72 | + """ |
| 73 | + print("\nCreating a fungible token with admin key...") |
| 74 | + |
| 75 | + transaction = ( |
| 76 | + TokenCreateTransaction() |
| 77 | + .set_token_name("Admin Key Demo Token") |
| 78 | + .set_token_symbol("AKDT") |
| 79 | + .set_decimals(2) |
| 80 | + .set_initial_supply(1000) |
| 81 | + .set_treasury_account_id(operator_id) |
| 82 | + .set_token_type(TokenType.FUNGIBLE_COMMON) |
| 83 | + .set_supply_type(SupplyType.INFINITE) |
| 84 | + .set_admin_key(admin_key) # Only admin key is set |
| 85 | + .freeze_with(client) |
| 86 | + ) |
| 87 | + |
| 88 | + # Sign with operator (treasury) and admin key |
| 89 | + transaction.sign(operator_key) |
| 90 | + transaction.sign(admin_key) |
| 91 | + |
| 92 | + receipt = transaction.execute(client) |
| 93 | + if receipt.status != ResponseCode.SUCCESS: |
| 94 | + print(f"Token creation failed with status: {ResponseCode(receipt.status).name}") |
| 95 | + sys.exit(1) |
| 96 | + |
| 97 | + token_id = receipt.token_id |
| 98 | + print(f"✅ Token created successfully with ID: {token_id}") |
| 99 | + return token_id |
| 100 | + |
| 101 | + |
| 102 | +def demonstrate_admin_update_memo(client, token_id, admin_key): |
| 103 | + """Demonstrate updating token memo using admin key.""" |
| 104 | + print(f"\nUpdating token memo for {token_id} using admin key...") |
| 105 | + |
| 106 | + transaction = ( |
| 107 | + TokenUpdateTransaction() |
| 108 | + .set_token_id(token_id) |
| 109 | + .set_token_memo("Updated by admin key") |
| 110 | + .freeze_with(client) |
| 111 | + .sign(admin_key) # Only admin key signature needed for updates |
| 112 | + ) |
| 113 | + |
| 114 | + receipt = transaction.execute(client) |
| 115 | + if receipt.status != ResponseCode.SUCCESS: |
| 116 | + print(f"Token update failed with status: {ResponseCode(receipt.status).name}") |
| 117 | + return False |
| 118 | + |
| 119 | + print("✅ Token memo updated successfully using admin key") |
| 120 | + return True |
| 121 | + |
| 122 | + |
| 123 | +def demonstrate_failed_supply_key_addition(client, token_id, admin_key): |
| 124 | + """ |
| 125 | + Demonstrate that admin key cannot add a new supply key if none was present during creation. |
| 126 | + This shows the limitation of admin key privileges. |
| 127 | + """ |
| 128 | + print(f"\nAttempting to add supply key to {token_id} (this should fail)...") |
| 129 | + |
| 130 | + new_supply_key = PrivateKey.generate_ed25519() |
| 131 | + |
| 132 | + transaction = ( |
| 133 | + TokenUpdateTransaction() |
| 134 | + .set_token_id(token_id) |
| 135 | + .set_supply_key(new_supply_key) # Trying to add supply key that wasn't present |
| 136 | + .freeze_with(client) |
| 137 | + .sign(admin_key) # Admin key cannot authorize adding new keys |
| 138 | + ) |
| 139 | + |
| 140 | + try: |
| 141 | + receipt = transaction.execute(client) |
| 142 | + if receipt.status != ResponseCode.SUCCESS: |
| 143 | + print(f"❌ As expected, adding supply key failed: {ResponseCode(receipt.status).name}") |
| 144 | + print(" Admin key cannot authorize adding keys that were not present during token creation.") |
| 145 | + return True # Expected failure |
| 146 | + else: |
| 147 | + print("⚠️ Unexpectedly succeeded - this shouldn't happen") |
| 148 | + return False |
| 149 | + except Exception as e: |
| 150 | + print(f"❌ As expected, adding supply key failed with exception: {e}") |
| 151 | + return True |
| 152 | + |
| 153 | + |
| 154 | +def demonstrate_admin_key_update(client, token_id, admin_key, operator_key): |
| 155 | + """ |
| 156 | + Demonstrate updating the admin key itself using current admin key authorization. |
| 157 | + This shows admin key can change itself. |
| 158 | + """ |
| 159 | + print(f"\nUpdating admin key for {token_id} to operator key...") |
| 160 | + |
| 161 | + transaction = ( |
| 162 | + TokenUpdateTransaction() |
| 163 | + .set_token_id(token_id) |
| 164 | + .set_admin_key(operator_key) # Change admin key to operator key |
| 165 | + .freeze_with(client) |
| 166 | + .sign(admin_key) # Current admin key authorizes the change |
| 167 | + .sign(operator_key) # New admin key must also sign |
| 168 | + ) |
| 169 | + |
| 170 | + receipt = transaction.execute(client) |
| 171 | + if receipt.status != ResponseCode.SUCCESS: |
| 172 | + print(f"Admin key update failed with status: {ResponseCode(receipt.status).name}") |
| 173 | + return False |
| 174 | + |
| 175 | + print("✅ Admin key updated successfully") |
| 176 | + return True |
| 177 | + |
| 178 | + |
| 179 | +def demonstrate_token_deletion(client, token_id, operator_key): |
| 180 | + """ |
| 181 | + Demonstrate deleting the token using admin key (now operator key). |
| 182 | + Note: Since we updated admin key to operator_key, we use that. |
| 183 | + """ |
| 184 | + print(f"\nDeleting token {token_id} using admin key...") |
| 185 | + |
| 186 | + transaction = ( |
| 187 | + TokenDeleteTransaction() |
| 188 | + .set_token_id(token_id) |
| 189 | + .freeze_with(client) |
| 190 | + .sign(operator_key) # Admin key (now operator_key) signs the deletion |
| 191 | + ) |
| 192 | + |
| 193 | + receipt = transaction.execute(client) |
| 194 | + if receipt.status != ResponseCode.SUCCESS: |
| 195 | + print(f"Token deletion failed with status: {ResponseCode(receipt.status).name}") |
| 196 | + return False |
| 197 | + |
| 198 | + print("✅ Token deleted successfully using admin key") |
| 199 | + return True |
| 200 | + |
| 201 | + |
| 202 | +def get_token_info(client, token_id): |
| 203 | + """Query and display token information.""" |
| 204 | + try: |
| 205 | + info = ( |
| 206 | + TokenInfoQuery() |
| 207 | + .set_token_id(token_id) |
| 208 | + .execute(client) |
| 209 | + ) |
| 210 | + print(f"\nToken Info for {token_id}:") |
| 211 | + print(f" Name: {info.name}") |
| 212 | + print(f" Symbol: {info.symbol}") |
| 213 | + print(f" Memo: {info.memo}") |
| 214 | + print(f" Admin Key: {info.admin_key}") |
| 215 | + print(f" Supply Key: {info.supply_key}") |
| 216 | + return info |
| 217 | + except Exception as e: |
| 218 | + print(f"Failed to get token info: {e}") |
| 219 | + return None |
| 220 | + |
| 221 | + |
| 222 | +def main(): |
| 223 | + """ |
| 224 | + Main function demonstrating admin key capabilities: |
| 225 | + 1. Create token with admin key |
| 226 | + 2. Update token memo (admin privilege) |
| 227 | + 3. Attempt to add supply key (should fail) |
| 228 | + 4. Update admin key itself |
| 229 | + 5. Delete token (admin privilege) |
| 230 | + """ |
| 231 | + client, operator_id, operator_key = setup_client() |
| 232 | + admin_key = generate_admin_key() |
| 233 | + |
| 234 | + # Step 1: Create token with admin key |
| 235 | + token_id = create_token_with_admin_key(client, operator_id, operator_key, admin_key) |
| 236 | + |
| 237 | + # Get initial token info |
| 238 | + get_token_info(client, token_id) |
| 239 | + |
| 240 | + # Step 2: Demonstrate admin-only update |
| 241 | + if demonstrate_admin_update_memo(client, token_id, admin_key): |
| 242 | + get_token_info(client, token_id) # Verify the update |
| 243 | + |
| 244 | + # Step 3: Show limitation - cannot add new keys |
| 245 | + demonstrate_failed_supply_key_addition(client, token_id, admin_key) |
| 246 | + |
| 247 | + # Step 4: Update admin key itself |
| 248 | + if demonstrate_admin_key_update(client, token_id, admin_key, operator_key): |
| 249 | + get_token_info(client, token_id) # Verify admin key changed |
| 250 | + |
| 251 | + # Step 5: Delete token using admin privilege |
| 252 | + demonstrate_token_deletion(client, token_id, operator_key) |
| 253 | + |
| 254 | + print("\n🎉 Admin key demonstration completed!") |
| 255 | + |
| 256 | + |
| 257 | +if __name__ == "__main__": |
| 258 | + main() |
0 commit comments