-
Notifications
You must be signed in to change notification settings - Fork 148
/
create-account-with-alias-and-receiver-signature.js
137 lines (119 loc) · 4.13 KB
/
create-account-with-alias-and-receiver-signature.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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.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 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`);
} catch (error) {
console.error(error);
}
client.close();
}
void main();