Skip to content

Commit f6a5770

Browse files
authored
Streamline Key and Alias specifications for AccountCreateTransaction
1 parent 13580ce commit f6a5770

File tree

36 files changed

+510
-75
lines changed

36 files changed

+510
-75
lines changed

Examples/AccountAllowance/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private func createAccount(_ client: Client, _ name: StaticString) async throws
5252
let key = PrivateKey.generateEd25519()
5353

5454
let reciept = try await AccountCreateTransaction()
55-
.key(.single(key.publicKey))
55+
.keyWithoutAlias(.single(key.publicKey))
5656
.initialBalance(5)
5757
.accountMemo("[sdk::swift::accountAllowanceExample::\(name)]")
5858
.execute(client)

Examples/AddNftAllowance/main.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ internal enum Program {
7777
let receiverKey = PrivateKey.generateEd25519()
7878

7979
let spenderAccountId = try await AccountCreateTransaction()
80-
.key(Key.single(spenderKey.publicKey))
80+
.keyWithoutAlias(Key.single(spenderKey.publicKey))
8181
.initialBalance(Hbar(2))
8282
.execute(client)
8383
.getReceipt(client)
8484
.accountId!
8585

8686
let receiverAccountId = try await AccountCreateTransaction()
87-
.key(Key.single(receiverKey.publicKey))
87+
.keyWithoutAlias(Key.single(receiverKey.publicKey))
8888
.initialBalance(Hbar(2))
8989
.execute(client)
9090
.getReceipt(client)

Examples/CreateAccount/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ internal enum Program {
3535
print("public key = \(newKey.publicKey)")
3636

3737
let response = try await AccountCreateTransaction()
38-
.key(.single(newKey.publicKey))
38+
.keyWithoutAlias(.single(newKey.publicKey))
3939
.initialBalance(5)
4040
.execute(client)
4141

Examples/CreateAccountThresholdKey/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ internal enum Program {
5151
)
5252

5353
let transactionResponse = try await AccountCreateTransaction()
54-
.key(.keyList(transactionKey))
54+
.keyWithoutAlias(.keyList(transactionKey))
5555
.initialBalance(Hbar(10))
5656
.execute(client)
5757

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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+
}

Examples/InitializeClientWithMirrorNetwork/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ internal enum Program {
4545
* Step 2: Create an account
4646
*/
4747
let aliceId = try await AccountCreateTransaction()
48-
.key(.single(privateKey.publicKey))
48+
.keyWithoutAlias(.single(privateKey.publicKey))
4949
.initialBalance(Hbar(5))
5050
.execute(client)
5151
.getReceipt(client)

Examples/LongTermScheduledTransaction/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ internal enum Program {
5151
*/
5252
print("Creating new account... (w/ above key list as an account key).")
5353
let aliceId = try await AccountCreateTransaction()
54-
.key(.keyList(thresholdKey))
54+
.keyWithoutAlias(.keyList(thresholdKey))
5555
.initialBalance(Hbar(2))
5656
.execute(client)
5757
.getReceipt(client)

Examples/MultiAppTransfer/main.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ internal enum Program {
3939
let exchangeAccountId = try await AccountCreateTransaction()
4040
// the exchange only accepts transfers that it validates through a side channel (e.g. REST API)
4141
.receiverSignatureRequired(true)
42-
.key(.single(exchangeKey.publicKey))
42+
.keyWithoutAlias(.single(exchangeKey.publicKey))
4343
// The owner key has to sign this transaction
4444
// when receiver_signature_required is true
4545
.freezeWith(client)
@@ -52,7 +52,7 @@ internal enum Program {
5252
// the user with a balance of 5 h
5353
let userAccountId = try await AccountCreateTransaction()
5454
.initialBalance(Hbar(5))
55-
.key(.single(userKey.publicKey))
55+
.keyWithoutAlias(.single(userKey.publicKey))
5656
.execute(client)
5757
.getReceipt(client)
5858
.accountId!

Examples/MultiSigOffline/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ internal enum Program {
4242

4343
let createAccountTransaction = try await AccountCreateTransaction()
4444
.initialBalance(2)
45-
.key(.keyList(keylist))
45+
.keyWithoutAlias(.keyList(keylist))
4646
.execute(client)
4747

4848
let accountId = try await createAccountTransaction.getReceipt(client).accountId!

Examples/NftUpdateMetadata/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ internal enum Program {
7777
let nftSerials = tokenMintReceipt.serials!
7878

7979
let accountCreateTransaction = try await AccountCreateTransaction()
80-
.key(.single(env.operatorKey.publicKey))
80+
.keyWithoutAlias(.single(env.operatorKey.publicKey))
8181
.maxAutomaticTokenAssociations(10)
8282
.execute(client)
8383

0 commit comments

Comments
 (0)