Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: account create transaction with key derived alias #2834

6 changes: 3 additions & 3 deletions examples/account-allowance.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async function main() {

try {
let transaction = await new AccountCreateTransaction()
.setKey(aliceKey)
.setKeyWithoutAlias(aliceKey)
.setInitialBalance(new Hbar(5))
.freezeWithSigner(wallet);
transaction = await transaction.signWithSigner(wallet);
Expand All @@ -55,7 +55,7 @@ async function main() {
const aliceId = (await response.getReceiptWithSigner(wallet)).accountId;

transaction = await new AccountCreateTransaction()
.setKey(bobKey)
.setKeyWithoutAlias(bobKey)
.setInitialBalance(new Hbar(5))
.freezeWithSigner(wallet);
transaction = await transaction.signWithSigner(wallet);
Expand All @@ -67,7 +67,7 @@ async function main() {
).accountId;

transaction = await new AccountCreateTransaction()
.setKey(charlieKey)
.setKeyWithoutAlias(charlieKey)
.setInitialBalance(new Hbar(5))
.freezeWithSigner(wallet);
transaction = await transaction.signWithSigner(wallet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ async function main() {
const accountCreateTx = new AccountCreateTransaction()
.setReceiverSignatureRequired(true)
.setInitialBalance(Hbar.fromTinybars(100))
.setKey(adminKey)
.setKeyWithoutAlias(adminKey)
.setAlias(evmAddress)
.freezeWith(client);

Expand Down
169 changes: 149 additions & 20 deletions examples/create-account-with-alias.js
venilinvasilev marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
Client,
Hbar,
AccountInfoQuery,
TransactionReceiptQuery,
AccountCreateTransaction,
} from "@hashgraph/sdk";

Expand Down Expand Up @@ -74,51 +73,181 @@ async function main() {
*
* Use the `AccountCreateTransaction`
* - Populate `setAlias(evmAddress)` field with the Ethereum public address
* Sign the `AccountCreateTransaction` transaction with both the new private key and key paying for the transaction fee
* Get the account ID of the newly created account
*/
const accountCreateTx = new AccountCreateTransaction()
.setInitialBalance(Hbar.fromTinybars(100))
.setKey(operatorKey)
.setAlias(evmAddress)
.freezeWith(client);
const receipt = await (
await (
await new AccountCreateTransaction()
.setInitialBalance(Hbar.fromTinybars(100))
.setKeyWithoutAlias(operatorKey)
.setAlias(evmAddress)
.freezeWith(client)
.sign(privateKey)
).execute(client)
).getReceipt(client);

const newAccountId = receipt.accountId.toString();
console.log(`Account ID of the newly created account: ${newAccountId}`);

/**
*
* Step 5
*
* Sign the `AccountCreateTransaction` transaction with both the new private key and key paying for the transaction fee
* Get the `AccountInfo` and show that the account has contractAccountId
*/
const accountCreateTxSign = await accountCreateTx.sign(privateKey);
const accountCreateTxResponse =
await accountCreateTxSign.execute(client);
const accountInfo = await new AccountInfoQuery()
.setAccountId(newAccountId)
.execute(client);

console.log(
`The newly created account has an alias: ${accountInfo.contractAccountId}`,
);
} catch (error) {
console.error(error);
}

/* Create an account with derived EVM alias from private ECDSA account key
*
* Reference: [Streamline key and alias specifications for AccountCreateTransaction #2795](https://github.com/hiero-ledger/hiero-sdk-js/issues/2795)
*/
console.log(
"---Create an account with derived EVM alias from private ECDSA account key---",
);

try {
/**
* Step 1
*
* Step 6
* Create an ECSDA private key
*/
const privateKey = PrivateKey.generateECDSA();
console.log(`Private key: ${privateKey.toStringDer()}`);

/**
*
* Step 2
*
* Use the `AccountCreateTransaction`
* - Populate `setECDSAKeyWithAlias(privateKey)` field with the generated ECDSA private key
* Sign the `AccountCreateTransaction` transaction with the generated private key and execute it
* Get the account ID of the newly created account
*/
const receipt = await new TransactionReceiptQuery()
.setTransactionId(accountCreateTxResponse.transactionId)

const receipt = await (
await (
await new AccountCreateTransaction()
.setInitialBalance(Hbar.fromTinybars(100))
.setECDSAKeyWithAlias(privateKey)
.freezeWith(client)
.sign(privateKey)
).execute(client)
).getReceipt(client);

const newAccountId = receipt.accountId.toString();
console.log(`Account ID of the newly created account: ${newAccountId}`);

/**
*
* Step 3
*
* Get the `AccountInfo` and examine the created account key and alias
*/
const accountInfo = await new AccountInfoQuery()
.setAccountId(newAccountId)
.execute(client);

console.log(
`Account's key ${accountInfo.key.toString()} is the same as ${privateKey.publicKey.toStringDer()}`,
);

if (
!accountInfo.contractAccountId.startsWith(
"000000000000000000000000",
)
) {
console.log(
`Initial EVM address: ${privateKey.publicKey.toEvmAddress()} is the same as ${
accountInfo.contractAccountId
}`,
);
} else {
console.log(`The new account doesn't have an EVM alias`);
}
} catch (error) {
console.error(error);
}

venilinvasilev marked this conversation as resolved.
Show resolved Hide resolved
/* Create an account with derived EVM alias from private ECDSA alias key
*
* Reference: [Streamline key and alias specifications for AccountCreateTransaction #2795](https://github.com/hiero-ledger/hiero-sdk-js/issues/2795)
*/

console.log(
"---Create an account with derived EVM alias from private ECDSA alias key---",
);

try {
/**
* Step 1
*
* Create an account key and an ECSDA private alias key
*/
const key = PrivateKey.generateED25519();
const aliasKey = PrivateKey.generateECDSA();
console.log(`Alias key: ${aliasKey.toStringDer()}`);

/**
*
* Step 2
*
* Use the `AccountCreateTransaction`
* - Populate `setKeyWithAlias(key, privateKey)` fields with the generated account key and the alias ECDSA private key
* Sign the `AccountCreateTransaction` transaction with both keys and execute.
* Get the account ID of the newly created account
*
*/

const receipt = await (
await (
await (
await new AccountCreateTransaction()
.setInitialBalance(Hbar.fromTinybars(100))
.setKeyWithAlias(key, aliasKey)
.freezeWith(client)
.sign(key)
).sign(aliasKey)
).execute(client)
).getReceipt(client);

const newAccountId = receipt.accountId.toString();
console.log(`Account ID of the newly created account: ${newAccountId}`);

/**
*
* Step 7
* Step 3
*
* Get the `AccountInfo` and show that the account has contractAccountId
* Get the `AccountInfo` and examine the created account key and alias
*/
const accountInfo = await new AccountInfoQuery()
.setAccountId(newAccountId)
.execute(client);

accountInfo.contractAccountId !== null
? console.log(
`The newly created account has an alias: ${accountInfo.contractAccountId}`,
)
: console.log(`The new account doesn't have an alias`);
console.log(
`Account's key ${accountInfo.key.toString()} is the same as ${key.publicKey.toString()}`,
);

if (
!accountInfo.contractAccountId.startsWith(
"000000000000000000000000",
)
) {
console.log(
`Initial EVM address: ${accountInfo.contractAccountId} is the same as ${aliasKey.publicKey.toEvmAddress()}`,
);
} else {
console.log(`The new account doesn't have an alias`);
}
} catch (error) {
console.error(error);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/create-account-with-thresholdkey.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ async function main() {
*/
console.log("Creating new account...");
const accountCreateTxResponse = await new AccountCreateTransaction()
.setKey(thresholdKey)
.setKeyWithoutAlias(thresholdKey)
.setInitialBalance(new Hbar(100))
.execute(client);

Expand Down
2 changes: 1 addition & 1 deletion examples/create-account.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async function main() {
try {
let transaction = await new AccountCreateTransaction()
.setInitialBalance(new Hbar(10)) // 10 h
.setKey(newKey.publicKey)
.setKeyWithoutAlias(newKey.publicKey)
.freezeWithSigner(wallet);

transaction = await transaction.signWithSigner(wallet);
Expand Down
2 changes: 1 addition & 1 deletion examples/delete-account.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async function main() {
try {
let transaction = await new AccountCreateTransaction()
.setInitialBalance(new Hbar(10)) // 10 h
.setKey(newKey.publicKey)
.setKeyWithoutAlias(newKey.publicKey)
.freezeWithSigner(wallet);
transaction = await transaction.signWithSigner(wallet);
const response = await transaction.executeWithSigner(wallet);
Expand Down
6 changes: 3 additions & 3 deletions examples/exempt-custom-fees.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async function main() {
let firstAccountPublicKey = firstAccountPrivateKey.publicKey;

let createAccountAtx = await new AccountCreateTransaction()
.setKey(firstAccountPublicKey)
.setKeyWithoutAlias(firstAccountPublicKey)
.setInitialBalance(Hbar.fromString("1000"))
.freezeWithSigner(wallet);
createAccountAtx = await createAccountAtx.signWithSigner(wallet);
Expand All @@ -81,7 +81,7 @@ async function main() {
let secondAccountPublicKey = secondAccountPrivateKey.publicKey;

let createAccountBtx = await new AccountCreateTransaction()
.setKey(secondAccountPublicKey)
.setKeyWithoutAlias(secondAccountPublicKey)
.setInitialBalance(Hbar.fromString("1000"))
.freezeWithSigner(wallet);
createAccountBtx = await createAccountBtx.signWithSigner(wallet);
Expand All @@ -100,7 +100,7 @@ async function main() {
let thirdAccountPublicKey = thirdAccountPrivateKey.publicKey;

let createAccountCtx = await new AccountCreateTransaction()
.setKey(thirdAccountPublicKey)
.setKeyWithoutAlias(thirdAccountPublicKey)
.setInitialBalance(Hbar.fromString("1000"))
.freezeWithSigner(wallet);
createAccountCtx = await createAccountCtx.signWithSigner(wallet);
Expand Down
2 changes: 1 addition & 1 deletion examples/generate-txid-on-demand.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async function main() {

const accountCreateTx = await new AccountCreateTransaction()
.setInitialBalance(new Hbar(10)) // 10 h
.setKey(newKey.publicKey)
.setKeyWithoutAlias(newKey.publicKey)
.freezeWith(client)
.execute(client);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async function main() {
try {
let transaction = new AccountCreateTransaction()
.setInitialBalance(new Hbar(1))
.setKey(accountKey)
.setKeyWithoutAlias(accountKey)
.freezeWith(client);

transaction = await transaction.sign(accountKey);
Expand Down
2 changes: 1 addition & 1 deletion examples/long-term-schedule-transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async function main() {
const aliceId = (
await (
await new AccountCreateTransaction()
.setKey(thresholdKey)
.setKeyWithoutAlias(thresholdKey)
.setInitialBalance(new Hbar(2))
.execute(client)
).getReceipt(client)
Expand Down
2 changes: 1 addition & 1 deletion examples/multi-node-multi-signature-remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async function main() {

const createAccountTransaction = new AccountCreateTransaction()
.setInitialBalance(new Hbar(2))
.setKey(keyList);
.setKeyWithoutAlias(keyList);

const createResponse = await createAccountTransaction.execute(client);
const createReceipt = await createResponse.getReceipt(client);
Expand Down
2 changes: 1 addition & 1 deletion examples/multi-node-multi-signature-removeAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async function main() {
*/
const createAccountTransaction = new AccountCreateTransaction()
.setInitialBalance(new Hbar(2))
.setKey(keyList);
.setKeyWithoutAlias(keyList);

const createResponse = await createAccountTransaction.execute(client);
const createReceipt = await createResponse.getReceipt(client);
Expand Down
2 changes: 1 addition & 1 deletion examples/multi-node-multi-signature.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async function main() {

const createAccountTransaction = new AccountCreateTransaction()
.setInitialBalance(new Hbar(2))
.setKey(keyList);
.setKeyWithoutAlias(keyList);

const createResponse = await createAccountTransaction.execute(client);
const createReceipt = await createResponse.getReceipt(client);
Expand Down
2 changes: 1 addition & 1 deletion examples/multi-sig-offline.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async function main() {

const createAccountTransaction = new AccountCreateTransaction()
.setInitialBalance(new Hbar(2)) // 5 h
.setKey(keyList);
.setKeyWithoutAlias(keyList);

const response = await createAccountTransaction.execute(client);

Expand Down
Loading
Loading