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 6cd253d
Show file tree
Hide file tree
Showing 3 changed files with 370 additions and 0 deletions.
128 changes: 128 additions & 0 deletions examples/create-account-with-alias-from-alias-key.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import {
AccountId,
PrivateKey,
Client,
Hbar,
AccountInfoQuery,
TransactionReceiptQuery,
AccountCreateTransaction,
} from "@hashgraph/sdk";

import dotenv from "dotenv";

dotenv.config();

/*
Reference: [Streamline key and alias specifications for AccountCreateTransaction #2795](https://github.com/hiero-ledger/hiero-sdk-js/issues/2795)
*/

async function main() {
if (process.env.OPERATOR_ID == null || process.env.OPERATOR_KEY == null) {
throw new Error(
"Environment variables OPERATOR_ID, and OPERATOR_KEY are required.",
);
}
const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorKey = PrivateKey.fromStringDer(process.env.OPERATOR_KEY);

const nodes = {
"127.0.0.1:50211": new AccountId(3),
};

const client = Client.forNetwork(nodes).setOperator(
operatorId,
operatorKey,
);

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();
}

void main();
123 changes: 123 additions & 0 deletions examples/create-account-with-alias-from-key.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import {
AccountId,
PrivateKey,
Client,
Hbar,
AccountInfoQuery,
TransactionReceiptQuery,
AccountCreateTransaction,
} from "@hashgraph/sdk";

import dotenv from "dotenv";

dotenv.config();

/*
Reference: [Streamline key and alias specifications for AccountCreateTransaction #2795](https://github.com/hiero-ledger/hiero-sdk-js/issues/2795)
*/

async function main() {
if (process.env.OPERATOR_ID == null || process.env.OPERATOR_KEY == null) {
throw new Error(
"Environment variables OPERATOR_ID, and OPERATOR_KEY are required.",
);
}
const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorKey = PrivateKey.fromStringDer(process.env.OPERATOR_KEY);

const nodes = {
"127.0.0.1:50211": new AccountId(3),
};

const client = Client.forNetwork(nodes).setOperator(
operatorId,
operatorKey,
);

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);
}

client.close();
}

void main();
119 changes: 119 additions & 0 deletions examples/create-account-without-alias.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import {
AccountId,
PrivateKey,
Client,
Hbar,
AccountInfoQuery,
TransactionReceiptQuery,
AccountCreateTransaction,
} from "@hashgraph/sdk";

import dotenv from "dotenv";

dotenv.config();

/*
Reference: [Streamline key and alias specifications for AccountCreateTransaction #2795](https://github.com/hiero-ledger/hiero-sdk-js/issues/2795)
*/

async function main() {
if (process.env.OPERATOR_ID == null || process.env.OPERATOR_KEY == null) {
throw new Error(
"Environment variables OPERATOR_ID, and OPERATOR_KEY are required.",
);
}
const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorKey = PrivateKey.fromStringDer(process.env.OPERATOR_KEY);

const nodes = {
"127.0.0.1:50211": new AccountId(3),
};

const client = Client.forNetwork(nodes).setOperator(
operatorId,
operatorKey,
);

try {
/**
* Step 1
*
* Generate an account key
*/
const accountKey = PrivateKey.generateED25519();
console.log(`Account key: ${accountKey.toStringDer()}`);

/**
*
* Step 2
*
* Use the `AccountCreateTransaction`
* - Populate `setKeyWithoutAlias(key)` field with the generated account key
*/

const accountCreateTx = new AccountCreateTransaction()
.setInitialBalance(Hbar.fromTinybars(100))
.setKeyWithoutAlias(accountKey)
.freezeWith(client);

/**
*
* Step 3
*
* Sign the `AccountCreateTransaction` transaction with the account key and execute
*/
const accountCreateTxSign = await accountCreateTx.sign(accountKey);

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() === accountKey.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")
) {
console.log(`The new account doesn't have an EVM alias`);
} else {
console.log(
`The new account has an alias ${accountInfo.contractAccountId}`,
);
}
} catch (error) {
console.error(error);
}

client.close();
}

void main();

0 comments on commit 6cd253d

Please sign in to comment.