Search in sources :

Example 36 with Client

use of com.hedera.hashgraph.sdk.Client 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();
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) AccountId(com.hedera.hashgraph.sdk.AccountId) PublicKey(com.hedera.hashgraph.sdk.PublicKey) AccountBalance(com.hedera.hashgraph.sdk.AccountBalance) AccountInfoQuery(com.hedera.hashgraph.sdk.AccountInfoQuery) AccountBalanceQuery(com.hedera.hashgraph.sdk.AccountBalanceQuery) Hbar(com.hedera.hashgraph.sdk.Hbar) Client(com.hedera.hashgraph.sdk.Client) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountInfo(com.hedera.hashgraph.sdk.AccountInfo)

Example 37 with Client

use of com.hedera.hashgraph.sdk.Client in project hedera-sdk-java by hashgraph.

the class FileAppendChunkedExample 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);
    TransactionResponse transactionResponse = new FileCreateTransaction().setKeys(OPERATOR_KEY.getPublicKey()).setContents("Hello from Hedera.").setMaxTransactionFee(// 2 HBAR
    new Hbar(2)).execute(client);
    TransactionReceipt receipt = transactionResponse.getReceipt(client);
    FileId newFileId = Objects.requireNonNull(receipt.fileId);
    System.out.println("fileId: " + newFileId);
    StringBuilder contents = new StringBuilder();
    for (int i = 0; i <= 4096 * 9; i++) {
        contents.append("1");
    }
    TransactionReceipt fileAppendReceipt = new FileAppendTransaction().setNodeAccountIds(Collections.singletonList(transactionResponse.nodeId)).setFileId(newFileId).setContents(contents.toString()).setMaxChunks(40).setMaxTransactionFee(new Hbar(1000)).freezeWith(client).execute(client).getReceipt(client);
    System.out.println(fileAppendReceipt.toString());
    FileInfo info = new FileInfoQuery().setFileId(newFileId).execute(client);
    System.out.println("File size according to `FileInfoQuery`: " + info.size);
}
Also used : FileCreateTransaction(com.hedera.hashgraph.sdk.FileCreateTransaction) FileInfoQuery(com.hedera.hashgraph.sdk.FileInfoQuery) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) FileInfo(com.hedera.hashgraph.sdk.FileInfo) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) FileAppendTransaction(com.hedera.hashgraph.sdk.FileAppendTransaction) Hbar(com.hedera.hashgraph.sdk.Hbar) FileId(com.hedera.hashgraph.sdk.FileId) Client(com.hedera.hashgraph.sdk.Client)

Example 38 with Client

use of com.hedera.hashgraph.sdk.Client in project hedera-sdk-java by hashgraph.

the class GetAccountInfoExample method main.

public static void main(String[] args) throws PrecheckStatusException, TimeoutException {
    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);
    AccountInfo info = new AccountInfoQuery().setAccountId(client.getOperatorAccountId()).setQueryPayment(new Hbar(1)).execute(client);
    System.out.println("info.key                          = " + info.key);
    System.out.println("info.isReceiverSignatureRequired  = " + info.isReceiverSignatureRequired);
    System.out.println("info.expirationTime               = " + info.expirationTime);
}
Also used : AccountInfoQuery(com.hedera.hashgraph.sdk.AccountInfoQuery) Hbar(com.hedera.hashgraph.sdk.Hbar) Client(com.hedera.hashgraph.sdk.Client) AccountInfo(com.hedera.hashgraph.sdk.AccountInfo)

Example 39 with Client

use of com.hedera.hashgraph.sdk.Client in project hedera-sdk-java by hashgraph.

the class GetAddressBookExample method main.

public static void main(String[] args) throws PrecheckStatusException, IOException, TimeoutException {
    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);
    FileContentsQuery fileQuery = new FileContentsQuery().setFileId(FileId.ADDRESS_BOOK);
    Hbar cost = fileQuery.getCost(client);
    System.out.println("file contents cost: " + cost);
    fileQuery.setMaxQueryPayment(new Hbar(1));
    ByteString contents = fileQuery.execute(client);
    Files.deleteIfExists(FileSystems.getDefault().getPath("address-book.proto.bin"));
    Files.copy(new ByteArrayInputStream(contents.toByteArray()), FileSystems.getDefault().getPath("address-book.proto.bin"));
    // NEW (Feb 25 2022): you can now fetch the address book for free from a mirror node with AddressBookQuery
    NodeAddressBook addressBook = new AddressBookQuery().setFileId(FileId.ADDRESS_BOOK).execute(client);
    System.out.println(addressBook);
}
Also used : FileContentsQuery(com.hedera.hashgraph.sdk.FileContentsQuery) AddressBookQuery(com.hedera.hashgraph.sdk.AddressBookQuery) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteString(com.google.protobuf.ByteString) NodeAddressBook(com.hedera.hashgraph.sdk.NodeAddressBook) Hbar(com.hedera.hashgraph.sdk.Hbar) Client(com.hedera.hashgraph.sdk.Client)

Example 40 with Client

use of com.hedera.hashgraph.sdk.Client in project hedera-sdk-java by hashgraph.

the class GetFileContentsExample method main.

public static void main(String[] args) throws ReceiptStatusException, TimeoutException, PrecheckStatusException {
    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);
    // Content to be stored in the file
    byte[] fileContents = "Hedera is great!".getBytes(StandardCharsets.UTF_8);
    // Create the new file and set its properties
    TransactionResponse newFileTransactionResponse = new FileCreateTransaction().setKeys(// The public key of the owner of the file
    OPERATOR_KEY).setContents(// Contents of the file
    fileContents).setMaxTransactionFee(new Hbar(2)).execute(client);
    FileId newFileId = Objects.requireNonNull(newFileTransactionResponse.getReceipt(client).fileId);
    // Print the file ID to console
    System.out.println("The new file ID is " + newFileId.toString());
    // Get file contents
    ByteString contents = new FileContentsQuery().setFileId(newFileId).execute(client);
    // Prints query results to console
    System.out.println("File content query results: " + contents.toStringUtf8());
}
Also used : FileContentsQuery(com.hedera.hashgraph.sdk.FileContentsQuery) FileCreateTransaction(com.hedera.hashgraph.sdk.FileCreateTransaction) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) ByteString(com.google.protobuf.ByteString) Hbar(com.hedera.hashgraph.sdk.Hbar) FileId(com.hedera.hashgraph.sdk.FileId) Client(com.hedera.hashgraph.sdk.Client)

Aggregations

Client (com.hedera.hashgraph.sdk.Client)41 Hbar (com.hedera.hashgraph.sdk.Hbar)23 AccountId (com.hedera.hashgraph.sdk.AccountId)20 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)20 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)16 TransactionReceipt (com.hedera.hashgraph.sdk.TransactionReceipt)16 TransferTransaction (com.hedera.hashgraph.sdk.TransferTransaction)13 AccountCreateTransaction (com.hedera.hashgraph.sdk.AccountCreateTransaction)12 AccountBalanceQuery (com.hedera.hashgraph.sdk.AccountBalanceQuery)9 Var (com.google.errorprone.annotations.Var)6 FileCreateTransaction (com.hedera.hashgraph.sdk.FileCreateTransaction)6 FileId (com.hedera.hashgraph.sdk.FileId)6 KeyList (com.hedera.hashgraph.sdk.KeyList)6 PublicKey (com.hedera.hashgraph.sdk.PublicKey)6 TimeoutException (java.util.concurrent.TimeoutException)6 AccountDeleteTransaction (com.hedera.hashgraph.sdk.AccountDeleteTransaction)5 ByteString (com.google.protobuf.ByteString)4 AccountBalance (com.hedera.hashgraph.sdk.AccountBalance)4 ScheduleId (com.hedera.hashgraph.sdk.ScheduleId)4 ScheduleInfoQuery (com.hedera.hashgraph.sdk.ScheduleInfoQuery)4