use of com.hedera.hashgraph.sdk.AccountInfoQuery in project hedera-sdk-java by hashgraph.
the class AccountInfoIntegrationTest method getCostAccountInfoForClientOperator.
@Test
@DisplayName("Can get cost for account info query")
void getCostAccountInfoForClientOperator() throws Exception {
var testEnv = new IntegrationTestEnv(1);
var info = new AccountInfoQuery().setAccountId(testEnv.operatorId).setMaxQueryPayment(new Hbar(1));
var cost = info.getCost(testEnv.client);
var accInfo = info.setQueryPayment(cost).execute(testEnv.client);
assertThat(accInfo.accountId).isEqualTo(testEnv.operatorId);
testEnv.close();
}
use of com.hedera.hashgraph.sdk.AccountInfoQuery in project hedera-sdk-java by hashgraph.
the class AccountUpdateIntegrationTest method canUpdateAccountWithNewKey.
@Test
@DisplayName("Can update account with a new key")
void canUpdateAccountWithNewKey() throws Exception {
var testEnv = new IntegrationTestEnv(1);
var key1 = PrivateKey.generateED25519();
var key2 = PrivateKey.generateED25519();
var response = new AccountCreateTransaction().setKey(key1).execute(testEnv.client);
var accountId = Objects.requireNonNull(response.getReceipt(testEnv.client).accountId);
@Var var info = new AccountInfoQuery().setAccountId(accountId).execute(testEnv.client);
assertThat(info.accountId).isEqualTo(accountId);
assertThat(info.isDeleted).isFalse();
assertThat(info.key.toString()).isEqualTo(key1.getPublicKey().toString());
assertThat(info.balance).isEqualTo(new Hbar(0));
assertThat(info.autoRenewPeriod).isEqualTo(Duration.ofDays(90));
assertThat(info.proxyAccountId).isNull();
assertThat(info.proxyReceived).isEqualTo(Hbar.ZERO);
new AccountUpdateTransaction().setAccountId(accountId).setKey(key2.getPublicKey()).freezeWith(testEnv.client).sign(key1).sign(key2).execute(testEnv.client).getReceipt(testEnv.client);
info = new AccountInfoQuery().setAccountId(accountId).execute(testEnv.client);
assertThat(info.accountId).isEqualTo(accountId);
assertThat(info.isDeleted).isFalse();
assertThat(info.key.toString()).isEqualTo(key2.getPublicKey().toString());
assertThat(info.balance).isEqualTo(new Hbar(0));
assertThat(info.autoRenewPeriod).isEqualTo(Duration.ofDays(90));
assertThat(info.proxyAccountId).isNull();
assertThat(info.proxyReceived).isEqualTo(Hbar.ZERO);
testEnv.close(accountId, key2);
}
use of com.hedera.hashgraph.sdk.AccountInfoQuery in project hedera-sdk-java by hashgraph.
the class AccountCreateIntegrationTest method managesExpiration.
@Test
@DisplayName("Regenerates TransactionIds in response to expiration")
void managesExpiration() throws Exception {
var testEnv = new IntegrationTestEnv(1);
var key = PrivateKey.generate();
var accountCreateTx = new AccountCreateTransaction().setKey(key).setTransactionValidDuration(Duration.ofSeconds(25)).freezeWith(testEnv.client);
Thread.sleep(30000);
var response = accountCreateTx.execute(testEnv.client);
var accountId = Objects.requireNonNull(response.getReceipt(testEnv.client).accountId);
var info = new AccountInfoQuery().setAccountId(accountId).execute(testEnv.client);
assertThat(info.accountId).isEqualTo(accountId);
assertThat(info.isDeleted).isFalse();
assertThat(info.key.toString()).isEqualTo(key.getPublicKey().toString());
assertThat(info.balance).isEqualTo(new Hbar(0));
assertThat(info.autoRenewPeriod).isEqualTo(Duration.ofDays(90));
assertThat(info.proxyAccountId).isNull();
assertThat(info.proxyReceived).isEqualTo(Hbar.ZERO);
testEnv.close(accountId, key);
}
use of com.hedera.hashgraph.sdk.AccountInfoQuery in project hedera-sdk-java by hashgraph.
the class AccountCreateIntegrationTest method canCreateAccountWithNoInitialBalance.
@Test
@DisplayName("Can create account with no initial balance")
void canCreateAccountWithNoInitialBalance() throws Exception {
var testEnv = new IntegrationTestEnv(1);
var key = PrivateKey.generateED25519();
var response = new AccountCreateTransaction().setKey(key).execute(testEnv.client);
var accountId = Objects.requireNonNull(response.getReceipt(testEnv.client).accountId);
var info = new AccountInfoQuery().setAccountId(accountId).execute(testEnv.client);
assertThat(info.accountId).isEqualTo(accountId);
assertThat(info.isDeleted).isFalse();
assertThat(info.key.toString()).isEqualTo(key.getPublicKey().toString());
assertThat(info.balance).isEqualTo(new Hbar(0));
assertThat(info.autoRenewPeriod).isEqualTo(Duration.ofDays(90));
assertThat(info.proxyAccountId).isNull();
assertThat(info.proxyReceived).isEqualTo(Hbar.ZERO);
testEnv.close(accountId, key);
}
use of com.hedera.hashgraph.sdk.AccountInfoQuery in project hedera-sdk-java by hashgraph.
the class AccountAliasExample method main.
public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException {
Client client = Client.forName(HEDERA_NETWORK);
// Defaults the operator account ID and key such that all generated transactions will be paid for
// by this account and be signed by this key
client.setOperator(OPERATOR_ID, OPERATOR_KEY);
/*
* Hedera supports a form of lazy account creation.
*
* You can "create" an account by generating a private key, and then deriving the public key,
* without any need to interact with the Hedera network. The public key more or less acts as the user's
* account ID. This public key is an account's aliasKey: a public key that aliases (or will eventually alias)
* to a Hedera account.
*
* An AccountId takes one of two forms: a normal AccountId with a null aliasKey member takes the form 0.0.123,
* while an account ID with a non-null aliasKey member takes the form
* 0.0.302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf7777
* Note the prefix of "0.0." indicating the shard and realm. Also note that the aliasKey is stringified
* as a hex-encoded ASN1 DER representation of the key.
*
* An AccountId with an aliasKey can be used just like a normal AccountId for the purposes of queries and
* transactions, however most queries and transactions involving such an AccountId won't work until Hbar has
* been transferred to the aliasKey account.
*
* There is no record in the Hedera network of an account associated with a given aliasKey
* until an amount of Hbar is transferred to the account. The moment that Hbar is transferred to that aliasKey
* AccountId is the moment that that account actually begins to exist in the Hedera ledger.
*/
System.out.println("\"Creating\" a new account");
PrivateKey privateKey = PrivateKey.generateED25519();
PublicKey publicKey = privateKey.getPublicKey();
// Assuming that the target shard and realm are known.
// For now they are virtually always 0 and 0.
AccountId aliasAccountId = publicKey.toAccountId(0, 0);
System.out.println("New account ID: " + aliasAccountId);
System.out.println("Just the aliasKey: " + aliasAccountId.aliasKey);
/*
* Note that no queries or transactions have taken place yet.
* This account "creation" process is entirely local.
*
* AccountId.fromString() can construct an AccountId with an aliasKey.
* It expects a string of the form 0.0.123 in the case of a normal AccountId, or of the form
* 0.0.302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf7777
* in the case of an AccountId with aliasKey. Note the prefix of "0.0." to indicate the shard and realm.
*
* If the shard and realm are known, you may use PublicKey.fromString().toAccountId() to construct the
* aliasKey AccountId
*/
AccountId fromString = AccountId.fromString("0.0.302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf7777");
AccountId fromKeyString = PublicKey.fromString("302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf7777").toAccountId(0, 0);
System.out.println("Transferring some Hbar to the new account");
new TransferTransaction().addHbarTransfer(OPERATOR_ID, new Hbar(10).negated()).addHbarTransfer(aliasAccountId, new Hbar(10)).execute(client).getReceipt(client);
AccountBalance balance = new AccountBalanceQuery().setAccountId(aliasAccountId).execute(client);
System.out.println("Balances of the new account: " + balance);
AccountInfo info = new AccountInfoQuery().setAccountId(aliasAccountId).execute(client);
System.out.println("Info about the new account: " + info);
/*
* Note that once an account exists in the ledger, it is assigned a normal AccountId, which can be retrieved
* via an AccountInfoQuery.
*
* Users may continue to refer to the account by its aliasKey AccountId, but they may also
* now refer to it by its normal AccountId
*/
System.out.println("The normal account ID: " + info.accountId);
System.out.println("The alias key: " + info.aliasKey);
System.out.println("Example complete!");
client.close();
}
Aggregations