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

Feature add alias support in AccountCreateTX #1563

Merged
merged 4 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions examples/create-account-with-alias-and-receiver-signature.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import {
AccountId,
PrivateKey,
Client,
Hbar,
AccountInfoQuery,
TransactionReceiptQuery,
AccountCreateTransaction,
} from "@hashgraph/sdk";

import dotenv from "dotenv";

dotenv.config();

/*
Reference: [HIP-583 Expand alias support in CryptoCreate & CryptoTransfer Transactions](https://hips.hedera.com/hip/hip-583)
## Example 1:
- Create an ECSDA private key and an ED25519 admin private key
- Extract the ECDSA public key
- Extract the Ethereum public address
- Use the `AccountCreateTransaction`
- populate `setAlias(evmAddress)` field with the Ethereum public address
- populate the `setReceiverSignatureRequired` to true
- Sign the `AccountCreateTransaction` transaction with both the new private key and the admin key
- Get the `AccountInfo` and show that the account has contractAccountId
*/

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.fromString(process.env.OPERATOR_KEY);

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

/**
* Step 1
*
* Create an ECSDA private key and an ED25519 admin private key
*/
const adminKey = PrivateKey.generateED25519();
console.log(`Admin private key: ${adminKey.toStringDer()}`);

const privateKey = PrivateKey.generateECDSA();
console.log(`Private key: ${privateKey.toStringDer()}`);

/**
* Step 2
*
* Extract the ECDSA public key
*/
const publicKey = privateKey.publicKey;
console.log(`Public key: ${publicKey.toStringDer()}`);

/**
*
* Step 3
*
* Extract the Ethereum public address
*/
const evmAddress = publicKey.toEvmAddress();
console.log(`Corresponding evm address: ${evmAddress}`);

/**
*
* Step 4
*
* Use the `AccountCreateTransaction`
* - Populate `setAlias(evmAddress)` field with the Ethereum public address
* - Populate the `setReceiverSignatureRequired()` to `true`
*/
const accountCreateTx = new AccountCreateTransaction()
.setReceiverSignatureRequired(true)
.setInitialBalance(Hbar.fromTinybars(100))
.setKey(adminKey)
.setAlias(evmAddress)
.freezeWith(client);

/**
*
* Step 5
*
* Sign the `AccountCreateTransaction` transaction with both the new private key and key paying for the transaction fee
*/
const accountCreateTxSign = await (await accountCreateTx.sign(privateKey)).sign(adminKey);
const accountCreateTxResponse = await accountCreateTxSign.execute(client);

/**
*
* Step 6
*
* 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 7
*
* Get the `AccountInfo` and show that the account has contractAccountId
*/
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`);

}

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

import dotenv from "dotenv";

dotenv.config();

/*
Reference: [HIP-583 Expand alias support in CryptoCreate & CryptoTransfer Transactions](https://hips.hedera.com/hip/hip-583)
## Example 1:
- Create a ECSDA private key
- Extract the ECDSA public key
- Extract the Ethereum public address
- 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 `AccountInfo` and show that the account has contractAccountId
*/

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.fromString(process.env.OPERATOR_KEY);

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

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

/**
* Step 2
*
* Extract the ECDSA public key
*/
const publicKey = privateKey.publicKey;
console.log(`Public key: ${publicKey.toStringDer()}`);

/**
*
* Step 3
*
* Extract the Ethereum public address
*/
const evmAddress = publicKey.toEvmAddress();
console.log(`Corresponding evm address: ${evmAddress}`);

/**
*
* Step 4
*
* Use the `AccountCreateTransaction`
* - Populate `setAlias(evmAddress)` field with the Ethereum public address
*/
const accountCreateTx = new AccountCreateTransaction()
.setInitialBalance(Hbar.fromTinybars(100))
.setKey(operatorKey)
.setAlias(evmAddress)
.freezeWith(client);

/**
*
* Step 5
*
* Sign the `AccountCreateTransaction` transaction with both the new private key and key paying for the transaction fee
*/
const accountCreateTxSign = await accountCreateTx.sign(privateKey);
const accountCreateTxResponse = await accountCreateTxSign.execute(client);

/**
*
* Step 6
*
* 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 7
*
* Get the `AccountInfo` and show that the account has contractAccountId
*/
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`);

}

void main();
2 changes: 1 addition & 1 deletion examples/lazy-create-transfer-tx.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async function main() {
* Extract the Ethereum public address
*/
const evmAddress = publicKey.toEvmAddress();
console.log(`New account ID: ${evmAddress}`);
console.log(`Corresponding evm address: ${evmAddress}`);

/**
*
Expand Down
Loading