|
| 1 | +/* |
| 2 | + * |
| 3 | + * Hedera Swift SDK |
| 4 | + * |
| 5 | + * Copyright (C) 2022 - 2025 Hedera Hashgraph, LLC |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +import Foundation |
| 22 | +import Hiero |
| 23 | +import SwiftDotenv |
| 24 | + |
| 25 | +@main |
| 26 | +internal enum Program { |
| 27 | + internal static func main() async throws { |
| 28 | + print("Starting Example") |
| 29 | + let env = try Dotenv.load() |
| 30 | + let client = try Client.forName(env.networkName) |
| 31 | + |
| 32 | + client.setOperator(env.operatorAccountId, env.operatorKey) |
| 33 | + // Creates a Hedera account with an alias using an ECDSA key. |
| 34 | + try await createAccountWithAlias(client) |
| 35 | + // Creates a Hedera account with both ED25519 and ECDSA keys. |
| 36 | + try await createAccountWithBothKeys(client) |
| 37 | + // Creates a Hedera account without an alias. |
| 38 | + try await createAccountWithoutAlias(client) |
| 39 | + |
| 40 | + print("Example finished") |
| 41 | + } |
| 42 | + |
| 43 | + internal static func createAccountWithAlias(_ client: Client) async throws { |
| 44 | + print("Creating account with alias") |
| 45 | + |
| 46 | + // Step 1: |
| 47 | + // Create a new ECDSA key |
| 48 | + let privateKey = PrivateKey.generateEcdsa() |
| 49 | + |
| 50 | + // Step 2: |
| 51 | + // Extract the ECDSA public key and generate EVM address |
| 52 | + let publicKey = privateKey.publicKey |
| 53 | + let evmAddress = publicKey.toEvmAddress()! |
| 54 | + |
| 55 | + // Step 3: |
| 56 | + // Create the account with an alias |
| 57 | + let accountId = try await AccountCreateTransaction() |
| 58 | + .keyWithAlias(privateKey) |
| 59 | + .freezeWith(client) |
| 60 | + .sign(privateKey) |
| 61 | + .execute(client) |
| 62 | + .getReceipt(client) |
| 63 | + .accountId! |
| 64 | + |
| 65 | + // Step 4: |
| 66 | + // Query account info |
| 67 | + let info = try await AccountInfoQuery() |
| 68 | + .accountId(accountId) |
| 69 | + .execute(client) |
| 70 | + |
| 71 | + print("Initial EVM address: \(evmAddress) is the same as \(info.contractAccountId)") |
| 72 | + } |
| 73 | + |
| 74 | + // Creates an account with an alias and a key |
| 75 | + internal static func createAccountWithBothKeys(_ client: Client) async throws { |
| 76 | + print("Creating account with an alias and a key") |
| 77 | + |
| 78 | + // Step 1: |
| 79 | + // Create a new ECDSA key and Ed25519 key |
| 80 | + let ed25519Key = PrivateKey.generateEd25519() |
| 81 | + let ecdsaKey = PrivateKey.generateEcdsa() |
| 82 | + |
| 83 | + // Step 2: |
| 84 | + // Extract the ECDSA public key and generate EVM address |
| 85 | + let evmAddress = ecdsaKey.publicKey.toEvmAddress()! |
| 86 | + |
| 87 | + // Step 3: |
| 88 | + // Create the account with both keys. Transaction needs to be signed |
| 89 | + // by both keys. |
| 90 | + let accountId = try await AccountCreateTransaction() |
| 91 | + .keyWithAlias(.single(ed25519Key.publicKey), ecdsaKey) |
| 92 | + .freezeWith(client) |
| 93 | + .sign(ed25519Key) |
| 94 | + .sign(ecdsaKey) |
| 95 | + .execute(client) |
| 96 | + .getReceipt(client) |
| 97 | + .accountId! |
| 98 | + |
| 99 | + // Step 4: |
| 100 | + // Query account info |
| 101 | + let info = try await AccountInfoQuery() |
| 102 | + .accountId(accountId) |
| 103 | + .execute(client) |
| 104 | + |
| 105 | + print("Account's key: \(info.key) is the same as \(ed25519Key.publicKey)") |
| 106 | + print("Initial EVM address: \(evmAddress) is the same as \(info.contractAccountId)") |
| 107 | + } |
| 108 | + |
| 109 | + // Creates an account without an alias. |
| 110 | + internal static func createAccountWithoutAlias(_ client: Client) async throws { |
| 111 | + print("Creating account without an alias") |
| 112 | + |
| 113 | + // Step 1: |
| 114 | + // Create a new ECDSA key |
| 115 | + let privateKey = PrivateKey.generateEcdsa() |
| 116 | + |
| 117 | + // Step 2: |
| 118 | + // Create an account without an alias. |
| 119 | + let accountId = try await AccountCreateTransaction() |
| 120 | + .keyWithoutAlias(.single(privateKey.publicKey)) |
| 121 | + .freezeWith(client) |
| 122 | + .sign(privateKey) |
| 123 | + .execute(client) |
| 124 | + .getReceipt(client) |
| 125 | + .accountId! |
| 126 | + |
| 127 | + // Step 3: |
| 128 | + // Query account info |
| 129 | + let info = try await AccountInfoQuery() |
| 130 | + .accountId(accountId) |
| 131 | + .execute(client) |
| 132 | + |
| 133 | + let isZeroAddress = isZeroAddress(try info.contractAccountId.bytes) |
| 134 | + |
| 135 | + print("Account's key: \(info.key) is the same as \(privateKey.publicKey)") |
| 136 | + print("Account has no alias: \(isZeroAddress)") |
| 137 | + } |
| 138 | + |
| 139 | + // Checks if an address is a zero address (all first 12 bytes are zero) |
| 140 | + internal static func isZeroAddress(_ address: [UInt8]) -> Bool { |
| 141 | + // Check first 12 bytes are all zero |
| 142 | + for byte in address[..<12] where byte != 0 { |
| 143 | + return false |
| 144 | + } |
| 145 | + return true |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +extension Environment { |
| 150 | + /// Account ID for the operator to use in this example. |
| 151 | + internal var operatorAccountId: AccountId { |
| 152 | + AccountId(self["OPERATOR_ID"]!.stringValue)! |
| 153 | + } |
| 154 | + |
| 155 | + /// Private key for the operator to use in this example. |
| 156 | + internal var operatorKey: PrivateKey { |
| 157 | + PrivateKey(self["OPERATOR_KEY"]!.stringValue)! |
| 158 | + } |
| 159 | + |
| 160 | + /// The name of the hedera network this example should be ran against. |
| 161 | + /// |
| 162 | + /// Testnet by default. |
| 163 | + internal var networkName: String { |
| 164 | + self["HEDERA_NETWORK"]?.stringValue ?? "testnet" |
| 165 | + } |
| 166 | +} |
0 commit comments