Skip to content

Commit

Permalink
feat(examples): add code examples for key alias functionalities
Browse files Browse the repository at this point in the history
Added code examples demonstrating how to use:
- `setKeyWithAlias(ECDSAKey)`
- `setKeyWithAlias(Key, ECDSAKey)`
- `setKeyWithoutAlias(Key)`

Signed-off-by: venilinvasilev <[email protected]>
  • Loading branch information
venilinvasilev committed Feb 3, 2025
1 parent f44aac9 commit 30f3dd4
Showing 1 changed file with 181 additions and 0 deletions.
181 changes: 181 additions & 0 deletions examples/create-account-with-alias.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,187 @@ async function main() {
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)
*/

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

/**
*
* Step 2
*
* Use the `AccountCreateTransaction`
* - Populate `setKeyWithAlias(privateKey)` field with the generated ECDSA private key
*/
const accountCreateTx = new AccountCreateTransaction()
.setInitialBalance(Hbar.fromTinybars(100))
.setKeyWithAlias(privateKey)
.freezeWith(client);

/**
*
* Step 3
*
* Sign the `AccountCreateTransaction` transaction with the generated private key and execute it
*/
const accountCreateTxSign = await accountCreateTx.sign(privateKey);
const accountCreateTxResponse =
await accountCreateTxSign.execute(client);

/**
*
* Step 4
*
* Get the account ID of the newly created account
*/
const receipt = await new TransactionReceiptQuery()
.setTransactionId(accountCreateTxResponse.transactionId)
.execute(client);

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

/**
*
* Step 4
*
* Get the `AccountInfo` and examine the created account key and alias
*/
const accountInfo = await new AccountInfoQuery()
.setAccountId(newAccountId)
.execute(client);
const matchesKeyOrNot =
accountInfo.key.toString() === privateKey.publicKey.toString()
? "matches"
: "doesn't match";

console.log(
`The newly created account has a key ${accountInfo.key.toString()} and it ${matchesKeyOrNot} the ECDSA private key`,
);

if (
accountInfo.contractAccountId !== null &&
!accountInfo.contractAccountId.startsWith(
"000000000000000000000000",
)
) {
const matchesOrNot =
privateKey.publicKey.toEvmAddress() ===
accountInfo.contractAccountId
? "matches"
: "doesn't match";
console.log(
`The newly created account has an EVM alias ${accountInfo.contractAccountId} and it ${matchesOrNot} the alias from the ECDSA private account key`,
);
} else {
console.log(`The new account doesn't have an EVM alias`);
}
} catch (error) {
console.error(error);
}

/* 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)
*/

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
*/

const accountCreateTx = new AccountCreateTransaction()
.setInitialBalance(Hbar.fromTinybars(100))
.setKeyWithAlias(key, aliasKey)
.freezeWith(client);

/**
*
* Step 3
*
* Sign the `AccountCreateTransaction` transaction with both keys and execute.
*/
const accountCreateTxSign = await (
await accountCreateTx.sign(key)
).sign(aliasKey);
const accountCreateTxResponse =
await accountCreateTxSign.execute(client);

/**
*
* Step 4
*
* Get the account ID of the newly created account
*/
const receipt = await new TransactionReceiptQuery()
.setTransactionId(accountCreateTxResponse.transactionId)
.execute(client);

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

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

const matchesKeyOrNot =
accountInfo.key.toString() === key.publicKey.toString()
? "matches"
: "doesn't match";

console.log(
`The newly created account has a key ${accountInfo.key.toString()} and it ${matchesKeyOrNot} the private key`,
);

if (
accountInfo.contractAccountId !== null &&
!accountInfo.contractAccountId.startsWith(
"000000000000000000000000",
)
) {
const matchesOrNot =
aliasKey.publicKey.toEvmAddress() ===
accountInfo.contractAccountId
? "matches"
: "doesn't match";
console.log(
`The newly created account has an EVM alias ${accountInfo.contractAccountId} and it ${matchesOrNot} the alias from the alias ECDSA private key`,
);
} else {
console.log(`The new account doesn't have an alias`);
}
} catch (error) {
console.error(error);
}

client.close();
}

Expand Down

0 comments on commit 30f3dd4

Please sign in to comment.