Skip to content

Commit 9a16331

Browse files
feat: account create transaction with key derived alias (#2834)
* feat(account-create-transaction): add new key alias derivation API Added new APIs to AccountCreateTransaction: - `setECDSAKeyWithAlias(ECDSAPrivateKey)`: Sets ECDSA private key and alias derived from it. - `setKeyWithAlias(Key, ECDSAPrivateKey)`: Allows setting account key and a separate ECDSA private key for EVM address derivation. - `setKeyWithoutAlias(Key)`: Explicitly sets key without alias setting. Deprecated: - `setKey(Key)`: Replaced with `setKeyWithoutAlias` method. Includes unit tests for all new functionality. Signed-off-by: venilinvasilev <[email protected]> * test(account-create-transaction): add integration tests Added integration tests for `AccountCreateTransaction` to validate new key alias APIs Replaced deprecated `setKey(Key)` references with `setKeyWithoutAlias(Key)`, aligning with the new API changes. Signed-off-by: venilinvasilev <[email protected]> * refactor(tests): replace setKey with setKeyWithoutAlias in tests Updated all test suites to replace deprecated `setKey(Key)` calls with `setKeyWithoutAlias(Key)`, ensuring compatibility with the latest API changes. Signed-off-by: venilinvasilev <[email protected]> * refactor(examples): replace setKey with setKeyWithoutAlias in examples Replaced all occurrences of `setKey(Key)` with `setKeyWithoutAlias(Key)` in example implementations, ensuring consistency with the latest API updates. Signed-off-by: venilinvasilev <[email protected]> * refactor(tck): replace deprecated setKey with setKeyWithoutAlias Updated a remaining deprecated reference to `setKey(Key)` in tck Signed-off-by: venilinvasilev <[email protected]> * feat(examples): add code examples for key alias functionalities Added code examples demonstrating how to use: - `setECDSAKeyWithAlias(ECDSAKey)` - `setKeyWithAlias(Key, ECDSAKey)` - `setKeyWithoutAlias(Key)` Signed-off-by: venilinvasilev <[email protected]> --------- Signed-off-by: venilinvasilev <[email protected]>
1 parent 00f64ac commit 9a16331

File tree

65 files changed

+896
-219
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+896
-219
lines changed

examples/account-allowance.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ async function main() {
4646

4747
try {
4848
let transaction = await new AccountCreateTransaction()
49-
.setKey(aliceKey)
49+
.setKeyWithoutAlias(aliceKey)
5050
.setInitialBalance(new Hbar(5))
5151
.freezeWithSigner(wallet);
5252
transaction = await transaction.signWithSigner(wallet);
@@ -55,7 +55,7 @@ async function main() {
5555
const aliceId = (await response.getReceiptWithSigner(wallet)).accountId;
5656

5757
transaction = await new AccountCreateTransaction()
58-
.setKey(bobKey)
58+
.setKeyWithoutAlias(bobKey)
5959
.setInitialBalance(new Hbar(5))
6060
.freezeWithSigner(wallet);
6161
transaction = await transaction.signWithSigner(wallet);
@@ -67,7 +67,7 @@ async function main() {
6767
).accountId;
6868

6969
transaction = await new AccountCreateTransaction()
70-
.setKey(charlieKey)
70+
.setKeyWithoutAlias(charlieKey)
7171
.setInitialBalance(new Hbar(5))
7272
.freezeWithSigner(wallet);
7373
transaction = await transaction.signWithSigner(wallet);

examples/create-account-with-alias-and-receiver-signature.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ async function main() {
8383
const accountCreateTx = new AccountCreateTransaction()
8484
.setReceiverSignatureRequired(true)
8585
.setInitialBalance(Hbar.fromTinybars(100))
86-
.setKey(adminKey)
86+
.setKeyWithoutAlias(adminKey)
8787
.setAlias(evmAddress)
8888
.freezeWith(client);
8989

examples/create-account-with-alias.js

Lines changed: 149 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
Client,
55
Hbar,
66
AccountInfoQuery,
7-
TransactionReceiptQuery,
87
AccountCreateTransaction,
98
} from "@hashgraph/sdk";
109

@@ -74,51 +73,181 @@ async function main() {
7473
*
7574
* Use the `AccountCreateTransaction`
7675
* - Populate `setAlias(evmAddress)` field with the Ethereum public address
76+
* Sign the `AccountCreateTransaction` transaction with both the new private key and key paying for the transaction fee
77+
* Get the account ID of the newly created account
7778
*/
78-
const accountCreateTx = new AccountCreateTransaction()
79-
.setInitialBalance(Hbar.fromTinybars(100))
80-
.setKey(operatorKey)
81-
.setAlias(evmAddress)
82-
.freezeWith(client);
79+
const receipt = await (
80+
await (
81+
await new AccountCreateTransaction()
82+
.setInitialBalance(Hbar.fromTinybars(100))
83+
.setKeyWithoutAlias(operatorKey)
84+
.setAlias(evmAddress)
85+
.freezeWith(client)
86+
.sign(privateKey)
87+
).execute(client)
88+
).getReceipt(client);
89+
90+
const newAccountId = receipt.accountId.toString();
91+
console.log(`Account ID of the newly created account: ${newAccountId}`);
8392

8493
/**
8594
*
8695
* Step 5
8796
*
88-
* Sign the `AccountCreateTransaction` transaction with both the new private key and key paying for the transaction fee
97+
* Get the `AccountInfo` and show that the account has contractAccountId
8998
*/
90-
const accountCreateTxSign = await accountCreateTx.sign(privateKey);
91-
const accountCreateTxResponse =
92-
await accountCreateTxSign.execute(client);
99+
const accountInfo = await new AccountInfoQuery()
100+
.setAccountId(newAccountId)
101+
.execute(client);
93102

103+
console.log(
104+
`The newly created account has an alias: ${accountInfo.contractAccountId}`,
105+
);
106+
} catch (error) {
107+
console.error(error);
108+
}
109+
110+
/* Create an account with derived EVM alias from private ECDSA account key
111+
*
112+
* Reference: [Streamline key and alias specifications for AccountCreateTransaction #2795](https://github.com/hiero-ledger/hiero-sdk-js/issues/2795)
113+
*/
114+
console.log(
115+
"---Create an account with derived EVM alias from private ECDSA account key---",
116+
);
117+
118+
try {
94119
/**
120+
* Step 1
95121
*
96-
* Step 6
122+
* Create an ECSDA private key
123+
*/
124+
const privateKey = PrivateKey.generateECDSA();
125+
console.log(`Private key: ${privateKey.toStringDer()}`);
126+
127+
/**
128+
*
129+
* Step 2
97130
*
131+
* Use the `AccountCreateTransaction`
132+
* - Populate `setECDSAKeyWithAlias(privateKey)` field with the generated ECDSA private key
133+
* Sign the `AccountCreateTransaction` transaction with the generated private key and execute it
98134
* Get the account ID of the newly created account
99135
*/
100-
const receipt = await new TransactionReceiptQuery()
101-
.setTransactionId(accountCreateTxResponse.transactionId)
136+
137+
const receipt = await (
138+
await (
139+
await new AccountCreateTransaction()
140+
.setInitialBalance(Hbar.fromTinybars(100))
141+
.setECDSAKeyWithAlias(privateKey)
142+
.freezeWith(client)
143+
.sign(privateKey)
144+
).execute(client)
145+
).getReceipt(client);
146+
147+
const newAccountId = receipt.accountId.toString();
148+
console.log(`Account ID of the newly created account: ${newAccountId}`);
149+
150+
/**
151+
*
152+
* Step 3
153+
*
154+
* Get the `AccountInfo` and examine the created account key and alias
155+
*/
156+
const accountInfo = await new AccountInfoQuery()
157+
.setAccountId(newAccountId)
102158
.execute(client);
103159

160+
console.log(
161+
`Account's key ${accountInfo.key.toString()} is the same as ${privateKey.publicKey.toStringDer()}`,
162+
);
163+
164+
if (
165+
!accountInfo.contractAccountId.startsWith(
166+
"000000000000000000000000",
167+
)
168+
) {
169+
console.log(
170+
`Initial EVM address: ${privateKey.publicKey.toEvmAddress()} is the same as ${
171+
accountInfo.contractAccountId
172+
}`,
173+
);
174+
} else {
175+
console.log(`The new account doesn't have an EVM alias`);
176+
}
177+
} catch (error) {
178+
console.error(error);
179+
}
180+
181+
/* Create an account with derived EVM alias from private ECDSA alias key
182+
*
183+
* Reference: [Streamline key and alias specifications for AccountCreateTransaction #2795](https://github.com/hiero-ledger/hiero-sdk-js/issues/2795)
184+
*/
185+
186+
console.log(
187+
"---Create an account with derived EVM alias from private ECDSA alias key---",
188+
);
189+
190+
try {
191+
/**
192+
* Step 1
193+
*
194+
* Create an account key and an ECSDA private alias key
195+
*/
196+
const key = PrivateKey.generateED25519();
197+
const aliasKey = PrivateKey.generateECDSA();
198+
console.log(`Alias key: ${aliasKey.toStringDer()}`);
199+
200+
/**
201+
*
202+
* Step 2
203+
*
204+
* Use the `AccountCreateTransaction`
205+
* - Populate `setKeyWithAlias(key, privateKey)` fields with the generated account key and the alias ECDSA private key
206+
* Sign the `AccountCreateTransaction` transaction with both keys and execute.
207+
* Get the account ID of the newly created account
208+
*
209+
*/
210+
211+
const receipt = await (
212+
await (
213+
await (
214+
await new AccountCreateTransaction()
215+
.setInitialBalance(Hbar.fromTinybars(100))
216+
.setKeyWithAlias(key, aliasKey)
217+
.freezeWith(client)
218+
.sign(key)
219+
).sign(aliasKey)
220+
).execute(client)
221+
).getReceipt(client);
222+
104223
const newAccountId = receipt.accountId.toString();
105224
console.log(`Account ID of the newly created account: ${newAccountId}`);
106225

107226
/**
108227
*
109-
* Step 7
228+
* Step 3
110229
*
111-
* Get the `AccountInfo` and show that the account has contractAccountId
230+
* Get the `AccountInfo` and examine the created account key and alias
112231
*/
113232
const accountInfo = await new AccountInfoQuery()
114233
.setAccountId(newAccountId)
115234
.execute(client);
116235

117-
accountInfo.contractAccountId !== null
118-
? console.log(
119-
`The newly created account has an alias: ${accountInfo.contractAccountId}`,
120-
)
121-
: console.log(`The new account doesn't have an alias`);
236+
console.log(
237+
`Account's key ${accountInfo.key.toString()} is the same as ${key.publicKey.toString()}`,
238+
);
239+
240+
if (
241+
!accountInfo.contractAccountId.startsWith(
242+
"000000000000000000000000",
243+
)
244+
) {
245+
console.log(
246+
`Initial EVM address: ${accountInfo.contractAccountId} is the same as ${aliasKey.publicKey.toEvmAddress()}`,
247+
);
248+
} else {
249+
console.log(`The new account doesn't have an alias`);
250+
}
122251
} catch (error) {
123252
console.error(error);
124253
}

examples/create-account-with-thresholdkey.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ async function main() {
8282
*/
8383
console.log("Creating new account...");
8484
const accountCreateTxResponse = await new AccountCreateTransaction()
85-
.setKey(thresholdKey)
85+
.setKeyWithoutAlias(thresholdKey)
8686
.setInitialBalance(new Hbar(100))
8787
.execute(client);
8888

examples/create-account.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ async function main() {
3737
try {
3838
let transaction = await new AccountCreateTransaction()
3939
.setInitialBalance(new Hbar(10)) // 10 h
40-
.setKey(newKey.publicKey)
40+
.setKeyWithoutAlias(newKey.publicKey)
4141
.freezeWithSigner(wallet);
4242

4343
transaction = await transaction.signWithSigner(wallet);

examples/delete-account.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ async function main() {
3838
try {
3939
let transaction = await new AccountCreateTransaction()
4040
.setInitialBalance(new Hbar(10)) // 10 h
41-
.setKey(newKey.publicKey)
41+
.setKeyWithoutAlias(newKey.publicKey)
4242
.freezeWithSigner(wallet);
4343
transaction = await transaction.signWithSigner(wallet);
4444
const response = await transaction.executeWithSigner(wallet);

examples/exempt-custom-fees.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ async function main() {
6262
let firstAccountPublicKey = firstAccountPrivateKey.publicKey;
6363

6464
let createAccountAtx = await new AccountCreateTransaction()
65-
.setKey(firstAccountPublicKey)
65+
.setKeyWithoutAlias(firstAccountPublicKey)
6666
.setInitialBalance(Hbar.fromString("1000"))
6767
.freezeWithSigner(wallet);
6868
createAccountAtx = await createAccountAtx.signWithSigner(wallet);
@@ -81,7 +81,7 @@ async function main() {
8181
let secondAccountPublicKey = secondAccountPrivateKey.publicKey;
8282

8383
let createAccountBtx = await new AccountCreateTransaction()
84-
.setKey(secondAccountPublicKey)
84+
.setKeyWithoutAlias(secondAccountPublicKey)
8585
.setInitialBalance(Hbar.fromString("1000"))
8686
.freezeWithSigner(wallet);
8787
createAccountBtx = await createAccountBtx.signWithSigner(wallet);
@@ -100,7 +100,7 @@ async function main() {
100100
let thirdAccountPublicKey = thirdAccountPrivateKey.publicKey;
101101

102102
let createAccountCtx = await new AccountCreateTransaction()
103-
.setKey(thirdAccountPublicKey)
103+
.setKeyWithoutAlias(thirdAccountPublicKey)
104104
.setInitialBalance(Hbar.fromString("1000"))
105105
.freezeWithSigner(wallet);
106106
createAccountCtx = await createAccountCtx.signWithSigner(wallet);

examples/generate-txid-on-demand.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ async function main() {
8080

8181
const accountCreateTx = await new AccountCreateTransaction()
8282
.setInitialBalance(new Hbar(10)) // 10 h
83-
.setKey(newKey.publicKey)
83+
.setKeyWithoutAlias(newKey.publicKey)
8484
.freezeWith(client)
8585
.execute(client);
8686

examples/initialize-client-with-mirror-node-adress-book.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ async function main() {
3939
try {
4040
let transaction = new AccountCreateTransaction()
4141
.setInitialBalance(new Hbar(1))
42-
.setKey(accountKey)
42+
.setKeyWithoutAlias(accountKey)
4343
.freezeWith(client);
4444

4545
transaction = await transaction.sign(accountKey);

examples/long-term-schedule-transaction.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ async function main() {
4848
const aliceId = (
4949
await (
5050
await new AccountCreateTransaction()
51-
.setKey(thresholdKey)
51+
.setKeyWithoutAlias(thresholdKey)
5252
.setInitialBalance(new Hbar(2))
5353
.execute(client)
5454
).getReceipt(client)

examples/multi-node-multi-signature-remove.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ async function main() {
6262

6363
const createAccountTransaction = new AccountCreateTransaction()
6464
.setInitialBalance(new Hbar(2))
65-
.setKey(keyList);
65+
.setKeyWithoutAlias(keyList);
6666

6767
const createResponse = await createAccountTransaction.execute(client);
6868
const createReceipt = await createResponse.getReceipt(client);

examples/multi-node-multi-signature-removeAll.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ async function main() {
6363
*/
6464
const createAccountTransaction = new AccountCreateTransaction()
6565
.setInitialBalance(new Hbar(2))
66-
.setKey(keyList);
66+
.setKeyWithoutAlias(keyList);
6767

6868
const createResponse = await createAccountTransaction.execute(client);
6969
const createReceipt = await createResponse.getReceipt(client);

examples/multi-node-multi-signature.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ async function main() {
5757

5858
const createAccountTransaction = new AccountCreateTransaction()
5959
.setInitialBalance(new Hbar(2))
60-
.setKey(keyList);
60+
.setKeyWithoutAlias(keyList);
6161

6262
const createResponse = await createAccountTransaction.execute(client);
6363
const createReceipt = await createResponse.getReceipt(client);

examples/multi-sig-offline.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ async function main() {
4848

4949
const createAccountTransaction = new AccountCreateTransaction()
5050
.setInitialBalance(new Hbar(2)) // 5 h
51-
.setKey(keyList);
51+
.setKeyWithoutAlias(keyList);
5252

5353
const response = await createAccountTransaction.execute(client);
5454

0 commit comments

Comments
 (0)