Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 5x 5x 5x 1x 4x 4x 5x 1x 1x 1x 1x 1x 1x 1x | // SPDX-License-Identifier: Apache-2.0
import {
AccountCreateTransaction,
AccountId,
AccountInfo,
AccountInfoQuery,
Client,
Hbar,
PrivateKey,
PublicKey,
TransferTransaction
} from '@hashgraph/sdk';
import { IAccountProps } from '../configuration/types/IAccountProps';
import { getPrivateKey, KeyType } from '../configuration/types/IPrivateKey';
/**
* Provides utility methods for working with accounts.
*/
export class AccountUtils {
/**
* Creates an account with the given properties.
* @param account The account properties.
* @param client The client to use for creating the account.
* @returns {Promise<{privateKey: PrivateKey, accountInfo: AccountInfo}>}
* The private key and account info of the created account.
*/
public static async createAccountFromProps(account: IAccountProps,
client: Client): Promise<{privateKey: PrivateKey, accountInfo: AccountInfo}> {
const keyType = account.privateKey ? account.privateKey.type : KeyType.ECDSA;
const privateKey = account.privateKey ? getPrivateKey(account.privateKey) : PrivateKey.generateECDSA();
let accountInfo: AccountInfo;
if (keyType === KeyType.ED25519) {
accountInfo = await this.createAccount(privateKey.publicKey, account.balance, client);
} else {
const accountId = privateKey.publicKey.toAccountId(0, 0);
accountInfo = await this.createAliasedAccount(accountId, account.balance, client);
}
return { accountInfo, privateKey };
}
/**
* Creates an account with the given properties.
* @param aliasAccountId The alias ID of the account to create.
* @param initialBalance The initial balance of the account.
* @param client The client to use for creating the account.
* @returns {AccountInfo} The account info of the created account.
*/
public static async createAliasedAccount(aliasAccountId: AccountId,
initialBalance: number,
client: Client): Promise<AccountInfo> {
const hbarAmount = new Hbar(initialBalance);
const response = await new TransferTransaction()
.addHbarTransfer(client.operatorAccountId!, hbarAmount.negated())
.addHbarTransfer(aliasAccountId, hbarAmount)
.execute(client);
await response.getReceipt(client);
return new AccountInfoQuery()
.setAccountId(aliasAccountId)
.execute(client);
}
/**
* Creates an account with the given properties.
* @param publicKey The public key of the account to create.
* @param initialBalance The initial balance of the account.
* @param client The client to use for creating the account.
* @returns {AccountInfo} The account info of the created account.
*/
public static async createAccount(publicKey: PublicKey,
initialBalance: number,
client: Client): Promise<AccountInfo> {
const response = await new AccountCreateTransaction()
.setKey(publicKey)
.setInitialBalance(new Hbar(initialBalance))
.execute(client);
const receipt = await response.getReceipt(client);
return new AccountInfoQuery()
.setAccountId(receipt.accountId!)
.execute(client);
}
}
|