@hashgraph/hedera-local
Version:
Developer tooling for running Local Hedera Network (Consensus + Mirror Nodes).
83 lines • 4.11 kB
JavaScript
;
// SPDX-License-Identifier: Apache-2.0
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AccountUtils = void 0;
const sdk_1 = require("@hashgraph/sdk");
const IPrivateKey_1 = require("../configuration/types/IPrivateKey");
/**
* Provides utility methods for working with accounts.
*/
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.
*/
static createAccountFromProps(account, client) {
return __awaiter(this, void 0, void 0, function* () {
const keyType = account.privateKey ? account.privateKey.type : IPrivateKey_1.KeyType.ECDSA;
const privateKey = account.privateKey ? (0, IPrivateKey_1.getPrivateKey)(account.privateKey) : sdk_1.PrivateKey.generateECDSA();
let accountInfo;
if (keyType === IPrivateKey_1.KeyType.ED25519) {
accountInfo = yield this.createAccount(privateKey.publicKey, account.balance, client);
}
else {
const accountId = privateKey.publicKey.toAccountId(0, 0);
accountInfo = yield 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.
*/
static createAliasedAccount(aliasAccountId, initialBalance, client) {
return __awaiter(this, void 0, void 0, function* () {
const hbarAmount = new sdk_1.Hbar(initialBalance);
const response = yield new sdk_1.TransferTransaction()
.addHbarTransfer(client.operatorAccountId, hbarAmount.negated())
.addHbarTransfer(aliasAccountId, hbarAmount)
.execute(client);
yield response.getReceipt(client);
return new sdk_1.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.
*/
static createAccount(publicKey, initialBalance, client) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield new sdk_1.AccountCreateTransaction()
.setKey(publicKey)
.setInitialBalance(new sdk_1.Hbar(initialBalance))
.execute(client);
const receipt = yield response.getReceipt(client);
return new sdk_1.AccountInfoQuery()
.setAccountId(receipt.accountId)
.execute(client);
});
}
}
exports.AccountUtils = AccountUtils;
//# sourceMappingURL=AccountUtils.js.map