Search in sources :

Example 21 with PrivateKey

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

the class ScheduledTransferExample 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);
    Objects.requireNonNull(client.getOperatorAccountId());
    /*
         * A scheduled transaction is a transaction that has been proposed by an account,
         * but which requires more signatures before it will actually execute on the Hedera network.
         *
         * For example, if Alice wants to transfer an amount of Hbar to Bob, and Bob has
         * receiverSignatureRequired set to true, then that transaction must be signed by
         * both Alice and Bob before the transaction will be executed.
         *
         * To solve this problem, Alice can propose the transaction by creating a scheduled
         * transaction on the Hedera network which, if executed, would transfer Hbar from
         * Alice to Bob.  That scheduled transaction will have a ScheduleId by which we can
         * refer to that scheduled transaction.  Alice can communicate the ScheduleId to Bob, and
         * then Bob can use a ScheduleSignTransaction to sign that scheduled transaction.
         *
         * Bob has a 30 minute window in which to sign the scheduled transaction, starting at the
         * moment that Alice creates the scheduled transaction.  If a scheduled transaction
         * is not signed by all of the necessary signatories within the 30 minute window,
         * that scheduled transaction will expire, and will not be executed.
         *
         * Once a scheduled transaction has all of the signatures necessary to execute, it will
         * be executed on the Hedera network automatically.  If you create a scheduled transaction
         * on the Hedera network, but that transaction only requires your signature in order to
         * execute and no one else's, that scheduled transaction will be automatically
         * executed immediately.
         */
    PrivateKey bobsKey = PrivateKey.generateED25519();
    AccountId bobsId = new AccountCreateTransaction().setReceiverSignatureRequired(true).setKey(bobsKey).setInitialBalance(new Hbar(10)).freezeWith(client).sign(bobsKey).execute(client).getReceipt(client).accountId;
    Objects.requireNonNull(bobsId);
    System.out.println("Alice's ID: " + client.getOperatorAccountId().toStringWithChecksum(client));
    System.out.println("Bob's ID: " + bobsId.toStringWithChecksum(client));
    AccountBalance bobsInitialBalance = new AccountBalanceQuery().setAccountId(bobsId).execute(client);
    System.out.println("Bob's initial balance:");
    System.out.println(bobsInitialBalance);
    TransferTransaction transferToSchedule = new TransferTransaction().addHbarTransfer(client.getOperatorAccountId(), new Hbar(-10)).addHbarTransfer(bobsId, new Hbar(10));
    System.out.println("Transfer to be scheduled:");
    System.out.println(transferToSchedule);
    /*
         * The payerAccountId is the account that will be charged the fee
         * for executing the scheduled transaction if/when it is executed.
         * That fee is separate from the fee that we will pay to execute the
         * ScheduleCreateTransaction itself.
         *
         * To clarify: Alice pays a fee to execute the ScheduleCreateTransaction,
         * which creates the scheduled transaction on the Hedera network.
         * She specifies when creating the scheduled transaction that Bob will pay
         * the fee for the scheduled transaction when it is executed.
         *
         * If payerAccountId is not specified, the account who creates the scheduled transaction
         * will be charged for executing the scheduled transaction.
         */
    ScheduleId scheduleId = new ScheduleCreateTransaction().setScheduledTransaction(transferToSchedule).setPayerAccountId(bobsId).execute(client).getReceipt(client).scheduleId;
    Objects.requireNonNull(scheduleId);
    System.out.println("The scheduleId is: " + scheduleId.toStringWithChecksum(client));
    /*
         * Bob's balance should be unchanged.  The transfer has been scheduled, but it hasn't been executed yet
         * because it requires Bob's signature.
         */
    AccountBalance bobsBalanceAfterSchedule = new AccountBalanceQuery().setAccountId(bobsId).execute(client);
    System.out.println("Bob's balance after scheduling the transfer (should be unchanged):");
    System.out.println(bobsBalanceAfterSchedule);
    /*
         * Once Alice has communicated the scheduleId to Bob, Bob can query for information about the
         * scheduled transaction.
         */
    ScheduleInfo scheduledTransactionInfo = new ScheduleInfoQuery().setScheduleId(scheduleId).execute(client);
    System.out.println("Info about scheduled transaction:");
    System.out.println(scheduledTransactionInfo);
    /*
         * getScheduledTransaction() will return an SDK Transaction object identical to the transaction
         * that was scheduled, which Bob can then inspect like a normal transaction.
         */
    Transaction<?> scheduledTransaction = scheduledTransactionInfo.getScheduledTransaction();
    // We happen to know that this transaction is (or certainly ought to be) a TransferTransaction
    if (scheduledTransaction instanceof TransferTransaction) {
        TransferTransaction scheduledTransfer = (TransferTransaction) scheduledTransaction;
        System.out.println("The scheduled transfer transaction from Bob's POV:");
        System.out.println(scheduledTransfer);
    } else {
        System.out.println("The scheduled transaction was not a transfer transaction.");
        System.out.println("Something has gone horribly wrong.  Crashing...");
        System.exit(-1);
    }
    new ScheduleSignTransaction().setScheduleId(scheduleId).freezeWith(client).sign(bobsKey).execute(client).getReceipt(client);
    AccountBalance balanceAfterSigning = new AccountBalanceQuery().setAccountId(bobsId).execute(client);
    System.out.println("Bob's balance after signing the scheduled transaction:");
    System.out.println(balanceAfterSigning);
    ScheduleInfo postTransactionInfo = new ScheduleInfoQuery().setScheduleId(scheduleId).execute(client);
    System.out.println("Info on the scheduled transaction, executedAt should no longer be null:");
    System.out.println(postTransactionInfo);
    // Clean up
    new AccountDeleteTransaction().setTransferAccountId(client.getOperatorAccountId()).setAccountId(bobsId).freezeWith(client).sign(bobsKey).execute(client).getReceipt(client);
    client.close();
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) AccountId(com.hedera.hashgraph.sdk.AccountId) ScheduleSignTransaction(com.hedera.hashgraph.sdk.ScheduleSignTransaction) AccountBalanceQuery(com.hedera.hashgraph.sdk.AccountBalanceQuery) AccountDeleteTransaction(com.hedera.hashgraph.sdk.AccountDeleteTransaction) Hbar(com.hedera.hashgraph.sdk.Hbar) ScheduleInfoQuery(com.hedera.hashgraph.sdk.ScheduleInfoQuery) ScheduleId(com.hedera.hashgraph.sdk.ScheduleId) ScheduleInfo(com.hedera.hashgraph.sdk.ScheduleInfo) AccountBalance(com.hedera.hashgraph.sdk.AccountBalance) Client(com.hedera.hashgraph.sdk.Client) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) ScheduleCreateTransaction(com.hedera.hashgraph.sdk.ScheduleCreateTransaction)

Example 22 with PrivateKey

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

the class UpdateAccountPublicKeyExample 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);
    client.setDefaultMaxTransactionFee(new Hbar(10));
    // First, we create a new account so we don't affect our account
    PrivateKey key1 = PrivateKey.generateED25519();
    PrivateKey key2 = PrivateKey.generateED25519();
    TransactionResponse acctTransactionResponse = new AccountCreateTransaction().setKey(key1.getPublicKey()).setInitialBalance(new Hbar(1)).execute(client);
    System.out.println("transaction ID: " + acctTransactionResponse);
    AccountId accountId = Objects.requireNonNull(acctTransactionResponse.getReceipt(client).accountId);
    System.out.println("account = " + accountId);
    System.out.println("key = " + key1.getPublicKey());
    // Next, we update the key
    System.out.println(" :: update public key of account " + accountId);
    System.out.println("set key = " + key2.getPublicKey());
    TransactionResponse accountUpdateTransactionResponse = new AccountUpdateTransaction().setAccountId(accountId).setKey(key2.getPublicKey()).freezeWith(client).sign(key1).sign(key2).execute(client);
    System.out.println("transaction ID: " + accountUpdateTransactionResponse);
    // (important!) wait for the transaction to complete by querying the receipt
    accountUpdateTransactionResponse.getReceipt(client);
    // Now we fetch the account information to check if the key was changed
    System.out.println(" :: getAccount and check our current key");
    AccountInfo info = new AccountInfoQuery().setAccountId(accountId).execute(client);
    System.out.println("key = " + info.key);
}
Also used : AccountUpdateTransaction(com.hedera.hashgraph.sdk.AccountUpdateTransaction) PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) AccountId(com.hedera.hashgraph.sdk.AccountId) AccountInfoQuery(com.hedera.hashgraph.sdk.AccountInfoQuery) Hbar(com.hedera.hashgraph.sdk.Hbar) Client(com.hedera.hashgraph.sdk.Client) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) AccountInfo(com.hedera.hashgraph.sdk.AccountInfo)

Example 23 with PrivateKey

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

the class TokenTransferIntegrationTest method insufficientBalanceForFee.

@Test
@DisplayName("Cannot transfer tokens if balance is insufficient to pay fee")
void insufficientBalanceForFee() throws Exception {
    var testEnv = new IntegrationTestEnv(1).useThrowawayAccount();
    PrivateKey key1 = PrivateKey.generateED25519();
    PrivateKey key2 = PrivateKey.generateED25519();
    var accountId1 = new AccountCreateTransaction().setKey(key1).setInitialBalance(new Hbar(2)).execute(testEnv.client).getReceipt(testEnv.client).accountId;
    var accountId2 = new AccountCreateTransaction().setKey(key2).setInitialBalance(new Hbar(2)).execute(testEnv.client).getReceipt(testEnv.client).accountId;
    var tokenId = new TokenCreateTransaction().setTokenName("ffff").setTokenSymbol("F").setInitialSupply(1).setCustomFees(Collections.singletonList(new CustomFixedFee().setAmount(5000_000_000L).setFeeCollectorAccountId(testEnv.operatorId))).setTreasuryAccountId(testEnv.operatorId).setAdminKey(testEnv.operatorKey).setFeeScheduleKey(testEnv.operatorKey).execute(testEnv.client).getReceipt(testEnv.client).tokenId;
    new TokenAssociateTransaction().setAccountId(accountId1).setTokenIds(Collections.singletonList(tokenId)).freezeWith(testEnv.client).sign(key1).execute(testEnv.client).getReceipt(testEnv.client);
    new TokenAssociateTransaction().setAccountId(accountId2).setTokenIds(Collections.singletonList(tokenId)).freezeWith(testEnv.client).sign(key2).execute(testEnv.client).getReceipt(testEnv.client);
    new TransferTransaction().addTokenTransfer(tokenId, testEnv.operatorId, -1).addTokenTransfer(tokenId, accountId1, 1).freezeWith(testEnv.client).sign(key1).execute(testEnv.client).getReceipt(testEnv.client);
    assertThatExceptionOfType(ReceiptStatusException.class).isThrownBy(() -> {
        new TransferTransaction().addTokenTransfer(tokenId, accountId1, -1).addTokenTransfer(tokenId, accountId2, 1).freezeWith(testEnv.client).sign(key1).sign(key2).execute(testEnv.client).getReceipt(testEnv.client);
    }).satisfies(error -> assertThat(error.getMessage()).containsAnyOf(Status.INSUFFICIENT_SENDER_ACCOUNT_BALANCE_FOR_CUSTOM_FEE.toString(), Status.INSUFFICIENT_PAYER_BALANCE_FOR_CUSTOM_FEE.toString()));
    testEnv.wipeAccountHbars(accountId1, key1);
    testEnv.wipeAccountHbars(accountId2, key2);
    testEnv.close();
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) TokenAssociateTransaction(com.hedera.hashgraph.sdk.TokenAssociateTransaction) CustomFixedFee(com.hedera.hashgraph.sdk.CustomFixedFee) Hbar(com.hedera.hashgraph.sdk.Hbar) TokenCreateTransaction(com.hedera.hashgraph.sdk.TokenCreateTransaction) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 24 with PrivateKey

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

the class CreateAccountExample 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);
    // Generate a Ed25519 private, public key pair
    PrivateKey newKey = PrivateKey.generateED25519();
    PublicKey newPublicKey = newKey.getPublicKey();
    System.out.println("private key = " + newKey);
    System.out.println("public key = " + newPublicKey);
    TransactionResponse transactionResponse = new AccountCreateTransaction().setKey(newPublicKey).setInitialBalance(Hbar.fromTinybars(1000)).execute(client);
    // This will wait for the receipt to become available
    TransactionReceipt receipt = transactionResponse.getReceipt(client);
    AccountId newAccountId = receipt.accountId;
    System.out.println("account = " + newAccountId);
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) AccountId(com.hedera.hashgraph.sdk.AccountId) PublicKey(com.hedera.hashgraph.sdk.PublicKey) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) Client(com.hedera.hashgraph.sdk.Client) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction)

Example 25 with PrivateKey

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

the class CreateAccountThresholdKeyExample method main.

public static void main(String[] args) throws PrecheckStatusException, TimeoutException, 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);
    // Generate three new Ed25519 private, public key pairs.
    // You do not need the private keys to create the Threshold Key List,
    // you only need the public keys, and if you're doing things correctly,
    // you probably shouldn't have these private keys.
    PrivateKey[] privateKeys = new PrivateKey[3];
    PublicKey[] publicKeys = new PublicKey[3];
    for (int i = 0; i < 3; i++) {
        PrivateKey key = PrivateKey.generateED25519();
        privateKeys[i] = key;
        publicKeys[i] = key.getPublicKey();
    }
    System.out.println("public keys: ");
    for (Key key : publicKeys) {
        System.out.println(key);
    }
    // require 2 of the 3 keys we generated to sign on anything modifying this account
    KeyList transactionKey = KeyList.withThreshold(2);
    Collections.addAll(transactionKey, publicKeys);
    TransactionResponse transactionResponse = new AccountCreateTransaction().setKey(transactionKey).setInitialBalance(new Hbar(10)).execute(client);
    // This will wait for the receipt to become available
    TransactionReceipt receipt = transactionResponse.getReceipt(client);
    AccountId newAccountId = Objects.requireNonNull(receipt.accountId);
    System.out.println("account = " + newAccountId);
    TransactionResponse transferTransactionResponse = new TransferTransaction().addHbarTransfer(newAccountId, new Hbar(10).negated()).addHbarTransfer(new AccountId(3), new Hbar(10)).freezeWith(client).sign(privateKeys[0]).sign(privateKeys[1]).execute(client);
    // (important!) wait for the transfer to go to consensus
    transferTransactionResponse.getReceipt(client);
    Hbar balanceAfter = new AccountBalanceQuery().setAccountId(newAccountId).execute(client).hbars;
    System.out.println("account balance after transfer: " + balanceAfter);
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) AccountId(com.hedera.hashgraph.sdk.AccountId) PublicKey(com.hedera.hashgraph.sdk.PublicKey) KeyList(com.hedera.hashgraph.sdk.KeyList) AccountBalanceQuery(com.hedera.hashgraph.sdk.AccountBalanceQuery) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) Hbar(com.hedera.hashgraph.sdk.Hbar) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) Client(com.hedera.hashgraph.sdk.Client) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) Key(com.hedera.hashgraph.sdk.Key) PublicKey(com.hedera.hashgraph.sdk.PublicKey) PrivateKey(com.hedera.hashgraph.sdk.PrivateKey)

Aggregations

PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)28 AccountCreateTransaction (com.hedera.hashgraph.sdk.AccountCreateTransaction)16 AccountId (com.hedera.hashgraph.sdk.AccountId)16 Client (com.hedera.hashgraph.sdk.Client)15 Hbar (com.hedera.hashgraph.sdk.Hbar)13 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)13 TransferTransaction (com.hedera.hashgraph.sdk.TransferTransaction)13 TransactionReceipt (com.hedera.hashgraph.sdk.TransactionReceipt)11 KeyList (com.hedera.hashgraph.sdk.KeyList)10 PublicKey (com.hedera.hashgraph.sdk.PublicKey)9 Var (com.google.errorprone.annotations.Var)7 AccountBalanceQuery (com.hedera.hashgraph.sdk.AccountBalanceQuery)6 AccountDeleteTransaction (com.hedera.hashgraph.sdk.AccountDeleteTransaction)6 ScheduleId (com.hedera.hashgraph.sdk.ScheduleId)6 ScheduleSignTransaction (com.hedera.hashgraph.sdk.ScheduleSignTransaction)6 ScheduleCreateTransaction (com.hedera.hashgraph.sdk.ScheduleCreateTransaction)5 ScheduleInfoQuery (com.hedera.hashgraph.sdk.ScheduleInfoQuery)5 ScheduleInfo (com.hedera.hashgraph.sdk.ScheduleInfo)4 TokenAssociateTransaction (com.hedera.hashgraph.sdk.TokenAssociateTransaction)4 TokenCreateTransaction (com.hedera.hashgraph.sdk.TokenCreateTransaction)4