forked from hashgraph/hedera-sdk-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-account-with-alias.js
115 lines (100 loc) · 3.28 KB
/
create-account-with-alias.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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();