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:
- `setECDSAKeyWithAlias(ECDSAKey)`
- `setKeyWithAlias(Key, ECDSAKey)`
- `setKeyWithoutAlias(Key)`

Signed-off-by: venilinvasilev <[email protected]>
  • Loading branch information
venilinvasilev committed Feb 11, 2025
1 parent 7cbaea3 commit 28259c2
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 26 deletions.
169 changes: 149 additions & 20 deletions examples/create-account-with-alias.js
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))
.setKeyWithoutAlias(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);
}

/* 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
12 changes: 6 additions & 6 deletions examples/token-airdrop-example.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@ async function main() {
* Create 4 accounts
*/

const privateKey = PrivateKey.generateED25519();
const privateKey = PrivateKey.generateECDSA();
const { accountId: accountId1 } = await (
await new AccountCreateTransaction()
.setKeyWithAlias(privateKey)
.setECDSAKeyWithAlias(privateKey)
.setInitialBalance(new Hbar(10))
.setMaxAutomaticTokenAssociations(-1)
.execute(client)
).getReceipt(client);

const privateKey2 = PrivateKey.generateED25519();
const privateKey2 = PrivateKey.generateECDSA();
const { accountId: accountId2 } = await (
await new AccountCreateTransaction()
.setKeyWithAlias(privateKey2)
.setECDSAKeyWithAlias(privateKey2)
.setInitialBalance(new Hbar(10))
.setMaxAutomaticTokenAssociations(1)
.execute(client)
Expand All @@ -75,10 +75,10 @@ async function main() {
.execute(client)
).getReceipt(client);

const treasuryKey = PrivateKey.generateED25519();
const treasuryKey = PrivateKey.generateECDSA();
const { accountId: treasuryAccount } = await (
await new AccountCreateTransaction()
.setKeyWithAlias(treasuryKey)
.setECDSAKeyWithAlias(treasuryKey)
.setInitialBalance(new Hbar(10))
.setMaxAutomaticTokenAssociations(-1)
.execute(client)
Expand Down

0 comments on commit 28259c2

Please sign in to comment.